1 #!/usr/bin/env python 2 3 """ 4 Program data structures. There are two separate kinds of structures: those with 5 context, which are the values manipulated by programs, and those without 6 context, which are typically constant things which are stored alongside the 7 program but which are wrapped in context-dependent structures in the running 8 program. 9 10 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie <paul@boddie.org.uk> 11 12 This program is free software; you can redistribute it and/or modify it under 13 the terms of the GNU General Public License as published by the Free Software 14 Foundation; either version 3 of the License, or (at your option) any later 15 version. 16 17 This program is distributed in the hope that it will be useful, but WITHOUT 18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 19 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 20 details. 21 22 You should have received a copy of the GNU General Public License along with 23 this program. If not, see <http://www.gnu.org/licenses/>. 24 25 -------- 26 27 The principal value structure class in this module is the Attr class which 28 represents attributes of objects and retains the context of each reference to an 29 attribute. This class models program behaviour at run-time. 30 31 The central data structure classes in this module are the following: 32 33 * Class 34 * Function 35 * Module 36 37 All of the above support the Naming interface either explicitly or through 38 general conformance, meaning that all can be asked to provide their 'full_name' 39 using the method of that name. 40 41 Additionally, all of the above also support a dictionary interface in order to 42 access names within their defined scopes. Specific methods also exist in order 43 to distinguish between certain kinds of attributes: 44 45 * Class: class_attributes, all_class_attributes, instance_attributes, all_attributes 46 * Function: parameters, locals, all_locals 47 * Module: module_attributes 48 49 These specific methods are useful in certain situations. 50 51 The above classes also provide an 'astnode' attribute, indicating the AST node 52 where each such object is defined. 53 """ 54 55 from micropython.program import ReplaceableContext, PlaceholderContext 56 from micropython.basicdata import * 57 from micropython.errors import * 58 from micropython.objectset import * 59 from micropython.types import * 60 import sys 61 62 try: 63 set 64 except NameError: 65 from sets import Set as set 66 67 class NamespaceDict: 68 69 "A mix-in providing dictionary methods." 70 71 def __init__(self, module=None): 72 self.module = module 73 self.namespace = {} 74 self.globals = set() 75 self.lambdas = {} # only really useful for functions 76 self.finalised = 0 77 78 # Attributes accessed on objects, potentially narrowing their types. 79 # Specific namespaces should define known names during initialisation. 80 # For example, functions can define names belonging to parameters. 81 82 # Attribute users, defining names which use attributes. 83 84 self.attribute_users = [{}] # stack of assignments and branches 85 self.attribute_user_shelves = [] 86 87 # Suspended user details plus loop details. 88 89 self.suspended_broken_users = [] # stack of lists of user dicts 90 self.suspended_continuing_users = [] # stack of lists of user dicts 91 92 # Scope usage, indicating the origin of names. 93 94 self.scope_usage = [{}] # stack of scope usage 95 self.scope_shelves = [] 96 97 # Abandoned usage, useful for reviving usage for exception handlers. 98 99 self.abandoned_users = [[]] # stack of lists of users 100 101 # Define attribute usage to identify active program sections. 102 # Attribute users are AST nodes defining names. 103 104 self.all_attribute_users = set() 105 106 # Attribute/name definition and access. 107 108 def __delitem__(self, name): 109 del self.namespace[name] 110 111 def has_key(self, name): 112 return self.namespace.has_key(name) 113 114 def keys(self): 115 return self.namespace.keys() 116 117 def values(self): 118 return self.namespace.values() 119 120 def items(self): 121 return self.namespace.items() 122 123 def __getitem__(self, name): 124 return self.namespace[name] 125 126 def get(self, name, default=None): 127 return self.namespace.get(name, default) 128 129 # Administrative methods. 130 131 def finalise(self, objtable): 132 self.finalise_attributes() 133 self.finalise_users(objtable) 134 135 def items_for_vacuum(self): 136 return self.items() + self.lambdas.items() 137 138 def vacuum_item(self, name): 139 if self.has_key(name): 140 del self[name] 141 return 1 142 else: 143 return 0 144 145 def add_lambda(self, obj): 146 attr = Attr(None, self, obj.name) 147 attr.update([self.get_context_and_value(obj)], single_assignment=1) 148 self.lambdas[obj.name] = attr 149 150 # Specialised access methods. 151 152 def get_using_node(self, name, node): 153 154 """ 155 Access the given 'name' through this namespace, making use of the module 156 and builtins namespaces if necessary, annotating the given 'node' with 157 the scope involved. 158 """ 159 160 attr, scope, full_name = self._get_with_scope(name) 161 162 if scope is not None: 163 node._scope = scope 164 self.note_scope(name, scope) 165 166 if full_name is not None and (scope != "local" or isinstance(self, (Class, Module))): 167 self.use_specific_attribute(full_name, name) 168 169 return attr 170 171 def _get_with_scope(self, name, external=0): 172 173 """ 174 Find the source of the given 'name', returning the attribute object, 175 scope (constant, local, global or builtins), and the full name of the 176 source namespace (or None for constants). 177 178 If the optional 'external' flag is set to a true value, only external 179 (non-local) namespaces will be involved in the search. 180 """ 181 182 module = self.module 183 builtins = module and module.builtins or None 184 importer = module and module.importer or None 185 186 # Constants. 187 188 if importer is not None and importer.predefined_constants.has_key(name): 189 return importer.get_predefined_constant(name), "constant", None 190 191 # Locals. 192 193 elif not external and self.has_key(name): 194 return self[name], "local", self.full_name() 195 196 # Globals. 197 198 elif module is not None and module is not self and module.has_key(name): 199 return module[name], "global", module.full_name() 200 201 # Builtins. 202 203 elif builtins is not None and builtins.has_key(name): 204 return builtins[name], "builtins", builtins.full_name() 205 206 # Unknown. 207 208 else: 209 return None, None, None 210 211 # Attribute definition methods. 212 213 def __setitem__(self, name, value): 214 self.set(name, value) 215 216 def set(self, name, value, single_assignment=1): 217 218 """ 219 A more powerful set operation, making 'name' refer to 'value' whilst 220 indicating whether a 'single_assignment' (true by default) occurs in 221 this operation (or whether the operation covers potentially many 222 assignments in the lifetime of a program). 223 """ 224 225 if value is None: 226 print >>sys.stderr, "Warning: name %r in namespace %r has an unknown value (evaluated to None)." % (name, self.full_name()) 227 value = make_instance() 228 229 if name in self.globals: 230 self.module.set(name, value, 0) 231 else: 232 self._set(name, value, single_assignment) 233 self.define_scope(name, "local") 234 235 def set_module(self, name, value): 236 237 """ 238 A specialised set operation, making 'name' refer to 'value' in the 239 context of making a module reference available in association with 240 'name' as part of the import of that module or a submodule of that 241 module. 242 """ 243 244 self._set(name, value, 1) 245 246 def _set(self, name, attr_or_value, single_assignment=1): 247 248 """ 249 The underlying set operation associating 'name' with the given 250 'attr_or_value'. 251 See: docs/assignment.txt 252 """ 253 254 # Add and/or obtain the namespace entry. 255 256 if not self.namespace.has_key(name): 257 self.namespace[name] = Attr(None, self, name) 258 259 attr = self.namespace[name] 260 self._set_using_attr(attr, attr_or_value, single_assignment) 261 262 def _set_using_attr(self, attr, attr_or_value, single_assignment=1): 263 264 # Handle attribute assignment as well as assignment of basic objects. 265 # Attempt to fix the context if not explicitly defined. 266 267 if isinstance(attr_or_value, Attr): 268 context_values = self.get_updated_context_values(attr_or_value.context_values) 269 else: 270 context_values = self.get_updated_context_values([self.get_context_and_value(attr_or_value)]) 271 272 attr.update(context_values, single_assignment) 273 274 def get_context_and_value(self, value): 275 276 "Return a context, value tuple for the given 'value'." 277 278 # Functions have a replaceable context. 279 280 if isinstance(value, Function): 281 return (ReplaceableContext, value) 282 283 # Classes use placeholder contexts which cannot be replaced but which 284 # do not communicate useful contextual information. 285 286 elif isinstance(value, Class): 287 return (PlaceholderContext, value) 288 289 # Other values employ themselves as the context. 290 291 else: 292 return (value, value) 293 294 def get_updated_context_values(self, context_values): 295 296 """ 297 Adapt the contexts found in the given 'context_values', returning a new 298 set. 299 See: docs/assignment.txt 300 """ 301 302 results = set() 303 304 for context, value in context_values: 305 306 # Set the context of instances to themselves. 307 308 if isinstance(value, Instance): 309 results.add((value, value)) 310 else: 311 results.add((context, value)) 312 313 return results 314 315 def make_global(self, name): 316 317 "Declare 'name' as a global in the current namespace." 318 319 if not self.namespace.has_key(name): 320 self.globals.add(name) 321 self.define_scope(name, "global") 322 return 1 323 else: 324 return 0 325 326 # Attribute positioning. 327 328 def attributes_as_list(self): 329 330 "Return the attributes in a list." 331 332 self.finalise_attributes() 333 l = [None] * len(self.keys()) 334 for attr in self.values(): 335 l[attr.position] = attr 336 return l 337 338 def finalise_attributes(self): 339 340 "Make sure all attributes are fully defined." 341 342 if self.finalised: 343 return 344 345 # The default action is to assign attribute positions sequentially. 346 347 for i, attr in enumerate(self.values()): 348 attr.position = i 349 350 self.finalised = 1 351 352 def unfinalise_attributes(self): 353 354 "Open attribute definitions to editing and subsequent finalisation." 355 356 self.finalised = 0 357 358 # Attribute usage methods. 359 360 def finalise_attribute_usage(self): 361 362 "Propagate attribute usage for the namespace to the importer." 363 364 module = self.module 365 importer = module and module.importer 366 367 if importer is not None: 368 369 # Visit each user and examine the attribute usage for each name. 370 371 for user in self.all_attribute_users: 372 373 # First, visit the contributors and combine their attribute 374 # usage with the usage recorded directly on the user. 375 376 self.get_usage_from_contributors(user) 377 378 # Record the defining user on each contributor. 379 380 for contributor in user._attrcontributors: 381 contributor._attrdefs.append(user) 382 383 # Then, tell the importer about the usage. 384 385 for name in user._attrnames.keys(): 386 387 # Only provide information about names defined by this user. 388 389 usage = user._attrcombined.get(name, []) 390 391 # Skip reporting where no actual usage occurs. 392 393 if usage is None: 394 continue 395 396 # Eliminate non-usage. 397 398 importer.use_names(user, name, tuple([attrnames for attrnames in usage if attrnames]), self.full_name()) 399 400 def finalise_users(self, objtable): 401 402 "Record the object types for generating guards." 403 404 # Visit each user and examine the attribute usage for each name. 405 406 for user in self.all_attribute_users: 407 user._attrtypes = self._deduce_types(user._attrcombined, objtable) 408 self._finalise_contributor(user, objtable) 409 410 def _finalise_contributors(self, node, objtable): 411 412 """ 413 Visit the contributing branches of 'node', finalising them using the 414 given 'objtable'. 415 """ 416 417 for contributor in node._attrbranches: 418 self._finalise_contributor(contributor, objtable) 419 420 def _finalise_contributor(self, node, objtable): 421 422 """ 423 Record the specific object types being used in various regions of a 424 program unit. 425 """ 426 427 if not hasattr(node, "_attrspecifictypes"): 428 merged = {} 429 430 # Get the combined usage information from the user definitions. 431 432 for user in node._attrdefs or [node]: 433 434 # Filter the usage for each name using the local usage 435 # information. 436 437 for name, usage in user._attrcombined.items(): 438 localusage = node._attrnames.get(name) 439 440 if usage and localusage: 441 if not merged.has_key(name): 442 merged[name] = ObjectSet() 443 444 for attrnames, value in usage.items(): 445 if attrnames and localusage.issubset(attrnames): 446 merged[name][attrnames] = value 447 448 node._attrmerged = merged 449 node._attrspecifictypes = self._deduce_types(node._attrmerged, objtable) 450 451 self._finalise_contributors(node, objtable) 452 453 def _deduce_types(self, usage, objtable): 454 455 """ 456 Deduce the types for names from the given attribute 'usage' and using 457 the given 'objtable'. 458 """ 459 460 attrtypes = {} 461 for name, combined_usage in usage.items(): 462 if combined_usage is not None: 463 objtypes = get_object_types_for_usage(combined_usage, objtable, name, self.full_name(), True) 464 if objtypes: 465 if isinstance(self, Function) and self.is_method() and name == "self": 466 objtypes = filter_using_self(objtypes, self.parent) 467 attrtypes[name] = objtypes 468 return attrtypes 469 470 def get_usage_from_contributors(self, node): 471 472 """ 473 Obtain usage information from the given 'node', combined with usage 474 details from its contributors, returning a tuple containing a set of all 475 contributors employed along with a dictionary mapping names to lists of 476 usage possibilities (each a collection of attribute names). 477 """ 478 479 unfinished = {} 480 481 if not hasattr(node, "_attrcombined"): 482 node._attrcombined = None 483 node._attrcontributors = None 484 485 for contributor in node._attrbranches: 486 487 # Get contributor details. 488 489 unfinished_contributors = self.get_usage_from_contributors(contributor) 490 491 # Collect unfinished contributors and affected nodes. 492 493 # Where the contributor is already set to None, a loop has 494 # occurred and this node will need to have its usage 495 # recalculated later for the unfinished contributor. 496 497 if contributor._attrcombined is None: 498 if not unfinished.has_key(contributor): 499 unfinished[contributor] = [] 500 unfinished[contributor].append(node) 501 continue 502 503 # Where the contributor provides usage details, it may also 504 # communicate unfinished contributor information. As a 505 # consequence, this node is also affected. 506 507 for unfinished_contributor, nodes in unfinished_contributors.items(): 508 if not unfinished.has_key(unfinished_contributor): 509 unfinished[unfinished_contributor] = nodes 510 else: 511 unfinished[unfinished_contributor] += nodes 512 513 if node not in unfinished[unfinished_contributor]: 514 unfinished[unfinished_contributor].append(node) 515 516 # Set the current state of the usage on this node. 517 518 node._attrcombined, node._attrcontributors = \ 519 self.get_usage_from_contributors_for_node(node) 520 521 # Complete unfinished contributors relying on this node. 522 523 if unfinished.has_key(node): 524 processed = set() 525 for contributor in unfinished[node]: 526 if not contributor in processed: 527 processed.add(contributor) 528 529 contributor._attrcombined, contributor._attrcontributors = \ 530 self.get_usage_from_contributors_for_node(contributor) 531 532 del unfinished[node] 533 534 return unfinished 535 536 def get_usage_from_contributors_for_node(self, node): 537 538 # Visit each contributor, gathering usage for each name. 539 540 contributor_usage = {} 541 all_contributions = [] 542 all_contributors = set() 543 544 for contributor in node._attrbranches: 545 usage = contributor._attrcombined 546 if usage is not None: 547 all_contributions.append(usage) 548 549 all_contributors.add(contributor) 550 contributors = contributor._attrcontributors 551 if contributors is not None: 552 all_contributors.update(contributors) 553 554 # Get contributed usage for each contributor. 555 # This gathers usage for each name such as {(a, b), (c, d)} and 556 # {(a, b), (e, f)} into a single set {(a, b), (c, d), (e, f)}. 557 558 update_mapping_dict(contributor_usage, all_contributions) 559 560 # Then get the resulting usage. 561 # First, make the current usage compatible with the contributed 562 # usage: this makes the attribute usage for each name merely one 563 # member in a list of many possibilities. 564 # Then, combine the current usage with the contributed usage. 565 # Thus, usage of {(f, g)} combined with {(a, b), (c, d)} would give 566 # {(f, g, a, b), (f, g, c, d)}. 567 568 return combine_mapping_dicts(deepen_mapping_dict(node._attrnames), contributor_usage), all_contributors 569 570 def use_attribute(self, name, attrname, value=None): 571 572 """ 573 Note usage on the attribute user 'name' of the attribute 'attrname', 574 noting an assignment if 'value' is specified. 575 """ 576 577 return self._use_attribute(name, attrname, value) 578 579 def use_specific_attribute(self, objname, attrname): 580 581 "Declare the usage on 'objname' of the given 'attrname'." 582 583 self._use_specific_attribute(objname, attrname) 584 585 # These shadow various methods in the InspectedModule class, and provide 586 # implementations generally. 587 588 def _use_specific_attribute(self, objname, attrname, from_name=None): 589 590 """ 591 Note attribute usage specifically on 'objname' - an object which is 592 known at inspection time - or in the current unit if 'objname' is None, 593 nominating a specific attribute 'attrname'. 594 595 This bypasses attribute user mechanisms. 596 """ 597 598 from_name = from_name or self.full_name() 599 objname = objname or from_name 600 module = self.module 601 importer = module and module.importer 602 603 if importer is not None: 604 importer.use_specific_name(objname, attrname, from_name) 605 606 def _use_attribute(self, name, attrname, value=None): 607 608 """ 609 Indicate the use of the given 'name' in this namespace of an attribute 610 with the given 'attrname'. If the optional 'value' is specified, an 611 assignment using the given 'value' is recorded. 612 """ 613 614 users = self.attribute_users[-1] 615 616 # If no users are defined for the name, it cannot be handled. 617 618 if not users.has_key(name): 619 return [] 620 621 # Add the usage to all current users. 622 623 for user in users[name]: 624 values = user._attrnames[name] 625 if values is None: 626 values = user._attrnames[name] = ObjectSet() 627 628 # Add an entry for the attribute, optionally with an assigned 629 # value. 630 631 values.add(attrname) 632 if value is not None: 633 values[attrname].add(value) 634 635 return users[name] 636 637 def _define_attribute_user(self, node): 638 639 """ 640 Define 'node' as the user of attributes, indicating the point where the 641 user is defined. 642 """ 643 644 name = node.name 645 self._define_attribute_user_for_name(node, name) 646 647 def _define_attribute_user_for_name(self, node, name): 648 649 "Define 'node' as the user of attributes for the given 'name'." 650 651 users = self.attribute_users[-1] 652 653 # This node overrides previous definitions. 654 655 users[name] = set([node]) 656 657 # Record the attribute combinations for the name. 658 659 self._init_attribute_user_for_name(node, name) 660 661 # Remember this user. 662 663 self.all_attribute_users.add(node) 664 665 def _init_attribute_user_for_name(self, node, name): 666 667 "Make sure that 'node' is initialised for 'name'." 668 669 self._init_attribute_user(node) 670 node._attrnames[name] = None 671 672 def _init_attribute_user(self, node): 673 674 # Attribute usage for names. 675 676 if not hasattr(node, "_attrnames"): 677 node._attrnames = {} 678 node._attrmerged = {} 679 680 # Branches contributing usage to this node. 681 682 if not hasattr(node, "_attrbranches"): 683 node._attrbranches = [] 684 685 # Definitions receiving usage from this node. 686 687 if not hasattr(node, "_attrdefs"): 688 node._attrdefs = [] 689 690 def _define_attribute_accessor(self, name, attrname, node, value): 691 692 # NOTE: Revisiting of nodes may occur for loops. 693 694 if not hasattr(node, "_attrusers"): 695 node._attrusers = set() 696 697 node._attrusers.update(self.use_attribute(name, attrname, value)) 698 node._username = name 699 700 # Branch management methods. 701 702 def _new_branchpoint(self, loop_node=None): 703 704 """ 705 Establish a new branchpoint where several control-flow branches diverge 706 and subsequently converge. 707 """ 708 709 self.attribute_user_shelves.append([]) 710 self.scope_shelves.append([]) 711 712 if loop_node is not None: 713 self.suspended_broken_users.append([]) 714 self.suspended_continuing_users.append((loop_node, [])) 715 716 def _new_branch(self, node): 717 718 """ 719 Establish a new control-flow branch, transferring attribute usage to 720 the new branch so that it may be augmented for each name locally. 721 722 Add the given 'node' as an active user to be informed of attribute 723 usage. 724 """ 725 726 attribute_users = self.attribute_users[-1] 727 728 # Define this node as the active attribute user for all currently 729 # defined names. 730 731 new_users = {} 732 733 for name in attribute_users.keys(): 734 new_users[name] = [node] 735 self._init_attribute_user_for_name(node, name) 736 737 self._init_attribute_user(node) 738 self.attribute_users.append(new_users) 739 740 # Add this user as a contributor to the previously active users. 741 742 self._connect_users_to_branch(attribute_users, node) 743 744 # Retain a record of scope usage. 745 746 scope_usage = {} 747 scope_usage.update(self.scope_usage[-1]) 748 self.scope_usage.append(scope_usage) 749 750 # Retain a record of abandoned branch users. 751 752 self.abandoned_users.append([]) 753 754 def _connect_users_to_branch(self, attribute_users, node): 755 756 """ 757 Given the 'attribute_users' mapping, connect the users referenced in the 758 mapping to the given branch 'node'. 759 """ 760 761 all_users = set() 762 763 for users in attribute_users.values(): 764 all_users.update(users) 765 766 for user in all_users: 767 self._init_attribute_user(user) 768 user._attrbranches.append(node) 769 770 def _abandon_branch(self, retain_branch=True): 771 772 """ 773 Abandon scope usage, permitting locally different scopes for names, 774 provided these cannot "escape" from the branch. 775 """ 776 777 attribute_users = self.attribute_users[-1] 778 779 self.attribute_users[-1] = {} 780 self.scope_usage[-1] = abandoned_branch_scope 781 782 if retain_branch: 783 self.abandoned_users[-1].append(attribute_users) 784 785 def _suspend_broken_branch(self): 786 787 """ 788 Suspend a branch for resumption after the current loop. 789 """ 790 791 attribute_users = self.attribute_users[-1] 792 793 users = self.suspended_broken_users[-1] 794 users.append(attribute_users) 795 self._abandon_branch(False) 796 797 def _suspend_continuing_branch(self): 798 799 """ 800 Suspend a branch for resumption after the current iteration. 801 """ 802 803 attribute_users = self.attribute_users[-1] 804 805 loop_node, users = self.suspended_continuing_users[-1] 806 users.append(attribute_users) 807 self._abandon_branch(False) 808 809 def _shelve_branch(self): 810 811 """ 812 Shelve the current control-flow branch, recording the attribute usage 813 for subsequent merging. If this branch should be abandoned, the usage 814 observations are still recorded but will not contribute to subsequent 815 observations after a merge. 816 """ 817 818 users = self.attribute_users.pop() 819 self.attribute_user_shelves[-1].append(users) 820 821 scope_usage = self.scope_usage.pop() 822 self.scope_shelves[-1].append(scope_usage) 823 824 def _merge_branches(self): 825 826 """ 827 Merge control-flow branches. This should find the users active within 828 each branch, which have been "shelved", and update the active users 829 dictionary with these contributions. 830 """ 831 832 # Combine the attribute users. This ensures that a list of users 833 # affected by attribute usage is maintained for the current branch. 834 835 all_shelved_users = self.attribute_user_shelves.pop() 836 new_users = merge_mapping_dicts(all_shelved_users) 837 self.attribute_users[-1] = new_users 838 839 # Abandoned users are retained for exception handling purposes. 840 841 all_abandoned_users = self.abandoned_users.pop() 842 new_abandoned_users = merge_mapping_dicts(all_abandoned_users) 843 self.abandoned_users[-1].append(new_abandoned_users) 844 845 # Combine the scope usage. 846 847 scope_usage = self.scope_usage[-1] 848 new_scope_usage = {} 849 850 all_scope_usage = self.scope_shelves.pop() 851 all_scope_names = set() 852 853 # Find all the names for whom scope information has been defined. 854 855 for shelved_usage in all_scope_usage: 856 all_scope_names.update(shelved_usage.keys()) 857 858 for shelved_usage in all_scope_usage: 859 for name in all_scope_names: 860 861 # Find the recorded scope for the name. 862 863 if shelved_usage.has_key(name): 864 scope = shelved_usage[name] 865 elif scope_usage.has_key(name): 866 scope = scope_usage[name] 867 868 # For abandoned branches, no scope is asserted for a name. 869 870 elif isinstance(shelved_usage, AbandonedBranchScope): 871 scope = None 872 873 # If no scope is recorded, find a suitable external source. 874 875 else: 876 attr, scope, full_name = self._get_with_scope(name, external=1) 877 878 # Attempt to record the scope, testing for conflicts. 879 880 if scope: 881 if not new_scope_usage.has_key(name): 882 new_scope_usage[name] = scope 883 else: 884 new_scope = new_scope_usage[name] 885 if new_scope != scope: 886 if isinstance(new_scope, ScopeConflict): 887 if isinstance(scope, ScopeConflict): 888 scopes = scope.scopes.union(new_scope.scopes) 889 else: 890 scopes = new_scope.scopes.union(set([scope])) 891 elif isinstance(scope, ScopeConflict): 892 scopes = scope.scopes.union(set([new_scope])) 893 else: 894 scopes = set([scope, new_scope]) 895 new_scope_usage[name] = ScopeConflict(scopes) 896 897 self.scope_usage[-1] = new_scope_usage 898 899 def _resume_broken_branches(self): 900 901 """ 902 Incorporate users from suspended broken branches into the current set of 903 active users. 904 """ 905 906 suspended_users = self.suspended_broken_users.pop() 907 current_users = self.attribute_users[-1] 908 new_users = merge_mapping_dicts(suspended_users + [current_users]) 909 self.attribute_users[-1] = new_users 910 911 def _resume_continuing_branches(self): 912 913 """ 914 Incorporate users from suspended continuing branches into the current 915 set of active users, merging usage from the latter with the former. 916 """ 917 918 loop_node, suspended_users = self.suspended_continuing_users.pop() 919 current_users = self.attribute_users[-1] 920 921 # Connect the suspended users to the loop node. 922 923 for users in suspended_users: 924 self._connect_users_to_branch(users, loop_node) 925 926 # Merge suspended branches with the current branch. 927 928 new_users = merge_mapping_dicts(suspended_users + [current_users]) 929 self.attribute_users[-1] = new_users 930 931 def _resume_abandoned_branches(self): 932 933 """ 934 Incorporate users from abandoned branches into the current set of active 935 users. The abandoned branches are taken from the containing branch. 936 """ 937 938 current_users = self.attribute_users[-1] 939 abandoned_users = self.abandoned_users[-2] 940 new_users = merge_mapping_dicts(abandoned_users + [current_users]) 941 self.attribute_users[-1] = new_users 942 943 # Scope usage methods. 944 945 def define_scope(self, name, scope): 946 947 """ 948 Define 'name' as being from the given 'scope' in the current namespace. 949 """ 950 951 self.scope_usage[-1][name] = scope 952 953 def note_scope(self, name, scope): 954 955 """ 956 Note usage of 'name' from the given 'scope' in the current namespace. 957 If a conflict has been recorded previously, raise an exception. 958 """ 959 960 scope_usage = self.scope_usage[-1] 961 962 if scope_usage.has_key(name): 963 found_scope = scope_usage[name] 964 if isinstance(found_scope, ScopeConflict): 965 raise InspectError("Scope conflict for %r: defined as %s." % ( 966 name, ", ".join(found_scope.scopes))) 967 968 scope_usage[name] = scope 969 970 def used_in_scope(self, name, scope): 971 972 """ 973 Return whether 'name' is used from the given 'scope' in the current 974 namespace. 975 """ 976 977 scope_usage = self.scope_usage[-1] 978 return scope_usage.get(name) == scope 979 980 # Special helper classes for usage and scope resolution. 981 982 class EmptyDict: 983 984 "A class providing dictionaries which retain no information." 985 986 def has_key(self, name): 987 return 0 988 989 def __setitem__(self, name, value): 990 pass 991 992 def __getitem__(self, name): 993 raise KeyError, name 994 995 def get(self, name, default=None): 996 return default 997 998 def keys(self): 999 return [] 1000 1001 values = items = keys 1002 1003 class AbandonedBranchScope(EmptyDict): 1004 1005 """ 1006 A class providing a value or state for an abandoned branch distinct from an 1007 empty scope dictionary. 1008 """ 1009 1010 pass 1011 1012 abandoned_branch_scope = AbandonedBranchScope() 1013 1014 class ScopeConflict: 1015 1016 """ 1017 A scope conflict caused when different code branches contribute different 1018 sources of names. 1019 """ 1020 1021 def __init__(self, scopes): 1022 self.scopes = scopes 1023 1024 class NullBranch: 1025 1026 "A class representing an attribute user for a non-existent branch." 1027 1028 pass 1029 1030 # Program data structures. 1031 1032 class Attr: 1033 1034 "An attribute entry having a context." 1035 1036 def __init__(self, position, parent, name): 1037 1038 """ 1039 Initialise the attribute with the given 'position' within the collection 1040 of attributes of its 'parent', indicating its 'name'. 1041 """ 1042 1043 self.position = position 1044 self.parent = parent 1045 self.name = name 1046 1047 # Possible values. 1048 1049 self.context_values = set() 1050 1051 # Number of assignments per name. 1052 1053 self.assignments = None 1054 1055 # Value-related methods. 1056 1057 def get_contexts(self): 1058 return [c for (c, v) in self.context_values] 1059 1060 def get_values(self): 1061 return [v for (c, v) in self.context_values] 1062 1063 def get_context(self): 1064 1065 "Get the context referenced by the attribute." 1066 1067 if self.assignments == 1 and len(self.context_values) == 1: 1068 return self.get_contexts()[0] 1069 else: 1070 return None 1071 1072 def get_value(self): 1073 1074 "Get the value referenced by the attribute." 1075 1076 if self.assignments == 1 and len(self.context_values) == 1: 1077 return self.get_values()[0] 1078 else: 1079 return None 1080 1081 __call__ = get_value # convenient access to any single value 1082 1083 def update(self, context_values, single_assignment): 1084 1085 """ 1086 Update the attribute, adding the 'context_values' provided to the 1087 known details associated with the attribute, changing the number of 1088 assignments according to the 'single_assignment' status of the 1089 operation, where a true value indicates that only one assignment is 1090 associated with the update, and a false value indicates that potentially 1091 many assignments may be involved. 1092 """ 1093 1094 if self.context_values.issuperset(context_values) and \ 1095 not (make_instance(), make_instance()) in context_values: 1096 return 1097 1098 self.update_assignments(len(set(context_values)), single_assignment) 1099 self.context_values.update(context_values) 1100 1101 def update_assignments(self, n, single_assignment): 1102 if self.assignments is None: 1103 if single_assignment: 1104 self.assignments = n 1105 else: 1106 self.assignments = AtLeast(n) 1107 else: 1108 if single_assignment: 1109 self.assignments += n 1110 else: 1111 self.assignments += AtLeast(n) 1112 1113 def is_constant(self): 1114 1115 """ 1116 Return whether this attribute references something that can be regarded 1117 as being constant within a particular scope. 1118 """ 1119 1120 return self.assignments == 1 1121 1122 def is_strict_constant(self): 1123 1124 """ 1125 Return whether this attribute references something that can be regarded 1126 as being constant. 1127 """ 1128 1129 value = self.get_value() 1130 return not (value is None or (isinstance(value, Instance) and not isinstance(value, Constant))) 1131 1132 def is_static_attribute(self): 1133 1134 """ 1135 Return whether this attribute is defined on a fixed/static object such 1136 as a class or a module. 1137 """ 1138 1139 return isinstance(self.parent, (Class, Module)) 1140 1141 def defines_ambiguous_class(self): 1142 1143 "Return whether this attribute defines more than one class." 1144 1145 if self.assignments > 1: 1146 have_class = 0 1147 for obj in self.get_values(): 1148 if isinstance(obj, Class): 1149 if have_class: 1150 return 1 1151 have_class = 1 1152 1153 return 0 1154 1155 def defined_within_hierarchy(self): 1156 1157 """ 1158 Return whether the parent and context of the attribute belong to the 1159 same class hierarchy. 1160 """ 1161 1162 # Must be defined within a class. 1163 1164 if isinstance(self.parent, Class): 1165 1166 # To be sure, all contexts must be classes and be the same as the 1167 # parent, or be a superclass of the parent, or be a subclass of the 1168 # parent. 1169 1170 for context in self.get_contexts(): 1171 if not ( 1172 isinstance(context, Class) and ( 1173 context is self.parent or 1174 context.has_subclass(self.parent) or 1175 self.parent.has_subclass(context)) 1176 ): 1177 return 0 1178 1179 return 1 1180 1181 # Instance attributes are not defined within a hierarchy. 1182 1183 else: 1184 return 0 1185 1186 def defined_outside_hierarchy(self): 1187 1188 """ 1189 Return whether the parent and context of the attribute never belong to 1190 the same class hierarchy. 1191 """ 1192 1193 # Must be defined within a class. 1194 1195 if isinstance(self.parent, Class): 1196 1197 # To be sure, all contexts must be classes and be the same as the 1198 # parent, or be a superclass of the parent, or be a subclass of the 1199 # parent. 1200 1201 for context in self.get_contexts(): 1202 if not ( 1203 isinstance(context, Class) and not ( 1204 context is self.parent or 1205 context.has_subclass(self.parent) or 1206 self.parent.has_subclass(context)) 1207 ): 1208 return 0 1209 1210 return 1 1211 1212 # Instance attributes are not defined within a hierarchy. 1213 1214 else: 1215 return 0 1216 1217 def __repr__(self): 1218 if self.position is not None: 1219 position = "at %r, " % self.position 1220 else: 1221 position = "" 1222 return "<attribute %s.%s (%sassigned %r)>" % ( 1223 shortrepr(self.parent), self.name, 1224 position, self.assignments 1225 ) 1226 1227 def __shortrepr__(self): 1228 return "%s.%s (at %r)" % (shortrepr(self.parent), self.name, self.position) 1229 1230 def _context_values_str(self): 1231 l = [] 1232 for (c, v) in self.context_values: 1233 l.append("(c=%s, v=%s)" % (shortrepr(c), shortrepr(v))) 1234 return ", ".join(l) 1235 1236 class Class(NamespaceDict, Naming, Constant): 1237 1238 "A base class for common/normal classes and the type class." 1239 1240 def __init__(self, name, parent=None, module=None, node=None): 1241 1242 """ 1243 Initialise the class with the given 'name', optional 'parent' object, 1244 'module' and AST 'node'. The optional information must be set at a later 1245 point using the 'set_context' method if omitted. 1246 """ 1247 1248 NamespaceDict.__init__(self, module) 1249 self.name = name 1250 self.parent = parent 1251 self.astnode = node 1252 1253 # Superclasses, descendants and attributes. 1254 1255 self.bases = [] 1256 self.descendants = set() 1257 self.instattr = set() # instance attributes 1258 self.instattr_tentative = set() # tentative/suspected instance attributes 1259 self.relocated = set() # attributes which do not have the same 1260 # position as those of the same name in 1261 # some superclasses 1262 1263 # Caches. 1264 1265 self.reset_caches() 1266 1267 # Image generation details. 1268 1269 self.location = None 1270 self.code_location = None 1271 self.code_body_location = None # corresponds to the instantiator 1272 1273 self.instantiator = None 1274 self.instance_template_location = None # for creating instances at run-time 1275 1276 # Program-related details. 1277 1278 self.blocks = None 1279 self.temp_usage = 0 1280 self.local_usage = 0 1281 self.all_local_usage = 0 1282 1283 # Add an attribute to this class for use by instances. 1284 1285 self.set("__class__", self) 1286 1287 def set_context(self, parent, module, node): 1288 1289 "Set the 'parent', 'module' and 'node' of a class created in advance." 1290 1291 self.parent = parent 1292 self.module = module 1293 self.astnode = node 1294 1295 def reset_caches(self): 1296 1297 "Reset the caches." 1298 1299 self.all_instattr = None # cache for instance_attributes 1300 self.all_instattr_names = None # from all_instattr 1301 self.all_classattr = None # cache for all_class_attributes 1302 self.all_classattr_names = None # from all_classattr 1303 self.allattr = None # cache for all_attributes 1304 self.allattr_names = None # from allattr 1305 1306 def __repr__(self): 1307 if self.location is not None: 1308 return "<class %s (at %r)>" % (shortrepr(self), self.location) 1309 else: 1310 return "<class %s>" % shortrepr(self) 1311 1312 def __shortrepr__(self): 1313 return "%s.%s" % (shortrepr(self.parent), self.name) 1314 1315 def get_body_block(self): 1316 return self.get_instantiator().blocks[0] 1317 1318 # Namespace-related methods. 1319 1320 def get_updated_context_values(self, context_values): 1321 1322 """ 1323 Adapt the contexts found in the given 'context_values', returning a new 1324 set. 1325 See: docs/assignment.txt 1326 """ 1327 1328 results = set() 1329 1330 for context, value in context_values: 1331 1332 # Change the ownership of functions. 1333 1334 if context is ReplaceableContext and value is not None and isinstance(value, Function): 1335 results.add((self, value)) 1336 else: 1337 results.add((context, value)) 1338 1339 return NamespaceDict.get_updated_context_values(self, results) 1340 1341 # Administrative methods. 1342 1343 def items_for_vacuum(self): 1344 1345 "Consider both class and instance attributes for vacuuming." 1346 1347 items = [] 1348 for name in self.instattr: 1349 items.append((name, None)) 1350 return NamespaceDict.items_for_vacuum(self) + items 1351 1352 def vacuum_item(self, name): 1353 1354 "Vacuum 'name' from the class or instance attribute collections." 1355 1356 # NOTE: Hack to prevent damage to exceptions. 1357 1358 if name == "_pc": 1359 return 0 1360 1361 if not NamespaceDict.vacuum_item(self, name): 1362 self.instattr.remove(name) 1363 return 1 1364 1365 def finalise_attributes(self): 1366 1367 "Make sure that all attributes are fully defined." 1368 1369 if self.finalised: 1370 return 1371 1372 self.finalise_class_attributes() 1373 self.finalise_instance_attributes() 1374 self.finalised = 1 1375 1376 def unfinalise_attributes(self): 1377 1378 "Open attribute definitions to editing and subsequent finalisation." 1379 1380 self.reset_caches() 1381 self.finalised = 0 1382 1383 # Convenience methods for accessing functions and methods. 1384 1385 def get_instantiator(self): 1386 1387 "Return a function which can be used to instantiate the class." 1388 1389 if self.instantiator is None: 1390 self.instantiator = self.get_init_method().as_instantiator() 1391 return self.instantiator 1392 1393 def get_init_method(self): 1394 return self.all_class_attributes()["__init__"].get_value() 1395 1396 # Class-specific methods. 1397 1398 def add_base(self, base): 1399 self.bases.append(base) 1400 base.add_descendant(self) 1401 1402 def add_instance_attribute(self, name, tentative=False): 1403 if tentative: 1404 self.instattr_tentative.add(name) 1405 else: 1406 self.instattr.add(name) 1407 1408 def add_descendant(self, cls): 1409 self.descendants.add(cls) 1410 for base in self.bases: 1411 base.add_descendant(cls) 1412 1413 def has_subclass(self, other): 1414 return other in self.descendants 1415 1416 def all_descendants(self): 1417 d = {} 1418 for cls in self.descendants: 1419 d[cls.full_name()] = cls 1420 return d 1421 1422 "Return the attribute names provided by this class only." 1423 1424 class_attribute_names = NamespaceDict.keys 1425 1426 def class_attributes(self): 1427 1428 "Return class attributes provided by this class only." 1429 1430 return dict(self) 1431 1432 def all_class_attribute_names(self): 1433 1434 "Return the attribute names provided by classes in this hierarchy." 1435 1436 if self.all_classattr_names is None: 1437 self.all_class_attributes() 1438 self.all_classattr_names = self.all_classattr.keys() 1439 return self.all_classattr_names 1440 1441 def all_class_attributes(self): 1442 1443 "Return all class attributes, indicating the class which provides them." 1444 1445 self.finalise_class_attributes() 1446 return self.all_classattr 1447 1448 def finalise_class_attributes(self): 1449 1450 "Make sure that the class attributes are fully defined." 1451 1452 if self.all_classattr is None: 1453 self.all_classattr = {} 1454 clsattr = {} 1455 1456 # Record provisional position information for attributes of this 1457 # class. 1458 1459 for name in self.class_attributes().keys(): 1460 1461 # Special case: __class__ has to be at position 0. 1462 1463 if name == "__class__": 1464 clsattr[name] = set([0]) 1465 else: 1466 clsattr[name] = set() # position not yet defined 1467 1468 reversed_bases = self.bases[:] 1469 reversed_bases.reverse() 1470 1471 # For the bases in reverse order, acquire class attribute details. 1472 1473 for cls in reversed_bases: 1474 for name, attr in cls.all_class_attributes().items(): 1475 self.all_classattr[name] = attr 1476 1477 # Record previous attribute information. 1478 1479 if clsattr.has_key(name): 1480 clsattr[name].add(attr.position) 1481 1482 # Record class attributes provided by this class and its bases, 1483 # along with their positions. 1484 1485 self.all_classattr.update(self.class_attributes()) 1486 1487 if clsattr: 1488 for i, name in enumerate(self._get_position_list(clsattr)): 1489 self.all_classattr[name].position = i 1490 1491 return self.all_classattr 1492 1493 def instance_attribute_names(self): 1494 1495 "Return the instance attribute names provided by the class." 1496 1497 if self.all_instattr_names is None: 1498 self.instance_attributes() 1499 return self.all_instattr_names 1500 1501 def instance_attributes(self): 1502 1503 "Return instance-only attributes for instances of this class." 1504 1505 self.finalise_instance_attributes() 1506 return self.all_instattr 1507 1508 def finalise_instance_attributes(self): 1509 1510 "Make sure that the instance attributes are fully defined." 1511 1512 # Eliminate tentative instance attributes that are actually class 1513 # attributes. 1514 1515 for attrname in self.all_class_attributes().keys(): 1516 if attrname in self.instattr_tentative: 1517 self.instattr_tentative.remove(attrname) 1518 1519 for cls in self.descendants: 1520 for attrname in cls.class_attribute_names(): 1521 if attrname in self.instattr_tentative: 1522 self.instattr_tentative.remove(attrname) 1523 1524 for attrname in self.instattr_tentative: 1525 self.instattr.add(attrname) 1526 1527 # Cache the attributes by converting the positioned attributes into a 1528 # dictionary. 1529 1530 if self.all_instattr is None: 1531 self.all_instattr = self._get_attributes() 1532 self.all_instattr_names = self.all_instattr.keys() 1533 1534 return self.all_instattr 1535 1536 def _get_attributes(self): 1537 1538 """ 1539 Return a dictionary mapping names to Attr instances incorporating 1540 information about their positions in the final instance structure. 1541 """ 1542 1543 instattr = {} 1544 1545 # Record provisional position information for attributes of this 1546 # instance. 1547 1548 for name in self.instattr: 1549 instattr[name] = set() # position not yet defined 1550 1551 reversed_bases = self.bases[:] 1552 reversed_bases.reverse() 1553 1554 # For the bases in reverse order, acquire instance attribute 1555 # details. 1556 1557 for cls in reversed_bases: 1558 for name, attr in cls.instance_attributes().items(): 1559 1560 # Record previous attribute information. 1561 1562 if instattr.has_key(name): 1563 instattr[name].add(attr.position) 1564 else: 1565 instattr[name] = set([attr.position]) 1566 1567 # Build the dictionary of attributes using the existing positions known 1568 # for each name. 1569 1570 d = {} 1571 for i, name in enumerate(self._get_position_list(instattr)): 1572 d[name] = Attr(i, make_instance(), name) 1573 return d 1574 1575 def _get_position_list(self, positions): 1576 1577 """ 1578 Return a list of attribute names for the given 'positions' mapping from 1579 names to positions, indicating the positions of the attributes in the 1580 final instance structure. 1581 """ 1582 1583 position_items = positions.items() 1584 namearray = [None] * len(position_items) 1585 1586 # Get the positions in ascending order of list size, with lists 1587 # of the same size ordered according to their smallest position 1588 # value. 1589 1590 position_items.sort(self._cmp_positions) 1591 1592 # Get the names in position order. 1593 1594 held = [] 1595 1596 for name, pos in position_items: 1597 pos = list(pos) 1598 pos.sort() 1599 if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: 1600 namearray[pos[0]] = name 1601 else: 1602 if pos: 1603 self.relocated.add(name) 1604 held.append((name, pos)) 1605 1606 for i, attr in enumerate(namearray): 1607 if attr is None: 1608 name, pos = held.pop() 1609 namearray[i] = name 1610 1611 return namearray 1612 1613 def _cmp_positions(self, a, b): 1614 1615 "Compare name plus position list operands 'a' and 'b'." 1616 1617 name_a, list_a = a 1618 name_b, list_b = b 1619 if len(list_a) < len(list_b): 1620 return -1 1621 elif len(list_a) > len(list_b): 1622 return 1 1623 elif not list_a: 1624 return 0 1625 else: 1626 return cmp(min(list_a), min(list_b)) 1627 1628 def all_attribute_names(self): 1629 1630 """ 1631 Return the names of all attributes provided by instances of this class. 1632 """ 1633 1634 self.allattr_names = self.allattr_names or self.all_attributes().keys() 1635 return self.allattr_names 1636 1637 def all_attributes(self): 1638 1639 """ 1640 Return all attributes for an instance, indicating either the class which 1641 provides them or that the instance itself provides them. 1642 1643 Note that __class__ acts like a class attribute for both instances and 1644 classes, and must be able to convey distinct values. 1645 """ 1646 1647 if self.allattr is None: 1648 self.allattr = {} 1649 self.allattr.update(self.all_class_attributes()) 1650 for name, attr in self.instance_attributes().items(): 1651 if self.allattr.has_key(name) and name != "__class__": 1652 print >>sys.stderr, "Warning: instance attribute %r in %r overrides class attribute." % (name, self) 1653 self.allattr[name] = attr 1654 return self.allattr 1655 1656 class TypeClass(Class): 1657 1658 "A special class for the type class." 1659 1660 pass 1661 1662 class CommonClass(Class): 1663 1664 "An inspected class." 1665 1666 pass 1667 1668 class Function(NamespaceDict, Naming, Constant): 1669 1670 "An inspected function." 1671 1672 def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, 1673 dynamic_def=0, module=None, node=None): 1674 1675 """ 1676 Initialise the function with the given 'name', 'parent', list of 1677 'argnames', list of 'defaults', the 'has_star' flag (indicating the 1678 presence of a * parameter), the 'has_dstar' flag (indicating the 1679 presence of a ** parameter), optional 'dynamic_def' (indicating that the 1680 function must be handled dynamically), optional 'module', and optional 1681 AST 'node'. 1682 """ 1683 1684 NamespaceDict.__init__(self, module) 1685 1686 if name is None: 1687 self.name = "lambda#%d" % new_lambda() 1688 self._is_lambda = 1 1689 else: 1690 self.name = name 1691 self._is_lambda = 0 1692 1693 self.parent = parent 1694 self.argnames = argnames 1695 self.defaults = defaults 1696 self.has_star = has_star 1697 self.has_dstar = has_dstar 1698 self.dynamic_def = dynamic_def 1699 self.astnode = node 1700 1701 # Initialise the positional names. 1702 1703 self.positional_names = self.argnames[:] 1704 if has_dstar: 1705 self.dstar_name = self.positional_names[-1] 1706 del self.positional_names[-1] 1707 if has_star: 1708 self.star_name = self.positional_names[-1] 1709 del self.positional_names[-1] 1710 1711 # Initialise default storage. 1712 # NOTE: This must be initialised separately due to the reliance on node 1713 # NOTE: visiting. 1714 1715 self.default_attrs = [] 1716 1717 # Initialise attribute usage. 1718 1719 if node is not None: 1720 for arg in argnames: 1721 1722 # Define attribute users. 1723 1724 self._define_attribute_user_for_name(node, arg) 1725 1726 # Caches. 1727 1728 self.localnames = None # cache for locals 1729 1730 # Add parameters to the namespace. 1731 1732 self._add_parameters(argnames) 1733 1734 # Image generation details. 1735 1736 self.dynamic = None 1737 self.location = None 1738 self.code_location = None 1739 self.code_body_location = None 1740 1741 # Program-related details. 1742 1743 self.blocks = None 1744 self.body_block = None 1745 1746 self.temp_usage = 0 1747 self.local_usage = 0 1748 self.all_local_usage = 0 1749 1750 def _add_parameters(self, argnames): 1751 1752 "Add 'argnames' to the namespace." 1753 1754 for name in argnames: 1755 self.set(name, make_instance()) 1756 1757 for name, top_level in self._flattened_parameters(argnames): 1758 if not top_level: 1759 self.set(name, make_instance()) 1760 1761 def _flattened_parameters(self, argnames, top_level=1): 1762 l = [] 1763 for name in argnames: 1764 if isinstance(name, tuple): 1765 l += self._flattened_parameters(name, 0) 1766 else: 1767 l.append((name, top_level)) 1768 return l 1769 1770 def __repr__(self): 1771 if self.location is not None: 1772 return "<function %s (at %r, code at %r)>" % ( 1773 shortrepr(self), self.location, self.code_location 1774 ) 1775 else: 1776 return "<function %s>" % shortrepr(self) 1777 1778 def __shortrepr__(self): 1779 return "%s.%s(%s)" % (shortrepr(self.parent), self.name, ", ".join(self.argnames)) 1780 1781 def get_body_block(self): 1782 return self.body_block 1783 1784 def is_lambda(self): 1785 return self._is_lambda 1786 1787 # Defaults-related methods. 1788 1789 def store_default(self, attr_or_value): 1790 1791 """ 1792 Reserve space for defaults, set outside the function, potentially on a 1793 dynamic basis, using the 'attr_or_value'. 1794 """ 1795 1796 attr = Attr(None, self, None) 1797 self._set_using_attr(attr, attr_or_value) 1798 self.default_attrs.append(attr) 1799 1800 def make_dynamic(self): 1801 1802 "Return whether this function must be handled using a dynamic object." 1803 1804 if self.dynamic is None: 1805 for attr in self.default_attrs: 1806 if not attr.is_strict_constant() and self.dynamic_def: 1807 self.dynamic = 1 1808 self._make_dynamic() 1809 break 1810 else: 1811 self.dynamic = 0 1812 1813 return self.dynamic 1814 1815 is_dynamic = make_dynamic 1816 1817 def _make_dynamic(self): 1818 1819 "Where functions have dynamic defaults, add a context argument." 1820 1821 name = "<context>" 1822 self.argnames.insert(0, name) 1823 self.positional_names.insert(0, name) 1824 self.set(name, make_instance()) 1825 1826 # Namespace-related methods. 1827 1828 def make_global(self, name): 1829 1830 "Declare 'name' as a global in the current namespace." 1831 1832 if name not in self.argnames and not self.has_key(name): 1833 self.globals.add(name) 1834 return 1 1835 else: 1836 return 0 1837 1838 def parameters(self): 1839 1840 """ 1841 Return a dictionary mapping parameter names to their position in the 1842 parameter list. 1843 """ 1844 1845 parameters = {} 1846 for i, name in enumerate(self.argnames): 1847 parameters[name] = i 1848 return parameters 1849 1850 def tuple_parameters(self, argnames=None): 1851 1852 """ 1853 Return a list of (position, parameter) entries corresponding to tuple 1854 parameters, where each parameter may either be a string or another such 1855 list of entries. 1856 """ 1857 1858 names = argnames or self.argnames 1859 1860 l = [] 1861 for i, name in enumerate(names): 1862 if isinstance(name, tuple): 1863 l.append((i, self.tuple_parameters(name))) 1864 elif argnames: 1865 l.append((i, name)) 1866 return l 1867 1868 def all_locals(self): 1869 1870 "Return a dictionary mapping names to local and parameter details." 1871 1872 return dict(self) 1873 1874 def locals(self): 1875 1876 "Return a dictionary mapping names to local details." 1877 1878 if self.localnames is None: 1879 self.localnames = {} 1880 self.localnames.update(self.all_locals()) 1881 for name in self.argnames: 1882 del self.localnames[name] 1883 return self.localnames 1884 1885 def is_method(self): 1886 1887 """ 1888 Return whether this function is a method explicitly defined in a class. 1889 """ 1890 1891 return isinstance(self.parent, Class) 1892 1893 def is_relocated(self, name): 1894 1895 """ 1896 Determine whether the given attribute 'name' is relocated for instances 1897 having this function as a method. 1898 """ 1899 1900 for cls in self.parent.descendants: 1901 if name in cls.relocated: 1902 return 1 1903 return 0 1904 1905 # Administrative methods. 1906 1907 def items_for_vacuum(self): 1908 return self.lambdas.items() 1909 1910 def vacuum_item(self, name): 1911 del self.lambdas[name] 1912 return 1 1913 1914 def finalise_attributes(self): 1915 1916 """ 1917 Make sure all attributes (locals) are fully defined. Note that locals 1918 are not attributes in the sense of class, module or instance attributes. 1919 Defaults are also finalised by this method. 1920 """ 1921 1922 if self.finalised: 1923 return 1924 1925 # Defaults. 1926 1927 for i, default in enumerate(self.default_attrs): 1928 default.position = i 1929 1930 # Parameters. 1931 1932 i = self._finalise_parameters() 1933 1934 if i is not None: 1935 nparams = i + 1 1936 else: 1937 nparams = 0 1938 1939 # Locals (and tuple parameter names). 1940 1941 i = None 1942 for i, attr in enumerate(self.locals().values()): 1943 attr.position = i + nparams 1944 1945 if i is not None: 1946 nothers = i + 1 1947 else: 1948 nothers = 0 1949 1950 self.local_usage = nothers 1951 self.all_local_usage = nparams + nothers 1952 self.finalised = 1 1953 1954 def _finalise_parameters(self): 1955 if not self.argnames: 1956 return None 1957 1958 for i, name in enumerate(self.argnames): 1959 self[name].position = i 1960 1961 return i 1962 1963 def as_instantiator(self): 1964 1965 "Make an instantiator function from a method, keeping all arguments." 1966 1967 function = Function(self.parent.name, self.parent.parent, self.argnames, self.defaults, 1968 self.has_star, self.has_dstar, self.dynamic_def, self.module) 1969 function.default_attrs = self.default_attrs 1970 return function 1971 1972 class UnresolvedName(NamespaceDict, Constant): 1973 1974 "A module, class or function which was mentioned but could not be imported." 1975 1976 def __init__(self, name, parent_name, module=None): 1977 NamespaceDict.__init__(self, module) 1978 self.name = name 1979 self.parent_name = parent_name 1980 self.parent = None 1981 1982 self.descendants = set() 1983 1984 def add_descendant(self, cls): 1985 self.descendants.add(cls) 1986 1987 def all_attributes(self): 1988 return {} 1989 1990 def all_attribute_names(self): 1991 return [] 1992 1993 all_class_attributes = class_attributes = instance_attributes = all_attributes 1994 all_class_attribute_names = class_attribute_names = instance_attribute_names = all_attribute_names 1995 1996 def __repr__(self): 1997 return "<unknown %s>" % shortrepr(self) 1998 1999 def __shortrepr__(self): 2000 return "%s.%s" % (self.parent_name, self.name) 2001 2002 def full_name(self): 2003 if self.name is not None: 2004 return self.parent_name + "." + self.name 2005 else: 2006 return self.parent_name 2007 2008 class Module(NamespaceDict, Constant): 2009 2010 "An inspected module's core details." 2011 2012 def __init__(self, name, importer): 2013 NamespaceDict.__init__(self, self) 2014 self.name = name 2015 self.importer = importer 2016 self.parent = None 2017 2018 # Original location details. 2019 2020 self.astnode = None 2021 2022 # Complete lists of classes and functions. 2023 2024 self.all_objects = set() 2025 2026 # A set of global names that cannot support combined attribute usage 2027 # observations because they may be modified within functions during 2028 # initialisation. 2029 2030 self.modified_names = set() 2031 2032 # Keyword records. 2033 2034 self.keyword_names = set() 2035 2036 # Image generation details. 2037 2038 self.location = None 2039 self.code_location = None 2040 2041 # Program-related details. 2042 2043 self.blocks = None 2044 self.temp_usage = 0 2045 self.local_usage = 0 2046 self.all_local_usage = 0 2047 2048 def full_name(self): 2049 return self.name 2050 2051 def __repr__(self): 2052 if self.location is not None: 2053 return "<module %s (at %r)>" % (self.name, self.location) 2054 else: 2055 return "<module %s>" % shortrepr(self) 2056 2057 def __shortrepr__(self): 2058 return self.name 2059 2060 # Attribute methods. 2061 2062 "Return the module attribute names provided by the module." 2063 2064 module_attribute_names = NamespaceDict.keys 2065 2066 def module_attributes(self): 2067 2068 "Return a dictionary mapping names to module attributes." 2069 2070 return dict(self) 2071 2072 def modify_name(self, name): 2073 2074 """ 2075 Modify a global 'name' invalidating various assumptions about its 2076 behaviour based on the module namespace being "safe" and suitable for 2077 attribute usage and constant value observations. 2078 """ 2079 2080 self.modified_names.add(name) 2081 2082 # Establish an attribute directly in the namespace if not present. 2083 2084 if not self.namespace.has_key(name): 2085 self.namespace[name] = Attr(None, self, name) 2086 2087 attr = self.namespace[name] 2088 attr.update_assignments(1, False) 2089 2090 # Attribute usage methods that apply to module globals in certain 2091 # circumstances. 2092 2093 def _use_attribute(self, name, attrname, value=None): 2094 if name not in self.modified_names: 2095 return NamespaceDict._use_attribute(self, name, attrname, value) 2096 else: 2097 self.importer.use_name(attrname, self.full_name(), value) 2098 return [] 2099 2100 def _define_attribute_user_for_name(self, node, name): 2101 if name not in self.modified_names: 2102 NamespaceDict._define_attribute_user_for_name(self, node, name) 2103 2104 def _init_attribute_user_for_name(self, node, name): 2105 if name not in self.modified_names: 2106 NamespaceDict._init_attribute_user_for_name(self, node, name) 2107 2108 def _define_attribute_accessor(self, name, attrname, node, value): 2109 if name not in self.modified_names: 2110 NamespaceDict._define_attribute_accessor(self, name, attrname, node, value) 2111 else: 2112 self.importer.use_name(attrname, self.full_name(), value) 2113 2114 # Pre-made instances. 2115 2116 type_class = TypeClass("type") # details to be filled in later 2117 2118 # Class construction. 2119 2120 def get_class(name, parent, module, node): 2121 2122 """ 2123 Return a Class instance for the class with the given 'name', 'parent', 2124 'module' and 'node'. 2125 """ 2126 2127 if name == "type" and module.full_name() == "__builtins__": 2128 type_class.set_context(parent, module, node) 2129 return type_class 2130 else: 2131 return CommonClass(name, parent, module, node) 2132 2133 # Lambda sequence numbering. 2134 2135 lambda_index = 0 2136 2137 def new_lambda(): 2138 2139 "Return a new sequence number for a lambda definition." 2140 2141 global lambda_index 2142 lambda_index += 1 2143 return lambda_index 2144 2145 # Special representations. 2146 2147 class AtLeast: 2148 2149 "A special representation for numbers of a given value or greater." 2150 2151 def __init__(self, count): 2152 self.count = count 2153 2154 def __eq__(self, other): 2155 return 0 2156 2157 __lt__ = __le__ = __eq__ 2158 2159 def __ne__(self, other): 2160 return 1 2161 2162 def __gt__(self, other): 2163 if isinstance(other, AtLeast): 2164 return 0 2165 else: 2166 return self.count > other 2167 2168 def __ge__(self, other): 2169 if isinstance(other, AtLeast): 2170 return 0 2171 else: 2172 return self.count >= other 2173 2174 def __iadd__(self, other): 2175 if isinstance(other, AtLeast): 2176 self.count += other.count 2177 else: 2178 self.count += other 2179 return self 2180 2181 def __radd__(self, other): 2182 if isinstance(other, AtLeast): 2183 return AtLeast(self.count + other.count) 2184 else: 2185 return AtLeast(self.count + other) 2186 2187 def __repr__(self): 2188 return "AtLeast(%r)" % self.count 2189 2190 # vim: tabstop=4 expandtab shiftwidth=4