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