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