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@21 | 275 | def __eq__(self, other): |
paul@21 | 276 | return self.value == other.value |
paul@21 | 277 | |
paul@21 | 278 | def __hash__(self): |
paul@21 | 279 | return hash(self.value) |
paul@21 | 280 | |
paul@4 | 281 | class Class(NamespaceDict, Naming): |
paul@0 | 282 | |
paul@0 | 283 | "An inspected class." |
paul@0 | 284 | |
paul@53 | 285 | def __init__(self, name, parent, global_namespace=None, node=None): |
paul@54 | 286 | |
paul@54 | 287 | """ |
paul@54 | 288 | Initialise the class with the given 'name', 'parent' object, optional |
paul@54 | 289 | 'global_namespace' and optional AST 'node'. |
paul@54 | 290 | """ |
paul@54 | 291 | |
paul@24 | 292 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 293 | self.name = name |
paul@53 | 294 | self.parent = parent |
paul@17 | 295 | self.node = node |
paul@12 | 296 | |
paul@53 | 297 | # Superclasses, descendants and attributes. |
paul@47 | 298 | |
paul@0 | 299 | self.bases = [] |
paul@53 | 300 | self.descendants = set() |
paul@46 | 301 | self.instattr = set() # instance attributes |
paul@49 | 302 | self.relocated = set() # attributes which do not have the same |
paul@49 | 303 | # position as those of the same name in |
paul@49 | 304 | # some superclasses |
paul@12 | 305 | |
paul@47 | 306 | # Caches. |
paul@47 | 307 | |
paul@46 | 308 | self.all_instattr = None # cache for instance_attributes |
paul@46 | 309 | self.all_instattr_names = None # from all_instattr |
paul@46 | 310 | self.all_classattr = None # cache for all_class_attributes |
paul@12 | 311 | self.all_classattr_names = None # from all_classattr |
paul@46 | 312 | self.allattr = None # cache for all_attributes |
paul@46 | 313 | self.allattr_names = None # from allattr |
paul@9 | 314 | |
paul@63 | 315 | # Add this class to its attributes. |
paul@63 | 316 | |
paul@63 | 317 | self.set("__class__", self) |
paul@63 | 318 | |
paul@9 | 319 | # Image generation details. |
paul@9 | 320 | |
paul@9 | 321 | self.location = None |
paul@19 | 322 | self.code_location = None |
paul@61 | 323 | self.instantiator = None |
paul@0 | 324 | |
paul@12 | 325 | def __repr__(self): |
paul@37 | 326 | if self.location is not None: |
paul@53 | 327 | return "Class(%r, %r, location=%r)" % (self.name, self.parent, self.location) |
paul@37 | 328 | else: |
paul@53 | 329 | return "Class(%r, %r)" % (self.name, self.parent) |
paul@12 | 330 | |
paul@49 | 331 | def finalise_attributes(self): |
paul@49 | 332 | |
paul@49 | 333 | "Make sure that all attributes are fully defined." |
paul@49 | 334 | |
paul@49 | 335 | self.finalise_class_attributes() |
paul@49 | 336 | self.finalise_instance_attributes() |
paul@49 | 337 | |
paul@61 | 338 | def get_instantiator(self): |
paul@61 | 339 | |
paul@61 | 340 | "Return a function which can be used to instantiate the class." |
paul@61 | 341 | |
paul@61 | 342 | if self.instantiator is None: |
paul@61 | 343 | init_method = self.all_class_attributes()["__init__"].value |
paul@61 | 344 | self.instantiator = init_method.function_from_method() |
paul@61 | 345 | return self.instantiator |
paul@61 | 346 | |
paul@49 | 347 | # Class-specific methods. |
paul@49 | 348 | |
paul@0 | 349 | def add_base(self, base): |
paul@0 | 350 | self.bases.append(base) |
paul@53 | 351 | base.add_descendant(self) |
paul@0 | 352 | |
paul@12 | 353 | def add_instance_attribute(self, name): |
paul@12 | 354 | self.instattr.add(name) |
paul@9 | 355 | |
paul@53 | 356 | def add_descendant(self, cls): |
paul@53 | 357 | self.descendants.add(cls) |
paul@53 | 358 | for base in self.bases: |
paul@53 | 359 | base.add_descendant(cls) |
paul@53 | 360 | |
paul@36 | 361 | "Return the attribute names provided by this class only." |
paul@9 | 362 | |
paul@36 | 363 | class_attribute_names = NamespaceDict.keys |
paul@4 | 364 | |
paul@4 | 365 | def class_attributes(self): |
paul@4 | 366 | |
paul@12 | 367 | "Return class attributes provided by this class only." |
paul@4 | 368 | |
paul@49 | 369 | self.finalise_class_attributes() |
paul@53 | 370 | return dict(self) |
paul@12 | 371 | |
paul@12 | 372 | def all_class_attribute_names(self): |
paul@12 | 373 | |
paul@12 | 374 | "Return the attribute names provided by classes in this hierarchy." |
paul@12 | 375 | |
paul@12 | 376 | if self.all_classattr_names is None: |
paul@12 | 377 | self.all_class_attributes() |
paul@12 | 378 | return self.all_classattr_names |
paul@12 | 379 | |
paul@12 | 380 | def all_class_attributes(self): |
paul@12 | 381 | |
paul@12 | 382 | "Return all class attributes, indicating the class which provides them." |
paul@12 | 383 | |
paul@49 | 384 | self.finalise_class_attributes() |
paul@49 | 385 | return self.all_classattr |
paul@49 | 386 | |
paul@49 | 387 | def finalise_class_attributes(self): |
paul@49 | 388 | |
paul@49 | 389 | "Make sure that the class attributes are fully defined." |
paul@49 | 390 | |
paul@12 | 391 | if self.all_classattr is None: |
paul@12 | 392 | self.all_classattr = {} |
paul@49 | 393 | clsattr = {} |
paul@49 | 394 | |
paul@49 | 395 | # Record provisional position information for attributes of this |
paul@49 | 396 | # class. |
paul@49 | 397 | |
paul@49 | 398 | for name in self.class_attributes().keys(): |
paul@49 | 399 | clsattr[name] = set() # position not yet defined |
paul@12 | 400 | |
paul@9 | 401 | reversed_bases = self.bases[:] |
paul@9 | 402 | reversed_bases.reverse() |
paul@49 | 403 | |
paul@49 | 404 | # For the bases in reverse order, acquire class attribute details. |
paul@49 | 405 | |
paul@9 | 406 | for cls in reversed_bases: |
paul@49 | 407 | for name, attr in cls.all_class_attributes().items(): |
paul@49 | 408 | self.all_classattr[name] = attr |
paul@10 | 409 | |
paul@49 | 410 | # Record previous attribute information. |
paul@49 | 411 | |
paul@49 | 412 | if clsattr.has_key(name): |
paul@49 | 413 | clsattr[name].add(attr.position) |
paul@49 | 414 | |
paul@49 | 415 | # Record class attributes provided by this class and its bases, |
paul@49 | 416 | # along with their positions. |
paul@10 | 417 | |
paul@12 | 418 | self.all_classattr.update(self.class_attributes()) |
paul@9 | 419 | |
paul@49 | 420 | if clsattr: |
paul@49 | 421 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@49 | 422 | self.all_classattr[name].position = i |
paul@49 | 423 | |
paul@12 | 424 | return self.all_classattr |
paul@4 | 425 | |
paul@7 | 426 | def instance_attribute_names(self): |
paul@7 | 427 | |
paul@7 | 428 | "Return the instance attribute names provided by the class." |
paul@7 | 429 | |
paul@12 | 430 | if self.all_instattr_names is None: |
paul@12 | 431 | self.instance_attributes() |
paul@12 | 432 | return self.all_instattr_names |
paul@7 | 433 | |
paul@4 | 434 | def instance_attributes(self): |
paul@4 | 435 | |
paul@9 | 436 | "Return instance-only attributes for instances of this class." |
paul@9 | 437 | |
paul@49 | 438 | self.finalise_instance_attributes() |
paul@49 | 439 | return self.all_instattr |
paul@49 | 440 | |
paul@49 | 441 | def finalise_instance_attributes(self): |
paul@49 | 442 | |
paul@49 | 443 | "Make sure that the instance attributes are fully defined." |
paul@49 | 444 | |
paul@12 | 445 | if self.all_instattr is None: |
paul@12 | 446 | self.all_instattr = {} |
paul@46 | 447 | instattr = {} |
paul@12 | 448 | |
paul@49 | 449 | # Record provisional position information for attributes of this |
paul@49 | 450 | # instance. |
paul@49 | 451 | |
paul@49 | 452 | for name in self.instattr: |
paul@49 | 453 | instattr[name] = set() # position not yet defined |
paul@49 | 454 | |
paul@12 | 455 | reversed_bases = self.bases[:] |
paul@12 | 456 | reversed_bases.reverse() |
paul@46 | 457 | |
paul@46 | 458 | # For the bases in reverse order, acquire instance attribute |
paul@46 | 459 | # details. |
paul@46 | 460 | |
paul@12 | 461 | for cls in reversed_bases: |
paul@46 | 462 | for name, attr in cls.instance_attributes().items(): |
paul@49 | 463 | |
paul@49 | 464 | # Record previous attribute information. |
paul@12 | 465 | |
paul@49 | 466 | if instattr.has_key(name): |
paul@49 | 467 | instattr[name].add(attr.position) |
paul@46 | 468 | |
paul@48 | 469 | # Cache the attributes by converting the positioned attributes into |
paul@48 | 470 | # a dictionary. |
paul@48 | 471 | |
paul@46 | 472 | if not instattr: |
paul@46 | 473 | self.all_instattr = {} |
paul@46 | 474 | else: |
paul@49 | 475 | self.all_instattr = self._get_attributes(instattr) |
paul@12 | 476 | |
paul@12 | 477 | self.all_instattr_names = self.all_instattr.keys() |
paul@12 | 478 | |
paul@12 | 479 | return self.all_instattr |
paul@9 | 480 | |
paul@49 | 481 | def _get_position_list(self, positions): |
paul@48 | 482 | |
paul@48 | 483 | """ |
paul@49 | 484 | Return a list of attribute names for the given 'positions' mapping from |
paul@49 | 485 | names to positions, indicating the positions of the attributes in the |
paul@49 | 486 | final instance structure. |
paul@48 | 487 | """ |
paul@48 | 488 | |
paul@49 | 489 | position_items = positions.items() |
paul@49 | 490 | namearray = [None] * len(position_items) |
paul@48 | 491 | |
paul@48 | 492 | # Get the positions in ascending order of list size, with lists |
paul@48 | 493 | # of the same size ordered according to their smallest position |
paul@48 | 494 | # value. |
paul@48 | 495 | |
paul@49 | 496 | position_items.sort(self._cmp_positions) |
paul@48 | 497 | |
paul@48 | 498 | # Get the names in position order. |
paul@48 | 499 | |
paul@48 | 500 | held = [] |
paul@48 | 501 | |
paul@49 | 502 | for name, pos in position_items: |
paul@48 | 503 | pos = list(pos) |
paul@51 | 504 | pos.sort() |
paul@51 | 505 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@49 | 506 | namearray[pos[0]] = name |
paul@48 | 507 | else: |
paul@49 | 508 | if pos: |
paul@49 | 509 | self.relocated.add(name) |
paul@48 | 510 | held.append((name, pos)) |
paul@48 | 511 | |
paul@49 | 512 | for i, attr in enumerate(namearray): |
paul@48 | 513 | if attr is None: |
paul@48 | 514 | name, pos = held.pop() |
paul@49 | 515 | namearray[i] = name |
paul@49 | 516 | |
paul@49 | 517 | #print self.name, positions |
paul@49 | 518 | #print "->", namearray |
paul@49 | 519 | return namearray |
paul@49 | 520 | |
paul@49 | 521 | def _get_attributes(self, positions): |
paul@48 | 522 | |
paul@49 | 523 | """ |
paul@49 | 524 | For the given 'positions' mapping from names to positions, return a |
paul@49 | 525 | dictionary mapping names to Attr instances incorporating information |
paul@49 | 526 | about their positions in the final instance structure. |
paul@49 | 527 | """ |
paul@49 | 528 | |
paul@49 | 529 | d = {} |
paul@49 | 530 | for i, name in enumerate(self._get_position_list(positions)): |
paul@54 | 531 | d[name] = Attr(i, Instance(), name, None) |
paul@49 | 532 | return d |
paul@48 | 533 | |
paul@46 | 534 | def _cmp_positions(self, a, b): |
paul@48 | 535 | |
paul@48 | 536 | "Compare name plus position list operands 'a' and 'b'." |
paul@48 | 537 | |
paul@46 | 538 | name_a, list_a = a |
paul@46 | 539 | name_b, list_b = b |
paul@46 | 540 | if len(list_a) < len(list_b): |
paul@46 | 541 | return -1 |
paul@46 | 542 | elif len(list_a) > len(list_b): |
paul@46 | 543 | return 1 |
paul@49 | 544 | elif not list_a: |
paul@49 | 545 | return 0 |
paul@46 | 546 | else: |
paul@46 | 547 | return cmp(min(list_a), min(list_b)) |
paul@46 | 548 | |
paul@9 | 549 | def all_attribute_names(self): |
paul@9 | 550 | |
paul@9 | 551 | """ |
paul@9 | 552 | Return the names of all attributes provided by instances of this class. |
paul@9 | 553 | """ |
paul@9 | 554 | |
paul@9 | 555 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@9 | 556 | return self.allattr_names |
paul@9 | 557 | |
paul@9 | 558 | def all_attributes(self): |
paul@9 | 559 | |
paul@4 | 560 | """ |
paul@4 | 561 | Return all attributes for an instance, indicating either the class which |
paul@4 | 562 | provides them or that the instance itself provides them. |
paul@4 | 563 | """ |
paul@4 | 564 | |
paul@9 | 565 | if self.allattr is None: |
paul@9 | 566 | self.allattr = {} |
paul@12 | 567 | self.allattr.update(self.all_class_attributes()) |
paul@47 | 568 | for name, attr in self.instance_attributes().items(): |
paul@9 | 569 | if self.allattr.has_key(name): |
paul@9 | 570 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@47 | 571 | self.allattr[name] = attr |
paul@9 | 572 | return self.allattr |
paul@4 | 573 | |
paul@12 | 574 | class Function(NamespaceDict, Naming): |
paul@0 | 575 | |
paul@0 | 576 | "An inspected function." |
paul@0 | 577 | |
paul@62 | 578 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, global_namespace=None, node=None): |
paul@66 | 579 | |
paul@66 | 580 | """ |
paul@66 | 581 | Initialise the function with the given 'name', 'parent', list of |
paul@66 | 582 | 'argnames', list of 'defaults', the 'has_star' flag (indicating the |
paul@66 | 583 | presence of a * parameter), the 'has_dstar' flag (indicating the |
paul@66 | 584 | presence of a ** parameter), optional 'global_namespace', and optional |
paul@66 | 585 | AST 'node'. |
paul@66 | 586 | """ |
paul@66 | 587 | |
paul@24 | 588 | NamespaceDict.__init__(self, global_namespace) |
paul@3 | 589 | self.name = name |
paul@53 | 590 | self.parent = parent |
paul@3 | 591 | self.argnames = argnames |
paul@62 | 592 | self.defaults = defaults |
paul@66 | 593 | self.has_star = has_star |
paul@66 | 594 | self.has_dstar = has_dstar |
paul@66 | 595 | self.node = node |
paul@66 | 596 | |
paul@66 | 597 | # Initialise the positional names. |
paul@66 | 598 | |
paul@59 | 599 | self.positional_names = self.argnames[:] |
paul@59 | 600 | if has_dstar: |
paul@59 | 601 | self.dstar_name = self.positional_names[-1] |
paul@59 | 602 | del self.positional_names[-1] |
paul@59 | 603 | if has_star: |
paul@59 | 604 | self.star_name = self.positional_names[-1] |
paul@59 | 605 | del self.positional_names[-1] |
paul@66 | 606 | |
paul@66 | 607 | # Initialise default storage. |
paul@66 | 608 | |
paul@66 | 609 | self.default_attrs = [] |
paul@3 | 610 | |
paul@36 | 611 | # Caches. |
paul@36 | 612 | |
paul@12 | 613 | self.localnames = None # cache for locals |
paul@12 | 614 | |
paul@17 | 615 | # Add parameters to the namespace. |
paul@17 | 616 | |
paul@25 | 617 | self._add_parameters(argnames) |
paul@17 | 618 | |
paul@9 | 619 | # Image generation details. |
paul@9 | 620 | |
paul@9 | 621 | self.location = None |
paul@19 | 622 | self.code_location = None |
paul@9 | 623 | |
paul@25 | 624 | def _add_parameters(self, argnames): |
paul@25 | 625 | for name in argnames: |
paul@25 | 626 | if isinstance(name, tuple): |
paul@25 | 627 | self._add_parameters(name) |
paul@25 | 628 | else: |
paul@54 | 629 | self.set(name, None) |
paul@25 | 630 | |
paul@4 | 631 | def __repr__(self): |
paul@37 | 632 | if self.location is not None: |
paul@62 | 633 | return "Function(%r, %r, %r, %r, %r, %r, location=%r)" % ( |
paul@62 | 634 | self.name, self.parent, self.argnames, self.defaults, self.has_star, self.has_dstar, self.location |
paul@37 | 635 | ) |
paul@37 | 636 | else: |
paul@62 | 637 | return "Function(%r, %r, %r, %r, %r, %r)" % ( |
paul@62 | 638 | self.name, self.parent, self.argnames, self.defaults, self.has_star, self.has_dstar |
paul@37 | 639 | ) |
paul@4 | 640 | |
paul@66 | 641 | def store_default(self, value): |
paul@66 | 642 | attr = Attr(None, self, None, value) |
paul@66 | 643 | attr.update(value, 1) |
paul@66 | 644 | self.default_attrs.append(attr) |
paul@66 | 645 | |
paul@12 | 646 | def make_global(self, name): |
paul@13 | 647 | if name not in self.argnames and not self.has_key(name): |
paul@12 | 648 | self.globals.add(name) |
paul@12 | 649 | else: |
paul@40 | 650 | raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@12 | 651 | |
paul@7 | 652 | def parameters(self): |
paul@7 | 653 | |
paul@7 | 654 | """ |
paul@7 | 655 | Return a dictionary mapping parameter names to their position in the |
paul@7 | 656 | parameter list. |
paul@7 | 657 | """ |
paul@7 | 658 | |
paul@7 | 659 | parameters = {} |
paul@7 | 660 | for i, name in enumerate(self.argnames): |
paul@7 | 661 | parameters[name] = i |
paul@7 | 662 | return parameters |
paul@7 | 663 | |
paul@36 | 664 | def all_locals(self): |
paul@36 | 665 | |
paul@36 | 666 | "Return a dictionary mapping names to local and parameter details." |
paul@36 | 667 | |
paul@53 | 668 | return dict(self) |
paul@36 | 669 | |
paul@12 | 670 | def locals(self): |
paul@12 | 671 | |
paul@12 | 672 | "Return a dictionary mapping names to local details." |
paul@12 | 673 | |
paul@12 | 674 | if self.localnames is None: |
paul@12 | 675 | self.localnames = {} |
paul@36 | 676 | self.localnames.update(self.all_locals()) |
paul@36 | 677 | for name in self.argnames: |
paul@36 | 678 | del self.localnames[name] |
paul@12 | 679 | return self.localnames |
paul@12 | 680 | |
paul@63 | 681 | def is_method(self): |
paul@63 | 682 | |
paul@63 | 683 | "Return whether this function is a method." |
paul@63 | 684 | |
paul@63 | 685 | return isinstance(self.parent, Class) |
paul@63 | 686 | |
paul@54 | 687 | def is_relocated(self, name): |
paul@54 | 688 | |
paul@54 | 689 | """ |
paul@54 | 690 | Determine whether the given attribute 'name' is relocated for instances |
paul@54 | 691 | having this function as a method. |
paul@54 | 692 | """ |
paul@54 | 693 | |
paul@54 | 694 | for cls in self.parent.descendants: |
paul@54 | 695 | if name in cls.relocated: |
paul@54 | 696 | return 1 |
paul@54 | 697 | return 0 |
paul@54 | 698 | |
paul@54 | 699 | def finalise_attributes(self): |
paul@54 | 700 | |
paul@66 | 701 | """ |
paul@66 | 702 | Make sure all attributes (locals) are fully defined. Note that locals |
paul@66 | 703 | are not attributes in the sense of class, module or instance attributes. |
paul@66 | 704 | Defaults are also finalised by this method. |
paul@66 | 705 | """ |
paul@66 | 706 | |
paul@66 | 707 | for i, default in enumerate(self.default_attrs): |
paul@66 | 708 | default.position = i |
paul@54 | 709 | |
paul@63 | 710 | i = None |
paul@54 | 711 | for i, name in enumerate(self.argnames): |
paul@54 | 712 | self[name].position = i |
paul@54 | 713 | |
paul@63 | 714 | if i is not None: |
paul@63 | 715 | j = i |
paul@63 | 716 | for i, attr in enumerate(self.locals().values()): |
paul@63 | 717 | attr.position = i + j |
paul@54 | 718 | |
paul@66 | 719 | # NOTE: May also have temporary variables. |
paul@66 | 720 | |
paul@61 | 721 | def function_from_method(self): |
paul@61 | 722 | |
paul@61 | 723 | "Make a function from a method." |
paul@61 | 724 | |
paul@62 | 725 | return Function(self.name, self.parent, self.argnames[1:], self.defaults, |
paul@62 | 726 | self.has_star, self.has_dstar, self.global_namespace, self.node) |
paul@61 | 727 | |
paul@53 | 728 | class UnresolvedName(NamespaceDict): |
paul@3 | 729 | |
paul@3 | 730 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 731 | |
paul@24 | 732 | def __init__(self, name, parent_name, global_namespace=None): |
paul@24 | 733 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 734 | self.name = name |
paul@7 | 735 | self.parent_name = parent_name |
paul@0 | 736 | |
paul@12 | 737 | def all_class_attributes(self): |
paul@7 | 738 | return {} |
paul@7 | 739 | |
paul@13 | 740 | def instance_attributes(self): |
paul@13 | 741 | return {} |
paul@13 | 742 | |
paul@4 | 743 | def __repr__(self): |
paul@7 | 744 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@4 | 745 | |
paul@53 | 746 | def full_name(self): |
paul@53 | 747 | if self.name is not None: |
paul@53 | 748 | return self.parent_name + "." + self.name |
paul@53 | 749 | else: |
paul@53 | 750 | return self.parent_name |
paul@53 | 751 | |
paul@54 | 752 | class Instance: |
paul@54 | 753 | |
paul@54 | 754 | "A placeholder indicating the involvement of an instance." |
paul@54 | 755 | |
paul@54 | 756 | def __repr__(self): |
paul@54 | 757 | return "Instance()" |
paul@54 | 758 | |
paul@15 | 759 | class Module(NamespaceDict): |
paul@0 | 760 | |
paul@20 | 761 | "An inspected module's core details." |
paul@20 | 762 | |
paul@15 | 763 | def __init__(self, name): |
paul@24 | 764 | NamespaceDict.__init__(self, self) |
paul@1 | 765 | self.name = name |
paul@0 | 766 | |
paul@7 | 767 | # Complete lists of classes and functions. |
paul@7 | 768 | |
paul@33 | 769 | self.all_objects = set() |
paul@0 | 770 | |
paul@22 | 771 | # Constant records. |
paul@22 | 772 | |
paul@22 | 773 | self.constant_values = {} |
paul@22 | 774 | self.constant_list = None # cache for constants |
paul@22 | 775 | |
paul@71 | 776 | # Keyword records. |
paul@71 | 777 | |
paul@71 | 778 | self.keyword_names = set() |
paul@71 | 779 | |
paul@15 | 780 | # Image generation details. |
paul@15 | 781 | |
paul@15 | 782 | self.location = None |
paul@19 | 783 | self.code_location = None |
paul@15 | 784 | |
paul@20 | 785 | # Original location details. |
paul@20 | 786 | |
paul@20 | 787 | self.node = None |
paul@20 | 788 | |
paul@15 | 789 | def full_name(self): |
paul@15 | 790 | return self.name |
paul@15 | 791 | |
paul@15 | 792 | def __repr__(self): |
paul@37 | 793 | if self.location is not None: |
paul@37 | 794 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@37 | 795 | else: |
paul@37 | 796 | return "Module(%r)" % self.name |
paul@15 | 797 | |
paul@15 | 798 | # Attribute methods. |
paul@15 | 799 | |
paul@36 | 800 | "Return the module attribute names provided by the module." |
paul@15 | 801 | |
paul@36 | 802 | module_attribute_names = NamespaceDict.keys |
paul@15 | 803 | |
paul@15 | 804 | def module_attributes(self): |
paul@15 | 805 | |
paul@15 | 806 | "Return a dictionary mapping names to module attributes." |
paul@15 | 807 | |
paul@53 | 808 | return dict(self) |
paul@15 | 809 | |
paul@22 | 810 | def constants(self): |
paul@22 | 811 | |
paul@22 | 812 | "Return a list of constants." |
paul@22 | 813 | |
paul@22 | 814 | if self.constant_list is None: |
paul@22 | 815 | self.constant_list = list(self.constant_values.values()) |
paul@22 | 816 | |
paul@22 | 817 | return self.constant_list |
paul@22 | 818 | |
paul@21 | 819 | # Program visitors. |
paul@21 | 820 | |
paul@15 | 821 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 822 | |
paul@20 | 823 | """ |
paul@20 | 824 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 825 | capable of being used as an AST visitor. |
paul@20 | 826 | """ |
paul@15 | 827 | |
paul@71 | 828 | def __init__(self, name, importer): |
paul@71 | 829 | |
paul@71 | 830 | """ |
paul@71 | 831 | Initialise this visitor with a module 'name' and an 'importer' which is |
paul@71 | 832 | used to provide access to other modules when required. |
paul@71 | 833 | """ |
paul@71 | 834 | |
paul@15 | 835 | ASTVisitor.__init__(self) |
paul@15 | 836 | Module.__init__(self, name) |
paul@15 | 837 | self.visitor = self |
paul@15 | 838 | |
paul@15 | 839 | self.importer = importer |
paul@61 | 840 | self.builtins = self.importer.modules.get("__builtins__") |
paul@15 | 841 | self.loaded = 0 |
paul@15 | 842 | |
paul@1 | 843 | # Current expression state. |
paul@1 | 844 | |
paul@1 | 845 | self.expr = None |
paul@1 | 846 | |
paul@1 | 847 | # Namespace state. |
paul@1 | 848 | |
paul@34 | 849 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@34 | 850 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 851 | self.namespaces = [] |
paul@0 | 852 | self.module = None |
paul@0 | 853 | |
paul@0 | 854 | def parse(self, filename): |
paul@9 | 855 | |
paul@9 | 856 | "Parse the file having the given 'filename'." |
paul@9 | 857 | |
paul@0 | 858 | module = compiler.parseFile(filename) |
paul@0 | 859 | self.process(module) |
paul@0 | 860 | |
paul@0 | 861 | def process(self, module): |
paul@9 | 862 | |
paul@9 | 863 | "Process the given 'module'." |
paul@9 | 864 | |
paul@20 | 865 | self.node = self.module = module |
paul@1 | 866 | processed = self.dispatch(module) |
paul@13 | 867 | if self.has_key("__all__"): |
paul@12 | 868 | all = self["__all__"] |
paul@1 | 869 | if isinstance(all, compiler.ast.List): |
paul@1 | 870 | for n in all.nodes: |
paul@12 | 871 | self[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 872 | return processed |
paul@0 | 873 | |
paul@3 | 874 | def vacuum(self): |
paul@9 | 875 | |
paul@9 | 876 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 877 | |
paul@13 | 878 | for name, value in self.items(): |
paul@3 | 879 | if isinstance(value, Module) and not value.loaded: |
paul@12 | 880 | del self[name] |
paul@0 | 881 | |
paul@16 | 882 | # Complain about globals not initialised at the module level. |
paul@16 | 883 | |
paul@16 | 884 | if isinstance(value, Global): |
paul@16 | 885 | print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) |
paul@16 | 886 | |
paul@3 | 887 | # Namespace methods. |
paul@0 | 888 | |
paul@0 | 889 | def store(self, name, obj): |
paul@12 | 890 | |
paul@12 | 891 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 892 | |
paul@12 | 893 | if not self.namespaces: |
paul@34 | 894 | self.set(name, obj, not self.in_loop) |
paul@12 | 895 | else: |
paul@34 | 896 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 897 | |
paul@12 | 898 | # Record all non-local objects. |
paul@12 | 899 | |
paul@12 | 900 | if not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@33 | 901 | self.all_objects.add(obj) |
paul@0 | 902 | |
paul@34 | 903 | def store_instance_attr(self, name): |
paul@12 | 904 | |
paul@12 | 905 | "Record instance attribute 'name' in the current class." |
paul@12 | 906 | |
paul@0 | 907 | if self.in_init: |
paul@15 | 908 | |
paul@12 | 909 | # Current namespace is the function. |
paul@12 | 910 | # Previous namespace is the class. |
paul@15 | 911 | |
paul@12 | 912 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 913 | |
paul@7 | 914 | def get_parent(self): |
paul@71 | 915 | |
paul@71 | 916 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 917 | |
paul@12 | 918 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 919 | |
paul@3 | 920 | # Visitor methods. |
paul@3 | 921 | |
paul@3 | 922 | def default(self, node, *args): |
paul@39 | 923 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 924 | |
paul@3 | 925 | def dispatch(self, node, *args): |
paul@3 | 926 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 927 | |
paul@0 | 928 | def NOP(self, node): |
paul@21 | 929 | for n in node.getChildNodes(): |
paul@21 | 930 | self.dispatch(n) |
paul@19 | 931 | return None |
paul@0 | 932 | |
paul@1 | 933 | visitAdd = NOP |
paul@1 | 934 | |
paul@0 | 935 | visitAnd = NOP |
paul@0 | 936 | |
paul@4 | 937 | visitAssert = NOP |
paul@4 | 938 | |
paul@0 | 939 | def visitAssign(self, node): |
paul@0 | 940 | self.expr = self.dispatch(node.expr) |
paul@0 | 941 | for n in node.nodes: |
paul@0 | 942 | self.dispatch(n) |
paul@0 | 943 | return None |
paul@0 | 944 | |
paul@0 | 945 | def visitAssAttr(self, node): |
paul@0 | 946 | expr = self.dispatch(node.expr) |
paul@36 | 947 | if isinstance(expr, Attr) and expr.name == "self": |
paul@34 | 948 | self.store_instance_attr(node.attrname) |
paul@0 | 949 | return None |
paul@0 | 950 | |
paul@0 | 951 | def visitAssList(self, node): |
paul@0 | 952 | for n in node.nodes: |
paul@0 | 953 | self.dispatch(n) |
paul@0 | 954 | return None |
paul@0 | 955 | |
paul@0 | 956 | def visitAssName(self, node): |
paul@36 | 957 | if isinstance(self.expr, Attr): |
paul@36 | 958 | self.store(node.name, self.expr.value) |
paul@36 | 959 | else: |
paul@36 | 960 | self.store(node.name, self.expr) |
paul@0 | 961 | return None |
paul@0 | 962 | |
paul@0 | 963 | visitAssTuple = visitAssList |
paul@0 | 964 | |
paul@2 | 965 | visitAugAssign = NOP |
paul@2 | 966 | |
paul@4 | 967 | visitBackquote = NOP |
paul@4 | 968 | |
paul@2 | 969 | visitBitand = NOP |
paul@2 | 970 | |
paul@2 | 971 | visitBitor = NOP |
paul@2 | 972 | |
paul@4 | 973 | visitBitxor = NOP |
paul@4 | 974 | |
paul@4 | 975 | visitBreak = NOP |
paul@4 | 976 | |
paul@0 | 977 | visitCallFunc = NOP |
paul@0 | 978 | |
paul@0 | 979 | def visitClass(self, node): |
paul@12 | 980 | if self.namespaces: |
paul@41 | 981 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@0 | 982 | else: |
paul@53 | 983 | cls = Class(node.name, self.get_parent(), self, node) |
paul@37 | 984 | |
paul@37 | 985 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 986 | # definitions of classes. |
paul@37 | 987 | |
paul@0 | 988 | for base in node.bases: |
paul@36 | 989 | expr = self.dispatch(base) |
paul@36 | 990 | if isinstance(expr, Attr): |
paul@36 | 991 | if expr.assignments != 1: |
paul@39 | 992 | raise InspectError(self.full_name(), node, |
paul@40 | 993 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 994 | else: |
paul@36 | 995 | cls.add_base(expr.value) |
paul@36 | 996 | else: # if expr is None: |
paul@39 | 997 | raise InspectError(self.full_name(), node, |
paul@40 | 998 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 999 | |
paul@59 | 1000 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 1001 | # If no bases exist, adopt the 'object' class. |
paul@59 | 1002 | |
paul@61 | 1003 | if not node.bases and not (self.name == "__builtins__" and node.name == "object") : |
paul@59 | 1004 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@59 | 1005 | cls.add_base(expr.value) |
paul@59 | 1006 | |
paul@21 | 1007 | # Make a back reference from the node for code generation. |
paul@21 | 1008 | |
paul@39 | 1009 | node.unit = cls |
paul@21 | 1010 | |
paul@0 | 1011 | # Make an entry for the class. |
paul@0 | 1012 | |
paul@0 | 1013 | self.store(node.name, cls) |
paul@0 | 1014 | |
paul@59 | 1015 | # Process the class body. |
paul@59 | 1016 | |
paul@12 | 1017 | self.namespaces.append(cls) |
paul@0 | 1018 | self.dispatch(node.code) |
paul@12 | 1019 | self.namespaces.pop() |
paul@0 | 1020 | |
paul@19 | 1021 | return None |
paul@0 | 1022 | |
paul@4 | 1023 | visitCompare = NOP |
paul@4 | 1024 | |
paul@21 | 1025 | def visitConst(self, node): |
paul@21 | 1026 | const = Const(node.value) |
paul@22 | 1027 | self.constant_values[node.value] = const |
paul@24 | 1028 | return const |
paul@0 | 1029 | |
paul@4 | 1030 | visitContinue = NOP |
paul@4 | 1031 | |
paul@4 | 1032 | visitDecorators = NOP |
paul@4 | 1033 | |
paul@0 | 1034 | visitDict = NOP |
paul@0 | 1035 | |
paul@0 | 1036 | visitDiscard = NOP |
paul@0 | 1037 | |
paul@4 | 1038 | visitDiv = NOP |
paul@4 | 1039 | |
paul@4 | 1040 | visitEllipsis = NOP |
paul@4 | 1041 | |
paul@4 | 1042 | visitExec = NOP |
paul@4 | 1043 | |
paul@4 | 1044 | visitExpression = NOP |
paul@4 | 1045 | |
paul@4 | 1046 | visitFloorDiv = NOP |
paul@4 | 1047 | |
paul@34 | 1048 | def visitFor(self, node): |
paul@34 | 1049 | self.in_loop = 1 |
paul@34 | 1050 | self.NOP(node) |
paul@34 | 1051 | self.in_loop = 0 |
paul@0 | 1052 | |
paul@0 | 1053 | def visitFrom(self, node): |
paul@0 | 1054 | module = self.importer.load(node.modname, 1) |
paul@0 | 1055 | |
paul@35 | 1056 | #if module is None: |
paul@35 | 1057 | # print "Warning:", node.modname, "not imported." |
paul@0 | 1058 | |
paul@0 | 1059 | for name, alias in node.names: |
paul@0 | 1060 | if name != "*": |
paul@4 | 1061 | if module is not None and module.namespace.has_key(name): |
paul@24 | 1062 | attr = module[name] |
paul@36 | 1063 | self.store(alias or name, attr.value) |
paul@2 | 1064 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 1065 | self.importer.load(attr.name) |
paul@24 | 1066 | |
paul@24 | 1067 | # Support the import of names from missing modules. |
paul@24 | 1068 | |
paul@3 | 1069 | else: |
paul@24 | 1070 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 1071 | else: |
paul@3 | 1072 | if module is not None: |
paul@3 | 1073 | for n in module.namespace.keys(): |
paul@24 | 1074 | attr = module[n] |
paul@36 | 1075 | self.store(n, attr.value) |
paul@3 | 1076 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 1077 | self.importer.load(attr.name) |
paul@0 | 1078 | |
paul@0 | 1079 | return None |
paul@0 | 1080 | |
paul@0 | 1081 | def visitFunction(self, node): |
paul@3 | 1082 | function = Function( |
paul@3 | 1083 | node.name, |
paul@53 | 1084 | self.get_parent(), |
paul@3 | 1085 | node.argnames, |
paul@62 | 1086 | node.defaults, |
paul@17 | 1087 | (node.flags & 4 != 0), |
paul@17 | 1088 | (node.flags & 8 != 0), |
paul@24 | 1089 | self, |
paul@17 | 1090 | node |
paul@3 | 1091 | ) |
paul@0 | 1092 | |
paul@21 | 1093 | # Make a back reference from the node for code generation. |
paul@21 | 1094 | |
paul@39 | 1095 | node.unit = function |
paul@21 | 1096 | |
paul@66 | 1097 | # Process the defaults. |
paul@66 | 1098 | |
paul@66 | 1099 | for n in node.defaults: |
paul@66 | 1100 | self.expr = self.dispatch(n) |
paul@66 | 1101 | if isinstance(self.expr, Attr): |
paul@66 | 1102 | function.store_default(self.expr.value) |
paul@66 | 1103 | else: |
paul@66 | 1104 | function.store_default(self.expr) |
paul@66 | 1105 | |
paul@66 | 1106 | # Enter the function. |
paul@66 | 1107 | |
paul@12 | 1108 | self.namespaces.append(function) |
paul@15 | 1109 | |
paul@12 | 1110 | # Current namespace is the function. |
paul@12 | 1111 | # Previous namespace is the class. |
paul@15 | 1112 | |
paul@12 | 1113 | if node.name == "__init__" and isinstance(self.namespaces[-2], Class): |
paul@0 | 1114 | self.in_init = 1 |
paul@15 | 1115 | |
paul@12 | 1116 | self.dispatch(node.code) |
paul@12 | 1117 | self.in_init = 0 |
paul@12 | 1118 | self.namespaces.pop() |
paul@3 | 1119 | |
paul@3 | 1120 | self.store(node.name, function) |
paul@0 | 1121 | return None |
paul@0 | 1122 | |
paul@4 | 1123 | visitGenExpr = NOP |
paul@4 | 1124 | |
paul@4 | 1125 | visitGenExprFor = NOP |
paul@4 | 1126 | |
paul@4 | 1127 | visitGenExprIf = NOP |
paul@4 | 1128 | |
paul@4 | 1129 | visitGenExprInner = NOP |
paul@4 | 1130 | |
paul@0 | 1131 | def visitGetattr(self, node): |
paul@0 | 1132 | expr = self.dispatch(node.expr) |
paul@36 | 1133 | if isinstance(expr, Attr): |
paul@36 | 1134 | value = expr.value |
paul@36 | 1135 | if isinstance(value, Module): |
paul@36 | 1136 | return value.namespace.get(node.attrname) |
paul@36 | 1137 | elif isinstance(value, UnresolvedName): |
paul@36 | 1138 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@61 | 1139 | if self.builtins is not None: |
paul@61 | 1140 | return self.builtins.get(node.attrname) |
paul@61 | 1141 | else: |
paul@61 | 1142 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@4 | 1143 | |
paul@12 | 1144 | def visitGlobal(self, node): |
paul@12 | 1145 | if self.namespaces: |
paul@12 | 1146 | for name in node.names: |
paul@12 | 1147 | self.namespaces[-1].make_global(name) |
paul@37 | 1148 | |
paul@37 | 1149 | # Record a global entry for the name in the module. |
paul@37 | 1150 | |
paul@15 | 1151 | if not self.has_key(name): |
paul@16 | 1152 | self[name] = Global() |
paul@0 | 1153 | |
paul@0 | 1154 | def visitIf(self, node): |
paul@0 | 1155 | for test, body in node.tests: |
paul@0 | 1156 | self.dispatch(body) |
paul@0 | 1157 | if node.else_ is not None: |
paul@0 | 1158 | self.dispatch(node.else_) |
paul@0 | 1159 | return None |
paul@0 | 1160 | |
paul@12 | 1161 | visitIfExp = NOP |
paul@12 | 1162 | |
paul@0 | 1163 | def visitImport(self, node): |
paul@0 | 1164 | for name, alias in node.names: |
paul@2 | 1165 | if alias is not None: |
paul@24 | 1166 | self.store(alias, self.importer.load(name, 1) or UnresolvedName(None, name, self)) |
paul@2 | 1167 | else: |
paul@24 | 1168 | self.store(name.split(".")[0], self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self)) |
paul@0 | 1169 | |
paul@0 | 1170 | return None |
paul@0 | 1171 | |
paul@4 | 1172 | visitInvert = NOP |
paul@4 | 1173 | |
paul@59 | 1174 | def visitKeyword(self, node): |
paul@59 | 1175 | self.dispatch(node.expr) |
paul@59 | 1176 | const = Const(node.name) |
paul@59 | 1177 | self.constant_values[node.name] = const |
paul@71 | 1178 | self.keyword_names.add(node.name) |
paul@59 | 1179 | return None |
paul@4 | 1180 | |
paul@4 | 1181 | visitLambda = NOP |
paul@4 | 1182 | |
paul@1 | 1183 | visitLeftShift = NOP |
paul@1 | 1184 | |
paul@0 | 1185 | visitList = NOP |
paul@0 | 1186 | |
paul@4 | 1187 | visitListComp = NOP |
paul@4 | 1188 | |
paul@4 | 1189 | visitListCompFor = NOP |
paul@4 | 1190 | |
paul@4 | 1191 | visitListCompIf = NOP |
paul@4 | 1192 | |
paul@4 | 1193 | visitMod = NOP |
paul@4 | 1194 | |
paul@0 | 1195 | def visitModule(self, node): |
paul@0 | 1196 | return self.dispatch(node.node) |
paul@0 | 1197 | |
paul@2 | 1198 | visitMul = NOP |
paul@2 | 1199 | |
paul@0 | 1200 | def visitName(self, node): |
paul@0 | 1201 | name = node.name |
paul@36 | 1202 | if self.namespaces and self.namespaces[-1].has_key(name): |
paul@35 | 1203 | return self.namespaces[-1][name] |
paul@13 | 1204 | elif self.has_key(name): |
paul@12 | 1205 | return self[name] |
paul@61 | 1206 | elif self.builtins is not None and self.builtins.has_key(name): |
paul@61 | 1207 | return self.builtins[name] |
paul@0 | 1208 | else: |
paul@0 | 1209 | return None |
paul@0 | 1210 | |
paul@0 | 1211 | visitNot = NOP |
paul@0 | 1212 | |
paul@0 | 1213 | visitOr = NOP |
paul@0 | 1214 | |
paul@0 | 1215 | visitPass = NOP |
paul@0 | 1216 | |
paul@4 | 1217 | visitPower = NOP |
paul@4 | 1218 | |
paul@4 | 1219 | visitPrint = NOP |
paul@4 | 1220 | |
paul@4 | 1221 | visitPrintnl = NOP |
paul@4 | 1222 | |
paul@0 | 1223 | visitRaise = NOP |
paul@0 | 1224 | |
paul@4 | 1225 | visitReturn = NOP |
paul@4 | 1226 | |
paul@1 | 1227 | visitRightShift = NOP |
paul@1 | 1228 | |
paul@4 | 1229 | visitSlice = NOP |
paul@4 | 1230 | |
paul@23 | 1231 | visitSliceobj = NOP |
paul@23 | 1232 | |
paul@0 | 1233 | def visitStmt(self, node): |
paul@0 | 1234 | for n in node.nodes: |
paul@0 | 1235 | self.dispatch(n) |
paul@0 | 1236 | return None |
paul@0 | 1237 | |
paul@4 | 1238 | visitSub = NOP |
paul@4 | 1239 | |
paul@0 | 1240 | visitSubscript = NOP |
paul@0 | 1241 | |
paul@0 | 1242 | def visitTryExcept(self, node): |
paul@0 | 1243 | self.dispatch(node.body) |
paul@0 | 1244 | for name, var, n in node.handlers: |
paul@0 | 1245 | self.dispatch(n) |
paul@0 | 1246 | if node.else_ is not None: |
paul@0 | 1247 | self.dispatch(node.else_) |
paul@0 | 1248 | return None |
paul@0 | 1249 | |
paul@4 | 1250 | visitTryFinally = NOP |
paul@4 | 1251 | |
paul@1 | 1252 | visitTuple = NOP |
paul@1 | 1253 | |
paul@4 | 1254 | visitUnaryAdd = NOP |
paul@4 | 1255 | |
paul@4 | 1256 | visitUnarySub = NOP |
paul@4 | 1257 | |
paul@34 | 1258 | def visitWhile(self, node): |
paul@34 | 1259 | self.in_loop = 1 |
paul@34 | 1260 | self.NOP(node) |
paul@34 | 1261 | self.in_loop = 0 |
paul@4 | 1262 | |
paul@4 | 1263 | visitWith = NOP |
paul@4 | 1264 | |
paul@4 | 1265 | visitYield = NOP |
paul@4 | 1266 | |
paul@16 | 1267 | class Global: |
paul@16 | 1268 | |
paul@16 | 1269 | """ |
paul@16 | 1270 | A reference to an object assigned to a global from outside the module |
paul@16 | 1271 | top-level. |
paul@16 | 1272 | """ |
paul@16 | 1273 | |
paul@16 | 1274 | pass |
paul@16 | 1275 | |
paul@0 | 1276 | # vim: tabstop=4 expandtab shiftwidth=4 |