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