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