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, 2012 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 import sys 79 80 # Program visitors. 81 82 class InspectedModule(ASTVisitor, Module): 83 84 """ 85 An inspected module, providing core details via the Module superclass, but 86 capable of being used as an AST visitor. 87 """ 88 89 def __init__(self, name, importer): 90 91 """ 92 Initialise this visitor with a module 'name' and an 'importer' which is 93 used to provide access to other modules when required. 94 """ 95 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. 132 133 self.store("__name__", self._visitConst(self.full_name())) 134 135 # First, visit module-level code, recording global names. 136 137 processed = self.dispatch(module) 138 139 # Then, for each function, detect and record globals declared in those 140 # functions. 141 142 for node, namespaces in self.functions: 143 self.process_globals(node) 144 145 self.finalise_attribute_usage() 146 147 # Then, visit each function, recording other names. 148 149 for node, namespaces in self.functions: 150 self._visitFunctionBody(node, namespaces) 151 namespaces[-1].finalise_attribute_usage() 152 153 # Add references to other modules declared using the __all__ global. 154 155 if self.has_key("__all__"): 156 all = self["__all__"] 157 if isinstance(all, compiler.ast.List): 158 for n in all.nodes: 159 self.store(n.value, self.importer.add_module(self.name + "." + n.value)) 160 161 return processed 162 163 def process_globals(self, node): 164 165 """ 166 Within the given 'node', process global declarations, adjusting the 167 module namespace. 168 """ 169 170 for n in node.getChildNodes(): 171 if isinstance(n, compiler.ast.Global): 172 for name in n.names: 173 if not self.has_key(name): 174 self[name] = make_instance() 175 else: 176 self.process_globals(n) 177 178 def vacuum(self): 179 180 """ 181 Vacuum the module namespace, removing unreferenced objects and unused 182 names. 183 """ 184 185 if self.should_optimise_unused_objects(): 186 self.vacuum_object(self) 187 188 all_objects = list(self.all_objects) 189 190 for obj in all_objects: 191 self.vacuum_object(obj) 192 193 def vacuum_object(self, obj, delete_all=0): 194 195 "Vacuum the given object 'obj'." 196 197 # Get all constant objects in apparent use. 198 199 if delete_all: 200 obj_objects = set() 201 else: 202 obj_objects = [] 203 for name, attr in obj.items_for_vacuum(): 204 205 # Get constant objects for attributes in use. 206 207 if self.importer.uses_attribute(obj.full_name(), name) and \ 208 attr is not None and attr.is_constant(): 209 210 value = attr.get_value() 211 obj_objects.append(value) 212 213 # Now vacuum unused attributes and objects not in use. 214 215 for name, attr in obj.items_for_vacuum(): 216 217 # Only consider deleting entire unused objects or things accessible 218 # via names which are never used. 219 220 if delete_all or not self.importer.uses_attribute(obj.full_name(), name): 221 obj.vacuum_item(name) 222 223 # Delete any unambiguous attribute value. Such values can only 224 # have been defined within the object and therefore are not 225 # redefined by other code regions. 226 227 if attr is not None and attr.is_constant(): 228 value = attr.get_value() 229 230 # The value must have this object as a parent. 231 # However, it must not be shared by several names. 232 233 if value is not obj and value.parent is obj and \ 234 value in self.all_objects and value not in obj_objects: 235 236 self.all_objects.remove(value) 237 238 # Delete class contents and lambdas from functions. 239 240 self.vacuum_object(value, 1) 241 242 def unfinalise(self): 243 244 "Reset finalised information for the module." 245 246 for obj in self.all_objects: 247 obj.unfinalise_attributes() 248 249 def finalise(self, objtable): 250 251 "Finalise the module." 252 253 for obj in self.all_objects: 254 obj.finalise(objtable) 255 256 self.finalise_users(objtable) 257 258 def add_object(self, obj, any_scope=0): 259 260 """ 261 Record 'obj' if non-local or if the optional 'any_scope' is set to a 262 true value. 263 """ 264 265 if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): 266 self.all_objects.add(obj) 267 268 # Optimisation tests. 269 270 def should_optimise_unused_objects(self): 271 return "unused_objects" in self.optimisations 272 273 # Namespace methods. 274 275 def in_class(self, namespaces=None): 276 namespaces = namespaces or self.namespaces 277 return len(namespaces) > 1 and isinstance(namespaces[-2], Class) 278 279 def store(self, name, obj): 280 281 "Record attribute or local 'name', storing 'obj'." 282 283 # Store in the module. 284 285 if not self.namespaces: 286 if self.in_loop and self.used_in_scope(name, "builtins"): 287 raise InspectError("Name %r already used as a built-in." % name) 288 else: 289 self.set(name, obj, not self.in_loop) 290 291 # Or store locally. 292 293 else: 294 locals = self.namespaces[-1] 295 296 if self.in_loop and locals.used_in_scope(name, "global") and not name in locals.globals: 297 raise InspectError("Name %r already used as global." % name) 298 elif self.in_loop and locals.used_in_scope(name, "builtins"): 299 raise InspectError("Name %r already used as a built-in." % name) 300 else: 301 locals.set(name, obj, not self.in_loop) 302 303 def store_lambda(self, obj): 304 305 "Store a lambda function 'obj'." 306 307 self.add_object(obj) 308 self.get_namespace().add_lambda(obj) 309 310 def store_module_attr(self, name, module): 311 312 """ 313 Record module attribute 'name' in the given 'module' using the current 314 expression. 315 """ 316 317 module.set(name, self.expr, 0) 318 self.use_specific_attribute(module.full_name(), name) 319 320 def store_class_attr(self, name, cls): 321 322 """ 323 Record class attribute 'name' in the given class 'cls' using the current 324 expression. 325 """ 326 327 cls.set(name, self.expr, 0) 328 self.use_specific_attribute(cls.full_name(), name) 329 330 def store_instance_attr(self, name, tentative=False): 331 332 """ 333 Record instance attribute 'name' in the current class. If 'tentative' is 334 set to a true value, the instance attribute will be discarded if a class 335 attribute is observed. 336 """ 337 338 if self.in_method: 339 340 # Current namespace is the function. 341 # Previous namespace is the class. 342 343 cls = self.namespaces[-2] 344 cls.add_instance_attribute(name, tentative) 345 346 # NOTE: The instance attribute, although defined in a specific 347 # NOTE: class, obviously appears in all descendant classes. 348 349 self.use_specific_attribute(cls.full_name(), name) 350 351 def get_namespace(self): 352 353 "Return the parent (or most recent) namespace currently exposed." 354 355 return (self.namespaces[-1:] or [self])[0] 356 357 def use_name(self, name, node=None, value=None): 358 359 """ 360 Use the given 'name' within the current namespace/unit, either in 361 conjunction with a particular object (if 'node' is specified and not 362 None) or unconditionally. 363 """ 364 365 if node is not None and isinstance(node, compiler.ast.Name): 366 self.use_attribute(node.name, name, value) 367 368 # For general name usage, declare usage of the given name from this 369 # particular unit. 370 371 else: 372 unit = self.get_namespace() 373 self.importer.use_name(name, unit.full_name(), value) 374 375 def use_constant(self, const): 376 377 "Use the given 'const' within the current namespace/unit." 378 379 unit = self.get_namespace() 380 self.importer.use_constant(const, unit.full_name()) 381 382 # Attribute usage methods. 383 # These are convenience methods which refer to the specific namespace's 384 # implementation of these operations. 385 386 def new_branchpoint(self, loop_node=None): 387 self.get_namespace()._new_branchpoint(loop_node) 388 389 def new_branch(self, node): 390 self.get_namespace()._new_branch(node) 391 392 def abandon_branch(self): 393 self.get_namespace()._abandon_branch() 394 395 def suspend_broken_branch(self): 396 self.get_namespace()._suspend_broken_branch() 397 398 def suspend_continuing_branch(self): 399 self.get_namespace()._suspend_continuing_branch() 400 401 def shelve_branch(self): 402 self.get_namespace()._shelve_branch() 403 404 def merge_branches(self): 405 self.get_namespace()._merge_branches() 406 407 def resume_broken_branches(self): 408 self.get_namespace()._resume_broken_branches() 409 410 def resume_continuing_branches(self): 411 self.get_namespace()._resume_continuing_branches() 412 413 def define_attribute_user(self, node): 414 415 """ 416 Define 'node' as the user of attributes, indicating the point where the 417 user is defined. 418 """ 419 420 self.get_namespace()._define_attribute_user(node) 421 422 def use_attribute(self, name, attrname, value=None): 423 424 """ 425 Note usage on the attribute user 'name' of the attribute 'attrname', 426 noting an assignment if 'value' is specified. 427 """ 428 429 return self.get_namespace()._use_attribute(name, attrname, value) 430 431 def use_specific_attribute(self, objname, attrname, from_name=None): 432 433 """ 434 Note usage on the object having the given 'objname' of the attribute 435 'attrname'. If 'objname' is None, the current namespace is chosen as the 436 object providing the attribute. 437 """ 438 439 return self.get_namespace()._use_specific_attribute(objname, attrname, from_name) 440 441 # Visitor methods. 442 443 def default(self, node, *args): 444 raise InspectError("Node class %r is not supported." % node.__class__) 445 446 def NOP(self, node): 447 for n in node.getChildNodes(): 448 self.dispatch(n) 449 450 def NOP_ABANDON(self, node): 451 self.NOP(node) 452 self.abandon_branch() 453 454 def TEST_NOP(self, node): 455 self.use_name("__bool__", node) 456 self.NOP(node) 457 458 def OP(self, node): 459 for n in node.getChildNodes(): 460 self.dispatch(n) 461 return make_instance() 462 463 def TEST_OP(self, node): 464 self.use_name("__bool__", node) 465 return self.OP(node) 466 467 # Generic support for classes of operations. 468 469 def _ensureOperators(self): 470 attr, scope, namespace = self._get_with_scope("$operator") 471 if attr is None: 472 module = self.importer.load("operator") 473 self["$operator"] = module 474 else: 475 module = attr.get_value() 476 return module 477 478 def _visitOperator(self, node, operator_name=None): 479 480 "Accounting method for the operator 'node'." 481 482 operator_module = self._ensureOperators() 483 operator_fn = operator_functions[operator_name or node.__class__.__name__] 484 self.use_specific_attribute(operator_module.full_name(), operator_fn) 485 return self.OP(node) 486 487 _visitBinary = _visitOperator 488 _visitUnary = _visitOperator 489 490 def _visitAttr(self, expr, attrname, node): 491 492 """ 493 Process the attribute provided by the given 'expr' with the given 494 'attrname' and involving the given 'node'. 495 """ 496 497 # Attempt to identify the nature of the attribute. 498 499 if isinstance(expr, Attr): 500 value = expr.get_value() 501 502 # Get the attribute and record its usage. 503 504 if isinstance(value, (Class, Module)): 505 506 # Check for class.__class__. 507 508 if attrname == "__class__" and isinstance(value, Class): 509 attr = type_class 510 else: 511 attr = value.get(attrname) or make_instance() 512 self.use_specific_attribute(value.full_name(), attrname) 513 514 elif isinstance(value, UnresolvedName): 515 attr = UnresolvedName(attrname, value.full_name(), self) 516 517 # The actual attribute is not readily identifiable and is assumed 518 # to be an instance. 519 520 else: 521 522 # Record any instance attributes. 523 524 if expr.name == "self": 525 self.store_instance_attr(attrname, tentative=True) 526 527 attr = make_instance() 528 529 # Note usage of the attribute where a local is involved. 530 531 self._visitAttrUser(expr, attrname, node) 532 533 # No particular attribute has been identified, thus a general instance 534 # is assumed. 535 536 else: 537 attr = make_instance() 538 self.use_name(attrname, node) 539 540 return attr 541 542 def _visitAttrUser(self, expr, attrname, node, value=None): 543 544 """ 545 Note usage of the attribute provided by 'expr' with the given 'attrname' 546 where a local is involved, annotating the given 'node'. If the optional 547 'value' is given, note an assignment for future effects on attributes 548 where such attributes are inferred from the usage. 549 """ 550 551 # Access to attribute via a local. 552 553 if expr.parent is self.get_namespace(): 554 555 # NOTE: Revisiting of nodes may occur for loops. 556 557 if not hasattr(node, "_attrusers"): 558 node._attrusers = set() 559 560 node._attrusers.update(self.use_attribute(expr.name, attrname, value)) 561 node._username = expr.name 562 else: 563 self.use_name(attrname, node.expr, value) 564 565 def _visitConst(self, value): 566 567 """ 568 Register the constant given by 'value', if necessary, returning the 569 resulting object. The type name is noted as being used, thus preserving 570 the class in any generated program. 571 """ 572 573 self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value)) 574 const = self.importer.make_constant(value) 575 self.use_constant(const) 576 return const 577 578 def _visitFunction(self, node, name): 579 580 """ 581 Return a function object for the function defined by 'node' with the 582 given 'name'. If a lambda expression is being visited, 'name' should be 583 None. 584 """ 585 586 # Define the function object. 587 588 function = Function( 589 name, 590 self.get_namespace(), 591 node.argnames, 592 node.defaults, 593 (node.flags & 4 != 0), 594 (node.flags & 8 != 0), 595 self.in_loop or self.in_function, 596 self, 597 node 598 ) 599 600 self.add_object(function, any_scope=1) 601 602 # Make a back reference from the node for code generation. 603 604 node.unit = function 605 606 # Process the defaults. 607 608 for n in node.defaults: 609 self.expr = self.dispatch(n) 610 function.store_default(self.expr) 611 612 # Note attribute usage where tuple parameters are involved. 613 614 if function.tuple_parameters(): 615 self.use_name("__getitem__", node) 616 617 # Record the namespace context of the function for later processing. 618 619 self.functions.append((node, self.namespaces + [function])) 620 621 # Store the function. 622 623 if name is not None: 624 self.store(name, function) 625 else: 626 self.store_lambda(function) 627 628 # Test the defaults and assess whether an dynamic object will result. 629 630 function.make_dynamic() 631 return function 632 633 def _visitFunctionBody(self, node, namespaces): 634 635 "Enter the function." 636 637 # Current namespace is the function. 638 # Previous namespace is the class. 639 640 if self.in_class(namespaces): 641 self.in_method = 1 642 643 in_function = self.in_function 644 in_loop = self.in_loop 645 self.in_function = 1 646 self.in_loop = 0 647 648 self.namespaces = namespaces 649 self.dispatch(node.code) 650 651 self.in_loop = in_loop 652 self.in_function = in_function 653 self.in_method = 0 654 655 # Specific handler methods. 656 657 visitAdd = _visitBinary 658 659 visitAnd = TEST_OP 660 661 visitAssert = NOP 662 663 def visitAssign(self, node): 664 self.expr = self.dispatch(node.expr) 665 self.in_assignment = 1 666 for n in node.nodes: 667 self.dispatch(n) 668 self.in_assignment = 0 669 670 def visitAssAttr(self, node): 671 expr = self.dispatch(node.expr) 672 attrname = node.attrname 673 674 # Record the attribute on the presumed target. 675 676 if isinstance(expr, Attr): 677 value = expr.get_value() 678 679 if expr.name == "self": 680 self.store_instance_attr(attrname) 681 self.use_attribute(expr.name, attrname, value) # NOTE: Impose constraints on the type given the hierarchy. 682 self._visitAttrUser(expr, attrname, node, self.expr) 683 684 elif isinstance(value, Module): 685 self.store_module_attr(attrname, value) 686 print >>sys.stderr, "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) 687 688 elif isinstance(value, Class): 689 self.store_class_attr(attrname, value) 690 691 # Note usage of the attribute where a local is involved. 692 693 else: 694 self._visitAttrUser(expr, attrname, node, self.expr) 695 696 else: 697 self.use_name(attrname, node) 698 699 def visitAssList(self, node): 700 701 # Declare names which will be used by generated code. 702 703 self.use_name("__getitem__", node) 704 705 # Process the assignment. 706 707 for i, n in enumerate(node.nodes): 708 self.dispatch(n) 709 self._visitConst(i) # for __getitem__(i) at run-time 710 711 def visitAssName(self, node): 712 if hasattr(node, "flags") and node.flags == "OP_DELETE": 713 print >>sys.stderr, "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) 714 #raise InspectError("Deletion of attribute %r is not supported." % node.name) 715 716 self.store(node.name, self.expr) 717 self.define_attribute_user(node) 718 719 # Ensure the presence of the given name in this namespace. 720 # NOTE: Consider not registering assignments involving methods, since 721 # NOTE: this is merely creating aliases for such methods. 722 723 if isinstance(self.get_namespace(), (Class, Module)): 724 if not isinstance(self.expr, Attr) or not isinstance(self.expr.get_value(), Function): 725 self.use_specific_attribute(None, node.name) 726 else: 727 fn = self.expr.get_value() 728 ns = self.get_namespace().full_name() 729 self.use_specific_attribute(fn.parent.full_name(), fn.name, "%s.%s" % (ns, node.name)) 730 731 visitAssTuple = visitAssList 732 733 def visitAugAssign(self, node): 734 735 # Accounting. 736 737 operator_fn = operator_functions.get(node.op) 738 operator_module = self._ensureOperators() 739 self.use_specific_attribute(operator_module.full_name(), operator_fn) 740 741 # Process the assignment. 742 743 self.expr = self.dispatch(node.expr) 744 745 # NOTE: Similar to micropython.ast handler code. 746 # NOTE: Slices and subscripts are supported by __setitem__(slice) and 747 # NOTE: not __setslice__. 748 749 if isinstance(node.node, compiler.ast.Name): 750 self.visitAssName(node.node) 751 elif isinstance(node.node, compiler.ast.Getattr): 752 self.visitAssAttr(node.node) 753 else: 754 self.use_specific_attribute("__builtins__", "slice") 755 self.use_name("__setitem__", node) 756 757 visitBackquote = OP 758 759 visitBitand = _visitBinary 760 761 visitBitor = _visitBinary 762 763 visitBitxor = _visitBinary 764 765 def visitBreak(self, node): 766 self.NOP(node) 767 self.suspend_broken_branch() 768 769 visitCallFunc = OP 770 771 def visitClass(self, node): 772 773 """ 774 Register the class at the given 'node' subject to the restrictions 775 mentioned in the module docstring. 776 """ 777 778 if self.namespaces: 779 print >>sys.stderr, "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) 780 return 781 else: 782 if self.in_loop: 783 print >>sys.stderr, "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) 784 785 cls = get_class(node.name, self.get_namespace(), self, node) 786 787 # Make a back reference from the node for code generation. 788 789 node.unit = cls 790 791 # Process base classes in the context of the class's namespace. 792 # This confines references to such classes to the class instead of 793 # the namespace in which it is defined. 794 795 self.namespaces.append(cls) 796 797 # Visit the base class expressions, attempting to find concrete 798 # definitions of classes. 799 800 for base in node.bases: 801 expr = self.dispatch(base) 802 803 # Each base class must be constant and known at compile-time. 804 805 if isinstance(expr, Attr): 806 if expr.assignments != 1: 807 raise InspectError("Base class %r for %r is not constant: %r" % (base, cls.full_name(), expr)) 808 else: 809 cls.add_base(expr.get_value()) 810 811 # Where no expression value is available, the base class is 812 # not identifiable. 813 814 else: 815 raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) 816 817 # NOTE: Potentially dubious measure to permit __init__ availability. 818 # If no bases exist, adopt the 'object' class. 819 820 if not node.bases and not (self.name == "__builtins__" and node.name == "object") : 821 expr = self.dispatch(compiler.ast.Name("object")) 822 cls.add_base(expr.get_value()) 823 824 # Make an entry for the class in the parent namespace. 825 826 self.namespaces.pop() 827 self.store(node.name, cls) 828 self.add_object(cls) 829 830 # Process the class body in its own namespace. 831 # Add __name__ to the namespace. 832 833 self.namespaces.append(cls) 834 self.store("__name__", self._visitConst(node.name)) 835 self.dispatch(node.code) 836 self.namespaces.pop() 837 838 cls.finalise_attribute_usage() 839 return cls 840 841 def visitCompare(self, node): 842 843 # Accounting. 844 # NOTE: Replicates some code in micropython.ast.visitCompare. 845 846 self.use_name("__bool__", node) 847 848 this_node = node 849 850 for op in node.ops: 851 op_name, next_node = op 852 853 # Define name/attribute usage. 854 # Get the applicable operation. 855 856 operator_fn = operator_functions.get(op_name) 857 858 # For operators, reference the specific function involved. 859 860 if operator_fn is not None: 861 operator_module = self._ensureOperators() 862 self.use_specific_attribute(operator_module.full_name(), operator_fn) 863 864 # Define __contains__ usage on the next node. 865 866 elif op_name.endswith("in"): 867 self.use_name("__contains__", next_node) 868 869 this_node = next_node 870 871 return self.OP(node) 872 873 def visitConst(self, node): 874 return self._visitConst(node.value) 875 876 def visitContinue(self, node): 877 self.NOP(node) 878 self.suspend_continuing_branch() 879 880 visitDecorators = NOP 881 882 visitDict = OP 883 884 visitDiscard = NOP 885 886 visitDiv = _visitBinary 887 888 visitEllipsis = NOP 889 890 visitExec = NOP 891 892 visitExpression = OP 893 894 visitFloorDiv = _visitBinary 895 896 def visitFor(self, node): 897 self.new_branchpoint(node) 898 899 # Declare names which will be used by generated code. 900 901 self.use_name("__iter__", node.list) 902 self.use_name("next") 903 self.use_name("StopIteration") 904 905 in_loop = self.in_loop 906 self.in_loop = 1 907 self.dispatch(node.list) 908 909 # NOTE: Could generate AST nodes for the actual operations instead of 910 # NOTE: manually generating code in micropython.ast. 911 912 self.expr = make_instance() # each element is a result of a function call 913 self.dispatch(node.assign) 914 915 # Enter the loop. 916 # Propagate attribute usage to branches. 917 918 self.new_branch(node) 919 self.dispatch(node.body) 920 921 self.resume_continuing_branches() 922 923 self.shelve_branch() 924 925 self.in_loop = in_loop 926 927 # A null branch is used to record a path around the loop. 928 929 self.new_branch(node.else_ or NullBranch()) 930 self.shelve_branch() 931 932 self.merge_branches() 933 934 # The else clause is evaluated outside any branch. 935 936 if node.else_ is not None: 937 self.dispatch(node.else_) 938 939 # Any suspended branches from the loop can now be resumed. 940 941 self.resume_broken_branches() 942 943 def visitFrom(self, node): 944 module = self.importer.load(node.modname, 1) 945 946 #if module is None: 947 # print >>sys.stderr, "Warning:", node.modname, "not imported." 948 949 for name, alias in node.names: 950 if name != "*": 951 if module is not None: 952 953 # Missing names may refer to submodules. 954 955 if not module.has_key(name): 956 submodule = self.importer.load(node.modname + "." + name, 1) 957 if submodule is not None: 958 module.store(name, submodule) 959 960 # Complete the import if the name was found. 961 962 if module.has_key(name): 963 attr = module[name] 964 self.store(alias or name, attr) 965 self.use_specific_attribute(module.full_name(), name) 966 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 967 self.importer.load(attr.get_value().name) 968 continue 969 970 # Support the import of names from missing modules. 971 972 self.store(alias or name, UnresolvedName(name, node.modname, self)) 973 974 else: 975 if module is not None: 976 for n in module.keys(): 977 attr = module[n] 978 self.store(n, attr) 979 self.use_specific_attribute(module.full_name(), n) 980 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 981 self.importer.load(attr.get_value().name) 982 983 def visitFunction(self, node): 984 return self._visitFunction(node, node.name) 985 986 visitGenExpr = OP 987 988 visitGenExprFor = NOP 989 990 visitGenExprIf = NOP 991 992 visitGenExprInner = NOP 993 994 def visitGetattr(self, node): 995 expr = self.dispatch(node.expr) 996 attrname = node.attrname 997 return self._visitAttr(expr, attrname, node) 998 999 def visitGlobal(self, node): 1000 if self.namespaces: 1001 for name in node.names: 1002 ns = self.namespaces[-1] 1003 if not ns.make_global(name): 1004 raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) 1005 1006 # The name is recorded in an earlier process. 1007 1008 def visitIf(self, node): 1009 self.use_name("__bool__", node) 1010 self.new_branchpoint() 1011 1012 # Propagate attribute usage to branches. 1013 1014 for test, body in node.tests: 1015 self.dispatch(test) 1016 1017 self.new_branch(body) 1018 self.dispatch(body) 1019 self.shelve_branch() 1020 1021 # Maintain a branch for the else clause. 1022 1023 self.new_branch(node.else_ or NullBranch()) 1024 if node.else_ is not None: 1025 self.dispatch(node.else_) 1026 self.shelve_branch() 1027 1028 self.merge_branches() 1029 1030 def visitIfExp(self, node): 1031 self.use_name("__bool__", node) 1032 self.new_branchpoint() 1033 1034 # Propagate attribute usage to branches. 1035 1036 self.dispatch(node.test) 1037 1038 self.new_branch(node.then) 1039 self.dispatch(node.then) 1040 self.shelve_branch() 1041 1042 self.new_branch(node.else_) 1043 self.dispatch(node.else_) 1044 self.shelve_branch() 1045 1046 self.merge_branches() 1047 return make_instance() # either outcome is possible 1048 1049 def visitImport(self, node): 1050 for name, alias in node.names: 1051 if alias is not None: 1052 module = self.importer.load(name, 1) or UnresolvedName(None, name, self) 1053 self.store(alias, module) 1054 else: 1055 module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) 1056 self.store(name.split(".")[0], module) 1057 1058 visitInvert = _visitUnary 1059 1060 def visitKeyword(self, node): 1061 self.dispatch(node.expr) 1062 self._visitConst(node.name) 1063 self.keyword_names.add(node.name) 1064 1065 def visitLambda(self, node): 1066 fn = self._visitFunction(node, None) 1067 self.use_specific_attribute(None, fn.name) 1068 return fn 1069 1070 visitLeftShift = _visitBinary 1071 1072 def visitList(self, node): 1073 self.use_specific_attribute("__builtins__", "list") 1074 return self.OP(node) 1075 1076 def visitListComp(self, node): 1077 1078 # Note that explicit dispatch is performed. 1079 1080 if node.quals: 1081 self.visitListCompFor(node.quals[0], node.quals[1:], node.expr) 1082 return make_instance() 1083 1084 def visitListCompFor(self, node, following_quals, expr): 1085 self.new_branchpoint() 1086 1087 # Declare names which will be used by generated code. 1088 1089 self.use_name("__iter__", node.list) 1090 self.use_name("next") 1091 1092 in_loop = self.in_loop 1093 self.in_loop = 1 1094 self.dispatch(node.list) 1095 1096 # NOTE: Could generate AST nodes for the actual operations instead of 1097 # NOTE: manually generating code in micropython.ast. 1098 1099 self.expr = make_instance() # each element is a result of a function call 1100 self.dispatch(node.assign) 1101 1102 # Enter the loop. 1103 # Propagate attribute usage to branches. 1104 1105 self.new_branch(node) 1106 1107 # Note that explicit dispatch is performed. 1108 1109 if node.ifs: 1110 self.visitListCompIf(node.ifs[0], node.ifs[1:], following_quals, expr) 1111 elif following_quals: 1112 self.visitListCompFor(following_quals[0], following_quals[1:], expr) 1113 else: 1114 self.dispatch(expr) 1115 1116 self.shelve_branch() 1117 self.in_loop = in_loop 1118 1119 self.merge_branches() 1120 1121 def visitListCompIf(self, node, following_ifs, following_quals, expr): 1122 self.use_name("__bool__", node) 1123 self.new_branchpoint() 1124 1125 # Propagate attribute usage to branches. 1126 1127 self.dispatch(node.test) 1128 1129 # Note that explicit dispatch is performed. 1130 1131 if following_ifs: 1132 self.visitListCompIf(following_ifs[0], following_ifs[1:], following_quals, expr) 1133 elif following_quals: 1134 self.visitListCompFor(following_quals[0], following_quals[1:], expr) 1135 else: 1136 self.new_branch(expr) 1137 self.dispatch(expr) 1138 self.shelve_branch() 1139 1140 # Maintain a branch for the else clause. 1141 1142 self.new_branch(NullBranch()) 1143 self.shelve_branch() 1144 1145 self.merge_branches() 1146 1147 visitMod = _visitBinary 1148 1149 def visitModule(self, node): 1150 1151 # Make a back reference from the node for code generation. 1152 1153 node.unit = self 1154 return self.dispatch(node.node) 1155 1156 visitMul = _visitBinary 1157 1158 def visitName(self, node): 1159 return self.get_namespace().get_using_node(node.name, node) or make_instance() 1160 1161 visitNot = TEST_OP 1162 1163 visitOr = TEST_OP 1164 1165 visitPass = NOP 1166 1167 visitPower = _visitBinary 1168 1169 def _visitPrint(self, node, function_name): 1170 self.NOP(node) 1171 self.use_specific_attribute("__builtins__", function_name) 1172 1173 def visitPrint(self, node): 1174 self._visitPrint(node, "_print") 1175 1176 def visitPrintnl(self, node): 1177 self._visitPrint(node, "_printnl") 1178 1179 visitRaise = NOP_ABANDON 1180 1181 visitReturn = NOP_ABANDON 1182 1183 visitRightShift = _visitBinary 1184 1185 def visitSlice(self, node): 1186 return self._visitOperator(node, self.in_assignment and "AssSlice" or "Slice") 1187 1188 visitSliceobj = OP 1189 1190 def visitStmt(self, node): 1191 for n in node.nodes: 1192 self.dispatch(n) 1193 1194 visitSub = _visitBinary 1195 1196 def visitSubscript(self, node): 1197 return self._visitOperator(node, self.in_assignment and "AssSubscript" or "Subscript") 1198 1199 def visitTryExcept(self, node): 1200 self.dispatch(node.body) 1201 1202 self.new_branchpoint() 1203 1204 for name, var, n in node.handlers: 1205 self.new_branch(node) 1206 1207 # Establish the local for the handler. 1208 1209 if var is not None: 1210 self.dispatch(var) 1211 if n is not None: 1212 self.dispatch(n) 1213 1214 self.shelve_branch() 1215 1216 self.new_branch(node.else_ or NullBranch()) 1217 if node.else_ is not None: 1218 self.dispatch(node.else_) 1219 self.shelve_branch() 1220 1221 self.merge_branches() 1222 1223 visitTryFinally = NOP 1224 1225 visitTuple = OP 1226 1227 visitUnaryAdd = _visitUnary 1228 1229 visitUnarySub = _visitUnary 1230 1231 def visitWhile(self, node): 1232 self.use_name("__bool__", node) 1233 self.new_branchpoint(node) 1234 1235 # Propagate attribute usage to branches. 1236 1237 in_loop = self.in_loop 1238 self.in_loop = 1 1239 1240 # The test is evaluated initially and again in the loop. 1241 1242 self.dispatch(node.test) 1243 1244 self.new_branch(node) 1245 self.dispatch(node.body) 1246 1247 self.resume_continuing_branches() 1248 1249 self.dispatch(node.test) 1250 self.shelve_branch() 1251 1252 self.in_loop = in_loop 1253 1254 # A null branch is used to record a path around the loop. 1255 1256 self.new_branch(node.else_ or NullBranch()) 1257 self.shelve_branch() 1258 1259 self.merge_branches() 1260 1261 # The else clause is evaluated outside any branch. 1262 1263 if node.else_ is not None: 1264 self.dispatch(node.else_) 1265 1266 # Any suspended branches from the loop can now be resumed. 1267 1268 self.resume_broken_branches() 1269 1270 visitWith = NOP 1271 1272 visitYield = NOP 1273 1274 # vim: tabstop=4 expandtab shiftwidth=4