paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@83 | 3 | """ |
paul@305 | 4 | Program data structures. There are two separate kinds of structures: those with |
paul@305 | 5 | context, which are the values manipulated by programs, and those without |
paul@305 | 6 | context, which are typically constant things which are stored alongside the |
paul@305 | 7 | program but which are wrapped in context-dependent structures in the running |
paul@305 | 8 | program. |
paul@83 | 9 | |
paul@488 | 10 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie <paul@boddie.org.uk> |
paul@83 | 11 | |
paul@83 | 12 | This program is free software; you can redistribute it and/or modify it under |
paul@83 | 13 | the terms of the GNU General Public License as published by the Free Software |
paul@83 | 14 | Foundation; either version 3 of the License, or (at your option) any later |
paul@83 | 15 | version. |
paul@83 | 16 | |
paul@83 | 17 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@83 | 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@83 | 19 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@83 | 20 | details. |
paul@83 | 21 | |
paul@83 | 22 | You should have received a copy of the GNU General Public License along with |
paul@83 | 23 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@83 | 24 | |
paul@83 | 25 | -------- |
paul@83 | 26 | |
paul@305 | 27 | The principal value structure class in this module is the Attr class which |
paul@305 | 28 | represents attributes of objects and retains the context of each reference to an |
paul@305 | 29 | attribute. This class models program behaviour at run-time. |
paul@305 | 30 | |
paul@190 | 31 | The central data structure classes in this module are the following: |
paul@83 | 32 | |
paul@83 | 33 | * Class |
paul@83 | 34 | * Function |
paul@83 | 35 | * Module |
paul@83 | 36 | |
paul@83 | 37 | All of the above support the Naming interface either explicitly or through |
paul@83 | 38 | general conformance, meaning that all can be asked to provide their 'full_name' |
paul@83 | 39 | using the method of that name. |
paul@83 | 40 | |
paul@83 | 41 | Additionally, all of the above also support a dictionary interface in order to |
paul@83 | 42 | access names within their defined scopes. Specific methods also exist in order |
paul@83 | 43 | to distinguish between certain kinds of attributes: |
paul@83 | 44 | |
paul@190 | 45 | * Class: class_attributes, all_class_attributes, instance_attributes, all_attributes |
paul@83 | 46 | * Function: parameters, locals, all_locals |
paul@83 | 47 | * Module: module_attributes |
paul@83 | 48 | |
paul@83 | 49 | These specific methods are useful in certain situations. |
paul@83 | 50 | |
paul@92 | 51 | The above classes also provide an 'astnode' attribute, indicating the AST node |
paul@92 | 52 | where each such object is defined. |
paul@83 | 53 | """ |
paul@83 | 54 | |
paul@428 | 55 | from micropython.program import ReplaceableContext, PlaceholderContext |
paul@554 | 56 | from micropython.basicdata import * |
paul@555 | 57 | from micropython.errors import * |
paul@554 | 58 | from micropython.objectset import * |
paul@555 | 59 | from micropython.types import * |
paul@507 | 60 | import sys |
paul@83 | 61 | |
paul@555 | 62 | try: |
paul@555 | 63 | set |
paul@555 | 64 | except NameError: |
paul@555 | 65 | from sets import Set as set |
paul@555 | 66 | |
paul@83 | 67 | class NamespaceDict: |
paul@83 | 68 | |
paul@83 | 69 | "A mix-in providing dictionary methods." |
paul@83 | 70 | |
paul@180 | 71 | def __init__(self, module=None): |
paul@343 | 72 | self.module = module |
paul@83 | 73 | self.namespace = {} |
paul@83 | 74 | self.globals = set() |
paul@351 | 75 | self.lambdas = {} # only really useful for functions |
paul@92 | 76 | self.finalised = 0 |
paul@83 | 77 | |
paul@272 | 78 | # Attributes accessed on objects, potentially narrowing their types. |
paul@272 | 79 | # Specific namespaces should define known names during initialisation. |
paul@280 | 80 | # For example, functions can define names belonging to parameters. |
paul@272 | 81 | |
paul@305 | 82 | # Attribute users, defining names which use attributes. |
paul@305 | 83 | |
paul@379 | 84 | self.attribute_users = [{}] # stack of assignments and branches |
paul@379 | 85 | self.attribute_user_shelves = [] |
paul@305 | 86 | |
paul@387 | 87 | # Suspended user details plus loop details. |
paul@387 | 88 | |
paul@388 | 89 | self.suspended_broken_users = [] # stack of lists of user dicts |
paul@388 | 90 | self.suspended_continuing_users = [] # stack of lists of user dicts |
paul@387 | 91 | |
paul@343 | 92 | # Scope usage, indicating the origin of names. |
paul@343 | 93 | |
paul@343 | 94 | self.scope_usage = [{}] # stack of scope usage |
paul@343 | 95 | self.scope_shelves = [] |
paul@343 | 96 | |
paul@540 | 97 | # Abandoned usage, useful for reviving usage for exception handlers. |
paul@540 | 98 | |
paul@540 | 99 | self.abandoned_users = [[]] # stack of lists of users |
paul@540 | 100 | |
paul@305 | 101 | # Define attribute usage to identify active program sections. |
paul@343 | 102 | # Attribute users are AST nodes defining names. |
paul@305 | 103 | |
paul@309 | 104 | self.all_attribute_users = set() |
paul@272 | 105 | |
paul@272 | 106 | # Attribute/name definition and access. |
paul@272 | 107 | |
paul@167 | 108 | def __delitem__(self, name): |
paul@167 | 109 | del self.namespace[name] |
paul@167 | 110 | |
paul@167 | 111 | def has_key(self, name): |
paul@167 | 112 | return self.namespace.has_key(name) |
paul@167 | 113 | |
paul@167 | 114 | def keys(self): |
paul@167 | 115 | return self.namespace.keys() |
paul@167 | 116 | |
paul@167 | 117 | def values(self): |
paul@167 | 118 | return self.namespace.values() |
paul@167 | 119 | |
paul@167 | 120 | def items(self): |
paul@167 | 121 | return self.namespace.items() |
paul@167 | 122 | |
paul@83 | 123 | def __getitem__(self, name): |
paul@83 | 124 | return self.namespace[name] |
paul@83 | 125 | |
paul@348 | 126 | def get(self, name, default=None): |
paul@348 | 127 | return self.namespace.get(name, default) |
paul@348 | 128 | |
paul@351 | 129 | # Administrative methods. |
paul@351 | 130 | |
paul@437 | 131 | def finalise(self, objtable): |
paul@437 | 132 | self.finalise_attributes() |
paul@495 | 133 | self.finalise_users(objtable) |
paul@437 | 134 | |
paul@351 | 135 | def items_for_vacuum(self): |
paul@351 | 136 | return self.items() + self.lambdas.items() |
paul@351 | 137 | |
paul@351 | 138 | def vacuum_item(self, name): |
paul@363 | 139 | if self.has_key(name): |
paul@363 | 140 | del self[name] |
paul@394 | 141 | return 1 |
paul@394 | 142 | else: |
paul@394 | 143 | return 0 |
paul@351 | 144 | |
paul@351 | 145 | def add_lambda(self, obj): |
paul@351 | 146 | attr = Attr(None, self, obj.name) |
paul@351 | 147 | attr.update([self.get_context_and_value(obj)], single_assignment=1) |
paul@351 | 148 | self.lambdas[obj.name] = attr |
paul@351 | 149 | |
paul@348 | 150 | # Specialised access methods. |
paul@348 | 151 | |
paul@343 | 152 | def get_using_node(self, name, node): |
paul@343 | 153 | |
paul@343 | 154 | """ |
paul@343 | 155 | Access the given 'name' through this namespace, making use of the module |
paul@343 | 156 | and builtins namespaces if necessary, annotating the given 'node' with |
paul@343 | 157 | the scope involved. |
paul@343 | 158 | """ |
paul@343 | 159 | |
paul@343 | 160 | attr, scope, full_name = self._get_with_scope(name) |
paul@343 | 161 | |
paul@343 | 162 | if scope is not None: |
paul@343 | 163 | node._scope = scope |
paul@343 | 164 | self.note_scope(name, scope) |
paul@343 | 165 | |
paul@551 | 166 | if full_name is not None and (scope != "local" or isinstance(self, (Class, Module))): |
paul@343 | 167 | self.use_specific_attribute(full_name, name) |
paul@343 | 168 | |
paul@343 | 169 | return attr |
paul@343 | 170 | |
paul@343 | 171 | def _get_with_scope(self, name, external=0): |
paul@343 | 172 | |
paul@348 | 173 | """ |
paul@348 | 174 | Find the source of the given 'name', returning the attribute object, |
paul@348 | 175 | scope (constant, local, global or builtins), and the full name of the |
paul@348 | 176 | source namespace (or None for constants). |
paul@348 | 177 | |
paul@348 | 178 | If the optional 'external' flag is set to a true value, only external |
paul@348 | 179 | (non-local) namespaces will be involved in the search. |
paul@348 | 180 | """ |
paul@348 | 181 | |
paul@343 | 182 | module = self.module |
paul@343 | 183 | builtins = module and module.builtins or None |
paul@343 | 184 | importer = module and module.importer or None |
paul@343 | 185 | |
paul@343 | 186 | # Constants. |
paul@343 | 187 | |
paul@343 | 188 | if importer is not None and importer.predefined_constants.has_key(name): |
paul@343 | 189 | return importer.get_predefined_constant(name), "constant", None |
paul@343 | 190 | |
paul@343 | 191 | # Locals. |
paul@343 | 192 | |
paul@343 | 193 | elif not external and self.has_key(name): |
paul@343 | 194 | return self[name], "local", self.full_name() |
paul@343 | 195 | |
paul@343 | 196 | # Globals. |
paul@343 | 197 | |
paul@344 | 198 | elif module is not None and module is not self and module.has_key(name): |
paul@343 | 199 | return module[name], "global", module.full_name() |
paul@343 | 200 | |
paul@343 | 201 | # Builtins. |
paul@343 | 202 | |
paul@343 | 203 | elif builtins is not None and builtins.has_key(name): |
paul@343 | 204 | return builtins[name], "builtins", builtins.full_name() |
paul@343 | 205 | |
paul@343 | 206 | # Unknown. |
paul@343 | 207 | |
paul@343 | 208 | else: |
paul@343 | 209 | return None, None, None |
paul@343 | 210 | |
paul@348 | 211 | # Attribute definition methods. |
paul@83 | 212 | |
paul@83 | 213 | def __setitem__(self, name, value): |
paul@83 | 214 | self.set(name, value) |
paul@83 | 215 | |
paul@83 | 216 | def set(self, name, value, single_assignment=1): |
paul@83 | 217 | |
paul@83 | 218 | """ |
paul@83 | 219 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@83 | 220 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@83 | 221 | this operation (or whether the operation covers potentially many |
paul@83 | 222 | assignments in the lifetime of a program). |
paul@83 | 223 | """ |
paul@83 | 224 | |
paul@442 | 225 | if value is None: |
paul@507 | 226 | print >>sys.stderr, "Warning: name %r in namespace %r has an unknown value (evaluated to None)." % (name, self.full_name()) |
paul@448 | 227 | value = make_instance() |
paul@442 | 228 | |
paul@83 | 229 | if name in self.globals: |
paul@180 | 230 | self.module.set(name, value, 0) |
paul@83 | 231 | else: |
paul@192 | 232 | self._set(name, value, single_assignment) |
paul@362 | 233 | self.define_scope(name, "local") |
paul@83 | 234 | |
paul@83 | 235 | def set_module(self, name, value): |
paul@83 | 236 | |
paul@83 | 237 | """ |
paul@83 | 238 | A specialised set operation, making 'name' refer to 'value' in the |
paul@83 | 239 | context of making a module reference available in association with |
paul@83 | 240 | 'name' as part of the import of that module or a submodule of that |
paul@83 | 241 | module. |
paul@83 | 242 | """ |
paul@83 | 243 | |
paul@192 | 244 | self._set(name, value, 1) |
paul@83 | 245 | |
paul@192 | 246 | def _set(self, name, attr_or_value, single_assignment=1): |
paul@83 | 247 | |
paul@187 | 248 | """ |
paul@192 | 249 | The underlying set operation associating 'name' with the given |
paul@192 | 250 | 'attr_or_value'. |
paul@187 | 251 | See: docs/assignment.txt |
paul@187 | 252 | """ |
paul@83 | 253 | |
paul@192 | 254 | # Add and/or obtain the namespace entry. |
paul@87 | 255 | |
paul@192 | 256 | if not self.namespace.has_key(name): |
paul@192 | 257 | self.namespace[name] = Attr(None, self, name) |
paul@120 | 258 | |
paul@192 | 259 | attr = self.namespace[name] |
paul@329 | 260 | self._set_using_attr(attr, attr_or_value, single_assignment) |
paul@329 | 261 | |
paul@329 | 262 | def _set_using_attr(self, attr, attr_or_value, single_assignment=1): |
paul@187 | 263 | |
paul@192 | 264 | # Handle attribute assignment as well as assignment of basic objects. |
paul@237 | 265 | # Attempt to fix the context if not explicitly defined. |
paul@192 | 266 | |
paul@192 | 267 | if isinstance(attr_or_value, Attr): |
paul@237 | 268 | context_values = self.get_updated_context_values(attr_or_value.context_values) |
paul@237 | 269 | else: |
paul@237 | 270 | context_values = self.get_updated_context_values([self.get_context_and_value(attr_or_value)]) |
paul@187 | 271 | |
paul@237 | 272 | attr.update(context_values, single_assignment) |
paul@237 | 273 | |
paul@237 | 274 | def get_context_and_value(self, value): |
paul@237 | 275 | |
paul@237 | 276 | "Return a context, value tuple for the given 'value'." |
paul@237 | 277 | |
paul@237 | 278 | # Functions have a replaceable context. |
paul@120 | 279 | |
paul@237 | 280 | if isinstance(value, Function): |
paul@237 | 281 | return (ReplaceableContext, value) |
paul@237 | 282 | |
paul@237 | 283 | # Classes use placeholder contexts which cannot be replaced but which |
paul@237 | 284 | # do not communicate useful contextual information. |
paul@237 | 285 | |
paul@237 | 286 | elif isinstance(value, Class): |
paul@237 | 287 | return (PlaceholderContext, value) |
paul@237 | 288 | |
paul@237 | 289 | # Other values employ themselves as the context. |
paul@87 | 290 | |
paul@192 | 291 | else: |
paul@237 | 292 | return (value, value) |
paul@83 | 293 | |
paul@192 | 294 | def get_updated_context_values(self, context_values): |
paul@87 | 295 | |
paul@119 | 296 | """ |
paul@192 | 297 | Adapt the contexts found in the given 'context_values', returning a new |
paul@192 | 298 | set. |
paul@187 | 299 | See: docs/assignment.txt |
paul@119 | 300 | """ |
paul@87 | 301 | |
paul@192 | 302 | results = set() |
paul@192 | 303 | |
paul@192 | 304 | for context, value in context_values: |
paul@192 | 305 | |
paul@192 | 306 | # Set the context of instances to themselves. |
paul@187 | 307 | |
paul@192 | 308 | if isinstance(value, Instance): |
paul@192 | 309 | results.add((value, value)) |
paul@192 | 310 | else: |
paul@192 | 311 | results.add((context, value)) |
paul@192 | 312 | |
paul@192 | 313 | return results |
paul@87 | 314 | |
paul@83 | 315 | def make_global(self, name): |
paul@306 | 316 | |
paul@306 | 317 | "Declare 'name' as a global in the current namespace." |
paul@306 | 318 | |
paul@83 | 319 | if not self.namespace.has_key(name): |
paul@83 | 320 | self.globals.add(name) |
paul@362 | 321 | self.define_scope(name, "global") |
paul@178 | 322 | return 1 |
paul@83 | 323 | else: |
paul@178 | 324 | return 0 |
paul@83 | 325 | |
paul@272 | 326 | # Attribute positioning. |
paul@272 | 327 | |
paul@83 | 328 | def attributes_as_list(self): |
paul@92 | 329 | |
paul@92 | 330 | "Return the attributes in a list." |
paul@92 | 331 | |
paul@83 | 332 | self.finalise_attributes() |
paul@83 | 333 | l = [None] * len(self.keys()) |
paul@83 | 334 | for attr in self.values(): |
paul@83 | 335 | l[attr.position] = attr |
paul@83 | 336 | return l |
paul@83 | 337 | |
paul@287 | 338 | def finalise_attributes(self): |
paul@83 | 339 | |
paul@83 | 340 | "Make sure all attributes are fully defined." |
paul@83 | 341 | |
paul@92 | 342 | if self.finalised: |
paul@92 | 343 | return |
paul@92 | 344 | |
paul@83 | 345 | # The default action is to assign attribute positions sequentially. |
paul@83 | 346 | |
paul@83 | 347 | for i, attr in enumerate(self.values()): |
paul@83 | 348 | attr.position = i |
paul@83 | 349 | |
paul@92 | 350 | self.finalised = 1 |
paul@92 | 351 | |
paul@282 | 352 | def unfinalise_attributes(self): |
paul@282 | 353 | |
paul@282 | 354 | "Open attribute definitions to editing and subsequent finalisation." |
paul@282 | 355 | |
paul@282 | 356 | self.finalised = 0 |
paul@282 | 357 | |
paul@272 | 358 | # Attribute usage methods. |
paul@272 | 359 | |
paul@344 | 360 | def finalise_attribute_usage(self): |
paul@344 | 361 | |
paul@344 | 362 | "Propagate attribute usage for the namespace to the importer." |
paul@344 | 363 | |
paul@344 | 364 | module = self.module |
paul@344 | 365 | importer = module and module.importer |
paul@344 | 366 | |
paul@344 | 367 | if importer is not None: |
paul@381 | 368 | |
paul@381 | 369 | # Visit each user and examine the attribute usage for each name. |
paul@381 | 370 | |
paul@381 | 371 | for user in self.all_attribute_users: |
paul@381 | 372 | |
paul@381 | 373 | # First, visit the contributors and combine their attribute |
paul@381 | 374 | # usage with the usage recorded directly on the user. |
paul@381 | 375 | |
paul@508 | 376 | self.get_usage_from_contributors(user) |
paul@381 | 377 | |
paul@381 | 378 | # Record the defining user on each contributor. |
paul@381 | 379 | |
paul@508 | 380 | for contributor in user._attrcontributors: |
paul@381 | 381 | contributor._attrdefs.append(user) |
paul@381 | 382 | |
paul@381 | 383 | # Then, tell the importer about the usage. |
paul@381 | 384 | |
paul@384 | 385 | for name in user._attrnames.keys(): |
paul@384 | 386 | |
paul@384 | 387 | # Only provide information about names defined by this user. |
paul@384 | 388 | |
paul@508 | 389 | usage = user._attrcombined.get(name, []) |
paul@384 | 390 | |
paul@384 | 391 | # Skip reporting where no actual usage occurs. |
paul@384 | 392 | |
paul@476 | 393 | if usage is None: |
paul@384 | 394 | continue |
paul@384 | 395 | |
paul@384 | 396 | # Eliminate non-usage. |
paul@384 | 397 | |
paul@384 | 398 | importer.use_names(user, name, tuple([attrnames for attrnames in usage if attrnames]), self.full_name()) |
paul@379 | 399 | |
paul@478 | 400 | def finalise_users(self, objtable): |
paul@478 | 401 | |
paul@478 | 402 | "Record the object types for generating guards." |
paul@478 | 403 | |
paul@478 | 404 | # Visit each user and examine the attribute usage for each name. |
paul@478 | 405 | |
paul@478 | 406 | for user in self.all_attribute_users: |
paul@489 | 407 | user._attrtypes = self._deduce_types(user._attrcombined, objtable) |
paul@488 | 408 | self._finalise_contributor(user, objtable) |
paul@488 | 409 | |
paul@490 | 410 | def _finalise_contributors(self, node, objtable): |
paul@490 | 411 | |
paul@490 | 412 | """ |
paul@490 | 413 | Visit the contributing branches of 'node', finalising them using the |
paul@490 | 414 | given 'objtable'. |
paul@490 | 415 | """ |
paul@490 | 416 | |
paul@490 | 417 | for contributor in node._attrbranches: |
paul@490 | 418 | self._finalise_contributor(contributor, objtable) |
paul@490 | 419 | |
paul@488 | 420 | def _finalise_contributor(self, node, objtable): |
paul@488 | 421 | |
paul@488 | 422 | """ |
paul@488 | 423 | Record the specific object types being used in various regions of a |
paul@488 | 424 | program unit. |
paul@488 | 425 | """ |
paul@488 | 426 | |
paul@490 | 427 | if not hasattr(node, "_attrspecifictypes"): |
paul@489 | 428 | merged = {} |
paul@503 | 429 | |
paul@503 | 430 | # Get the combined usage information from the user definitions. |
paul@503 | 431 | |
paul@503 | 432 | for user in node._attrdefs or [node]: |
paul@503 | 433 | |
paul@503 | 434 | # Filter the usage for each name using the local usage |
paul@503 | 435 | # information. |
paul@503 | 436 | |
paul@503 | 437 | for name, usage in user._attrcombined.items(): |
paul@503 | 438 | localusage = node._attrnames.get(name) |
paul@503 | 439 | |
paul@503 | 440 | if usage and localusage: |
paul@503 | 441 | if not merged.has_key(name): |
paul@503 | 442 | merged[name] = ObjectSet() |
paul@503 | 443 | |
paul@503 | 444 | for attrnames, value in usage.items(): |
paul@503 | 445 | if attrnames and localusage.issubset(attrnames): |
paul@503 | 446 | merged[name][attrnames] = value |
paul@503 | 447 | |
paul@503 | 448 | node._attrmerged = merged |
paul@490 | 449 | node._attrspecifictypes = self._deduce_types(node._attrmerged, objtable) |
paul@490 | 450 | |
paul@490 | 451 | self._finalise_contributors(node, objtable) |
paul@489 | 452 | |
paul@489 | 453 | def _deduce_types(self, usage, objtable): |
paul@488 | 454 | |
paul@488 | 455 | """ |
paul@489 | 456 | Deduce the types for names from the given attribute 'usage' and using |
paul@489 | 457 | the given 'objtable'. |
paul@488 | 458 | """ |
paul@488 | 459 | |
paul@488 | 460 | attrtypes = {} |
paul@489 | 461 | for name, combined_usage in usage.items(): |
paul@489 | 462 | if combined_usage is not None: |
paul@525 | 463 | objtypes = get_object_types_for_usage(combined_usage, objtable, name, self.full_name(), True) |
paul@532 | 464 | if objtypes: |
paul@532 | 465 | if isinstance(self, Function) and self.is_method() and name == "self": |
paul@532 | 466 | objtypes = filter_using_self(objtypes, self.parent) |
paul@522 | 467 | attrtypes[name] = objtypes |
paul@488 | 468 | return attrtypes |
paul@478 | 469 | |
paul@379 | 470 | def get_usage_from_contributors(self, node): |
paul@379 | 471 | |
paul@379 | 472 | """ |
paul@379 | 473 | Obtain usage information from the given 'node', combined with usage |
paul@381 | 474 | details from its contributors, returning a tuple containing a set of all |
paul@381 | 475 | contributors employed along with a dictionary mapping names to lists of |
paul@381 | 476 | usage possibilities (each a collection of attribute names). |
paul@379 | 477 | """ |
paul@379 | 478 | |
paul@508 | 479 | unfinished = {} |
paul@508 | 480 | |
paul@384 | 481 | if not hasattr(node, "_attrcombined"): |
paul@508 | 482 | node._attrcombined = None |
paul@528 | 483 | node._attrcontributors = None |
paul@384 | 484 | |
paul@384 | 485 | for contributor in node._attrbranches: |
paul@384 | 486 | |
paul@508 | 487 | # Get contributor details. |
paul@508 | 488 | |
paul@508 | 489 | unfinished_contributors = self.get_usage_from_contributors(contributor) |
paul@508 | 490 | |
paul@508 | 491 | # Collect unfinished contributors and affected nodes. |
paul@508 | 492 | |
paul@528 | 493 | # Where the contributor is already set to None, a loop has |
paul@528 | 494 | # occurred and this node will need to have its usage |
paul@528 | 495 | # recalculated later for the unfinished contributor. |
paul@528 | 496 | |
paul@528 | 497 | if contributor._attrcombined is None: |
paul@508 | 498 | if not unfinished.has_key(contributor): |
paul@508 | 499 | unfinished[contributor] = [] |
paul@508 | 500 | unfinished[contributor].append(node) |
paul@508 | 501 | continue |
paul@508 | 502 | |
paul@528 | 503 | # Where the contributor provides usage details, it may also |
paul@528 | 504 | # communicate unfinished contributor information. As a |
paul@528 | 505 | # consequence, this node is also affected. |
paul@528 | 506 | |
paul@508 | 507 | for unfinished_contributor, nodes in unfinished_contributors.items(): |
paul@508 | 508 | if not unfinished.has_key(unfinished_contributor): |
paul@508 | 509 | unfinished[unfinished_contributor] = nodes |
paul@508 | 510 | else: |
paul@508 | 511 | unfinished[unfinished_contributor] += nodes |
paul@508 | 512 | |
paul@528 | 513 | if node not in unfinished[unfinished_contributor]: |
paul@528 | 514 | unfinished[unfinished_contributor].append(node) |
paul@508 | 515 | |
paul@508 | 516 | # Set the current state of the usage on this node. |
paul@508 | 517 | |
paul@528 | 518 | node._attrcombined, node._attrcontributors = \ |
paul@528 | 519 | self.get_usage_from_contributors_for_node(node) |
paul@508 | 520 | |
paul@508 | 521 | # Complete unfinished contributors relying on this node. |
paul@508 | 522 | |
paul@508 | 523 | if unfinished.has_key(node): |
paul@508 | 524 | processed = set() |
paul@508 | 525 | for contributor in unfinished[node]: |
paul@508 | 526 | if not contributor in processed: |
paul@508 | 527 | processed.add(contributor) |
paul@528 | 528 | |
paul@528 | 529 | contributor._attrcombined, contributor._attrcontributors = \ |
paul@528 | 530 | self.get_usage_from_contributors_for_node(contributor) |
paul@528 | 531 | |
paul@508 | 532 | del unfinished[node] |
paul@508 | 533 | |
paul@508 | 534 | return unfinished |
paul@508 | 535 | |
paul@508 | 536 | def get_usage_from_contributors_for_node(self, node): |
paul@508 | 537 | |
paul@508 | 538 | # Visit each contributor, gathering usage for each name. |
paul@508 | 539 | |
paul@508 | 540 | contributor_usage = {} |
paul@508 | 541 | all_contributions = [] |
paul@528 | 542 | all_contributors = set() |
paul@508 | 543 | |
paul@508 | 544 | for contributor in node._attrbranches: |
paul@508 | 545 | usage = contributor._attrcombined |
paul@508 | 546 | if usage is not None: |
paul@508 | 547 | all_contributions.append(usage) |
paul@508 | 548 | |
paul@528 | 549 | all_contributors.add(contributor) |
paul@528 | 550 | contributors = contributor._attrcontributors |
paul@528 | 551 | if contributors is not None: |
paul@528 | 552 | all_contributors.update(contributors) |
paul@528 | 553 | |
paul@508 | 554 | # Get contributed usage for each contributor. |
paul@508 | 555 | # This gathers usage for each name such as {(a, b), (c, d)} and |
paul@508 | 556 | # {(a, b), (e, f)} into a single set {(a, b), (c, d), (e, f)}. |
paul@508 | 557 | |
paul@508 | 558 | update_mapping_dict(contributor_usage, all_contributions) |
paul@508 | 559 | |
paul@508 | 560 | # Then get the resulting usage. |
paul@508 | 561 | # First, make the current usage compatible with the contributed |
paul@508 | 562 | # usage: this makes the attribute usage for each name merely one |
paul@508 | 563 | # member in a list of many possibilities. |
paul@508 | 564 | # Then, combine the current usage with the contributed usage. |
paul@508 | 565 | # Thus, usage of {(f, g)} combined with {(a, b), (c, d)} would give |
paul@508 | 566 | # {(f, g, a, b), (f, g, c, d)}. |
paul@508 | 567 | |
paul@528 | 568 | return combine_mapping_dicts(deepen_mapping_dict(node._attrnames), contributor_usage), all_contributors |
paul@280 | 569 | |
paul@404 | 570 | def use_attribute(self, name, attrname, value=None): |
paul@404 | 571 | |
paul@404 | 572 | """ |
paul@404 | 573 | Note usage on the attribute user 'name' of the attribute 'attrname', |
paul@404 | 574 | noting an assignment if 'value' is specified. |
paul@404 | 575 | """ |
paul@404 | 576 | |
paul@404 | 577 | return self._use_attribute(name, attrname, value) |
paul@285 | 578 | |
paul@343 | 579 | def use_specific_attribute(self, objname, attrname): |
paul@343 | 580 | |
paul@347 | 581 | "Declare the usage on 'objname' of the given 'attrname'." |
paul@347 | 582 | |
paul@347 | 583 | self._use_specific_attribute(objname, attrname) |
paul@347 | 584 | |
paul@347 | 585 | # These shadow various methods in the InspectedModule class, and provide |
paul@347 | 586 | # implementations generally. |
paul@347 | 587 | |
paul@434 | 588 | def _use_specific_attribute(self, objname, attrname, from_name=None): |
paul@347 | 589 | |
paul@343 | 590 | """ |
paul@343 | 591 | Note attribute usage specifically on 'objname' - an object which is |
paul@343 | 592 | known at inspection time - or in the current unit if 'objname' is None, |
paul@343 | 593 | nominating a specific attribute 'attrname'. |
paul@343 | 594 | |
paul@343 | 595 | This bypasses attribute user mechanisms. |
paul@343 | 596 | """ |
paul@343 | 597 | |
paul@434 | 598 | from_name = from_name or self.full_name() |
paul@343 | 599 | objname = objname or from_name |
paul@343 | 600 | module = self.module |
paul@343 | 601 | importer = module and module.importer |
paul@343 | 602 | |
paul@343 | 603 | if importer is not None: |
paul@343 | 604 | importer.use_specific_name(objname, attrname, from_name) |
paul@343 | 605 | |
paul@404 | 606 | def _use_attribute(self, name, attrname, value=None): |
paul@280 | 607 | |
paul@280 | 608 | """ |
paul@304 | 609 | Indicate the use of the given 'name' in this namespace of an attribute |
paul@404 | 610 | with the given 'attrname'. If the optional 'value' is specified, an |
paul@404 | 611 | assignment using the given 'value' is recorded. |
paul@280 | 612 | """ |
paul@280 | 613 | |
paul@278 | 614 | users = self.attribute_users[-1] |
paul@280 | 615 | |
paul@565 | 616 | # If no users are defined for the name, it cannot be handled. |
paul@560 | 617 | |
paul@560 | 618 | if not users.has_key(name): |
paul@565 | 619 | return [] |
paul@560 | 620 | |
paul@377 | 621 | # Add the usage to all current users. |
paul@377 | 622 | |
paul@560 | 623 | for user in users[name]: |
paul@560 | 624 | values = user._attrnames[name] |
paul@560 | 625 | if values is None: |
paul@560 | 626 | values = user._attrnames[name] = ObjectSet() |
paul@560 | 627 | |
paul@560 | 628 | # Add an entry for the attribute, optionally with an assigned |
paul@560 | 629 | # value. |
paul@560 | 630 | |
paul@560 | 631 | values.add(attrname) |
paul@560 | 632 | if value is not None: |
paul@560 | 633 | values[attrname].add(value) |
paul@560 | 634 | |
paul@560 | 635 | return users[name] |
paul@272 | 636 | |
paul@278 | 637 | def _define_attribute_user(self, node): |
paul@279 | 638 | |
paul@279 | 639 | """ |
paul@279 | 640 | Define 'node' as the user of attributes, indicating the point where the |
paul@279 | 641 | user is defined. |
paul@279 | 642 | """ |
paul@279 | 643 | |
paul@278 | 644 | name = node.name |
paul@279 | 645 | self._define_attribute_user_for_name(node, name) |
paul@279 | 646 | |
paul@279 | 647 | def _define_attribute_user_for_name(self, node, name): |
paul@280 | 648 | |
paul@280 | 649 | "Define 'node' as the user of attributes for the given 'name'." |
paul@280 | 650 | |
paul@278 | 651 | users = self.attribute_users[-1] |
paul@306 | 652 | |
paul@308 | 653 | # This node overrides previous definitions. |
paul@308 | 654 | |
paul@304 | 655 | users[name] = set([node]) |
paul@280 | 656 | |
paul@280 | 657 | # Record the attribute combinations for the name. |
paul@280 | 658 | |
paul@311 | 659 | self._init_attribute_user_for_name(node, name) |
paul@311 | 660 | |
paul@311 | 661 | # Remember this user. |
paul@311 | 662 | |
paul@311 | 663 | self.all_attribute_users.add(node) |
paul@311 | 664 | |
paul@311 | 665 | def _init_attribute_user_for_name(self, node, name): |
paul@311 | 666 | |
paul@311 | 667 | "Make sure that 'node' is initialised for 'name'." |
paul@311 | 668 | |
paul@379 | 669 | self._init_attribute_user(node) |
paul@476 | 670 | node._attrnames[name] = None |
paul@379 | 671 | |
paul@379 | 672 | def _init_attribute_user(self, node): |
paul@381 | 673 | |
paul@381 | 674 | # Attribute usage for names. |
paul@381 | 675 | |
paul@279 | 676 | if not hasattr(node, "_attrnames"): |
paul@457 | 677 | node._attrnames = {} |
paul@489 | 678 | node._attrmerged = {} |
paul@303 | 679 | |
paul@381 | 680 | # Branches contributing usage to this node. |
paul@381 | 681 | |
paul@379 | 682 | if not hasattr(node, "_attrbranches"): |
paul@379 | 683 | node._attrbranches = [] |
paul@272 | 684 | |
paul@381 | 685 | # Definitions receiving usage from this node. |
paul@381 | 686 | |
paul@381 | 687 | if not hasattr(node, "_attrdefs"): |
paul@381 | 688 | node._attrdefs = [] |
paul@381 | 689 | |
paul@556 | 690 | def _define_attribute_accessor(self, name, attrname, node, value): |
paul@556 | 691 | |
paul@556 | 692 | # NOTE: Revisiting of nodes may occur for loops. |
paul@556 | 693 | |
paul@556 | 694 | if not hasattr(node, "_attrusers"): |
paul@556 | 695 | node._attrusers = set() |
paul@556 | 696 | |
paul@556 | 697 | node._attrusers.update(self.use_attribute(name, attrname, value)) |
paul@556 | 698 | node._username = name |
paul@556 | 699 | |
paul@343 | 700 | # Branch management methods. |
paul@343 | 701 | |
paul@388 | 702 | def _new_branchpoint(self, loop_node=None): |
paul@305 | 703 | |
paul@305 | 704 | """ |
paul@305 | 705 | Establish a new branchpoint where several control-flow branches diverge |
paul@305 | 706 | and subsequently converge. |
paul@305 | 707 | """ |
paul@305 | 708 | |
paul@379 | 709 | self.attribute_user_shelves.append([]) |
paul@343 | 710 | self.scope_shelves.append([]) |
paul@272 | 711 | |
paul@388 | 712 | if loop_node is not None: |
paul@388 | 713 | self.suspended_broken_users.append([]) |
paul@388 | 714 | self.suspended_continuing_users.append((loop_node, [])) |
paul@387 | 715 | |
paul@379 | 716 | def _new_branch(self, node): |
paul@305 | 717 | |
paul@305 | 718 | """ |
paul@305 | 719 | Establish a new control-flow branch, transferring attribute usage to |
paul@305 | 720 | the new branch so that it may be augmented for each name locally. |
paul@311 | 721 | |
paul@377 | 722 | Add the given 'node' as an active user to be informed of attribute |
paul@377 | 723 | usage. |
paul@305 | 724 | """ |
paul@305 | 725 | |
paul@379 | 726 | attribute_users = self.attribute_users[-1] |
paul@379 | 727 | |
paul@379 | 728 | # Define this node as the active attribute user for all currently |
paul@379 | 729 | # defined names. |
paul@379 | 730 | |
paul@377 | 731 | new_users = {} |
paul@377 | 732 | |
paul@379 | 733 | for name in attribute_users.keys(): |
paul@377 | 734 | new_users[name] = [node] |
paul@379 | 735 | self._init_attribute_user_for_name(node, name) |
paul@379 | 736 | |
paul@379 | 737 | self._init_attribute_user(node) |
paul@343 | 738 | self.attribute_users.append(new_users) |
paul@306 | 739 | |
paul@379 | 740 | # Add this user as a contributor to the previously active users. |
paul@379 | 741 | |
paul@388 | 742 | self._connect_users_to_branch(attribute_users, node) |
paul@388 | 743 | |
paul@388 | 744 | # Retain a record of scope usage. |
paul@388 | 745 | |
paul@388 | 746 | scope_usage = {} |
paul@388 | 747 | scope_usage.update(self.scope_usage[-1]) |
paul@388 | 748 | self.scope_usage.append(scope_usage) |
paul@388 | 749 | |
paul@540 | 750 | # Retain a record of abandoned branch users. |
paul@540 | 751 | |
paul@540 | 752 | self.abandoned_users.append([]) |
paul@540 | 753 | |
paul@388 | 754 | def _connect_users_to_branch(self, attribute_users, node): |
paul@388 | 755 | |
paul@388 | 756 | """ |
paul@388 | 757 | Given the 'attribute_users' mapping, connect the users referenced in the |
paul@388 | 758 | mapping to the given branch 'node'. |
paul@388 | 759 | """ |
paul@388 | 760 | |
paul@379 | 761 | all_users = set() |
paul@379 | 762 | |
paul@379 | 763 | for users in attribute_users.values(): |
paul@379 | 764 | all_users.update(users) |
paul@379 | 765 | |
paul@379 | 766 | for user in all_users: |
paul@379 | 767 | self._init_attribute_user(user) |
paul@379 | 768 | user._attrbranches.append(node) |
paul@311 | 769 | |
paul@540 | 770 | def _abandon_branch(self, retain_branch=True): |
paul@362 | 771 | |
paul@362 | 772 | """ |
paul@362 | 773 | Abandon scope usage, permitting locally different scopes for names, |
paul@362 | 774 | provided these cannot "escape" from the branch. |
paul@362 | 775 | """ |
paul@362 | 776 | |
paul@540 | 777 | attribute_users = self.attribute_users[-1] |
paul@540 | 778 | |
paul@540 | 779 | self.attribute_users[-1] = {} |
paul@457 | 780 | self.scope_usage[-1] = abandoned_branch_scope |
paul@299 | 781 | |
paul@540 | 782 | if retain_branch: |
paul@540 | 783 | self.abandoned_users[-1].append(attribute_users) |
paul@540 | 784 | |
paul@388 | 785 | def _suspend_broken_branch(self): |
paul@387 | 786 | |
paul@387 | 787 | """ |
paul@387 | 788 | Suspend a branch for resumption after the current loop. |
paul@387 | 789 | """ |
paul@387 | 790 | |
paul@388 | 791 | attribute_users = self.attribute_users[-1] |
paul@388 | 792 | |
paul@540 | 793 | users = self.suspended_broken_users[-1] |
paul@540 | 794 | users.append(attribute_users) |
paul@540 | 795 | self._abandon_branch(False) |
paul@388 | 796 | |
paul@388 | 797 | def _suspend_continuing_branch(self): |
paul@388 | 798 | |
paul@388 | 799 | """ |
paul@388 | 800 | Suspend a branch for resumption after the current iteration. |
paul@388 | 801 | """ |
paul@388 | 802 | |
paul@388 | 803 | attribute_users = self.attribute_users[-1] |
paul@388 | 804 | |
paul@540 | 805 | loop_node, users = self.suspended_continuing_users[-1] |
paul@540 | 806 | users.append(attribute_users) |
paul@540 | 807 | self._abandon_branch(False) |
paul@387 | 808 | |
paul@272 | 809 | def _shelve_branch(self): |
paul@303 | 810 | |
paul@303 | 811 | """ |
paul@303 | 812 | Shelve the current control-flow branch, recording the attribute usage |
paul@303 | 813 | for subsequent merging. If this branch should be abandoned, the usage |
paul@303 | 814 | observations are still recorded but will not contribute to subsequent |
paul@303 | 815 | observations after a merge. |
paul@303 | 816 | """ |
paul@303 | 817 | |
paul@304 | 818 | users = self.attribute_users.pop() |
paul@379 | 819 | self.attribute_user_shelves[-1].append(users) |
paul@272 | 820 | |
paul@343 | 821 | scope_usage = self.scope_usage.pop() |
paul@343 | 822 | self.scope_shelves[-1].append(scope_usage) |
paul@343 | 823 | |
paul@379 | 824 | def _merge_branches(self): |
paul@303 | 825 | |
paul@303 | 826 | """ |
paul@309 | 827 | Merge control-flow branches. This should find the users active within |
paul@309 | 828 | each branch, which have been "shelved", and update the active users |
paul@309 | 829 | dictionary with these contributions. |
paul@303 | 830 | """ |
paul@303 | 831 | |
paul@306 | 832 | # Combine the attribute users. This ensures that a list of users |
paul@306 | 833 | # affected by attribute usage is maintained for the current branch. |
paul@303 | 834 | |
paul@379 | 835 | all_shelved_users = self.attribute_user_shelves.pop() |
paul@404 | 836 | new_users = merge_mapping_dicts(all_shelved_users) |
paul@309 | 837 | self.attribute_users[-1] = new_users |
paul@288 | 838 | |
paul@540 | 839 | # Abandoned users are retained for exception handling purposes. |
paul@540 | 840 | |
paul@540 | 841 | all_abandoned_users = self.abandoned_users.pop() |
paul@540 | 842 | new_abandoned_users = merge_mapping_dicts(all_abandoned_users) |
paul@540 | 843 | self.abandoned_users[-1].append(new_abandoned_users) |
paul@540 | 844 | |
paul@343 | 845 | # Combine the scope usage. |
paul@343 | 846 | |
paul@343 | 847 | scope_usage = self.scope_usage[-1] |
paul@343 | 848 | new_scope_usage = {} |
paul@343 | 849 | |
paul@343 | 850 | all_scope_usage = self.scope_shelves.pop() |
paul@343 | 851 | all_scope_names = set() |
paul@343 | 852 | |
paul@343 | 853 | # Find all the names for whom scope information has been defined. |
paul@343 | 854 | |
paul@343 | 855 | for shelved_usage in all_scope_usage: |
paul@343 | 856 | all_scope_names.update(shelved_usage.keys()) |
paul@343 | 857 | |
paul@343 | 858 | for shelved_usage in all_scope_usage: |
paul@343 | 859 | for name in all_scope_names: |
paul@343 | 860 | |
paul@343 | 861 | # Find the recorded scope for the name. |
paul@343 | 862 | |
paul@343 | 863 | if shelved_usage.has_key(name): |
paul@343 | 864 | scope = shelved_usage[name] |
paul@343 | 865 | elif scope_usage.has_key(name): |
paul@343 | 866 | scope = scope_usage[name] |
paul@343 | 867 | |
paul@362 | 868 | # For abandoned branches, no scope is asserted for a name. |
paul@362 | 869 | |
paul@362 | 870 | elif isinstance(shelved_usage, AbandonedBranchScope): |
paul@362 | 871 | scope = None |
paul@362 | 872 | |
paul@343 | 873 | # If no scope is recorded, find a suitable external source. |
paul@343 | 874 | |
paul@343 | 875 | else: |
paul@343 | 876 | attr, scope, full_name = self._get_with_scope(name, external=1) |
paul@343 | 877 | |
paul@343 | 878 | # Attempt to record the scope, testing for conflicts. |
paul@343 | 879 | |
paul@343 | 880 | if scope: |
paul@343 | 881 | if not new_scope_usage.has_key(name): |
paul@343 | 882 | new_scope_usage[name] = scope |
paul@538 | 883 | else: |
paul@538 | 884 | new_scope = new_scope_usage[name] |
paul@538 | 885 | if new_scope != scope: |
paul@538 | 886 | if isinstance(new_scope, ScopeConflict): |
paul@538 | 887 | if isinstance(scope, ScopeConflict): |
paul@538 | 888 | scopes = scope.scopes.union(new_scope.scopes) |
paul@538 | 889 | else: |
paul@538 | 890 | scopes = new_scope.scopes.union(set([scope])) |
paul@538 | 891 | elif isinstance(scope, ScopeConflict): |
paul@538 | 892 | scopes = scope.scopes.union(set([new_scope])) |
paul@538 | 893 | else: |
paul@538 | 894 | scopes = set([scope, new_scope]) |
paul@538 | 895 | new_scope_usage[name] = ScopeConflict(scopes) |
paul@362 | 896 | |
paul@362 | 897 | self.scope_usage[-1] = new_scope_usage |
paul@343 | 898 | |
paul@388 | 899 | def _resume_broken_branches(self): |
paul@388 | 900 | |
paul@388 | 901 | """ |
paul@388 | 902 | Incorporate users from suspended broken branches into the current set of |
paul@388 | 903 | active users. |
paul@388 | 904 | """ |
paul@388 | 905 | |
paul@388 | 906 | suspended_users = self.suspended_broken_users.pop() |
paul@388 | 907 | current_users = self.attribute_users[-1] |
paul@404 | 908 | new_users = merge_mapping_dicts(suspended_users + [current_users]) |
paul@388 | 909 | self.attribute_users[-1] = new_users |
paul@388 | 910 | |
paul@388 | 911 | def _resume_continuing_branches(self): |
paul@387 | 912 | |
paul@387 | 913 | """ |
paul@388 | 914 | Incorporate users from suspended continuing branches into the current |
paul@388 | 915 | set of active users, merging usage from the latter with the former. |
paul@387 | 916 | """ |
paul@387 | 917 | |
paul@388 | 918 | loop_node, suspended_users = self.suspended_continuing_users.pop() |
paul@387 | 919 | current_users = self.attribute_users[-1] |
paul@388 | 920 | |
paul@388 | 921 | # Connect the suspended users to the loop node. |
paul@388 | 922 | |
paul@388 | 923 | for users in suspended_users: |
paul@388 | 924 | self._connect_users_to_branch(users, loop_node) |
paul@388 | 925 | |
paul@388 | 926 | # Merge suspended branches with the current branch. |
paul@388 | 927 | |
paul@404 | 928 | new_users = merge_mapping_dicts(suspended_users + [current_users]) |
paul@387 | 929 | self.attribute_users[-1] = new_users |
paul@387 | 930 | |
paul@540 | 931 | def _resume_abandoned_branches(self): |
paul@540 | 932 | |
paul@540 | 933 | """ |
paul@540 | 934 | Incorporate users from abandoned branches into the current set of active |
paul@540 | 935 | users. The abandoned branches are taken from the containing branch. |
paul@540 | 936 | """ |
paul@540 | 937 | |
paul@540 | 938 | current_users = self.attribute_users[-1] |
paul@540 | 939 | abandoned_users = self.abandoned_users[-2] |
paul@540 | 940 | new_users = merge_mapping_dicts(abandoned_users + [current_users]) |
paul@540 | 941 | self.attribute_users[-1] = new_users |
paul@540 | 942 | |
paul@343 | 943 | # Scope usage methods. |
paul@343 | 944 | |
paul@362 | 945 | def define_scope(self, name, scope): |
paul@362 | 946 | |
paul@362 | 947 | """ |
paul@362 | 948 | Define 'name' as being from the given 'scope' in the current namespace. |
paul@362 | 949 | """ |
paul@362 | 950 | |
paul@362 | 951 | self.scope_usage[-1][name] = scope |
paul@362 | 952 | |
paul@343 | 953 | def note_scope(self, name, scope): |
paul@343 | 954 | |
paul@362 | 955 | """ |
paul@362 | 956 | Note usage of 'name' from the given 'scope' in the current namespace. |
paul@362 | 957 | If a conflict has been recorded previously, raise an exception. |
paul@362 | 958 | """ |
paul@362 | 959 | |
paul@362 | 960 | scope_usage = self.scope_usage[-1] |
paul@343 | 961 | |
paul@362 | 962 | if scope_usage.has_key(name): |
paul@362 | 963 | found_scope = scope_usage[name] |
paul@362 | 964 | if isinstance(found_scope, ScopeConflict): |
paul@538 | 965 | raise InspectError("Scope conflict for %r: defined as %s." % ( |
paul@538 | 966 | name, ", ".join(found_scope.scopes))) |
paul@362 | 967 | |
paul@362 | 968 | scope_usage[name] = scope |
paul@343 | 969 | |
paul@343 | 970 | def used_in_scope(self, name, scope): |
paul@343 | 971 | |
paul@343 | 972 | """ |
paul@343 | 973 | Return whether 'name' is used from the given 'scope' in the current |
paul@343 | 974 | namespace. |
paul@343 | 975 | """ |
paul@343 | 976 | |
paul@343 | 977 | scope_usage = self.scope_usage[-1] |
paul@343 | 978 | return scope_usage.get(name) == scope |
paul@343 | 979 | |
paul@385 | 980 | # Special helper classes for usage and scope resolution. |
paul@385 | 981 | |
paul@385 | 982 | class EmptyDict: |
paul@385 | 983 | |
paul@385 | 984 | "A class providing dictionaries which retain no information." |
paul@362 | 985 | |
paul@362 | 986 | def has_key(self, name): |
paul@362 | 987 | return 0 |
paul@362 | 988 | |
paul@362 | 989 | def __setitem__(self, name, value): |
paul@362 | 990 | pass |
paul@362 | 991 | |
paul@362 | 992 | def __getitem__(self, name): |
paul@362 | 993 | raise KeyError, name |
paul@362 | 994 | |
paul@362 | 995 | def get(self, name, default=None): |
paul@362 | 996 | return default |
paul@362 | 997 | |
paul@362 | 998 | def keys(self): |
paul@362 | 999 | return [] |
paul@362 | 1000 | |
paul@362 | 1001 | values = items = keys |
paul@362 | 1002 | |
paul@385 | 1003 | class AbandonedBranchScope(EmptyDict): |
paul@385 | 1004 | |
paul@385 | 1005 | """ |
paul@385 | 1006 | A class providing a value or state for an abandoned branch distinct from an |
paul@385 | 1007 | empty scope dictionary. |
paul@385 | 1008 | """ |
paul@385 | 1009 | |
paul@385 | 1010 | pass |
paul@385 | 1011 | |
paul@457 | 1012 | abandoned_branch_scope = AbandonedBranchScope() |
paul@457 | 1013 | |
paul@362 | 1014 | class ScopeConflict: |
paul@362 | 1015 | |
paul@362 | 1016 | """ |
paul@362 | 1017 | A scope conflict caused when different code branches contribute different |
paul@362 | 1018 | sources of names. |
paul@362 | 1019 | """ |
paul@362 | 1020 | |
paul@538 | 1021 | def __init__(self, scopes): |
paul@538 | 1022 | self.scopes = scopes |
paul@362 | 1023 | |
paul@379 | 1024 | class NullBranch: |
paul@379 | 1025 | |
paul@379 | 1026 | "A class representing an attribute user for a non-existent branch." |
paul@379 | 1027 | |
paul@379 | 1028 | pass |
paul@379 | 1029 | |
paul@305 | 1030 | # Program data structures. |
paul@83 | 1031 | |
paul@83 | 1032 | class Attr: |
paul@83 | 1033 | |
paul@87 | 1034 | "An attribute entry having a context." |
paul@83 | 1035 | |
paul@192 | 1036 | def __init__(self, position, parent, name): |
paul@119 | 1037 | |
paul@119 | 1038 | """ |
paul@119 | 1039 | Initialise the attribute with the given 'position' within the collection |
paul@192 | 1040 | of attributes of its 'parent', indicating its 'name'. |
paul@119 | 1041 | """ |
paul@119 | 1042 | |
paul@83 | 1043 | self.position = position |
paul@83 | 1044 | self.parent = parent |
paul@83 | 1045 | self.name = name |
paul@192 | 1046 | |
paul@272 | 1047 | # Possible values. |
paul@272 | 1048 | |
paul@192 | 1049 | self.context_values = set() |
paul@83 | 1050 | |
paul@83 | 1051 | # Number of assignments per name. |
paul@83 | 1052 | |
paul@192 | 1053 | self.assignments = None |
paul@192 | 1054 | |
paul@268 | 1055 | # Value-related methods. |
paul@268 | 1056 | |
paul@192 | 1057 | def get_contexts(self): |
paul@192 | 1058 | return [c for (c, v) in self.context_values] |
paul@167 | 1059 | |
paul@192 | 1060 | def get_values(self): |
paul@192 | 1061 | return [v for (c, v) in self.context_values] |
paul@192 | 1062 | |
paul@192 | 1063 | def get_context(self): |
paul@578 | 1064 | |
paul@578 | 1065 | "Get the context referenced by the attribute." |
paul@578 | 1066 | |
paul@578 | 1067 | if self.assignments == 1 and len(self.context_values) == 1: |
paul@237 | 1068 | return self.get_contexts()[0] |
paul@237 | 1069 | else: |
paul@237 | 1070 | return None |
paul@192 | 1071 | |
paul@192 | 1072 | def get_value(self): |
paul@578 | 1073 | |
paul@578 | 1074 | "Get the value referenced by the attribute." |
paul@578 | 1075 | |
paul@578 | 1076 | if self.assignments == 1 and len(self.context_values) == 1: |
paul@237 | 1077 | return self.get_values()[0] |
paul@237 | 1078 | else: |
paul@237 | 1079 | return None |
paul@192 | 1080 | |
paul@508 | 1081 | __call__ = get_value # convenient access to any single value |
paul@508 | 1082 | |
paul@192 | 1083 | def update(self, context_values, single_assignment): |
paul@83 | 1084 | |
paul@83 | 1085 | """ |
paul@192 | 1086 | Update the attribute, adding the 'context_values' provided to the |
paul@192 | 1087 | known details associated with the attribute, changing the number of |
paul@192 | 1088 | assignments according to the 'single_assignment' status of the |
paul@192 | 1089 | operation, where a true value indicates that only one assignment is |
paul@192 | 1090 | associated with the update, and a false value indicates that potentially |
paul@192 | 1091 | many assignments may be involved. |
paul@83 | 1092 | """ |
paul@83 | 1093 | |
paul@442 | 1094 | if self.context_values.issuperset(context_values) and \ |
paul@448 | 1095 | not (make_instance(), make_instance()) in context_values: |
paul@442 | 1096 | return |
paul@442 | 1097 | |
paul@578 | 1098 | self.update_assignments(len(set(context_values)), single_assignment) |
paul@578 | 1099 | self.context_values.update(context_values) |
paul@578 | 1100 | |
paul@578 | 1101 | def update_assignments(self, n, single_assignment): |
paul@83 | 1102 | if self.assignments is None: |
paul@83 | 1103 | if single_assignment: |
paul@578 | 1104 | self.assignments = n |
paul@83 | 1105 | else: |
paul@578 | 1106 | self.assignments = AtLeast(n) |
paul@83 | 1107 | else: |
paul@83 | 1108 | if single_assignment: |
paul@578 | 1109 | self.assignments += n |
paul@83 | 1110 | else: |
paul@578 | 1111 | self.assignments += AtLeast(n) |
paul@87 | 1112 | |
paul@331 | 1113 | def is_constant(self): |
paul@331 | 1114 | |
paul@331 | 1115 | """ |
paul@331 | 1116 | Return whether this attribute references something that can be regarded |
paul@331 | 1117 | as being constant within a particular scope. |
paul@331 | 1118 | """ |
paul@331 | 1119 | |
paul@331 | 1120 | return self.assignments == 1 |
paul@331 | 1121 | |
paul@330 | 1122 | def is_strict_constant(self): |
paul@330 | 1123 | |
paul@330 | 1124 | """ |
paul@330 | 1125 | Return whether this attribute references something that can be regarded |
paul@330 | 1126 | as being constant. |
paul@330 | 1127 | """ |
paul@330 | 1128 | |
paul@330 | 1129 | value = self.get_value() |
paul@468 | 1130 | return not (value is None or (isinstance(value, Instance) and not isinstance(value, Constant))) |
paul@330 | 1131 | |
paul@242 | 1132 | def is_static_attribute(self): |
paul@242 | 1133 | |
paul@242 | 1134 | """ |
paul@242 | 1135 | Return whether this attribute is defined on a fixed/static object such |
paul@242 | 1136 | as a class or a module. |
paul@242 | 1137 | """ |
paul@242 | 1138 | |
paul@401 | 1139 | return isinstance(self.parent, (Class, Module)) |
paul@127 | 1140 | |
paul@243 | 1141 | def defines_ambiguous_class(self): |
paul@243 | 1142 | |
paul@243 | 1143 | "Return whether this attribute defines more than one class." |
paul@243 | 1144 | |
paul@243 | 1145 | if self.assignments > 1: |
paul@243 | 1146 | have_class = 0 |
paul@243 | 1147 | for obj in self.get_values(): |
paul@243 | 1148 | if isinstance(obj, Class): |
paul@243 | 1149 | if have_class: |
paul@243 | 1150 | return 1 |
paul@243 | 1151 | have_class = 1 |
paul@243 | 1152 | |
paul@243 | 1153 | return 0 |
paul@243 | 1154 | |
paul@119 | 1155 | def defined_within_hierarchy(self): |
paul@119 | 1156 | |
paul@119 | 1157 | """ |
paul@119 | 1158 | Return whether the parent and context of the attribute belong to the |
paul@119 | 1159 | same class hierarchy. |
paul@119 | 1160 | """ |
paul@119 | 1161 | |
paul@192 | 1162 | # Must be defined within a class. |
paul@192 | 1163 | |
paul@192 | 1164 | if isinstance(self.parent, Class): |
paul@192 | 1165 | |
paul@192 | 1166 | # To be sure, all contexts must be classes and be the same as the |
paul@192 | 1167 | # parent, or be a superclass of the parent, or be a subclass of the |
paul@192 | 1168 | # parent. |
paul@192 | 1169 | |
paul@192 | 1170 | for context in self.get_contexts(): |
paul@192 | 1171 | if not ( |
paul@192 | 1172 | isinstance(context, Class) and ( |
paul@192 | 1173 | context is self.parent or |
paul@192 | 1174 | context.has_subclass(self.parent) or |
paul@192 | 1175 | self.parent.has_subclass(context)) |
paul@192 | 1176 | ): |
paul@192 | 1177 | return 0 |
paul@192 | 1178 | |
paul@192 | 1179 | return 1 |
paul@192 | 1180 | |
paul@192 | 1181 | # Instance attributes are not defined within a hierarchy. |
paul@192 | 1182 | |
paul@192 | 1183 | else: |
paul@192 | 1184 | return 0 |
paul@119 | 1185 | |
paul@194 | 1186 | def defined_outside_hierarchy(self): |
paul@194 | 1187 | |
paul@194 | 1188 | """ |
paul@194 | 1189 | Return whether the parent and context of the attribute never belong to |
paul@194 | 1190 | the same class hierarchy. |
paul@194 | 1191 | """ |
paul@194 | 1192 | |
paul@194 | 1193 | # Must be defined within a class. |
paul@194 | 1194 | |
paul@194 | 1195 | if isinstance(self.parent, Class): |
paul@194 | 1196 | |
paul@194 | 1197 | # To be sure, all contexts must be classes and be the same as the |
paul@194 | 1198 | # parent, or be a superclass of the parent, or be a subclass of the |
paul@194 | 1199 | # parent. |
paul@194 | 1200 | |
paul@194 | 1201 | for context in self.get_contexts(): |
paul@194 | 1202 | if not ( |
paul@194 | 1203 | isinstance(context, Class) and not ( |
paul@194 | 1204 | context is self.parent or |
paul@194 | 1205 | context.has_subclass(self.parent) or |
paul@194 | 1206 | self.parent.has_subclass(context)) |
paul@194 | 1207 | ): |
paul@194 | 1208 | return 0 |
paul@194 | 1209 | |
paul@194 | 1210 | return 1 |
paul@194 | 1211 | |
paul@194 | 1212 | # Instance attributes are not defined within a hierarchy. |
paul@194 | 1213 | |
paul@194 | 1214 | else: |
paul@194 | 1215 | return 0 |
paul@194 | 1216 | |
paul@83 | 1217 | def __repr__(self): |
paul@494 | 1218 | if self.position is not None: |
paul@494 | 1219 | position = "at %r, " % self.position |
paul@494 | 1220 | else: |
paul@494 | 1221 | position = "" |
paul@497 | 1222 | return "<attribute %s.%s (%sassigned %r)>" % ( |
paul@494 | 1223 | shortrepr(self.parent), self.name, |
paul@494 | 1224 | position, self.assignments |
paul@87 | 1225 | ) |
paul@83 | 1226 | |
paul@329 | 1227 | def __shortrepr__(self): |
paul@494 | 1228 | return "%s.%s (at %r)" % (shortrepr(self.parent), self.name, self.position) |
paul@329 | 1229 | |
paul@192 | 1230 | def _context_values_str(self): |
paul@192 | 1231 | l = [] |
paul@192 | 1232 | for (c, v) in self.context_values: |
paul@192 | 1233 | l.append("(c=%s, v=%s)" % (shortrepr(c), shortrepr(v))) |
paul@192 | 1234 | return ", ".join(l) |
paul@192 | 1235 | |
paul@85 | 1236 | class Class(NamespaceDict, Naming, Constant): |
paul@83 | 1237 | |
paul@394 | 1238 | "A base class for common/normal classes and the type class." |
paul@394 | 1239 | |
paul@394 | 1240 | def __init__(self, name, parent=None, module=None, node=None): |
paul@83 | 1241 | |
paul@83 | 1242 | """ |
paul@394 | 1243 | Initialise the class with the given 'name', optional 'parent' object, |
paul@394 | 1244 | 'module' and AST 'node'. The optional information must be set at a later |
paul@394 | 1245 | point using the 'set_context' method if omitted. |
paul@83 | 1246 | """ |
paul@83 | 1247 | |
paul@180 | 1248 | NamespaceDict.__init__(self, module) |
paul@83 | 1249 | self.name = name |
paul@83 | 1250 | self.parent = parent |
paul@91 | 1251 | self.astnode = node |
paul@83 | 1252 | |
paul@83 | 1253 | # Superclasses, descendants and attributes. |
paul@83 | 1254 | |
paul@83 | 1255 | self.bases = [] |
paul@83 | 1256 | self.descendants = set() |
paul@83 | 1257 | self.instattr = set() # instance attributes |
paul@527 | 1258 | self.instattr_tentative = set() # tentative/suspected instance attributes |
paul@83 | 1259 | self.relocated = set() # attributes which do not have the same |
paul@83 | 1260 | # position as those of the same name in |
paul@83 | 1261 | # some superclasses |
paul@83 | 1262 | |
paul@83 | 1263 | # Caches. |
paul@83 | 1264 | |
paul@282 | 1265 | self.reset_caches() |
paul@83 | 1266 | |
paul@83 | 1267 | # Image generation details. |
paul@83 | 1268 | |
paul@83 | 1269 | self.location = None |
paul@83 | 1270 | self.code_location = None |
paul@240 | 1271 | self.code_body_location = None # corresponds to the instantiator |
paul@240 | 1272 | |
paul@83 | 1273 | self.instantiator = None |
paul@184 | 1274 | self.instance_template_location = None # for creating instances at run-time |
paul@83 | 1275 | |
paul@83 | 1276 | # Program-related details. |
paul@83 | 1277 | |
paul@173 | 1278 | self.blocks = None |
paul@116 | 1279 | self.temp_usage = 0 |
paul@116 | 1280 | self.local_usage = 0 |
paul@133 | 1281 | self.all_local_usage = 0 |
paul@83 | 1282 | |
paul@401 | 1283 | # Add an attribute to this class for use by instances. |
paul@401 | 1284 | |
paul@401 | 1285 | self.set("__class__", self) |
paul@394 | 1286 | |
paul@394 | 1287 | def set_context(self, parent, module, node): |
paul@401 | 1288 | |
paul@401 | 1289 | "Set the 'parent', 'module' and 'node' of a class created in advance." |
paul@401 | 1290 | |
paul@394 | 1291 | self.parent = parent |
paul@394 | 1292 | self.module = module |
paul@394 | 1293 | self.astnode = node |
paul@394 | 1294 | |
paul@282 | 1295 | def reset_caches(self): |
paul@282 | 1296 | |
paul@282 | 1297 | "Reset the caches." |
paul@282 | 1298 | |
paul@282 | 1299 | self.all_instattr = None # cache for instance_attributes |
paul@282 | 1300 | self.all_instattr_names = None # from all_instattr |
paul@282 | 1301 | self.all_classattr = None # cache for all_class_attributes |
paul@282 | 1302 | self.all_classattr_names = None # from all_classattr |
paul@282 | 1303 | self.allattr = None # cache for all_attributes |
paul@282 | 1304 | self.allattr_names = None # from allattr |
paul@282 | 1305 | |
paul@83 | 1306 | def __repr__(self): |
paul@83 | 1307 | if self.location is not None: |
paul@494 | 1308 | return "<class %s (at %r)>" % (shortrepr(self), self.location) |
paul@83 | 1309 | else: |
paul@494 | 1310 | return "<class %s>" % shortrepr(self) |
paul@87 | 1311 | |
paul@87 | 1312 | def __shortrepr__(self): |
paul@494 | 1313 | return "%s.%s" % (shortrepr(self.parent), self.name) |
paul@87 | 1314 | |
paul@227 | 1315 | def get_body_block(self): |
paul@227 | 1316 | return self.get_instantiator().blocks[0] |
paul@215 | 1317 | |
paul@189 | 1318 | # Namespace-related methods. |
paul@189 | 1319 | |
paul@192 | 1320 | def get_updated_context_values(self, context_values): |
paul@87 | 1321 | |
paul@119 | 1322 | """ |
paul@192 | 1323 | Adapt the contexts found in the given 'context_values', returning a new |
paul@192 | 1324 | set. |
paul@187 | 1325 | See: docs/assignment.txt |
paul@119 | 1326 | """ |
paul@87 | 1327 | |
paul@192 | 1328 | results = set() |
paul@192 | 1329 | |
paul@192 | 1330 | for context, value in context_values: |
paul@187 | 1331 | |
paul@187 | 1332 | # Change the ownership of functions. |
paul@187 | 1333 | |
paul@237 | 1334 | if context is ReplaceableContext and value is not None and isinstance(value, Function): |
paul@192 | 1335 | results.add((self, value)) |
paul@192 | 1336 | else: |
paul@192 | 1337 | results.add((context, value)) |
paul@187 | 1338 | |
paul@192 | 1339 | return NamespaceDict.get_updated_context_values(self, results) |
paul@83 | 1340 | |
paul@351 | 1341 | # Administrative methods. |
paul@351 | 1342 | |
paul@394 | 1343 | def items_for_vacuum(self): |
paul@405 | 1344 | |
paul@405 | 1345 | "Consider both class and instance attributes for vacuuming." |
paul@405 | 1346 | |
paul@394 | 1347 | items = [] |
paul@394 | 1348 | for name in self.instattr: |
paul@394 | 1349 | items.append((name, None)) |
paul@394 | 1350 | return NamespaceDict.items_for_vacuum(self) + items |
paul@394 | 1351 | |
paul@394 | 1352 | def vacuum_item(self, name): |
paul@405 | 1353 | |
paul@405 | 1354 | "Vacuum 'name' from the class or instance attribute collections." |
paul@405 | 1355 | |
paul@405 | 1356 | # NOTE: Hack to prevent damage to exceptions. |
paul@405 | 1357 | |
paul@405 | 1358 | if name == "_pc": |
paul@405 | 1359 | return 0 |
paul@405 | 1360 | |
paul@394 | 1361 | if not NamespaceDict.vacuum_item(self, name): |
paul@394 | 1362 | self.instattr.remove(name) |
paul@394 | 1363 | return 1 |
paul@394 | 1364 | |
paul@287 | 1365 | def finalise_attributes(self): |
paul@83 | 1366 | |
paul@83 | 1367 | "Make sure that all attributes are fully defined." |
paul@83 | 1368 | |
paul@92 | 1369 | if self.finalised: |
paul@92 | 1370 | return |
paul@92 | 1371 | |
paul@83 | 1372 | self.finalise_class_attributes() |
paul@83 | 1373 | self.finalise_instance_attributes() |
paul@92 | 1374 | self.finalised = 1 |
paul@83 | 1375 | |
paul@282 | 1376 | def unfinalise_attributes(self): |
paul@282 | 1377 | |
paul@282 | 1378 | "Open attribute definitions to editing and subsequent finalisation." |
paul@282 | 1379 | |
paul@282 | 1380 | self.reset_caches() |
paul@282 | 1381 | self.finalised = 0 |
paul@282 | 1382 | |
paul@184 | 1383 | # Convenience methods for accessing functions and methods. |
paul@184 | 1384 | |
paul@83 | 1385 | def get_instantiator(self): |
paul@83 | 1386 | |
paul@83 | 1387 | "Return a function which can be used to instantiate the class." |
paul@83 | 1388 | |
paul@83 | 1389 | if self.instantiator is None: |
paul@212 | 1390 | self.instantiator = self.get_init_method().as_instantiator() |
paul@83 | 1391 | return self.instantiator |
paul@83 | 1392 | |
paul@137 | 1393 | def get_init_method(self): |
paul@192 | 1394 | return self.all_class_attributes()["__init__"].get_value() |
paul@137 | 1395 | |
paul@83 | 1396 | # Class-specific methods. |
paul@83 | 1397 | |
paul@83 | 1398 | def add_base(self, base): |
paul@83 | 1399 | self.bases.append(base) |
paul@83 | 1400 | base.add_descendant(self) |
paul@83 | 1401 | |
paul@527 | 1402 | def add_instance_attribute(self, name, tentative=False): |
paul@527 | 1403 | if tentative: |
paul@527 | 1404 | self.instattr_tentative.add(name) |
paul@527 | 1405 | else: |
paul@527 | 1406 | self.instattr.add(name) |
paul@83 | 1407 | |
paul@83 | 1408 | def add_descendant(self, cls): |
paul@83 | 1409 | self.descendants.add(cls) |
paul@83 | 1410 | for base in self.bases: |
paul@83 | 1411 | base.add_descendant(cls) |
paul@83 | 1412 | |
paul@122 | 1413 | def has_subclass(self, other): |
paul@122 | 1414 | return other in self.descendants |
paul@122 | 1415 | |
paul@135 | 1416 | def all_descendants(self): |
paul@135 | 1417 | d = {} |
paul@135 | 1418 | for cls in self.descendants: |
paul@135 | 1419 | d[cls.full_name()] = cls |
paul@135 | 1420 | return d |
paul@135 | 1421 | |
paul@83 | 1422 | "Return the attribute names provided by this class only." |
paul@83 | 1423 | |
paul@83 | 1424 | class_attribute_names = NamespaceDict.keys |
paul@83 | 1425 | |
paul@83 | 1426 | def class_attributes(self): |
paul@83 | 1427 | |
paul@83 | 1428 | "Return class attributes provided by this class only." |
paul@83 | 1429 | |
paul@83 | 1430 | return dict(self) |
paul@83 | 1431 | |
paul@83 | 1432 | def all_class_attribute_names(self): |
paul@83 | 1433 | |
paul@83 | 1434 | "Return the attribute names provided by classes in this hierarchy." |
paul@83 | 1435 | |
paul@83 | 1436 | if self.all_classattr_names is None: |
paul@83 | 1437 | self.all_class_attributes() |
paul@282 | 1438 | self.all_classattr_names = self.all_classattr.keys() |
paul@83 | 1439 | return self.all_classattr_names |
paul@83 | 1440 | |
paul@83 | 1441 | def all_class_attributes(self): |
paul@83 | 1442 | |
paul@83 | 1443 | "Return all class attributes, indicating the class which provides them." |
paul@83 | 1444 | |
paul@83 | 1445 | self.finalise_class_attributes() |
paul@83 | 1446 | return self.all_classattr |
paul@83 | 1447 | |
paul@83 | 1448 | def finalise_class_attributes(self): |
paul@83 | 1449 | |
paul@83 | 1450 | "Make sure that the class attributes are fully defined." |
paul@83 | 1451 | |
paul@83 | 1452 | if self.all_classattr is None: |
paul@83 | 1453 | self.all_classattr = {} |
paul@83 | 1454 | clsattr = {} |
paul@83 | 1455 | |
paul@83 | 1456 | # Record provisional position information for attributes of this |
paul@83 | 1457 | # class. |
paul@83 | 1458 | |
paul@83 | 1459 | for name in self.class_attributes().keys(): |
paul@396 | 1460 | |
paul@396 | 1461 | # Special case: __class__ has to be at position 0. |
paul@396 | 1462 | |
paul@396 | 1463 | if name == "__class__": |
paul@396 | 1464 | clsattr[name] = set([0]) |
paul@396 | 1465 | else: |
paul@396 | 1466 | clsattr[name] = set() # position not yet defined |
paul@83 | 1467 | |
paul@83 | 1468 | reversed_bases = self.bases[:] |
paul@83 | 1469 | reversed_bases.reverse() |
paul@83 | 1470 | |
paul@83 | 1471 | # For the bases in reverse order, acquire class attribute details. |
paul@83 | 1472 | |
paul@83 | 1473 | for cls in reversed_bases: |
paul@83 | 1474 | for name, attr in cls.all_class_attributes().items(): |
paul@83 | 1475 | self.all_classattr[name] = attr |
paul@83 | 1476 | |
paul@83 | 1477 | # Record previous attribute information. |
paul@83 | 1478 | |
paul@83 | 1479 | if clsattr.has_key(name): |
paul@83 | 1480 | clsattr[name].add(attr.position) |
paul@83 | 1481 | |
paul@83 | 1482 | # Record class attributes provided by this class and its bases, |
paul@83 | 1483 | # along with their positions. |
paul@83 | 1484 | |
paul@83 | 1485 | self.all_classattr.update(self.class_attributes()) |
paul@83 | 1486 | |
paul@83 | 1487 | if clsattr: |
paul@83 | 1488 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@83 | 1489 | self.all_classattr[name].position = i |
paul@83 | 1490 | |
paul@83 | 1491 | return self.all_classattr |
paul@83 | 1492 | |
paul@83 | 1493 | def instance_attribute_names(self): |
paul@83 | 1494 | |
paul@83 | 1495 | "Return the instance attribute names provided by the class." |
paul@83 | 1496 | |
paul@83 | 1497 | if self.all_instattr_names is None: |
paul@83 | 1498 | self.instance_attributes() |
paul@83 | 1499 | return self.all_instattr_names |
paul@83 | 1500 | |
paul@83 | 1501 | def instance_attributes(self): |
paul@83 | 1502 | |
paul@83 | 1503 | "Return instance-only attributes for instances of this class." |
paul@83 | 1504 | |
paul@83 | 1505 | self.finalise_instance_attributes() |
paul@83 | 1506 | return self.all_instattr |
paul@83 | 1507 | |
paul@83 | 1508 | def finalise_instance_attributes(self): |
paul@83 | 1509 | |
paul@83 | 1510 | "Make sure that the instance attributes are fully defined." |
paul@83 | 1511 | |
paul@527 | 1512 | # Eliminate tentative instance attributes that are actually class |
paul@527 | 1513 | # attributes. |
paul@527 | 1514 | |
paul@527 | 1515 | for attrname in self.all_class_attributes().keys(): |
paul@527 | 1516 | if attrname in self.instattr_tentative: |
paul@527 | 1517 | self.instattr_tentative.remove(attrname) |
paul@527 | 1518 | |
paul@527 | 1519 | for cls in self.descendants: |
paul@527 | 1520 | for attrname in cls.class_attribute_names(): |
paul@527 | 1521 | if attrname in self.instattr_tentative: |
paul@527 | 1522 | self.instattr_tentative.remove(attrname) |
paul@527 | 1523 | |
paul@527 | 1524 | for attrname in self.instattr_tentative: |
paul@527 | 1525 | self.instattr.add(attrname) |
paul@527 | 1526 | |
paul@210 | 1527 | # Cache the attributes by converting the positioned attributes into a |
paul@210 | 1528 | # dictionary. |
paul@83 | 1529 | |
paul@210 | 1530 | if self.all_instattr is None: |
paul@210 | 1531 | self.all_instattr = self._get_attributes() |
paul@83 | 1532 | self.all_instattr_names = self.all_instattr.keys() |
paul@83 | 1533 | |
paul@83 | 1534 | return self.all_instattr |
paul@83 | 1535 | |
paul@210 | 1536 | def _get_attributes(self): |
paul@210 | 1537 | |
paul@210 | 1538 | """ |
paul@210 | 1539 | Return a dictionary mapping names to Attr instances incorporating |
paul@210 | 1540 | information about their positions in the final instance structure. |
paul@210 | 1541 | """ |
paul@210 | 1542 | |
paul@210 | 1543 | instattr = {} |
paul@210 | 1544 | |
paul@210 | 1545 | # Record provisional position information for attributes of this |
paul@210 | 1546 | # instance. |
paul@210 | 1547 | |
paul@210 | 1548 | for name in self.instattr: |
paul@401 | 1549 | instattr[name] = set() # position not yet defined |
paul@210 | 1550 | |
paul@210 | 1551 | reversed_bases = self.bases[:] |
paul@210 | 1552 | reversed_bases.reverse() |
paul@210 | 1553 | |
paul@210 | 1554 | # For the bases in reverse order, acquire instance attribute |
paul@210 | 1555 | # details. |
paul@210 | 1556 | |
paul@210 | 1557 | for cls in reversed_bases: |
paul@210 | 1558 | for name, attr in cls.instance_attributes().items(): |
paul@210 | 1559 | |
paul@210 | 1560 | # Record previous attribute information. |
paul@210 | 1561 | |
paul@210 | 1562 | if instattr.has_key(name): |
paul@210 | 1563 | instattr[name].add(attr.position) |
paul@235 | 1564 | else: |
paul@235 | 1565 | instattr[name] = set([attr.position]) |
paul@210 | 1566 | |
paul@210 | 1567 | # Build the dictionary of attributes using the existing positions known |
paul@210 | 1568 | # for each name. |
paul@210 | 1569 | |
paul@210 | 1570 | d = {} |
paul@210 | 1571 | for i, name in enumerate(self._get_position_list(instattr)): |
paul@448 | 1572 | d[name] = Attr(i, make_instance(), name) |
paul@210 | 1573 | return d |
paul@210 | 1574 | |
paul@83 | 1575 | def _get_position_list(self, positions): |
paul@83 | 1576 | |
paul@83 | 1577 | """ |
paul@83 | 1578 | Return a list of attribute names for the given 'positions' mapping from |
paul@83 | 1579 | names to positions, indicating the positions of the attributes in the |
paul@83 | 1580 | final instance structure. |
paul@83 | 1581 | """ |
paul@83 | 1582 | |
paul@83 | 1583 | position_items = positions.items() |
paul@83 | 1584 | namearray = [None] * len(position_items) |
paul@83 | 1585 | |
paul@83 | 1586 | # Get the positions in ascending order of list size, with lists |
paul@83 | 1587 | # of the same size ordered according to their smallest position |
paul@83 | 1588 | # value. |
paul@83 | 1589 | |
paul@83 | 1590 | position_items.sort(self._cmp_positions) |
paul@83 | 1591 | |
paul@83 | 1592 | # Get the names in position order. |
paul@83 | 1593 | |
paul@83 | 1594 | held = [] |
paul@83 | 1595 | |
paul@83 | 1596 | for name, pos in position_items: |
paul@83 | 1597 | pos = list(pos) |
paul@83 | 1598 | pos.sort() |
paul@83 | 1599 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@83 | 1600 | namearray[pos[0]] = name |
paul@83 | 1601 | else: |
paul@83 | 1602 | if pos: |
paul@83 | 1603 | self.relocated.add(name) |
paul@83 | 1604 | held.append((name, pos)) |
paul@83 | 1605 | |
paul@83 | 1606 | for i, attr in enumerate(namearray): |
paul@83 | 1607 | if attr is None: |
paul@83 | 1608 | name, pos = held.pop() |
paul@83 | 1609 | namearray[i] = name |
paul@83 | 1610 | |
paul@83 | 1611 | return namearray |
paul@83 | 1612 | |
paul@83 | 1613 | def _cmp_positions(self, a, b): |
paul@83 | 1614 | |
paul@83 | 1615 | "Compare name plus position list operands 'a' and 'b'." |
paul@83 | 1616 | |
paul@83 | 1617 | name_a, list_a = a |
paul@83 | 1618 | name_b, list_b = b |
paul@83 | 1619 | if len(list_a) < len(list_b): |
paul@83 | 1620 | return -1 |
paul@83 | 1621 | elif len(list_a) > len(list_b): |
paul@83 | 1622 | return 1 |
paul@83 | 1623 | elif not list_a: |
paul@83 | 1624 | return 0 |
paul@83 | 1625 | else: |
paul@83 | 1626 | return cmp(min(list_a), min(list_b)) |
paul@83 | 1627 | |
paul@83 | 1628 | def all_attribute_names(self): |
paul@83 | 1629 | |
paul@83 | 1630 | """ |
paul@83 | 1631 | Return the names of all attributes provided by instances of this class. |
paul@83 | 1632 | """ |
paul@83 | 1633 | |
paul@83 | 1634 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@83 | 1635 | return self.allattr_names |
paul@83 | 1636 | |
paul@83 | 1637 | def all_attributes(self): |
paul@83 | 1638 | |
paul@83 | 1639 | """ |
paul@83 | 1640 | Return all attributes for an instance, indicating either the class which |
paul@83 | 1641 | provides them or that the instance itself provides them. |
paul@394 | 1642 | |
paul@401 | 1643 | Note that __class__ acts like a class attribute for both instances and |
paul@401 | 1644 | classes, and must be able to convey distinct values. |
paul@83 | 1645 | """ |
paul@83 | 1646 | |
paul@83 | 1647 | if self.allattr is None: |
paul@83 | 1648 | self.allattr = {} |
paul@83 | 1649 | self.allattr.update(self.all_class_attributes()) |
paul@83 | 1650 | for name, attr in self.instance_attributes().items(): |
paul@394 | 1651 | if self.allattr.has_key(name) and name != "__class__": |
paul@507 | 1652 | print >>sys.stderr, "Warning: instance attribute %r in %r overrides class attribute." % (name, self) |
paul@83 | 1653 | self.allattr[name] = attr |
paul@83 | 1654 | return self.allattr |
paul@83 | 1655 | |
paul@394 | 1656 | class TypeClass(Class): |
paul@394 | 1657 | |
paul@394 | 1658 | "A special class for the type class." |
paul@394 | 1659 | |
paul@401 | 1660 | pass |
paul@394 | 1661 | |
paul@394 | 1662 | class CommonClass(Class): |
paul@394 | 1663 | |
paul@394 | 1664 | "An inspected class." |
paul@394 | 1665 | |
paul@401 | 1666 | pass |
paul@394 | 1667 | |
paul@85 | 1668 | class Function(NamespaceDict, Naming, Constant): |
paul@83 | 1669 | |
paul@83 | 1670 | "An inspected function." |
paul@83 | 1671 | |
paul@331 | 1672 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, |
paul@331 | 1673 | dynamic_def=0, module=None, node=None): |
paul@83 | 1674 | |
paul@83 | 1675 | """ |
paul@83 | 1676 | Initialise the function with the given 'name', 'parent', list of |
paul@83 | 1677 | 'argnames', list of 'defaults', the 'has_star' flag (indicating the |
paul@83 | 1678 | presence of a * parameter), the 'has_dstar' flag (indicating the |
paul@351 | 1679 | presence of a ** parameter), optional 'dynamic_def' (indicating that the |
paul@351 | 1680 | function must be handled dynamically), optional 'module', and optional |
paul@351 | 1681 | AST 'node'. |
paul@83 | 1682 | """ |
paul@83 | 1683 | |
paul@180 | 1684 | NamespaceDict.__init__(self, module) |
paul@351 | 1685 | |
paul@351 | 1686 | if name is None: |
paul@351 | 1687 | self.name = "lambda#%d" % new_lambda() |
paul@351 | 1688 | self._is_lambda = 1 |
paul@351 | 1689 | else: |
paul@351 | 1690 | self.name = name |
paul@351 | 1691 | self._is_lambda = 0 |
paul@351 | 1692 | |
paul@83 | 1693 | self.parent = parent |
paul@83 | 1694 | self.argnames = argnames |
paul@83 | 1695 | self.defaults = defaults |
paul@83 | 1696 | self.has_star = has_star |
paul@83 | 1697 | self.has_dstar = has_dstar |
paul@331 | 1698 | self.dynamic_def = dynamic_def |
paul@91 | 1699 | self.astnode = node |
paul@83 | 1700 | |
paul@83 | 1701 | # Initialise the positional names. |
paul@83 | 1702 | |
paul@83 | 1703 | self.positional_names = self.argnames[:] |
paul@83 | 1704 | if has_dstar: |
paul@83 | 1705 | self.dstar_name = self.positional_names[-1] |
paul@83 | 1706 | del self.positional_names[-1] |
paul@83 | 1707 | if has_star: |
paul@83 | 1708 | self.star_name = self.positional_names[-1] |
paul@83 | 1709 | del self.positional_names[-1] |
paul@83 | 1710 | |
paul@83 | 1711 | # Initialise default storage. |
paul@83 | 1712 | # NOTE: This must be initialised separately due to the reliance on node |
paul@83 | 1713 | # NOTE: visiting. |
paul@83 | 1714 | |
paul@83 | 1715 | self.default_attrs = [] |
paul@83 | 1716 | |
paul@272 | 1717 | # Initialise attribute usage. |
paul@272 | 1718 | |
paul@358 | 1719 | if node is not None: |
paul@358 | 1720 | for arg in argnames: |
paul@279 | 1721 | |
paul@358 | 1722 | # Define attribute users. |
paul@279 | 1723 | |
paul@279 | 1724 | self._define_attribute_user_for_name(node, arg) |
paul@279 | 1725 | |
paul@83 | 1726 | # Caches. |
paul@83 | 1727 | |
paul@83 | 1728 | self.localnames = None # cache for locals |
paul@83 | 1729 | |
paul@83 | 1730 | # Add parameters to the namespace. |
paul@83 | 1731 | |
paul@83 | 1732 | self._add_parameters(argnames) |
paul@83 | 1733 | |
paul@83 | 1734 | # Image generation details. |
paul@83 | 1735 | |
paul@330 | 1736 | self.dynamic = None |
paul@83 | 1737 | self.location = None |
paul@83 | 1738 | self.code_location = None |
paul@240 | 1739 | self.code_body_location = None |
paul@83 | 1740 | |
paul@83 | 1741 | # Program-related details. |
paul@83 | 1742 | |
paul@173 | 1743 | self.blocks = None |
paul@225 | 1744 | self.body_block = None |
paul@225 | 1745 | |
paul@116 | 1746 | self.temp_usage = 0 |
paul@116 | 1747 | self.local_usage = 0 |
paul@133 | 1748 | self.all_local_usage = 0 |
paul@83 | 1749 | |
paul@83 | 1750 | def _add_parameters(self, argnames): |
paul@368 | 1751 | |
paul@368 | 1752 | "Add 'argnames' to the namespace." |
paul@368 | 1753 | |
paul@368 | 1754 | for name in argnames: |
paul@448 | 1755 | self.set(name, make_instance()) |
paul@368 | 1756 | |
paul@368 | 1757 | for name, top_level in self._flattened_parameters(argnames): |
paul@368 | 1758 | if not top_level: |
paul@448 | 1759 | self.set(name, make_instance()) |
paul@368 | 1760 | |
paul@368 | 1761 | def _flattened_parameters(self, argnames, top_level=1): |
paul@368 | 1762 | l = [] |
paul@83 | 1763 | for name in argnames: |
paul@83 | 1764 | if isinstance(name, tuple): |
paul@368 | 1765 | l += self._flattened_parameters(name, 0) |
paul@83 | 1766 | else: |
paul@368 | 1767 | l.append((name, top_level)) |
paul@368 | 1768 | return l |
paul@83 | 1769 | |
paul@83 | 1770 | def __repr__(self): |
paul@83 | 1771 | if self.location is not None: |
paul@494 | 1772 | return "<function %s (at %r, code at %r)>" % ( |
paul@494 | 1773 | shortrepr(self), self.location, self.code_location |
paul@83 | 1774 | ) |
paul@83 | 1775 | else: |
paul@494 | 1776 | return "<function %s>" % shortrepr(self) |
paul@83 | 1777 | |
paul@87 | 1778 | def __shortrepr__(self): |
paul@494 | 1779 | return "%s.%s(%s)" % (shortrepr(self.parent), self.name, ", ".join(self.argnames)) |
paul@87 | 1780 | |
paul@227 | 1781 | def get_body_block(self): |
paul@227 | 1782 | return self.body_block |
paul@215 | 1783 | |
paul@351 | 1784 | def is_lambda(self): |
paul@351 | 1785 | return self._is_lambda |
paul@351 | 1786 | |
paul@330 | 1787 | # Defaults-related methods. |
paul@189 | 1788 | |
paul@329 | 1789 | def store_default(self, attr_or_value): |
paul@329 | 1790 | |
paul@329 | 1791 | """ |
paul@329 | 1792 | Reserve space for defaults, set outside the function, potentially on a |
paul@329 | 1793 | dynamic basis, using the 'attr_or_value'. |
paul@329 | 1794 | """ |
paul@329 | 1795 | |
paul@192 | 1796 | attr = Attr(None, self, None) |
paul@329 | 1797 | self._set_using_attr(attr, attr_or_value) |
paul@83 | 1798 | self.default_attrs.append(attr) |
paul@83 | 1799 | |
paul@330 | 1800 | def make_dynamic(self): |
paul@330 | 1801 | |
paul@330 | 1802 | "Return whether this function must be handled using a dynamic object." |
paul@330 | 1803 | |
paul@330 | 1804 | if self.dynamic is None: |
paul@330 | 1805 | for attr in self.default_attrs: |
paul@331 | 1806 | if not attr.is_strict_constant() and self.dynamic_def: |
paul@330 | 1807 | self.dynamic = 1 |
paul@330 | 1808 | self._make_dynamic() |
paul@330 | 1809 | break |
paul@330 | 1810 | else: |
paul@330 | 1811 | self.dynamic = 0 |
paul@330 | 1812 | |
paul@330 | 1813 | return self.dynamic |
paul@330 | 1814 | |
paul@330 | 1815 | is_dynamic = make_dynamic |
paul@330 | 1816 | |
paul@330 | 1817 | def _make_dynamic(self): |
paul@330 | 1818 | |
paul@330 | 1819 | "Where functions have dynamic defaults, add a context argument." |
paul@330 | 1820 | |
paul@330 | 1821 | name = "<context>" |
paul@330 | 1822 | self.argnames.insert(0, name) |
paul@330 | 1823 | self.positional_names.insert(0, name) |
paul@448 | 1824 | self.set(name, make_instance()) |
paul@330 | 1825 | |
paul@330 | 1826 | # Namespace-related methods. |
paul@330 | 1827 | |
paul@83 | 1828 | def make_global(self, name): |
paul@339 | 1829 | |
paul@339 | 1830 | "Declare 'name' as a global in the current namespace." |
paul@339 | 1831 | |
paul@83 | 1832 | if name not in self.argnames and not self.has_key(name): |
paul@83 | 1833 | self.globals.add(name) |
paul@178 | 1834 | return 1 |
paul@83 | 1835 | else: |
paul@178 | 1836 | return 0 |
paul@83 | 1837 | |
paul@83 | 1838 | def parameters(self): |
paul@83 | 1839 | |
paul@83 | 1840 | """ |
paul@83 | 1841 | Return a dictionary mapping parameter names to their position in the |
paul@83 | 1842 | parameter list. |
paul@83 | 1843 | """ |
paul@83 | 1844 | |
paul@83 | 1845 | parameters = {} |
paul@83 | 1846 | for i, name in enumerate(self.argnames): |
paul@83 | 1847 | parameters[name] = i |
paul@83 | 1848 | return parameters |
paul@83 | 1849 | |
paul@368 | 1850 | def tuple_parameters(self, argnames=None): |
paul@368 | 1851 | |
paul@368 | 1852 | """ |
paul@368 | 1853 | Return a list of (position, parameter) entries corresponding to tuple |
paul@368 | 1854 | parameters, where each parameter may either be a string or another such |
paul@368 | 1855 | list of entries. |
paul@368 | 1856 | """ |
paul@368 | 1857 | |
paul@368 | 1858 | names = argnames or self.argnames |
paul@368 | 1859 | |
paul@368 | 1860 | l = [] |
paul@368 | 1861 | for i, name in enumerate(names): |
paul@368 | 1862 | if isinstance(name, tuple): |
paul@368 | 1863 | l.append((i, self.tuple_parameters(name))) |
paul@368 | 1864 | elif argnames: |
paul@368 | 1865 | l.append((i, name)) |
paul@368 | 1866 | return l |
paul@368 | 1867 | |
paul@83 | 1868 | def all_locals(self): |
paul@83 | 1869 | |
paul@83 | 1870 | "Return a dictionary mapping names to local and parameter details." |
paul@83 | 1871 | |
paul@83 | 1872 | return dict(self) |
paul@83 | 1873 | |
paul@83 | 1874 | def locals(self): |
paul@83 | 1875 | |
paul@83 | 1876 | "Return a dictionary mapping names to local details." |
paul@83 | 1877 | |
paul@83 | 1878 | if self.localnames is None: |
paul@83 | 1879 | self.localnames = {} |
paul@83 | 1880 | self.localnames.update(self.all_locals()) |
paul@83 | 1881 | for name in self.argnames: |
paul@83 | 1882 | del self.localnames[name] |
paul@83 | 1883 | return self.localnames |
paul@83 | 1884 | |
paul@83 | 1885 | def is_method(self): |
paul@83 | 1886 | |
paul@241 | 1887 | """ |
paul@241 | 1888 | Return whether this function is a method explicitly defined in a class. |
paul@241 | 1889 | """ |
paul@83 | 1890 | |
paul@83 | 1891 | return isinstance(self.parent, Class) |
paul@83 | 1892 | |
paul@83 | 1893 | def is_relocated(self, name): |
paul@83 | 1894 | |
paul@83 | 1895 | """ |
paul@83 | 1896 | Determine whether the given attribute 'name' is relocated for instances |
paul@83 | 1897 | having this function as a method. |
paul@83 | 1898 | """ |
paul@83 | 1899 | |
paul@83 | 1900 | for cls in self.parent.descendants: |
paul@83 | 1901 | if name in cls.relocated: |
paul@83 | 1902 | return 1 |
paul@83 | 1903 | return 0 |
paul@83 | 1904 | |
paul@351 | 1905 | # Administrative methods. |
paul@351 | 1906 | |
paul@351 | 1907 | def items_for_vacuum(self): |
paul@351 | 1908 | return self.lambdas.items() |
paul@351 | 1909 | |
paul@351 | 1910 | def vacuum_item(self, name): |
paul@351 | 1911 | del self.lambdas[name] |
paul@394 | 1912 | return 1 |
paul@351 | 1913 | |
paul@287 | 1914 | def finalise_attributes(self): |
paul@83 | 1915 | |
paul@83 | 1916 | """ |
paul@83 | 1917 | Make sure all attributes (locals) are fully defined. Note that locals |
paul@83 | 1918 | are not attributes in the sense of class, module or instance attributes. |
paul@83 | 1919 | Defaults are also finalised by this method. |
paul@83 | 1920 | """ |
paul@83 | 1921 | |
paul@210 | 1922 | if self.finalised: |
paul@210 | 1923 | return |
paul@210 | 1924 | |
paul@210 | 1925 | # Defaults. |
paul@210 | 1926 | |
paul@83 | 1927 | for i, default in enumerate(self.default_attrs): |
paul@83 | 1928 | default.position = i |
paul@83 | 1929 | |
paul@368 | 1930 | # Parameters. |
paul@210 | 1931 | |
paul@368 | 1932 | i = self._finalise_parameters() |
paul@83 | 1933 | |
paul@83 | 1934 | if i is not None: |
paul@144 | 1935 | nparams = i + 1 |
paul@83 | 1936 | else: |
paul@144 | 1937 | nparams = 0 |
paul@144 | 1938 | |
paul@368 | 1939 | # Locals (and tuple parameter names). |
paul@368 | 1940 | |
paul@144 | 1941 | i = None |
paul@144 | 1942 | for i, attr in enumerate(self.locals().values()): |
paul@144 | 1943 | attr.position = i + nparams |
paul@83 | 1944 | |
paul@144 | 1945 | if i is not None: |
paul@144 | 1946 | nothers = i + 1 |
paul@144 | 1947 | else: |
paul@144 | 1948 | nothers = 0 |
paul@83 | 1949 | |
paul@144 | 1950 | self.local_usage = nothers |
paul@144 | 1951 | self.all_local_usage = nparams + nothers |
paul@92 | 1952 | self.finalised = 1 |
paul@83 | 1953 | |
paul@368 | 1954 | def _finalise_parameters(self): |
paul@368 | 1955 | if not self.argnames: |
paul@368 | 1956 | return None |
paul@368 | 1957 | |
paul@368 | 1958 | for i, name in enumerate(self.argnames): |
paul@368 | 1959 | self[name].position = i |
paul@368 | 1960 | |
paul@368 | 1961 | return i |
paul@368 | 1962 | |
paul@212 | 1963 | def as_instantiator(self): |
paul@83 | 1964 | |
paul@230 | 1965 | "Make an instantiator function from a method, keeping all arguments." |
paul@83 | 1966 | |
paul@230 | 1967 | function = Function(self.parent.name, self.parent.parent, self.argnames, self.defaults, |
paul@358 | 1968 | self.has_star, self.has_dstar, self.dynamic_def, self.module) |
paul@83 | 1969 | function.default_attrs = self.default_attrs |
paul@83 | 1970 | return function |
paul@83 | 1971 | |
paul@85 | 1972 | class UnresolvedName(NamespaceDict, Constant): |
paul@83 | 1973 | |
paul@83 | 1974 | "A module, class or function which was mentioned but could not be imported." |
paul@83 | 1975 | |
paul@180 | 1976 | def __init__(self, name, parent_name, module=None): |
paul@180 | 1977 | NamespaceDict.__init__(self, module) |
paul@83 | 1978 | self.name = name |
paul@83 | 1979 | self.parent_name = parent_name |
paul@88 | 1980 | self.parent = None |
paul@167 | 1981 | |
paul@167 | 1982 | self.descendants = set() |
paul@167 | 1983 | |
paul@167 | 1984 | def add_descendant(self, cls): |
paul@167 | 1985 | self.descendants.add(cls) |
paul@83 | 1986 | |
paul@363 | 1987 | def all_attributes(self): |
paul@83 | 1988 | return {} |
paul@83 | 1989 | |
paul@363 | 1990 | def all_attribute_names(self): |
paul@363 | 1991 | return [] |
paul@363 | 1992 | |
paul@363 | 1993 | all_class_attributes = class_attributes = instance_attributes = all_attributes |
paul@363 | 1994 | all_class_attribute_names = class_attribute_names = instance_attribute_names = all_attribute_names |
paul@83 | 1995 | |
paul@83 | 1996 | def __repr__(self): |
paul@494 | 1997 | return "<unknown %s>" % shortrepr(self) |
paul@494 | 1998 | |
paul@494 | 1999 | def __shortrepr__(self): |
paul@494 | 2000 | return "%s.%s" % (self.parent_name, self.name) |
paul@87 | 2001 | |
paul@83 | 2002 | def full_name(self): |
paul@83 | 2003 | if self.name is not None: |
paul@83 | 2004 | return self.parent_name + "." + self.name |
paul@83 | 2005 | else: |
paul@83 | 2006 | return self.parent_name |
paul@83 | 2007 | |
paul@85 | 2008 | class Module(NamespaceDict, Constant): |
paul@83 | 2009 | |
paul@83 | 2010 | "An inspected module's core details." |
paul@83 | 2011 | |
paul@343 | 2012 | def __init__(self, name, importer): |
paul@83 | 2013 | NamespaceDict.__init__(self, self) |
paul@83 | 2014 | self.name = name |
paul@343 | 2015 | self.importer = importer |
paul@88 | 2016 | self.parent = None |
paul@83 | 2017 | |
paul@83 | 2018 | # Original location details. |
paul@83 | 2019 | |
paul@91 | 2020 | self.astnode = None |
paul@83 | 2021 | |
paul@83 | 2022 | # Complete lists of classes and functions. |
paul@83 | 2023 | |
paul@83 | 2024 | self.all_objects = set() |
paul@83 | 2025 | |
paul@576 | 2026 | # A set of global names that cannot support combined attribute usage |
paul@576 | 2027 | # observations because they may be modified within functions during |
paul@576 | 2028 | # initialisation. |
paul@576 | 2029 | |
paul@576 | 2030 | self.modified_names = set() |
paul@575 | 2031 | |
paul@83 | 2032 | # Keyword records. |
paul@83 | 2033 | |
paul@83 | 2034 | self.keyword_names = set() |
paul@83 | 2035 | |
paul@83 | 2036 | # Image generation details. |
paul@83 | 2037 | |
paul@83 | 2038 | self.location = None |
paul@83 | 2039 | self.code_location = None |
paul@83 | 2040 | |
paul@83 | 2041 | # Program-related details. |
paul@83 | 2042 | |
paul@173 | 2043 | self.blocks = None |
paul@116 | 2044 | self.temp_usage = 0 |
paul@116 | 2045 | self.local_usage = 0 |
paul@133 | 2046 | self.all_local_usage = 0 |
paul@83 | 2047 | |
paul@83 | 2048 | def full_name(self): |
paul@83 | 2049 | return self.name |
paul@83 | 2050 | |
paul@83 | 2051 | def __repr__(self): |
paul@83 | 2052 | if self.location is not None: |
paul@494 | 2053 | return "<module %s (at %r)>" % (self.name, self.location) |
paul@83 | 2054 | else: |
paul@494 | 2055 | return "<module %s>" % shortrepr(self) |
paul@83 | 2056 | |
paul@87 | 2057 | def __shortrepr__(self): |
paul@494 | 2058 | return self.name |
paul@87 | 2059 | |
paul@83 | 2060 | # Attribute methods. |
paul@83 | 2061 | |
paul@83 | 2062 | "Return the module attribute names provided by the module." |
paul@83 | 2063 | |
paul@83 | 2064 | module_attribute_names = NamespaceDict.keys |
paul@83 | 2065 | |
paul@83 | 2066 | def module_attributes(self): |
paul@83 | 2067 | |
paul@83 | 2068 | "Return a dictionary mapping names to module attributes." |
paul@83 | 2069 | |
paul@83 | 2070 | return dict(self) |
paul@83 | 2071 | |
paul@578 | 2072 | def modify_name(self, name): |
paul@578 | 2073 | |
paul@578 | 2074 | """ |
paul@578 | 2075 | Modify a global 'name' invalidating various assumptions about its |
paul@578 | 2076 | behaviour based on the module namespace being "safe" and suitable for |
paul@578 | 2077 | attribute usage and constant value observations. |
paul@578 | 2078 | """ |
paul@578 | 2079 | |
paul@578 | 2080 | self.modified_names.add(name) |
paul@578 | 2081 | |
paul@578 | 2082 | # Establish an attribute directly in the namespace if not present. |
paul@578 | 2083 | |
paul@578 | 2084 | if not self.namespace.has_key(name): |
paul@578 | 2085 | self.namespace[name] = Attr(None, self, name) |
paul@578 | 2086 | |
paul@578 | 2087 | attr = self.namespace[name] |
paul@578 | 2088 | attr.update_assignments(1, False) |
paul@578 | 2089 | |
paul@575 | 2090 | # Attribute usage methods that apply to module globals in certain |
paul@575 | 2091 | # circumstances. |
paul@575 | 2092 | |
paul@576 | 2093 | def _use_attribute(self, name, attrname, value=None): |
paul@576 | 2094 | if name not in self.modified_names: |
paul@576 | 2095 | return NamespaceDict._use_attribute(self, name, attrname, value) |
paul@576 | 2096 | else: |
paul@577 | 2097 | self.importer.use_name(attrname, self.full_name(), value) |
paul@576 | 2098 | return [] |
paul@576 | 2099 | |
paul@576 | 2100 | def _define_attribute_user_for_name(self, node, name): |
paul@576 | 2101 | if name not in self.modified_names: |
paul@576 | 2102 | NamespaceDict._define_attribute_user_for_name(self, node, name) |
paul@576 | 2103 | |
paul@576 | 2104 | def _init_attribute_user_for_name(self, node, name): |
paul@576 | 2105 | if name not in self.modified_names: |
paul@576 | 2106 | NamespaceDict._init_attribute_user_for_name(self, node, name) |
paul@576 | 2107 | |
paul@576 | 2108 | def _define_attribute_accessor(self, name, attrname, node, value): |
paul@576 | 2109 | if name not in self.modified_names: |
paul@576 | 2110 | NamespaceDict._define_attribute_accessor(self, name, attrname, node, value) |
paul@577 | 2111 | else: |
paul@577 | 2112 | self.importer.use_name(attrname, self.full_name(), value) |
paul@575 | 2113 | |
paul@394 | 2114 | # Pre-made instances. |
paul@394 | 2115 | |
paul@394 | 2116 | type_class = TypeClass("type") # details to be filled in later |
paul@394 | 2117 | |
paul@394 | 2118 | # Class construction. |
paul@394 | 2119 | |
paul@394 | 2120 | def get_class(name, parent, module, node): |
paul@394 | 2121 | |
paul@394 | 2122 | """ |
paul@394 | 2123 | Return a Class instance for the class with the given 'name', 'parent', |
paul@394 | 2124 | 'module' and 'node'. |
paul@394 | 2125 | """ |
paul@394 | 2126 | |
paul@394 | 2127 | if name == "type" and module.full_name() == "__builtins__": |
paul@394 | 2128 | type_class.set_context(parent, module, node) |
paul@394 | 2129 | return type_class |
paul@394 | 2130 | else: |
paul@394 | 2131 | return CommonClass(name, parent, module, node) |
paul@394 | 2132 | |
paul@554 | 2133 | # Lambda sequence numbering. |
paul@554 | 2134 | |
paul@554 | 2135 | lambda_index = 0 |
paul@554 | 2136 | |
paul@554 | 2137 | def new_lambda(): |
paul@554 | 2138 | |
paul@554 | 2139 | "Return a new sequence number for a lambda definition." |
paul@554 | 2140 | |
paul@554 | 2141 | global lambda_index |
paul@554 | 2142 | lambda_index += 1 |
paul@554 | 2143 | return lambda_index |
paul@554 | 2144 | |
paul@555 | 2145 | # Special representations. |
paul@555 | 2146 | |
paul@555 | 2147 | class AtLeast: |
paul@555 | 2148 | |
paul@555 | 2149 | "A special representation for numbers of a given value or greater." |
paul@555 | 2150 | |
paul@555 | 2151 | def __init__(self, count): |
paul@555 | 2152 | self.count = count |
paul@555 | 2153 | |
paul@555 | 2154 | def __eq__(self, other): |
paul@555 | 2155 | return 0 |
paul@555 | 2156 | |
paul@555 | 2157 | __lt__ = __le__ = __eq__ |
paul@555 | 2158 | |
paul@555 | 2159 | def __ne__(self, other): |
paul@555 | 2160 | return 1 |
paul@555 | 2161 | |
paul@555 | 2162 | def __gt__(self, other): |
paul@555 | 2163 | if isinstance(other, AtLeast): |
paul@555 | 2164 | return 0 |
paul@555 | 2165 | else: |
paul@555 | 2166 | return self.count > other |
paul@555 | 2167 | |
paul@555 | 2168 | def __ge__(self, other): |
paul@555 | 2169 | if isinstance(other, AtLeast): |
paul@555 | 2170 | return 0 |
paul@555 | 2171 | else: |
paul@555 | 2172 | return self.count >= other |
paul@555 | 2173 | |
paul@555 | 2174 | def __iadd__(self, other): |
paul@555 | 2175 | if isinstance(other, AtLeast): |
paul@555 | 2176 | self.count += other.count |
paul@555 | 2177 | else: |
paul@555 | 2178 | self.count += other |
paul@555 | 2179 | return self |
paul@555 | 2180 | |
paul@555 | 2181 | def __radd__(self, other): |
paul@555 | 2182 | if isinstance(other, AtLeast): |
paul@555 | 2183 | return AtLeast(self.count + other.count) |
paul@555 | 2184 | else: |
paul@555 | 2185 | return AtLeast(self.count + other) |
paul@555 | 2186 | |
paul@555 | 2187 | def __repr__(self): |
paul@555 | 2188 | return "AtLeast(%r)" % self.count |
paul@555 | 2189 | |
paul@83 | 2190 | # vim: tabstop=4 expandtab shiftwidth=4 |