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