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