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