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