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