1 #!/usr/bin/env python 2 3 """ 4 Inspect source files, obtaining details of classes and attributes. 5 6 Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 21 -------- 22 23 The results of inspecting a module are as follows: 24 25 Constants 26 --------- 27 28 All constants identified within the code shall be registered. 29 30 Classes 31 ------- 32 33 All global classes shall be registered; local classes (within functions) or 34 nested classes (within classes) are not currently registered. 35 36 Base classes must be detected and constant. 37 38 All classes without bases are made to inherit from __builtins__.object in order 39 to support some standard methods. 40 41 Functions 42 --------- 43 44 All functions and lambda definitions shall be registered. 45 46 Namespaces 47 ---------- 48 49 Modules define their own "global" namespace, within which classes, functions 50 and lambda definitions establish a hierarchy of namespaces. 51 52 Only local, global and built-in namespaces are recognised; closures are not 53 supported. 54 55 Assignments 56 ----------- 57 58 Name assignment and attribute assignment involving modules and classes cause 59 names to be associated with values within namespaces. 60 61 Any assignments within loops are considered to cause the targets of such 62 assignments to provide non-constant values. 63 64 Assignments to names are only really considered to cause the targets of such 65 assignments to provide constant values if the targets reside in the module 66 namespace or in class namespaces, subject to the above conditions. 67 68 Assignments to names within functions are not generally considered to cause the 69 targets of such assignments to provide constant values since functions can be 70 invoked many times with different inputs. This affects particularly the 71 definition of functions or lambdas within functions. However, there may be 72 benefits in considering a local to be constant within a single invocation. 73 """ 74 75 from micropython.common import * 76 from micropython.data import * 77 import compiler.ast 78 79 # Program visitors. 80 81 class InspectedModule(ASTVisitor, Module): 82 83 """ 84 An inspected module, providing core details via the Module superclass, but 85 capable of being used as an AST visitor. 86 """ 87 88 def __init__(self, name, importer): 89 90 """ 91 Initialise this visitor with a module 'name' and an 'importer' which is 92 used to provide access to other modules when required. 93 """ 94 95 ASTVisitor.__init__(self) 96 Module.__init__(self, name, importer) 97 self.visitor = self 98 99 # Import machinery links. 100 101 self.optimisations = self.importer.optimisations 102 self.builtins = self.importer.modules.get("__builtins__") 103 self.loaded = 0 104 105 # Current expression state. 106 107 self.expr = None 108 self.in_assignment = 0 # For slice and subscript handling. 109 110 # Namespace state. 111 112 self.in_method = 0 # Find instance attributes in all methods. 113 self.in_function = 0 # Note function presence, affecting definitions. 114 self.in_loop = 0 # Note loop "membership", affecting assignments. 115 self.namespaces = [] 116 self.functions = [] 117 118 def parse(self, filename): 119 120 "Parse the file having the given 'filename'." 121 122 module = compiler.parseFile(filename) 123 self.process(module) 124 125 def process(self, module): 126 127 "Process the given 'module'." 128 129 self.astnode = module 130 131 # Add __name__ to the namespace. 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 def add_object(self, obj, any_scope=0): 257 258 """ 259 Record 'obj' if non-local or if the optional 'any_scope' is set to a 260 true value. 261 """ 262 263 if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): 264 self.all_objects.add(obj) 265 266 # Optimisation tests. 267 268 def should_optimise_unused_objects(self): 269 return "unused_objects" in self.optimisations 270 271 # Namespace methods. 272 273 def in_class(self, namespaces=None): 274 namespaces = namespaces or self.namespaces 275 return len(namespaces) > 1 and isinstance(namespaces[-2], Class) 276 277 def store(self, name, obj): 278 279 "Record attribute or local 'name', storing 'obj'." 280 281 # Store in the module. 282 283 if not self.namespaces: 284 if self.in_loop and self.used_in_scope(name, "builtins"): 285 raise InspectError("Name %r already used as a built-in." % name) 286 else: 287 self.set(name, obj, not self.in_loop) 288 289 # Or store locally. 290 291 else: 292 locals = self.namespaces[-1] 293 294 if self.in_loop and locals.used_in_scope(name, "global") and not name in locals.globals: 295 raise InspectError("Name %r already used as global." % name) 296 elif self.in_loop and locals.used_in_scope(name, "builtins"): 297 raise InspectError("Name %r already used as a built-in." % name) 298 else: 299 locals.set(name, obj, not self.in_loop) 300 301 def store_lambda(self, obj): 302 303 "Store a lambda function 'obj'." 304 305 self.add_object(obj) 306 self.get_namespace().add_lambda(obj) 307 308 def store_module_attr(self, name, module): 309 310 """ 311 Record module attribute 'name' in the given 'module' using the current 312 expression. 313 """ 314 315 module.set(name, self.expr, 0) 316 self.use_specific_attribute(module.full_name(), name) 317 318 def store_class_attr(self, name, cls): 319 320 """ 321 Record class attribute 'name' in the given class 'cls' using the current 322 expression. 323 """ 324 325 cls.set(name, self.expr, 0) 326 self.use_specific_attribute(cls.full_name(), name) 327 328 def store_instance_attr(self, name): 329 330 "Record instance attribute 'name' in the current class." 331 332 if self.in_method: 333 334 # Current namespace is the function. 335 # Previous namespace is the class. 336 337 cls = self.namespaces[-2] 338 cls.add_instance_attribute(name) 339 340 # NOTE: The instance attribute, although defined in a specific 341 # NOTE: class, obviously appears in all descendant classes. 342 343 self.use_specific_attribute(cls.full_name(), name) 344 345 def get_namespace(self): 346 347 "Return the parent (or most recent) namespace currently exposed." 348 349 return (self.namespaces[-1:] or [self])[0] 350 351 def use_name(self, name, node=None, value=None): 352 353 """ 354 Use the given 'name' within the current namespace/unit, either in 355 conjunction with a particular object (if 'node' is specified and not 356 None) or unconditionally. 357 """ 358 359 if node is not None and isinstance(node, compiler.ast.Name): 360 self.use_attribute(node.name, name, value) 361 362 # For general name usage, declare usage of the given name from this 363 # particular unit. 364 365 else: 366 unit = self.get_namespace() 367 self.importer.use_name(name, unit.full_name(), value) 368 369 def use_constant(self, const): 370 371 "Use the given 'const' within the current namespace/unit." 372 373 unit = self.get_namespace() 374 self.importer.use_constant(const, unit.full_name()) 375 376 # Attribute usage methods. 377 # These are convenience methods which refer to the specific namespace's 378 # implementation of these operations. 379 380 def new_branchpoint(self, loop_node=None): 381 self.get_namespace()._new_branchpoint(loop_node) 382 383 def new_branch(self, node): 384 self.get_namespace()._new_branch(node) 385 386 def abandon_branch(self): 387 self.get_namespace()._abandon_branch() 388 389 def suspend_broken_branch(self): 390 self.get_namespace()._suspend_broken_branch() 391 392 def suspend_continuing_branch(self): 393 self.get_namespace()._suspend_continuing_branch() 394 395 def shelve_branch(self): 396 self.get_namespace()._shelve_branch() 397 398 def merge_branches(self): 399 self.get_namespace()._merge_branches() 400 401 def resume_broken_branches(self): 402 self.get_namespace()._resume_broken_branches() 403 404 def resume_continuing_branches(self): 405 self.get_namespace()._resume_continuing_branches() 406 407 def define_attribute_user(self, node): 408 409 """ 410 Define 'node' as the user of attributes, indicating the point where the 411 user is defined. 412 """ 413 414 self.get_namespace()._define_attribute_user(node) 415 416 def use_attribute(self, name, attrname, value=None): 417 418 """ 419 Note usage on the attribute user 'name' of the attribute 'attrname', 420 noting an assignment if 'value' is specified. 421 """ 422 423 return self.get_namespace()._use_attribute(name, attrname, value) 424 425 def use_specific_attribute(self, objname, attrname, from_name=None): 426 427 """ 428 Note usage on the object having the given 'objname' of the attribute 429 'attrname'. If 'objname' is None, the current namespace is chosen as the 430 object providing the attribute. 431 """ 432 433 return self.get_namespace()._use_specific_attribute(objname, attrname, from_name) 434 435 # Visitor methods. 436 437 def default(self, node, *args): 438 raise InspectError("Node class %r is not supported." % node.__class__) 439 440 def NOP(self, node): 441 for n in node.getChildNodes(): 442 self.dispatch(n) 443 444 def NOP_ABANDON(self, node): 445 self.NOP(node) 446 self.abandon_branch() 447 448 def TEST_NOP(self, node): 449 self.use_name("__bool__", node) 450 self.NOP(node) 451 452 def OP(self, node): 453 for n in node.getChildNodes(): 454 self.dispatch(n) 455 return make_instance() 456 457 def TEST_OP(self, node): 458 self.use_name("__bool__", node) 459 return self.OP(node) 460 461 # Generic support for classes of operations. 462 463 def _ensureOperators(self): 464 attr, scope, namespace = self._get_with_scope("$operator") 465 if attr is None: 466 module = self.importer.load("operator") 467 self["$operator"] = module 468 else: 469 module = attr.get_value() 470 return module 471 472 def _visitOperator(self, node, operator_name=None): 473 474 "Accounting method for the operator 'node'." 475 476 operator_module = self._ensureOperators() 477 operator_fn = operator_functions[operator_name or node.__class__.__name__] 478 self.use_specific_attribute(operator_module.full_name(), operator_fn) 479 return self.OP(node) 480 481 _visitBinary = _visitOperator 482 _visitUnary = _visitOperator 483 484 def _visitAttr(self, expr, attrname, node): 485 486 """ 487 Process the attribute provided by the given 'expr' with the given 488 'attrname' and involving the given 'node'. 489 """ 490 491 # Attempt to identify the nature of the attribute. 492 493 if isinstance(expr, Attr): 494 value = expr.get_value() 495 496 # Get the attribute and record its usage. 497 498 if isinstance(value, (Class, Module)): 499 500 # Check for class.__class__. 501 502 if attrname == "__class__" and isinstance(value, Class): 503 attr = type_class 504 else: 505 attr = value.get(attrname) or make_instance() 506 self.use_specific_attribute(value.full_name(), attrname) 507 508 elif isinstance(value, UnresolvedName): 509 attr = UnresolvedName(attrname, value.full_name(), self) 510 511 # The actual attribute is not readily identifiable and is assumed 512 # to be an instance. 513 514 else: 515 attr = make_instance() 516 517 # Note usage of the attribute where a local is involved. 518 519 self._visitAttrUser(expr, attrname, node) 520 521 # No particular attribute has been identified, thus a general instance 522 # is assumed. 523 524 else: 525 attr = make_instance() 526 self.use_name(attrname, node) 527 528 return attr 529 530 def _visitAttrUser(self, expr, attrname, node, value=None): 531 532 """ 533 Note usage of the attribute provided by 'expr' with the given 'attrname' 534 where a local is involved, annotating the given 'node'. If the optional 535 'value' is given, note an assignment for future effects on attributes 536 where such attributes are inferred from the usage. 537 """ 538 539 # Access to attribute via a local. 540 541 if expr.parent is self.get_namespace(): 542 543 # NOTE: Revisiting of nodes may occur for loops. 544 545 if not hasattr(node, "_attrusers"): 546 node._attrusers = set() 547 548 node._attrusers.update(self.use_attribute(expr.name, attrname, value)) 549 node._username = expr.name 550 else: 551 self.use_name(attrname, node.expr, value) 552 553 def _visitConst(self, value): 554 555 """ 556 Register the constant given by 'value', if necessary, returning the 557 resulting object. The type name is noted as being used, thus preserving 558 the class in any generated program. 559 """ 560 561 self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value)) 562 const = self.importer.make_constant(value) 563 self.use_constant(const) 564 return const 565 566 def _visitFunction(self, node, name): 567 568 """ 569 Return a function object for the function defined by 'node' with the 570 given 'name'. If a lambda expression is being visited, 'name' should be 571 None. 572 """ 573 574 # Define the function object. 575 576 function = Function( 577 name, 578 self.get_namespace(), 579 node.argnames, 580 node.defaults, 581 (node.flags & 4 != 0), 582 (node.flags & 8 != 0), 583 self.in_loop or self.in_function, 584 self, 585 node 586 ) 587 588 self.add_object(function, any_scope=1) 589 590 # Make a back reference from the node for code generation. 591 592 node.unit = function 593 594 # Process the defaults. 595 596 for n in node.defaults: 597 self.expr = self.dispatch(n) 598 function.store_default(self.expr) 599 600 # Note attribute usage where tuple parameters are involved. 601 602 if function.tuple_parameters(): 603 self.use_name("__getitem__", node) 604 605 # Record the namespace context of the function for later processing. 606 607 self.functions.append((node, self.namespaces + [function])) 608 609 # Store the function. 610 611 if name is not None: 612 self.store(name, function) 613 else: 614 self.store_lambda(function) 615 616 # Test the defaults and assess whether an dynamic object will result. 617 618 function.make_dynamic() 619 return function 620 621 def _visitFunctionBody(self, node, namespaces): 622 623 "Enter the function." 624 625 # Current namespace is the function. 626 # Previous namespace is the class. 627 628 if self.in_class(namespaces): 629 self.in_method = 1 630 631 in_function = self.in_function 632 in_loop = self.in_loop 633 self.in_function = 1 634 self.in_loop = 0 635 636 self.namespaces = namespaces 637 self.dispatch(node.code) 638 639 self.in_loop = in_loop 640 self.in_function = in_function 641 self.in_method = 0 642 643 # Specific handler methods. 644 645 visitAdd = _visitBinary 646 647 visitAnd = TEST_OP 648 649 visitAssert = NOP 650 651 def visitAssign(self, node): 652 self.expr = self.dispatch(node.expr) 653 self.in_assignment = 1 654 for n in node.nodes: 655 self.dispatch(n) 656 self.in_assignment = 0 657 658 def visitAssAttr(self, node): 659 expr = self.dispatch(node.expr) 660 attrname = node.attrname 661 662 # Record the attribute on the presumed target. 663 664 if isinstance(expr, Attr): 665 value = expr.get_value() 666 667 if expr.name == "self": 668 self.store_instance_attr(attrname) 669 670 elif isinstance(value, Module): 671 self.store_module_attr(attrname, value) 672 print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) 673 674 elif isinstance(value, Class): 675 self.store_class_attr(attrname, value) 676 677 # Note usage of the attribute where a local is involved. 678 679 else: 680 self._visitAttrUser(expr, attrname, node, self.expr) 681 682 else: 683 self.use_name(attrname, node) 684 685 def visitAssList(self, node): 686 687 # Declare names which will be used by generated code. 688 689 self.use_name("__getitem__", node) 690 691 # Process the assignment. 692 693 for i, n in enumerate(node.nodes): 694 self.dispatch(n) 695 self._visitConst(i) # for __getitem__(i) at run-time 696 697 def visitAssName(self, node): 698 if hasattr(node, "flags") and node.flags == "OP_DELETE": 699 print "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) 700 #raise InspectError("Deletion of attribute %r is not supported." % node.name) 701 702 self.store(node.name, self.expr) 703 self.define_attribute_user(node) 704 705 # Ensure the presence of the given name in this namespace. 706 # NOTE: Consider not registering assignments involving methods, since 707 # NOTE: this is merely creating aliases for such methods. 708 709 if isinstance(self.get_namespace(), (Class, Module)): 710 if not isinstance(self.expr, Attr) or not isinstance(self.expr.get_value(), Function): 711 self.use_specific_attribute(None, node.name) 712 else: 713 fn = self.expr.get_value() 714 ns = self.get_namespace().full_name() 715 self.use_specific_attribute(fn.parent.full_name(), fn.name, "%s.%s" % (ns, node.name)) 716 717 visitAssTuple = visitAssList 718 719 def visitAugAssign(self, node): 720 721 # Accounting. 722 723 operator_fn = operator_functions.get(node.op) 724 operator_module = self._ensureOperators() 725 self.use_specific_attribute(operator_module.full_name(), operator_fn) 726 727 # Process the assignment. 728 729 self.expr = self.dispatch(node.expr) 730 731 # NOTE: Similar to micropython.ast handler code. 732 # NOTE: Slices and subscripts are supported by __setitem__(slice) and 733 # NOTE: not __setslice__. 734 735 if isinstance(node.node, compiler.ast.Name): 736 self.visitAssName(node.node) 737 elif isinstance(node.node, compiler.ast.Getattr): 738 self.visitAssAttr(node.node) 739 else: 740 self.use_specific_attribute("__builtins__", "slice") 741 self.use_name("__setitem__", node) 742 743 visitBackquote = OP 744 745 visitBitand = _visitBinary 746 747 visitBitor = _visitBinary 748 749 visitBitxor = _visitBinary 750 751 def visitBreak(self, node): 752 self.NOP(node) 753 self.suspend_broken_branch() 754 755 visitCallFunc = OP 756 757 def visitClass(self, node): 758 759 """ 760 Register the class at the given 'node' subject to the restrictions 761 mentioned in the module docstring. 762 """ 763 764 if self.namespaces: 765 print "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) 766 return 767 else: 768 if self.in_loop: 769 print "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) 770 771 cls = get_class(node.name, self.get_namespace(), self, node) 772 773 # Make a back reference from the node for code generation. 774 775 node.unit = cls 776 777 # Process base classes in the context of the class's namespace. 778 # This confines references to such classes to the class instead of 779 # the namespace in which it is defined. 780 781 self.namespaces.append(cls) 782 783 # Visit the base class expressions, attempting to find concrete 784 # definitions of classes. 785 786 for base in node.bases: 787 expr = self.dispatch(base) 788 789 # Each base class must be constant and known at compile-time. 790 791 if isinstance(expr, Attr): 792 if expr.assignments != 1: 793 raise InspectError("Base class %r for %r is not constant: %r" % (base, cls.full_name(), expr)) 794 else: 795 cls.add_base(expr.get_value()) 796 797 # Where no expression value is available, the base class is 798 # not identifiable. 799 800 else: 801 raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) 802 803 # NOTE: Potentially dubious measure to permit __init__ availability. 804 # If no bases exist, adopt the 'object' class. 805 806 if not node.bases and not (self.name == "__builtins__" and node.name == "object") : 807 expr = self.dispatch(compiler.ast.Name("object")) 808 cls.add_base(expr.get_value()) 809 810 # Make an entry for the class in the parent namespace. 811 812 self.namespaces.pop() 813 self.store(node.name, cls) 814 self.add_object(cls) 815 816 # Process the class body in its own namespace. 817 # Add __name__ to the namespace. 818 819 self.namespaces.append(cls) 820 self.store("__name__", self._visitConst(node.name)) 821 self.dispatch(node.code) 822 self.namespaces.pop() 823 824 cls.finalise_attribute_usage() 825 return cls 826 827 def visitCompare(self, node): 828 829 # Accounting. 830 # NOTE: Replicates some code in micropython.ast.visitCompare. 831 832 self.use_name("__bool__", node) 833 834 this_node = node 835 836 for op in node.ops: 837 op_name, next_node = op 838 839 # Define name/attribute usage. 840 # Get the applicable operation. 841 842 operator_fn = operator_functions.get(op_name) 843 844 # For operators, reference the specific function involved. 845 846 if operator_fn is not None: 847 operator_module = self._ensureOperators() 848 self.use_specific_attribute(operator_module.full_name(), operator_fn) 849 850 # Define __contains__ usage on the next node. 851 852 elif op_name.endswith("in"): 853 self.use_name("__contains__", next_node) 854 855 this_node = next_node 856 857 return self.OP(node) 858 859 def visitConst(self, node): 860 return self._visitConst(node.value) 861 862 def visitContinue(self, node): 863 self.NOP(node) 864 self.suspend_continuing_branch() 865 866 visitDecorators = NOP 867 868 visitDict = OP 869 870 visitDiscard = NOP 871 872 visitDiv = _visitBinary 873 874 visitEllipsis = NOP 875 876 visitExec = NOP 877 878 visitExpression = OP 879 880 visitFloorDiv = _visitBinary 881 882 def visitFor(self, node): 883 self.new_branchpoint(node) 884 885 # Declare names which will be used by generated code. 886 887 self.use_name("__iter__", node.list) 888 self.use_name("next") 889 self.use_name("StopIteration") 890 891 in_loop = self.in_loop 892 self.in_loop = 1 893 self.dispatch(node.list) 894 895 # NOTE: Could generate AST nodes for the actual operations instead of 896 # NOTE: manually generating code in micropython.ast. 897 898 self.expr = make_instance() # each element is a result of a function call 899 self.dispatch(node.assign) 900 901 # Enter the loop. 902 # Propagate attribute usage to branches. 903 904 self.new_branch(node) 905 self.dispatch(node.body) 906 907 self.resume_continuing_branches() 908 909 self.shelve_branch() 910 911 self.in_loop = in_loop 912 913 # A null branch is used to record a path around the loop. 914 915 self.new_branch(node.else_ or NullBranch()) 916 self.shelve_branch() 917 918 self.merge_branches() 919 920 # The else clause is evaluated outside any branch. 921 922 if node.else_ is not None: 923 self.dispatch(node.else_) 924 925 # Any suspended branches from the loop can now be resumed. 926 927 self.resume_broken_branches() 928 929 def visitFrom(self, node): 930 module = self.importer.load(node.modname, 1) 931 932 #if module is None: 933 # print "Warning:", node.modname, "not imported." 934 935 for name, alias in node.names: 936 if name != "*": 937 if module is not None and module.has_key(name): 938 attr = module[name] 939 self.store(alias or name, attr) 940 self.use_specific_attribute(module.full_name(), name) 941 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 942 self.importer.load(attr.get_value().name) 943 944 # Support the import of names from missing modules. 945 946 else: 947 self.store(alias or name, UnresolvedName(name, node.modname, self)) 948 else: 949 if module is not None: 950 for n in module.keys(): 951 attr = module[n] 952 self.store(n, attr) 953 self.use_specific_attribute(module.full_name(), n) 954 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 955 self.importer.load(attr.get_value().name) 956 957 def visitFunction(self, node): 958 return self._visitFunction(node, node.name) 959 960 visitGenExpr = OP 961 962 visitGenExprFor = NOP 963 964 visitGenExprIf = NOP 965 966 visitGenExprInner = NOP 967 968 def visitGetattr(self, node): 969 expr = self.dispatch(node.expr) 970 attrname = node.attrname 971 return self._visitAttr(expr, attrname, node) 972 973 def visitGlobal(self, node): 974 if self.namespaces: 975 for name in node.names: 976 ns = self.namespaces[-1] 977 if not ns.make_global(name): 978 raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) 979 980 # The name is recorded in an earlier process. 981 982 def visitIf(self, node): 983 self.use_name("__bool__", node) 984 self.new_branchpoint() 985 986 # Propagate attribute usage to branches. 987 988 for test, body in node.tests: 989 self.dispatch(test) 990 991 self.new_branch(body) 992 self.dispatch(body) 993 self.shelve_branch() 994 995 # Maintain a branch for the else clause. 996 997 self.new_branch(node.else_ or NullBranch()) 998 if node.else_ is not None: 999 self.dispatch(node.else_) 1000 self.shelve_branch() 1001 1002 self.merge_branches() 1003 1004 def visitIfExp(self, node): 1005 self.use_name("__bool__", node) 1006 self.new_branchpoint() 1007 1008 # Propagate attribute usage to branches. 1009 1010 self.dispatch(node.test) 1011 1012 self.new_branch(node.then) 1013 self.dispatch(node.then) 1014 self.shelve_branch() 1015 1016 self.new_branch(node.else_) 1017 self.dispatch(node.else_) 1018 self.shelve_branch() 1019 1020 self.merge_branches() 1021 return make_instance() # either outcome is possible 1022 1023 def visitImport(self, node): 1024 for name, alias in node.names: 1025 if alias is not None: 1026 module = self.importer.load(name, 1) or UnresolvedName(None, name, self) 1027 self.store(alias, module) 1028 else: 1029 module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) 1030 self.store(name.split(".")[0], module) 1031 1032 visitInvert = _visitUnary 1033 1034 def visitKeyword(self, node): 1035 self.dispatch(node.expr) 1036 self._visitConst(node.name) 1037 self.keyword_names.add(node.name) 1038 1039 def visitLambda(self, node): 1040 fn = self._visitFunction(node, None) 1041 self.use_specific_attribute(None, fn.name) 1042 return fn 1043 1044 visitLeftShift = _visitBinary 1045 1046 def visitList(self, node): 1047 self.use_specific_attribute("__builtins__", "list") 1048 return self.OP(node) 1049 1050 def visitListComp(self, node): 1051 for qual in node.quals: 1052 self.dispatch(qual) 1053 self.dispatch(node.expr) 1054 return make_instance() 1055 1056 def visitListCompFor(self, node): 1057 self.new_branchpoint() 1058 1059 # Declare names which will be used by generated code. 1060 1061 self.use_name("__iter__", node.list) 1062 self.use_name("next") 1063 1064 in_loop = self.in_loop 1065 self.in_loop = 1 1066 self.dispatch(node.list) 1067 1068 # NOTE: Could generate AST nodes for the actual operations instead of 1069 # NOTE: manually generating code in micropython.ast. 1070 1071 self.expr = make_instance() # each element is a result of a function call 1072 self.dispatch(node.assign) 1073 1074 # Enter the loop. 1075 # Propagate attribute usage to branches. 1076 1077 self.new_branch(node) 1078 1079 for if_ in node.ifs: 1080 self.dispatch(if_) 1081 1082 self.shelve_branch() 1083 self.in_loop = in_loop 1084 1085 self.merge_branches() 1086 1087 visitListCompIf = TEST_NOP 1088 1089 visitMod = _visitBinary 1090 1091 def visitModule(self, node): 1092 1093 # Make a back reference from the node for code generation. 1094 1095 node.unit = self 1096 return self.dispatch(node.node) 1097 1098 visitMul = _visitBinary 1099 1100 def visitName(self, node): 1101 return self.get_namespace().get_using_node(node.name, node) or make_instance() 1102 1103 visitNot = TEST_OP 1104 1105 visitOr = TEST_OP 1106 1107 visitPass = NOP 1108 1109 visitPower = _visitBinary 1110 1111 def _visitPrint(self, node, function_name): 1112 self.NOP(node) 1113 self.use_specific_attribute("__builtins__", function_name) 1114 1115 def visitPrint(self, node): 1116 self._visitPrint(node, "_print") 1117 1118 def visitPrintnl(self, node): 1119 self._visitPrint(node, "_printnl") 1120 1121 visitRaise = NOP_ABANDON 1122 1123 visitReturn = NOP_ABANDON 1124 1125 visitRightShift = _visitBinary 1126 1127 def visitSlice(self, node): 1128 return self._visitOperator(node, self.in_assignment and "AssSlice" or "Slice") 1129 1130 visitSliceobj = OP 1131 1132 def visitStmt(self, node): 1133 for n in node.nodes: 1134 self.dispatch(n) 1135 1136 visitSub = _visitBinary 1137 1138 def visitSubscript(self, node): 1139 return self._visitOperator(node, self.in_assignment and "AssSubscript" or "Subscript") 1140 1141 def visitTryExcept(self, node): 1142 self.dispatch(node.body) 1143 1144 self.new_branchpoint() 1145 1146 for name, var, n in node.handlers: 1147 self.new_branch(node) 1148 1149 # Establish the local for the handler. 1150 1151 if var is not None: 1152 self.dispatch(var) 1153 if n is not None: 1154 self.dispatch(n) 1155 1156 self.shelve_branch() 1157 1158 self.new_branch(node.else_ or NullBranch()) 1159 if node.else_ is not None: 1160 self.dispatch(node.else_) 1161 self.shelve_branch() 1162 1163 self.merge_branches() 1164 1165 visitTryFinally = NOP 1166 1167 visitTuple = OP 1168 1169 visitUnaryAdd = _visitUnary 1170 1171 visitUnarySub = _visitUnary 1172 1173 def visitWhile(self, node): 1174 self.use_name("__bool__", node) 1175 self.new_branchpoint(node) 1176 1177 # Propagate attribute usage to branches. 1178 1179 in_loop = self.in_loop 1180 self.in_loop = 1 1181 1182 # The test is evaluated initially and again in the loop. 1183 1184 self.dispatch(node.test) 1185 1186 self.new_branch(node) 1187 self.dispatch(node.body) 1188 1189 self.resume_continuing_branches() 1190 1191 self.dispatch(node.test) 1192 self.shelve_branch() 1193 1194 self.in_loop = in_loop 1195 1196 # A null branch is used to record a path around the loop. 1197 1198 self.new_branch(node.else_ or NullBranch()) 1199 self.shelve_branch() 1200 1201 self.merge_branches() 1202 1203 # The else clause is evaluated outside any branch. 1204 1205 if node.else_ is not None: 1206 self.dispatch(node.else_) 1207 1208 # Any suspended branches from the loop can now be resumed. 1209 1210 self.resume_broken_branches() 1211 1212 visitWith = NOP 1213 1214 visitYield = NOP 1215 1216 # vim: tabstop=4 expandtab shiftwidth=4