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