paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@83 | 3 | """ |
paul@83 | 4 | Data classes. |
paul@83 | 5 | |
paul@83 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@83 | 7 | |
paul@83 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@83 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@83 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@83 | 11 | version. |
paul@83 | 12 | |
paul@83 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@83 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@83 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@83 | 16 | details. |
paul@83 | 17 | |
paul@83 | 18 | You should have received a copy of the GNU General Public License along with |
paul@83 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@83 | 20 | |
paul@83 | 21 | -------- |
paul@83 | 22 | |
paul@83 | 23 | The central classes in this module are the following: |
paul@83 | 24 | |
paul@83 | 25 | * Class |
paul@83 | 26 | * Function |
paul@83 | 27 | * Module |
paul@83 | 28 | |
paul@83 | 29 | All of the above support the Naming interface either explicitly or through |
paul@83 | 30 | general conformance, meaning that all can be asked to provide their 'full_name' |
paul@83 | 31 | using the method of that name. |
paul@83 | 32 | |
paul@83 | 33 | Additionally, all of the above also support a dictionary interface in order to |
paul@83 | 34 | access names within their defined scopes. Specific methods also exist in order |
paul@83 | 35 | to distinguish between certain kinds of attributes: |
paul@83 | 36 | |
paul@83 | 37 | * Class: (class|all_class|instance|all)_attributes |
paul@83 | 38 | * Function: parameters, locals, all_locals |
paul@83 | 39 | * Module: module_attributes |
paul@83 | 40 | |
paul@83 | 41 | These specific methods are useful in certain situations. |
paul@83 | 42 | |
paul@92 | 43 | The above classes also provide an 'astnode' attribute, indicating the AST node |
paul@92 | 44 | where each such object is defined. |
paul@83 | 45 | """ |
paul@83 | 46 | |
paul@83 | 47 | from micropython.common import * |
paul@83 | 48 | |
paul@87 | 49 | def shortrepr(obj): |
paul@87 | 50 | if obj is None: |
paul@87 | 51 | return repr(None) |
paul@87 | 52 | else: |
paul@87 | 53 | return obj.__shortrepr__() |
paul@87 | 54 | |
paul@83 | 55 | # Mix-ins and abstract classes. |
paul@83 | 56 | |
paul@83 | 57 | class NamespaceDict: |
paul@83 | 58 | |
paul@83 | 59 | "A mix-in providing dictionary methods." |
paul@83 | 60 | |
paul@83 | 61 | def __init__(self, global_namespace=None): |
paul@83 | 62 | self.namespace = {} |
paul@83 | 63 | self.globals = set() |
paul@83 | 64 | self.global_namespace = global_namespace |
paul@92 | 65 | self.finalised = 0 |
paul@83 | 66 | |
paul@83 | 67 | def __getitem__(self, name): |
paul@83 | 68 | return self.namespace[name] |
paul@83 | 69 | |
paul@83 | 70 | def get(self, name, default=None): |
paul@83 | 71 | return self.namespace.get(name, default) |
paul@83 | 72 | |
paul@83 | 73 | def __setitem__(self, name, value): |
paul@83 | 74 | self.set(name, value) |
paul@83 | 75 | |
paul@83 | 76 | def set(self, name, value, single_assignment=1): |
paul@83 | 77 | |
paul@83 | 78 | """ |
paul@83 | 79 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@83 | 80 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@83 | 81 | this operation (or whether the operation covers potentially many |
paul@83 | 82 | assignments in the lifetime of a program). |
paul@83 | 83 | """ |
paul@83 | 84 | |
paul@83 | 85 | if name in self.globals: |
paul@83 | 86 | self.global_namespace.set(name, value, 0) |
paul@83 | 87 | else: |
paul@83 | 88 | attr = self._set(name, value) |
paul@120 | 89 | attr.update(attr.value, single_assignment) |
paul@83 | 90 | |
paul@83 | 91 | def set_module(self, name, value): |
paul@83 | 92 | |
paul@83 | 93 | """ |
paul@83 | 94 | A specialised set operation, making 'name' refer to 'value' in the |
paul@83 | 95 | context of making a module reference available in association with |
paul@83 | 96 | 'name' as part of the import of that module or a submodule of that |
paul@83 | 97 | module. |
paul@83 | 98 | """ |
paul@83 | 99 | |
paul@83 | 100 | attr = self._set(name, value) |
paul@83 | 101 | if attr.assignments is None: |
paul@83 | 102 | attr.assignments = 1 |
paul@120 | 103 | attr.assignment_values.add(attr.value) |
paul@83 | 104 | |
paul@83 | 105 | def _set(self, name, value): |
paul@83 | 106 | |
paul@83 | 107 | "The underlying set operation associating 'name' with 'value'." |
paul@83 | 108 | |
paul@83 | 109 | if not self.namespace.has_key(name): |
paul@87 | 110 | |
paul@120 | 111 | # Either accept the attribute as specified. |
paul@120 | 112 | |
paul@120 | 113 | if isinstance(value, Attr): |
paul@120 | 114 | if value.context is not None: |
paul@120 | 115 | self.namespace[name] = Attr(None, self, value.context, name, value.value) |
paul@120 | 116 | return self.namespace[name] |
paul@120 | 117 | else: |
paul@120 | 118 | value = value.value |
paul@120 | 119 | |
paul@120 | 120 | # Or attempt to fix the context. |
paul@87 | 121 | |
paul@87 | 122 | context = self._context(value) |
paul@87 | 123 | self.namespace[name] = Attr(None, self, context, name, value) |
paul@87 | 124 | |
paul@83 | 125 | return self.namespace[name] |
paul@83 | 126 | |
paul@87 | 127 | def _context(self, value): |
paul@87 | 128 | |
paul@119 | 129 | """ |
paul@119 | 130 | Return the context to be used when storing the given 'value'. |
paul@119 | 131 | NOTE: This context is not likely to be useful when preparing an image |
paul@119 | 132 | NOTE: since only instance contexts have significant effects at run-time. |
paul@119 | 133 | """ |
paul@87 | 134 | |
paul@120 | 135 | return None |
paul@87 | 136 | |
paul@83 | 137 | def __delitem__(self, name): |
paul@83 | 138 | del self.namespace[name] |
paul@83 | 139 | |
paul@83 | 140 | def has_key(self, name): |
paul@83 | 141 | return self.namespace.has_key(name) |
paul@83 | 142 | |
paul@83 | 143 | def keys(self): |
paul@83 | 144 | return self.namespace.keys() |
paul@83 | 145 | |
paul@83 | 146 | def values(self): |
paul@83 | 147 | return self.namespace.values() |
paul@83 | 148 | |
paul@83 | 149 | def items(self): |
paul@83 | 150 | return self.namespace.items() |
paul@83 | 151 | |
paul@83 | 152 | def make_global(self, name): |
paul@83 | 153 | if not self.namespace.has_key(name): |
paul@83 | 154 | self.globals.add(name) |
paul@83 | 155 | else: |
paul@91 | 156 | raise InspectError(self.full_name(), self.astnode, "Name %r is both global and local in %r" % (name, self.full_name())) |
paul@83 | 157 | |
paul@83 | 158 | def get_assignments(self, name): |
paul@83 | 159 | if self.assignments.has_key(name): |
paul@83 | 160 | return max(self.assignments[name], len(self.assignment_values[name])) |
paul@83 | 161 | else: |
paul@83 | 162 | return None |
paul@83 | 163 | |
paul@83 | 164 | def attributes_as_list(self): |
paul@92 | 165 | |
paul@92 | 166 | "Return the attributes in a list." |
paul@92 | 167 | |
paul@83 | 168 | self.finalise_attributes() |
paul@83 | 169 | l = [None] * len(self.keys()) |
paul@83 | 170 | for attr in self.values(): |
paul@83 | 171 | l[attr.position] = attr |
paul@83 | 172 | return l |
paul@83 | 173 | |
paul@83 | 174 | def finalise_attributes(self): |
paul@83 | 175 | |
paul@83 | 176 | "Make sure all attributes are fully defined." |
paul@83 | 177 | |
paul@92 | 178 | if self.finalised: |
paul@92 | 179 | return |
paul@92 | 180 | |
paul@83 | 181 | # The default action is to assign attribute positions sequentially. |
paul@83 | 182 | |
paul@83 | 183 | for i, attr in enumerate(self.values()): |
paul@83 | 184 | attr.position = i |
paul@83 | 185 | |
paul@92 | 186 | self.finalised = 1 |
paul@92 | 187 | |
paul@85 | 188 | # Program data structures. There are two separate kinds of structures: those |
paul@85 | 189 | # with context, which are the values manipulated by programs, and those without |
paul@85 | 190 | # context, which are typically constant things which are stored alongside the |
paul@85 | 191 | # program but which are wrapped in context-dependent structures in the running |
paul@85 | 192 | # program. |
paul@83 | 193 | |
paul@83 | 194 | class Attr: |
paul@83 | 195 | |
paul@87 | 196 | "An attribute entry having a context." |
paul@83 | 197 | |
paul@87 | 198 | def __init__(self, position, parent, context, name, value=None, assignments=None): |
paul@119 | 199 | |
paul@119 | 200 | """ |
paul@119 | 201 | Initialise the attribute with the given 'position' within the collection |
paul@119 | 202 | of attributes of its 'parent', indicating the 'context' or origin of the |
paul@119 | 203 | attribute (where it was first defined), along with its 'name'. |
paul@119 | 204 | |
paul@119 | 205 | An optional 'value' indicates the typical contents of the attribute, and |
paul@119 | 206 | the optional number of 'assignments' may be used to determine whether |
paul@119 | 207 | the attribute is effectively constant. |
paul@119 | 208 | """ |
paul@119 | 209 | |
paul@83 | 210 | self.position = position |
paul@83 | 211 | self.parent = parent |
paul@87 | 212 | self.context = context |
paul@83 | 213 | self.name = name |
paul@83 | 214 | self.value = value |
paul@83 | 215 | |
paul@83 | 216 | # Number of assignments per name. |
paul@83 | 217 | |
paul@83 | 218 | self.assignments = assignments |
paul@83 | 219 | self.assignment_values = set() |
paul@83 | 220 | |
paul@83 | 221 | def update(self, value, single_assignment): |
paul@83 | 222 | |
paul@83 | 223 | """ |
paul@83 | 224 | Update the attribute, adding the 'value' provided to the known values |
paul@83 | 225 | associated with the attribute, changing the number of assignments |
paul@83 | 226 | according to the 'single_assignment' status of the operation, where |
paul@83 | 227 | a true value indicates that only one assignment is associated with the |
paul@83 | 228 | update, and a false value indicates that potentially many assignments |
paul@83 | 229 | may be involved. |
paul@83 | 230 | """ |
paul@83 | 231 | |
paul@83 | 232 | if self.assignments is None: |
paul@83 | 233 | if single_assignment: |
paul@83 | 234 | self.assignments = 1 |
paul@83 | 235 | else: |
paul@83 | 236 | self.assignments = AtLeast(1) |
paul@83 | 237 | else: |
paul@83 | 238 | if single_assignment: |
paul@83 | 239 | self.assignments += 1 |
paul@83 | 240 | else: |
paul@83 | 241 | self.assignments += AtLeast(1) |
paul@104 | 242 | |
paul@104 | 243 | if value is not None: |
paul@104 | 244 | self.assignment_values.add(value) |
paul@83 | 245 | |
paul@87 | 246 | def via_instance(self): |
paul@87 | 247 | |
paul@87 | 248 | """ |
paul@87 | 249 | Return either this attribute or a replacement where it is being accessed |
paul@87 | 250 | via an instance. |
paul@87 | 251 | """ |
paul@87 | 252 | |
paul@87 | 253 | if self.context is not None: |
paul@87 | 254 | |
paul@87 | 255 | # Check compatibility of the context with the parent. |
paul@87 | 256 | # Where the attribute originates within the same hierarchy, use an |
paul@87 | 257 | # instance as the context. |
paul@87 | 258 | |
paul@119 | 259 | if self.defined_within_hierarchy(): |
paul@87 | 260 | context = Instance() |
paul@87 | 261 | |
paul@87 | 262 | # Otherwise, preserve the existing context. |
paul@87 | 263 | |
paul@87 | 264 | else: |
paul@87 | 265 | context = self.context |
paul@87 | 266 | |
paul@87 | 267 | return Attr(self.position, self.parent, context, self.name, self.value, self.assignments) |
paul@87 | 268 | |
paul@87 | 269 | # Unknown contexts remain in use. |
paul@87 | 270 | |
paul@87 | 271 | else: |
paul@87 | 272 | return self |
paul@87 | 273 | |
paul@127 | 274 | def is_class_attribute(self): |
paul@127 | 275 | return isinstance(self.parent, Class) |
paul@127 | 276 | |
paul@119 | 277 | def defined_within_hierarchy(self): |
paul@119 | 278 | |
paul@119 | 279 | """ |
paul@119 | 280 | Return whether the parent and context of the attribute belong to the |
paul@119 | 281 | same class hierarchy. |
paul@119 | 282 | """ |
paul@119 | 283 | |
paul@119 | 284 | return isinstance(self.parent, Class) and isinstance(self.context, Class) and ( |
paul@119 | 285 | self.context is self.parent or |
paul@122 | 286 | self.context.has_subclass(self.parent) or |
paul@122 | 287 | self.parent.has_subclass(self.context)) |
paul@119 | 288 | |
paul@83 | 289 | def __repr__(self): |
paul@87 | 290 | return "Attr(%r, %s, %s, %r, %s, %r)" % ( |
paul@87 | 291 | self.position, shortrepr(self.parent), shortrepr(self.context), |
paul@87 | 292 | self.name, shortrepr(self.value), self.assignments |
paul@87 | 293 | ) |
paul@83 | 294 | |
paul@86 | 295 | # Instances are special in that they need to be wrapped together with context in |
paul@86 | 296 | # a running program, but they are not generally constant. |
paul@86 | 297 | |
paul@86 | 298 | class Instance: |
paul@86 | 299 | |
paul@86 | 300 | "A placeholder indicating the involvement of an instance." |
paul@86 | 301 | |
paul@87 | 302 | def __init__(self): |
paul@87 | 303 | self.parent = None |
paul@87 | 304 | |
paul@119 | 305 | # Image generation details. |
paul@119 | 306 | |
paul@119 | 307 | self.location = None |
paul@119 | 308 | |
paul@86 | 309 | def __repr__(self): |
paul@86 | 310 | return "Instance()" |
paul@86 | 311 | |
paul@87 | 312 | __shortrepr__ = __repr__ |
paul@87 | 313 | |
paul@85 | 314 | class Constant: |
paul@85 | 315 | |
paul@85 | 316 | "A superclass for all constant or context-free structures." |
paul@85 | 317 | |
paul@85 | 318 | pass |
paul@85 | 319 | |
paul@86 | 320 | class Const(Constant, Instance): |
paul@83 | 321 | |
paul@83 | 322 | "A constant object with no context." |
paul@83 | 323 | |
paul@83 | 324 | def __init__(self, value): |
paul@83 | 325 | self.value = value |
paul@88 | 326 | self.parent = None |
paul@83 | 327 | |
paul@83 | 328 | def __repr__(self): |
paul@83 | 329 | if self.location is not None: |
paul@83 | 330 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@83 | 331 | else: |
paul@83 | 332 | return "Const(%r)" % self.value |
paul@83 | 333 | |
paul@87 | 334 | __shortrepr__ = __repr__ |
paul@87 | 335 | |
paul@83 | 336 | # Support constants as dictionary keys in order to build constant tables. |
paul@83 | 337 | |
paul@83 | 338 | def __eq__(self, other): |
paul@83 | 339 | return self.value == other.value |
paul@83 | 340 | |
paul@83 | 341 | def __hash__(self): |
paul@83 | 342 | return hash(self.value) |
paul@83 | 343 | |
paul@83 | 344 | def value_type_name(self): |
paul@83 | 345 | return "__builtins__." + self.value.__class__.__name__ |
paul@83 | 346 | |
paul@85 | 347 | class Class(NamespaceDict, Naming, Constant): |
paul@83 | 348 | |
paul@83 | 349 | "An inspected class." |
paul@83 | 350 | |
paul@83 | 351 | def __init__(self, name, parent, global_namespace=None, node=None): |
paul@83 | 352 | |
paul@83 | 353 | """ |
paul@83 | 354 | Initialise the class with the given 'name', 'parent' object, optional |
paul@83 | 355 | 'global_namespace' and optional AST 'node'. |
paul@83 | 356 | """ |
paul@83 | 357 | |
paul@83 | 358 | NamespaceDict.__init__(self, global_namespace) |
paul@83 | 359 | self.name = name |
paul@83 | 360 | self.parent = parent |
paul@91 | 361 | self.astnode = node |
paul@83 | 362 | |
paul@83 | 363 | # Superclasses, descendants and attributes. |
paul@83 | 364 | |
paul@83 | 365 | self.bases = [] |
paul@83 | 366 | self.descendants = set() |
paul@83 | 367 | self.instattr = set() # instance attributes |
paul@83 | 368 | self.relocated = set() # attributes which do not have the same |
paul@83 | 369 | # position as those of the same name in |
paul@83 | 370 | # some superclasses |
paul@83 | 371 | |
paul@83 | 372 | # Caches. |
paul@83 | 373 | |
paul@83 | 374 | self.all_instattr = None # cache for instance_attributes |
paul@83 | 375 | self.all_instattr_names = None # from all_instattr |
paul@83 | 376 | self.all_classattr = None # cache for all_class_attributes |
paul@83 | 377 | self.all_classattr_names = None # from all_classattr |
paul@83 | 378 | self.allattr = None # cache for all_attributes |
paul@83 | 379 | self.allattr_names = None # from allattr |
paul@83 | 380 | |
paul@83 | 381 | # Add this class to its attributes. |
paul@83 | 382 | |
paul@83 | 383 | self.set("__class__", self) |
paul@83 | 384 | |
paul@83 | 385 | # Image generation details. |
paul@83 | 386 | |
paul@83 | 387 | self.location = None |
paul@83 | 388 | self.code_location = None |
paul@83 | 389 | self.instantiator = None |
paul@83 | 390 | |
paul@83 | 391 | # Program-related details. |
paul@83 | 392 | |
paul@116 | 393 | self.temp_usage = 0 |
paul@116 | 394 | self.local_usage = 0 |
paul@133 | 395 | self.all_local_usage = 0 |
paul@83 | 396 | |
paul@83 | 397 | def __repr__(self): |
paul@83 | 398 | if self.location is not None: |
paul@87 | 399 | return "Class(%r, %s, location=%r)" % (self.name, shortrepr(self.parent), self.location) |
paul@83 | 400 | else: |
paul@87 | 401 | return "Class(%r, %s)" % (self.name, shortrepr(self.parent)) |
paul@87 | 402 | |
paul@87 | 403 | def __shortrepr__(self): |
paul@87 | 404 | return "Class(%r, %s)" % (self.name, shortrepr(self.parent)) |
paul@87 | 405 | |
paul@87 | 406 | def _context(self, value): |
paul@87 | 407 | |
paul@119 | 408 | """ |
paul@119 | 409 | Return the context to be used when storing the given 'value'. |
paul@119 | 410 | NOTE: This context is not likely to be useful when preparing an image |
paul@119 | 411 | NOTE: since only instance contexts have significant effects at run-time. |
paul@119 | 412 | """ |
paul@87 | 413 | |
paul@87 | 414 | if value is not None: |
paul@87 | 415 | context = value.parent |
paul@87 | 416 | if isinstance(context, Module): |
paul@87 | 417 | return self |
paul@87 | 418 | else: |
paul@87 | 419 | return context |
paul@87 | 420 | else: |
paul@87 | 421 | return None |
paul@83 | 422 | |
paul@83 | 423 | def finalise_attributes(self): |
paul@83 | 424 | |
paul@83 | 425 | "Make sure that all attributes are fully defined." |
paul@83 | 426 | |
paul@92 | 427 | if self.finalised: |
paul@92 | 428 | return |
paul@92 | 429 | |
paul@83 | 430 | self.finalise_class_attributes() |
paul@83 | 431 | self.finalise_instance_attributes() |
paul@92 | 432 | self.finalised = 1 |
paul@83 | 433 | |
paul@83 | 434 | def get_instantiator(self): |
paul@83 | 435 | |
paul@83 | 436 | "Return a function which can be used to instantiate the class." |
paul@83 | 437 | |
paul@83 | 438 | if self.instantiator is None: |
paul@137 | 439 | self.instantiator = self.get_init_method().function_from_method() |
paul@83 | 440 | return self.instantiator |
paul@83 | 441 | |
paul@137 | 442 | def get_init_method(self): |
paul@137 | 443 | return self.all_class_attributes()["__init__"].value |
paul@137 | 444 | |
paul@83 | 445 | # Class-specific methods. |
paul@83 | 446 | |
paul@83 | 447 | def add_base(self, base): |
paul@83 | 448 | self.bases.append(base) |
paul@83 | 449 | base.add_descendant(self) |
paul@83 | 450 | |
paul@83 | 451 | def add_instance_attribute(self, name): |
paul@83 | 452 | self.instattr.add(name) |
paul@83 | 453 | |
paul@83 | 454 | def add_descendant(self, cls): |
paul@83 | 455 | self.descendants.add(cls) |
paul@83 | 456 | for base in self.bases: |
paul@83 | 457 | base.add_descendant(cls) |
paul@83 | 458 | |
paul@122 | 459 | def has_subclass(self, other): |
paul@122 | 460 | return other in self.descendants |
paul@122 | 461 | |
paul@135 | 462 | def all_descendants(self): |
paul@135 | 463 | d = {} |
paul@135 | 464 | for cls in self.descendants: |
paul@135 | 465 | d[cls.full_name()] = cls |
paul@135 | 466 | return d |
paul@135 | 467 | |
paul@83 | 468 | "Return the attribute names provided by this class only." |
paul@83 | 469 | |
paul@83 | 470 | class_attribute_names = NamespaceDict.keys |
paul@83 | 471 | |
paul@83 | 472 | def class_attributes(self): |
paul@83 | 473 | |
paul@83 | 474 | "Return class attributes provided by this class only." |
paul@83 | 475 | |
paul@83 | 476 | self.finalise_class_attributes() |
paul@83 | 477 | return dict(self) |
paul@83 | 478 | |
paul@83 | 479 | def all_class_attribute_names(self): |
paul@83 | 480 | |
paul@83 | 481 | "Return the attribute names provided by classes in this hierarchy." |
paul@83 | 482 | |
paul@83 | 483 | if self.all_classattr_names is None: |
paul@83 | 484 | self.all_class_attributes() |
paul@83 | 485 | return self.all_classattr_names |
paul@83 | 486 | |
paul@83 | 487 | def all_class_attributes(self): |
paul@83 | 488 | |
paul@83 | 489 | "Return all class attributes, indicating the class which provides them." |
paul@83 | 490 | |
paul@83 | 491 | self.finalise_class_attributes() |
paul@83 | 492 | return self.all_classattr |
paul@83 | 493 | |
paul@83 | 494 | def finalise_class_attributes(self): |
paul@83 | 495 | |
paul@83 | 496 | "Make sure that the class attributes are fully defined." |
paul@83 | 497 | |
paul@83 | 498 | if self.all_classattr is None: |
paul@83 | 499 | self.all_classattr = {} |
paul@83 | 500 | clsattr = {} |
paul@83 | 501 | |
paul@83 | 502 | # Record provisional position information for attributes of this |
paul@83 | 503 | # class. |
paul@83 | 504 | |
paul@83 | 505 | for name in self.class_attributes().keys(): |
paul@83 | 506 | clsattr[name] = set() # position not yet defined |
paul@83 | 507 | |
paul@83 | 508 | reversed_bases = self.bases[:] |
paul@83 | 509 | reversed_bases.reverse() |
paul@83 | 510 | |
paul@83 | 511 | # For the bases in reverse order, acquire class attribute details. |
paul@83 | 512 | |
paul@83 | 513 | for cls in reversed_bases: |
paul@83 | 514 | for name, attr in cls.all_class_attributes().items(): |
paul@83 | 515 | self.all_classattr[name] = attr |
paul@83 | 516 | |
paul@83 | 517 | # Record previous attribute information. |
paul@83 | 518 | |
paul@83 | 519 | if clsattr.has_key(name): |
paul@83 | 520 | clsattr[name].add(attr.position) |
paul@83 | 521 | |
paul@83 | 522 | # Record class attributes provided by this class and its bases, |
paul@83 | 523 | # along with their positions. |
paul@83 | 524 | |
paul@83 | 525 | self.all_classattr.update(self.class_attributes()) |
paul@83 | 526 | |
paul@83 | 527 | if clsattr: |
paul@83 | 528 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@83 | 529 | self.all_classattr[name].position = i |
paul@83 | 530 | |
paul@83 | 531 | return self.all_classattr |
paul@83 | 532 | |
paul@83 | 533 | def instance_attribute_names(self): |
paul@83 | 534 | |
paul@83 | 535 | "Return the instance attribute names provided by the class." |
paul@83 | 536 | |
paul@83 | 537 | if self.all_instattr_names is None: |
paul@83 | 538 | self.instance_attributes() |
paul@83 | 539 | return self.all_instattr_names |
paul@83 | 540 | |
paul@83 | 541 | def instance_attributes(self): |
paul@83 | 542 | |
paul@83 | 543 | "Return instance-only attributes for instances of this class." |
paul@83 | 544 | |
paul@83 | 545 | self.finalise_instance_attributes() |
paul@83 | 546 | return self.all_instattr |
paul@83 | 547 | |
paul@83 | 548 | def finalise_instance_attributes(self): |
paul@83 | 549 | |
paul@83 | 550 | "Make sure that the instance attributes are fully defined." |
paul@83 | 551 | |
paul@83 | 552 | if self.all_instattr is None: |
paul@83 | 553 | self.all_instattr = {} |
paul@83 | 554 | instattr = {} |
paul@83 | 555 | |
paul@83 | 556 | # Record provisional position information for attributes of this |
paul@83 | 557 | # instance. |
paul@83 | 558 | |
paul@83 | 559 | for name in self.instattr: |
paul@83 | 560 | instattr[name] = set() # position not yet defined |
paul@83 | 561 | |
paul@83 | 562 | reversed_bases = self.bases[:] |
paul@83 | 563 | reversed_bases.reverse() |
paul@83 | 564 | |
paul@83 | 565 | # For the bases in reverse order, acquire instance attribute |
paul@83 | 566 | # details. |
paul@83 | 567 | |
paul@83 | 568 | for cls in reversed_bases: |
paul@83 | 569 | for name, attr in cls.instance_attributes().items(): |
paul@83 | 570 | |
paul@83 | 571 | # Record previous attribute information. |
paul@83 | 572 | |
paul@83 | 573 | if instattr.has_key(name): |
paul@83 | 574 | instattr[name].add(attr.position) |
paul@83 | 575 | |
paul@83 | 576 | # Cache the attributes by converting the positioned attributes into |
paul@83 | 577 | # a dictionary. |
paul@83 | 578 | |
paul@83 | 579 | if not instattr: |
paul@83 | 580 | self.all_instattr = {} |
paul@83 | 581 | else: |
paul@83 | 582 | self.all_instattr = self._get_attributes(instattr) |
paul@83 | 583 | |
paul@83 | 584 | self.all_instattr_names = self.all_instattr.keys() |
paul@83 | 585 | |
paul@83 | 586 | return self.all_instattr |
paul@83 | 587 | |
paul@83 | 588 | def _get_position_list(self, positions): |
paul@83 | 589 | |
paul@83 | 590 | """ |
paul@83 | 591 | Return a list of attribute names for the given 'positions' mapping from |
paul@83 | 592 | names to positions, indicating the positions of the attributes in the |
paul@83 | 593 | final instance structure. |
paul@83 | 594 | """ |
paul@83 | 595 | |
paul@83 | 596 | position_items = positions.items() |
paul@83 | 597 | namearray = [None] * len(position_items) |
paul@83 | 598 | |
paul@83 | 599 | # Get the positions in ascending order of list size, with lists |
paul@83 | 600 | # of the same size ordered according to their smallest position |
paul@83 | 601 | # value. |
paul@83 | 602 | |
paul@83 | 603 | position_items.sort(self._cmp_positions) |
paul@83 | 604 | |
paul@83 | 605 | # Get the names in position order. |
paul@83 | 606 | |
paul@83 | 607 | held = [] |
paul@83 | 608 | |
paul@83 | 609 | for name, pos in position_items: |
paul@83 | 610 | pos = list(pos) |
paul@83 | 611 | pos.sort() |
paul@83 | 612 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@83 | 613 | namearray[pos[0]] = name |
paul@83 | 614 | else: |
paul@83 | 615 | if pos: |
paul@83 | 616 | self.relocated.add(name) |
paul@83 | 617 | held.append((name, pos)) |
paul@83 | 618 | |
paul@83 | 619 | for i, attr in enumerate(namearray): |
paul@83 | 620 | if attr is None: |
paul@83 | 621 | name, pos = held.pop() |
paul@83 | 622 | namearray[i] = name |
paul@83 | 623 | |
paul@83 | 624 | #print self.name, positions |
paul@83 | 625 | #print "->", namearray |
paul@83 | 626 | return namearray |
paul@83 | 627 | |
paul@83 | 628 | def _get_attributes(self, positions): |
paul@83 | 629 | |
paul@83 | 630 | """ |
paul@83 | 631 | For the given 'positions' mapping from names to positions, return a |
paul@83 | 632 | dictionary mapping names to Attr instances incorporating information |
paul@83 | 633 | about their positions in the final instance structure. |
paul@83 | 634 | """ |
paul@83 | 635 | |
paul@83 | 636 | d = {} |
paul@83 | 637 | for i, name in enumerate(self._get_position_list(positions)): |
paul@87 | 638 | d[name] = Attr(i, Instance(), None, name, None) |
paul@83 | 639 | return d |
paul@83 | 640 | |
paul@83 | 641 | def _cmp_positions(self, a, b): |
paul@83 | 642 | |
paul@83 | 643 | "Compare name plus position list operands 'a' and 'b'." |
paul@83 | 644 | |
paul@83 | 645 | name_a, list_a = a |
paul@83 | 646 | name_b, list_b = b |
paul@83 | 647 | if len(list_a) < len(list_b): |
paul@83 | 648 | return -1 |
paul@83 | 649 | elif len(list_a) > len(list_b): |
paul@83 | 650 | return 1 |
paul@83 | 651 | elif not list_a: |
paul@83 | 652 | return 0 |
paul@83 | 653 | else: |
paul@83 | 654 | return cmp(min(list_a), min(list_b)) |
paul@83 | 655 | |
paul@83 | 656 | def all_attribute_names(self): |
paul@83 | 657 | |
paul@83 | 658 | """ |
paul@83 | 659 | Return the names of all attributes provided by instances of this class. |
paul@83 | 660 | """ |
paul@83 | 661 | |
paul@83 | 662 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@83 | 663 | return self.allattr_names |
paul@83 | 664 | |
paul@83 | 665 | def all_attributes(self): |
paul@83 | 666 | |
paul@83 | 667 | """ |
paul@83 | 668 | Return all attributes for an instance, indicating either the class which |
paul@83 | 669 | provides them or that the instance itself provides them. |
paul@83 | 670 | """ |
paul@83 | 671 | |
paul@83 | 672 | if self.allattr is None: |
paul@83 | 673 | self.allattr = {} |
paul@83 | 674 | self.allattr.update(self.all_class_attributes()) |
paul@83 | 675 | for name, attr in self.instance_attributes().items(): |
paul@83 | 676 | if self.allattr.has_key(name): |
paul@83 | 677 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@83 | 678 | self.allattr[name] = attr |
paul@83 | 679 | return self.allattr |
paul@83 | 680 | |
paul@85 | 681 | class Function(NamespaceDict, Naming, Constant): |
paul@83 | 682 | |
paul@83 | 683 | "An inspected function." |
paul@83 | 684 | |
paul@83 | 685 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, global_namespace=None, node=None): |
paul@83 | 686 | |
paul@83 | 687 | """ |
paul@83 | 688 | Initialise the function with the given 'name', 'parent', list of |
paul@83 | 689 | 'argnames', list of 'defaults', the 'has_star' flag (indicating the |
paul@83 | 690 | presence of a * parameter), the 'has_dstar' flag (indicating the |
paul@83 | 691 | presence of a ** parameter), optional 'global_namespace', and optional |
paul@83 | 692 | AST 'node'. |
paul@83 | 693 | """ |
paul@83 | 694 | |
paul@83 | 695 | NamespaceDict.__init__(self, global_namespace) |
paul@83 | 696 | self.name = name |
paul@83 | 697 | self.parent = parent |
paul@83 | 698 | self.argnames = argnames |
paul@83 | 699 | self.defaults = defaults |
paul@83 | 700 | self.has_star = has_star |
paul@83 | 701 | self.has_dstar = has_dstar |
paul@91 | 702 | self.astnode = node |
paul@83 | 703 | |
paul@83 | 704 | # Initialise the positional names. |
paul@83 | 705 | |
paul@83 | 706 | self.positional_names = self.argnames[:] |
paul@83 | 707 | if has_dstar: |
paul@83 | 708 | self.dstar_name = self.positional_names[-1] |
paul@83 | 709 | del self.positional_names[-1] |
paul@83 | 710 | if has_star: |
paul@83 | 711 | self.star_name = self.positional_names[-1] |
paul@83 | 712 | del self.positional_names[-1] |
paul@83 | 713 | |
paul@83 | 714 | # Initialise default storage. |
paul@83 | 715 | # NOTE: This must be initialised separately due to the reliance on node |
paul@83 | 716 | # NOTE: visiting. |
paul@83 | 717 | |
paul@83 | 718 | self.default_attrs = [] |
paul@83 | 719 | |
paul@83 | 720 | # Caches. |
paul@83 | 721 | |
paul@83 | 722 | self.localnames = None # cache for locals |
paul@83 | 723 | |
paul@83 | 724 | # Add parameters to the namespace. |
paul@83 | 725 | |
paul@83 | 726 | self._add_parameters(argnames) |
paul@83 | 727 | |
paul@83 | 728 | # Image generation details. |
paul@83 | 729 | |
paul@83 | 730 | self.location = None |
paul@83 | 731 | self.code_location = None |
paul@83 | 732 | |
paul@83 | 733 | # Program-related details. |
paul@83 | 734 | |
paul@116 | 735 | self.temp_usage = 0 |
paul@116 | 736 | self.local_usage = 0 |
paul@133 | 737 | self.all_local_usage = 0 |
paul@83 | 738 | |
paul@83 | 739 | def _add_parameters(self, argnames): |
paul@83 | 740 | for name in argnames: |
paul@83 | 741 | if isinstance(name, tuple): |
paul@83 | 742 | self._add_parameters(name) |
paul@83 | 743 | else: |
paul@83 | 744 | self.set(name, None) |
paul@83 | 745 | |
paul@83 | 746 | def __repr__(self): |
paul@83 | 747 | if self.location is not None: |
paul@98 | 748 | return "Function(%r, %s, %r, location=%r, code_location=%r)" % ( |
paul@98 | 749 | self.name, shortrepr(self.parent), self.argnames, self.location, self.code_location |
paul@83 | 750 | ) |
paul@83 | 751 | else: |
paul@98 | 752 | return "Function(%r, %s, %r)" % ( |
paul@98 | 753 | self.name, shortrepr(self.parent), self.argnames |
paul@83 | 754 | ) |
paul@83 | 755 | |
paul@87 | 756 | def __shortrepr__(self): |
paul@87 | 757 | return "Function(%r, %s)" % ( |
paul@87 | 758 | self.name, shortrepr(self.parent) |
paul@87 | 759 | ) |
paul@87 | 760 | |
paul@83 | 761 | def store_default(self, value): |
paul@87 | 762 | attr = Attr(None, self, None, None, value) |
paul@83 | 763 | attr.update(value, 1) |
paul@83 | 764 | self.default_attrs.append(attr) |
paul@83 | 765 | |
paul@83 | 766 | def make_global(self, name): |
paul@83 | 767 | if name not in self.argnames and not self.has_key(name): |
paul@83 | 768 | self.globals.add(name) |
paul@83 | 769 | else: |
paul@91 | 770 | raise InspectError(self.full_name(), self.astnode, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@83 | 771 | |
paul@83 | 772 | def parameters(self): |
paul@83 | 773 | |
paul@83 | 774 | """ |
paul@83 | 775 | Return a dictionary mapping parameter names to their position in the |
paul@83 | 776 | parameter list. |
paul@83 | 777 | """ |
paul@83 | 778 | |
paul@83 | 779 | parameters = {} |
paul@83 | 780 | for i, name in enumerate(self.argnames): |
paul@83 | 781 | parameters[name] = i |
paul@83 | 782 | return parameters |
paul@83 | 783 | |
paul@83 | 784 | def all_locals(self): |
paul@83 | 785 | |
paul@83 | 786 | "Return a dictionary mapping names to local and parameter details." |
paul@83 | 787 | |
paul@83 | 788 | return dict(self) |
paul@83 | 789 | |
paul@83 | 790 | def locals(self): |
paul@83 | 791 | |
paul@83 | 792 | "Return a dictionary mapping names to local details." |
paul@83 | 793 | |
paul@83 | 794 | if self.localnames is None: |
paul@83 | 795 | self.localnames = {} |
paul@83 | 796 | self.localnames.update(self.all_locals()) |
paul@83 | 797 | for name in self.argnames: |
paul@83 | 798 | del self.localnames[name] |
paul@83 | 799 | return self.localnames |
paul@83 | 800 | |
paul@83 | 801 | def is_method(self): |
paul@83 | 802 | |
paul@83 | 803 | "Return whether this function is a method." |
paul@83 | 804 | |
paul@83 | 805 | return isinstance(self.parent, Class) |
paul@83 | 806 | |
paul@83 | 807 | def is_relocated(self, name): |
paul@83 | 808 | |
paul@83 | 809 | """ |
paul@83 | 810 | Determine whether the given attribute 'name' is relocated for instances |
paul@83 | 811 | having this function as a method. |
paul@83 | 812 | """ |
paul@83 | 813 | |
paul@83 | 814 | for cls in self.parent.descendants: |
paul@83 | 815 | if name in cls.relocated: |
paul@83 | 816 | return 1 |
paul@83 | 817 | return 0 |
paul@83 | 818 | |
paul@83 | 819 | def finalise_attributes(self): |
paul@83 | 820 | |
paul@83 | 821 | """ |
paul@83 | 822 | Make sure all attributes (locals) are fully defined. Note that locals |
paul@83 | 823 | are not attributes in the sense of class, module or instance attributes. |
paul@83 | 824 | Defaults are also finalised by this method. |
paul@83 | 825 | """ |
paul@83 | 826 | |
paul@83 | 827 | for i, default in enumerate(self.default_attrs): |
paul@83 | 828 | default.position = i |
paul@83 | 829 | |
paul@83 | 830 | i = None |
paul@83 | 831 | for i, name in enumerate(self.argnames): |
paul@83 | 832 | self[name].position = i |
paul@83 | 833 | |
paul@83 | 834 | if i is not None: |
paul@144 | 835 | nparams = i + 1 |
paul@83 | 836 | else: |
paul@144 | 837 | nparams = 0 |
paul@144 | 838 | |
paul@144 | 839 | i = None |
paul@144 | 840 | for i, attr in enumerate(self.locals().values()): |
paul@144 | 841 | attr.position = i + nparams |
paul@83 | 842 | |
paul@144 | 843 | if i is not None: |
paul@144 | 844 | nothers = i + 1 |
paul@144 | 845 | else: |
paul@144 | 846 | nothers = 0 |
paul@83 | 847 | |
paul@144 | 848 | self.local_usage = nothers |
paul@144 | 849 | self.all_local_usage = nparams + nothers |
paul@92 | 850 | self.finalised = 1 |
paul@83 | 851 | |
paul@83 | 852 | def function_from_method(self): |
paul@83 | 853 | |
paul@83 | 854 | "Make a function from a method." |
paul@83 | 855 | |
paul@83 | 856 | function = Function(self.name, self.parent, self.argnames[1:], self.defaults, |
paul@91 | 857 | self.has_star, self.has_dstar, self.global_namespace, self.astnode) |
paul@83 | 858 | function.default_attrs = self.default_attrs |
paul@83 | 859 | return function |
paul@83 | 860 | |
paul@85 | 861 | class UnresolvedName(NamespaceDict, Constant): |
paul@83 | 862 | |
paul@83 | 863 | "A module, class or function which was mentioned but could not be imported." |
paul@83 | 864 | |
paul@83 | 865 | def __init__(self, name, parent_name, global_namespace=None): |
paul@83 | 866 | NamespaceDict.__init__(self, global_namespace) |
paul@83 | 867 | self.name = name |
paul@83 | 868 | self.parent_name = parent_name |
paul@88 | 869 | self.parent = None |
paul@83 | 870 | |
paul@83 | 871 | def all_class_attributes(self): |
paul@83 | 872 | return {} |
paul@83 | 873 | |
paul@83 | 874 | def instance_attributes(self): |
paul@83 | 875 | return {} |
paul@83 | 876 | |
paul@83 | 877 | def __repr__(self): |
paul@83 | 878 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@83 | 879 | |
paul@87 | 880 | __shortrepr__ = __repr__ |
paul@87 | 881 | |
paul@83 | 882 | def full_name(self): |
paul@83 | 883 | if self.name is not None: |
paul@83 | 884 | return self.parent_name + "." + self.name |
paul@83 | 885 | else: |
paul@83 | 886 | return self.parent_name |
paul@83 | 887 | |
paul@85 | 888 | class Module(NamespaceDict, Constant): |
paul@83 | 889 | |
paul@83 | 890 | "An inspected module's core details." |
paul@83 | 891 | |
paul@83 | 892 | def __init__(self, name): |
paul@83 | 893 | NamespaceDict.__init__(self, self) |
paul@83 | 894 | self.name = name |
paul@88 | 895 | self.parent = None |
paul@83 | 896 | |
paul@83 | 897 | # Original location details. |
paul@83 | 898 | |
paul@91 | 899 | self.astnode = None |
paul@83 | 900 | |
paul@83 | 901 | # Complete lists of classes and functions. |
paul@83 | 902 | |
paul@83 | 903 | self.all_objects = set() |
paul@83 | 904 | |
paul@83 | 905 | # Keyword records. |
paul@83 | 906 | |
paul@83 | 907 | self.keyword_names = set() |
paul@83 | 908 | |
paul@83 | 909 | # Image generation details. |
paul@83 | 910 | |
paul@83 | 911 | self.location = None |
paul@83 | 912 | self.code_location = None |
paul@83 | 913 | |
paul@83 | 914 | # Program-related details. |
paul@83 | 915 | |
paul@116 | 916 | self.temp_usage = 0 |
paul@116 | 917 | self.local_usage = 0 |
paul@133 | 918 | self.all_local_usage = 0 |
paul@83 | 919 | |
paul@83 | 920 | def full_name(self): |
paul@83 | 921 | return self.name |
paul@83 | 922 | |
paul@83 | 923 | def __repr__(self): |
paul@83 | 924 | if self.location is not None: |
paul@83 | 925 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@83 | 926 | else: |
paul@83 | 927 | return "Module(%r)" % self.name |
paul@83 | 928 | |
paul@87 | 929 | def __shortrepr__(self): |
paul@87 | 930 | return "Module(%r)" % self.name |
paul@87 | 931 | |
paul@83 | 932 | # Attribute methods. |
paul@83 | 933 | |
paul@83 | 934 | "Return the module attribute names provided by the module." |
paul@83 | 935 | |
paul@83 | 936 | module_attribute_names = NamespaceDict.keys |
paul@83 | 937 | |
paul@83 | 938 | def module_attributes(self): |
paul@83 | 939 | |
paul@83 | 940 | "Return a dictionary mapping names to module attributes." |
paul@83 | 941 | |
paul@83 | 942 | return dict(self) |
paul@83 | 943 | |
paul@83 | 944 | # vim: tabstop=4 expandtab shiftwidth=4 |