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@639 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 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@91 | 20 | |
paul@91 | 21 | -------- |
paul@91 | 22 | |
paul@91 | 23 | The results of inspecting a module are as follows: |
paul@91 | 24 | |
paul@91 | 25 | Constants |
paul@91 | 26 | --------- |
paul@91 | 27 | |
paul@91 | 28 | All constants identified within the code shall be registered. |
paul@91 | 29 | |
paul@91 | 30 | Classes |
paul@91 | 31 | ------- |
paul@91 | 32 | |
paul@91 | 33 | All global classes shall be registered; local classes (within functions) or |
paul@91 | 34 | nested classes (within classes) are not currently registered. |
paul@91 | 35 | |
paul@91 | 36 | Base classes must be detected and constant. |
paul@91 | 37 | |
paul@91 | 38 | All classes without bases are made to inherit from __builtins__.object in order |
paul@91 | 39 | to support some standard methods. |
paul@91 | 40 | |
paul@91 | 41 | Functions |
paul@91 | 42 | --------- |
paul@91 | 43 | |
paul@91 | 44 | All functions and lambda definitions shall be registered. |
paul@91 | 45 | |
paul@91 | 46 | Namespaces |
paul@91 | 47 | ---------- |
paul@91 | 48 | |
paul@91 | 49 | Modules define their own "global" namespace, within which classes, functions |
paul@91 | 50 | and lambda definitions establish a hierarchy of namespaces. |
paul@91 | 51 | |
paul@91 | 52 | Only local, global and built-in namespaces are recognised; closures are not |
paul@91 | 53 | supported. |
paul@91 | 54 | |
paul@91 | 55 | Assignments |
paul@91 | 56 | ----------- |
paul@91 | 57 | |
paul@91 | 58 | Name assignment and attribute assignment involving modules and classes cause |
paul@91 | 59 | names to be associated with values within namespaces. |
paul@91 | 60 | |
paul@91 | 61 | Any assignments within loops are considered to cause the targets of such |
paul@91 | 62 | assignments to provide non-constant values. |
paul@91 | 63 | |
paul@91 | 64 | Assignments to names are only really considered to cause the targets of such |
paul@557 | 65 | assignments to provide constant values if the targets reside in class |
paul@557 | 66 | namespaces, subject to the above conditions. |
paul@91 | 67 | |
paul@91 | 68 | Assignments to names within functions are not generally considered to cause the |
paul@91 | 69 | targets of such assignments to provide constant values since functions can be |
paul@331 | 70 | invoked many times with different inputs. This affects particularly the |
paul@331 | 71 | definition of functions or lambdas within functions. However, there may be |
paul@331 | 72 | benefits in considering a local to be constant within a single invocation. |
paul@0 | 73 | """ |
paul@0 | 74 | |
paul@684 | 75 | from micropython.common import ASTVisitor, operator_functions, get_module_name |
paul@83 | 76 | from micropython.data import * |
paul@555 | 77 | from micropython.errors import * |
paul@738 | 78 | from micropython.stdcompiler import compiler |
paul@507 | 79 | import sys |
paul@15 | 80 | |
paul@622 | 81 | class NullBranch(compiler.ast.AttributeUser): |
paul@622 | 82 | |
paul@622 | 83 | "A class representing an attribute user for a non-existent branch." |
paul@622 | 84 | |
paul@622 | 85 | pass |
paul@622 | 86 | |
paul@21 | 87 | # Program visitors. |
paul@21 | 88 | |
paul@15 | 89 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 90 | |
paul@20 | 91 | """ |
paul@20 | 92 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 93 | capable of being used as an AST visitor. |
paul@575 | 94 | |
paul@575 | 95 | A module can be inspected through the invocation of the following methods in |
paul@575 | 96 | order: |
paul@575 | 97 | |
paul@575 | 98 | 1. parse |
paul@581 | 99 | 2. process |
paul@581 | 100 | 3. vacuum |
paul@581 | 101 | 4. finalise |
paul@575 | 102 | |
paul@575 | 103 | A module importer can be expected to perform these invocations. |
paul@20 | 104 | """ |
paul@15 | 105 | |
paul@71 | 106 | def __init__(self, name, importer): |
paul@71 | 107 | |
paul@71 | 108 | """ |
paul@71 | 109 | Initialise this visitor with a module 'name' and an 'importer' which is |
paul@71 | 110 | used to provide access to other modules when required. |
paul@71 | 111 | """ |
paul@71 | 112 | |
paul@343 | 113 | Module.__init__(self, name, importer) |
paul@15 | 114 | self.visitor = self |
paul@685 | 115 | self.filename = None |
paul@15 | 116 | |
paul@91 | 117 | # Import machinery links. |
paul@91 | 118 | |
paul@61 | 119 | self.builtins = self.importer.modules.get("__builtins__") |
paul@591 | 120 | self.loaded = False |
paul@591 | 121 | self.completed = False |
paul@15 | 122 | |
paul@1 | 123 | # Current expression state. |
paul@1 | 124 | |
paul@1 | 125 | self.expr = None |
paul@591 | 126 | self.in_assignment = False # For slice and subscript handling. |
paul@1 | 127 | |
paul@1 | 128 | # Namespace state. |
paul@1 | 129 | |
paul@591 | 130 | self.in_method = False # Find instance attributes in all methods. |
paul@591 | 131 | self.in_function = False # Note function presence, affecting definitions. |
paul@591 | 132 | self.in_loop = False # Note loop "membership", affecting assignments. |
paul@12 | 133 | self.namespaces = [] |
paul@235 | 134 | self.functions = [] |
paul@0 | 135 | |
paul@0 | 136 | def parse(self, filename): |
paul@9 | 137 | |
paul@9 | 138 | "Parse the file having the given 'filename'." |
paul@9 | 139 | |
paul@685 | 140 | self.filename = filename |
paul@581 | 141 | self.astnode = module = compiler.parseFile(filename) |
paul@581 | 142 | |
paul@581 | 143 | # Detect and record imports and globals declared in the module. |
paul@581 | 144 | |
paul@581 | 145 | self.process_structure(module) |
paul@0 | 146 | |
paul@581 | 147 | def complete(self): |
paul@581 | 148 | if not self.completed: |
paul@591 | 149 | self.completed = True |
paul@581 | 150 | self.process() |
paul@581 | 151 | if self.importer.verbose: |
paul@581 | 152 | print >>sys.stderr, "Completed import of", self.full_name() |
paul@581 | 153 | |
paul@581 | 154 | def process(self): |
paul@581 | 155 | return self.process_module(self.astnode) |
paul@581 | 156 | |
paul@581 | 157 | def process_module(self, module): |
paul@9 | 158 | |
paul@546 | 159 | """ |
paul@581 | 160 | Process the given 'module', visiting module-level code and function |
paul@581 | 161 | code. |
paul@546 | 162 | """ |
paul@9 | 163 | |
paul@439 | 164 | # Add __name__ to the namespace. |
paul@260 | 165 | |
paul@439 | 166 | self.store("__name__", self._visitConst(self.full_name())) |
paul@260 | 167 | |
paul@575 | 168 | # Visit module-level code, also recording global names. |
paul@236 | 169 | |
paul@1 | 170 | processed = self.dispatch(module) |
paul@235 | 171 | |
paul@344 | 172 | self.finalise_attribute_usage() |
paul@284 | 173 | |
paul@578 | 174 | # Visit functions. |
paul@578 | 175 | |
paul@578 | 176 | self.process_functions() |
paul@578 | 177 | |
paul@236 | 178 | # Add references to other modules declared using the __all__ global. |
paul@236 | 179 | |
paul@13 | 180 | if self.has_key("__all__"): |
paul@12 | 181 | all = self["__all__"] |
paul@1 | 182 | if isinstance(all, compiler.ast.List): |
paul@1 | 183 | for n in all.nodes: |
paul@167 | 184 | self.store(n.value, self.importer.add_module(self.name + "." + n.value)) |
paul@235 | 185 | |
paul@1 | 186 | return processed |
paul@0 | 187 | |
paul@546 | 188 | def process_functions(self): |
paul@546 | 189 | |
paul@546 | 190 | """ |
paul@546 | 191 | Process all function bodies. Deferred imports may occur during this |
paul@546 | 192 | process. |
paul@546 | 193 | """ |
paul@546 | 194 | |
paul@547 | 195 | # Then, visit each function, recording other names. This happens to |
paul@547 | 196 | # work for lambda definitions inside functions since they are added to |
paul@547 | 197 | # the end of self.functions and are thus visited as the iteration |
paul@547 | 198 | # reaches the end of the original list. |
paul@546 | 199 | |
paul@546 | 200 | for node, namespaces in self.functions: |
paul@546 | 201 | self._visitFunctionBody(node, namespaces) |
paul@546 | 202 | namespaces[-1].finalise_attribute_usage() |
paul@546 | 203 | |
paul@581 | 204 | def process_structure(self, node): |
paul@236 | 205 | |
paul@236 | 206 | """ |
paul@236 | 207 | Within the given 'node', process global declarations, adjusting the |
paul@581 | 208 | module namespace, and import statements, building a module dependency |
paul@581 | 209 | hierarchy. |
paul@236 | 210 | """ |
paul@236 | 211 | |
paul@236 | 212 | for n in node.getChildNodes(): |
paul@581 | 213 | |
paul@581 | 214 | # Module global detection. |
paul@581 | 215 | |
paul@236 | 216 | if isinstance(n, compiler.ast.Global): |
paul@236 | 217 | for name in n.names: |
paul@575 | 218 | |
paul@575 | 219 | # Each name may potentially be assigned many times. |
paul@578 | 220 | # We don't try and find out the specifics at this point and |
paul@578 | 221 | # just indicate that the name cannot be relied upon for |
paul@578 | 222 | # various observations. |
paul@575 | 223 | |
paul@581 | 224 | self.modify_name(name) |
paul@581 | 225 | |
paul@581 | 226 | # Module import declarations. |
paul@581 | 227 | |
paul@581 | 228 | elif isinstance(n, compiler.ast.From): |
paul@684 | 229 | modname, names = get_module_name(n, self) |
paul@683 | 230 | |
paul@683 | 231 | # Perform whole module relative imports. |
paul@581 | 232 | |
paul@683 | 233 | if not modname: |
paul@683 | 234 | for name, alias in names: |
paul@683 | 235 | self.record_import(name, n) |
paul@581 | 236 | |
paul@683 | 237 | # Otherwise, perform normal "from" imports. |
paul@581 | 238 | |
paul@683 | 239 | else: |
paul@683 | 240 | # Load the mentioned module. |
paul@683 | 241 | |
paul@683 | 242 | self.record_import(modname, n) |
paul@581 | 243 | |
paul@683 | 244 | # Speculatively load modules for names beneath the module. |
paul@683 | 245 | |
paul@683 | 246 | for name, alias in n.names: |
paul@683 | 247 | subname = modname + "." + name |
paul@683 | 248 | self.record_import(subname, n) |
paul@581 | 249 | |
paul@581 | 250 | elif isinstance(n, compiler.ast.Import): |
paul@581 | 251 | |
paul@581 | 252 | # Load the mentioned module. |
paul@581 | 253 | |
paul@581 | 254 | for name, alias in n.names: |
paul@581 | 255 | self.record_import(name, n) |
paul@581 | 256 | |
paul@581 | 257 | # Nodes using operator module functions. |
paul@581 | 258 | |
paul@581 | 259 | elif operator_functions.has_key(n.__class__.__name__) or \ |
paul@581 | 260 | isinstance(n, (compiler.ast.AugAssign, compiler.ast.Compare)): |
paul@581 | 261 | |
paul@581 | 262 | n._module = self.importer.load("operator") |
paul@576 | 263 | |
paul@236 | 264 | else: |
paul@581 | 265 | self.process_structure(n) |
paul@581 | 266 | |
paul@581 | 267 | def get_module_paths(self, name): |
paul@581 | 268 | |
paul@581 | 269 | """ |
paul@581 | 270 | Return the paths of modules leading to the module having the given |
paul@581 | 271 | 'name'. |
paul@581 | 272 | """ |
paul@581 | 273 | |
paul@581 | 274 | names = [] |
paul@581 | 275 | parts = [] |
paul@581 | 276 | for part in name.split("."): |
paul@581 | 277 | parts.append(part) |
paul@581 | 278 | names.append(".".join(parts)) |
paul@581 | 279 | return names |
paul@581 | 280 | |
paul@581 | 281 | def record_import(self, name, node): |
paul@581 | 282 | |
paul@581 | 283 | """ |
paul@581 | 284 | Record an import of a module with the given 'name' occurring at the |
paul@581 | 285 | given 'node'. |
paul@581 | 286 | """ |
paul@581 | 287 | |
paul@581 | 288 | module = self.importer.load(name, 1, importer=node) |
paul@581 | 289 | if module and not module.loaded: |
paul@581 | 290 | self.importer.circular_imports.add(module) |
paul@581 | 291 | |
paul@581 | 292 | def complete_import(self, name, return_leaf): |
paul@581 | 293 | |
paul@581 | 294 | """ |
paul@581 | 295 | Complete the import of the module with the given 'name', returning the |
paul@581 | 296 | module itself if 'return_leaf' is a true value, or returning the root of |
paul@581 | 297 | the module hierarchy if 'return_leaf' is a false value. |
paul@581 | 298 | """ |
paul@581 | 299 | |
paul@581 | 300 | top = module = None |
paul@581 | 301 | |
paul@581 | 302 | for modname in self.get_module_paths(name): |
paul@581 | 303 | |
paul@581 | 304 | # Attempt to get the module, returning None for non-existent |
paul@581 | 305 | # modules. |
paul@581 | 306 | |
paul@581 | 307 | try: |
paul@581 | 308 | module = self.importer.get_module(modname) |
paul@581 | 309 | except KeyError: |
paul@581 | 310 | return None |
paul@581 | 311 | |
paul@581 | 312 | if module: |
paul@581 | 313 | module.complete() |
paul@581 | 314 | |
paul@581 | 315 | if top is None: |
paul@581 | 316 | top = module |
paul@581 | 317 | |
paul@581 | 318 | if return_leaf: |
paul@581 | 319 | return module |
paul@581 | 320 | else: |
paul@581 | 321 | return top |
paul@236 | 322 | |
paul@3 | 323 | def vacuum(self): |
paul@9 | 324 | |
paul@171 | 325 | """ |
paul@171 | 326 | Vacuum the module namespace, removing unreferenced objects and unused |
paul@171 | 327 | names. |
paul@171 | 328 | """ |
paul@9 | 329 | |
paul@659 | 330 | self.vacuum_object(self) |
paul@0 | 331 | |
paul@659 | 332 | all_objects = list(self.all_objects) |
paul@167 | 333 | |
paul@659 | 334 | for obj in all_objects: |
paul@659 | 335 | self.vacuum_object(obj) |
paul@171 | 336 | |
paul@252 | 337 | def vacuum_object(self, obj, delete_all=0): |
paul@241 | 338 | |
paul@252 | 339 | "Vacuum the given object 'obj'." |
paul@171 | 340 | |
paul@468 | 341 | # Get all constant objects in apparent use. |
paul@468 | 342 | |
paul@468 | 343 | if delete_all: |
paul@468 | 344 | obj_objects = set() |
paul@468 | 345 | else: |
paul@468 | 346 | obj_objects = [] |
paul@468 | 347 | for name, attr in obj.items_for_vacuum(): |
paul@468 | 348 | |
paul@468 | 349 | # Get constant objects for attributes in use. |
paul@468 | 350 | |
paul@468 | 351 | if self.importer.uses_attribute(obj.full_name(), name) and \ |
paul@468 | 352 | attr is not None and attr.is_constant(): |
paul@468 | 353 | |
paul@468 | 354 | value = attr.get_value() |
paul@468 | 355 | obj_objects.append(value) |
paul@468 | 356 | |
paul@468 | 357 | # Now vacuum unused attributes and objects not in use. |
paul@468 | 358 | |
paul@351 | 359 | for name, attr in obj.items_for_vacuum(): |
paul@270 | 360 | |
paul@270 | 361 | # Only consider deleting entire unused objects or things accessible |
paul@270 | 362 | # via names which are never used. |
paul@270 | 363 | |
paul@344 | 364 | if delete_all or not self.importer.uses_attribute(obj.full_name(), name): |
paul@351 | 365 | obj.vacuum_item(name) |
paul@270 | 366 | |
paul@270 | 367 | # Delete any unambiguous attribute value. Such values can only |
paul@270 | 368 | # have been defined within the object and therefore are not |
paul@270 | 369 | # redefined by other code regions. |
paul@270 | 370 | |
paul@468 | 371 | if attr is not None and attr.is_constant(): |
paul@252 | 372 | value = attr.get_value() |
paul@171 | 373 | |
paul@394 | 374 | # The value must have this object as a parent. |
paul@468 | 375 | # However, it must not be shared by several names. |
paul@394 | 376 | |
paul@468 | 377 | if value is not obj and value.parent is obj and \ |
paul@468 | 378 | value in self.all_objects and value not in obj_objects: |
paul@468 | 379 | |
paul@252 | 380 | self.all_objects.remove(value) |
paul@171 | 381 | |
paul@351 | 382 | # Delete class contents and lambdas from functions. |
paul@241 | 383 | |
paul@351 | 384 | self.vacuum_object(value, 1) |
paul@171 | 385 | |
paul@287 | 386 | def unfinalise(self): |
paul@287 | 387 | |
paul@287 | 388 | "Reset finalised information for the module." |
paul@287 | 389 | |
paul@287 | 390 | for obj in self.all_objects: |
paul@287 | 391 | obj.unfinalise_attributes() |
paul@287 | 392 | |
paul@437 | 393 | def finalise(self, objtable): |
paul@171 | 394 | |
paul@171 | 395 | "Finalise the module." |
paul@171 | 396 | |
paul@171 | 397 | for obj in self.all_objects: |
paul@437 | 398 | obj.finalise(objtable) |
paul@171 | 399 | |
paul@478 | 400 | self.finalise_users(objtable) |
paul@478 | 401 | |
paul@167 | 402 | def add_object(self, obj, any_scope=0): |
paul@167 | 403 | |
paul@167 | 404 | """ |
paul@167 | 405 | Record 'obj' if non-local or if the optional 'any_scope' is set to a |
paul@167 | 406 | true value. |
paul@167 | 407 | """ |
paul@167 | 408 | |
paul@167 | 409 | if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@167 | 410 | self.all_objects.add(obj) |
paul@167 | 411 | |
paul@3 | 412 | # Namespace methods. |
paul@0 | 413 | |
paul@330 | 414 | def in_class(self, namespaces=None): |
paul@330 | 415 | namespaces = namespaces or self.namespaces |
paul@330 | 416 | return len(namespaces) > 1 and isinstance(namespaces[-2], Class) |
paul@330 | 417 | |
paul@694 | 418 | def store(self, name, obj, static_def=False): |
paul@12 | 419 | |
paul@694 | 420 | """ |
paul@694 | 421 | Record attribute or local 'name', storing 'obj'. Where 'static_def' is |
paul@694 | 422 | specified and set to a true value, the namespace will record a static |
paul@694 | 423 | definition for the given name. |
paul@694 | 424 | """ |
paul@12 | 425 | |
paul@339 | 426 | # Store in the module. |
paul@339 | 427 | |
paul@12 | 428 | if not self.namespaces: |
paul@342 | 429 | if self.in_loop and self.used_in_scope(name, "builtins"): |
paul@339 | 430 | raise InspectError("Name %r already used as a built-in." % name) |
paul@339 | 431 | else: |
paul@694 | 432 | ns = self |
paul@339 | 433 | |
paul@339 | 434 | # Or store locally. |
paul@339 | 435 | |
paul@12 | 436 | else: |
paul@341 | 437 | locals = self.namespaces[-1] |
paul@341 | 438 | |
paul@342 | 439 | if self.in_loop and locals.used_in_scope(name, "global") and not name in locals.globals: |
paul@341 | 440 | raise InspectError("Name %r already used as global." % name) |
paul@342 | 441 | elif self.in_loop and locals.used_in_scope(name, "builtins"): |
paul@341 | 442 | raise InspectError("Name %r already used as a built-in." % name) |
paul@341 | 443 | else: |
paul@694 | 444 | ns = locals |
paul@694 | 445 | |
paul@694 | 446 | ns.set(name, obj, not self.in_loop) |
paul@694 | 447 | if static_def: |
paul@694 | 448 | ns.get(name).static_assignments += 1 |
paul@707 | 449 | return ns.get(name) |
paul@12 | 450 | |
paul@85 | 451 | def store_lambda(self, obj): |
paul@91 | 452 | |
paul@91 | 453 | "Store a lambda function 'obj'." |
paul@91 | 454 | |
paul@167 | 455 | self.add_object(obj) |
paul@351 | 456 | self.get_namespace().add_lambda(obj) |
paul@85 | 457 | |
paul@94 | 458 | def store_module_attr(self, name, module): |
paul@94 | 459 | |
paul@94 | 460 | """ |
paul@94 | 461 | Record module attribute 'name' in the given 'module' using the current |
paul@94 | 462 | expression. |
paul@94 | 463 | """ |
paul@94 | 464 | |
paul@192 | 465 | module.set(name, self.expr, 0) |
paul@405 | 466 | self.use_specific_attribute(module.full_name(), name) |
paul@94 | 467 | |
paul@404 | 468 | def store_class_attr(self, name, cls): |
paul@91 | 469 | |
paul@94 | 470 | """ |
paul@404 | 471 | Record class attribute 'name' in the given class 'cls' using the current |
paul@94 | 472 | expression. |
paul@94 | 473 | """ |
paul@91 | 474 | |
paul@404 | 475 | cls.set(name, self.expr, 0) |
paul@405 | 476 | self.use_specific_attribute(cls.full_name(), name) |
paul@91 | 477 | |
paul@527 | 478 | def store_instance_attr(self, name, tentative=False): |
paul@12 | 479 | |
paul@527 | 480 | """ |
paul@527 | 481 | Record instance attribute 'name' in the current class. If 'tentative' is |
paul@527 | 482 | set to a true value, the instance attribute will be discarded if a class |
paul@527 | 483 | attribute is observed. |
paul@527 | 484 | """ |
paul@12 | 485 | |
paul@91 | 486 | if self.in_method: |
paul@15 | 487 | |
paul@12 | 488 | # Current namespace is the function. |
paul@12 | 489 | # Previous namespace is the class. |
paul@15 | 490 | |
paul@405 | 491 | cls = self.namespaces[-2] |
paul@527 | 492 | cls.add_instance_attribute(name, tentative) |
paul@406 | 493 | |
paul@406 | 494 | # NOTE: The instance attribute, although defined in a specific |
paul@406 | 495 | # NOTE: class, obviously appears in all descendant classes. |
paul@406 | 496 | |
paul@405 | 497 | self.use_specific_attribute(cls.full_name(), name) |
paul@0 | 498 | |
paul@272 | 499 | def get_namespace(self): |
paul@71 | 500 | |
paul@71 | 501 | "Return the parent (or most recent) namespace currently exposed." |
paul@71 | 502 | |
paul@12 | 503 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 504 | |
paul@623 | 505 | get_unit = get_namespace # compatibility method for error handling |
paul@623 | 506 | |
paul@557 | 507 | def use_name(self, name, node=None, value=None, ns=None): |
paul@323 | 508 | |
paul@323 | 509 | """ |
paul@323 | 510 | Use the given 'name' within the current namespace/unit, either in |
paul@323 | 511 | conjunction with a particular object (if 'node' is specified and not |
paul@323 | 512 | None) or unconditionally. |
paul@323 | 513 | """ |
paul@250 | 514 | |
paul@557 | 515 | unit = self.get_namespace() |
paul@557 | 516 | |
paul@557 | 517 | # Handle attribute usage situations within the current unit. |
paul@557 | 518 | |
paul@557 | 519 | if node is not None and isinstance(node, compiler.ast.Name) and ns is unit: |
paul@404 | 520 | self.use_attribute(node.name, name, value) |
paul@250 | 521 | |
paul@323 | 522 | # For general name usage, declare usage of the given name from this |
paul@323 | 523 | # particular unit. |
paul@323 | 524 | |
paul@323 | 525 | else: |
paul@404 | 526 | self.importer.use_name(name, unit.full_name(), value) |
paul@250 | 527 | |
paul@440 | 528 | def use_constant(self, const): |
paul@440 | 529 | |
paul@440 | 530 | "Use the given 'const' within the current namespace/unit." |
paul@440 | 531 | |
paul@440 | 532 | unit = self.get_namespace() |
paul@440 | 533 | self.importer.use_constant(const, unit.full_name()) |
paul@440 | 534 | |
paul@279 | 535 | # Attribute usage methods. |
paul@279 | 536 | # These are convenience methods which refer to the specific namespace's |
paul@279 | 537 | # implementation of these operations. |
paul@279 | 538 | |
paul@388 | 539 | def new_branchpoint(self, loop_node=None): |
paul@388 | 540 | self.get_namespace()._new_branchpoint(loop_node) |
paul@272 | 541 | |
paul@379 | 542 | def new_branch(self, node): |
paul@379 | 543 | self.get_namespace()._new_branch(node) |
paul@272 | 544 | |
paul@299 | 545 | def abandon_branch(self): |
paul@299 | 546 | self.get_namespace()._abandon_branch() |
paul@299 | 547 | |
paul@388 | 548 | def suspend_broken_branch(self): |
paul@388 | 549 | self.get_namespace()._suspend_broken_branch() |
paul@388 | 550 | |
paul@388 | 551 | def suspend_continuing_branch(self): |
paul@388 | 552 | self.get_namespace()._suspend_continuing_branch() |
paul@387 | 553 | |
paul@272 | 554 | def shelve_branch(self): |
paul@272 | 555 | self.get_namespace()._shelve_branch() |
paul@272 | 556 | |
paul@379 | 557 | def merge_branches(self): |
paul@379 | 558 | self.get_namespace()._merge_branches() |
paul@272 | 559 | |
paul@388 | 560 | def resume_broken_branches(self): |
paul@388 | 561 | self.get_namespace()._resume_broken_branches() |
paul@388 | 562 | |
paul@388 | 563 | def resume_continuing_branches(self): |
paul@388 | 564 | self.get_namespace()._resume_continuing_branches() |
paul@387 | 565 | |
paul@540 | 566 | def resume_abandoned_branches(self): |
paul@540 | 567 | self.get_namespace()._resume_abandoned_branches() |
paul@540 | 568 | |
paul@278 | 569 | def define_attribute_user(self, node): |
paul@279 | 570 | |
paul@279 | 571 | """ |
paul@279 | 572 | Define 'node' as the user of attributes, indicating the point where the |
paul@279 | 573 | user is defined. |
paul@279 | 574 | """ |
paul@279 | 575 | |
paul@278 | 576 | self.get_namespace()._define_attribute_user(node) |
paul@272 | 577 | |
paul@404 | 578 | def use_attribute(self, name, attrname, value=None): |
paul@323 | 579 | |
paul@404 | 580 | """ |
paul@404 | 581 | Note usage on the attribute user 'name' of the attribute 'attrname', |
paul@404 | 582 | noting an assignment if 'value' is specified. |
paul@404 | 583 | """ |
paul@323 | 584 | |
paul@404 | 585 | return self.get_namespace()._use_attribute(name, attrname, value) |
paul@272 | 586 | |
paul@434 | 587 | def use_specific_attribute(self, objname, attrname, from_name=None): |
paul@347 | 588 | |
paul@347 | 589 | """ |
paul@347 | 590 | Note usage on the object having the given 'objname' of the attribute |
paul@351 | 591 | 'attrname'. If 'objname' is None, the current namespace is chosen as the |
paul@351 | 592 | object providing the attribute. |
paul@347 | 593 | """ |
paul@347 | 594 | |
paul@434 | 595 | return self.get_namespace()._use_specific_attribute(objname, attrname, from_name) |
paul@347 | 596 | |
paul@556 | 597 | def define_attribute_accessor(self, name, attrname, node, value=None): |
paul@556 | 598 | |
paul@556 | 599 | """ |
paul@556 | 600 | Note applicable attribute users providing the given 'name' when |
paul@556 | 601 | accessing the given 'attrname' on the specified 'node', with the |
paul@556 | 602 | optional 'value' indicating an assignment. |
paul@556 | 603 | """ |
paul@556 | 604 | |
paul@556 | 605 | self.get_namespace()._define_attribute_accessor(name, attrname, node, value) |
paul@556 | 606 | |
paul@3 | 607 | # Visitor methods. |
paul@3 | 608 | |
paul@3 | 609 | def default(self, node, *args): |
paul@339 | 610 | raise InspectError("Node class %r is not supported." % node.__class__) |
paul@3 | 611 | |
paul@0 | 612 | def NOP(self, node): |
paul@21 | 613 | for n in node.getChildNodes(): |
paul@21 | 614 | self.dispatch(n) |
paul@0 | 615 | |
paul@299 | 616 | def NOP_ABANDON(self, node): |
paul@302 | 617 | self.NOP(node) |
paul@299 | 618 | self.abandon_branch() |
paul@299 | 619 | |
paul@462 | 620 | def TEST_NOP(self, node): |
paul@462 | 621 | self.use_name("__bool__", node) |
paul@462 | 622 | self.NOP(node) |
paul@462 | 623 | |
paul@82 | 624 | def OP(self, node): |
paul@82 | 625 | for n in node.getChildNodes(): |
paul@82 | 626 | self.dispatch(n) |
paul@448 | 627 | return make_instance() |
paul@1 | 628 | |
paul@462 | 629 | def TEST_OP(self, node): |
paul@462 | 630 | self.use_name("__bool__", node) |
paul@531 | 631 | self.new_branchpoint() |
paul@531 | 632 | |
paul@531 | 633 | # Propagate attribute usage to branches. |
paul@531 | 634 | # Each node starts a new conditional region, effectively making a deeply |
paul@531 | 635 | # nested collection of if-like statements. |
paul@531 | 636 | |
paul@531 | 637 | for n in node.nodes: |
paul@531 | 638 | self.new_branch(n) |
paul@531 | 639 | self.dispatch(n) |
paul@531 | 640 | |
paul@531 | 641 | # The nested regions must be terminated. |
paul@531 | 642 | |
paul@531 | 643 | for n in node.nodes: |
paul@531 | 644 | self.shelve_branch() |
paul@531 | 645 | |
paul@531 | 646 | self.merge_branches() |
paul@531 | 647 | return make_instance() |
paul@462 | 648 | |
paul@314 | 649 | # Generic support for classes of operations. |
paul@314 | 650 | |
paul@581 | 651 | def _ensureOperators(self, node): |
paul@358 | 652 | attr, scope, namespace = self._get_with_scope("$operator") |
paul@358 | 653 | if attr is None: |
paul@581 | 654 | module = node._module |
paul@581 | 655 | module.complete() |
paul@354 | 656 | self["$operator"] = module |
paul@358 | 657 | else: |
paul@358 | 658 | module = attr.get_value() |
paul@358 | 659 | return module |
paul@354 | 660 | |
paul@374 | 661 | def _visitOperator(self, node, operator_name=None): |
paul@172 | 662 | |
paul@358 | 663 | "Accounting method for the operator 'node'." |
paul@172 | 664 | |
paul@581 | 665 | operator_module = self._ensureOperators(node) |
paul@374 | 666 | operator_fn = operator_functions[operator_name or node.__class__.__name__] |
paul@354 | 667 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@172 | 668 | return self.OP(node) |
paul@172 | 669 | |
paul@372 | 670 | def _visitAttr(self, expr, attrname, node): |
paul@372 | 671 | |
paul@468 | 672 | """ |
paul@468 | 673 | Process the attribute provided by the given 'expr' with the given |
paul@468 | 674 | 'attrname' and involving the given 'node'. |
paul@468 | 675 | """ |
paul@468 | 676 | |
paul@372 | 677 | # Attempt to identify the nature of the attribute. |
paul@372 | 678 | |
paul@717 | 679 | if isinstance(expr, BaseAttr): |
paul@728 | 680 | if isinstance(expr, LocalAttr): |
paul@728 | 681 | value = expr.attr.get_value() |
paul@728 | 682 | else: |
paul@728 | 683 | value = expr.get_value() |
paul@372 | 684 | |
paul@372 | 685 | # Get the attribute and record its usage. |
paul@559 | 686 | # NOTE: Need to provide concrete values for things like base classes |
paul@559 | 687 | # NOTE: while also handling module attribute modification. |
paul@372 | 688 | |
paul@599 | 689 | # Only specific class attributes are detected here since class |
paul@599 | 690 | # attribute finalisation has not yet occurred. |
paul@599 | 691 | |
paul@559 | 692 | if isinstance(value, (Class, Module)): |
paul@401 | 693 | |
paul@401 | 694 | # Check for class.__class__. |
paul@401 | 695 | |
paul@401 | 696 | if attrname == "__class__" and isinstance(value, Class): |
paul@618 | 697 | attr = get_constant_class("type") |
paul@401 | 698 | else: |
paul@448 | 699 | attr = value.get(attrname) or make_instance() |
paul@372 | 700 | self.use_specific_attribute(value.full_name(), attrname) |
paul@372 | 701 | |
paul@372 | 702 | elif isinstance(value, UnresolvedName): |
paul@372 | 703 | attr = UnresolvedName(attrname, value.full_name(), self) |
paul@372 | 704 | |
paul@467 | 705 | # The actual attribute is not readily identifiable and is assumed |
paul@467 | 706 | # to be an instance. |
paul@467 | 707 | |
paul@372 | 708 | else: |
paul@527 | 709 | |
paul@527 | 710 | # Record any instance attributes. |
paul@527 | 711 | |
paul@527 | 712 | if expr.name == "self": |
paul@527 | 713 | self.store_instance_attr(attrname, tentative=True) |
paul@527 | 714 | |
paul@448 | 715 | attr = make_instance() |
paul@372 | 716 | |
paul@372 | 717 | # Note usage of the attribute where a local is involved. |
paul@372 | 718 | |
paul@404 | 719 | self._visitAttrUser(expr, attrname, node) |
paul@372 | 720 | |
paul@619 | 721 | # Constants provide specific kinds of expressions. |
paul@619 | 722 | # NOTE: If attributes are accessed on a pre-made, but not yet defined |
paul@619 | 723 | # NOTE: class, no useful attribute will be available. |
paul@619 | 724 | |
paul@619 | 725 | elif isinstance(expr, Const): |
paul@619 | 726 | attr = get_constant_class(expr.get_class_name()).all_attributes().get(attrname) or make_instance() |
paul@619 | 727 | |
paul@467 | 728 | # No particular attribute has been identified, thus a general instance |
paul@467 | 729 | # is assumed. |
paul@467 | 730 | |
paul@372 | 731 | else: |
paul@448 | 732 | attr = make_instance() |
paul@372 | 733 | self.use_name(attrname, node) |
paul@372 | 734 | |
paul@372 | 735 | return attr |
paul@358 | 736 | |
paul@404 | 737 | def _visitAttrUser(self, expr, attrname, node, value=None): |
paul@404 | 738 | |
paul@404 | 739 | """ |
paul@404 | 740 | Note usage of the attribute provided by 'expr' with the given 'attrname' |
paul@404 | 741 | where a local is involved, annotating the given 'node'. If the optional |
paul@404 | 742 | 'value' is given, note an assignment for future effects on attributes |
paul@404 | 743 | where such attributes are inferred from the usage. |
paul@404 | 744 | """ |
paul@404 | 745 | |
paul@571 | 746 | # Access to attributes via a local in functions, classes or modules. |
paul@571 | 747 | # Since module-level locals are globals that can be modified |
paul@571 | 748 | # independently of the namespace, any attribute usage observations made |
paul@571 | 749 | # here may be revoked later if such modification is thought to occur. |
paul@467 | 750 | |
paul@571 | 751 | if expr.parent is self.get_namespace(): |
paul@556 | 752 | self.define_attribute_accessor(expr.name, attrname, node, value) |
paul@404 | 753 | else: |
paul@557 | 754 | self.use_name(attrname, node.expr, value, ns=expr.parent) |
paul@404 | 755 | |
paul@439 | 756 | def _visitConst(self, value): |
paul@439 | 757 | |
paul@439 | 758 | """ |
paul@439 | 759 | Register the constant given by 'value', if necessary, returning the |
paul@439 | 760 | resulting object. The type name is noted as being used, thus preserving |
paul@439 | 761 | the class in any generated program. |
paul@439 | 762 | """ |
paul@439 | 763 | |
paul@439 | 764 | self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value)) |
paul@440 | 765 | const = self.importer.make_constant(value) |
paul@440 | 766 | self.use_constant(const) |
paul@440 | 767 | return const |
paul@439 | 768 | |
paul@85 | 769 | def _visitFunction(self, node, name): |
paul@85 | 770 | |
paul@85 | 771 | """ |
paul@85 | 772 | Return a function object for the function defined by 'node' with the |
paul@85 | 773 | given 'name'. If a lambda expression is being visited, 'name' should be |
paul@85 | 774 | None. |
paul@85 | 775 | """ |
paul@85 | 776 | |
paul@233 | 777 | # Define the function object. |
paul@233 | 778 | |
paul@665 | 779 | function = get_function( |
paul@85 | 780 | name, |
paul@272 | 781 | self.get_namespace(), |
paul@85 | 782 | node.argnames, |
paul@85 | 783 | node.defaults, |
paul@85 | 784 | (node.flags & 4 != 0), |
paul@85 | 785 | (node.flags & 8 != 0), |
paul@85 | 786 | self, |
paul@85 | 787 | node |
paul@85 | 788 | ) |
paul@85 | 789 | |
paul@233 | 790 | self.add_object(function, any_scope=1) |
paul@233 | 791 | |
paul@85 | 792 | # Make a back reference from the node for code generation. |
paul@85 | 793 | |
paul@85 | 794 | node.unit = function |
paul@85 | 795 | |
paul@85 | 796 | # Process the defaults. |
paul@85 | 797 | |
paul@85 | 798 | for n in node.defaults: |
paul@85 | 799 | self.expr = self.dispatch(n) |
paul@192 | 800 | function.store_default(self.expr) |
paul@85 | 801 | |
paul@368 | 802 | # Note attribute usage where tuple parameters are involved. |
paul@368 | 803 | |
paul@368 | 804 | if function.tuple_parameters(): |
paul@368 | 805 | self.use_name("__getitem__", node) |
paul@368 | 806 | |
paul@368 | 807 | # Record the namespace context of the function for later processing. |
paul@368 | 808 | |
paul@235 | 809 | self.functions.append((node, self.namespaces + [function])) |
paul@85 | 810 | |
paul@330 | 811 | # Store the function. |
paul@330 | 812 | |
paul@85 | 813 | if name is not None: |
paul@720 | 814 | self.define_attribute_user(compiler.ast.AssName(name, "OP_ASSIGN")) |
paul@694 | 815 | self.store(name, function, static_def=True) |
paul@85 | 816 | else: |
paul@85 | 817 | self.store_lambda(function) |
paul@167 | 818 | |
paul@330 | 819 | # Test the defaults and assess whether an dynamic object will result. |
paul@330 | 820 | |
paul@331 | 821 | function.make_dynamic() |
paul@331 | 822 | return function |
paul@85 | 823 | |
paul@235 | 824 | def _visitFunctionBody(self, node, namespaces): |
paul@235 | 825 | |
paul@235 | 826 | "Enter the function." |
paul@235 | 827 | |
paul@235 | 828 | # Current namespace is the function. |
paul@235 | 829 | # Previous namespace is the class. |
paul@235 | 830 | |
paul@330 | 831 | if self.in_class(namespaces): |
paul@591 | 832 | self.in_method = True |
paul@235 | 833 | |
paul@331 | 834 | in_function = self.in_function |
paul@331 | 835 | in_loop = self.in_loop |
paul@591 | 836 | self.in_function = True |
paul@591 | 837 | self.in_loop = False |
paul@331 | 838 | |
paul@235 | 839 | self.namespaces = namespaces |
paul@235 | 840 | self.dispatch(node.code) |
paul@235 | 841 | |
paul@331 | 842 | self.in_loop = in_loop |
paul@331 | 843 | self.in_function = in_function |
paul@591 | 844 | self.in_method = False |
paul@235 | 845 | |
paul@172 | 846 | # Specific handler methods. |
paul@172 | 847 | |
paul@580 | 848 | visitAdd = _visitOperator |
paul@82 | 849 | |
paul@462 | 850 | visitAnd = TEST_OP |
paul@0 | 851 | |
paul@4 | 852 | visitAssert = NOP |
paul@4 | 853 | |
paul@0 | 854 | def visitAssign(self, node): |
paul@0 | 855 | self.expr = self.dispatch(node.expr) |
paul@591 | 856 | self.in_assignment = True |
paul@0 | 857 | for n in node.nodes: |
paul@0 | 858 | self.dispatch(n) |
paul@591 | 859 | self.in_assignment = False |
paul@0 | 860 | |
paul@0 | 861 | def visitAssAttr(self, node): |
paul@0 | 862 | expr = self.dispatch(node.expr) |
paul@404 | 863 | attrname = node.attrname |
paul@272 | 864 | |
paul@272 | 865 | # Record the attribute on the presumed target. |
paul@272 | 866 | |
paul@717 | 867 | if isinstance(expr, BaseAttr): |
paul@728 | 868 | if isinstance(expr, LocalAttr): |
paul@728 | 869 | value = expr.attr.get_value() |
paul@728 | 870 | else: |
paul@728 | 871 | value = expr.get_value() |
paul@404 | 872 | |
paul@94 | 873 | if expr.name == "self": |
paul@404 | 874 | self.store_instance_attr(attrname) |
paul@556 | 875 | self.use_attribute(expr.name, attrname, value) |
paul@488 | 876 | self._visitAttrUser(expr, attrname, node, self.expr) |
paul@404 | 877 | |
paul@602 | 878 | # No definite attribute can be identified, since the instance |
paul@602 | 879 | # being accessed may be a subclass of the method's class. |
paul@602 | 880 | |
paul@602 | 881 | attr = make_instance() |
paul@602 | 882 | |
paul@404 | 883 | elif isinstance(value, Module): |
paul@404 | 884 | self.store_module_attr(attrname, value) |
paul@602 | 885 | attr = value.get(attrname) |
paul@272 | 886 | |
paul@404 | 887 | elif isinstance(value, Class): |
paul@404 | 888 | self.store_class_attr(attrname, value) |
paul@602 | 889 | attr = value.get(attrname) |
paul@404 | 890 | |
paul@285 | 891 | # Note usage of the attribute where a local is involved. |
paul@272 | 892 | |
paul@404 | 893 | else: |
paul@404 | 894 | self._visitAttrUser(expr, attrname, node, self.expr) |
paul@602 | 895 | attr = make_instance() |
paul@381 | 896 | |
paul@404 | 897 | else: |
paul@404 | 898 | self.use_name(attrname, node) |
paul@602 | 899 | attr = make_instance() |
paul@602 | 900 | |
paul@639 | 901 | node._expr = expr |
paul@602 | 902 | node._attr = attr |
paul@272 | 903 | |
paul@0 | 904 | def visitAssList(self, node): |
paul@204 | 905 | |
paul@204 | 906 | # Declare names which will be used by generated code. |
paul@204 | 907 | |
paul@323 | 908 | self.use_name("__getitem__", node) |
paul@204 | 909 | |
paul@204 | 910 | # Process the assignment. |
paul@204 | 911 | |
paul@713 | 912 | expr = self.expr |
paul@713 | 913 | self.expr = make_instance() # each element is a result of an item access |
paul@713 | 914 | |
paul@139 | 915 | for i, n in enumerate(node.nodes): |
paul@0 | 916 | self.dispatch(n) |
paul@439 | 917 | self._visitConst(i) # for __getitem__(i) at run-time |
paul@0 | 918 | |
paul@713 | 919 | self.expr = expr |
paul@713 | 920 | |
paul@0 | 921 | def visitAssName(self, node): |
paul@599 | 922 | if node.flags == "OP_DELETE": |
paul@507 | 923 | print >>sys.stderr, "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) |
paul@339 | 924 | #raise InspectError("Deletion of attribute %r is not supported." % node.name) |
paul@599 | 925 | self._visitAssName(node) |
paul@274 | 926 | |
paul@599 | 927 | def _visitAssName(self, node): |
paul@278 | 928 | self.define_attribute_user(node) |
paul@712 | 929 | self.store(node.name, self.expr) |
paul@285 | 930 | |
paul@285 | 931 | # Ensure the presence of the given name in this namespace. |
paul@431 | 932 | # NOTE: Consider not registering assignments involving methods, since |
paul@431 | 933 | # NOTE: this is merely creating aliases for such methods. |
paul@285 | 934 | |
paul@434 | 935 | if isinstance(self.get_namespace(), (Class, Module)): |
paul@728 | 936 | if isinstance(self.expr, BaseAttr): |
paul@728 | 937 | if isinstance(self.expr, LocalAttr): |
paul@728 | 938 | value = self.expr.attr.get_value() |
paul@728 | 939 | else: |
paul@728 | 940 | value = self.expr.get_value() |
paul@728 | 941 | else: |
paul@728 | 942 | value = None |
paul@728 | 943 | if not value or not isinstance(value, Function): |
paul@434 | 944 | self.use_specific_attribute(None, node.name) |
paul@434 | 945 | else: |
paul@728 | 946 | fn = value |
paul@434 | 947 | ns = self.get_namespace().full_name() |
paul@434 | 948 | self.use_specific_attribute(fn.parent.full_name(), fn.name, "%s.%s" % (ns, node.name)) |
paul@434 | 949 | |
paul@0 | 950 | visitAssTuple = visitAssList |
paul@0 | 951 | |
paul@172 | 952 | def visitAugAssign(self, node): |
paul@172 | 953 | |
paul@172 | 954 | # Accounting. |
paul@172 | 955 | |
paul@354 | 956 | operator_fn = operator_functions.get(node.op) |
paul@581 | 957 | operator_module = self._ensureOperators(node) |
paul@354 | 958 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@244 | 959 | |
paul@244 | 960 | # Process the assignment. |
paul@244 | 961 | |
paul@244 | 962 | self.expr = self.dispatch(node.expr) |
paul@744 | 963 | self.dispatch(node.node) |
paul@244 | 964 | |
paul@363 | 965 | # NOTE: Slices and subscripts are supported by __setitem__(slice) and |
paul@363 | 966 | # NOTE: not __setslice__. |
paul@244 | 967 | |
paul@244 | 968 | if isinstance(node.node, compiler.ast.Name): |
paul@599 | 969 | self._visitAssName(node.node) |
paul@244 | 970 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@244 | 971 | self.visitAssAttr(node.node) |
paul@244 | 972 | else: |
paul@363 | 973 | self.use_specific_attribute("__builtins__", "slice") |
paul@363 | 974 | self.use_name("__setitem__", node) |
paul@244 | 975 | |
paul@82 | 976 | visitBackquote = OP |
paul@4 | 977 | |
paul@580 | 978 | visitBitand = _visitOperator |
paul@2 | 979 | |
paul@580 | 980 | visitBitor = _visitOperator |
paul@2 | 981 | |
paul@580 | 982 | visitBitxor = _visitOperator |
paul@4 | 983 | |
paul@388 | 984 | def visitBreak(self, node): |
paul@388 | 985 | self.NOP(node) |
paul@388 | 986 | self.suspend_broken_branch() |
paul@4 | 987 | |
paul@82 | 988 | visitCallFunc = OP |
paul@0 | 989 | |
paul@0 | 990 | def visitClass(self, node): |
paul@91 | 991 | |
paul@91 | 992 | """ |
paul@91 | 993 | Register the class at the given 'node' subject to the restrictions |
paul@91 | 994 | mentioned in the module docstring. |
paul@91 | 995 | """ |
paul@91 | 996 | |
paul@12 | 997 | if self.namespaces: |
paul@507 | 998 | print >>sys.stderr, "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@442 | 999 | return |
paul@0 | 1000 | else: |
paul@331 | 1001 | if self.in_loop: |
paul@507 | 1002 | print >>sys.stderr, "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) |
paul@331 | 1003 | |
paul@394 | 1004 | cls = get_class(node.name, self.get_namespace(), self, node) |
paul@37 | 1005 | |
paul@349 | 1006 | # Make a back reference from the node for code generation. |
paul@349 | 1007 | |
paul@349 | 1008 | node.unit = cls |
paul@349 | 1009 | |
paul@349 | 1010 | # Process base classes in the context of the class's namespace. |
paul@349 | 1011 | # This confines references to such classes to the class instead of |
paul@349 | 1012 | # the namespace in which it is defined. |
paul@349 | 1013 | |
paul@349 | 1014 | self.namespaces.append(cls) |
paul@349 | 1015 | |
paul@37 | 1016 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 1017 | # definitions of classes. |
paul@37 | 1018 | |
paul@0 | 1019 | for base in node.bases: |
paul@36 | 1020 | expr = self.dispatch(base) |
paul@349 | 1021 | |
paul@349 | 1022 | # Each base class must be constant and known at compile-time. |
paul@349 | 1023 | |
paul@717 | 1024 | if isinstance(expr, BaseAttr): |
paul@728 | 1025 | if isinstance(expr, LocalAttr): |
paul@728 | 1026 | value = expr.attr.get_value() |
paul@728 | 1027 | else: |
paul@728 | 1028 | value = expr.get_value() |
paul@728 | 1029 | |
paul@728 | 1030 | if not value: |
paul@442 | 1031 | raise InspectError("Base class %r for %r is not constant: %r" % (base, cls.full_name(), expr)) |
paul@728 | 1032 | elif not isinstance(value, Class): |
paul@728 | 1033 | raise InspectError("Base class %r for %r is not a class: %r" % (base, cls.full_name(), value)) |
paul@36 | 1034 | else: |
paul@728 | 1035 | cls.add_base(value) |
paul@349 | 1036 | |
paul@349 | 1037 | # Where no expression value is available, the base class is |
paul@349 | 1038 | # not identifiable. |
paul@349 | 1039 | |
paul@349 | 1040 | else: |
paul@339 | 1041 | raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 1042 | |
paul@59 | 1043 | # NOTE: Potentially dubious measure to permit __init__ availability. |
paul@59 | 1044 | # If no bases exist, adopt the 'object' class. |
paul@59 | 1045 | |
paul@580 | 1046 | if not node.bases and not (self.name == "__builtins__" and node.name == "object"): |
paul@59 | 1047 | expr = self.dispatch(compiler.ast.Name("object")) |
paul@192 | 1048 | cls.add_base(expr.get_value()) |
paul@59 | 1049 | |
paul@349 | 1050 | # Make an entry for the class in the parent namespace. |
paul@21 | 1051 | |
paul@349 | 1052 | self.namespaces.pop() |
paul@720 | 1053 | self.define_attribute_user(compiler.ast.AssName(node.name, "OP_ASSIGN")) |
paul@694 | 1054 | self.store(node.name, cls, static_def=True) |
paul@167 | 1055 | self.add_object(cls) |
paul@0 | 1056 | |
paul@349 | 1057 | # Process the class body in its own namespace. |
paul@439 | 1058 | # Add __name__ to the namespace. |
paul@59 | 1059 | |
paul@12 | 1060 | self.namespaces.append(cls) |
paul@439 | 1061 | self.store("__name__", self._visitConst(node.name)) |
paul@0 | 1062 | self.dispatch(node.code) |
paul@12 | 1063 | self.namespaces.pop() |
paul@0 | 1064 | |
paul@349 | 1065 | cls.finalise_attribute_usage() |
paul@82 | 1066 | return cls |
paul@0 | 1067 | |
paul@172 | 1068 | def visitCompare(self, node): |
paul@172 | 1069 | |
paul@172 | 1070 | # Accounting. |
paul@172 | 1071 | # NOTE: Replicates some code in micropython.ast.visitCompare. |
paul@172 | 1072 | |
paul@462 | 1073 | self.use_name("__bool__", node) |
paul@462 | 1074 | |
paul@323 | 1075 | this_node = node |
paul@323 | 1076 | |
paul@172 | 1077 | for op in node.ops: |
paul@172 | 1078 | op_name, next_node = op |
paul@323 | 1079 | |
paul@354 | 1080 | # Define name/attribute usage. |
paul@354 | 1081 | # Get the applicable operation. |
paul@323 | 1082 | |
paul@354 | 1083 | operator_fn = operator_functions.get(op_name) |
paul@354 | 1084 | |
paul@354 | 1085 | # For operators, reference the specific function involved. |
paul@323 | 1086 | |
paul@354 | 1087 | if operator_fn is not None: |
paul@581 | 1088 | operator_module = self._ensureOperators(node) |
paul@354 | 1089 | self.use_specific_attribute(operator_module.full_name(), operator_fn) |
paul@323 | 1090 | |
paul@354 | 1091 | # Define __contains__ usage on the next node. |
paul@354 | 1092 | |
paul@172 | 1093 | elif op_name.endswith("in"): |
paul@323 | 1094 | self.use_name("__contains__", next_node) |
paul@323 | 1095 | |
paul@323 | 1096 | this_node = next_node |
paul@172 | 1097 | |
paul@172 | 1098 | return self.OP(node) |
paul@4 | 1099 | |
paul@21 | 1100 | def visitConst(self, node): |
paul@439 | 1101 | return self._visitConst(node.value) |
paul@0 | 1102 | |
paul@388 | 1103 | def visitContinue(self, node): |
paul@388 | 1104 | self.NOP(node) |
paul@388 | 1105 | self.suspend_continuing_branch() |
paul@4 | 1106 | |
paul@4 | 1107 | visitDecorators = NOP |
paul@4 | 1108 | |
paul@697 | 1109 | def visitDict(self, node): |
paul@697 | 1110 | self.use_specific_attribute("__builtins__", "dict") |
paul@697 | 1111 | return self.OP(node) |
paul@0 | 1112 | |
paul@0 | 1113 | visitDiscard = NOP |
paul@0 | 1114 | |
paul@580 | 1115 | visitDiv = _visitOperator |
paul@4 | 1116 | |
paul@4 | 1117 | visitEllipsis = NOP |
paul@4 | 1118 | |
paul@4 | 1119 | visitExec = NOP |
paul@4 | 1120 | |
paul@82 | 1121 | visitExpression = OP |
paul@4 | 1122 | |
paul@580 | 1123 | visitFloorDiv = _visitOperator |
paul@4 | 1124 | |
paul@34 | 1125 | def visitFor(self, node): |
paul@388 | 1126 | self.new_branchpoint(node) |
paul@204 | 1127 | |
paul@204 | 1128 | # Declare names which will be used by generated code. |
paul@204 | 1129 | |
paul@323 | 1130 | self.use_name("__iter__", node.list) |
paul@250 | 1131 | self.use_name("next") |
paul@345 | 1132 | self.use_name("StopIteration") |
paul@204 | 1133 | |
paul@331 | 1134 | in_loop = self.in_loop |
paul@591 | 1135 | self.in_loop = True |
paul@341 | 1136 | self.dispatch(node.list) |
paul@442 | 1137 | |
paul@442 | 1138 | # NOTE: Could generate AST nodes for the actual operations instead of |
paul@442 | 1139 | # NOTE: manually generating code in micropython.ast. |
paul@442 | 1140 | |
paul@448 | 1141 | self.expr = make_instance() # each element is a result of a function call |
paul@272 | 1142 | self.dispatch(node.assign) |
paul@272 | 1143 | |
paul@204 | 1144 | # Enter the loop. |
paul@275 | 1145 | # Propagate attribute usage to branches. |
paul@204 | 1146 | |
paul@379 | 1147 | self.new_branch(node) |
paul@272 | 1148 | self.dispatch(node.body) |
paul@385 | 1149 | |
paul@388 | 1150 | self.resume_continuing_branches() |
paul@385 | 1151 | |
paul@272 | 1152 | self.shelve_branch() |
paul@362 | 1153 | |
paul@331 | 1154 | self.in_loop = in_loop |
paul@272 | 1155 | |
paul@386 | 1156 | # A null branch is used to record a path around the loop. |
paul@275 | 1157 | |
paul@379 | 1158 | self.new_branch(node.else_ or NullBranch()) |
paul@379 | 1159 | self.shelve_branch() |
paul@272 | 1160 | |
paul@379 | 1161 | self.merge_branches() |
paul@386 | 1162 | |
paul@386 | 1163 | # The else clause is evaluated outside any branch. |
paul@386 | 1164 | |
paul@386 | 1165 | if node.else_ is not None: |
paul@386 | 1166 | self.dispatch(node.else_) |
paul@386 | 1167 | |
paul@387 | 1168 | # Any suspended branches from the loop can now be resumed. |
paul@387 | 1169 | |
paul@388 | 1170 | self.resume_broken_branches() |
paul@387 | 1171 | |
paul@0 | 1172 | def visitFrom(self, node): |
paul@684 | 1173 | modname, names = get_module_name(node, self) |
paul@683 | 1174 | |
paul@683 | 1175 | if not modname: |
paul@683 | 1176 | return self._visitImport(names) |
paul@683 | 1177 | |
paul@682 | 1178 | module = self.complete_import(modname, True) |
paul@0 | 1179 | |
paul@0 | 1180 | for name, alias in node.names: |
paul@563 | 1181 | |
paul@563 | 1182 | # For specific names, obtain and store referenced objects using |
paul@563 | 1183 | # the name or any alias provided in the current namespace. |
paul@563 | 1184 | |
paul@0 | 1185 | if name != "*": |
paul@545 | 1186 | if module: |
paul@501 | 1187 | |
paul@501 | 1188 | # Missing names may refer to submodules. |
paul@501 | 1189 | |
paul@682 | 1190 | submodule = self.complete_import(modname + "." + name, True) |
paul@581 | 1191 | if submodule: |
paul@581 | 1192 | if not module.has_key(name): |
paul@501 | 1193 | module.store(name, submodule) |
paul@501 | 1194 | |
paul@501 | 1195 | # Complete the import if the name was found. |
paul@501 | 1196 | |
paul@501 | 1197 | if module.has_key(name): |
paul@501 | 1198 | attr = module[name] |
paul@501 | 1199 | self.store(alias or name, attr) |
paul@501 | 1200 | self.use_specific_attribute(module.full_name(), name) |
paul@501 | 1201 | continue |
paul@24 | 1202 | |
paul@24 | 1203 | # Support the import of names from missing modules. |
paul@24 | 1204 | |
paul@682 | 1205 | self.store(alias or name, UnresolvedName(name, modname, self)) |
paul@501 | 1206 | |
paul@563 | 1207 | # For wildcards, obtain and store all objects from a module in the |
paul@563 | 1208 | # current namespace. |
paul@563 | 1209 | |
paul@3 | 1210 | else: |
paul@545 | 1211 | if module: |
paul@285 | 1212 | for n in module.keys(): |
paul@24 | 1213 | attr = module[n] |
paul@192 | 1214 | self.store(n, attr) |
paul@364 | 1215 | self.use_specific_attribute(module.full_name(), n) |
paul@0 | 1216 | |
paul@0 | 1217 | def visitFunction(self, node): |
paul@85 | 1218 | return self._visitFunction(node, node.name) |
paul@0 | 1219 | |
paul@745 | 1220 | # NOTE: GenExpr support is very similar to ListComp support. |
paul@745 | 1221 | |
paul@745 | 1222 | def visitGenExpr(self, node): |
paul@745 | 1223 | self.dispatch(node.code) |
paul@745 | 1224 | |
paul@745 | 1225 | def visitGenExprInner(self, node): |
paul@745 | 1226 | |
paul@745 | 1227 | # Note that explicit dispatch is performed. |
paul@745 | 1228 | |
paul@745 | 1229 | if node.quals: |
paul@745 | 1230 | self.visitGenExprFor(node.quals[0], node.quals[1:], node.expr) |
paul@745 | 1231 | return make_instance() |
paul@745 | 1232 | |
paul@745 | 1233 | def visitGenExprFor(self, node, following_quals, expr): |
paul@745 | 1234 | self.new_branchpoint() |
paul@745 | 1235 | |
paul@745 | 1236 | # Declare names which will be used by generated code. |
paul@4 | 1237 | |
paul@745 | 1238 | self.use_name("__iter__", node.iter) |
paul@745 | 1239 | self.use_name("next") |
paul@745 | 1240 | |
paul@745 | 1241 | in_loop = self.in_loop |
paul@745 | 1242 | self.in_loop = True |
paul@745 | 1243 | self.dispatch(node.iter) |
paul@745 | 1244 | |
paul@745 | 1245 | # NOTE: Could generate AST nodes for the actual operations instead of |
paul@745 | 1246 | # NOTE: manually generating code in micropython.ast. |
paul@745 | 1247 | |
paul@745 | 1248 | self.expr = make_instance() # each element is a result of a function call |
paul@745 | 1249 | self.dispatch(node.assign) |
paul@745 | 1250 | |
paul@745 | 1251 | # Enter the loop. |
paul@745 | 1252 | # Propagate attribute usage to branches. |
paul@745 | 1253 | |
paul@745 | 1254 | self.new_branch(node) |
paul@745 | 1255 | |
paul@745 | 1256 | # Note that explicit dispatch is performed. |
paul@4 | 1257 | |
paul@745 | 1258 | if node.ifs: |
paul@745 | 1259 | self.visitGenExprIf(node.ifs[0], node.ifs[1:], following_quals, expr) |
paul@745 | 1260 | elif following_quals: |
paul@745 | 1261 | self.visitGenExprFor(following_quals[0], following_quals[1:], expr) |
paul@745 | 1262 | else: |
paul@745 | 1263 | self.dispatch(expr) |
paul@745 | 1264 | |
paul@745 | 1265 | self.shelve_branch() |
paul@745 | 1266 | self.in_loop = in_loop |
paul@745 | 1267 | |
paul@745 | 1268 | self.merge_branches() |
paul@745 | 1269 | |
paul@745 | 1270 | def visitGenExprIf(self, node, following_ifs, following_quals, expr): |
paul@745 | 1271 | self.use_name("__bool__", node) |
paul@745 | 1272 | self.new_branchpoint() |
paul@745 | 1273 | |
paul@745 | 1274 | # Propagate attribute usage to branches. |
paul@4 | 1275 | |
paul@745 | 1276 | self.dispatch(node.test) |
paul@745 | 1277 | |
paul@745 | 1278 | # Note that explicit dispatch is performed. |
paul@745 | 1279 | |
paul@745 | 1280 | if following_ifs: |
paul@745 | 1281 | self.visitGenExprIf(following_ifs[0], following_ifs[1:], following_quals, expr) |
paul@745 | 1282 | elif following_quals: |
paul@745 | 1283 | self.visitGenExprFor(following_quals[0], following_quals[1:], expr) |
paul@745 | 1284 | else: |
paul@745 | 1285 | self.new_branch(expr) |
paul@745 | 1286 | self.dispatch(expr) |
paul@745 | 1287 | self.shelve_branch() |
paul@745 | 1288 | |
paul@745 | 1289 | # Maintain a branch for the else clause. |
paul@745 | 1290 | |
paul@745 | 1291 | self.new_branch(NullBranch()) |
paul@745 | 1292 | self.shelve_branch() |
paul@745 | 1293 | |
paul@745 | 1294 | self.merge_branches() |
paul@4 | 1295 | |
paul@0 | 1296 | def visitGetattr(self, node): |
paul@602 | 1297 | node._expr = self.dispatch(node.expr) |
paul@602 | 1298 | node._attr = self._visitAttr(node._expr, node.attrname, node) |
paul@556 | 1299 | return node._attr |
paul@4 | 1300 | |
paul@12 | 1301 | def visitGlobal(self, node): |
paul@12 | 1302 | if self.namespaces: |
paul@12 | 1303 | for name in node.names: |
paul@178 | 1304 | ns = self.namespaces[-1] |
paul@178 | 1305 | if not ns.make_global(name): |
paul@339 | 1306 | raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) |
paul@37 | 1307 | |
paul@236 | 1308 | # The name is recorded in an earlier process. |
paul@0 | 1309 | |
paul@0 | 1310 | def visitIf(self, node): |
paul@462 | 1311 | self.use_name("__bool__", node) |
paul@272 | 1312 | self.new_branchpoint() |
paul@272 | 1313 | |
paul@275 | 1314 | # Propagate attribute usage to branches. |
paul@275 | 1315 | |
paul@0 | 1316 | for test, body in node.tests: |
paul@202 | 1317 | self.dispatch(test) |
paul@272 | 1318 | |
paul@377 | 1319 | self.new_branch(body) |
paul@0 | 1320 | self.dispatch(body) |
paul@272 | 1321 | self.shelve_branch() |
paul@272 | 1322 | |
paul@377 | 1323 | # Maintain a branch for the else clause. |
paul@275 | 1324 | |
paul@379 | 1325 | self.new_branch(node.else_ or NullBranch()) |
paul@0 | 1326 | if node.else_ is not None: |
paul@0 | 1327 | self.dispatch(node.else_) |
paul@379 | 1328 | self.shelve_branch() |
paul@272 | 1329 | |
paul@272 | 1330 | self.merge_branches() |
paul@442 | 1331 | |
paul@442 | 1332 | def visitIfExp(self, node): |
paul@462 | 1333 | self.use_name("__bool__", node) |
paul@442 | 1334 | self.new_branchpoint() |
paul@442 | 1335 | |
paul@442 | 1336 | # Propagate attribute usage to branches. |
paul@442 | 1337 | |
paul@442 | 1338 | self.dispatch(node.test) |
paul@0 | 1339 | |
paul@442 | 1340 | self.new_branch(node.then) |
paul@442 | 1341 | self.dispatch(node.then) |
paul@442 | 1342 | self.shelve_branch() |
paul@442 | 1343 | |
paul@442 | 1344 | self.new_branch(node.else_) |
paul@442 | 1345 | self.dispatch(node.else_) |
paul@442 | 1346 | self.shelve_branch() |
paul@442 | 1347 | |
paul@442 | 1348 | self.merge_branches() |
paul@448 | 1349 | return make_instance() # either outcome is possible |
paul@12 | 1350 | |
paul@0 | 1351 | def visitImport(self, node): |
paul@683 | 1352 | self._visitImport(node.names) |
paul@683 | 1353 | |
paul@683 | 1354 | def _visitImport(self, names): |
paul@683 | 1355 | for name, alias in names: |
paul@581 | 1356 | module = self.complete_import(name, alias) |
paul@2 | 1357 | if alias is not None: |
paul@581 | 1358 | self.store(alias, module or UnresolvedName(None, name, self)) |
paul@2 | 1359 | else: |
paul@581 | 1360 | name_used = name.split(".")[0] |
paul@581 | 1361 | self.store(name_used, module or UnresolvedName(None, name_used, self)) |
paul@578 | 1362 | |
paul@580 | 1363 | visitInvert = _visitOperator |
paul@4 | 1364 | |
paul@59 | 1365 | def visitKeyword(self, node): |
paul@59 | 1366 | self.dispatch(node.expr) |
paul@439 | 1367 | self._visitConst(node.name) |
paul@71 | 1368 | self.keyword_names.add(node.name) |
paul@4 | 1369 | |
paul@85 | 1370 | def visitLambda(self, node): |
paul@351 | 1371 | fn = self._visitFunction(node, None) |
paul@351 | 1372 | self.use_specific_attribute(None, fn.name) |
paul@351 | 1373 | return fn |
paul@4 | 1374 | |
paul@580 | 1375 | visitLeftShift = _visitOperator |
paul@1 | 1376 | |
paul@346 | 1377 | def visitList(self, node): |
paul@347 | 1378 | self.use_specific_attribute("__builtins__", "list") |
paul@442 | 1379 | return self.OP(node) |
paul@0 | 1380 | |
paul@341 | 1381 | def visitListComp(self, node): |
paul@506 | 1382 | |
paul@506 | 1383 | # Note that explicit dispatch is performed. |
paul@506 | 1384 | |
paul@506 | 1385 | if node.quals: |
paul@506 | 1386 | self.visitListCompFor(node.quals[0], node.quals[1:], node.expr) |
paul@448 | 1387 | return make_instance() |
paul@341 | 1388 | |
paul@506 | 1389 | def visitListCompFor(self, node, following_quals, expr): |
paul@341 | 1390 | self.new_branchpoint() |
paul@341 | 1391 | |
paul@341 | 1392 | # Declare names which will be used by generated code. |
paul@341 | 1393 | |
paul@341 | 1394 | self.use_name("__iter__", node.list) |
paul@341 | 1395 | self.use_name("next") |
paul@4 | 1396 | |
paul@341 | 1397 | in_loop = self.in_loop |
paul@591 | 1398 | self.in_loop = True |
paul@341 | 1399 | self.dispatch(node.list) |
paul@442 | 1400 | |
paul@442 | 1401 | # NOTE: Could generate AST nodes for the actual operations instead of |
paul@442 | 1402 | # NOTE: manually generating code in micropython.ast. |
paul@442 | 1403 | |
paul@448 | 1404 | self.expr = make_instance() # each element is a result of a function call |
paul@341 | 1405 | self.dispatch(node.assign) |
paul@341 | 1406 | |
paul@341 | 1407 | # Enter the loop. |
paul@341 | 1408 | # Propagate attribute usage to branches. |
paul@341 | 1409 | |
paul@379 | 1410 | self.new_branch(node) |
paul@341 | 1411 | |
paul@506 | 1412 | # Note that explicit dispatch is performed. |
paul@506 | 1413 | |
paul@506 | 1414 | if node.ifs: |
paul@506 | 1415 | self.visitListCompIf(node.ifs[0], node.ifs[1:], following_quals, expr) |
paul@506 | 1416 | elif following_quals: |
paul@506 | 1417 | self.visitListCompFor(following_quals[0], following_quals[1:], expr) |
paul@506 | 1418 | else: |
paul@506 | 1419 | self.dispatch(expr) |
paul@341 | 1420 | |
paul@341 | 1421 | self.shelve_branch() |
paul@341 | 1422 | self.in_loop = in_loop |
paul@341 | 1423 | |
paul@379 | 1424 | self.merge_branches() |
paul@4 | 1425 | |
paul@506 | 1426 | def visitListCompIf(self, node, following_ifs, following_quals, expr): |
paul@506 | 1427 | self.use_name("__bool__", node) |
paul@506 | 1428 | self.new_branchpoint() |
paul@506 | 1429 | |
paul@506 | 1430 | # Propagate attribute usage to branches. |
paul@506 | 1431 | |
paul@506 | 1432 | self.dispatch(node.test) |
paul@506 | 1433 | |
paul@506 | 1434 | # Note that explicit dispatch is performed. |
paul@506 | 1435 | |
paul@506 | 1436 | if following_ifs: |
paul@506 | 1437 | self.visitListCompIf(following_ifs[0], following_ifs[1:], following_quals, expr) |
paul@506 | 1438 | elif following_quals: |
paul@506 | 1439 | self.visitListCompFor(following_quals[0], following_quals[1:], expr) |
paul@506 | 1440 | else: |
paul@506 | 1441 | self.new_branch(expr) |
paul@506 | 1442 | self.dispatch(expr) |
paul@506 | 1443 | self.shelve_branch() |
paul@506 | 1444 | |
paul@506 | 1445 | # Maintain a branch for the else clause. |
paul@506 | 1446 | |
paul@506 | 1447 | self.new_branch(NullBranch()) |
paul@506 | 1448 | self.shelve_branch() |
paul@506 | 1449 | |
paul@506 | 1450 | self.merge_branches() |
paul@4 | 1451 | |
paul@580 | 1452 | visitMod = _visitOperator |
paul@4 | 1453 | |
paul@0 | 1454 | def visitModule(self, node): |
paul@143 | 1455 | |
paul@143 | 1456 | # Make a back reference from the node for code generation. |
paul@143 | 1457 | |
paul@143 | 1458 | node.unit = self |
paul@0 | 1459 | return self.dispatch(node.node) |
paul@0 | 1460 | |
paul@580 | 1461 | visitMul = _visitOperator |
paul@2 | 1462 | |
paul@0 | 1463 | def visitName(self, node): |
paul@583 | 1464 | attr = self.get_namespace().get_using_node(node.name, node) or make_instance() |
paul@728 | 1465 | node._attr = self.get_namespace().get_for_local(node.name) or attr |
paul@728 | 1466 | return node._attr |
paul@0 | 1467 | |
paul@531 | 1468 | def visitNot(self, node): |
paul@531 | 1469 | self.use_name("__bool__", node) |
paul@531 | 1470 | self.dispatch(node.expr) |
paul@531 | 1471 | return make_instance() |
paul@0 | 1472 | |
paul@462 | 1473 | visitOr = TEST_OP |
paul@0 | 1474 | |
paul@0 | 1475 | visitPass = NOP |
paul@0 | 1476 | |
paul@580 | 1477 | visitPower = _visitOperator |
paul@4 | 1478 | |
paul@393 | 1479 | def _visitPrint(self, node, function_name): |
paul@393 | 1480 | self.NOP(node) |
paul@393 | 1481 | self.use_specific_attribute("__builtins__", function_name) |
paul@4 | 1482 | |
paul@393 | 1483 | def visitPrint(self, node): |
paul@393 | 1484 | self._visitPrint(node, "_print") |
paul@393 | 1485 | |
paul@393 | 1486 | def visitPrintnl(self, node): |
paul@393 | 1487 | self._visitPrint(node, "_printnl") |
paul@4 | 1488 | |
paul@299 | 1489 | visitRaise = NOP_ABANDON |
paul@0 | 1490 | |
paul@299 | 1491 | visitReturn = NOP_ABANDON |
paul@4 | 1492 | |
paul@580 | 1493 | visitRightShift = _visitOperator |
paul@1 | 1494 | |
paul@699 | 1495 | def visitSet(self, node): |
paul@699 | 1496 | self.use_specific_attribute("__builtins__", "set") |
paul@699 | 1497 | return self.OP(node) |
paul@699 | 1498 | |
paul@342 | 1499 | def visitSlice(self, node): |
paul@442 | 1500 | return self._visitOperator(node, self.in_assignment and "AssSlice" or "Slice") |
paul@4 | 1501 | |
paul@82 | 1502 | visitSliceobj = OP |
paul@23 | 1503 | |
paul@0 | 1504 | def visitStmt(self, node): |
paul@0 | 1505 | for n in node.nodes: |
paul@0 | 1506 | self.dispatch(n) |
paul@0 | 1507 | |
paul@580 | 1508 | visitSub = _visitOperator |
paul@4 | 1509 | |
paul@216 | 1510 | def visitSubscript(self, node): |
paul@442 | 1511 | return self._visitOperator(node, self.in_assignment and "AssSubscript" or "Subscript") |
paul@0 | 1512 | |
paul@0 | 1513 | def visitTryExcept(self, node): |
paul@540 | 1514 | self.new_branchpoint() |
paul@0 | 1515 | self.dispatch(node.body) |
paul@272 | 1516 | |
paul@0 | 1517 | for name, var, n in node.handlers: |
paul@572 | 1518 | if name is not None: |
paul@572 | 1519 | self.dispatch(name) |
paul@572 | 1520 | |
paul@537 | 1521 | self.new_branch(n) |
paul@97 | 1522 | |
paul@540 | 1523 | # Any abandoned branches from the body can now be resumed. |
paul@540 | 1524 | |
paul@540 | 1525 | self.resume_abandoned_branches() |
paul@540 | 1526 | |
paul@97 | 1527 | # Establish the local for the handler. |
paul@97 | 1528 | |
paul@99 | 1529 | if var is not None: |
paul@99 | 1530 | self.dispatch(var) |
paul@99 | 1531 | if n is not None: |
paul@99 | 1532 | self.dispatch(n) |
paul@272 | 1533 | |
paul@272 | 1534 | self.shelve_branch() |
paul@272 | 1535 | |
paul@540 | 1536 | # The else clause maintains the usage from the body but without the |
paul@540 | 1537 | # abandoned branches since they would never lead to the else clause |
paul@540 | 1538 | # being executed. |
paul@540 | 1539 | |
paul@379 | 1540 | self.new_branch(node.else_ or NullBranch()) |
paul@0 | 1541 | if node.else_ is not None: |
paul@0 | 1542 | self.dispatch(node.else_) |
paul@379 | 1543 | self.shelve_branch() |
paul@272 | 1544 | |
paul@272 | 1545 | self.merge_branches() |
paul@0 | 1546 | |
paul@4 | 1547 | visitTryFinally = NOP |
paul@4 | 1548 | |
paul@82 | 1549 | visitTuple = OP |
paul@1 | 1550 | |
paul@580 | 1551 | visitUnaryAdd = _visitOperator |
paul@4 | 1552 | |
paul@580 | 1553 | visitUnarySub = _visitOperator |
paul@4 | 1554 | |
paul@34 | 1555 | def visitWhile(self, node): |
paul@462 | 1556 | self.use_name("__bool__", node) |
paul@388 | 1557 | self.new_branchpoint(node) |
paul@272 | 1558 | |
paul@275 | 1559 | # Propagate attribute usage to branches. |
paul@275 | 1560 | |
paul@331 | 1561 | in_loop = self.in_loop |
paul@591 | 1562 | self.in_loop = True |
paul@381 | 1563 | |
paul@386 | 1564 | # The test is evaluated initially and again in the loop. |
paul@381 | 1565 | |
paul@272 | 1566 | self.dispatch(node.test) |
paul@381 | 1567 | |
paul@379 | 1568 | self.new_branch(node) |
paul@272 | 1569 | self.dispatch(node.body) |
paul@388 | 1570 | |
paul@388 | 1571 | self.resume_continuing_branches() |
paul@388 | 1572 | |
paul@386 | 1573 | self.dispatch(node.test) |
paul@386 | 1574 | self.shelve_branch() |
paul@381 | 1575 | |
paul@386 | 1576 | self.in_loop = in_loop |
paul@386 | 1577 | |
paul@386 | 1578 | # A null branch is used to record a path around the loop. |
paul@381 | 1579 | |
paul@386 | 1580 | self.new_branch(node.else_ or NullBranch()) |
paul@386 | 1581 | self.shelve_branch() |
paul@385 | 1582 | |
paul@386 | 1583 | self.merge_branches() |
paul@386 | 1584 | |
paul@386 | 1585 | # The else clause is evaluated outside any branch. |
paul@385 | 1586 | |
paul@385 | 1587 | if node.else_ is not None: |
paul@385 | 1588 | self.dispatch(node.else_) |
paul@385 | 1589 | |
paul@387 | 1590 | # Any suspended branches from the loop can now be resumed. |
paul@387 | 1591 | |
paul@388 | 1592 | self.resume_broken_branches() |
paul@387 | 1593 | |
paul@4 | 1594 | visitWith = NOP |
paul@4 | 1595 | |
paul@4 | 1596 | visitYield = NOP |
paul@4 | 1597 | |
paul@0 | 1598 | # vim: tabstop=4 expandtab shiftwidth=4 |