paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Inspect source files, obtaining details of classes and attributes. |
paul@0 | 5 | |
paul@31 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public License along with |
paul@0 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@20 | 20 | |
paul@20 | 21 | -------- |
paul@20 | 22 | |
paul@20 | 23 | The central classes in this module are the following: |
paul@20 | 24 | |
paul@20 | 25 | * Class |
paul@20 | 26 | * Function |
paul@20 | 27 | * Module |
paul@20 | 28 | * InspectedModule (derived from Module) |
paul@20 | 29 | |
paul@20 | 30 | All of the above support the Naming interface either explicitly or through |
paul@20 | 31 | general conformance, meaning that all can be asked to provide their 'full_name' |
paul@20 | 32 | using the method of that name. |
paul@20 | 33 | |
paul@20 | 34 | Additionally, all of the above also support a dictionary interface in order to |
paul@20 | 35 | access names within their defined scopes. Specific methods also exist in order |
paul@20 | 36 | to distinguish between certain kinds of attributes: |
paul@20 | 37 | |
paul@20 | 38 | * Class: (class|all_class|instance|all)_attributes |
paul@31 | 39 | * Function: parameters, locals, all_locals |
paul@20 | 40 | * Module: module_attributes |
paul@20 | 41 | |
paul@20 | 42 | These specific methods are useful in certain situations. |
paul@20 | 43 | |
paul@20 | 44 | The above classes also provide a 'node' attribute, indicating the AST node where |
paul@20 | 45 | each such object is defined. |
paul@0 | 46 | """ |
paul@0 | 47 | |
paul@39 | 48 | from micropython.common import * |
paul@0 | 49 | import compiler.ast |
paul@0 | 50 | from compiler.visitor import ASTVisitor |
paul@0 | 51 | try: |
paul@0 | 52 | set |
paul@0 | 53 | except NameError: |
paul@0 | 54 | from sets import Set as set |
paul@0 | 55 | |
paul@39 | 56 | class InspectError(ProcessingError): pass |
paul@0 | 57 | |
paul@31 | 58 | class AtLeast: |
paul@31 | 59 | |
paul@31 | 60 | "A special representation for numbers of a given value or greater." |
paul@31 | 61 | |
paul@31 | 62 | def __init__(self, count): |
paul@31 | 63 | self.count = count |
paul@31 | 64 | |
paul@31 | 65 | def __eq__(self, other): |
paul@31 | 66 | return 0 |
paul@31 | 67 | |
paul@31 | 68 | __lt__ = __le__ = __eq__ |
paul@31 | 69 | |
paul@31 | 70 | def __ne__(self, other): |
paul@31 | 71 | return 1 |
paul@31 | 72 | |
paul@31 | 73 | def __gt__(self, other): |
paul@31 | 74 | if isinstance(other, AtLeast): |
paul@31 | 75 | return 0 |
paul@31 | 76 | else: |
paul@31 | 77 | return self.count > other |
paul@31 | 78 | |
paul@31 | 79 | def __ge__(self, other): |
paul@31 | 80 | if isinstance(other, AtLeast): |
paul@31 | 81 | return 0 |
paul@31 | 82 | else: |
paul@31 | 83 | return self.count >= other |
paul@31 | 84 | |
paul@31 | 85 | def __iadd__(self, other): |
paul@31 | 86 | if isinstance(other, AtLeast): |
paul@31 | 87 | self.count += other.count |
paul@31 | 88 | else: |
paul@31 | 89 | self.count += other |
paul@31 | 90 | return self |
paul@31 | 91 | |
paul@31 | 92 | def __radd__(self, other): |
paul@31 | 93 | if isinstance(other, AtLeast): |
paul@31 | 94 | return AtLeast(self.count + other.count) |
paul@31 | 95 | else: |
paul@31 | 96 | return AtLeast(self.count + other) |
paul@31 | 97 | |
paul@31 | 98 | def __repr__(self): |
paul@31 | 99 | return "AtLeast(%r)" % self.count |
paul@31 | 100 | |
paul@12 | 101 | # Mix-ins and abstract classes. |
paul@12 | 102 | |
paul@3 | 103 | class NamespaceDict: |
paul@3 | 104 | |
paul@3 | 105 | "A mix-in providing dictionary methods." |
paul@3 | 106 | |
paul@24 | 107 | def __init__(self, global_namespace=None): |
paul@12 | 108 | self.namespace = {} |
paul@12 | 109 | self.globals = set() |
paul@24 | 110 | self.global_namespace = global_namespace |
paul@23 | 111 | |
paul@3 | 112 | def __getitem__(self, name): |
paul@3 | 113 | return self.namespace[name] |
paul@3 | 114 | |
paul@15 | 115 | def get(self, name, default=None): |
paul@15 | 116 | return self.namespace.get(name, default) |
paul@15 | 117 | |
paul@12 | 118 | def __setitem__(self, name, value): |
paul@37 | 119 | self.set(name, value) |
paul@31 | 120 | |
paul@34 | 121 | def set(self, name, value, single_assignment=1): |
paul@47 | 122 | |
paul@47 | 123 | """ |
paul@47 | 124 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@47 | 125 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@47 | 126 | this operation (or whether the operation covers potentially many |
paul@47 | 127 | assignments in the lifetime of a program). |
paul@47 | 128 | """ |
paul@47 | 129 | |
paul@37 | 130 | if name in self.globals: |
paul@37 | 131 | self.global_namespace.set(name, value, 0) |
paul@37 | 132 | else: |
paul@37 | 133 | attr = self._set(name, value) |
paul@12 | 134 | |
paul@37 | 135 | # NOTE: Insist on assignments with known values. |
paul@23 | 136 | |
paul@37 | 137 | if value is not None: |
paul@37 | 138 | attr.update(value, single_assignment) |
paul@32 | 139 | |
paul@32 | 140 | def set_module(self, name, value): |
paul@47 | 141 | |
paul@47 | 142 | """ |
paul@47 | 143 | A specialised set operation, making 'name' refer to 'value' in the |
paul@47 | 144 | context of making a module reference available in association with |
paul@47 | 145 | 'name' as part of the import of that module or a submodule of that |
paul@47 | 146 | module. |
paul@47 | 147 | """ |
paul@47 | 148 | |
paul@36 | 149 | attr = self._set(name, value) |
paul@36 | 150 | if attr.assignments is None: |
paul@36 | 151 | attr.assignments = 1 |
paul@36 | 152 | attr.assignment_values.add(value) |
paul@36 | 153 | |
paul@36 | 154 | def _set(self, name, value): |
paul@47 | 155 | |
paul@47 | 156 | "The underlying set operation associating 'name' with 'value'." |
paul@47 | 157 | |
paul@36 | 158 | if not self.namespace.has_key(name): |
paul@49 | 159 | self.namespace[name] = Attr(None, self, name, value) |
paul@36 | 160 | return self.namespace[name] |
paul@23 | 161 | |
paul@12 | 162 | def __delitem__(self, name): |
paul@12 | 163 | del self.namespace[name] |
paul@12 | 164 | |
paul@13 | 165 | def has_key(self, name): |
paul@13 | 166 | return self.namespace.has_key(name) |
paul@13 | 167 | |
paul@3 | 168 | def keys(self): |
paul@3 | 169 | return self.namespace.keys() |
paul@3 | 170 | |
paul@7 | 171 | def values(self): |
paul@7 | 172 | return self.namespace.values() |
paul@7 | 173 | |
paul@7 | 174 | def items(self): |
paul@7 | 175 | return self.namespace.items() |
paul@7 | 176 | |
paul@12 | 177 | def make_global(self, name): |
paul@12 | 178 | if not self.namespace.has_key(name): |
paul@12 | 179 | self.globals.add(name) |
paul@12 | 180 | else: |
paul@43 | 181 | raise InspectError(self.full_name(), self.node, "Name %r is both global and local in %r" % (name, self.full_name())) |
paul@12 | 182 | |
paul@32 | 183 | def get_assignments(self, name): |
paul@32 | 184 | if self.assignments.has_key(name): |
paul@32 | 185 | return max(self.assignments[name], len(self.assignment_values[name])) |
paul@32 | 186 | else: |
paul@32 | 187 | return None |
paul@32 | 188 | |
paul@49 | 189 | def attributes_as_list(self): |
paul@49 | 190 | self.finalise_attributes() |
paul@49 | 191 | l = [None] * len(self.keys()) |
paul@49 | 192 | for attr in self.values(): |
paul@37 | 193 | l[attr.position] = attr |
paul@37 | 194 | return l |
paul@37 | 195 | |
paul@49 | 196 | def finalise_attributes(self): |
paul@49 | 197 | |
paul@49 | 198 | "Make sure all attributes are fully defined." |
paul@49 | 199 | |
paul@49 | 200 | # The default action is to assign attribute positions sequentially. |
paul@49 | 201 | |
paul@49 | 202 | for i, attr in enumerate(self.values()): |
paul@49 | 203 | attr.position = i |
paul@49 | 204 | |
paul@4 | 205 | class Naming: |
paul@4 | 206 | |
paul@4 | 207 | "A mix-in providing naming conveniences." |
paul@4 | 208 | |
paul@4 | 209 | def full_name(self): |
paul@4 | 210 | if self.name is not None: |
paul@53 | 211 | return self.parent.full_name() + "." + self.name |
paul@4 | 212 | else: |
paul@53 | 213 | return self.parent.full_name() |
paul@4 | 214 | |
paul@12 | 215 | # Program data structures. |
paul@12 | 216 | |
paul@10 | 217 | class Attr: |
paul@10 | 218 | |
paul@54 | 219 | "An attribute entry having a parent as context." |
paul@10 | 220 | |
paul@36 | 221 | def __init__(self, position, parent, name, value=None, assignments=None): |
paul@10 | 222 | self.position = position |
paul@10 | 223 | self.parent = parent |
paul@36 | 224 | self.name = name |
paul@10 | 225 | self.value = value |
paul@10 | 226 | |
paul@36 | 227 | # Number of assignments per name. |
paul@36 | 228 | |
paul@36 | 229 | self.assignments = assignments |
paul@36 | 230 | self.assignment_values = set() |
paul@36 | 231 | |
paul@36 | 232 | def update(self, value, single_assignment): |
paul@47 | 233 | |
paul@47 | 234 | """ |
paul@47 | 235 | Update the attribute, adding the 'value' provided to the known values |
paul@47 | 236 | associated with the attribute, changing the number of assignments |
paul@47 | 237 | according to the 'single_assignment' status of the operation, where |
paul@47 | 238 | a true value indicates that only one assignment is associated with the |
paul@47 | 239 | update, and a false value indicates that potentially many assignments |
paul@47 | 240 | may be involved. |
paul@47 | 241 | """ |
paul@47 | 242 | |
paul@36 | 243 | if self.assignments is None: |
paul@36 | 244 | if single_assignment: |
paul@36 | 245 | self.assignments = 1 |
paul@36 | 246 | else: |
paul@36 | 247 | self.assignments = AtLeast(1) |
paul@36 | 248 | else: |
paul@36 | 249 | if single_assignment: |
paul@36 | 250 | self.assignments += 1 |
paul@36 | 251 | else: |
paul@36 | 252 | self.assignments += AtLeast(1) |
paul@36 | 253 | self.assignment_values.add(value) |
paul@36 | 254 | |
paul@10 | 255 | def __repr__(self): |
paul@36 | 256 | return "Attr(%r, %r, %r, %r, %r)" % (self.position, self.parent, self.name, self.value, self.assignments) |
paul@10 | 257 | |
paul@21 | 258 | class Const: |
paul@21 | 259 | |
paul@55 | 260 | "A constant object with no context." |
paul@21 | 261 | |
paul@21 | 262 | def __init__(self, value): |
paul@21 | 263 | self.value = value |
paul@21 | 264 | |
paul@21 | 265 | # Image generation details. |
paul@21 | 266 | |
paul@21 | 267 | self.location = None |
paul@21 | 268 | |
paul@21 | 269 | def __repr__(self): |
paul@37 | 270 | if self.location is not None: |
paul@37 | 271 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@37 | 272 | else: |
paul@37 | 273 | return "Const(%r)" % self.value |
paul@21 | 274 | |
paul@75 | 275 | # Support constants as dictionary keys in order to build constant tables. |
paul@75 | 276 | |
paul@21 | 277 | def __eq__(self, other): |
paul@21 | 278 | return self.value == other.value |
paul@21 | 279 | |
paul@21 | 280 | def __hash__(self): |
paul@21 | 281 | return hash(self.value) |
paul@21 | 282 | |
paul@75 | 283 | def value_type_name(self): |
paul@75 | 284 | return "__builtins__." + self.value.__class__.__name__ |
paul@75 | 285 | |
paul@4 | 286 | class Class(NamespaceDict, Naming): |
paul@0 | 287 | |
paul@0 | 288 | "An inspected class." |
paul@0 | 289 | |
paul@53 | 290 | def __init__(self, name, parent, global_namespace=None, node=None): |
paul@54 | 291 | |
paul@54 | 292 | """ |
paul@54 | 293 | Initialise the class with the given 'name', 'parent' object, optional |
paul@54 | 294 | 'global_namespace' and optional AST 'node'. |
paul@54 | 295 | """ |
paul@54 | 296 | |
paul@24 | 297 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 298 | self.name = name |
paul@53 | 299 | self.parent = parent |
paul@17 | 300 | self.node = node |
paul@12 | 301 | |
paul@53 | 302 | # Superclasses, descendants and attributes. |
paul@47 | 303 | |
paul@0 | 304 | self.bases = [] |
paul@53 | 305 | self.descendants = set() |
paul@46 | 306 | self.instattr = set() # instance attributes |
paul@49 | 307 | self.relocated = set() # attributes which do not have the same |
paul@49 | 308 | # position as those of the same name in |
paul@49 | 309 | # some superclasses |
paul@12 | 310 | |
paul@47 | 311 | # Caches. |
paul@47 | 312 | |
paul@46 | 313 | self.all_instattr = None # cache for instance_attributes |
paul@46 | 314 | self.all_instattr_names = None # from all_instattr |
paul@46 | 315 | self.all_classattr = None # cache for all_class_attributes |
paul@12 | 316 | self.all_classattr_names = None # from all_classattr |
paul@46 | 317 | self.allattr = None # cache for all_attributes |
paul@46 | 318 | self.allattr_names = None # from allattr |
paul@9 | 319 | |
paul@63 | 320 | # Add this class to its attributes. |
paul@63 | 321 | |
paul@63 | 322 | self.set("__class__", self) |
paul@63 | 323 | |
paul@9 | 324 | # Image generation details. |
paul@9 | 325 | |
paul@9 | 326 | self.location = None |
paul@19 | 327 | self.code_location = None |
paul@61 | 328 | self.instantiator = None |
paul@0 | 329 | |
paul@72 | 330 | # Program-related details. |
paul@72 | 331 | |
paul@73 | 332 | self.stack_usage = 0 |
paul@73 | 333 | self.stack_temp_usage = 0 |
paul@73 | 334 | self.stack_local_usage = 0 |
paul@72 | 335 | |
paul@12 | 336 | def __repr__(self): |
paul@37 | 337 | if self.location is not None: |
paul@53 | 338 | return "Class(%r, %r, location=%r)" % (self.name, self.parent, self.location) |
paul@37 | 339 | else: |
paul@53 | 340 | return "Class(%r, %r)" % (self.name, self.parent) |
paul@12 | 341 | |
paul@49 | 342 | def finalise_attributes(self): |
paul@49 | 343 | |
paul@49 | 344 | "Make sure that all attributes are fully defined." |
paul@49 | 345 | |
paul@49 | 346 | self.finalise_class_attributes() |
paul@49 | 347 | self.finalise_instance_attributes() |
paul@49 | 348 | |
paul@61 | 349 | def get_instantiator(self): |
paul@61 | 350 | |
paul@61 | 351 | "Return a function which can be used to instantiate the class." |
paul@61 | 352 | |
paul@61 | 353 | if self.instantiator is None: |
paul@61 | 354 | init_method = self.all_class_attributes()["__init__"].value |
paul@61 | 355 | self.instantiator = init_method.function_from_method() |
paul@61 | 356 | return self.instantiator |
paul@61 | 357 | |
paul@49 | 358 | # Class-specific methods. |
paul@49 | 359 | |
paul@0 | 360 | def add_base(self, base): |
paul@0 | 361 | self.bases.append(base) |
paul@53 | 362 | base.add_descendant(self) |
paul@0 | 363 | |
paul@12 | 364 | def add_instance_attribute(self, name): |
paul@12 | 365 | self.instattr.add(name) |
paul@9 | 366 | |
paul@53 | 367 | def add_descendant(self, cls): |
paul@53 | 368 | self.descendants.add(cls) |
paul@53 | 369 | for base in self.bases: |
paul@53 | 370 | base.add_descendant(cls) |
paul@53 | 371 | |
paul@36 | 372 | "Return the attribute names provided by this class only." |
paul@9 | 373 | |
paul@36 | 374 | class_attribute_names = NamespaceDict.keys |
paul@4 | 375 | |
paul@4 | 376 | def class_attributes(self): |
paul@4 | 377 | |
paul@12 | 378 | "Return class attributes provided by this class only." |
paul@4 | 379 | |
paul@49 | 380 | self.finalise_class_attributes() |
paul@53 | 381 | return dict(self) |
paul@12 | 382 | |
paul@12 | 383 | def all_class_attribute_names(self): |
paul@12 | 384 | |
paul@12 | 385 | "Return the attribute names provided by classes in this hierarchy." |
paul@12 | 386 | |
paul@12 | 387 | if self.all_classattr_names is None: |
paul@12 | 388 | self.all_class_attributes() |
paul@12 | 389 | return self.all_classattr_names |
paul@12 | 390 | |
paul@12 | 391 | def all_class_attributes(self): |
paul@12 | 392 | |
paul@12 | 393 | "Return all class attributes, indicating the class which provides them." |
paul@12 | 394 | |
paul@49 | 395 | self.finalise_class_attributes() |
paul@49 | 396 | return self.all_classattr |
paul@49 | 397 | |
paul@49 | 398 | def finalise_class_attributes(self): |
paul@49 | 399 | |
paul@49 | 400 | "Make sure that the class attributes are fully defined." |
paul@49 | 401 | |
paul@12 | 402 | if self.all_classattr is None: |
paul@12 | 403 | self.all_classattr = {} |
paul@49 | 404 | clsattr = {} |
paul@49 | 405 | |
paul@49 | 406 | # Record provisional position information for attributes of this |
paul@49 | 407 | # class. |
paul@49 | 408 | |
paul@49 | 409 | for name in self.class_attributes().keys(): |
paul@49 | 410 | clsattr[name] = set() # position not yet defined |
paul@12 | 411 | |
paul@9 | 412 | reversed_bases = self.bases[:] |
paul@9 | 413 | reversed_bases.reverse() |
paul@49 | 414 | |
paul@49 | 415 | # For the bases in reverse order, acquire class attribute details. |
paul@49 | 416 | |
paul@9 | 417 | for cls in reversed_bases: |
paul@49 | 418 | for name, attr in cls.all_class_attributes().items(): |
paul@49 | 419 | self.all_classattr[name] = attr |
paul@10 | 420 | |
paul@49 | 421 | # Record previous attribute information. |
paul@49 | 422 | |
paul@49 | 423 | if clsattr.has_key(name): |
paul@49 | 424 | clsattr[name].add(attr.position) |
paul@49 | 425 | |
paul@49 | 426 | # Record class attributes provided by this class and its bases, |
paul@49 | 427 | # along with their positions. |
paul@10 | 428 | |
paul@12 | 429 | self.all_classattr.update(self.class_attributes()) |
paul@9 | 430 | |
paul@49 | 431 | if clsattr: |
paul@49 | 432 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@49 | 433 | self.all_classattr[name].position = i |
paul@49 | 434 | |
paul@12 | 435 | return self.all_classattr |
paul@4 | 436 | |
paul@7 | 437 | def instance_attribute_names(self): |
paul@7 | 438 | |
paul@7 | 439 | "Return the instance attribute names provided by the class." |
paul@7 | 440 | |
paul@12 | 441 | if self.all_instattr_names is None: |
paul@12 | 442 | self.instance_attributes() |
paul@12 | 443 | return self.all_instattr_names |
paul@7 | 444 | |
paul@4 | 445 | def instance_attributes(self): |
paul@4 | 446 | |
paul@9 | 447 | "Return instance-only attributes for instances of this class." |
paul@9 | 448 | |
paul@49 | 449 | self.finalise_instance_attributes() |
paul@49 | 450 | return self.all_instattr |
paul@49 | 451 | |
paul@49 | 452 | def finalise_instance_attributes(self): |
paul@49 | 453 | |
paul@49 | 454 | "Make sure that the instance attributes are fully defined." |
paul@49 | 455 | |
paul@12 | 456 | if self.all_instattr is None: |
paul@12 | 457 | self.all_instattr = {} |
paul@46 | 458 | instattr = {} |
paul@12 | 459 | |
paul@49 | 460 | # Record provisional position information for attributes of this |
paul@49 | 461 | # instance. |
paul@49 | 462 | |
paul@49 | 463 | for name in self.instattr: |
paul@49 | 464 | instattr[name] = set() # position not yet defined |
paul@49 | 465 | |
paul@12 | 466 | reversed_bases = self.bases[:] |
paul@12 | 467 | reversed_bases.reverse() |
paul@46 | 468 | |
paul@46 | 469 | # For the bases in reverse order, acquire instance attribute |
paul@46 | 470 | # details. |
paul@46 | 471 | |
paul@12 | 472 | for cls in reversed_bases: |
paul@46 | 473 | for name, attr in cls.instance_attributes().items(): |
paul@49 | 474 | |
paul@49 | 475 | # Record previous attribute information. |
paul@12 | 476 | |
paul@49 | 477 | if instattr.has_key(name): |
paul@49 | 478 | instattr[name].add(attr.position) |
paul@46 | 479 | |
paul@48 | 480 | # Cache the attributes by converting the positioned attributes into |
paul@48 | 481 | # a dictionary. |
paul@48 | 482 | |
paul@46 | 483 | if not instattr: |
paul@46 | 484 | self.all_instattr = {} |
paul@46 | 485 | else: |
paul@49 | 486 | self.all_instattr = self._get_attributes(instattr) |
paul@12 | 487 | |
paul@12 | 488 | self.all_instattr_names = self.all_instattr.keys() |
paul@12 | 489 | |
paul@12 | 490 | return self.all_instattr |
paul@9 | 491 | |
paul@49 | 492 | def _get_position_list(self, positions): |
paul@48 | 493 | |
paul@48 | 494 | """ |
paul@49 | 495 | Return a list of attribute names for the given 'positions' mapping from |
paul@49 | 496 | names to positions, indicating the positions of the attributes in the |
paul@49 | 497 | final instance structure. |
paul@48 | 498 | """ |
paul@48 | 499 | |
paul@49 | 500 | position_items = positions.items() |
paul@49 | 501 | namearray = [None] * len(position_items) |
paul@48 | 502 | |
paul@48 | 503 | # Get the positions in ascending order of list size, with lists |
paul@48 | 504 | # of the same size ordered according to their smallest position |
paul@48 | 505 | # value. |
paul@48 | 506 | |
paul@49 | 507 | position_items.sort(self._cmp_positions) |
paul@48 | 508 | |
paul@48 | 509 | # Get the names in position order. |
paul@48 | 510 | |
paul@48 | 511 | held = [] |
paul@48 | 512 | |
paul@49 | 513 | for name, pos in position_items: |
paul@48 | 514 | pos = list(pos) |
paul@51 | 515 | pos.sort() |
paul@51 | 516 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@49 | 517 | namearray[pos[0]] = name |
paul@48 | 518 | else: |
paul@49 | 519 | if pos: |
paul@49 | 520 | self.relocated.add(name) |
paul@48 | 521 | held.append((name, pos)) |
paul@48 | 522 | |
paul@49 | 523 | for i, attr in enumerate(namearray): |
paul@48 | 524 | if attr is None: |
paul@48 | 525 | name, pos = held.pop() |
paul@49 | 526 | namearray[i] = name |
paul@49 | 527 | |
paul@49 | 528 | #print self.name, positions |
paul@49 | 529 | #print "->", namearray |
paul@49 | 530 | return namearray |
paul@49 | 531 | |
paul@49 | 532 | def _get_attributes(self, positions): |
paul@48 | 533 | |
paul@49 | 534 | """ |
paul@49 | 535 | For the given 'positions' mapping from names to positions, return a |
paul@49 | 536 | dictionary mapping names to Attr instances incorporating information |
paul@49 | 537 | about their positions in the final instance structure. |
paul@49 | 538 | """ |
paul@49 | 539 | |
paul@49 | 540 | d = {} |
paul@49 | 541 | for i, name in enumerate(self._get_position_list(positions)): |
paul@54 | 542 | d[name] = Attr(i, Instance(), name, None) |
paul@49 | 543 | return d |
paul@48 | 544 | |
paul@46 | 545 | def _cmp_positions(self, a, b): |
paul@48 | 546 | |
paul@48 | 547 | "Compare name plus position list operands 'a' and 'b'." |
paul@48 | 548 | |
paul@46 | 549 | name_a, list_a = a |
paul@46 | 550 | name_b, list_b = b |
paul@46 | 551 | if len(list_a) < len(list_b): |
paul@46 | 552 | return -1 |
paul@46 | 553 | elif len(list_a) > len(list_b): |
paul@46 | 554 | return 1 |
paul@49 | 555 | elif not list_a: |
paul@49 | 556 | return 0 |
paul@46 | 557 | else: |
paul@46 | 558 | return cmp(min(list_a), min(list_b)) |
paul@46 | 559 | |
paul@9 | 560 | def all_attribute_names(self): |
paul@9 | 561 | |
paul@9 | 562 | """ |
paul@9 | 563 | Return the names of all attributes provided by instances of this class. |
paul@9 | 564 | """ |
paul@9 | 565 | |
paul@9 | 566 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@9 | 567 | return self.allattr_names |
paul@9 | 568 | |
paul@9 | 569 | def all_attributes(self): |
paul@9 | 570 | |
paul@4 | 571 | """ |
paul@4 | 572 | Return all attributes for an instance, indicating either the class which |
paul@4 | 573 | provides them or that the instance itself provides them. |
paul@4 | 574 | """ |
paul@4 | 575 | |
paul@9 | 576 | if self.allattr is None: |
paul@9 | 577 | self.allattr = {} |
paul@12 | 578 | self.allattr.update(self.all_class_attributes()) |
paul@47 | 579 | for name, attr in self.instance_attributes().items(): |
paul@9 | 580 | if self.allattr.has_key(name): |
paul@9 | 581 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@47 | 582 | self.allattr[name] = attr |
paul@9 | 583 | return self.allattr |
paul@4 | 584 | |
paul@12 | 585 | class Function(NamespaceDict, Naming): |
paul@0 | 586 | |
paul@0 | 587 | "An inspected function." |
paul@0 | 588 | |
paul@62 | 589 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, global_namespace=None, node=None): |
paul@66 | 590 | |
paul@66 | 591 | """ |
paul@66 | 592 | Initialise the function with the given 'name', 'parent', list of |
paul@66 | 593 | 'argnames', list of 'defaults', the 'has_star' flag (indicating the |
paul@66 | 594 | presence of a * parameter), the 'has_dstar' flag (indicating the |
paul@66 | 595 | presence of a ** parameter), optional 'global_namespace', and optional |
paul@66 | 596 | AST 'node'. |
paul@66 | 597 | """ |
paul@66 | 598 | |
paul@24 | 599 | NamespaceDict.__init__(self, global_namespace) |
paul@3 | 600 | self.name = name |
paul@53 | 601 | self.parent = parent |
paul@3 | 602 | self.argnames = argnames |
paul@62 | 603 | self.defaults = defaults |
paul@66 | 604 | self.has_star = has_star |
paul@66 | 605 | self.has_dstar = has_dstar |
paul@66 | 606 | self.node = node |
paul@66 | 607 | |
paul@66 | 608 | # Initialise the positional names. |
paul@66 | 609 | |
paul@59 | 610 | self.positional_names = self.argnames[:] |
paul@59 | 611 | if has_dstar: |
paul@59 | 612 | self.dstar_name = self.positional_names[-1] |
paul@59 | 613 | del self.positional_names[-1] |
paul@59 | 614 | if has_star: |
paul@59 | 615 | self.star_name = self.positional_names[-1] |
paul@59 | 616 | del self.positional_names[-1] |
paul@66 | 617 | |
paul@66 | 618 | # Initialise default storage. |
paul@66 | 619 | |
paul@66 | 620 | self.default_attrs = [] |
paul@3 | 621 | |
paul@36 | 622 | # Caches. |
paul@36 | 623 | |
paul@12 | 624 | self.localnames = None # cache for locals |
paul@12 | 625 | |
paul@17 | 626 | # Add parameters to the namespace. |
paul@17 | 627 | |
paul@25 | 628 | self._add_parameters(argnames) |
paul@17 | 629 | |
paul@9 | 630 | # Image generation details. |
paul@9 | 631 | |
paul@9 | 632 | self.location = None |
paul@19 | 633 | self.code_location = None |
paul@9 | 634 | |
paul@72 | 635 | # Program-related details. |
paul@72 | 636 | |
paul@73 | 637 | self.stack_usage = 0 |
paul@73 | 638 | self.stack_temp_usage = 0 |
paul@73 | 639 | self.stack_local_usage = 0 |
paul@72 | 640 | |
paul@25 | 641 | def _add_parameters(self, argnames): |
paul@25 | 642 | for name in argnames: |
paul@25 | 643 | if isinstance(name, tuple): |
paul@25 | 644 | self._add_parameters(name) |
paul@25 | 645 | else: |
paul@54 | 646 | self.set(name, None) |
paul@25 | 647 | |
paul@4 | 648 | def __repr__(self): |
paul@37 | 649 | if self.location is not None: |
paul@62 | 650 | return "Function(%r, %r, %r, %r, %r, %r, location=%r)" % ( |
paul@62 | 651 | self.name, self.parent, self.argnames, self.defaults, self.has_star, self.has_dstar, self.location |
paul@37 | 652 | ) |
paul@37 | 653 | else: |
paul@62 | 654 | return "Function(%r, %r, %r, %r, %r, %r)" % ( |
paul@62 | 655 | self.name, self.parent, self.argnames, self.defaults, self.has_star, self.has_dstar |
paul@37 | 656 | ) |
paul@4 | 657 | |
paul@66 | 658 | def store_default(self, value): |
paul@66 | 659 | attr = Attr(None, self, None, value) |
paul@66 | 660 | attr.update(value, 1) |
paul@66 | 661 | self.default_attrs.append(attr) |
paul@66 | 662 | |
paul@12 | 663 | def make_global(self, name): |
paul@13 | 664 | if name not in self.argnames and not self.has_key(name): |
paul@12 | 665 | self.globals.add(name) |
paul@12 | 666 | else: |
paul@40 | 667 | raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@12 | 668 | |
paul@7 | 669 | def parameters(self): |
paul@7 | 670 | |
paul@7 | 671 | """ |
paul@7 | 672 | Return a dictionary mapping parameter names to their position in the |
paul@7 | 673 | parameter list. |
paul@7 | 674 | """ |
paul@7 | 675 | |
paul@7 | 676 | parameters = {} |
paul@7 | 677 | for i, name in enumerate(self.argnames): |
paul@7 | 678 | parameters[name] = i |
paul@7 | 679 | return parameters |
paul@7 | 680 | |
paul@36 | 681 | def all_locals(self): |
paul@36 | 682 | |
paul@36 | 683 | "Return a dictionary mapping names to local and parameter details." |
paul@36 | 684 | |
paul@53 | 685 | return dict(self) |
paul@36 | 686 | |
paul@12 | 687 | def locals(self): |
paul@12 | 688 | |
paul@12 | 689 | "Return a dictionary mapping names to local details." |
paul@12 | 690 | |
paul@12 | 691 | if self.localnames is None: |
paul@12 | 692 | self.localnames = {} |
paul@36 | 693 | self.localnames.update(self.all_locals()) |
paul@36 | 694 | for name in self.argnames: |
paul@36 | 695 | del self.localnames[name] |
paul@12 | 696 | return self.localnames |
paul@12 | 697 | |
paul@63 | 698 | def is_method(self): |
paul@63 | 699 | |
paul@63 | 700 | "Return whether this function is a method." |
paul@63 | 701 | |
paul@63 | 702 | return isinstance(self.parent, Class) |
paul@63 | 703 | |
paul@54 | 704 | def is_relocated(self, name): |
paul@54 | 705 | |
paul@54 | 706 | """ |
paul@54 | 707 | Determine whether the given attribute 'name' is relocated for instances |
paul@54 | 708 | having this function as a method. |
paul@54 | 709 | """ |
paul@54 | 710 | |
paul@54 | 711 | for cls in self.parent.descendants: |
paul@54 | 712 | if name in cls.relocated: |
paul@54 | 713 | return 1 |
paul@54 | 714 | return 0 |
paul@54 | 715 | |
paul@54 | 716 | def finalise_attributes(self): |
paul@54 | 717 | |
paul@66 | 718 | """ |
paul@66 | 719 | Make sure all attributes (locals) are fully defined. Note that locals |
paul@66 | 720 | are not attributes in the sense of class, module or instance attributes. |
paul@66 | 721 | Defaults are also finalised by this method. |
paul@66 | 722 | """ |
paul@66 | 723 | |
paul@66 | 724 | for i, default in enumerate(self.default_attrs): |
paul@66 | 725 | default.position = i |
paul@54 | 726 | |
paul@63 | 727 | i = None |
paul@54 | 728 | for i, name in enumerate(self.argnames): |
paul@54 | 729 | self[name].position = i |
paul@54 | 730 | |
paul@63 | 731 | if i is not None: |
paul@63 | 732 | j = i |
paul@73 | 733 | else: |
paul@73 | 734 | j = 0 |
paul@54 | 735 | |
paul@73 | 736 | i = -1 |
paul@73 | 737 | for i, attr in enumerate(self.locals().values()): |
paul@73 | 738 | attr.position = i + j |
paul@73 | 739 | |
paul@73 | 740 | self.stack_local_usage = i + 1 |
paul@66 | 741 | |
paul@61 | 742 | def function_from_method(self): |
paul@61 | 743 | |
paul@61 | 744 | "Make a function from a method." |
paul@61 | 745 | |
paul@62 | 746 | return Function(self.name, self.parent, self.argnames[1:], self.defaults, |
paul@62 | 747 | self.has_star, self.has_dstar, self.global_namespace, self.node) |
paul@61 | 748 | |
paul@53 | 749 | class UnresolvedName(NamespaceDict): |
paul@3 | 750 | |
paul@3 | 751 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 752 | |
paul@24 | 753 | def __init__(self, name, parent_name, global_namespace=None): |
paul@24 | 754 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 755 | self.name = name |
paul@7 | 756 | self.parent_name = parent_name |
paul@0 | 757 | |
paul@12 | 758 | def all_class_attributes(self): |
paul@7 | 759 | return {} |
paul@7 | 760 | |
paul@13 | 761 | def instance_attributes(self): |
paul@13 | 762 | return {} |
paul@13 | 763 | |
paul@4 | 764 | def __repr__(self): |
paul@7 | 765 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@4 | 766 | |
paul@53 | 767 | def full_name(self): |
paul@53 | 768 | if self.name is not None: |
paul@53 | 769 | return self.parent_name + "." + self.name |
paul@53 | 770 | else: |
paul@53 | 771 | return self.parent_name |
paul@53 | 772 | |
paul@54 | 773 | class Instance: |
paul@54 | 774 | |
paul@54 | 775 | "A placeholder indicating the involvement of an instance." |
paul@54 | 776 | |
paul@54 | 777 | def __repr__(self): |
paul@54 | 778 | return "Instance()" |
paul@54 | 779 | |
paul@15 | 780 | class Module(NamespaceDict): |
paul@0 | 781 | |
paul@20 | 782 | "An inspected module's core details." |
paul@20 | 783 | |
paul@15 | 784 | def __init__(self, name): |
paul@24 | 785 | NamespaceDict.__init__(self, self) |
paul@1 | 786 | self.name = name |
paul@0 | 787 | |
paul@72 | 788 | # Original location details. |
paul@72 | 789 | |
paul@72 | 790 | self.node = None |
paul@72 | 791 | |
paul@7 | 792 | # Complete lists of classes and functions. |
paul@7 | 793 | |
paul@33 | 794 | self.all_objects = set() |
paul@0 | 795 | |
paul@22 | 796 | # Constant records. |
paul@22 | 797 | |
paul@22 | 798 | self.constant_values = {} |
paul@22 | 799 | self.constant_list = None # cache for constants |
paul@22 | 800 | |
paul@71 | 801 | # Keyword records. |
paul@71 | 802 | |
paul@71 | 803 | self.keyword_names = set() |
paul@71 | 804 | |
paul@15 | 805 | # Image generation details. |
paul@15 | 806 | |
paul@15 | 807 | self.location = None |
paul@19 | 808 | self.code_location = None |
paul@15 | 809 | |
paul@72 | 810 | # Program-related details. |
paul@20 | 811 | |
paul@73 | 812 | self.stack_usage = 0 |
paul@73 | 813 | self.stack_temp_usage = 0 |
paul@73 | 814 | self.stack_local_usage = 0 |
paul@20 | 815 | |
paul@15 | 816 | def full_name(self): |
paul@15 | 817 | return self.name |
paul@15 | 818 | |
paul@15 | 819 | def __repr__(self): |
paul@37 | 820 | if self.location is not None: |
paul@37 | 821 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@37 | 822 | else: |
paul@37 | 823 | return "Module(%r)" % self.name |
paul@15 | 824 | |
paul@15 | 825 | # Attribute methods. |
paul@15 | 826 | |
paul@36 | 827 | "Return the module attribute names provided by the module." |
paul@15 | 828 | |
paul@36 | 829 | module_attribute_names = NamespaceDict.keys |
paul@15 | 830 | |
paul@15 | 831 | def module_attributes(self): |
paul@15 | 832 | |
paul@15 | 833 | "Return a dictionary mapping names to module attributes." |
paul@15 | 834 | |
paul@53 | 835 | return dict(self) |
paul@15 | 836 | |
paul@22 | 837 | def constants(self): |
paul@22 | 838 | |
paul@22 | 839 | "Return a list of constants." |
paul@22 | 840 | |
paul@22 | 841 | if self.constant_list is None: |
paul@22 | 842 | self.constant_list = list(self.constant_values.values()) |
paul@22 | 843 | |
paul@22 | 844 | return self.constant_list |
paul@22 | 845 | |
paul@21 | 846 | # Program visitors. |
paul@21 | 847 | |
paul@15 | 848 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 849 | |
paul@20 | 850 | """ |
paul@20 | 851 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 852 | capable of being used as an AST visitor. |
paul@20 | 853 | """ |
paul@15 | 854 | |
paul@71 | 855 | def __init__(self, name, importer): |
paul@71 | 856 | |
paul@71 | 857 | """ |
paul@71 | 858 | Initialise this visitor with a module 'name' and an 'importer' which is |
paul@71 | 859 | used to provide access to other modules when required. |
paul@71 | 860 | """ |
paul@71 | 861 | |
paul@15 | 862 | ASTVisitor.__init__(self) |
paul@15 | 863 | Module.__init__(self, name) |
paul@15 | 864 | self.visitor = self |
paul@15 | 865 | |
paul@15 | 866 | self.importer = importer |
paul@61 | 867 | self.builtins = self.importer.modules.get("__builtins__") |
paul@15 | 868 | self.loaded = 0 |
paul@15 | 869 | |
paul@1 | 870 | # Current expression state. |
paul@1 | 871 | |
paul@1 | 872 | self.expr = None |
paul@1 | 873 | |
paul@1 | 874 | # Namespace state. |
paul@1 | 875 | |
paul@34 | 876 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@34 | 877 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 878 | self.namespaces = [] |
paul@0 | 879 | self.module = None |
paul@0 | 880 | |
paul@0 | 881 | def parse(self, filename): |
paul@9 | 882 | |
paul@9 | 883 | "Parse the file having the given 'filename'." |
paul@9 | 884 | |
paul@0 | 885 | module = compiler.parseFile(filename) |
paul@0 | 886 | self.process(module) |
paul@0 | 887 | |
paul@0 | 888 | def process(self, module): |
paul@9 | 889 | |
paul@9 | 890 | "Process the given 'module'." |
paul@9 | 891 | |
paul@20 | 892 | self.node = self.module = module |
paul@1 | 893 | processed = self.dispatch(module) |
paul@13 | 894 | if self.has_key("__all__"): |
paul@12 | 895 | all = self["__all__"] |
paul@1 | 896 | if isinstance(all, compiler.ast.List): |
paul@1 | 897 | for n in all.nodes: |
paul@12 | 898 | self[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 899 | return processed |
paul@0 | 900 | |
paul@3 | 901 | def vacuum(self): |
paul@9 | 902 | |
paul@9 | 903 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 904 | |
paul@13 | 905 | for name, value in self.items(): |
paul@3 | 906 | if isinstance(value, Module) and not value.loaded: |
paul@12 | 907 | del self[name] |
paul@0 | 908 | |
paul@16 | 909 | # Complain about globals not initialised at the module level. |
paul@16 | 910 | |
paul@16 | 911 | if isinstance(value, Global): |
paul@16 | 912 | print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) |
paul@16 | 913 | |
paul@3 | 914 | # Namespace methods. |
paul@0 | 915 | |
paul@0 | 916 | def store(self, name, obj): |
paul@12 | 917 | |
paul@12 | 918 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 919 | |
paul@12 | 920 | if not self.namespaces: |
paul@34 | 921 | self.set(name, obj, not self.in_loop) |
paul@12 | 922 | else: |
paul@34 | 923 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 924 | |
paul@12 | 925 | # Record all non-local objects. |
paul@12 | 926 | |
paul@12 | 927 | if not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@33 | 928 | self.all_objects.add(obj) |
paul@0 | 929 | |
paul@34 | 930 | def store_instance_attr(self, name): |
paul@12 | 931 | |
paul@12 | 932 | "Record instance attribute 'name' in the current class." |
paul@12 | 933 | |
paul@0 | 934 | if self.in_init: |
paul@15 | 935 | |
paul@12 | 936 | # Current namespace is the function. |
paul@12 | 937 | # Previous namespace is the class. |
paul@15 | 938 | |
paul@12 | 939 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 940 | |
paul@7 | 941 | def get_parent(self): |
paul@71 | 942 | |
paul@71 | 943 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 944 | |
paul@12 | 945 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 946 | |
paul@3 | 947 | # Visitor methods. |
paul@3 | 948 | |
paul@3 | 949 | def default(self, node, *args): |
paul@39 | 950 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 951 | |
paul@3 | 952 | def dispatch(self, node, *args): |
paul@3 | 953 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 954 | |
paul@0 | 955 | def NOP(self, node): |
paul@21 | 956 | for n in node.getChildNodes(): |
paul@21 | 957 | self.dispatch(n) |
paul@19 | 958 | return None |
paul@0 | 959 | |
paul@1 | 960 | visitAdd = NOP |
paul@1 | 961 | |
paul@0 | 962 | visitAnd = NOP |
paul@0 | 963 | |
paul@4 | 964 | visitAssert = NOP |
paul@4 | 965 | |
paul@0 | 966 | def visitAssign(self, node): |
paul@0 | 967 | self.expr = self.dispatch(node.expr) |
paul@0 | 968 | for n in node.nodes: |
paul@0 | 969 | self.dispatch(n) |
paul@0 | 970 | return None |
paul@0 | 971 | |
paul@0 | 972 | def visitAssAttr(self, node): |
paul@0 | 973 | expr = self.dispatch(node.expr) |
paul@36 | 974 | if isinstance(expr, Attr) and expr.name == "self": |
paul@34 | 975 | self.store_instance_attr(node.attrname) |
paul@0 | 976 | return None |
paul@0 | 977 | |
paul@0 | 978 | def visitAssList(self, node): |
paul@0 | 979 | for n in node.nodes: |
paul@0 | 980 | self.dispatch(n) |
paul@0 | 981 | return None |
paul@0 | 982 | |
paul@0 | 983 | def visitAssName(self, node): |
paul@36 | 984 | if isinstance(self.expr, Attr): |
paul@36 | 985 | self.store(node.name, self.expr.value) |
paul@36 | 986 | else: |
paul@36 | 987 | self.store(node.name, self.expr) |
paul@0 | 988 | return None |
paul@0 | 989 | |
paul@0 | 990 | visitAssTuple = visitAssList |
paul@0 | 991 | |
paul@2 | 992 | visitAugAssign = NOP |
paul@2 | 993 | |
paul@4 | 994 | visitBackquote = NOP |
paul@4 | 995 | |
paul@2 | 996 | visitBitand = NOP |
paul@2 | 997 | |
paul@2 | 998 | visitBitor = NOP |
paul@2 | 999 | |
paul@4 | 1000 | visitBitxor = NOP |
paul@4 | 1001 | |
paul@4 | 1002 | visitBreak = NOP |
paul@4 | 1003 | |
paul@0 | 1004 | visitCallFunc = NOP |
paul@0 | 1005 | |
paul@0 | 1006 | def visitClass(self, node): |
paul@12 | 1007 | if self.namespaces: |
paul@41 | 1008 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@0 | 1009 | else: |
paul@53 | 1010 | cls = Class(node.name, self.get_parent(), self, node) |
paul@37 | 1011 | |
paul@37 | 1012 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 1013 | # definitions of classes. |
paul@37 | 1014 | |
paul@0 | 1015 | for base in node.bases: |
paul@36 | 1016 | expr = self.dispatch(base) |
paul@36 | 1017 | if isinstance(expr, Attr): |
paul@36 | 1018 | if expr.assignments != 1: |
paul@39 | 1019 | raise InspectError(self.full_name(), node, |
paul@40 | 1020 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 1021 | else: |
paul@36 | 1022 | cls.add_base(expr.value) |
paul@36 | 1023 | else: # if expr is None: |
paul@39 | 1024 | raise InspectError(self.full_name(), node, |
paul@40 | 1025 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 1026 | |
paul@59 | 1027 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 1028 | # If no bases exist, adopt the 'object' class. |
paul@59 | 1029 | |
paul@61 | 1030 | if not node.bases and not (self.name == "__builtins__" and node.name == "object") : |
paul@59 | 1031 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@59 | 1032 | cls.add_base(expr.value) |
paul@59 | 1033 | |
paul@21 | 1034 | # Make a back reference from the node for code generation. |
paul@21 | 1035 | |
paul@39 | 1036 | node.unit = cls |
paul@21 | 1037 | |
paul@0 | 1038 | # Make an entry for the class. |
paul@0 | 1039 | |
paul@0 | 1040 | self.store(node.name, cls) |
paul@0 | 1041 | |
paul@59 | 1042 | # Process the class body. |
paul@59 | 1043 | |
paul@12 | 1044 | self.namespaces.append(cls) |
paul@0 | 1045 | self.dispatch(node.code) |
paul@12 | 1046 | self.namespaces.pop() |
paul@0 | 1047 | |
paul@19 | 1048 | return None |
paul@0 | 1049 | |
paul@4 | 1050 | visitCompare = NOP |
paul@4 | 1051 | |
paul@21 | 1052 | def visitConst(self, node): |
paul@21 | 1053 | const = Const(node.value) |
paul@22 | 1054 | self.constant_values[node.value] = const |
paul@24 | 1055 | return const |
paul@0 | 1056 | |
paul@4 | 1057 | visitContinue = NOP |
paul@4 | 1058 | |
paul@4 | 1059 | visitDecorators = NOP |
paul@4 | 1060 | |
paul@0 | 1061 | visitDict = NOP |
paul@0 | 1062 | |
paul@0 | 1063 | visitDiscard = NOP |
paul@0 | 1064 | |
paul@4 | 1065 | visitDiv = NOP |
paul@4 | 1066 | |
paul@4 | 1067 | visitEllipsis = NOP |
paul@4 | 1068 | |
paul@4 | 1069 | visitExec = NOP |
paul@4 | 1070 | |
paul@4 | 1071 | visitExpression = NOP |
paul@4 | 1072 | |
paul@4 | 1073 | visitFloorDiv = NOP |
paul@4 | 1074 | |
paul@34 | 1075 | def visitFor(self, node): |
paul@34 | 1076 | self.in_loop = 1 |
paul@34 | 1077 | self.NOP(node) |
paul@34 | 1078 | self.in_loop = 0 |
paul@0 | 1079 | |
paul@0 | 1080 | def visitFrom(self, node): |
paul@0 | 1081 | module = self.importer.load(node.modname, 1) |
paul@0 | 1082 | |
paul@35 | 1083 | #if module is None: |
paul@35 | 1084 | # print "Warning:", node.modname, "not imported." |
paul@0 | 1085 | |
paul@0 | 1086 | for name, alias in node.names: |
paul@0 | 1087 | if name != "*": |
paul@4 | 1088 | if module is not None and module.namespace.has_key(name): |
paul@24 | 1089 | attr = module[name] |
paul@36 | 1090 | self.store(alias or name, attr.value) |
paul@2 | 1091 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 1092 | self.importer.load(attr.name) |
paul@24 | 1093 | |
paul@24 | 1094 | # Support the import of names from missing modules. |
paul@24 | 1095 | |
paul@3 | 1096 | else: |
paul@24 | 1097 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 1098 | else: |
paul@3 | 1099 | if module is not None: |
paul@3 | 1100 | for n in module.namespace.keys(): |
paul@24 | 1101 | attr = module[n] |
paul@36 | 1102 | self.store(n, attr.value) |
paul@3 | 1103 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 1104 | self.importer.load(attr.name) |
paul@0 | 1105 | |
paul@0 | 1106 | return None |
paul@0 | 1107 | |
paul@0 | 1108 | def visitFunction(self, node): |
paul@3 | 1109 | function = Function( |
paul@3 | 1110 | node.name, |
paul@53 | 1111 | self.get_parent(), |
paul@3 | 1112 | node.argnames, |
paul@62 | 1113 | node.defaults, |
paul@17 | 1114 | (node.flags & 4 != 0), |
paul@17 | 1115 | (node.flags & 8 != 0), |
paul@24 | 1116 | self, |
paul@17 | 1117 | node |
paul@3 | 1118 | ) |
paul@0 | 1119 | |
paul@21 | 1120 | # Make a back reference from the node for code generation. |
paul@21 | 1121 | |
paul@39 | 1122 | node.unit = function |
paul@21 | 1123 | |
paul@66 | 1124 | # Process the defaults. |
paul@66 | 1125 | |
paul@66 | 1126 | for n in node.defaults: |
paul@66 | 1127 | self.expr = self.dispatch(n) |
paul@66 | 1128 | if isinstance(self.expr, Attr): |
paul@66 | 1129 | function.store_default(self.expr.value) |
paul@66 | 1130 | else: |
paul@66 | 1131 | function.store_default(self.expr) |
paul@66 | 1132 | |
paul@66 | 1133 | # Enter the function. |
paul@66 | 1134 | |
paul@12 | 1135 | self.namespaces.append(function) |
paul@15 | 1136 | |
paul@12 | 1137 | # Current namespace is the function. |
paul@12 | 1138 | # Previous namespace is the class. |
paul@15 | 1139 | |
paul@12 | 1140 | if node.name == "__init__" and isinstance(self.namespaces[-2], Class): |
paul@0 | 1141 | self.in_init = 1 |
paul@15 | 1142 | |
paul@12 | 1143 | self.dispatch(node.code) |
paul@12 | 1144 | self.in_init = 0 |
paul@12 | 1145 | self.namespaces.pop() |
paul@3 | 1146 | |
paul@3 | 1147 | self.store(node.name, function) |
paul@0 | 1148 | return None |
paul@0 | 1149 | |
paul@4 | 1150 | visitGenExpr = NOP |
paul@4 | 1151 | |
paul@4 | 1152 | visitGenExprFor = NOP |
paul@4 | 1153 | |
paul@4 | 1154 | visitGenExprIf = NOP |
paul@4 | 1155 | |
paul@4 | 1156 | visitGenExprInner = NOP |
paul@4 | 1157 | |
paul@0 | 1158 | def visitGetattr(self, node): |
paul@0 | 1159 | expr = self.dispatch(node.expr) |
paul@36 | 1160 | if isinstance(expr, Attr): |
paul@36 | 1161 | value = expr.value |
paul@36 | 1162 | if isinstance(value, Module): |
paul@36 | 1163 | return value.namespace.get(node.attrname) |
paul@36 | 1164 | elif isinstance(value, UnresolvedName): |
paul@36 | 1165 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@61 | 1166 | if self.builtins is not None: |
paul@61 | 1167 | return self.builtins.get(node.attrname) |
paul@61 | 1168 | else: |
paul@61 | 1169 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@4 | 1170 | |
paul@12 | 1171 | def visitGlobal(self, node): |
paul@12 | 1172 | if self.namespaces: |
paul@12 | 1173 | for name in node.names: |
paul@12 | 1174 | self.namespaces[-1].make_global(name) |
paul@37 | 1175 | |
paul@37 | 1176 | # Record a global entry for the name in the module. |
paul@37 | 1177 | |
paul@15 | 1178 | if not self.has_key(name): |
paul@16 | 1179 | self[name] = Global() |
paul@0 | 1180 | |
paul@0 | 1181 | def visitIf(self, node): |
paul@0 | 1182 | for test, body in node.tests: |
paul@0 | 1183 | self.dispatch(body) |
paul@0 | 1184 | if node.else_ is not None: |
paul@0 | 1185 | self.dispatch(node.else_) |
paul@0 | 1186 | return None |
paul@0 | 1187 | |
paul@12 | 1188 | visitIfExp = NOP |
paul@12 | 1189 | |
paul@0 | 1190 | def visitImport(self, node): |
paul@0 | 1191 | for name, alias in node.names: |
paul@2 | 1192 | if alias is not None: |
paul@24 | 1193 | self.store(alias, self.importer.load(name, 1) or UnresolvedName(None, name, self)) |
paul@2 | 1194 | else: |
paul@24 | 1195 | self.store(name.split(".")[0], self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self)) |
paul@0 | 1196 | |
paul@0 | 1197 | return None |
paul@0 | 1198 | |
paul@4 | 1199 | visitInvert = NOP |
paul@4 | 1200 | |
paul@59 | 1201 | def visitKeyword(self, node): |
paul@59 | 1202 | self.dispatch(node.expr) |
paul@59 | 1203 | const = Const(node.name) |
paul@59 | 1204 | self.constant_values[node.name] = const |
paul@71 | 1205 | self.keyword_names.add(node.name) |
paul@59 | 1206 | return None |
paul@4 | 1207 | |
paul@4 | 1208 | visitLambda = NOP |
paul@4 | 1209 | |
paul@1 | 1210 | visitLeftShift = NOP |
paul@1 | 1211 | |
paul@0 | 1212 | visitList = NOP |
paul@0 | 1213 | |
paul@4 | 1214 | visitListComp = NOP |
paul@4 | 1215 | |
paul@4 | 1216 | visitListCompFor = NOP |
paul@4 | 1217 | |
paul@4 | 1218 | visitListCompIf = NOP |
paul@4 | 1219 | |
paul@4 | 1220 | visitMod = NOP |
paul@4 | 1221 | |
paul@0 | 1222 | def visitModule(self, node): |
paul@0 | 1223 | return self.dispatch(node.node) |
paul@0 | 1224 | |
paul@2 | 1225 | visitMul = NOP |
paul@2 | 1226 | |
paul@0 | 1227 | def visitName(self, node): |
paul@0 | 1228 | name = node.name |
paul@36 | 1229 | if self.namespaces and self.namespaces[-1].has_key(name): |
paul@35 | 1230 | return self.namespaces[-1][name] |
paul@13 | 1231 | elif self.has_key(name): |
paul@12 | 1232 | return self[name] |
paul@61 | 1233 | elif self.builtins is not None and self.builtins.has_key(name): |
paul@61 | 1234 | return self.builtins[name] |
paul@0 | 1235 | else: |
paul@0 | 1236 | return None |
paul@0 | 1237 | |
paul@0 | 1238 | visitNot = NOP |
paul@0 | 1239 | |
paul@0 | 1240 | visitOr = NOP |
paul@0 | 1241 | |
paul@0 | 1242 | visitPass = NOP |
paul@0 | 1243 | |
paul@4 | 1244 | visitPower = NOP |
paul@4 | 1245 | |
paul@4 | 1246 | visitPrint = NOP |
paul@4 | 1247 | |
paul@4 | 1248 | visitPrintnl = NOP |
paul@4 | 1249 | |
paul@0 | 1250 | visitRaise = NOP |
paul@0 | 1251 | |
paul@4 | 1252 | visitReturn = NOP |
paul@4 | 1253 | |
paul@1 | 1254 | visitRightShift = NOP |
paul@1 | 1255 | |
paul@4 | 1256 | visitSlice = NOP |
paul@4 | 1257 | |
paul@23 | 1258 | visitSliceobj = NOP |
paul@23 | 1259 | |
paul@0 | 1260 | def visitStmt(self, node): |
paul@0 | 1261 | for n in node.nodes: |
paul@0 | 1262 | self.dispatch(n) |
paul@0 | 1263 | return None |
paul@0 | 1264 | |
paul@4 | 1265 | visitSub = NOP |
paul@4 | 1266 | |
paul@0 | 1267 | visitSubscript = NOP |
paul@0 | 1268 | |
paul@0 | 1269 | def visitTryExcept(self, node): |
paul@0 | 1270 | self.dispatch(node.body) |
paul@0 | 1271 | for name, var, n in node.handlers: |
paul@0 | 1272 | self.dispatch(n) |
paul@0 | 1273 | if node.else_ is not None: |
paul@0 | 1274 | self.dispatch(node.else_) |
paul@0 | 1275 | return None |
paul@0 | 1276 | |
paul@4 | 1277 | visitTryFinally = NOP |
paul@4 | 1278 | |
paul@1 | 1279 | visitTuple = NOP |
paul@1 | 1280 | |
paul@4 | 1281 | visitUnaryAdd = NOP |
paul@4 | 1282 | |
paul@4 | 1283 | visitUnarySub = NOP |
paul@4 | 1284 | |
paul@34 | 1285 | def visitWhile(self, node): |
paul@34 | 1286 | self.in_loop = 1 |
paul@34 | 1287 | self.NOP(node) |
paul@34 | 1288 | self.in_loop = 0 |
paul@4 | 1289 | |
paul@4 | 1290 | visitWith = NOP |
paul@4 | 1291 | |
paul@4 | 1292 | visitYield = NOP |
paul@4 | 1293 | |
paul@16 | 1294 | class Global: |
paul@16 | 1295 | |
paul@16 | 1296 | """ |
paul@16 | 1297 | A reference to an object assigned to a global from outside the module |
paul@16 | 1298 | top-level. |
paul@16 | 1299 | """ |
paul@16 | 1300 | |
paul@16 | 1301 | pass |
paul@16 | 1302 | |
paul@0 | 1303 | # vim: tabstop=4 expandtab shiftwidth=4 |