paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@83 | 3 | """ |
paul@83 | 4 | Data classes. |
paul@83 | 5 | |
paul@178 | 6 | Copyright (C) 2007, 2008, 2009 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@180 | 61 | def __init__(self, module=None): |
paul@83 | 62 | self.namespace = {} |
paul@83 | 63 | self.globals = set() |
paul@180 | 64 | self.module = module |
paul@92 | 65 | self.finalised = 0 |
paul@83 | 66 | |
paul@167 | 67 | def __delitem__(self, name): |
paul@167 | 68 | del self.namespace[name] |
paul@167 | 69 | |
paul@167 | 70 | def has_key(self, name): |
paul@167 | 71 | return self.namespace.has_key(name) |
paul@167 | 72 | |
paul@167 | 73 | def keys(self): |
paul@167 | 74 | return self.namespace.keys() |
paul@167 | 75 | |
paul@167 | 76 | def values(self): |
paul@167 | 77 | return self.namespace.values() |
paul@167 | 78 | |
paul@167 | 79 | def items(self): |
paul@167 | 80 | return self.namespace.items() |
paul@167 | 81 | |
paul@83 | 82 | def __getitem__(self, name): |
paul@83 | 83 | return self.namespace[name] |
paul@83 | 84 | |
paul@83 | 85 | def get(self, name, default=None): |
paul@83 | 86 | return self.namespace.get(name, default) |
paul@83 | 87 | |
paul@83 | 88 | def __setitem__(self, name, value): |
paul@83 | 89 | self.set(name, value) |
paul@83 | 90 | |
paul@83 | 91 | def set(self, name, value, single_assignment=1): |
paul@83 | 92 | |
paul@83 | 93 | """ |
paul@83 | 94 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@83 | 95 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@83 | 96 | this operation (or whether the operation covers potentially many |
paul@83 | 97 | assignments in the lifetime of a program). |
paul@83 | 98 | """ |
paul@83 | 99 | |
paul@83 | 100 | if name in self.globals: |
paul@180 | 101 | self.module.set(name, value, 0) |
paul@83 | 102 | else: |
paul@83 | 103 | attr = self._set(name, value) |
paul@120 | 104 | attr.update(attr.value, single_assignment) |
paul@83 | 105 | |
paul@83 | 106 | def set_module(self, name, value): |
paul@83 | 107 | |
paul@83 | 108 | """ |
paul@83 | 109 | A specialised set operation, making 'name' refer to 'value' in the |
paul@83 | 110 | context of making a module reference available in association with |
paul@83 | 111 | 'name' as part of the import of that module or a submodule of that |
paul@83 | 112 | module. |
paul@83 | 113 | """ |
paul@83 | 114 | |
paul@83 | 115 | attr = self._set(name, value) |
paul@83 | 116 | if attr.assignments is None: |
paul@83 | 117 | attr.assignments = 1 |
paul@120 | 118 | attr.assignment_values.add(attr.value) |
paul@83 | 119 | |
paul@83 | 120 | def _set(self, name, value): |
paul@83 | 121 | |
paul@187 | 122 | """ |
paul@187 | 123 | The underlying set operation associating 'name' with 'value'. |
paul@187 | 124 | See: docs/assignment.txt |
paul@187 | 125 | """ |
paul@83 | 126 | |
paul@83 | 127 | if not self.namespace.has_key(name): |
paul@87 | 128 | |
paul@120 | 129 | # Either accept the attribute as specified. |
paul@120 | 130 | |
paul@120 | 131 | if isinstance(value, Attr): |
paul@120 | 132 | if value.context is not None: |
paul@120 | 133 | self.namespace[name] = Attr(None, self, value.context, name, value.value) |
paul@120 | 134 | return self.namespace[name] |
paul@187 | 135 | |
paul@187 | 136 | # Or accept the value of the attribute. |
paul@187 | 137 | |
paul@120 | 138 | else: |
paul@120 | 139 | value = value.value |
paul@120 | 140 | |
paul@187 | 141 | # Then/or attempt to fix the context. |
paul@87 | 142 | |
paul@87 | 143 | context = self._context(value) |
paul@87 | 144 | self.namespace[name] = Attr(None, self, context, name, value) |
paul@87 | 145 | |
paul@83 | 146 | return self.namespace[name] |
paul@83 | 147 | |
paul@87 | 148 | def _context(self, value): |
paul@87 | 149 | |
paul@119 | 150 | """ |
paul@119 | 151 | Return the context to be used when storing the given 'value'. |
paul@187 | 152 | See: docs/assignment.txt |
paul@119 | 153 | """ |
paul@87 | 154 | |
paul@187 | 155 | # Set the context of instances to themselves. |
paul@187 | 156 | |
paul@187 | 157 | if isinstance(value, Instance): |
paul@187 | 158 | return value |
paul@187 | 159 | else: |
paul@187 | 160 | return None |
paul@87 | 161 | |
paul@83 | 162 | def make_global(self, name): |
paul@83 | 163 | if not self.namespace.has_key(name): |
paul@83 | 164 | self.globals.add(name) |
paul@178 | 165 | return 1 |
paul@83 | 166 | else: |
paul@178 | 167 | return 0 |
paul@83 | 168 | |
paul@83 | 169 | def get_assignments(self, name): |
paul@83 | 170 | if self.assignments.has_key(name): |
paul@83 | 171 | return max(self.assignments[name], len(self.assignment_values[name])) |
paul@83 | 172 | else: |
paul@83 | 173 | return None |
paul@83 | 174 | |
paul@83 | 175 | def attributes_as_list(self): |
paul@92 | 176 | |
paul@92 | 177 | "Return the attributes in a list." |
paul@92 | 178 | |
paul@83 | 179 | self.finalise_attributes() |
paul@83 | 180 | l = [None] * len(self.keys()) |
paul@83 | 181 | for attr in self.values(): |
paul@83 | 182 | l[attr.position] = attr |
paul@83 | 183 | return l |
paul@83 | 184 | |
paul@83 | 185 | def finalise_attributes(self): |
paul@83 | 186 | |
paul@83 | 187 | "Make sure all attributes are fully defined." |
paul@83 | 188 | |
paul@92 | 189 | if self.finalised: |
paul@92 | 190 | return |
paul@92 | 191 | |
paul@83 | 192 | # The default action is to assign attribute positions sequentially. |
paul@83 | 193 | |
paul@83 | 194 | for i, attr in enumerate(self.values()): |
paul@83 | 195 | attr.position = i |
paul@83 | 196 | |
paul@92 | 197 | self.finalised = 1 |
paul@92 | 198 | |
paul@85 | 199 | # Program data structures. There are two separate kinds of structures: those |
paul@85 | 200 | # with context, which are the values manipulated by programs, and those without |
paul@85 | 201 | # context, which are typically constant things which are stored alongside the |
paul@85 | 202 | # program but which are wrapped in context-dependent structures in the running |
paul@85 | 203 | # program. |
paul@83 | 204 | |
paul@83 | 205 | class Attr: |
paul@83 | 206 | |
paul@87 | 207 | "An attribute entry having a context." |
paul@83 | 208 | |
paul@87 | 209 | def __init__(self, position, parent, context, name, value=None, assignments=None): |
paul@119 | 210 | |
paul@119 | 211 | """ |
paul@119 | 212 | Initialise the attribute with the given 'position' within the collection |
paul@119 | 213 | of attributes of its 'parent', indicating the 'context' or origin of the |
paul@119 | 214 | attribute (where it was first defined), along with its 'name'. |
paul@119 | 215 | |
paul@119 | 216 | An optional 'value' indicates the typical contents of the attribute, and |
paul@119 | 217 | the optional number of 'assignments' may be used to determine whether |
paul@119 | 218 | the attribute is effectively constant. |
paul@119 | 219 | """ |
paul@119 | 220 | |
paul@83 | 221 | self.position = position |
paul@83 | 222 | self.parent = parent |
paul@87 | 223 | self.context = context |
paul@83 | 224 | self.name = name |
paul@83 | 225 | self.value = value |
paul@83 | 226 | |
paul@83 | 227 | # Number of assignments per name. |
paul@83 | 228 | |
paul@83 | 229 | self.assignments = assignments |
paul@83 | 230 | self.assignment_values = set() |
paul@83 | 231 | |
paul@167 | 232 | def set_referenced(self): |
paul@167 | 233 | |
paul@167 | 234 | "Indicate that the contents are referenced via a namespace." |
paul@167 | 235 | |
paul@167 | 236 | for value in self.assignment_values: |
paul@167 | 237 | value.set_referenced() |
paul@167 | 238 | |
paul@83 | 239 | def update(self, value, single_assignment): |
paul@83 | 240 | |
paul@83 | 241 | """ |
paul@83 | 242 | Update the attribute, adding the 'value' provided to the known values |
paul@83 | 243 | associated with the attribute, changing the number of assignments |
paul@83 | 244 | according to the 'single_assignment' status of the operation, where |
paul@83 | 245 | a true value indicates that only one assignment is associated with the |
paul@83 | 246 | update, and a false value indicates that potentially many assignments |
paul@83 | 247 | may be involved. |
paul@83 | 248 | """ |
paul@83 | 249 | |
paul@83 | 250 | if self.assignments is None: |
paul@83 | 251 | if single_assignment: |
paul@83 | 252 | self.assignments = 1 |
paul@83 | 253 | else: |
paul@83 | 254 | self.assignments = AtLeast(1) |
paul@83 | 255 | else: |
paul@83 | 256 | if single_assignment: |
paul@83 | 257 | self.assignments += 1 |
paul@83 | 258 | else: |
paul@83 | 259 | self.assignments += AtLeast(1) |
paul@104 | 260 | |
paul@104 | 261 | if value is not None: |
paul@104 | 262 | self.assignment_values.add(value) |
paul@83 | 263 | |
paul@87 | 264 | def via_instance(self): |
paul@87 | 265 | |
paul@87 | 266 | """ |
paul@87 | 267 | Return either this attribute or a replacement where it is being accessed |
paul@87 | 268 | via an instance. |
paul@87 | 269 | """ |
paul@87 | 270 | |
paul@87 | 271 | if self.context is not None: |
paul@87 | 272 | |
paul@87 | 273 | # Check compatibility of the context with the parent. |
paul@87 | 274 | # Where the attribute originates within the same hierarchy, use an |
paul@87 | 275 | # instance as the context. |
paul@87 | 276 | |
paul@119 | 277 | if self.defined_within_hierarchy(): |
paul@87 | 278 | context = Instance() |
paul@87 | 279 | |
paul@87 | 280 | # Otherwise, preserve the existing context. |
paul@87 | 281 | |
paul@87 | 282 | else: |
paul@87 | 283 | context = self.context |
paul@87 | 284 | |
paul@87 | 285 | return Attr(self.position, self.parent, context, self.name, self.value, self.assignments) |
paul@87 | 286 | |
paul@87 | 287 | # Unknown contexts remain in use. |
paul@87 | 288 | |
paul@87 | 289 | else: |
paul@87 | 290 | return self |
paul@87 | 291 | |
paul@127 | 292 | def is_class_attribute(self): |
paul@127 | 293 | return isinstance(self.parent, Class) |
paul@127 | 294 | |
paul@119 | 295 | def defined_within_hierarchy(self): |
paul@119 | 296 | |
paul@119 | 297 | """ |
paul@119 | 298 | Return whether the parent and context of the attribute belong to the |
paul@119 | 299 | same class hierarchy. |
paul@119 | 300 | """ |
paul@119 | 301 | |
paul@119 | 302 | return isinstance(self.parent, Class) and isinstance(self.context, Class) and ( |
paul@119 | 303 | self.context is self.parent or |
paul@122 | 304 | self.context.has_subclass(self.parent) or |
paul@122 | 305 | self.parent.has_subclass(self.context)) |
paul@119 | 306 | |
paul@83 | 307 | def __repr__(self): |
paul@87 | 308 | return "Attr(%r, %s, %s, %r, %s, %r)" % ( |
paul@87 | 309 | self.position, shortrepr(self.parent), shortrepr(self.context), |
paul@87 | 310 | self.name, shortrepr(self.value), self.assignments |
paul@87 | 311 | ) |
paul@83 | 312 | |
paul@189 | 313 | def as_raw(self, objtable): |
paul@189 | 314 | return [ |
paul@189 | 315 | ( |
paul@189 | 316 | self.context and self.context.location, |
paul@189 | 317 | self.value and self.value.location |
paul@189 | 318 | ) |
paul@189 | 319 | ] |
paul@189 | 320 | |
paul@86 | 321 | # Instances are special in that they need to be wrapped together with context in |
paul@86 | 322 | # a running program, but they are not generally constant. |
paul@86 | 323 | |
paul@86 | 324 | class Instance: |
paul@86 | 325 | |
paul@86 | 326 | "A placeholder indicating the involvement of an instance." |
paul@86 | 327 | |
paul@87 | 328 | def __init__(self): |
paul@87 | 329 | self.parent = None |
paul@167 | 330 | self.referenced = 0 |
paul@87 | 331 | |
paul@119 | 332 | # Image generation details. |
paul@119 | 333 | |
paul@119 | 334 | self.location = None |
paul@119 | 335 | |
paul@167 | 336 | def set_referenced(self): |
paul@167 | 337 | self.referenced = 1 |
paul@167 | 338 | |
paul@86 | 339 | def __repr__(self): |
paul@86 | 340 | return "Instance()" |
paul@86 | 341 | |
paul@87 | 342 | __shortrepr__ = __repr__ |
paul@87 | 343 | |
paul@85 | 344 | class Constant: |
paul@85 | 345 | |
paul@85 | 346 | "A superclass for all constant or context-free structures." |
paul@85 | 347 | |
paul@85 | 348 | pass |
paul@85 | 349 | |
paul@167 | 350 | # Data objects appearing in programs before run-time. |
paul@167 | 351 | |
paul@86 | 352 | class Const(Constant, Instance): |
paul@83 | 353 | |
paul@83 | 354 | "A constant object with no context." |
paul@83 | 355 | |
paul@83 | 356 | def __init__(self, value): |
paul@167 | 357 | Instance.__init__(self) |
paul@83 | 358 | self.value = value |
paul@83 | 359 | |
paul@83 | 360 | def __repr__(self): |
paul@83 | 361 | if self.location is not None: |
paul@83 | 362 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@83 | 363 | else: |
paul@83 | 364 | return "Const(%r)" % self.value |
paul@83 | 365 | |
paul@87 | 366 | __shortrepr__ = __repr__ |
paul@87 | 367 | |
paul@189 | 368 | def as_raw(self, objtable): |
paul@189 | 369 | # NOTE: Need class details! |
paul@189 | 370 | return [ |
paul@189 | 371 | DataObject( |
paul@189 | 372 | objtable.as_list().get_code(self.value_type_name()), |
paul@189 | 373 | objtable.get_index(self.value_type_name()), |
paul@189 | 374 | None, |
paul@189 | 375 | None, |
paul@189 | 376 | 1, |
paul@189 | 377 | self.value_type_name() |
paul@189 | 378 | ) |
paul@189 | 379 | ] + self.raw_data() |
paul@189 | 380 | |
paul@189 | 381 | def raw_data(self): |
paul@189 | 382 | # NOTE: Start simple and use single entries for most types. |
paul@189 | 383 | if self.value_type_name() in ("__builtins__.tuple", "__builtins__.list"): |
paul@189 | 384 | return [len(self.value)] + list(self.value) |
paul@189 | 385 | else: |
paul@189 | 386 | return [self.value] |
paul@189 | 387 | |
paul@83 | 388 | # Support constants as dictionary keys in order to build constant tables. |
paul@83 | 389 | |
paul@83 | 390 | def __eq__(self, other): |
paul@176 | 391 | return self.value == other.value and self.value.__class__ is other.value.__class__ |
paul@83 | 392 | |
paul@83 | 393 | def __hash__(self): |
paul@83 | 394 | return hash(self.value) |
paul@83 | 395 | |
paul@83 | 396 | def value_type_name(self): |
paul@83 | 397 | return "__builtins__." + self.value.__class__.__name__ |
paul@83 | 398 | |
paul@85 | 399 | class Class(NamespaceDict, Naming, Constant): |
paul@83 | 400 | |
paul@83 | 401 | "An inspected class." |
paul@83 | 402 | |
paul@180 | 403 | def __init__(self, name, parent, module=None, node=None): |
paul@83 | 404 | |
paul@83 | 405 | """ |
paul@83 | 406 | Initialise the class with the given 'name', 'parent' object, optional |
paul@180 | 407 | 'module' and optional AST 'node'. |
paul@83 | 408 | """ |
paul@83 | 409 | |
paul@180 | 410 | NamespaceDict.__init__(self, module) |
paul@83 | 411 | self.name = name |
paul@83 | 412 | self.parent = parent |
paul@91 | 413 | self.astnode = node |
paul@167 | 414 | self.referenced = 0 |
paul@83 | 415 | |
paul@83 | 416 | # Superclasses, descendants and attributes. |
paul@83 | 417 | |
paul@83 | 418 | self.bases = [] |
paul@83 | 419 | self.descendants = set() |
paul@83 | 420 | self.instattr = set() # instance attributes |
paul@83 | 421 | self.relocated = set() # attributes which do not have the same |
paul@83 | 422 | # position as those of the same name in |
paul@83 | 423 | # some superclasses |
paul@83 | 424 | |
paul@83 | 425 | # Caches. |
paul@83 | 426 | |
paul@83 | 427 | self.all_instattr = None # cache for instance_attributes |
paul@83 | 428 | self.all_instattr_names = None # from all_instattr |
paul@83 | 429 | self.all_classattr = None # cache for all_class_attributes |
paul@83 | 430 | self.all_classattr_names = None # from all_classattr |
paul@83 | 431 | self.allattr = None # cache for all_attributes |
paul@83 | 432 | self.allattr_names = None # from allattr |
paul@83 | 433 | |
paul@83 | 434 | # Add this class to its attributes. |
paul@83 | 435 | |
paul@83 | 436 | self.set("__class__", self) |
paul@83 | 437 | |
paul@83 | 438 | # Image generation details. |
paul@83 | 439 | |
paul@83 | 440 | self.location = None |
paul@83 | 441 | self.code_location = None |
paul@83 | 442 | self.instantiator = None |
paul@184 | 443 | self.instance_template_location = None # for creating instances at run-time |
paul@83 | 444 | |
paul@83 | 445 | # Program-related details. |
paul@83 | 446 | |
paul@173 | 447 | self.blocks = None |
paul@116 | 448 | self.temp_usage = 0 |
paul@116 | 449 | self.local_usage = 0 |
paul@133 | 450 | self.all_local_usage = 0 |
paul@83 | 451 | |
paul@167 | 452 | def set_referenced(self): |
paul@167 | 453 | self.referenced = 1 |
paul@167 | 454 | |
paul@83 | 455 | def __repr__(self): |
paul@83 | 456 | if self.location is not None: |
paul@87 | 457 | return "Class(%r, %s, location=%r)" % (self.name, shortrepr(self.parent), self.location) |
paul@83 | 458 | else: |
paul@87 | 459 | return "Class(%r, %s)" % (self.name, shortrepr(self.parent)) |
paul@87 | 460 | |
paul@87 | 461 | def __shortrepr__(self): |
paul@87 | 462 | return "Class(%r, %s)" % (self.name, shortrepr(self.parent)) |
paul@87 | 463 | |
paul@189 | 464 | def as_raw(self, objtable): |
paul@189 | 465 | classcode = objtable.as_list().get_code(self.full_name()) |
paul@189 | 466 | attrcode = objtable.get_index(self.full_name()) |
paul@189 | 467 | |
paul@189 | 468 | # Append a template of an instance for use when instantiating classes. |
paul@189 | 469 | |
paul@189 | 470 | call_method = self.get("__call__") |
paul@189 | 471 | call_method_code_location = call_method and call_method.value.code_location |
paul@189 | 472 | |
paul@189 | 473 | # NOTE: The instantiator code is the first block of the class. |
paul@189 | 474 | |
paul@189 | 475 | instantiator_code_location = self.get_instantiator().blocks[0].location |
paul@189 | 476 | |
paul@189 | 477 | return [ |
paul@189 | 478 | DataObject( |
paul@189 | 479 | classcode, attrcode, call_method_code_location, |
paul@189 | 480 | ( |
paul@189 | 481 | call_method and len(call_method.value.positional_names), |
paul@189 | 482 | call_method and len(call_method.value.defaults) |
paul@189 | 483 | ), |
paul@189 | 484 | 1, |
paul@189 | 485 | self.full_name() |
paul@189 | 486 | ), |
paul@189 | 487 | DataObject( |
paul@189 | 488 | classcode, attrcode, instantiator_code_location, |
paul@189 | 489 | ( |
paul@189 | 490 | len(self.get_instantiator().positional_names), |
paul@189 | 491 | len(self.get_instantiator().defaults) |
paul@189 | 492 | ), |
paul@189 | 493 | 0, |
paul@189 | 494 | self.full_name() |
paul@189 | 495 | ) |
paul@189 | 496 | ] |
paul@189 | 497 | |
paul@189 | 498 | # Namespace-related methods. |
paul@189 | 499 | |
paul@87 | 500 | def _context(self, value): |
paul@87 | 501 | |
paul@119 | 502 | """ |
paul@119 | 503 | Return the context to be used when storing the given 'value'. |
paul@187 | 504 | See: docs/assignment.txt |
paul@119 | 505 | """ |
paul@87 | 506 | |
paul@87 | 507 | if value is not None: |
paul@187 | 508 | |
paul@187 | 509 | # Change the ownership of functions. |
paul@187 | 510 | |
paul@187 | 511 | if isinstance(value, Function): |
paul@187 | 512 | context = value.parent |
paul@187 | 513 | |
paul@187 | 514 | if isinstance(context, Module): |
paul@187 | 515 | return self |
paul@187 | 516 | else: |
paul@187 | 517 | return context |
paul@187 | 518 | |
paul@187 | 519 | return NamespaceDict._context(self, value) |
paul@83 | 520 | |
paul@83 | 521 | def finalise_attributes(self): |
paul@83 | 522 | |
paul@83 | 523 | "Make sure that all attributes are fully defined." |
paul@83 | 524 | |
paul@92 | 525 | if self.finalised: |
paul@92 | 526 | return |
paul@92 | 527 | |
paul@83 | 528 | self.finalise_class_attributes() |
paul@83 | 529 | self.finalise_instance_attributes() |
paul@92 | 530 | self.finalised = 1 |
paul@83 | 531 | |
paul@184 | 532 | # Convenience methods for accessing functions and methods. |
paul@184 | 533 | |
paul@83 | 534 | def get_instantiator(self): |
paul@83 | 535 | |
paul@83 | 536 | "Return a function which can be used to instantiate the class." |
paul@83 | 537 | |
paul@83 | 538 | if self.instantiator is None: |
paul@137 | 539 | self.instantiator = self.get_init_method().function_from_method() |
paul@83 | 540 | return self.instantiator |
paul@83 | 541 | |
paul@137 | 542 | def get_init_method(self): |
paul@137 | 543 | return self.all_class_attributes()["__init__"].value |
paul@137 | 544 | |
paul@83 | 545 | # Class-specific methods. |
paul@83 | 546 | |
paul@83 | 547 | def add_base(self, base): |
paul@83 | 548 | self.bases.append(base) |
paul@83 | 549 | base.add_descendant(self) |
paul@83 | 550 | |
paul@83 | 551 | def add_instance_attribute(self, name): |
paul@83 | 552 | self.instattr.add(name) |
paul@83 | 553 | |
paul@83 | 554 | def add_descendant(self, cls): |
paul@83 | 555 | self.descendants.add(cls) |
paul@83 | 556 | for base in self.bases: |
paul@83 | 557 | base.add_descendant(cls) |
paul@83 | 558 | |
paul@122 | 559 | def has_subclass(self, other): |
paul@122 | 560 | return other in self.descendants |
paul@122 | 561 | |
paul@135 | 562 | def all_descendants(self): |
paul@135 | 563 | d = {} |
paul@135 | 564 | for cls in self.descendants: |
paul@135 | 565 | d[cls.full_name()] = cls |
paul@135 | 566 | return d |
paul@135 | 567 | |
paul@83 | 568 | "Return the attribute names provided by this class only." |
paul@83 | 569 | |
paul@83 | 570 | class_attribute_names = NamespaceDict.keys |
paul@83 | 571 | |
paul@83 | 572 | def class_attributes(self): |
paul@83 | 573 | |
paul@83 | 574 | "Return class attributes provided by this class only." |
paul@83 | 575 | |
paul@83 | 576 | return dict(self) |
paul@83 | 577 | |
paul@83 | 578 | def all_class_attribute_names(self): |
paul@83 | 579 | |
paul@83 | 580 | "Return the attribute names provided by classes in this hierarchy." |
paul@83 | 581 | |
paul@83 | 582 | if self.all_classattr_names is None: |
paul@83 | 583 | self.all_class_attributes() |
paul@83 | 584 | return self.all_classattr_names |
paul@83 | 585 | |
paul@83 | 586 | def all_class_attributes(self): |
paul@83 | 587 | |
paul@83 | 588 | "Return all class attributes, indicating the class which provides them." |
paul@83 | 589 | |
paul@83 | 590 | self.finalise_class_attributes() |
paul@83 | 591 | return self.all_classattr |
paul@83 | 592 | |
paul@83 | 593 | def finalise_class_attributes(self): |
paul@83 | 594 | |
paul@83 | 595 | "Make sure that the class attributes are fully defined." |
paul@83 | 596 | |
paul@83 | 597 | if self.all_classattr is None: |
paul@83 | 598 | self.all_classattr = {} |
paul@83 | 599 | clsattr = {} |
paul@83 | 600 | |
paul@83 | 601 | # Record provisional position information for attributes of this |
paul@83 | 602 | # class. |
paul@83 | 603 | |
paul@83 | 604 | for name in self.class_attributes().keys(): |
paul@83 | 605 | clsattr[name] = set() # position not yet defined |
paul@83 | 606 | |
paul@83 | 607 | reversed_bases = self.bases[:] |
paul@83 | 608 | reversed_bases.reverse() |
paul@83 | 609 | |
paul@83 | 610 | # For the bases in reverse order, acquire class attribute details. |
paul@83 | 611 | |
paul@83 | 612 | for cls in reversed_bases: |
paul@83 | 613 | for name, attr in cls.all_class_attributes().items(): |
paul@83 | 614 | self.all_classattr[name] = attr |
paul@83 | 615 | |
paul@83 | 616 | # Record previous attribute information. |
paul@83 | 617 | |
paul@83 | 618 | if clsattr.has_key(name): |
paul@83 | 619 | clsattr[name].add(attr.position) |
paul@83 | 620 | |
paul@83 | 621 | # Record class attributes provided by this class and its bases, |
paul@83 | 622 | # along with their positions. |
paul@83 | 623 | |
paul@83 | 624 | self.all_classattr.update(self.class_attributes()) |
paul@83 | 625 | |
paul@83 | 626 | if clsattr: |
paul@83 | 627 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@83 | 628 | self.all_classattr[name].position = i |
paul@83 | 629 | |
paul@83 | 630 | return self.all_classattr |
paul@83 | 631 | |
paul@83 | 632 | def instance_attribute_names(self): |
paul@83 | 633 | |
paul@83 | 634 | "Return the instance attribute names provided by the class." |
paul@83 | 635 | |
paul@83 | 636 | if self.all_instattr_names is None: |
paul@83 | 637 | self.instance_attributes() |
paul@83 | 638 | return self.all_instattr_names |
paul@83 | 639 | |
paul@83 | 640 | def instance_attributes(self): |
paul@83 | 641 | |
paul@83 | 642 | "Return instance-only attributes for instances of this class." |
paul@83 | 643 | |
paul@83 | 644 | self.finalise_instance_attributes() |
paul@83 | 645 | return self.all_instattr |
paul@83 | 646 | |
paul@83 | 647 | def finalise_instance_attributes(self): |
paul@83 | 648 | |
paul@83 | 649 | "Make sure that the instance attributes are fully defined." |
paul@83 | 650 | |
paul@83 | 651 | if self.all_instattr is None: |
paul@83 | 652 | self.all_instattr = {} |
paul@83 | 653 | instattr = {} |
paul@83 | 654 | |
paul@83 | 655 | # Record provisional position information for attributes of this |
paul@83 | 656 | # instance. |
paul@83 | 657 | |
paul@83 | 658 | for name in self.instattr: |
paul@83 | 659 | instattr[name] = set() # position not yet defined |
paul@83 | 660 | |
paul@83 | 661 | reversed_bases = self.bases[:] |
paul@83 | 662 | reversed_bases.reverse() |
paul@83 | 663 | |
paul@83 | 664 | # For the bases in reverse order, acquire instance attribute |
paul@83 | 665 | # details. |
paul@83 | 666 | |
paul@83 | 667 | for cls in reversed_bases: |
paul@83 | 668 | for name, attr in cls.instance_attributes().items(): |
paul@83 | 669 | |
paul@83 | 670 | # Record previous attribute information. |
paul@83 | 671 | |
paul@83 | 672 | if instattr.has_key(name): |
paul@83 | 673 | instattr[name].add(attr.position) |
paul@83 | 674 | |
paul@83 | 675 | # Cache the attributes by converting the positioned attributes into |
paul@83 | 676 | # a dictionary. |
paul@83 | 677 | |
paul@83 | 678 | if not instattr: |
paul@83 | 679 | self.all_instattr = {} |
paul@83 | 680 | else: |
paul@83 | 681 | self.all_instattr = self._get_attributes(instattr) |
paul@83 | 682 | |
paul@83 | 683 | self.all_instattr_names = self.all_instattr.keys() |
paul@83 | 684 | |
paul@83 | 685 | return self.all_instattr |
paul@83 | 686 | |
paul@83 | 687 | def _get_position_list(self, positions): |
paul@83 | 688 | |
paul@83 | 689 | """ |
paul@83 | 690 | Return a list of attribute names for the given 'positions' mapping from |
paul@83 | 691 | names to positions, indicating the positions of the attributes in the |
paul@83 | 692 | final instance structure. |
paul@83 | 693 | """ |
paul@83 | 694 | |
paul@83 | 695 | position_items = positions.items() |
paul@83 | 696 | namearray = [None] * len(position_items) |
paul@83 | 697 | |
paul@83 | 698 | # Get the positions in ascending order of list size, with lists |
paul@83 | 699 | # of the same size ordered according to their smallest position |
paul@83 | 700 | # value. |
paul@83 | 701 | |
paul@83 | 702 | position_items.sort(self._cmp_positions) |
paul@83 | 703 | |
paul@83 | 704 | # Get the names in position order. |
paul@83 | 705 | |
paul@83 | 706 | held = [] |
paul@83 | 707 | |
paul@83 | 708 | for name, pos in position_items: |
paul@83 | 709 | pos = list(pos) |
paul@83 | 710 | pos.sort() |
paul@83 | 711 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@83 | 712 | namearray[pos[0]] = name |
paul@83 | 713 | else: |
paul@83 | 714 | if pos: |
paul@83 | 715 | self.relocated.add(name) |
paul@83 | 716 | held.append((name, pos)) |
paul@83 | 717 | |
paul@83 | 718 | for i, attr in enumerate(namearray): |
paul@83 | 719 | if attr is None: |
paul@83 | 720 | name, pos = held.pop() |
paul@83 | 721 | namearray[i] = name |
paul@83 | 722 | |
paul@83 | 723 | #print self.name, positions |
paul@83 | 724 | #print "->", namearray |
paul@83 | 725 | return namearray |
paul@83 | 726 | |
paul@83 | 727 | def _get_attributes(self, positions): |
paul@83 | 728 | |
paul@83 | 729 | """ |
paul@83 | 730 | For the given 'positions' mapping from names to positions, return a |
paul@83 | 731 | dictionary mapping names to Attr instances incorporating information |
paul@83 | 732 | about their positions in the final instance structure. |
paul@83 | 733 | """ |
paul@83 | 734 | |
paul@83 | 735 | d = {} |
paul@83 | 736 | for i, name in enumerate(self._get_position_list(positions)): |
paul@87 | 737 | d[name] = Attr(i, Instance(), None, name, None) |
paul@83 | 738 | return d |
paul@83 | 739 | |
paul@83 | 740 | def _cmp_positions(self, a, b): |
paul@83 | 741 | |
paul@83 | 742 | "Compare name plus position list operands 'a' and 'b'." |
paul@83 | 743 | |
paul@83 | 744 | name_a, list_a = a |
paul@83 | 745 | name_b, list_b = b |
paul@83 | 746 | if len(list_a) < len(list_b): |
paul@83 | 747 | return -1 |
paul@83 | 748 | elif len(list_a) > len(list_b): |
paul@83 | 749 | return 1 |
paul@83 | 750 | elif not list_a: |
paul@83 | 751 | return 0 |
paul@83 | 752 | else: |
paul@83 | 753 | return cmp(min(list_a), min(list_b)) |
paul@83 | 754 | |
paul@83 | 755 | def all_attribute_names(self): |
paul@83 | 756 | |
paul@83 | 757 | """ |
paul@83 | 758 | Return the names of all attributes provided by instances of this class. |
paul@83 | 759 | """ |
paul@83 | 760 | |
paul@83 | 761 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@83 | 762 | return self.allattr_names |
paul@83 | 763 | |
paul@83 | 764 | def all_attributes(self): |
paul@83 | 765 | |
paul@83 | 766 | """ |
paul@83 | 767 | Return all attributes for an instance, indicating either the class which |
paul@83 | 768 | provides them or that the instance itself provides them. |
paul@83 | 769 | """ |
paul@83 | 770 | |
paul@83 | 771 | if self.allattr is None: |
paul@83 | 772 | self.allattr = {} |
paul@83 | 773 | self.allattr.update(self.all_class_attributes()) |
paul@83 | 774 | for name, attr in self.instance_attributes().items(): |
paul@83 | 775 | if self.allattr.has_key(name): |
paul@83 | 776 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@83 | 777 | self.allattr[name] = attr |
paul@83 | 778 | return self.allattr |
paul@83 | 779 | |
paul@85 | 780 | class Function(NamespaceDict, Naming, Constant): |
paul@83 | 781 | |
paul@83 | 782 | "An inspected function." |
paul@83 | 783 | |
paul@180 | 784 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, module=None, node=None): |
paul@83 | 785 | |
paul@83 | 786 | """ |
paul@83 | 787 | Initialise the function with the given 'name', 'parent', list of |
paul@83 | 788 | 'argnames', list of 'defaults', the 'has_star' flag (indicating the |
paul@83 | 789 | presence of a * parameter), the 'has_dstar' flag (indicating the |
paul@180 | 790 | presence of a ** parameter), optional 'module', and optional AST 'node'. |
paul@83 | 791 | """ |
paul@83 | 792 | |
paul@180 | 793 | NamespaceDict.__init__(self, module) |
paul@83 | 794 | self.name = name |
paul@83 | 795 | self.parent = parent |
paul@83 | 796 | self.argnames = argnames |
paul@83 | 797 | self.defaults = defaults |
paul@83 | 798 | self.has_star = has_star |
paul@83 | 799 | self.has_dstar = has_dstar |
paul@91 | 800 | self.astnode = node |
paul@167 | 801 | self.referenced = 0 |
paul@83 | 802 | |
paul@83 | 803 | # Initialise the positional names. |
paul@83 | 804 | |
paul@83 | 805 | self.positional_names = self.argnames[:] |
paul@83 | 806 | if has_dstar: |
paul@83 | 807 | self.dstar_name = self.positional_names[-1] |
paul@83 | 808 | del self.positional_names[-1] |
paul@83 | 809 | if has_star: |
paul@83 | 810 | self.star_name = self.positional_names[-1] |
paul@83 | 811 | del self.positional_names[-1] |
paul@83 | 812 | |
paul@83 | 813 | # Initialise default storage. |
paul@83 | 814 | # NOTE: This must be initialised separately due to the reliance on node |
paul@83 | 815 | # NOTE: visiting. |
paul@83 | 816 | |
paul@83 | 817 | self.default_attrs = [] |
paul@83 | 818 | |
paul@83 | 819 | # Caches. |
paul@83 | 820 | |
paul@83 | 821 | self.localnames = None # cache for locals |
paul@83 | 822 | |
paul@83 | 823 | # Add parameters to the namespace. |
paul@83 | 824 | |
paul@83 | 825 | self._add_parameters(argnames) |
paul@83 | 826 | |
paul@83 | 827 | # Image generation details. |
paul@83 | 828 | |
paul@83 | 829 | self.location = None |
paul@83 | 830 | self.code_location = None |
paul@83 | 831 | |
paul@83 | 832 | # Program-related details. |
paul@83 | 833 | |
paul@173 | 834 | self.blocks = None |
paul@116 | 835 | self.temp_usage = 0 |
paul@116 | 836 | self.local_usage = 0 |
paul@133 | 837 | self.all_local_usage = 0 |
paul@83 | 838 | |
paul@167 | 839 | def set_referenced(self): |
paul@167 | 840 | self.referenced = 1 |
paul@167 | 841 | |
paul@83 | 842 | def _add_parameters(self, argnames): |
paul@83 | 843 | for name in argnames: |
paul@83 | 844 | if isinstance(name, tuple): |
paul@83 | 845 | self._add_parameters(name) |
paul@83 | 846 | else: |
paul@83 | 847 | self.set(name, None) |
paul@83 | 848 | |
paul@83 | 849 | def __repr__(self): |
paul@83 | 850 | if self.location is not None: |
paul@98 | 851 | return "Function(%r, %s, %r, location=%r, code_location=%r)" % ( |
paul@98 | 852 | self.name, shortrepr(self.parent), self.argnames, self.location, self.code_location |
paul@83 | 853 | ) |
paul@83 | 854 | else: |
paul@98 | 855 | return "Function(%r, %s, %r)" % ( |
paul@98 | 856 | self.name, shortrepr(self.parent), self.argnames |
paul@83 | 857 | ) |
paul@83 | 858 | |
paul@87 | 859 | def __shortrepr__(self): |
paul@87 | 860 | return "Function(%r, %s)" % ( |
paul@87 | 861 | self.name, shortrepr(self.parent) |
paul@87 | 862 | ) |
paul@87 | 863 | |
paul@189 | 864 | def as_raw(self, objtable): |
paul@189 | 865 | # NOTE: Need class and parameter details! Should arguably be types.FunctionType. |
paul@189 | 866 | return [ |
paul@189 | 867 | DataObject( |
paul@189 | 868 | objtable.as_list().get_code("__builtins__.function"), |
paul@189 | 869 | objtable.get_index("__builtins__.function"), |
paul@189 | 870 | self.code_location, |
paul@189 | 871 | ( |
paul@189 | 872 | len(self.positional_names), |
paul@189 | 873 | len(self.defaults) |
paul@189 | 874 | ), |
paul@189 | 875 | 0, |
paul@189 | 876 | "__builtins__.function" |
paul@189 | 877 | ) |
paul@189 | 878 | ] |
paul@189 | 879 | |
paul@189 | 880 | # Namespace-related methods. |
paul@189 | 881 | |
paul@83 | 882 | def store_default(self, value): |
paul@87 | 883 | attr = Attr(None, self, None, None, value) |
paul@83 | 884 | attr.update(value, 1) |
paul@83 | 885 | self.default_attrs.append(attr) |
paul@83 | 886 | |
paul@83 | 887 | def make_global(self, name): |
paul@83 | 888 | if name not in self.argnames and not self.has_key(name): |
paul@83 | 889 | self.globals.add(name) |
paul@178 | 890 | return 1 |
paul@83 | 891 | else: |
paul@178 | 892 | return 0 |
paul@83 | 893 | |
paul@83 | 894 | def parameters(self): |
paul@83 | 895 | |
paul@83 | 896 | """ |
paul@83 | 897 | Return a dictionary mapping parameter names to their position in the |
paul@83 | 898 | parameter list. |
paul@83 | 899 | """ |
paul@83 | 900 | |
paul@83 | 901 | parameters = {} |
paul@83 | 902 | for i, name in enumerate(self.argnames): |
paul@83 | 903 | parameters[name] = i |
paul@83 | 904 | return parameters |
paul@83 | 905 | |
paul@83 | 906 | def all_locals(self): |
paul@83 | 907 | |
paul@83 | 908 | "Return a dictionary mapping names to local and parameter details." |
paul@83 | 909 | |
paul@83 | 910 | return dict(self) |
paul@83 | 911 | |
paul@83 | 912 | def locals(self): |
paul@83 | 913 | |
paul@83 | 914 | "Return a dictionary mapping names to local details." |
paul@83 | 915 | |
paul@83 | 916 | if self.localnames is None: |
paul@83 | 917 | self.localnames = {} |
paul@83 | 918 | self.localnames.update(self.all_locals()) |
paul@83 | 919 | for name in self.argnames: |
paul@83 | 920 | del self.localnames[name] |
paul@83 | 921 | return self.localnames |
paul@83 | 922 | |
paul@83 | 923 | def is_method(self): |
paul@83 | 924 | |
paul@83 | 925 | "Return whether this function is a method." |
paul@83 | 926 | |
paul@83 | 927 | return isinstance(self.parent, Class) |
paul@83 | 928 | |
paul@83 | 929 | def is_relocated(self, name): |
paul@83 | 930 | |
paul@83 | 931 | """ |
paul@83 | 932 | Determine whether the given attribute 'name' is relocated for instances |
paul@83 | 933 | having this function as a method. |
paul@83 | 934 | """ |
paul@83 | 935 | |
paul@83 | 936 | for cls in self.parent.descendants: |
paul@83 | 937 | if name in cls.relocated: |
paul@83 | 938 | return 1 |
paul@83 | 939 | return 0 |
paul@83 | 940 | |
paul@83 | 941 | def finalise_attributes(self): |
paul@83 | 942 | |
paul@83 | 943 | """ |
paul@83 | 944 | Make sure all attributes (locals) are fully defined. Note that locals |
paul@83 | 945 | are not attributes in the sense of class, module or instance attributes. |
paul@83 | 946 | Defaults are also finalised by this method. |
paul@83 | 947 | """ |
paul@83 | 948 | |
paul@83 | 949 | for i, default in enumerate(self.default_attrs): |
paul@83 | 950 | default.position = i |
paul@83 | 951 | |
paul@83 | 952 | i = None |
paul@83 | 953 | for i, name in enumerate(self.argnames): |
paul@83 | 954 | self[name].position = i |
paul@83 | 955 | |
paul@83 | 956 | if i is not None: |
paul@144 | 957 | nparams = i + 1 |
paul@83 | 958 | else: |
paul@144 | 959 | nparams = 0 |
paul@144 | 960 | |
paul@144 | 961 | i = None |
paul@144 | 962 | for i, attr in enumerate(self.locals().values()): |
paul@144 | 963 | attr.position = i + nparams |
paul@83 | 964 | |
paul@144 | 965 | if i is not None: |
paul@144 | 966 | nothers = i + 1 |
paul@144 | 967 | else: |
paul@144 | 968 | nothers = 0 |
paul@83 | 969 | |
paul@144 | 970 | self.local_usage = nothers |
paul@144 | 971 | self.all_local_usage = nparams + nothers |
paul@92 | 972 | self.finalised = 1 |
paul@83 | 973 | |
paul@83 | 974 | def function_from_method(self): |
paul@83 | 975 | |
paul@83 | 976 | "Make a function from a method." |
paul@83 | 977 | |
paul@83 | 978 | function = Function(self.name, self.parent, self.argnames[1:], self.defaults, |
paul@180 | 979 | self.has_star, self.has_dstar, self.module, self.astnode) |
paul@83 | 980 | function.default_attrs = self.default_attrs |
paul@83 | 981 | return function |
paul@83 | 982 | |
paul@85 | 983 | class UnresolvedName(NamespaceDict, Constant): |
paul@83 | 984 | |
paul@83 | 985 | "A module, class or function which was mentioned but could not be imported." |
paul@83 | 986 | |
paul@180 | 987 | def __init__(self, name, parent_name, module=None): |
paul@180 | 988 | NamespaceDict.__init__(self, module) |
paul@83 | 989 | self.name = name |
paul@83 | 990 | self.parent_name = parent_name |
paul@88 | 991 | self.parent = None |
paul@167 | 992 | self.referenced = 0 |
paul@167 | 993 | |
paul@167 | 994 | self.descendants = set() |
paul@167 | 995 | |
paul@167 | 996 | def set_referenced(self): |
paul@167 | 997 | self.referenced = 1 |
paul@167 | 998 | |
paul@167 | 999 | def add_descendant(self, cls): |
paul@167 | 1000 | self.descendants.add(cls) |
paul@83 | 1001 | |
paul@83 | 1002 | def all_class_attributes(self): |
paul@83 | 1003 | return {} |
paul@83 | 1004 | |
paul@83 | 1005 | def instance_attributes(self): |
paul@83 | 1006 | return {} |
paul@83 | 1007 | |
paul@83 | 1008 | def __repr__(self): |
paul@83 | 1009 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@83 | 1010 | |
paul@87 | 1011 | __shortrepr__ = __repr__ |
paul@87 | 1012 | |
paul@83 | 1013 | def full_name(self): |
paul@83 | 1014 | if self.name is not None: |
paul@83 | 1015 | return self.parent_name + "." + self.name |
paul@83 | 1016 | else: |
paul@83 | 1017 | return self.parent_name |
paul@83 | 1018 | |
paul@85 | 1019 | class Module(NamespaceDict, Constant): |
paul@83 | 1020 | |
paul@83 | 1021 | "An inspected module's core details." |
paul@83 | 1022 | |
paul@83 | 1023 | def __init__(self, name): |
paul@83 | 1024 | NamespaceDict.__init__(self, self) |
paul@83 | 1025 | self.name = name |
paul@88 | 1026 | self.parent = None |
paul@167 | 1027 | self.referenced = 0 |
paul@83 | 1028 | |
paul@83 | 1029 | # Original location details. |
paul@83 | 1030 | |
paul@91 | 1031 | self.astnode = None |
paul@83 | 1032 | |
paul@83 | 1033 | # Complete lists of classes and functions. |
paul@83 | 1034 | |
paul@83 | 1035 | self.all_objects = set() |
paul@83 | 1036 | |
paul@83 | 1037 | # Keyword records. |
paul@83 | 1038 | |
paul@83 | 1039 | self.keyword_names = set() |
paul@83 | 1040 | |
paul@83 | 1041 | # Image generation details. |
paul@83 | 1042 | |
paul@83 | 1043 | self.location = None |
paul@83 | 1044 | self.code_location = None |
paul@83 | 1045 | |
paul@83 | 1046 | # Program-related details. |
paul@83 | 1047 | |
paul@173 | 1048 | self.blocks = None |
paul@116 | 1049 | self.temp_usage = 0 |
paul@116 | 1050 | self.local_usage = 0 |
paul@133 | 1051 | self.all_local_usage = 0 |
paul@83 | 1052 | |
paul@167 | 1053 | def set_referenced(self): |
paul@167 | 1054 | self.referenced = 1 |
paul@167 | 1055 | |
paul@83 | 1056 | def full_name(self): |
paul@83 | 1057 | return self.name |
paul@83 | 1058 | |
paul@83 | 1059 | def __repr__(self): |
paul@83 | 1060 | if self.location is not None: |
paul@83 | 1061 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@83 | 1062 | else: |
paul@83 | 1063 | return "Module(%r)" % self.name |
paul@83 | 1064 | |
paul@87 | 1065 | def __shortrepr__(self): |
paul@87 | 1066 | return "Module(%r)" % self.name |
paul@87 | 1067 | |
paul@189 | 1068 | def as_raw(self, objtable): |
paul@189 | 1069 | return [ |
paul@189 | 1070 | DataObject( |
paul@189 | 1071 | objtable.as_list().get_code(self.full_name()), |
paul@189 | 1072 | None, # module name not used as an attribute |
paul@189 | 1073 | None, |
paul@189 | 1074 | None, |
paul@189 | 1075 | 0, |
paul@189 | 1076 | self.full_name() |
paul@189 | 1077 | ) |
paul@189 | 1078 | ] |
paul@189 | 1079 | |
paul@83 | 1080 | # Attribute methods. |
paul@83 | 1081 | |
paul@83 | 1082 | "Return the module attribute names provided by the module." |
paul@83 | 1083 | |
paul@83 | 1084 | module_attribute_names = NamespaceDict.keys |
paul@83 | 1085 | |
paul@83 | 1086 | def module_attributes(self): |
paul@83 | 1087 | |
paul@83 | 1088 | "Return a dictionary mapping names to module attributes." |
paul@83 | 1089 | |
paul@83 | 1090 | return dict(self) |
paul@83 | 1091 | |
paul@83 | 1092 | # vim: tabstop=4 expandtab shiftwidth=4 |