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