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