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