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