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