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