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