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 _visitUnary(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 = _visitUnary 429 430 def _visitFunction(self, node, name): 431 432 """ 433 Return a function object for the function defined by 'node' with the 434 given 'name'. If a lambda expression is being visited, 'name' should be 435 None. 436 """ 437 438 # Define the function object. 439 440 function = Function( 441 name, 442 self.get_namespace(), 443 node.argnames, 444 node.defaults, 445 (node.flags & 4 != 0), 446 (node.flags & 8 != 0), 447 self.in_loop or self.in_function, 448 self, 449 node 450 ) 451 452 self.add_object(function, any_scope=1) 453 454 # Make a back reference from the node for code generation. 455 456 node.unit = function 457 458 # Process the defaults. 459 460 for n in node.defaults: 461 self.expr = self.dispatch(n) 462 function.store_default(self.expr) 463 464 # Note attribute usage where tuple parameters are involved. 465 466 if function.tuple_parameters(): 467 self.use_name("__getitem__", node) 468 469 # Record the namespace context of the function for later processing. 470 471 self.functions.append((node, self.namespaces + [function])) 472 473 # Store the function. 474 475 if name is not None: 476 self.store(name, function) 477 else: 478 self.store_lambda(function) 479 480 # Test the defaults and assess whether an dynamic object will result. 481 482 function.make_dynamic() 483 return function 484 485 def _visitFunctionBody(self, node, namespaces): 486 487 "Enter the function." 488 489 # Current namespace is the function. 490 # Previous namespace is the class. 491 492 if self.in_class(namespaces): 493 self.in_method = 1 494 495 in_function = self.in_function 496 in_loop = self.in_loop 497 self.in_function = 1 498 self.in_loop = 0 499 500 self.namespaces = namespaces 501 self.dispatch(node.code) 502 503 self.in_loop = in_loop 504 self.in_function = in_function 505 self.in_method = 0 506 507 # Specific handler methods. 508 509 visitAdd = _visitBinary 510 511 visitAnd = OP 512 513 visitAssert = NOP 514 515 def visitAssign(self, node): 516 self.expr = self.dispatch(node.expr) 517 for n in node.nodes: 518 self.dispatch(n) 519 return None 520 521 def visitAssAttr(self, node): 522 expr = self.dispatch(node.expr) 523 524 # Record the attribute on the presumed target. 525 526 if isinstance(expr, Attr): 527 if expr.name == "self": 528 if not self.store_class_attr(node.attrname): 529 self.store_instance_attr(node.attrname) 530 elif isinstance(expr.get_value(), Module): 531 self.store_module_attr(node.attrname, expr.get_value()) 532 print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) 533 534 # Note usage of the attribute where a local is involved. 535 536 if expr.parent is self.get_namespace(): 537 node._attrusers = self.use_attribute(expr.name, node.attrname) 538 node._username = expr.name 539 540 return None 541 542 def visitAssList(self, node): 543 544 # Declare names which will be used by generated code. 545 546 self.use_name("__getitem__", node) 547 548 # Process the assignment. 549 550 for i, n in enumerate(node.nodes): 551 self.dispatch(n) 552 self.importer.make_constant(i) # for __getitem__(i) at run-time 553 return None 554 555 def visitAssName(self, node): 556 if hasattr(node, "flags") and node.flags == "OP_DELETE": 557 print "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) 558 #raise InspectError("Deletion of attribute %r is not supported." % node.name) 559 560 self.store(node.name, self.expr) 561 self.define_attribute_user(node) 562 563 # Ensure the presence of the given name in this namespace. 564 565 self.use_specific_attribute(None, node.name) 566 return None 567 568 visitAssTuple = visitAssList 569 570 def visitAugAssign(self, node): 571 572 # Accounting. 573 574 operator_fn = operator_functions.get(node.op) 575 operator_module = self._ensureOperators() 576 self.use_specific_attribute(operator_module.full_name(), operator_fn) 577 578 # Process the assignment. 579 580 self.expr = self.dispatch(node.expr) 581 582 # NOTE: Similar to micropython.ast handler code. 583 # NOTE: Slices and subscripts are supported by __setitem__(slice) and 584 # NOTE: not __setslice__. 585 586 if isinstance(node.node, compiler.ast.Name): 587 self.visitAssName(node.node) 588 elif isinstance(node.node, compiler.ast.Getattr): 589 self.visitAssAttr(node.node) 590 else: 591 self.use_specific_attribute("__builtins__", "slice") 592 self.use_name("__setitem__", node) 593 594 return None 595 596 visitBackquote = OP 597 598 visitBitand = _visitBinary 599 600 visitBitor = _visitBinary 601 602 visitBitxor = _visitBinary 603 604 visitBreak = NOP_ABANDON 605 606 visitCallFunc = OP 607 608 def visitClass(self, node): 609 610 """ 611 Register the class at the given 'node' subject to the restrictions 612 mentioned in the module docstring. 613 """ 614 615 if self.namespaces: 616 print "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) 617 return None 618 else: 619 if self.in_loop: 620 print "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) 621 622 cls = Class(node.name, self.get_namespace(), self, node) 623 624 # Make a back reference from the node for code generation. 625 626 node.unit = cls 627 628 # Process base classes in the context of the class's namespace. 629 # This confines references to such classes to the class instead of 630 # the namespace in which it is defined. 631 632 self.namespaces.append(cls) 633 634 # Visit the base class expressions, attempting to find concrete 635 # definitions of classes. 636 637 for base in node.bases: 638 expr = self.dispatch(base) 639 640 # Each base class must be constant and known at compile-time. 641 642 if isinstance(expr, Attr): 643 if expr.assignments != 1: 644 raise InspectError("Base class %r for %r is not constant." % (base, cls.full_name())) 645 else: 646 cls.add_base(expr.get_value()) 647 648 # Where no expression value is available, the base class is 649 # not identifiable. 650 651 else: 652 raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) 653 654 # NOTE: Potentially dubious measure to permit __init__ availability. 655 # If no bases exist, adopt the 'object' class. 656 657 if not node.bases and not (self.name == "__builtins__" and node.name == "object") : 658 expr = self.dispatch(compiler.ast.Name("object")) 659 cls.add_base(expr.get_value()) 660 661 # Make an entry for the class in the parent namespace. 662 663 self.namespaces.pop() 664 self.store(node.name, cls) 665 self.add_object(cls) 666 667 # Process the class body in its own namespace. 668 669 self.namespaces.append(cls) 670 self.dispatch(node.code) 671 self.namespaces.pop() 672 673 cls.finalise_attribute_usage() 674 return cls 675 676 def visitCompare(self, node): 677 678 # Accounting. 679 # NOTE: Replicates some code in micropython.ast.visitCompare. 680 681 this_node = node 682 683 for op in node.ops: 684 op_name, next_node = op 685 686 # Define name/attribute usage. 687 # Get the applicable operation. 688 689 operator_fn = operator_functions.get(op_name) 690 691 # For operators, reference the specific function involved. 692 693 if operator_fn is not None: 694 operator_module = self._ensureOperators() 695 self.use_specific_attribute(operator_module.full_name(), operator_fn) 696 697 # Define __contains__ usage on the next node. 698 699 elif op_name.endswith("in"): 700 self.use_name("__contains__", next_node) 701 702 this_node = next_node 703 704 return self.OP(node) 705 706 def visitConst(self, node): 707 708 # Register the constant, if necessary, returning the resulting object. 709 710 self.use_name(self.importer.get_constant_type_name(node.value), node) 711 return self.importer.make_constant(node.value) 712 713 visitContinue = NOP_ABANDON 714 715 visitDecorators = NOP 716 717 visitDict = OP 718 719 visitDiscard = NOP 720 721 visitDiv = _visitBinary 722 723 visitEllipsis = NOP 724 725 visitExec = NOP 726 727 visitExpression = OP 728 729 visitFloorDiv = _visitBinary 730 731 def visitFor(self, node): 732 self.new_branchpoint() 733 734 # Declare names which will be used by generated code. 735 736 self.use_name("__iter__", node.list) 737 self.use_name("next") 738 self.use_name("StopIteration") 739 740 in_loop = self.in_loop 741 self.in_loop = 1 742 self.dispatch(node.list) 743 self.dispatch(node.assign) 744 745 # Enter the loop. 746 # Propagate attribute usage to branches. 747 748 self.new_branch(node) 749 self.dispatch(node.body) 750 self.shelve_branch() 751 752 self.in_loop = in_loop 753 754 # Maintain a branch for the else clause or the current retained usage 755 # where execution avoids the conditional clauses. 756 757 self.new_branch() 758 if node.else_ is not None: 759 self.dispatch(node.else_) 760 self.shelve_branch() 761 762 self.merge_branches() 763 return None 764 765 def visitFrom(self, node): 766 module = self.importer.load(node.modname, 1) 767 768 #if module is None: 769 # print "Warning:", node.modname, "not imported." 770 771 for name, alias in node.names: 772 if name != "*": 773 if module is not None and module.has_key(name): 774 attr = module[name] 775 self.store(alias or name, attr) 776 self.use_specific_attribute(module.full_name(), name) 777 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 778 self.importer.load(attr.get_value().name) 779 780 # Support the import of names from missing modules. 781 782 else: 783 self.store(alias or name, UnresolvedName(name, node.modname, self)) 784 else: 785 if module is not None: 786 for n in module.keys(): 787 attr = module[n] 788 self.store(n, attr) 789 self.use_specific_attribute(module.full_name(), n) 790 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 791 self.importer.load(attr.get_value().name) 792 793 return None 794 795 def visitFunction(self, node): 796 return self._visitFunction(node, node.name) 797 798 visitGenExpr = OP 799 800 visitGenExprFor = NOP 801 802 visitGenExprIf = NOP 803 804 visitGenExprInner = NOP 805 806 def visitGetattr(self, node): 807 expr = self.dispatch(node.expr) 808 attrname = node.attrname 809 810 # Attempt to identify the nature of the attribute. 811 812 if isinstance(expr, Attr): 813 value = expr.get_value() 814 815 # Get the attribute and record its usage. 816 817 if isinstance(value, (Class, Module)): 818 attr = value.get(attrname) 819 self.use_specific_attribute(value.full_name(), attrname) 820 821 elif isinstance(value, UnresolvedName): 822 attr = UnresolvedName(attrname, value.full_name(), self) 823 824 else: 825 attr = None 826 827 # Note usage of the attribute where a local is involved. 828 829 if expr.parent is self.get_namespace(): 830 node._attrusers = self.use_attribute(expr.name, attrname) 831 node._username = expr.name 832 else: 833 self.use_name(attrname, node.expr) 834 835 elif self.builtins is not None: 836 attr = self.builtins.get(attrname) 837 self.use_specific_attribute(self.builtins.full_name(), attrname) 838 839 else: 840 attr = UnresolvedName(attrname, value.full_name(), self) 841 842 return attr 843 844 def visitGlobal(self, node): 845 if self.namespaces: 846 for name in node.names: 847 ns = self.namespaces[-1] 848 if not ns.make_global(name): 849 raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) 850 851 # The name is recorded in an earlier process. 852 853 def visitIf(self, node): 854 self.new_branchpoint() 855 856 # Propagate attribute usage to branches. 857 858 for test, body in node.tests: 859 self.dispatch(test) 860 861 self.new_branch() 862 self.dispatch(body) 863 self.shelve_branch() 864 865 # Maintain a branch for the else clause or the current retained usage 866 # where execution avoids the conditional clauses. 867 868 self.new_branch() 869 if node.else_ is not None: 870 self.dispatch(node.else_) 871 self.shelve_branch() 872 873 self.merge_branches() 874 return None 875 876 visitIfExp = NOP 877 878 def visitImport(self, node): 879 for name, alias in node.names: 880 if alias is not None: 881 module = self.importer.load(name, 1) or UnresolvedName(None, name, self) 882 self.store(alias, module) 883 else: 884 module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) 885 self.store(name.split(".")[0], module) 886 887 return None 888 889 visitInvert = _visitUnary 890 891 def visitKeyword(self, node): 892 self.dispatch(node.expr) 893 self.importer.make_constant(node.name) 894 self.keyword_names.add(node.name) 895 return None 896 897 def visitLambda(self, node): 898 fn = self._visitFunction(node, None) 899 self.use_specific_attribute(None, fn.name) 900 return fn 901 902 visitLeftShift = _visitBinary 903 904 def visitList(self, node): 905 self.use_specific_attribute("__builtins__", "list") 906 self.OP(node) 907 908 def visitListComp(self, node): 909 for qual in node.quals: 910 self.dispatch(qual) 911 self.dispatch(node.expr) 912 913 def visitListCompFor(self, node): 914 self.new_branchpoint() 915 916 # Declare names which will be used by generated code. 917 918 self.use_name("__iter__", node.list) 919 self.use_name("next") 920 921 in_loop = self.in_loop 922 self.in_loop = 1 923 self.dispatch(node.list) 924 self.dispatch(node.assign) 925 926 # Enter the loop. 927 # Propagate attribute usage to branches. 928 929 self.new_branch(node) 930 931 for if_ in node.ifs: 932 self.dispatch(if_) 933 934 self.shelve_branch() 935 self.in_loop = in_loop 936 937 self.merge_branches() 938 return None 939 940 visitListCompIf = NOP 941 942 visitMod = _visitBinary 943 944 def visitModule(self, node): 945 946 # Make a back reference from the node for code generation. 947 948 node.unit = self 949 return self.dispatch(node.node) 950 951 visitMul = _visitBinary 952 953 def visitName(self, node): 954 return self.get_namespace().get_using_node(node.name, node) 955 956 visitNot = OP 957 958 visitOr = OP 959 960 visitPass = NOP 961 962 visitPower = _visitBinary 963 964 visitPrint = NOP 965 966 visitPrintnl = NOP 967 968 visitRaise = NOP_ABANDON 969 970 visitReturn = NOP_ABANDON 971 972 visitRightShift = _visitBinary 973 974 def visitSlice(self, node): 975 self.use_specific_attribute("__builtins__", "slice") 976 self.use_name("__getitem__", node) 977 self.OP(node) 978 979 visitSliceobj = OP 980 981 def visitStmt(self, node): 982 for n in node.nodes: 983 self.dispatch(n) 984 return None 985 986 visitSub = _visitBinary 987 988 def visitSubscript(self, node): 989 self.use_name("__getitem__", node) 990 self.OP(node) 991 992 def visitTryExcept(self, node): 993 self.dispatch(node.body) 994 995 self.new_branchpoint() 996 997 for name, var, n in node.handlers: 998 self.new_branch() 999 1000 # Establish the local for the handler. 1001 1002 if var is not None: 1003 self.dispatch(var) 1004 if n is not None: 1005 self.dispatch(n) 1006 1007 self.shelve_branch() 1008 1009 self.new_branch() 1010 if node.else_ is not None: 1011 self.dispatch(node.else_) 1012 self.shelve_branch() 1013 1014 self.merge_branches() 1015 return None 1016 1017 visitTryFinally = NOP 1018 1019 visitTuple = OP 1020 1021 visitUnaryAdd = _visitUnary 1022 1023 visitUnarySub = _visitUnary 1024 1025 def visitWhile(self, node): 1026 self.new_branchpoint() 1027 1028 # Propagate attribute usage to branches. 1029 1030 in_loop = self.in_loop 1031 self.in_loop = 1 1032 self.dispatch(node.test) 1033 self.new_branch(node) 1034 self.dispatch(node.body) 1035 self.shelve_branch() 1036 self.in_loop = in_loop 1037 1038 # Maintain a branch for the else clause or the current retained usage 1039 # where execution avoids the conditional clauses. 1040 # NOTE: Consider merging here before the else clause. 1041 1042 self.new_branch() 1043 if node.else_ is not None: 1044 self.dispatch(node.else_) 1045 self.shelve_branch() 1046 1047 self.merge_branches() 1048 return None 1049 1050 visitWith = NOP 1051 1052 visitYield = NOP 1053 1054 # vim: tabstop=4 expandtab shiftwidth=4