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@774 | 10 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Paul Boddie <paul@boddie.org.uk> |
paul@83 | 11 | |
paul@83 | 12 | This program is free software; you can redistribute it and/or modify it under |
paul@83 | 13 | the terms of the GNU General Public License as published by the Free Software |
paul@83 | 14 | Foundation; either version 3 of the License, or (at your option) any later |
paul@83 | 15 | version. |
paul@83 | 16 | |
paul@83 | 17 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@83 | 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@83 | 19 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@83 | 20 | details. |
paul@83 | 21 | |
paul@83 | 22 | You should have received a copy of the GNU General Public License along with |
paul@83 | 23 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@83 | 24 | |
paul@83 | 25 | -------- |
paul@83 | 26 | |
paul@305 | 27 | The principal value structure class in this module is the Attr class which |
paul@305 | 28 | represents attributes of objects and retains the context of each reference to an |
paul@305 | 29 | attribute. This class models program behaviour at run-time. |
paul@305 | 30 | |
paul@190 | 31 | The central data structure classes in this module are the following: |
paul@83 | 32 | |
paul@83 | 33 | * Class |
paul@83 | 34 | * Function |
paul@83 | 35 | * Module |
paul@83 | 36 | |
paul@83 | 37 | All of the above support the Naming interface either explicitly or through |
paul@83 | 38 | general conformance, meaning that all can be asked to provide their 'full_name' |
paul@83 | 39 | using the method of that name. |
paul@83 | 40 | |
paul@83 | 41 | Additionally, all of the above also support a dictionary interface in order to |
paul@83 | 42 | access names within their defined scopes. Specific methods also exist in order |
paul@83 | 43 | to distinguish between certain kinds of attributes: |
paul@83 | 44 | |
paul@190 | 45 | * Class: class_attributes, all_class_attributes, instance_attributes, all_attributes |
paul@83 | 46 | * Function: parameters, locals, all_locals |
paul@83 | 47 | * Module: module_attributes |
paul@83 | 48 | |
paul@83 | 49 | These specific methods are useful in certain situations. |
paul@83 | 50 | |
paul@92 | 51 | The above classes also provide an 'astnode' attribute, indicating the AST node |
paul@92 | 52 | where each such object is defined. |
paul@83 | 53 | """ |
paul@83 | 54 | |
paul@428 | 55 | from micropython.program import ReplaceableContext, PlaceholderContext |
paul@554 | 56 | from micropython.basicdata import * |
paul@622 | 57 | from micropython.branch import BranchTracking |
paul@507 | 58 | import sys |
paul@83 | 59 | |
paul@555 | 60 | try: |
paul@555 | 61 | set |
paul@555 | 62 | except NameError: |
paul@555 | 63 | from sets import Set as set |
paul@555 | 64 | |
paul@717 | 65 | # Context and value manipulation. |
paul@717 | 66 | |
paul@705 | 67 | def get_context_and_value(value): |
paul@705 | 68 | |
paul@705 | 69 | "Return a context, value tuple for the given 'value'." |
paul@705 | 70 | |
paul@705 | 71 | # Functions have a replaceable context. |
paul@705 | 72 | |
paul@705 | 73 | if isinstance(value, Function): |
paul@705 | 74 | return (ReplaceableContext, value) |
paul@705 | 75 | |
paul@705 | 76 | # Classes use placeholder contexts which cannot be replaced but which |
paul@705 | 77 | # do not communicate useful contextual information. |
paul@705 | 78 | |
paul@705 | 79 | elif isinstance(value, Class): |
paul@705 | 80 | return (PlaceholderContext, value) |
paul@705 | 81 | |
paul@705 | 82 | # Other values employ themselves as the context. |
paul@705 | 83 | |
paul@705 | 84 | else: |
paul@705 | 85 | return (value, value) |
paul@705 | 86 | |
paul@717 | 87 | # Namespace classes. |
paul@717 | 88 | |
paul@622 | 89 | class NamespaceDict(Namespace, BranchTracking): |
paul@83 | 90 | |
paul@83 | 91 | "A mix-in providing dictionary methods." |
paul@83 | 92 | |
paul@180 | 93 | def __init__(self, module=None): |
paul@622 | 94 | BranchTracking.__init__(self) |
paul@622 | 95 | |
paul@343 | 96 | self.module = module |
paul@83 | 97 | self.namespace = {} |
paul@774 | 98 | self.parent_namespaces = [] |
paul@83 | 99 | self.globals = set() |
paul@351 | 100 | self.lambdas = {} # only really useful for functions |
paul@591 | 101 | self.finalised = False |
paul@83 | 102 | |
paul@774 | 103 | def set_parent_namespaces(self, namespaces): |
paul@774 | 104 | self.parent_namespaces = namespaces |
paul@774 | 105 | |
paul@774 | 106 | def get_parent_namespaces(self): |
paul@774 | 107 | return self.parent_namespaces |
paul@774 | 108 | |
paul@272 | 109 | # Attribute/name definition and access. |
paul@272 | 110 | |
paul@167 | 111 | def __delitem__(self, name): |
paul@167 | 112 | del self.namespace[name] |
paul@167 | 113 | |
paul@167 | 114 | def has_key(self, name): |
paul@167 | 115 | return self.namespace.has_key(name) |
paul@167 | 116 | |
paul@167 | 117 | def keys(self): |
paul@167 | 118 | return self.namespace.keys() |
paul@167 | 119 | |
paul@167 | 120 | def values(self): |
paul@167 | 121 | return self.namespace.values() |
paul@167 | 122 | |
paul@167 | 123 | def items(self): |
paul@167 | 124 | return self.namespace.items() |
paul@167 | 125 | |
paul@83 | 126 | def __getitem__(self, name): |
paul@83 | 127 | return self.namespace[name] |
paul@83 | 128 | |
paul@348 | 129 | def get(self, name, default=None): |
paul@348 | 130 | return self.namespace.get(name, default) |
paul@348 | 131 | |
paul@622 | 132 | # Introspection methods. |
paul@622 | 133 | |
paul@622 | 134 | def is_method(self): |
paul@622 | 135 | |
paul@622 | 136 | """ |
paul@622 | 137 | Return whether this function is a method explicitly defined in a class. |
paul@622 | 138 | """ |
paul@622 | 139 | |
paul@622 | 140 | return False |
paul@622 | 141 | |
paul@351 | 142 | # Administrative methods. |
paul@351 | 143 | |
paul@437 | 144 | def finalise(self, objtable): |
paul@437 | 145 | self.finalise_attributes() |
paul@495 | 146 | self.finalise_users(objtable) |
paul@437 | 147 | |
paul@351 | 148 | def items_for_vacuum(self): |
paul@351 | 149 | return self.items() + self.lambdas.items() |
paul@351 | 150 | |
paul@351 | 151 | def vacuum_item(self, name): |
paul@363 | 152 | if self.has_key(name): |
paul@363 | 153 | del self[name] |
paul@591 | 154 | return True |
paul@394 | 155 | else: |
paul@591 | 156 | return False |
paul@351 | 157 | |
paul@351 | 158 | def add_lambda(self, obj): |
paul@351 | 159 | attr = Attr(None, self, obj.name) |
paul@705 | 160 | attr.update([get_context_and_value(obj)], single_assignment=1) |
paul@351 | 161 | self.lambdas[obj.name] = attr |
paul@351 | 162 | |
paul@348 | 163 | # Specialised access methods. |
paul@348 | 164 | |
paul@343 | 165 | def get_using_node(self, name, node): |
paul@343 | 166 | |
paul@343 | 167 | """ |
paul@343 | 168 | Access the given 'name' through this namespace, making use of the module |
paul@343 | 169 | and builtins namespaces if necessary, annotating the given 'node' with |
paul@343 | 170 | the scope involved. |
paul@343 | 171 | """ |
paul@343 | 172 | |
paul@343 | 173 | attr, scope, full_name = self._get_with_scope(name) |
paul@343 | 174 | |
paul@343 | 175 | if scope is not None: |
paul@343 | 176 | node._scope = scope |
paul@343 | 177 | self.note_scope(name, scope) |
paul@343 | 178 | |
paul@551 | 179 | if full_name is not None and (scope != "local" or isinstance(self, (Class, Module))): |
paul@343 | 180 | self.use_specific_attribute(full_name, name) |
paul@343 | 181 | |
paul@343 | 182 | return attr |
paul@343 | 183 | |
paul@727 | 184 | def get_for_local(self, name): |
paul@727 | 185 | |
paul@727 | 186 | """ |
paul@727 | 187 | Get an attribute for a local name annotation, maintaining specific |
paul@727 | 188 | assignment information. |
paul@727 | 189 | """ |
paul@727 | 190 | |
paul@727 | 191 | attr, scope, full_name = self._get_with_scope(name) |
paul@727 | 192 | |
paul@727 | 193 | if scope != "local": |
paul@727 | 194 | return None |
paul@727 | 195 | |
paul@727 | 196 | users = self.attribute_users[-1] |
paul@727 | 197 | if users.has_key(name): |
paul@731 | 198 | return LocalAttr(self, name, nodes=users[name], attr=attr) |
paul@727 | 199 | else: |
paul@727 | 200 | return attr |
paul@727 | 201 | |
paul@343 | 202 | def _get_with_scope(self, name, external=0): |
paul@343 | 203 | |
paul@348 | 204 | """ |
paul@348 | 205 | Find the source of the given 'name', returning the attribute object, |
paul@348 | 206 | scope (constant, local, global or builtins), and the full name of the |
paul@348 | 207 | source namespace (or None for constants). |
paul@348 | 208 | |
paul@348 | 209 | If the optional 'external' flag is set to a true value, only external |
paul@348 | 210 | (non-local) namespaces will be involved in the search. |
paul@348 | 211 | """ |
paul@348 | 212 | |
paul@343 | 213 | module = self.module |
paul@343 | 214 | builtins = module and module.builtins or None |
paul@343 | 215 | importer = module and module.importer or None |
paul@343 | 216 | |
paul@343 | 217 | # Constants. |
paul@343 | 218 | |
paul@716 | 219 | if importer and importer.predefined_constants.has_key(name): |
paul@343 | 220 | return importer.get_predefined_constant(name), "constant", None |
paul@343 | 221 | |
paul@343 | 222 | # Locals. |
paul@343 | 223 | |
paul@778 | 224 | if not external and self.defined_as_local(name) and self.has_key(name): |
paul@343 | 225 | return self[name], "local", self.full_name() |
paul@343 | 226 | |
paul@774 | 227 | # Outer scopes. |
paul@774 | 228 | |
paul@774 | 229 | for outer in self.parent_namespaces[::-1]: |
paul@791 | 230 | if isinstance(outer, Class): |
paul@791 | 231 | continue |
paul@774 | 232 | if outer.has_key(name): |
paul@774 | 233 | print >>sys.stderr, "Warning: name %r in namespace %r is provided by an outer scope: %r" % (name, self.full_name(), outer.full_name()) |
paul@774 | 234 | return outer[name], "outer", outer.full_name() |
paul@774 | 235 | |
paul@343 | 236 | # Globals. |
paul@343 | 237 | |
paul@774 | 238 | if module and module is not self and module.has_key(name): |
paul@343 | 239 | return module[name], "global", module.full_name() |
paul@343 | 240 | |
paul@343 | 241 | # Builtins. |
paul@343 | 242 | |
paul@774 | 243 | if builtins and builtins.has_key(name): |
paul@343 | 244 | return builtins[name], "builtins", builtins.full_name() |
paul@343 | 245 | |
paul@343 | 246 | # Unknown. |
paul@343 | 247 | |
paul@774 | 248 | return None, None, None |
paul@343 | 249 | |
paul@348 | 250 | # Attribute definition methods. |
paul@83 | 251 | |
paul@83 | 252 | def __setitem__(self, name, value): |
paul@83 | 253 | self.set(name, value) |
paul@83 | 254 | |
paul@83 | 255 | def set(self, name, value, single_assignment=1): |
paul@83 | 256 | |
paul@83 | 257 | """ |
paul@83 | 258 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@83 | 259 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@83 | 260 | this operation (or whether the operation covers potentially many |
paul@83 | 261 | assignments in the lifetime of a program). |
paul@83 | 262 | """ |
paul@83 | 263 | |
paul@442 | 264 | if value is None: |
paul@507 | 265 | print >>sys.stderr, "Warning: name %r in namespace %r has an unknown value (evaluated to None)." % (name, self.full_name()) |
paul@448 | 266 | value = make_instance() |
paul@442 | 267 | |
paul@83 | 268 | if name in self.globals: |
paul@180 | 269 | self.module.set(name, value, 0) |
paul@83 | 270 | else: |
paul@192 | 271 | self._set(name, value, single_assignment) |
paul@362 | 272 | self.define_scope(name, "local") |
paul@83 | 273 | |
paul@83 | 274 | def set_module(self, name, value): |
paul@83 | 275 | |
paul@83 | 276 | """ |
paul@83 | 277 | A specialised set operation, making 'name' refer to 'value' in the |
paul@83 | 278 | context of making a module reference available in association with |
paul@83 | 279 | 'name' as part of the import of that module or a submodule of that |
paul@83 | 280 | module. |
paul@83 | 281 | """ |
paul@83 | 282 | |
paul@192 | 283 | self._set(name, value, 1) |
paul@83 | 284 | |
paul@192 | 285 | def _set(self, name, attr_or_value, single_assignment=1): |
paul@83 | 286 | |
paul@187 | 287 | """ |
paul@192 | 288 | The underlying set operation associating 'name' with the given |
paul@192 | 289 | 'attr_or_value'. |
paul@187 | 290 | See: docs/assignment.txt |
paul@187 | 291 | """ |
paul@83 | 292 | |
paul@717 | 293 | # For locals, direct assignments to individual name users. |
paul@717 | 294 | |
paul@717 | 295 | users = self.attribute_users[-1] |
paul@717 | 296 | |
paul@717 | 297 | if users.has_key(name): |
paul@717 | 298 | |
paul@717 | 299 | # Note that upon assignment there will probably be only one user. |
paul@717 | 300 | |
paul@717 | 301 | for user in users[name]: |
paul@717 | 302 | user._values = user._values or {} |
paul@728 | 303 | if isinstance(attr_or_value, BaseAttr): |
paul@728 | 304 | attr = attr_or_value |
paul@728 | 305 | else: |
paul@728 | 306 | attr = Attr(None, self, name) |
paul@728 | 307 | self._set_using_attr(attr, attr_or_value) # enforce single assignment |
paul@728 | 308 | user._values[name] = attr |
paul@717 | 309 | |
paul@192 | 310 | # Add and/or obtain the namespace entry. |
paul@87 | 311 | |
paul@192 | 312 | if not self.namespace.has_key(name): |
paul@192 | 313 | self.namespace[name] = Attr(None, self, name) |
paul@712 | 314 | attr = self.namespace[name] |
paul@120 | 315 | |
paul@712 | 316 | # Update the attribute records. |
paul@712 | 317 | |
paul@329 | 318 | self._set_using_attr(attr, attr_or_value, single_assignment) |
paul@329 | 319 | |
paul@329 | 320 | def _set_using_attr(self, attr, attr_or_value, single_assignment=1): |
paul@187 | 321 | |
paul@192 | 322 | # Handle attribute assignment as well as assignment of basic objects. |
paul@717 | 323 | |
paul@717 | 324 | context_values = self.get_contexts_and_values(attr_or_value) |
paul@717 | 325 | attr.update(context_values, single_assignment) |
paul@717 | 326 | |
paul@717 | 327 | def get_contexts_and_values(self, attr_or_value): |
paul@717 | 328 | |
paul@717 | 329 | "Return adapted contexts and values for the given 'attr_or_value'." |
paul@717 | 330 | |
paul@237 | 331 | # Attempt to fix the context if not explicitly defined. |
paul@192 | 332 | |
paul@717 | 333 | if isinstance(attr_or_value, BaseAttr): |
paul@728 | 334 | if isinstance(attr_or_value, LocalAttr): |
paul@728 | 335 | attr_or_value = attr_or_value.attr |
paul@717 | 336 | return self.get_updated_context_values(attr_or_value.get_context_values()) |
paul@237 | 337 | else: |
paul@717 | 338 | return self.get_updated_context_values([get_context_and_value(attr_or_value)]) |
paul@237 | 339 | |
paul@192 | 340 | def get_updated_context_values(self, context_values): |
paul@87 | 341 | |
paul@119 | 342 | """ |
paul@192 | 343 | Adapt the contexts found in the given 'context_values', returning a new |
paul@192 | 344 | set. |
paul@187 | 345 | See: docs/assignment.txt |
paul@119 | 346 | """ |
paul@87 | 347 | |
paul@192 | 348 | results = set() |
paul@192 | 349 | |
paul@192 | 350 | for context, value in context_values: |
paul@192 | 351 | |
paul@192 | 352 | # Set the context of instances to themselves. |
paul@187 | 353 | |
paul@192 | 354 | if isinstance(value, Instance): |
paul@192 | 355 | results.add((value, value)) |
paul@192 | 356 | else: |
paul@192 | 357 | results.add((context, value)) |
paul@192 | 358 | |
paul@192 | 359 | return results |
paul@87 | 360 | |
paul@83 | 361 | def make_global(self, name): |
paul@306 | 362 | |
paul@306 | 363 | "Declare 'name' as a global in the current namespace." |
paul@306 | 364 | |
paul@83 | 365 | if not self.namespace.has_key(name): |
paul@83 | 366 | self.globals.add(name) |
paul@362 | 367 | self.define_scope(name, "global") |
paul@591 | 368 | return True |
paul@83 | 369 | else: |
paul@591 | 370 | return False |
paul@83 | 371 | |
paul@272 | 372 | # Attribute positioning. |
paul@272 | 373 | |
paul@83 | 374 | def attributes_as_list(self): |
paul@92 | 375 | |
paul@92 | 376 | "Return the attributes in a list." |
paul@92 | 377 | |
paul@83 | 378 | self.finalise_attributes() |
paul@83 | 379 | l = [None] * len(self.keys()) |
paul@83 | 380 | for attr in self.values(): |
paul@83 | 381 | l[attr.position] = attr |
paul@83 | 382 | return l |
paul@83 | 383 | |
paul@287 | 384 | def finalise_attributes(self): |
paul@83 | 385 | |
paul@83 | 386 | "Make sure all attributes are fully defined." |
paul@83 | 387 | |
paul@92 | 388 | if self.finalised: |
paul@92 | 389 | return |
paul@92 | 390 | |
paul@83 | 391 | # The default action is to assign attribute positions sequentially. |
paul@83 | 392 | |
paul@83 | 393 | for i, attr in enumerate(self.values()): |
paul@83 | 394 | attr.position = i |
paul@83 | 395 | |
paul@591 | 396 | self.finalised = True |
paul@92 | 397 | |
paul@282 | 398 | def unfinalise_attributes(self): |
paul@282 | 399 | |
paul@282 | 400 | "Open attribute definitions to editing and subsequent finalisation." |
paul@282 | 401 | |
paul@591 | 402 | self.finalised = False |
paul@282 | 403 | |
paul@305 | 404 | # Program data structures. |
paul@83 | 405 | |
paul@743 | 406 | def only(s): |
paul@743 | 407 | if len(s) == 1: |
paul@743 | 408 | for i in s: |
paul@743 | 409 | return i |
paul@743 | 410 | return None |
paul@743 | 411 | |
paul@717 | 412 | class BaseAttr: |
paul@83 | 413 | |
paul@717 | 414 | "A basic attribute entry." |
paul@83 | 415 | |
paul@730 | 416 | def __init__(self, parent, name, parent_type=None): |
paul@119 | 417 | |
paul@119 | 418 | """ |
paul@730 | 419 | Initialise the attribute within the collection of attributes of its |
paul@730 | 420 | 'parent', indicating its 'name'. If the 'parent_type' is specified, it |
paul@730 | 421 | will contain the type of any instance parent. |
paul@119 | 422 | """ |
paul@119 | 423 | |
paul@83 | 424 | self.parent = parent |
paul@83 | 425 | self.name = name |
paul@647 | 426 | self.parent_type = parent_type |
paul@192 | 427 | |
paul@694 | 428 | # Number of static "class" or "def" assignments per name. |
paul@694 | 429 | |
paul@694 | 430 | self.static_assignments = 0 |
paul@694 | 431 | |
paul@787 | 432 | def get_type(self): |
paul@787 | 433 | |
paul@787 | 434 | "Return the type of the referenced object or None if not known." |
paul@787 | 435 | |
paul@787 | 436 | if isinstance(self.parent, Instance): |
paul@787 | 437 | return self.parent_type |
paul@787 | 438 | else: |
paul@787 | 439 | return self.parent |
paul@787 | 440 | |
paul@192 | 441 | def get_contexts(self): |
paul@717 | 442 | return [c for (c, v) in self.get_context_values()] |
paul@167 | 443 | |
paul@192 | 444 | def get_values(self): |
paul@717 | 445 | return [v for (c, v) in self.get_context_values()] |
paul@192 | 446 | |
paul@192 | 447 | def get_context(self): |
paul@578 | 448 | |
paul@578 | 449 | "Get the context referenced by the attribute." |
paul@578 | 450 | |
paul@743 | 451 | if self.get_assignments() == 1: |
paul@743 | 452 | context_value = only(self.get_context_values()) |
paul@743 | 453 | return context_value and context_value[0] |
paul@237 | 454 | else: |
paul@237 | 455 | return None |
paul@192 | 456 | |
paul@192 | 457 | def get_value(self): |
paul@578 | 458 | |
paul@578 | 459 | "Get the value referenced by the attribute." |
paul@578 | 460 | |
paul@743 | 461 | if self.get_assignments() == 1: |
paul@743 | 462 | context_value = only(self.get_context_values()) |
paul@743 | 463 | return context_value and context_value[1] |
paul@237 | 464 | else: |
paul@237 | 465 | return None |
paul@192 | 466 | |
paul@508 | 467 | __call__ = get_value # convenient access to any single value |
paul@508 | 468 | |
paul@331 | 469 | def is_constant(self): |
paul@331 | 470 | |
paul@331 | 471 | """ |
paul@331 | 472 | Return whether this attribute references something that can be regarded |
paul@331 | 473 | as being constant within a particular scope. |
paul@331 | 474 | """ |
paul@331 | 475 | |
paul@717 | 476 | return self.get_assignments() == 1 |
paul@331 | 477 | |
paul@330 | 478 | def is_strict_constant(self): |
paul@330 | 479 | |
paul@330 | 480 | """ |
paul@330 | 481 | Return whether this attribute references something that can be regarded |
paul@330 | 482 | as being constant. |
paul@330 | 483 | """ |
paul@330 | 484 | |
paul@330 | 485 | value = self.get_value() |
paul@468 | 486 | return not (value is None or (isinstance(value, Instance) and not isinstance(value, Constant))) |
paul@330 | 487 | |
paul@242 | 488 | def is_static_attribute(self): |
paul@242 | 489 | |
paul@242 | 490 | """ |
paul@242 | 491 | Return whether this attribute is defined on a fixed/static object such |
paul@242 | 492 | as a class or a module. |
paul@242 | 493 | """ |
paul@242 | 494 | |
paul@401 | 495 | return isinstance(self.parent, (Class, Module)) |
paul@127 | 496 | |
paul@243 | 497 | def defines_ambiguous_class(self): |
paul@243 | 498 | |
paul@243 | 499 | "Return whether this attribute defines more than one class." |
paul@243 | 500 | |
paul@717 | 501 | if self.get_assignments() > 1: |
paul@591 | 502 | have_class = False |
paul@243 | 503 | for obj in self.get_values(): |
paul@243 | 504 | if isinstance(obj, Class): |
paul@243 | 505 | if have_class: |
paul@591 | 506 | return True |
paul@591 | 507 | have_class = True |
paul@591 | 508 | |
paul@591 | 509 | return False |
paul@243 | 510 | |
paul@119 | 511 | def defined_within_hierarchy(self): |
paul@119 | 512 | |
paul@119 | 513 | """ |
paul@119 | 514 | Return whether the parent and context of the attribute belong to the |
paul@119 | 515 | same class hierarchy. |
paul@119 | 516 | """ |
paul@119 | 517 | |
paul@192 | 518 | # Must be defined within a class. |
paul@192 | 519 | |
paul@192 | 520 | if isinstance(self.parent, Class): |
paul@192 | 521 | |
paul@192 | 522 | # To be sure, all contexts must be classes and be the same as the |
paul@192 | 523 | # parent, or be a superclass of the parent, or be a subclass of the |
paul@192 | 524 | # parent. |
paul@192 | 525 | |
paul@192 | 526 | for context in self.get_contexts(): |
paul@192 | 527 | if not ( |
paul@192 | 528 | isinstance(context, Class) and ( |
paul@192 | 529 | context is self.parent or |
paul@192 | 530 | context.has_subclass(self.parent) or |
paul@192 | 531 | self.parent.has_subclass(context)) |
paul@192 | 532 | ): |
paul@591 | 533 | return False |
paul@591 | 534 | |
paul@591 | 535 | return True |
paul@192 | 536 | |
paul@192 | 537 | # Instance attributes are not defined within a hierarchy. |
paul@192 | 538 | |
paul@192 | 539 | else: |
paul@591 | 540 | return False |
paul@119 | 541 | |
paul@194 | 542 | def defined_outside_hierarchy(self): |
paul@194 | 543 | |
paul@194 | 544 | """ |
paul@194 | 545 | Return whether the parent and context of the attribute never belong to |
paul@194 | 546 | the same class hierarchy. |
paul@194 | 547 | """ |
paul@194 | 548 | |
paul@194 | 549 | # Must be defined within a class. |
paul@194 | 550 | |
paul@194 | 551 | if isinstance(self.parent, Class): |
paul@194 | 552 | |
paul@194 | 553 | # To be sure, all contexts must be classes and be the same as the |
paul@194 | 554 | # parent, or be a superclass of the parent, or be a subclass of the |
paul@194 | 555 | # parent. |
paul@194 | 556 | |
paul@194 | 557 | for context in self.get_contexts(): |
paul@640 | 558 | if ( |
paul@194 | 559 | isinstance(context, Class) and not ( |
paul@194 | 560 | context is self.parent or |
paul@194 | 561 | context.has_subclass(self.parent) or |
paul@194 | 562 | self.parent.has_subclass(context)) |
paul@194 | 563 | ): |
paul@591 | 564 | return False |
paul@591 | 565 | |
paul@591 | 566 | return True |
paul@194 | 567 | |
paul@194 | 568 | # Instance attributes are not defined within a hierarchy. |
paul@194 | 569 | |
paul@194 | 570 | else: |
paul@640 | 571 | return True |
paul@194 | 572 | |
paul@83 | 573 | def __repr__(self): |
paul@730 | 574 | return "<attribute %s.%s (%sassigned %r)>" % ( |
paul@494 | 575 | shortrepr(self.parent), self.name, |
paul@730 | 576 | self._repr_parent_type(), self.get_assignments() |
paul@87 | 577 | ) |
paul@83 | 578 | |
paul@730 | 579 | def _repr_parent_type(self): |
paul@730 | 580 | if self.parent_type is not None: |
paul@730 | 581 | return "parent %s, " % shortrepr(self.parent_type) |
paul@730 | 582 | else: |
paul@730 | 583 | return "" |
paul@730 | 584 | |
paul@329 | 585 | def __shortrepr__(self): |
paul@730 | 586 | return "%s.%s" % (shortrepr(self.parent), self.name) |
paul@329 | 587 | |
paul@717 | 588 | class LocalAttr(BaseAttr): |
paul@717 | 589 | |
paul@717 | 590 | """ |
paul@717 | 591 | An attribute of a local namespace - a local name - derived from potentially |
paul@717 | 592 | many assignments. This attribute dynamically generates value information |
paul@717 | 593 | from that stored on assignment nodes so that it may be possible to update |
paul@717 | 594 | such nodes and thus the knowledge about the nature of the attribute at a |
paul@717 | 595 | later point. |
paul@717 | 596 | """ |
paul@717 | 597 | |
paul@731 | 598 | def __init__(self, parent, name, nodes, attr): |
paul@731 | 599 | BaseAttr.__init__(self, parent, name) |
paul@717 | 600 | self.nodes = nodes or set() |
paul@728 | 601 | self.attr = attr |
paul@723 | 602 | self.users = None |
paul@723 | 603 | |
paul@723 | 604 | def _get_defining_users(self): |
paul@723 | 605 | if self.users is None: |
paul@723 | 606 | users = set() |
paul@723 | 607 | for node in self.nodes: |
paul@729 | 608 | for user in node._attrdefs or [node]: |
paul@729 | 609 | if user._values and user._values.has_key(self.name): |
paul@729 | 610 | users.add(user) |
paul@723 | 611 | self.users = users |
paul@723 | 612 | return self.users |
paul@723 | 613 | |
paul@717 | 614 | def get_assignments(self): |
paul@723 | 615 | return len(self._get_defining_users()) |
paul@717 | 616 | |
paul@717 | 617 | def get_context_values(self): |
paul@742 | 618 | |
paul@742 | 619 | """ |
paul@742 | 620 | Return the context, value pairs for the attribute. Since these may be |
paul@742 | 621 | derived from value information obtained from the defining users, and |
paul@742 | 622 | since such information may be changed during the deduction process, the |
paul@742 | 623 | result of this method may change after initial inspection. |
paul@742 | 624 | """ |
paul@742 | 625 | |
paul@717 | 626 | context_values = set() |
paul@793 | 627 | |
paul@723 | 628 | for def_user in self._get_defining_users(): |
paul@723 | 629 | attr = def_user._values[self.name] |
paul@793 | 630 | |
paul@793 | 631 | # Attributes provide their values via this local attribute. |
paul@793 | 632 | |
paul@723 | 633 | if isinstance(attr, BaseAttr): |
paul@723 | 634 | context_values.update(attr.get_context_values()) |
paul@793 | 635 | |
paul@793 | 636 | # Non-attributes are propagated using the conversion rule. |
paul@793 | 637 | |
paul@723 | 638 | else: |
paul@723 | 639 | context_values.add(get_context_and_value(attr)) |
paul@793 | 640 | |
paul@717 | 641 | return context_values |
paul@717 | 642 | |
paul@793 | 643 | def __repr__(self): |
paul@793 | 644 | return "<local attribute %s.%s (%sassigned %r)>" % ( |
paul@793 | 645 | shortrepr(self.parent), self.name, |
paul@793 | 646 | self._repr_parent_type(), self.get_assignments() |
paul@793 | 647 | ) |
paul@793 | 648 | |
paul@717 | 649 | class Attr(BaseAttr): |
paul@717 | 650 | |
paul@717 | 651 | "An attribute entry having context and value information." |
paul@717 | 652 | |
paul@717 | 653 | def __init__(self, position, parent, name, parent_type=None): |
paul@730 | 654 | |
paul@730 | 655 | """ |
paul@730 | 656 | Initialise the attribute with the given 'position' within the collection |
paul@730 | 657 | of attributes of its 'parent', indicating its 'name'. If the |
paul@730 | 658 | 'parent_type' is specified, it will contain the type of any instance |
paul@730 | 659 | parent. |
paul@730 | 660 | """ |
paul@730 | 661 | |
paul@730 | 662 | BaseAttr.__init__(self, parent, name, parent_type) |
paul@730 | 663 | self.position = position |
paul@717 | 664 | |
paul@717 | 665 | # Possible values. |
paul@717 | 666 | |
paul@717 | 667 | self.context_values = set() |
paul@717 | 668 | |
paul@717 | 669 | # Number of assignments per name. |
paul@717 | 670 | |
paul@717 | 671 | self.assignments = None |
paul@717 | 672 | |
paul@717 | 673 | def get_assignments(self): |
paul@717 | 674 | return self.assignments |
paul@717 | 675 | |
paul@717 | 676 | def get_context_values(self): |
paul@717 | 677 | return self.context_values |
paul@717 | 678 | |
paul@717 | 679 | # Generic update operations on attributes. |
paul@717 | 680 | |
paul@717 | 681 | def update(self, context_values, single_assignment): |
paul@717 | 682 | |
paul@717 | 683 | """ |
paul@717 | 684 | Update the attribute, adding the 'context_values' provided to the |
paul@717 | 685 | known details associated with the attribute, changing the number of |
paul@717 | 686 | assignments according to the 'single_assignment' status of the |
paul@717 | 687 | operation, where a true value indicates that only one assignment is |
paul@717 | 688 | associated with the update, and a false value indicates that potentially |
paul@717 | 689 | many assignments may be involved. |
paul@717 | 690 | """ |
paul@717 | 691 | |
paul@717 | 692 | if self.context_values.issuperset(context_values) and \ |
paul@717 | 693 | not (make_instance(), make_instance()) in context_values: |
paul@717 | 694 | return |
paul@717 | 695 | |
paul@717 | 696 | self.update_assignments(len(set(context_values)), single_assignment) |
paul@717 | 697 | self.context_values.update(context_values) |
paul@717 | 698 | |
paul@717 | 699 | def update_assignments(self, n, single_assignment): |
paul@717 | 700 | |
paul@717 | 701 | """ |
paul@717 | 702 | Update the assignment count by 'n' or "at least" 'n' if |
paul@717 | 703 | 'single_assignment' is given as a false value. |
paul@717 | 704 | """ |
paul@717 | 705 | |
paul@717 | 706 | if self.assignments is None: |
paul@717 | 707 | if single_assignment: |
paul@717 | 708 | self.assignments = n |
paul@717 | 709 | else: |
paul@717 | 710 | self.assignments = AtLeast(n) |
paul@717 | 711 | else: |
paul@717 | 712 | if single_assignment: |
paul@717 | 713 | self.assignments += n |
paul@717 | 714 | else: |
paul@717 | 715 | self.assignments += AtLeast(n) |
paul@192 | 716 | |
paul@730 | 717 | def __repr__(self): |
paul@730 | 718 | return "<attribute %s.%s (%s%sassigned %r)>" % ( |
paul@730 | 719 | shortrepr(self.parent), self.name, |
paul@730 | 720 | self._repr_parent_type(), self._repr_position(), |
paul@730 | 721 | self.get_assignments() |
paul@730 | 722 | ) |
paul@730 | 723 | |
paul@730 | 724 | def _repr_position(self): |
paul@730 | 725 | if self.position is not None: |
paul@730 | 726 | return "at %r, " % self.position |
paul@730 | 727 | else: |
paul@730 | 728 | return "" |
paul@730 | 729 | |
paul@730 | 730 | def __shortrepr__(self): |
paul@730 | 731 | return "%s.%s (at %r)" % (shortrepr(self.parent), self.name, |
paul@730 | 732 | self._repr_position()) |
paul@730 | 733 | |
paul@85 | 734 | class Class(NamespaceDict, Naming, Constant): |
paul@83 | 735 | |
paul@394 | 736 | "A base class for common/normal classes and the type class." |
paul@394 | 737 | |
paul@665 | 738 | def __init__(self, name, parent=None, module=None, node=None, original_name=None): |
paul@83 | 739 | |
paul@83 | 740 | """ |
paul@394 | 741 | Initialise the class with the given 'name', optional 'parent' object, |
paul@394 | 742 | 'module' and AST 'node'. The optional information must be set at a later |
paul@394 | 743 | point using the 'set_context' method if omitted. |
paul@83 | 744 | """ |
paul@83 | 745 | |
paul@180 | 746 | NamespaceDict.__init__(self, module) |
paul@83 | 747 | self.name = name |
paul@83 | 748 | self.parent = parent |
paul@91 | 749 | self.astnode = node |
paul@665 | 750 | self.original_name = original_name or name |
paul@83 | 751 | |
paul@83 | 752 | # Superclasses, descendants and attributes. |
paul@83 | 753 | |
paul@83 | 754 | self.bases = [] |
paul@83 | 755 | self.descendants = set() |
paul@83 | 756 | self.instattr = set() # instance attributes |
paul@527 | 757 | self.instattr_tentative = set() # tentative/suspected instance attributes |
paul@83 | 758 | self.relocated = set() # attributes which do not have the same |
paul@83 | 759 | # position as those of the same name in |
paul@83 | 760 | # some superclasses |
paul@83 | 761 | |
paul@83 | 762 | # Caches. |
paul@83 | 763 | |
paul@282 | 764 | self.reset_caches() |
paul@83 | 765 | |
paul@667 | 766 | # Program-related details. |
paul@240 | 767 | |
paul@83 | 768 | self.instantiator = None |
paul@116 | 769 | self.temp_usage = 0 |
paul@116 | 770 | self.local_usage = 0 |
paul@133 | 771 | self.all_local_usage = 0 |
paul@83 | 772 | |
paul@401 | 773 | # Add an attribute to this class for use by instances. |
paul@401 | 774 | |
paul@401 | 775 | self.set("__class__", self) |
paul@394 | 776 | |
paul@394 | 777 | def set_context(self, parent, module, node): |
paul@401 | 778 | |
paul@401 | 779 | "Set the 'parent', 'module' and 'node' of a class created in advance." |
paul@401 | 780 | |
paul@394 | 781 | self.parent = parent |
paul@394 | 782 | self.module = module |
paul@394 | 783 | self.astnode = node |
paul@394 | 784 | |
paul@282 | 785 | def reset_caches(self): |
paul@282 | 786 | |
paul@282 | 787 | "Reset the caches." |
paul@282 | 788 | |
paul@282 | 789 | self.all_instattr = None # cache for instance_attributes |
paul@282 | 790 | self.all_instattr_names = None # from all_instattr |
paul@282 | 791 | self.all_classattr = None # cache for all_class_attributes |
paul@282 | 792 | self.all_classattr_names = None # from all_classattr |
paul@282 | 793 | self.allattr = None # cache for all_attributes |
paul@282 | 794 | self.allattr_names = None # from allattr |
paul@282 | 795 | |
paul@83 | 796 | def __repr__(self): |
paul@667 | 797 | return "<class %s>" % shortrepr(self) |
paul@87 | 798 | |
paul@87 | 799 | def __shortrepr__(self): |
paul@494 | 800 | return "%s.%s" % (shortrepr(self.parent), self.name) |
paul@87 | 801 | |
paul@189 | 802 | # Namespace-related methods. |
paul@189 | 803 | |
paul@192 | 804 | def get_updated_context_values(self, context_values): |
paul@87 | 805 | |
paul@119 | 806 | """ |
paul@192 | 807 | Adapt the contexts found in the given 'context_values', returning a new |
paul@192 | 808 | set. |
paul@187 | 809 | See: docs/assignment.txt |
paul@119 | 810 | """ |
paul@87 | 811 | |
paul@192 | 812 | results = set() |
paul@192 | 813 | |
paul@192 | 814 | for context, value in context_values: |
paul@187 | 815 | |
paul@187 | 816 | # Change the ownership of functions. |
paul@187 | 817 | |
paul@237 | 818 | if context is ReplaceableContext and value is not None and isinstance(value, Function): |
paul@192 | 819 | results.add((self, value)) |
paul@192 | 820 | else: |
paul@192 | 821 | results.add((context, value)) |
paul@187 | 822 | |
paul@192 | 823 | return NamespaceDict.get_updated_context_values(self, results) |
paul@83 | 824 | |
paul@351 | 825 | # Administrative methods. |
paul@351 | 826 | |
paul@394 | 827 | def items_for_vacuum(self): |
paul@405 | 828 | |
paul@405 | 829 | "Consider both class and instance attributes for vacuuming." |
paul@405 | 830 | |
paul@394 | 831 | items = [] |
paul@394 | 832 | for name in self.instattr: |
paul@394 | 833 | items.append((name, None)) |
paul@394 | 834 | return NamespaceDict.items_for_vacuum(self) + items |
paul@394 | 835 | |
paul@394 | 836 | def vacuum_item(self, name): |
paul@405 | 837 | |
paul@405 | 838 | "Vacuum 'name' from the class or instance attribute collections." |
paul@405 | 839 | |
paul@405 | 840 | # NOTE: Hack to prevent damage to exceptions. |
paul@405 | 841 | |
paul@405 | 842 | if name == "_pc": |
paul@591 | 843 | return False |
paul@405 | 844 | |
paul@394 | 845 | if not NamespaceDict.vacuum_item(self, name): |
paul@394 | 846 | self.instattr.remove(name) |
paul@591 | 847 | return True |
paul@394 | 848 | |
paul@287 | 849 | def finalise_attributes(self): |
paul@83 | 850 | |
paul@83 | 851 | "Make sure that all attributes are fully defined." |
paul@83 | 852 | |
paul@92 | 853 | if self.finalised: |
paul@92 | 854 | return |
paul@92 | 855 | |
paul@83 | 856 | self.finalise_class_attributes() |
paul@83 | 857 | self.finalise_instance_attributes() |
paul@591 | 858 | self.finalised = True |
paul@83 | 859 | |
paul@282 | 860 | def unfinalise_attributes(self): |
paul@282 | 861 | |
paul@282 | 862 | "Open attribute definitions to editing and subsequent finalisation." |
paul@282 | 863 | |
paul@282 | 864 | self.reset_caches() |
paul@591 | 865 | self.finalised = False |
paul@282 | 866 | |
paul@184 | 867 | # Convenience methods for accessing functions and methods. |
paul@184 | 868 | |
paul@83 | 869 | def get_instantiator(self): |
paul@83 | 870 | |
paul@83 | 871 | "Return a function which can be used to instantiate the class." |
paul@83 | 872 | |
paul@83 | 873 | if self.instantiator is None: |
paul@212 | 874 | self.instantiator = self.get_init_method().as_instantiator() |
paul@83 | 875 | return self.instantiator |
paul@83 | 876 | |
paul@137 | 877 | def get_init_method(self): |
paul@192 | 878 | return self.all_class_attributes()["__init__"].get_value() |
paul@137 | 879 | |
paul@83 | 880 | # Class-specific methods. |
paul@83 | 881 | |
paul@83 | 882 | def add_base(self, base): |
paul@83 | 883 | self.bases.append(base) |
paul@83 | 884 | base.add_descendant(self) |
paul@83 | 885 | |
paul@527 | 886 | def add_instance_attribute(self, name, tentative=False): |
paul@527 | 887 | if tentative: |
paul@527 | 888 | self.instattr_tentative.add(name) |
paul@527 | 889 | else: |
paul@527 | 890 | self.instattr.add(name) |
paul@83 | 891 | |
paul@83 | 892 | def add_descendant(self, cls): |
paul@83 | 893 | self.descendants.add(cls) |
paul@83 | 894 | for base in self.bases: |
paul@83 | 895 | base.add_descendant(cls) |
paul@83 | 896 | |
paul@122 | 897 | def has_subclass(self, other): |
paul@122 | 898 | return other in self.descendants |
paul@122 | 899 | |
paul@135 | 900 | def all_descendants(self): |
paul@135 | 901 | d = {} |
paul@135 | 902 | for cls in self.descendants: |
paul@135 | 903 | d[cls.full_name()] = cls |
paul@135 | 904 | return d |
paul@135 | 905 | |
paul@83 | 906 | "Return the attribute names provided by this class only." |
paul@83 | 907 | |
paul@83 | 908 | class_attribute_names = NamespaceDict.keys |
paul@83 | 909 | |
paul@83 | 910 | def class_attributes(self): |
paul@83 | 911 | |
paul@83 | 912 | "Return class attributes provided by this class only." |
paul@83 | 913 | |
paul@83 | 914 | return dict(self) |
paul@83 | 915 | |
paul@83 | 916 | def all_class_attribute_names(self): |
paul@83 | 917 | |
paul@83 | 918 | "Return the attribute names provided by classes in this hierarchy." |
paul@83 | 919 | |
paul@83 | 920 | if self.all_classattr_names is None: |
paul@83 | 921 | self.all_class_attributes() |
paul@282 | 922 | self.all_classattr_names = self.all_classattr.keys() |
paul@83 | 923 | return self.all_classattr_names |
paul@83 | 924 | |
paul@83 | 925 | def all_class_attributes(self): |
paul@83 | 926 | |
paul@83 | 927 | "Return all class attributes, indicating the class which provides them." |
paul@83 | 928 | |
paul@83 | 929 | self.finalise_class_attributes() |
paul@83 | 930 | return self.all_classattr |
paul@83 | 931 | |
paul@83 | 932 | def finalise_class_attributes(self): |
paul@83 | 933 | |
paul@83 | 934 | "Make sure that the class attributes are fully defined." |
paul@83 | 935 | |
paul@83 | 936 | if self.all_classattr is None: |
paul@83 | 937 | self.all_classattr = {} |
paul@83 | 938 | clsattr = {} |
paul@83 | 939 | |
paul@83 | 940 | # Record provisional position information for attributes of this |
paul@83 | 941 | # class. |
paul@83 | 942 | |
paul@83 | 943 | for name in self.class_attributes().keys(): |
paul@396 | 944 | |
paul@396 | 945 | # Special case: __class__ has to be at position 0. |
paul@396 | 946 | |
paul@396 | 947 | if name == "__class__": |
paul@396 | 948 | clsattr[name] = set([0]) |
paul@396 | 949 | else: |
paul@396 | 950 | clsattr[name] = set() # position not yet defined |
paul@83 | 951 | |
paul@83 | 952 | reversed_bases = self.bases[:] |
paul@83 | 953 | reversed_bases.reverse() |
paul@83 | 954 | |
paul@83 | 955 | # For the bases in reverse order, acquire class attribute details. |
paul@83 | 956 | |
paul@83 | 957 | for cls in reversed_bases: |
paul@83 | 958 | for name, attr in cls.all_class_attributes().items(): |
paul@83 | 959 | self.all_classattr[name] = attr |
paul@83 | 960 | |
paul@83 | 961 | # Record previous attribute information. |
paul@83 | 962 | |
paul@83 | 963 | if clsattr.has_key(name): |
paul@83 | 964 | clsattr[name].add(attr.position) |
paul@83 | 965 | |
paul@83 | 966 | # Record class attributes provided by this class and its bases, |
paul@83 | 967 | # along with their positions. |
paul@83 | 968 | |
paul@83 | 969 | self.all_classattr.update(self.class_attributes()) |
paul@83 | 970 | |
paul@83 | 971 | if clsattr: |
paul@83 | 972 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@83 | 973 | self.all_classattr[name].position = i |
paul@83 | 974 | |
paul@83 | 975 | return self.all_classattr |
paul@83 | 976 | |
paul@83 | 977 | def instance_attribute_names(self): |
paul@83 | 978 | |
paul@83 | 979 | "Return the instance attribute names provided by the class." |
paul@83 | 980 | |
paul@83 | 981 | if self.all_instattr_names is None: |
paul@83 | 982 | self.instance_attributes() |
paul@83 | 983 | return self.all_instattr_names |
paul@83 | 984 | |
paul@83 | 985 | def instance_attributes(self): |
paul@83 | 986 | |
paul@83 | 987 | "Return instance-only attributes for instances of this class." |
paul@83 | 988 | |
paul@83 | 989 | self.finalise_instance_attributes() |
paul@83 | 990 | return self.all_instattr |
paul@83 | 991 | |
paul@667 | 992 | def instance_attributes_as_list(self): |
paul@667 | 993 | |
paul@667 | 994 | "Return instance-only attributes in a list ordered by position." |
paul@667 | 995 | |
paul@667 | 996 | attrs = self.instance_attributes().values() |
paul@667 | 997 | attrs.sort(cmp=lambda x, y: cmp(x.position, y.position)) |
paul@667 | 998 | return attrs |
paul@667 | 999 | |
paul@83 | 1000 | def finalise_instance_attributes(self): |
paul@83 | 1001 | |
paul@83 | 1002 | "Make sure that the instance attributes are fully defined." |
paul@83 | 1003 | |
paul@527 | 1004 | # Eliminate tentative instance attributes that are actually class |
paul@527 | 1005 | # attributes. |
paul@527 | 1006 | |
paul@527 | 1007 | for attrname in self.all_class_attributes().keys(): |
paul@527 | 1008 | if attrname in self.instattr_tentative: |
paul@527 | 1009 | self.instattr_tentative.remove(attrname) |
paul@527 | 1010 | |
paul@527 | 1011 | for cls in self.descendants: |
paul@527 | 1012 | for attrname in cls.class_attribute_names(): |
paul@527 | 1013 | if attrname in self.instattr_tentative: |
paul@527 | 1014 | self.instattr_tentative.remove(attrname) |
paul@527 | 1015 | |
paul@527 | 1016 | for attrname in self.instattr_tentative: |
paul@527 | 1017 | self.instattr.add(attrname) |
paul@527 | 1018 | |
paul@210 | 1019 | # Cache the attributes by converting the positioned attributes into a |
paul@210 | 1020 | # dictionary. |
paul@83 | 1021 | |
paul@210 | 1022 | if self.all_instattr is None: |
paul@210 | 1023 | self.all_instattr = self._get_attributes() |
paul@83 | 1024 | self.all_instattr_names = self.all_instattr.keys() |
paul@83 | 1025 | |
paul@83 | 1026 | return self.all_instattr |
paul@83 | 1027 | |
paul@210 | 1028 | def _get_attributes(self): |
paul@210 | 1029 | |
paul@210 | 1030 | """ |
paul@210 | 1031 | Return a dictionary mapping names to Attr instances incorporating |
paul@210 | 1032 | information about their positions in the final instance structure. |
paul@210 | 1033 | """ |
paul@210 | 1034 | |
paul@210 | 1035 | instattr = {} |
paul@210 | 1036 | |
paul@210 | 1037 | # Record provisional position information for attributes of this |
paul@210 | 1038 | # instance. |
paul@210 | 1039 | |
paul@210 | 1040 | for name in self.instattr: |
paul@401 | 1041 | instattr[name] = set() # position not yet defined |
paul@210 | 1042 | |
paul@210 | 1043 | reversed_bases = self.bases[:] |
paul@210 | 1044 | reversed_bases.reverse() |
paul@210 | 1045 | |
paul@210 | 1046 | # For the bases in reverse order, acquire instance attribute |
paul@210 | 1047 | # details. |
paul@210 | 1048 | |
paul@210 | 1049 | for cls in reversed_bases: |
paul@210 | 1050 | for name, attr in cls.instance_attributes().items(): |
paul@210 | 1051 | |
paul@210 | 1052 | # Record previous attribute information. |
paul@210 | 1053 | |
paul@210 | 1054 | if instattr.has_key(name): |
paul@210 | 1055 | instattr[name].add(attr.position) |
paul@235 | 1056 | else: |
paul@235 | 1057 | instattr[name] = set([attr.position]) |
paul@210 | 1058 | |
paul@210 | 1059 | # Build the dictionary of attributes using the existing positions known |
paul@210 | 1060 | # for each name. |
paul@210 | 1061 | |
paul@210 | 1062 | d = {} |
paul@210 | 1063 | for i, name in enumerate(self._get_position_list(instattr)): |
paul@647 | 1064 | d[name] = Attr(i, make_instance(), name, self) |
paul@210 | 1065 | return d |
paul@210 | 1066 | |
paul@83 | 1067 | def _get_position_list(self, positions): |
paul@83 | 1068 | |
paul@83 | 1069 | """ |
paul@83 | 1070 | Return a list of attribute names for the given 'positions' mapping from |
paul@83 | 1071 | names to positions, indicating the positions of the attributes in the |
paul@83 | 1072 | final instance structure. |
paul@83 | 1073 | """ |
paul@83 | 1074 | |
paul@83 | 1075 | position_items = positions.items() |
paul@83 | 1076 | namearray = [None] * len(position_items) |
paul@83 | 1077 | |
paul@83 | 1078 | # Get the positions in ascending order of list size, with lists |
paul@83 | 1079 | # of the same size ordered according to their smallest position |
paul@83 | 1080 | # value. |
paul@83 | 1081 | |
paul@83 | 1082 | position_items.sort(self._cmp_positions) |
paul@83 | 1083 | |
paul@83 | 1084 | # Get the names in position order. |
paul@83 | 1085 | |
paul@83 | 1086 | held = [] |
paul@83 | 1087 | |
paul@83 | 1088 | for name, pos in position_items: |
paul@83 | 1089 | pos = list(pos) |
paul@83 | 1090 | pos.sort() |
paul@83 | 1091 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@83 | 1092 | namearray[pos[0]] = name |
paul@83 | 1093 | else: |
paul@83 | 1094 | if pos: |
paul@83 | 1095 | self.relocated.add(name) |
paul@83 | 1096 | held.append((name, pos)) |
paul@83 | 1097 | |
paul@83 | 1098 | for i, attr in enumerate(namearray): |
paul@83 | 1099 | if attr is None: |
paul@83 | 1100 | name, pos = held.pop() |
paul@83 | 1101 | namearray[i] = name |
paul@83 | 1102 | |
paul@83 | 1103 | return namearray |
paul@83 | 1104 | |
paul@83 | 1105 | def _cmp_positions(self, a, b): |
paul@83 | 1106 | |
paul@83 | 1107 | "Compare name plus position list operands 'a' and 'b'." |
paul@83 | 1108 | |
paul@83 | 1109 | name_a, list_a = a |
paul@83 | 1110 | name_b, list_b = b |
paul@83 | 1111 | if len(list_a) < len(list_b): |
paul@83 | 1112 | return -1 |
paul@83 | 1113 | elif len(list_a) > len(list_b): |
paul@83 | 1114 | return 1 |
paul@83 | 1115 | elif not list_a: |
paul@83 | 1116 | return 0 |
paul@83 | 1117 | else: |
paul@83 | 1118 | return cmp(min(list_a), min(list_b)) |
paul@83 | 1119 | |
paul@83 | 1120 | def all_attribute_names(self): |
paul@83 | 1121 | |
paul@83 | 1122 | """ |
paul@83 | 1123 | Return the names of all attributes provided by instances of this class. |
paul@83 | 1124 | """ |
paul@83 | 1125 | |
paul@83 | 1126 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@83 | 1127 | return self.allattr_names |
paul@83 | 1128 | |
paul@83 | 1129 | def all_attributes(self): |
paul@83 | 1130 | |
paul@83 | 1131 | """ |
paul@83 | 1132 | Return all attributes for an instance, indicating either the class which |
paul@83 | 1133 | provides them or that the instance itself provides them. |
paul@394 | 1134 | |
paul@401 | 1135 | Note that __class__ acts like a class attribute for both instances and |
paul@401 | 1136 | classes, and must be able to convey distinct values. |
paul@83 | 1137 | """ |
paul@83 | 1138 | |
paul@83 | 1139 | if self.allattr is None: |
paul@83 | 1140 | self.allattr = {} |
paul@83 | 1141 | self.allattr.update(self.all_class_attributes()) |
paul@83 | 1142 | for name, attr in self.instance_attributes().items(): |
paul@394 | 1143 | if self.allattr.has_key(name) and name != "__class__": |
paul@507 | 1144 | print >>sys.stderr, "Warning: instance attribute %r in %r overrides class attribute." % (name, self) |
paul@83 | 1145 | self.allattr[name] = attr |
paul@83 | 1146 | return self.allattr |
paul@83 | 1147 | |
paul@85 | 1148 | class Function(NamespaceDict, Naming, Constant): |
paul@83 | 1149 | |
paul@83 | 1150 | "An inspected function." |
paul@83 | 1151 | |
paul@331 | 1152 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, |
paul@665 | 1153 | dynamic_def=0, module=None, node=None, original_name=None): |
paul@83 | 1154 | |
paul@83 | 1155 | """ |
paul@83 | 1156 | Initialise the function with the given 'name', 'parent', list of |
paul@83 | 1157 | 'argnames', list of 'defaults', the 'has_star' flag (indicating the |
paul@83 | 1158 | presence of a * parameter), the 'has_dstar' flag (indicating the |
paul@351 | 1159 | presence of a ** parameter), optional 'dynamic_def' (indicating that the |
paul@351 | 1160 | function must be handled dynamically), optional 'module', and optional |
paul@351 | 1161 | AST 'node'. |
paul@83 | 1162 | """ |
paul@83 | 1163 | |
paul@180 | 1164 | NamespaceDict.__init__(self, module) |
paul@351 | 1165 | |
paul@351 | 1166 | if name is None: |
paul@351 | 1167 | self.name = "lambda#%d" % new_lambda() |
paul@591 | 1168 | self._is_lambda = True |
paul@351 | 1169 | else: |
paul@351 | 1170 | self.name = name |
paul@591 | 1171 | self._is_lambda = False |
paul@351 | 1172 | |
paul@665 | 1173 | self.original_name = original_name or name |
paul@665 | 1174 | |
paul@83 | 1175 | self.parent = parent |
paul@83 | 1176 | self.argnames = argnames |
paul@83 | 1177 | self.defaults = defaults |
paul@83 | 1178 | self.has_star = has_star |
paul@83 | 1179 | self.has_dstar = has_dstar |
paul@331 | 1180 | self.dynamic_def = dynamic_def |
paul@91 | 1181 | self.astnode = node |
paul@83 | 1182 | |
paul@83 | 1183 | # Initialise the positional names. |
paul@83 | 1184 | |
paul@83 | 1185 | self.positional_names = self.argnames[:] |
paul@83 | 1186 | if has_dstar: |
paul@83 | 1187 | self.dstar_name = self.positional_names[-1] |
paul@83 | 1188 | del self.positional_names[-1] |
paul@83 | 1189 | if has_star: |
paul@83 | 1190 | self.star_name = self.positional_names[-1] |
paul@83 | 1191 | del self.positional_names[-1] |
paul@83 | 1192 | |
paul@83 | 1193 | # Initialise default storage. |
paul@83 | 1194 | # NOTE: This must be initialised separately due to the reliance on node |
paul@83 | 1195 | # NOTE: visiting. |
paul@83 | 1196 | |
paul@83 | 1197 | self.default_attrs = [] |
paul@83 | 1198 | |
paul@272 | 1199 | # Initialise attribute usage. |
paul@272 | 1200 | |
paul@358 | 1201 | if node is not None: |
paul@358 | 1202 | for arg in argnames: |
paul@279 | 1203 | |
paul@358 | 1204 | # Define attribute users. |
paul@279 | 1205 | |
paul@279 | 1206 | self._define_attribute_user_for_name(node, arg) |
paul@279 | 1207 | |
paul@83 | 1208 | # Caches. |
paul@83 | 1209 | |
paul@83 | 1210 | self.localnames = None # cache for locals |
paul@83 | 1211 | |
paul@83 | 1212 | # Add parameters to the namespace. |
paul@83 | 1213 | |
paul@83 | 1214 | self._add_parameters(argnames) |
paul@83 | 1215 | |
paul@83 | 1216 | # Image generation details. |
paul@83 | 1217 | |
paul@330 | 1218 | self.dynamic = None |
paul@83 | 1219 | |
paul@83 | 1220 | # Program-related details. |
paul@83 | 1221 | |
paul@116 | 1222 | self.temp_usage = 0 |
paul@116 | 1223 | self.local_usage = 0 |
paul@133 | 1224 | self.all_local_usage = 0 |
paul@83 | 1225 | |
paul@83 | 1226 | def _add_parameters(self, argnames): |
paul@368 | 1227 | |
paul@368 | 1228 | "Add 'argnames' to the namespace." |
paul@368 | 1229 | |
paul@368 | 1230 | for name in argnames: |
paul@448 | 1231 | self.set(name, make_instance()) |
paul@368 | 1232 | |
paul@368 | 1233 | for name, top_level in self._flattened_parameters(argnames): |
paul@368 | 1234 | if not top_level: |
paul@448 | 1235 | self.set(name, make_instance()) |
paul@368 | 1236 | |
paul@368 | 1237 | def _flattened_parameters(self, argnames, top_level=1): |
paul@368 | 1238 | l = [] |
paul@83 | 1239 | for name in argnames: |
paul@83 | 1240 | if isinstance(name, tuple): |
paul@368 | 1241 | l += self._flattened_parameters(name, 0) |
paul@83 | 1242 | else: |
paul@368 | 1243 | l.append((name, top_level)) |
paul@368 | 1244 | return l |
paul@83 | 1245 | |
paul@83 | 1246 | def __repr__(self): |
paul@667 | 1247 | return "<function %s>" % shortrepr(self) |
paul@83 | 1248 | |
paul@87 | 1249 | def __shortrepr__(self): |
paul@793 | 1250 | return "%s.%s(%s)" % (shortrepr(self.parent), self.name, ", ".join([repr(arg) for arg in self.argnames])) |
paul@87 | 1251 | |
paul@351 | 1252 | def is_lambda(self): |
paul@351 | 1253 | return self._is_lambda |
paul@351 | 1254 | |
paul@330 | 1255 | # Defaults-related methods. |
paul@189 | 1256 | |
paul@329 | 1257 | def store_default(self, attr_or_value): |
paul@329 | 1258 | |
paul@329 | 1259 | """ |
paul@329 | 1260 | Reserve space for defaults, set outside the function, potentially on a |
paul@329 | 1261 | dynamic basis, using the 'attr_or_value'. |
paul@329 | 1262 | """ |
paul@329 | 1263 | |
paul@192 | 1264 | attr = Attr(None, self, None) |
paul@329 | 1265 | self._set_using_attr(attr, attr_or_value) |
paul@83 | 1266 | self.default_attrs.append(attr) |
paul@83 | 1267 | |
paul@330 | 1268 | def make_dynamic(self): |
paul@330 | 1269 | |
paul@330 | 1270 | "Return whether this function must be handled using a dynamic object." |
paul@330 | 1271 | |
paul@330 | 1272 | if self.dynamic is None: |
paul@330 | 1273 | for attr in self.default_attrs: |
paul@331 | 1274 | if not attr.is_strict_constant() and self.dynamic_def: |
paul@591 | 1275 | self.dynamic = True |
paul@330 | 1276 | self._make_dynamic() |
paul@330 | 1277 | break |
paul@330 | 1278 | else: |
paul@591 | 1279 | self.dynamic = False |
paul@330 | 1280 | |
paul@330 | 1281 | return self.dynamic |
paul@330 | 1282 | |
paul@330 | 1283 | is_dynamic = make_dynamic |
paul@330 | 1284 | |
paul@330 | 1285 | def _make_dynamic(self): |
paul@330 | 1286 | |
paul@763 | 1287 | "Where functions have dynamic defaults, add a context local." |
paul@330 | 1288 | |
paul@763 | 1289 | self.set("__context__", make_instance()) |
paul@330 | 1290 | |
paul@330 | 1291 | # Namespace-related methods. |
paul@330 | 1292 | |
paul@83 | 1293 | def make_global(self, name): |
paul@339 | 1294 | |
paul@339 | 1295 | "Declare 'name' as a global in the current namespace." |
paul@339 | 1296 | |
paul@83 | 1297 | if name not in self.argnames and not self.has_key(name): |
paul@83 | 1298 | self.globals.add(name) |
paul@591 | 1299 | return True |
paul@83 | 1300 | else: |
paul@591 | 1301 | return False |
paul@83 | 1302 | |
paul@83 | 1303 | def parameters(self): |
paul@83 | 1304 | |
paul@83 | 1305 | """ |
paul@83 | 1306 | Return a dictionary mapping parameter names to their position in the |
paul@83 | 1307 | parameter list. |
paul@83 | 1308 | """ |
paul@83 | 1309 | |
paul@83 | 1310 | parameters = {} |
paul@83 | 1311 | for i, name in enumerate(self.argnames): |
paul@83 | 1312 | parameters[name] = i |
paul@83 | 1313 | return parameters |
paul@83 | 1314 | |
paul@368 | 1315 | def tuple_parameters(self, argnames=None): |
paul@368 | 1316 | |
paul@368 | 1317 | """ |
paul@368 | 1318 | Return a list of (position, parameter) entries corresponding to tuple |
paul@368 | 1319 | parameters, where each parameter may either be a string or another such |
paul@368 | 1320 | list of entries. |
paul@368 | 1321 | """ |
paul@368 | 1322 | |
paul@368 | 1323 | names = argnames or self.argnames |
paul@368 | 1324 | |
paul@368 | 1325 | l = [] |
paul@368 | 1326 | for i, name in enumerate(names): |
paul@368 | 1327 | if isinstance(name, tuple): |
paul@368 | 1328 | l.append((i, self.tuple_parameters(name))) |
paul@368 | 1329 | elif argnames: |
paul@368 | 1330 | l.append((i, name)) |
paul@368 | 1331 | return l |
paul@368 | 1332 | |
paul@83 | 1333 | def all_locals(self): |
paul@83 | 1334 | |
paul@83 | 1335 | "Return a dictionary mapping names to local and parameter details." |
paul@83 | 1336 | |
paul@83 | 1337 | return dict(self) |
paul@83 | 1338 | |
paul@83 | 1339 | def locals(self): |
paul@83 | 1340 | |
paul@83 | 1341 | "Return a dictionary mapping names to local details." |
paul@83 | 1342 | |
paul@83 | 1343 | if self.localnames is None: |
paul@83 | 1344 | self.localnames = {} |
paul@83 | 1345 | self.localnames.update(self.all_locals()) |
paul@83 | 1346 | for name in self.argnames: |
paul@83 | 1347 | del self.localnames[name] |
paul@83 | 1348 | return self.localnames |
paul@83 | 1349 | |
paul@83 | 1350 | def is_method(self): |
paul@83 | 1351 | |
paul@241 | 1352 | """ |
paul@241 | 1353 | Return whether this function is a method explicitly defined in a class. |
paul@241 | 1354 | """ |
paul@83 | 1355 | |
paul@83 | 1356 | return isinstance(self.parent, Class) |
paul@83 | 1357 | |
paul@83 | 1358 | def is_relocated(self, name): |
paul@83 | 1359 | |
paul@83 | 1360 | """ |
paul@83 | 1361 | Determine whether the given attribute 'name' is relocated for instances |
paul@83 | 1362 | having this function as a method. |
paul@83 | 1363 | """ |
paul@83 | 1364 | |
paul@83 | 1365 | for cls in self.parent.descendants: |
paul@83 | 1366 | if name in cls.relocated: |
paul@591 | 1367 | return True |
paul@591 | 1368 | return False |
paul@83 | 1369 | |
paul@351 | 1370 | # Administrative methods. |
paul@351 | 1371 | |
paul@351 | 1372 | def items_for_vacuum(self): |
paul@351 | 1373 | return self.lambdas.items() |
paul@351 | 1374 | |
paul@351 | 1375 | def vacuum_item(self, name): |
paul@351 | 1376 | del self.lambdas[name] |
paul@591 | 1377 | return True |
paul@351 | 1378 | |
paul@287 | 1379 | def finalise_attributes(self): |
paul@83 | 1380 | |
paul@83 | 1381 | """ |
paul@83 | 1382 | Make sure all attributes (locals) are fully defined. Note that locals |
paul@83 | 1383 | are not attributes in the sense of class, module or instance attributes. |
paul@83 | 1384 | Defaults are also finalised by this method. |
paul@83 | 1385 | """ |
paul@83 | 1386 | |
paul@210 | 1387 | if self.finalised: |
paul@210 | 1388 | return |
paul@210 | 1389 | |
paul@763 | 1390 | # Defaults are positioned in the function structure. |
paul@210 | 1391 | |
paul@83 | 1392 | for i, default in enumerate(self.default_attrs): |
paul@83 | 1393 | default.position = i |
paul@83 | 1394 | |
paul@368 | 1395 | # Parameters. |
paul@210 | 1396 | |
paul@368 | 1397 | i = self._finalise_parameters() |
paul@83 | 1398 | |
paul@83 | 1399 | if i is not None: |
paul@144 | 1400 | nparams = i + 1 |
paul@83 | 1401 | else: |
paul@144 | 1402 | nparams = 0 |
paul@144 | 1403 | |
paul@763 | 1404 | # Locals (and tuple parameter names) are positioned in the current stack |
paul@763 | 1405 | # frame. |
paul@368 | 1406 | |
paul@144 | 1407 | i = None |
paul@144 | 1408 | for i, attr in enumerate(self.locals().values()): |
paul@144 | 1409 | attr.position = i + nparams |
paul@83 | 1410 | |
paul@144 | 1411 | if i is not None: |
paul@144 | 1412 | nothers = i + 1 |
paul@144 | 1413 | else: |
paul@144 | 1414 | nothers = 0 |
paul@83 | 1415 | |
paul@144 | 1416 | self.local_usage = nothers |
paul@144 | 1417 | self.all_local_usage = nparams + nothers |
paul@591 | 1418 | self.finalised = True |
paul@83 | 1419 | |
paul@368 | 1420 | def _finalise_parameters(self): |
paul@368 | 1421 | if not self.argnames: |
paul@368 | 1422 | return None |
paul@368 | 1423 | |
paul@368 | 1424 | for i, name in enumerate(self.argnames): |
paul@368 | 1425 | self[name].position = i |
paul@368 | 1426 | |
paul@368 | 1427 | return i |
paul@368 | 1428 | |
paul@212 | 1429 | def as_instantiator(self): |
paul@83 | 1430 | |
paul@230 | 1431 | "Make an instantiator function from a method, keeping all arguments." |
paul@83 | 1432 | |
paul@230 | 1433 | function = Function(self.parent.name, self.parent.parent, self.argnames, self.defaults, |
paul@358 | 1434 | self.has_star, self.has_dstar, self.dynamic_def, self.module) |
paul@83 | 1435 | function.default_attrs = self.default_attrs |
paul@83 | 1436 | return function |
paul@83 | 1437 | |
paul@85 | 1438 | class UnresolvedName(NamespaceDict, Constant): |
paul@83 | 1439 | |
paul@83 | 1440 | "A module, class or function which was mentioned but could not be imported." |
paul@83 | 1441 | |
paul@180 | 1442 | def __init__(self, name, parent_name, module=None): |
paul@180 | 1443 | NamespaceDict.__init__(self, module) |
paul@83 | 1444 | self.name = name |
paul@83 | 1445 | self.parent_name = parent_name |
paul@88 | 1446 | self.parent = None |
paul@167 | 1447 | |
paul@167 | 1448 | self.descendants = set() |
paul@167 | 1449 | |
paul@167 | 1450 | def add_descendant(self, cls): |
paul@167 | 1451 | self.descendants.add(cls) |
paul@83 | 1452 | |
paul@363 | 1453 | def all_attributes(self): |
paul@83 | 1454 | return {} |
paul@83 | 1455 | |
paul@363 | 1456 | def all_attribute_names(self): |
paul@363 | 1457 | return [] |
paul@363 | 1458 | |
paul@363 | 1459 | all_class_attributes = class_attributes = instance_attributes = all_attributes |
paul@363 | 1460 | all_class_attribute_names = class_attribute_names = instance_attribute_names = all_attribute_names |
paul@83 | 1461 | |
paul@83 | 1462 | def __repr__(self): |
paul@494 | 1463 | return "<unknown %s>" % shortrepr(self) |
paul@494 | 1464 | |
paul@494 | 1465 | def __shortrepr__(self): |
paul@607 | 1466 | if self.name is not None: |
paul@607 | 1467 | return "%s.%s" % (self.parent_name, self.name) |
paul@607 | 1468 | else: |
paul@607 | 1469 | return self.parent_name |
paul@87 | 1470 | |
paul@83 | 1471 | def full_name(self): |
paul@83 | 1472 | if self.name is not None: |
paul@83 | 1473 | return self.parent_name + "." + self.name |
paul@83 | 1474 | else: |
paul@83 | 1475 | return self.parent_name |
paul@83 | 1476 | |
paul@85 | 1477 | class Module(NamespaceDict, Constant): |
paul@83 | 1478 | |
paul@83 | 1479 | "An inspected module's core details." |
paul@83 | 1480 | |
paul@343 | 1481 | def __init__(self, name, importer): |
paul@83 | 1482 | NamespaceDict.__init__(self, self) |
paul@83 | 1483 | self.name = name |
paul@343 | 1484 | self.importer = importer |
paul@88 | 1485 | self.parent = None |
paul@83 | 1486 | |
paul@83 | 1487 | # Original location details. |
paul@83 | 1488 | |
paul@91 | 1489 | self.astnode = None |
paul@83 | 1490 | |
paul@83 | 1491 | # Complete lists of classes and functions. |
paul@83 | 1492 | |
paul@83 | 1493 | self.all_objects = set() |
paul@83 | 1494 | |
paul@581 | 1495 | # Whether the module is imported in a circular fashion, exposing the |
paul@581 | 1496 | # unfinished namespace to external modification. |
paul@581 | 1497 | |
paul@581 | 1498 | self.circular_import = self in importer.circular_imports |
paul@581 | 1499 | |
paul@576 | 1500 | # A set of global names that cannot support combined attribute usage |
paul@576 | 1501 | # observations because they may be modified within functions during |
paul@576 | 1502 | # initialisation. |
paul@576 | 1503 | |
paul@576 | 1504 | self.modified_names = set() |
paul@575 | 1505 | |
paul@83 | 1506 | # Keyword records. |
paul@83 | 1507 | |
paul@83 | 1508 | self.keyword_names = set() |
paul@83 | 1509 | |
paul@83 | 1510 | # Program-related details. |
paul@83 | 1511 | |
paul@116 | 1512 | self.temp_usage = 0 |
paul@116 | 1513 | self.local_usage = 0 |
paul@133 | 1514 | self.all_local_usage = 0 |
paul@83 | 1515 | |
paul@83 | 1516 | def full_name(self): |
paul@83 | 1517 | return self.name |
paul@83 | 1518 | |
paul@83 | 1519 | def __repr__(self): |
paul@667 | 1520 | return "<module %s>" % shortrepr(self) |
paul@83 | 1521 | |
paul@87 | 1522 | def __shortrepr__(self): |
paul@494 | 1523 | return self.name |
paul@87 | 1524 | |
paul@83 | 1525 | # Attribute methods. |
paul@83 | 1526 | |
paul@83 | 1527 | "Return the module attribute names provided by the module." |
paul@83 | 1528 | |
paul@83 | 1529 | module_attribute_names = NamespaceDict.keys |
paul@83 | 1530 | |
paul@83 | 1531 | def module_attributes(self): |
paul@83 | 1532 | |
paul@83 | 1533 | "Return a dictionary mapping names to module attributes." |
paul@83 | 1534 | |
paul@83 | 1535 | return dict(self) |
paul@83 | 1536 | |
paul@612 | 1537 | all_attributes = module_attributes |
paul@612 | 1538 | |
paul@609 | 1539 | def get_static_attribute(self, name): |
paul@609 | 1540 | |
paul@609 | 1541 | """ |
paul@609 | 1542 | Return a static attribute for the given 'name' or None if no such |
paul@609 | 1543 | attribute exists. |
paul@609 | 1544 | """ |
paul@609 | 1545 | |
paul@609 | 1546 | return self.get(name) |
paul@609 | 1547 | |
paul@578 | 1548 | def modify_name(self, name): |
paul@578 | 1549 | |
paul@578 | 1550 | """ |
paul@578 | 1551 | Modify a global 'name' invalidating various assumptions about its |
paul@578 | 1552 | behaviour based on the module namespace being "safe" and suitable for |
paul@578 | 1553 | attribute usage and constant value observations. |
paul@578 | 1554 | """ |
paul@578 | 1555 | |
paul@578 | 1556 | self.modified_names.add(name) |
paul@578 | 1557 | |
paul@578 | 1558 | # Establish an attribute directly in the namespace if not present. |
paul@578 | 1559 | |
paul@578 | 1560 | if not self.namespace.has_key(name): |
paul@578 | 1561 | self.namespace[name] = Attr(None, self, name) |
paul@578 | 1562 | |
paul@581 | 1563 | # Indicate that there is at least one assignment to the name, although |
paul@581 | 1564 | # no actual values are recorded. |
paul@581 | 1565 | |
paul@578 | 1566 | attr = self.namespace[name] |
paul@578 | 1567 | attr.update_assignments(1, False) |
paul@578 | 1568 | |
paul@581 | 1569 | def set(self, name, value, single_assignment=1): |
paul@581 | 1570 | |
paul@581 | 1571 | """ |
paul@581 | 1572 | Set the module attribute with the given 'name', having the given 'value' |
paul@581 | 1573 | that is assigned once if 'single_assignment' is omitted to given as a |
paul@581 | 1574 | true value. |
paul@581 | 1575 | |
paul@581 | 1576 | Where the module is imported before it is completely initialised, all |
paul@581 | 1577 | assignments will be regarded as multiple assignments since it is |
paul@581 | 1578 | possible that an external assignment to the name may occur. |
paul@581 | 1579 | """ |
paul@581 | 1580 | |
paul@581 | 1581 | NamespaceDict.set(self, name, value, not self.circular_import and single_assignment) |
paul@581 | 1582 | |
paul@575 | 1583 | # Attribute usage methods that apply to module globals in certain |
paul@575 | 1584 | # circumstances. |
paul@575 | 1585 | |
paul@634 | 1586 | def can_use_name_for_usage(self, name): |
paul@634 | 1587 | return name not in self.modified_names and not self.circular_import |
paul@634 | 1588 | |
paul@576 | 1589 | def _use_attribute(self, name, attrname, value=None): |
paul@634 | 1590 | if self.can_use_name_for_usage(name): |
paul@576 | 1591 | return NamespaceDict._use_attribute(self, name, attrname, value) |
paul@576 | 1592 | else: |
paul@577 | 1593 | self.importer.use_name(attrname, self.full_name(), value) |
paul@576 | 1594 | return [] |
paul@576 | 1595 | |
paul@576 | 1596 | def _define_attribute_user_for_name(self, node, name): |
paul@634 | 1597 | if self.can_use_name_for_usage(name): |
paul@576 | 1598 | NamespaceDict._define_attribute_user_for_name(self, node, name) |
paul@576 | 1599 | |
paul@576 | 1600 | def _init_attribute_user_for_name(self, node, name): |
paul@634 | 1601 | if self.can_use_name_for_usage(name): |
paul@576 | 1602 | NamespaceDict._init_attribute_user_for_name(self, node, name) |
paul@576 | 1603 | |
paul@576 | 1604 | def _define_attribute_accessor(self, name, attrname, node, value): |
paul@634 | 1605 | if self.can_use_name_for_usage(name): |
paul@576 | 1606 | NamespaceDict._define_attribute_accessor(self, name, attrname, node, value) |
paul@577 | 1607 | else: |
paul@577 | 1608 | self.importer.use_name(attrname, self.full_name(), value) |
paul@575 | 1609 | |
paul@617 | 1610 | # Pre-made class instances. |
paul@617 | 1611 | # For each of these, their details will be filled in later. |
paul@617 | 1612 | |
paul@617 | 1613 | premade = { |
paul@617 | 1614 | "bool" : Class("bool"), |
paul@617 | 1615 | "float" : Class("float"), |
paul@617 | 1616 | "int" : Class("int"), |
paul@617 | 1617 | "long" : Class("long"), |
paul@617 | 1618 | "str" : Class("str"), |
paul@617 | 1619 | "unicode" : Class("unicode"), |
paul@617 | 1620 | "type" : Class("type"), |
paul@709 | 1621 | "NoneType" : Class("NoneType"), |
paul@617 | 1622 | } |
paul@394 | 1623 | |
paul@618 | 1624 | def get_constant_class(name): |
paul@618 | 1625 | return premade[name] |
paul@618 | 1626 | |
paul@394 | 1627 | # Class construction. |
paul@394 | 1628 | |
paul@394 | 1629 | def get_class(name, parent, module, node): |
paul@394 | 1630 | |
paul@394 | 1631 | """ |
paul@394 | 1632 | Return a Class instance for the class with the given 'name', 'parent', |
paul@394 | 1633 | 'module' and 'node'. |
paul@394 | 1634 | """ |
paul@394 | 1635 | |
paul@617 | 1636 | if premade.has_key(name) and module.full_name() == "__builtins__": |
paul@617 | 1637 | cls = premade[name] |
paul@617 | 1638 | cls.set_context(parent, module, node) |
paul@394 | 1639 | else: |
paul@635 | 1640 | # Where names are reused in a namespace, differentiate between classes |
paul@635 | 1641 | # using a name index. |
paul@635 | 1642 | |
paul@665 | 1643 | original_name = name |
paul@665 | 1644 | |
paul@635 | 1645 | if parent.has_key(name): |
paul@694 | 1646 | assignments = parent[name].static_assignments |
paul@749 | 1647 | if assignments >= 1: |
paul@694 | 1648 | name = "%s#%d" % (name, assignments + 1) |
paul@635 | 1649 | |
paul@665 | 1650 | cls = Class(name, parent, module, node, original_name) |
paul@635 | 1651 | |
paul@635 | 1652 | # Add a reference for the class's "shadow" name. |
paul@635 | 1653 | |
paul@635 | 1654 | parent.use_specific_attribute(parent.full_name(), name) |
paul@635 | 1655 | |
paul@635 | 1656 | return cls |
paul@394 | 1657 | |
paul@665 | 1658 | # Function construction. |
paul@665 | 1659 | |
paul@665 | 1660 | def get_function(name, parent, argnames, defaults, has_star, has_dstar, |
paul@665 | 1661 | dynamic_def=0, module=None, node=None): |
paul@665 | 1662 | |
paul@665 | 1663 | """ |
paul@665 | 1664 | Return a Function instance for the class with the given 'name', 'parent', |
paul@665 | 1665 | and other details. |
paul@665 | 1666 | """ |
paul@665 | 1667 | |
paul@665 | 1668 | original_name = name |
paul@665 | 1669 | |
paul@665 | 1670 | if parent.has_key(name): |
paul@694 | 1671 | assignments = parent[name].static_assignments |
paul@749 | 1672 | if assignments >= 1: |
paul@694 | 1673 | name = "%s#%d" % (name, assignments + 1) |
paul@665 | 1674 | |
paul@665 | 1675 | fn = Function(name, parent, argnames, defaults, has_star, has_dstar, |
paul@665 | 1676 | dynamic_def, module, node, original_name) |
paul@665 | 1677 | |
paul@665 | 1678 | # Add a reference for the function's "shadow" name. |
paul@665 | 1679 | |
paul@665 | 1680 | if name: |
paul@665 | 1681 | parent.use_specific_attribute(parent.full_name(), name) |
paul@665 | 1682 | |
paul@665 | 1683 | return fn |
paul@665 | 1684 | |
paul@554 | 1685 | # Lambda sequence numbering. |
paul@554 | 1686 | |
paul@554 | 1687 | lambda_index = 0 |
paul@554 | 1688 | |
paul@554 | 1689 | def new_lambda(): |
paul@554 | 1690 | |
paul@554 | 1691 | "Return a new sequence number for a lambda definition." |
paul@554 | 1692 | |
paul@554 | 1693 | global lambda_index |
paul@554 | 1694 | lambda_index += 1 |
paul@554 | 1695 | return lambda_index |
paul@554 | 1696 | |
paul@555 | 1697 | # Special representations. |
paul@555 | 1698 | |
paul@555 | 1699 | class AtLeast: |
paul@555 | 1700 | |
paul@555 | 1701 | "A special representation for numbers of a given value or greater." |
paul@555 | 1702 | |
paul@555 | 1703 | def __init__(self, count): |
paul@555 | 1704 | self.count = count |
paul@555 | 1705 | |
paul@555 | 1706 | def __eq__(self, other): |
paul@591 | 1707 | return False |
paul@555 | 1708 | |
paul@555 | 1709 | __lt__ = __le__ = __eq__ |
paul@555 | 1710 | |
paul@555 | 1711 | def __ne__(self, other): |
paul@591 | 1712 | return True |
paul@555 | 1713 | |
paul@555 | 1714 | def __gt__(self, other): |
paul@555 | 1715 | if isinstance(other, AtLeast): |
paul@591 | 1716 | return False |
paul@555 | 1717 | else: |
paul@555 | 1718 | return self.count > other |
paul@555 | 1719 | |
paul@555 | 1720 | def __ge__(self, other): |
paul@555 | 1721 | if isinstance(other, AtLeast): |
paul@591 | 1722 | return False |
paul@555 | 1723 | else: |
paul@555 | 1724 | return self.count >= other |
paul@555 | 1725 | |
paul@555 | 1726 | def __iadd__(self, other): |
paul@555 | 1727 | if isinstance(other, AtLeast): |
paul@555 | 1728 | self.count += other.count |
paul@555 | 1729 | else: |
paul@555 | 1730 | self.count += other |
paul@555 | 1731 | return self |
paul@555 | 1732 | |
paul@555 | 1733 | def __radd__(self, other): |
paul@555 | 1734 | if isinstance(other, AtLeast): |
paul@555 | 1735 | return AtLeast(self.count + other.count) |
paul@555 | 1736 | else: |
paul@555 | 1737 | return AtLeast(self.count + other) |
paul@555 | 1738 | |
paul@555 | 1739 | def __repr__(self): |
paul@555 | 1740 | return "AtLeast(%r)" % self.count |
paul@555 | 1741 | |
paul@83 | 1742 | # vim: tabstop=4 expandtab shiftwidth=4 |