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