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