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