1 #!/usr/bin/env python 2 3 """ 4 Inspect source files, obtaining details of classes and attributes. 5 6 Copyright (C) 2007, 2008, 2009, 2010 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 21 -------- 22 23 The results of inspecting a module are as follows: 24 25 Constants 26 --------- 27 28 All constants identified within the code shall be registered. 29 30 Classes 31 ------- 32 33 All global classes shall be registered; local classes (within functions) or 34 nested classes (within classes) are not currently registered. 35 36 Base classes must be detected and constant. 37 38 All classes without bases are made to inherit from __builtins__.object in order 39 to support some standard methods. 40 41 Functions 42 --------- 43 44 All functions and lambda definitions shall be registered. 45 46 Namespaces 47 ---------- 48 49 Modules define their own "global" namespace, within which classes, functions 50 and lambda definitions establish a hierarchy of namespaces. 51 52 Only local, global and built-in namespaces are recognised; closures are not 53 supported. 54 55 Assignments 56 ----------- 57 58 Name assignment and attribute assignment involving modules and classes cause 59 names to be associated with values within namespaces. 60 61 Any assignments within loops are considered to cause the targets of such 62 assignments to provide non-constant values. 63 64 Assignments to names are only really considered to cause the targets of such 65 assignments to provide constant values if the targets reside in the module 66 namespace or in class namespaces, subject to the above conditions. 67 68 Assignments to names within functions are not generally considered to cause the 69 targets of such assignments to provide constant values since functions can be 70 invoked many times with different inputs. This affects particularly the 71 definition of functions or lambdas within functions. However, there may be 72 benefits in considering a local to be constant within a single invocation. 73 """ 74 75 from micropython.common import * 76 from micropython.data import * 77 import compiler.ast 78 79 # Program visitors. 80 81 class InspectedModule(ASTVisitor, Module): 82 83 """ 84 An inspected module, providing core details via the Module superclass, but 85 capable of being used as an AST visitor. 86 """ 87 88 def __init__(self, name, importer): 89 90 """ 91 Initialise this visitor with a module 'name' and an 'importer' which is 92 used to provide access to other modules when required. 93 """ 94 95 ASTVisitor.__init__(self) 96 Module.__init__(self, name, importer) 97 self.visitor = self 98 99 # Import machinery links. 100 101 self.optimisations = self.importer.optimisations 102 self.builtins = self.importer.modules.get("__builtins__") 103 self.loaded = 0 104 105 # Current expression state. 106 107 self.expr = None 108 109 # Namespace state. 110 111 self.in_method = 0 # Find instance attributes in all methods. 112 self.in_function = 0 # Note function presence, affecting definitions. 113 self.in_loop = 0 # Note loop "membership", affecting assignments. 114 self.namespaces = [] 115 self.functions = [] 116 117 def parse(self, filename): 118 119 "Parse the file having the given 'filename'." 120 121 module = compiler.parseFile(filename) 122 self.process(module) 123 124 def process(self, module): 125 126 "Process the given 'module'." 127 128 self.astnode = module 129 130 # Add __name__ to the namespace by adding an explicit assignment to the 131 # module. 132 133 module.node.nodes.insert(0, compiler.ast.Assign( 134 [compiler.ast.AssName("__name__", 0)], 135 compiler.ast.Const(self.full_name()) 136 )) 137 138 # First, visit module-level code, recording global names. 139 140 processed = self.dispatch(module) 141 142 # Then, for each function, detect and record globals declared in those 143 # functions. 144 145 for node, namespaces in self.functions: 146 self.process_globals(node) 147 148 self.finalise_attribute_usage() 149 150 # Then, visit each function, recording other names. 151 152 for node, namespaces in self.functions: 153 self._visitFunctionBody(node, namespaces) 154 namespaces[-1].finalise_attribute_usage() 155 156 # Add references to other modules declared using the __all__ global. 157 158 if self.has_key("__all__"): 159 all = self["__all__"] 160 if isinstance(all, compiler.ast.List): 161 for n in all.nodes: 162 self.store(n.value, self.importer.add_module(self.name + "." + n.value)) 163 164 return processed 165 166 def process_globals(self, node): 167 168 """ 169 Within the given 'node', process global declarations, adjusting the 170 module namespace. 171 """ 172 173 for n in node.getChildNodes(): 174 if isinstance(n, compiler.ast.Global): 175 for name in n.names: 176 if not self.has_key(name): 177 self[name] = None 178 else: 179 self.process_globals(n) 180 181 def vacuum(self): 182 183 """ 184 Vacuum the module namespace, removing unreferenced objects and unused 185 names. 186 """ 187 188 if self.should_optimise_unused_objects(): 189 self.vacuum_object(self) 190 191 all_objects = list(self.all_objects) 192 193 for obj in all_objects: 194 self.vacuum_object(obj) 195 196 def vacuum_object(self, obj, delete_all=0): 197 198 "Vacuum the given object 'obj'." 199 200 for name, attr in obj.items_for_vacuum(): 201 202 # Only consider deleting entire unused objects or things accessible 203 # via names which are never used. 204 205 if delete_all or not self.importer.uses_attribute(obj.full_name(), name): 206 obj.vacuum_item(name) 207 208 # Delete any unambiguous attribute value. Such values can only 209 # have been defined within the object and therefore are not 210 # redefined by other code regions. 211 212 if attr.assignments == 1: 213 value = attr.get_value() 214 215 if value is not obj and value in self.all_objects: 216 self.all_objects.remove(value) 217 218 # Delete class contents and lambdas from functions. 219 220 self.vacuum_object(value, 1) 221 222 def unfinalise(self): 223 224 "Reset finalised information for the module." 225 226 for obj in self.all_objects: 227 obj.unfinalise_attributes() 228 229 def finalise(self): 230 231 "Finalise the module." 232 233 for obj in self.all_objects: 234 obj.finalise_attributes() 235 236 def add_object(self, obj, any_scope=0): 237 238 """ 239 Record 'obj' if non-local or if the optional 'any_scope' is set to a 240 true value. 241 """ 242 243 if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): 244 self.all_objects.add(obj) 245 246 # Optimisation tests. 247 248 def should_optimise_unused_objects(self): 249 return "unused_objects" in self.optimisations 250 251 # Namespace methods. 252 253 def in_class(self, namespaces=None): 254 namespaces = namespaces or self.namespaces 255 return len(namespaces) > 1 and isinstance(namespaces[-2], Class) 256 257 def store(self, name, obj): 258 259 "Record attribute or local 'name', storing 'obj'." 260 261 # Store in the module. 262 263 if not self.namespaces: 264 if self.in_loop and self.used_in_scope(name, "builtins"): 265 raise InspectError("Name %r already used as a built-in." % name) 266 else: 267 self.set(name, obj, not self.in_loop) 268 269 # Or store locally. 270 271 else: 272 locals = self.namespaces[-1] 273 274 if self.in_loop and locals.used_in_scope(name, "global") and not name in locals.globals: 275 raise InspectError("Name %r already used as global." % name) 276 elif self.in_loop and locals.used_in_scope(name, "builtins"): 277 raise InspectError("Name %r already used as a built-in." % name) 278 else: 279 locals.set(name, obj, not self.in_loop) 280 281 def store_lambda(self, obj): 282 283 "Store a lambda function 'obj'." 284 285 self.add_object(obj) 286 self.get_namespace().add_lambda(obj) 287 288 def store_module_attr(self, name, module): 289 290 """ 291 Record module attribute 'name' in the given 'module' using the current 292 expression. 293 """ 294 295 module.set(name, self.expr, 0) 296 297 def store_class_attr(self, name): 298 299 """ 300 Record class attribute 'name' in the current class using the current 301 expression. 302 """ 303 304 if self.in_method and self.namespaces[-2].has_key(name): 305 self.namespaces[-2].set(name, self.expr, 0) 306 return 1 307 308 return 0 309 310 def store_instance_attr(self, name): 311 312 "Record instance attribute 'name' in the current class." 313 314 if self.in_method: 315 316 # Current namespace is the function. 317 # Previous namespace is the class. 318 319 self.namespaces[-2].add_instance_attribute(name) 320 321 def get_namespace(self): 322 323 "Return the parent (or most recent) namespace currently exposed." 324 325 return (self.namespaces[-1:] or [self])[0] 326 327 def use_name(self, name, node=None): 328 329 """ 330 Use the given 'name' within the current namespace/unit, either in 331 conjunction with a particular object (if 'node' is specified and not 332 None) or unconditionally. 333 """ 334 335 if node is not None and isinstance(node, compiler.ast.Name): 336 self.use_attribute(node.name, name) 337 338 # For general name usage, declare usage of the given name from this 339 # particular unit. 340 341 else: 342 unit = self.get_namespace() 343 self.importer.use_name(name, unit.full_name()) 344 345 # Attribute usage methods. 346 # These are convenience methods which refer to the specific namespace's 347 # implementation of these operations. 348 349 def new_branchpoint(self): 350 self.get_namespace()._new_branchpoint() 351 352 def new_branch(self, loop_node=None): 353 self.get_namespace()._new_branch(loop_node) 354 355 def abandon_branch(self): 356 self.get_namespace()._abandon_branch() 357 358 def shelve_branch(self): 359 self.get_namespace()._shelve_branch() 360 361 def merge_branches(self): 362 self.get_namespace()._merge_branches() 363 364 def define_attribute_user(self, node): 365 366 """ 367 Define 'node' as the user of attributes, indicating the point where the 368 user is defined. 369 """ 370 371 self.get_namespace()._define_attribute_user(node) 372 373 def use_attribute(self, name, attrname): 374 375 "Note usage on the attribute user 'name' of the attribute 'attrname'." 376 377 return self.get_namespace()._use_attribute(name, attrname) 378 379 def use_specific_attribute(self, objname, attrname): 380 381 """ 382 Note usage on the object having the given 'objname' of the attribute 383 'attrname'. If 'objname' is None, the current namespace is chosen as the 384 object providing the attribute. 385 """ 386 387 return self.get_namespace()._use_specific_attribute(objname, attrname) 388 389 # Visitor methods. 390 391 def default(self, node, *args): 392 raise InspectError("Node class %r is not supported." % node.__class__) 393 394 def NOP(self, node): 395 for n in node.getChildNodes(): 396 self.dispatch(n) 397 return None 398 399 def NOP_ABANDON(self, node): 400 self.NOP(node) 401 self.abandon_branch() 402 403 def OP(self, node): 404 for n in node.getChildNodes(): 405 self.dispatch(n) 406 return Instance() 407 408 # Generic support for classes of operations. 409 410 def _ensureOperators(self): 411 attr, scope, namespace = self._get_with_scope("$operator") 412 if attr is None: 413 module = self.importer.load("operator") 414 self["$operator"] = module 415 else: 416 module = attr.get_value() 417 return module 418 419 def _visitOperator(self, node): 420 421 "Accounting method for the operator 'node'." 422 423 operator_module = self._ensureOperators() 424 operator_fn = operator_functions[node.__class__.__name__] 425 self.use_specific_attribute(operator_module.full_name(), operator_fn) 426 return self.OP(node) 427 428 _visitBinary = _visitOperator 429 _visitUnary = _visitOperator 430 431 def _visitAttr(self, expr, attrname, node): 432 433 # Attempt to identify the nature of the attribute. 434 435 if isinstance(expr, Attr): 436 value = expr.get_value() 437 438 # Get the attribute and record its usage. 439 440 if isinstance(value, (Class, Module)): 441 attr = value.get(attrname) 442 self.use_specific_attribute(value.full_name(), attrname) 443 444 elif isinstance(value, UnresolvedName): 445 attr = UnresolvedName(attrname, value.full_name(), self) 446 447 else: 448 attr = None 449 450 # Note usage of the attribute where a local is involved. 451 452 if expr.parent is self.get_namespace(): 453 node._attrusers = self.use_attribute(expr.name, attrname) 454 node._username = expr.name 455 else: 456 self.use_name(attrname, node.expr) 457 458 elif self.builtins is not None: 459 attr = self.builtins.get(attrname) 460 self.use_specific_attribute(self.builtins.full_name(), attrname) 461 462 else: 463 attr = None 464 self.use_name(attrname, node) 465 466 return attr 467 468 def _visitFunction(self, node, name): 469 470 """ 471 Return a function object for the function defined by 'node' with the 472 given 'name'. If a lambda expression is being visited, 'name' should be 473 None. 474 """ 475 476 # Define the function object. 477 478 function = Function( 479 name, 480 self.get_namespace(), 481 node.argnames, 482 node.defaults, 483 (node.flags & 4 != 0), 484 (node.flags & 8 != 0), 485 self.in_loop or self.in_function, 486 self, 487 node 488 ) 489 490 self.add_object(function, any_scope=1) 491 492 # Make a back reference from the node for code generation. 493 494 node.unit = function 495 496 # Process the defaults. 497 498 for n in node.defaults: 499 self.expr = self.dispatch(n) 500 function.store_default(self.expr) 501 502 # Note attribute usage where tuple parameters are involved. 503 504 if function.tuple_parameters(): 505 self.use_name("__getitem__", node) 506 507 # Record the namespace context of the function for later processing. 508 509 self.functions.append((node, self.namespaces + [function])) 510 511 # Store the function. 512 513 if name is not None: 514 self.store(name, function) 515 else: 516 self.store_lambda(function) 517 518 # Test the defaults and assess whether an dynamic object will result. 519 520 function.make_dynamic() 521 return function 522 523 def _visitFunctionBody(self, node, namespaces): 524 525 "Enter the function." 526 527 # Current namespace is the function. 528 # Previous namespace is the class. 529 530 if self.in_class(namespaces): 531 self.in_method = 1 532 533 in_function = self.in_function 534 in_loop = self.in_loop 535 self.in_function = 1 536 self.in_loop = 0 537 538 self.namespaces = namespaces 539 self.dispatch(node.code) 540 541 self.in_loop = in_loop 542 self.in_function = in_function 543 self.in_method = 0 544 545 # Specific handler methods. 546 547 visitAdd = _visitBinary 548 549 visitAnd = OP 550 551 visitAssert = NOP 552 553 def visitAssign(self, node): 554 self.expr = self.dispatch(node.expr) 555 for n in node.nodes: 556 self.dispatch(n) 557 return None 558 559 def visitAssAttr(self, node): 560 expr = self.dispatch(node.expr) 561 562 # Record the attribute on the presumed target. 563 564 if isinstance(expr, Attr): 565 if expr.name == "self": 566 if not self.store_class_attr(node.attrname): 567 self.store_instance_attr(node.attrname) 568 elif isinstance(expr.get_value(), Module): 569 self.store_module_attr(node.attrname, expr.get_value()) 570 print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) 571 572 # Note usage of the attribute where a local is involved. 573 574 if expr.parent is self.get_namespace(): 575 node._attrusers = self.use_attribute(expr.name, node.attrname) 576 node._username = expr.name 577 578 return None 579 580 def visitAssList(self, node): 581 582 # Declare names which will be used by generated code. 583 584 self.use_name("__getitem__", node) 585 586 # Process the assignment. 587 588 for i, n in enumerate(node.nodes): 589 self.dispatch(n) 590 self.importer.make_constant(i) # for __getitem__(i) at run-time 591 return None 592 593 def visitAssName(self, node): 594 if hasattr(node, "flags") and node.flags == "OP_DELETE": 595 print "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) 596 #raise InspectError("Deletion of attribute %r is not supported." % node.name) 597 598 self.store(node.name, self.expr) 599 self.define_attribute_user(node) 600 601 # Ensure the presence of the given name in this namespace. 602 603 self.use_specific_attribute(None, node.name) 604 return None 605 606 visitAssTuple = visitAssList 607 608 def visitAugAssign(self, node): 609 610 # Accounting. 611 612 operator_fn = operator_functions.get(node.op) 613 operator_module = self._ensureOperators() 614 self.use_specific_attribute(operator_module.full_name(), operator_fn) 615 616 # Process the assignment. 617 618 self.expr = self.dispatch(node.expr) 619 620 # NOTE: Similar to micropython.ast handler code. 621 # NOTE: Slices and subscripts are supported by __setitem__(slice) and 622 # NOTE: not __setslice__. 623 624 if isinstance(node.node, compiler.ast.Name): 625 self.visitAssName(node.node) 626 elif isinstance(node.node, compiler.ast.Getattr): 627 self.visitAssAttr(node.node) 628 else: 629 self.use_specific_attribute("__builtins__", "slice") 630 self.use_name("__setitem__", node) 631 632 return None 633 634 visitBackquote = OP 635 636 visitBitand = _visitBinary 637 638 visitBitor = _visitBinary 639 640 visitBitxor = _visitBinary 641 642 visitBreak = NOP_ABANDON 643 644 visitCallFunc = OP 645 646 def visitClass(self, node): 647 648 """ 649 Register the class at the given 'node' subject to the restrictions 650 mentioned in the module docstring. 651 """ 652 653 if self.namespaces: 654 print "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) 655 return None 656 else: 657 if self.in_loop: 658 print "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) 659 660 cls = Class(node.name, self.get_namespace(), self, node) 661 662 # Make a back reference from the node for code generation. 663 664 node.unit = cls 665 666 # Process base classes in the context of the class's namespace. 667 # This confines references to such classes to the class instead of 668 # the namespace in which it is defined. 669 670 self.namespaces.append(cls) 671 672 # Visit the base class expressions, attempting to find concrete 673 # definitions of classes. 674 675 for base in node.bases: 676 expr = self.dispatch(base) 677 678 # Each base class must be constant and known at compile-time. 679 680 if isinstance(expr, Attr): 681 if expr.assignments != 1: 682 raise InspectError("Base class %r for %r is not constant." % (base, cls.full_name())) 683 else: 684 cls.add_base(expr.get_value()) 685 686 # Where no expression value is available, the base class is 687 # not identifiable. 688 689 else: 690 raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) 691 692 # NOTE: Potentially dubious measure to permit __init__ availability. 693 # If no bases exist, adopt the 'object' class. 694 695 if not node.bases and not (self.name == "__builtins__" and node.name == "object") : 696 expr = self.dispatch(compiler.ast.Name("object")) 697 cls.add_base(expr.get_value()) 698 699 # Make an entry for the class in the parent namespace. 700 701 self.namespaces.pop() 702 self.store(node.name, cls) 703 self.add_object(cls) 704 705 # Process the class body in its own namespace. 706 707 self.namespaces.append(cls) 708 self.dispatch(node.code) 709 self.namespaces.pop() 710 711 cls.finalise_attribute_usage() 712 return cls 713 714 def visitCompare(self, node): 715 716 # Accounting. 717 # NOTE: Replicates some code in micropython.ast.visitCompare. 718 719 this_node = node 720 721 for op in node.ops: 722 op_name, next_node = op 723 724 # Define name/attribute usage. 725 # Get the applicable operation. 726 727 operator_fn = operator_functions.get(op_name) 728 729 # For operators, reference the specific function involved. 730 731 if operator_fn is not None: 732 operator_module = self._ensureOperators() 733 self.use_specific_attribute(operator_module.full_name(), operator_fn) 734 735 # Define __contains__ usage on the next node. 736 737 elif op_name.endswith("in"): 738 self.use_name("__contains__", next_node) 739 740 this_node = next_node 741 742 return self.OP(node) 743 744 def visitConst(self, node): 745 746 # Register the constant, if necessary, returning the resulting object. 747 748 self.use_name(self.importer.get_constant_type_name(node.value), node) 749 return self.importer.make_constant(node.value) 750 751 visitContinue = NOP_ABANDON 752 753 visitDecorators = NOP 754 755 visitDict = OP 756 757 visitDiscard = NOP 758 759 visitDiv = _visitBinary 760 761 visitEllipsis = NOP 762 763 visitExec = NOP 764 765 visitExpression = OP 766 767 visitFloorDiv = _visitBinary 768 769 def visitFor(self, node): 770 self.new_branchpoint() 771 772 # Declare names which will be used by generated code. 773 774 self.use_name("__iter__", node.list) 775 self.use_name("next") 776 self.use_name("StopIteration") 777 778 in_loop = self.in_loop 779 self.in_loop = 1 780 self.dispatch(node.list) 781 self.dispatch(node.assign) 782 783 # Enter the loop. 784 # Propagate attribute usage to branches. 785 786 self.new_branch(node) 787 self.dispatch(node.body) 788 self.shelve_branch() 789 790 self.in_loop = in_loop 791 792 # Maintain a branch for the else clause or the current retained usage 793 # where execution avoids the conditional clauses. 794 795 self.new_branch() 796 if node.else_ is not None: 797 self.dispatch(node.else_) 798 self.shelve_branch() 799 800 self.merge_branches() 801 return None 802 803 def visitFrom(self, node): 804 module = self.importer.load(node.modname, 1) 805 806 #if module is None: 807 # print "Warning:", node.modname, "not imported." 808 809 for name, alias in node.names: 810 if name != "*": 811 if module is not None and module.has_key(name): 812 attr = module[name] 813 self.store(alias or name, attr) 814 self.use_specific_attribute(module.full_name(), name) 815 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 816 self.importer.load(attr.get_value().name) 817 818 # Support the import of names from missing modules. 819 820 else: 821 self.store(alias or name, UnresolvedName(name, node.modname, self)) 822 else: 823 if module is not None: 824 for n in module.keys(): 825 attr = module[n] 826 self.store(n, attr) 827 self.use_specific_attribute(module.full_name(), n) 828 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 829 self.importer.load(attr.get_value().name) 830 831 return None 832 833 def visitFunction(self, node): 834 return self._visitFunction(node, node.name) 835 836 visitGenExpr = OP 837 838 visitGenExprFor = NOP 839 840 visitGenExprIf = NOP 841 842 visitGenExprInner = NOP 843 844 def visitGetattr(self, node): 845 expr = self.dispatch(node.expr) 846 attrname = node.attrname 847 return self._visitAttr(expr, attrname, node) 848 849 def visitGlobal(self, node): 850 if self.namespaces: 851 for name in node.names: 852 ns = self.namespaces[-1] 853 if not ns.make_global(name): 854 raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) 855 856 # The name is recorded in an earlier process. 857 858 def visitIf(self, node): 859 self.new_branchpoint() 860 861 # Propagate attribute usage to branches. 862 863 for test, body in node.tests: 864 self.dispatch(test) 865 866 self.new_branch() 867 self.dispatch(body) 868 self.shelve_branch() 869 870 # Maintain a branch for the else clause or the current retained usage 871 # where execution avoids the conditional clauses. 872 873 self.new_branch() 874 if node.else_ is not None: 875 self.dispatch(node.else_) 876 self.shelve_branch() 877 878 self.merge_branches() 879 return None 880 881 visitIfExp = NOP 882 883 def visitImport(self, node): 884 for name, alias in node.names: 885 if alias is not None: 886 module = self.importer.load(name, 1) or UnresolvedName(None, name, self) 887 self.store(alias, module) 888 else: 889 module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) 890 self.store(name.split(".")[0], module) 891 892 return None 893 894 visitInvert = _visitUnary 895 896 def visitKeyword(self, node): 897 self.dispatch(node.expr) 898 self.importer.make_constant(node.name) 899 self.keyword_names.add(node.name) 900 return None 901 902 def visitLambda(self, node): 903 fn = self._visitFunction(node, None) 904 self.use_specific_attribute(None, fn.name) 905 return fn 906 907 visitLeftShift = _visitBinary 908 909 def visitList(self, node): 910 self.use_specific_attribute("__builtins__", "list") 911 self.OP(node) 912 913 def visitListComp(self, node): 914 for qual in node.quals: 915 self.dispatch(qual) 916 self.dispatch(node.expr) 917 918 def visitListCompFor(self, node): 919 self.new_branchpoint() 920 921 # Declare names which will be used by generated code. 922 923 self.use_name("__iter__", node.list) 924 self.use_name("next") 925 926 in_loop = self.in_loop 927 self.in_loop = 1 928 self.dispatch(node.list) 929 self.dispatch(node.assign) 930 931 # Enter the loop. 932 # Propagate attribute usage to branches. 933 934 self.new_branch(node) 935 936 for if_ in node.ifs: 937 self.dispatch(if_) 938 939 self.shelve_branch() 940 self.in_loop = in_loop 941 942 self.merge_branches() 943 return None 944 945 visitListCompIf = NOP 946 947 visitMod = _visitBinary 948 949 def visitModule(self, node): 950 951 # Make a back reference from the node for code generation. 952 953 node.unit = self 954 return self.dispatch(node.node) 955 956 visitMul = _visitBinary 957 958 def visitName(self, node): 959 return self.get_namespace().get_using_node(node.name, node) 960 961 visitNot = OP 962 963 visitOr = OP 964 965 visitPass = NOP 966 967 visitPower = _visitBinary 968 969 visitPrint = NOP 970 971 visitPrintnl = NOP 972 973 visitRaise = NOP_ABANDON 974 975 visitReturn = NOP_ABANDON 976 977 visitRightShift = _visitBinary 978 979 def visitSlice(self, node): 980 expr = self.dispatch(node.expr) 981 self._visitAttr(expr, "__getitem__", node) 982 983 if node.lower is not None: 984 self.dispatch(node.lower) 985 if node.upper is not None: 986 self.dispatch(node.upper) 987 988 self.use_specific_attribute("__builtins__", "slice") 989 990 visitSliceobj = OP 991 992 def visitStmt(self, node): 993 for n in node.nodes: 994 self.dispatch(n) 995 return None 996 997 visitSub = _visitBinary 998 999 def visitSubscript(self, node): 1000 expr = self.dispatch(node.expr) 1001 self._visitAttr(expr, "__getitem__", node) 1002 1003 for sub in node.subs: 1004 self.dispatch(sub) 1005 1006 def visitTryExcept(self, node): 1007 self.dispatch(node.body) 1008 1009 self.new_branchpoint() 1010 1011 for name, var, n in node.handlers: 1012 self.new_branch() 1013 1014 # Establish the local for the handler. 1015 1016 if var is not None: 1017 self.dispatch(var) 1018 if n is not None: 1019 self.dispatch(n) 1020 1021 self.shelve_branch() 1022 1023 self.new_branch() 1024 if node.else_ is not None: 1025 self.dispatch(node.else_) 1026 self.shelve_branch() 1027 1028 self.merge_branches() 1029 return None 1030 1031 visitTryFinally = NOP 1032 1033 visitTuple = OP 1034 1035 visitUnaryAdd = _visitUnary 1036 1037 visitUnarySub = _visitUnary 1038 1039 def visitWhile(self, node): 1040 self.new_branchpoint() 1041 1042 # Propagate attribute usage to branches. 1043 1044 in_loop = self.in_loop 1045 self.in_loop = 1 1046 self.dispatch(node.test) 1047 self.new_branch(node) 1048 self.dispatch(node.body) 1049 self.shelve_branch() 1050 self.in_loop = in_loop 1051 1052 # Maintain a branch for the else clause or the current retained usage 1053 # where execution avoids the conditional clauses. 1054 # NOTE: Consider merging here before the else clause. 1055 1056 self.new_branch() 1057 if node.else_ is not None: 1058 self.dispatch(node.else_) 1059 self.shelve_branch() 1060 1061 self.merge_branches() 1062 return None 1063 1064 visitWith = NOP 1065 1066 visitYield = NOP 1067 1068 # vim: tabstop=4 expandtab shiftwidth=4