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