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