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