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