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 if len(self.context_values) == 1: 1065 return self.get_contexts()[0] 1066 else: 1067 return None 1068 1069 def get_value(self): 1070 if len(self.context_values) == 1: 1071 return self.get_values()[0] 1072 else: 1073 return None 1074 1075 __call__ = get_value # convenient access to any single value 1076 1077 def update(self, context_values, single_assignment): 1078 1079 """ 1080 Update the attribute, adding the 'context_values' provided to the 1081 known details associated with the attribute, changing the number of 1082 assignments according to the 'single_assignment' status of the 1083 operation, where a true value indicates that only one assignment is 1084 associated with the update, and a false value indicates that potentially 1085 many assignments may be involved. 1086 """ 1087 1088 if self.context_values.issuperset(context_values) and \ 1089 not (make_instance(), make_instance()) in context_values: 1090 return 1091 1092 if self.assignments is None: 1093 if single_assignment: 1094 self.assignments = len(set(context_values)) 1095 else: 1096 self.assignments = AtLeast(len(set(context_values))) 1097 else: 1098 if single_assignment: 1099 self.assignments += 1 1100 else: 1101 self.assignments += AtLeast(1) 1102 1103 self.context_values.update(context_values) 1104 1105 def is_constant(self): 1106 1107 """ 1108 Return whether this attribute references something that can be regarded 1109 as being constant within a particular scope. 1110 """ 1111 1112 return self.assignments == 1 1113 1114 def is_strict_constant(self): 1115 1116 """ 1117 Return whether this attribute references something that can be regarded 1118 as being constant. 1119 """ 1120 1121 value = self.get_value() 1122 return not (value is None or (isinstance(value, Instance) and not isinstance(value, Constant))) 1123 1124 def is_static_attribute(self): 1125 1126 """ 1127 Return whether this attribute is defined on a fixed/static object such 1128 as a class or a module. 1129 """ 1130 1131 return isinstance(self.parent, (Class, Module)) 1132 1133 def defines_ambiguous_class(self): 1134 1135 "Return whether this attribute defines more than one class." 1136 1137 if self.assignments > 1: 1138 have_class = 0 1139 for obj in self.get_values(): 1140 if isinstance(obj, Class): 1141 if have_class: 1142 return 1 1143 have_class = 1 1144 1145 return 0 1146 1147 def defined_within_hierarchy(self): 1148 1149 """ 1150 Return whether the parent and context of the attribute belong to the 1151 same class hierarchy. 1152 """ 1153 1154 # Must be defined within a class. 1155 1156 if isinstance(self.parent, Class): 1157 1158 # To be sure, all contexts must be classes and be the same as the 1159 # parent, or be a superclass of the parent, or be a subclass of the 1160 # parent. 1161 1162 for context in self.get_contexts(): 1163 if not ( 1164 isinstance(context, Class) and ( 1165 context is self.parent or 1166 context.has_subclass(self.parent) or 1167 self.parent.has_subclass(context)) 1168 ): 1169 return 0 1170 1171 return 1 1172 1173 # Instance attributes are not defined within a hierarchy. 1174 1175 else: 1176 return 0 1177 1178 def defined_outside_hierarchy(self): 1179 1180 """ 1181 Return whether the parent and context of the attribute never belong to 1182 the same class hierarchy. 1183 """ 1184 1185 # Must be defined within a class. 1186 1187 if isinstance(self.parent, Class): 1188 1189 # To be sure, all contexts must be classes and be the same as the 1190 # parent, or be a superclass of the parent, or be a subclass of the 1191 # parent. 1192 1193 for context in self.get_contexts(): 1194 if not ( 1195 isinstance(context, Class) and not ( 1196 context is self.parent or 1197 context.has_subclass(self.parent) or 1198 self.parent.has_subclass(context)) 1199 ): 1200 return 0 1201 1202 return 1 1203 1204 # Instance attributes are not defined within a hierarchy. 1205 1206 else: 1207 return 0 1208 1209 def __repr__(self): 1210 if self.position is not None: 1211 position = "at %r, " % self.position 1212 else: 1213 position = "" 1214 return "<attribute %s.%s (%sassigned %r)>" % ( 1215 shortrepr(self.parent), self.name, 1216 position, self.assignments 1217 ) 1218 1219 def __shortrepr__(self): 1220 return "%s.%s (at %r)" % (shortrepr(self.parent), self.name, self.position) 1221 1222 def _context_values_str(self): 1223 l = [] 1224 for (c, v) in self.context_values: 1225 l.append("(c=%s, v=%s)" % (shortrepr(c), shortrepr(v))) 1226 return ", ".join(l) 1227 1228 class Class(NamespaceDict, Naming, Constant): 1229 1230 "A base class for common/normal classes and the type class." 1231 1232 def __init__(self, name, parent=None, module=None, node=None): 1233 1234 """ 1235 Initialise the class with the given 'name', optional 'parent' object, 1236 'module' and AST 'node'. The optional information must be set at a later 1237 point using the 'set_context' method if omitted. 1238 """ 1239 1240 NamespaceDict.__init__(self, module) 1241 self.name = name 1242 self.parent = parent 1243 self.astnode = node 1244 1245 # Superclasses, descendants and attributes. 1246 1247 self.bases = [] 1248 self.descendants = set() 1249 self.instattr = set() # instance attributes 1250 self.instattr_tentative = set() # tentative/suspected instance attributes 1251 self.relocated = set() # attributes which do not have the same 1252 # position as those of the same name in 1253 # some superclasses 1254 1255 # Caches. 1256 1257 self.reset_caches() 1258 1259 # Image generation details. 1260 1261 self.location = None 1262 self.code_location = None 1263 self.code_body_location = None # corresponds to the instantiator 1264 1265 self.instantiator = None 1266 self.instance_template_location = None # for creating instances at run-time 1267 1268 # Program-related details. 1269 1270 self.blocks = None 1271 self.temp_usage = 0 1272 self.local_usage = 0 1273 self.all_local_usage = 0 1274 1275 # Add an attribute to this class for use by instances. 1276 1277 self.set("__class__", self) 1278 1279 def set_context(self, parent, module, node): 1280 1281 "Set the 'parent', 'module' and 'node' of a class created in advance." 1282 1283 self.parent = parent 1284 self.module = module 1285 self.astnode = node 1286 1287 def reset_caches(self): 1288 1289 "Reset the caches." 1290 1291 self.all_instattr = None # cache for instance_attributes 1292 self.all_instattr_names = None # from all_instattr 1293 self.all_classattr = None # cache for all_class_attributes 1294 self.all_classattr_names = None # from all_classattr 1295 self.allattr = None # cache for all_attributes 1296 self.allattr_names = None # from allattr 1297 1298 def __repr__(self): 1299 if self.location is not None: 1300 return "<class %s (at %r)>" % (shortrepr(self), self.location) 1301 else: 1302 return "<class %s>" % shortrepr(self) 1303 1304 def __shortrepr__(self): 1305 return "%s.%s" % (shortrepr(self.parent), self.name) 1306 1307 def get_body_block(self): 1308 return self.get_instantiator().blocks[0] 1309 1310 # Namespace-related methods. 1311 1312 def get_updated_context_values(self, context_values): 1313 1314 """ 1315 Adapt the contexts found in the given 'context_values', returning a new 1316 set. 1317 See: docs/assignment.txt 1318 """ 1319 1320 results = set() 1321 1322 for context, value in context_values: 1323 1324 # Change the ownership of functions. 1325 1326 if context is ReplaceableContext and value is not None and isinstance(value, Function): 1327 results.add((self, value)) 1328 else: 1329 results.add((context, value)) 1330 1331 return NamespaceDict.get_updated_context_values(self, results) 1332 1333 # Administrative methods. 1334 1335 def items_for_vacuum(self): 1336 1337 "Consider both class and instance attributes for vacuuming." 1338 1339 items = [] 1340 for name in self.instattr: 1341 items.append((name, None)) 1342 return NamespaceDict.items_for_vacuum(self) + items 1343 1344 def vacuum_item(self, name): 1345 1346 "Vacuum 'name' from the class or instance attribute collections." 1347 1348 # NOTE: Hack to prevent damage to exceptions. 1349 1350 if name == "_pc": 1351 return 0 1352 1353 if not NamespaceDict.vacuum_item(self, name): 1354 self.instattr.remove(name) 1355 return 1 1356 1357 def finalise_attributes(self): 1358 1359 "Make sure that all attributes are fully defined." 1360 1361 if self.finalised: 1362 return 1363 1364 self.finalise_class_attributes() 1365 self.finalise_instance_attributes() 1366 self.finalised = 1 1367 1368 def unfinalise_attributes(self): 1369 1370 "Open attribute definitions to editing and subsequent finalisation." 1371 1372 self.reset_caches() 1373 self.finalised = 0 1374 1375 # Convenience methods for accessing functions and methods. 1376 1377 def get_instantiator(self): 1378 1379 "Return a function which can be used to instantiate the class." 1380 1381 if self.instantiator is None: 1382 self.instantiator = self.get_init_method().as_instantiator() 1383 return self.instantiator 1384 1385 def get_init_method(self): 1386 return self.all_class_attributes()["__init__"].get_value() 1387 1388 # Class-specific methods. 1389 1390 def add_base(self, base): 1391 self.bases.append(base) 1392 base.add_descendant(self) 1393 1394 def add_instance_attribute(self, name, tentative=False): 1395 if tentative: 1396 self.instattr_tentative.add(name) 1397 else: 1398 self.instattr.add(name) 1399 1400 def add_descendant(self, cls): 1401 self.descendants.add(cls) 1402 for base in self.bases: 1403 base.add_descendant(cls) 1404 1405 def has_subclass(self, other): 1406 return other in self.descendants 1407 1408 def all_descendants(self): 1409 d = {} 1410 for cls in self.descendants: 1411 d[cls.full_name()] = cls 1412 return d 1413 1414 "Return the attribute names provided by this class only." 1415 1416 class_attribute_names = NamespaceDict.keys 1417 1418 def class_attributes(self): 1419 1420 "Return class attributes provided by this class only." 1421 1422 return dict(self) 1423 1424 def all_class_attribute_names(self): 1425 1426 "Return the attribute names provided by classes in this hierarchy." 1427 1428 if self.all_classattr_names is None: 1429 self.all_class_attributes() 1430 self.all_classattr_names = self.all_classattr.keys() 1431 return self.all_classattr_names 1432 1433 def all_class_attributes(self): 1434 1435 "Return all class attributes, indicating the class which provides them." 1436 1437 self.finalise_class_attributes() 1438 return self.all_classattr 1439 1440 def finalise_class_attributes(self): 1441 1442 "Make sure that the class attributes are fully defined." 1443 1444 if self.all_classattr is None: 1445 self.all_classattr = {} 1446 clsattr = {} 1447 1448 # Record provisional position information for attributes of this 1449 # class. 1450 1451 for name in self.class_attributes().keys(): 1452 1453 # Special case: __class__ has to be at position 0. 1454 1455 if name == "__class__": 1456 clsattr[name] = set([0]) 1457 else: 1458 clsattr[name] = set() # position not yet defined 1459 1460 reversed_bases = self.bases[:] 1461 reversed_bases.reverse() 1462 1463 # For the bases in reverse order, acquire class attribute details. 1464 1465 for cls in reversed_bases: 1466 for name, attr in cls.all_class_attributes().items(): 1467 self.all_classattr[name] = attr 1468 1469 # Record previous attribute information. 1470 1471 if clsattr.has_key(name): 1472 clsattr[name].add(attr.position) 1473 1474 # Record class attributes provided by this class and its bases, 1475 # along with their positions. 1476 1477 self.all_classattr.update(self.class_attributes()) 1478 1479 if clsattr: 1480 for i, name in enumerate(self._get_position_list(clsattr)): 1481 self.all_classattr[name].position = i 1482 1483 return self.all_classattr 1484 1485 def instance_attribute_names(self): 1486 1487 "Return the instance attribute names provided by the class." 1488 1489 if self.all_instattr_names is None: 1490 self.instance_attributes() 1491 return self.all_instattr_names 1492 1493 def instance_attributes(self): 1494 1495 "Return instance-only attributes for instances of this class." 1496 1497 self.finalise_instance_attributes() 1498 return self.all_instattr 1499 1500 def finalise_instance_attributes(self): 1501 1502 "Make sure that the instance attributes are fully defined." 1503 1504 # Eliminate tentative instance attributes that are actually class 1505 # attributes. 1506 1507 for attrname in self.all_class_attributes().keys(): 1508 if attrname in self.instattr_tentative: 1509 self.instattr_tentative.remove(attrname) 1510 1511 for cls in self.descendants: 1512 for attrname in cls.class_attribute_names(): 1513 if attrname in self.instattr_tentative: 1514 self.instattr_tentative.remove(attrname) 1515 1516 for attrname in self.instattr_tentative: 1517 self.instattr.add(attrname) 1518 1519 # Cache the attributes by converting the positioned attributes into a 1520 # dictionary. 1521 1522 if self.all_instattr is None: 1523 self.all_instattr = self._get_attributes() 1524 self.all_instattr_names = self.all_instattr.keys() 1525 1526 return self.all_instattr 1527 1528 def _get_attributes(self): 1529 1530 """ 1531 Return a dictionary mapping names to Attr instances incorporating 1532 information about their positions in the final instance structure. 1533 """ 1534 1535 instattr = {} 1536 1537 # Record provisional position information for attributes of this 1538 # instance. 1539 1540 for name in self.instattr: 1541 instattr[name] = set() # position not yet defined 1542 1543 reversed_bases = self.bases[:] 1544 reversed_bases.reverse() 1545 1546 # For the bases in reverse order, acquire instance attribute 1547 # details. 1548 1549 for cls in reversed_bases: 1550 for name, attr in cls.instance_attributes().items(): 1551 1552 # Record previous attribute information. 1553 1554 if instattr.has_key(name): 1555 instattr[name].add(attr.position) 1556 else: 1557 instattr[name] = set([attr.position]) 1558 1559 # Build the dictionary of attributes using the existing positions known 1560 # for each name. 1561 1562 d = {} 1563 for i, name in enumerate(self._get_position_list(instattr)): 1564 d[name] = Attr(i, make_instance(), name) 1565 return d 1566 1567 def _get_position_list(self, positions): 1568 1569 """ 1570 Return a list of attribute names for the given 'positions' mapping from 1571 names to positions, indicating the positions of the attributes in the 1572 final instance structure. 1573 """ 1574 1575 position_items = positions.items() 1576 namearray = [None] * len(position_items) 1577 1578 # Get the positions in ascending order of list size, with lists 1579 # of the same size ordered according to their smallest position 1580 # value. 1581 1582 position_items.sort(self._cmp_positions) 1583 1584 # Get the names in position order. 1585 1586 held = [] 1587 1588 for name, pos in position_items: 1589 pos = list(pos) 1590 pos.sort() 1591 if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: 1592 namearray[pos[0]] = name 1593 else: 1594 if pos: 1595 self.relocated.add(name) 1596 held.append((name, pos)) 1597 1598 for i, attr in enumerate(namearray): 1599 if attr is None: 1600 name, pos = held.pop() 1601 namearray[i] = name 1602 1603 return namearray 1604 1605 def _cmp_positions(self, a, b): 1606 1607 "Compare name plus position list operands 'a' and 'b'." 1608 1609 name_a, list_a = a 1610 name_b, list_b = b 1611 if len(list_a) < len(list_b): 1612 return -1 1613 elif len(list_a) > len(list_b): 1614 return 1 1615 elif not list_a: 1616 return 0 1617 else: 1618 return cmp(min(list_a), min(list_b)) 1619 1620 def all_attribute_names(self): 1621 1622 """ 1623 Return the names of all attributes provided by instances of this class. 1624 """ 1625 1626 self.allattr_names = self.allattr_names or self.all_attributes().keys() 1627 return self.allattr_names 1628 1629 def all_attributes(self): 1630 1631 """ 1632 Return all attributes for an instance, indicating either the class which 1633 provides them or that the instance itself provides them. 1634 1635 Note that __class__ acts like a class attribute for both instances and 1636 classes, and must be able to convey distinct values. 1637 """ 1638 1639 if self.allattr is None: 1640 self.allattr = {} 1641 self.allattr.update(self.all_class_attributes()) 1642 for name, attr in self.instance_attributes().items(): 1643 if self.allattr.has_key(name) and name != "__class__": 1644 print >>sys.stderr, "Warning: instance attribute %r in %r overrides class attribute." % (name, self) 1645 self.allattr[name] = attr 1646 return self.allattr 1647 1648 class TypeClass(Class): 1649 1650 "A special class for the type class." 1651 1652 pass 1653 1654 class CommonClass(Class): 1655 1656 "An inspected class." 1657 1658 pass 1659 1660 class Function(NamespaceDict, Naming, Constant): 1661 1662 "An inspected function." 1663 1664 def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, 1665 dynamic_def=0, module=None, node=None): 1666 1667 """ 1668 Initialise the function with the given 'name', 'parent', list of 1669 'argnames', list of 'defaults', the 'has_star' flag (indicating the 1670 presence of a * parameter), the 'has_dstar' flag (indicating the 1671 presence of a ** parameter), optional 'dynamic_def' (indicating that the 1672 function must be handled dynamically), optional 'module', and optional 1673 AST 'node'. 1674 """ 1675 1676 NamespaceDict.__init__(self, module) 1677 1678 if name is None: 1679 self.name = "lambda#%d" % new_lambda() 1680 self._is_lambda = 1 1681 else: 1682 self.name = name 1683 self._is_lambda = 0 1684 1685 self.parent = parent 1686 self.argnames = argnames 1687 self.defaults = defaults 1688 self.has_star = has_star 1689 self.has_dstar = has_dstar 1690 self.dynamic_def = dynamic_def 1691 self.astnode = node 1692 1693 # Initialise the positional names. 1694 1695 self.positional_names = self.argnames[:] 1696 if has_dstar: 1697 self.dstar_name = self.positional_names[-1] 1698 del self.positional_names[-1] 1699 if has_star: 1700 self.star_name = self.positional_names[-1] 1701 del self.positional_names[-1] 1702 1703 # Initialise default storage. 1704 # NOTE: This must be initialised separately due to the reliance on node 1705 # NOTE: visiting. 1706 1707 self.default_attrs = [] 1708 1709 # Initialise attribute usage. 1710 1711 if node is not None: 1712 for arg in argnames: 1713 1714 # Define attribute users. 1715 1716 self._define_attribute_user_for_name(node, arg) 1717 1718 # Caches. 1719 1720 self.localnames = None # cache for locals 1721 1722 # Add parameters to the namespace. 1723 1724 self._add_parameters(argnames) 1725 1726 # Image generation details. 1727 1728 self.dynamic = None 1729 self.location = None 1730 self.code_location = None 1731 self.code_body_location = None 1732 1733 # Program-related details. 1734 1735 self.blocks = None 1736 self.body_block = None 1737 1738 self.temp_usage = 0 1739 self.local_usage = 0 1740 self.all_local_usage = 0 1741 1742 def _add_parameters(self, argnames): 1743 1744 "Add 'argnames' to the namespace." 1745 1746 for name in argnames: 1747 self.set(name, make_instance()) 1748 1749 for name, top_level in self._flattened_parameters(argnames): 1750 if not top_level: 1751 self.set(name, make_instance()) 1752 1753 def _flattened_parameters(self, argnames, top_level=1): 1754 l = [] 1755 for name in argnames: 1756 if isinstance(name, tuple): 1757 l += self._flattened_parameters(name, 0) 1758 else: 1759 l.append((name, top_level)) 1760 return l 1761 1762 def __repr__(self): 1763 if self.location is not None: 1764 return "<function %s (at %r, code at %r)>" % ( 1765 shortrepr(self), self.location, self.code_location 1766 ) 1767 else: 1768 return "<function %s>" % shortrepr(self) 1769 1770 def __shortrepr__(self): 1771 return "%s.%s(%s)" % (shortrepr(self.parent), self.name, ", ".join(self.argnames)) 1772 1773 def get_body_block(self): 1774 return self.body_block 1775 1776 def is_lambda(self): 1777 return self._is_lambda 1778 1779 # Defaults-related methods. 1780 1781 def store_default(self, attr_or_value): 1782 1783 """ 1784 Reserve space for defaults, set outside the function, potentially on a 1785 dynamic basis, using the 'attr_or_value'. 1786 """ 1787 1788 attr = Attr(None, self, None) 1789 self._set_using_attr(attr, attr_or_value) 1790 self.default_attrs.append(attr) 1791 1792 def make_dynamic(self): 1793 1794 "Return whether this function must be handled using a dynamic object." 1795 1796 if self.dynamic is None: 1797 for attr in self.default_attrs: 1798 if not attr.is_strict_constant() and self.dynamic_def: 1799 self.dynamic = 1 1800 self._make_dynamic() 1801 break 1802 else: 1803 self.dynamic = 0 1804 1805 return self.dynamic 1806 1807 is_dynamic = make_dynamic 1808 1809 def _make_dynamic(self): 1810 1811 "Where functions have dynamic defaults, add a context argument." 1812 1813 name = "<context>" 1814 self.argnames.insert(0, name) 1815 self.positional_names.insert(0, name) 1816 self.set(name, make_instance()) 1817 1818 # Namespace-related methods. 1819 1820 def make_global(self, name): 1821 1822 "Declare 'name' as a global in the current namespace." 1823 1824 if name not in self.argnames and not self.has_key(name): 1825 self.globals.add(name) 1826 return 1 1827 else: 1828 return 0 1829 1830 def parameters(self): 1831 1832 """ 1833 Return a dictionary mapping parameter names to their position in the 1834 parameter list. 1835 """ 1836 1837 parameters = {} 1838 for i, name in enumerate(self.argnames): 1839 parameters[name] = i 1840 return parameters 1841 1842 def tuple_parameters(self, argnames=None): 1843 1844 """ 1845 Return a list of (position, parameter) entries corresponding to tuple 1846 parameters, where each parameter may either be a string or another such 1847 list of entries. 1848 """ 1849 1850 names = argnames or self.argnames 1851 1852 l = [] 1853 for i, name in enumerate(names): 1854 if isinstance(name, tuple): 1855 l.append((i, self.tuple_parameters(name))) 1856 elif argnames: 1857 l.append((i, name)) 1858 return l 1859 1860 def all_locals(self): 1861 1862 "Return a dictionary mapping names to local and parameter details." 1863 1864 return dict(self) 1865 1866 def locals(self): 1867 1868 "Return a dictionary mapping names to local details." 1869 1870 if self.localnames is None: 1871 self.localnames = {} 1872 self.localnames.update(self.all_locals()) 1873 for name in self.argnames: 1874 del self.localnames[name] 1875 return self.localnames 1876 1877 def is_method(self): 1878 1879 """ 1880 Return whether this function is a method explicitly defined in a class. 1881 """ 1882 1883 return isinstance(self.parent, Class) 1884 1885 def is_relocated(self, name): 1886 1887 """ 1888 Determine whether the given attribute 'name' is relocated for instances 1889 having this function as a method. 1890 """ 1891 1892 for cls in self.parent.descendants: 1893 if name in cls.relocated: 1894 return 1 1895 return 0 1896 1897 # Administrative methods. 1898 1899 def items_for_vacuum(self): 1900 return self.lambdas.items() 1901 1902 def vacuum_item(self, name): 1903 del self.lambdas[name] 1904 return 1 1905 1906 def finalise_attributes(self): 1907 1908 """ 1909 Make sure all attributes (locals) are fully defined. Note that locals 1910 are not attributes in the sense of class, module or instance attributes. 1911 Defaults are also finalised by this method. 1912 """ 1913 1914 if self.finalised: 1915 return 1916 1917 # Defaults. 1918 1919 for i, default in enumerate(self.default_attrs): 1920 default.position = i 1921 1922 # Parameters. 1923 1924 i = self._finalise_parameters() 1925 1926 if i is not None: 1927 nparams = i + 1 1928 else: 1929 nparams = 0 1930 1931 # Locals (and tuple parameter names). 1932 1933 i = None 1934 for i, attr in enumerate(self.locals().values()): 1935 attr.position = i + nparams 1936 1937 if i is not None: 1938 nothers = i + 1 1939 else: 1940 nothers = 0 1941 1942 self.local_usage = nothers 1943 self.all_local_usage = nparams + nothers 1944 self.finalised = 1 1945 1946 def _finalise_parameters(self): 1947 if not self.argnames: 1948 return None 1949 1950 for i, name in enumerate(self.argnames): 1951 self[name].position = i 1952 1953 return i 1954 1955 def as_instantiator(self): 1956 1957 "Make an instantiator function from a method, keeping all arguments." 1958 1959 function = Function(self.parent.name, self.parent.parent, self.argnames, self.defaults, 1960 self.has_star, self.has_dstar, self.dynamic_def, self.module) 1961 function.default_attrs = self.default_attrs 1962 return function 1963 1964 class UnresolvedName(NamespaceDict, Constant): 1965 1966 "A module, class or function which was mentioned but could not be imported." 1967 1968 def __init__(self, name, parent_name, module=None): 1969 NamespaceDict.__init__(self, module) 1970 self.name = name 1971 self.parent_name = parent_name 1972 self.parent = None 1973 1974 self.descendants = set() 1975 1976 def add_descendant(self, cls): 1977 self.descendants.add(cls) 1978 1979 def all_attributes(self): 1980 return {} 1981 1982 def all_attribute_names(self): 1983 return [] 1984 1985 all_class_attributes = class_attributes = instance_attributes = all_attributes 1986 all_class_attribute_names = class_attribute_names = instance_attribute_names = all_attribute_names 1987 1988 def __repr__(self): 1989 return "<unknown %s>" % shortrepr(self) 1990 1991 def __shortrepr__(self): 1992 return "%s.%s" % (self.parent_name, self.name) 1993 1994 def full_name(self): 1995 if self.name is not None: 1996 return self.parent_name + "." + self.name 1997 else: 1998 return self.parent_name 1999 2000 class Module(NamespaceDict, Constant): 2001 2002 "An inspected module's core details." 2003 2004 def __init__(self, name, importer): 2005 NamespaceDict.__init__(self, self) 2006 self.name = name 2007 self.importer = importer 2008 self.parent = None 2009 2010 # Original location details. 2011 2012 self.astnode = None 2013 2014 # Complete lists of classes and functions. 2015 2016 self.all_objects = set() 2017 2018 # A set of global names that cannot support combined attribute usage 2019 # observations because they may be modified within functions during 2020 # initialisation. 2021 2022 self.modified_names = set() 2023 2024 # Keyword records. 2025 2026 self.keyword_names = set() 2027 2028 # Image generation details. 2029 2030 self.location = None 2031 self.code_location = None 2032 2033 # Program-related details. 2034 2035 self.blocks = None 2036 self.temp_usage = 0 2037 self.local_usage = 0 2038 self.all_local_usage = 0 2039 2040 def full_name(self): 2041 return self.name 2042 2043 def __repr__(self): 2044 if self.location is not None: 2045 return "<module %s (at %r)>" % (self.name, self.location) 2046 else: 2047 return "<module %s>" % shortrepr(self) 2048 2049 def __shortrepr__(self): 2050 return self.name 2051 2052 # Attribute methods. 2053 2054 "Return the module attribute names provided by the module." 2055 2056 module_attribute_names = NamespaceDict.keys 2057 2058 def module_attributes(self): 2059 2060 "Return a dictionary mapping names to module attributes." 2061 2062 return dict(self) 2063 2064 # Attribute usage methods that apply to module globals in certain 2065 # circumstances. 2066 2067 def _use_attribute(self, name, attrname, value=None): 2068 if name not in self.modified_names: 2069 return NamespaceDict._use_attribute(self, name, attrname, value) 2070 else: 2071 self.importer.use_name(attrname, self.full_name(), value) 2072 return [] 2073 2074 def _define_attribute_user_for_name(self, node, name): 2075 if name not in self.modified_names: 2076 NamespaceDict._define_attribute_user_for_name(self, node, name) 2077 2078 def _init_attribute_user_for_name(self, node, name): 2079 if name not in self.modified_names: 2080 NamespaceDict._init_attribute_user_for_name(self, node, name) 2081 2082 def _define_attribute_accessor(self, name, attrname, node, value): 2083 if name not in self.modified_names: 2084 NamespaceDict._define_attribute_accessor(self, name, attrname, node, value) 2085 else: 2086 self.importer.use_name(attrname, self.full_name(), value) 2087 2088 # Pre-made instances. 2089 2090 type_class = TypeClass("type") # details to be filled in later 2091 2092 # Class construction. 2093 2094 def get_class(name, parent, module, node): 2095 2096 """ 2097 Return a Class instance for the class with the given 'name', 'parent', 2098 'module' and 'node'. 2099 """ 2100 2101 if name == "type" and module.full_name() == "__builtins__": 2102 type_class.set_context(parent, module, node) 2103 return type_class 2104 else: 2105 return CommonClass(name, parent, module, node) 2106 2107 # Lambda sequence numbering. 2108 2109 lambda_index = 0 2110 2111 def new_lambda(): 2112 2113 "Return a new sequence number for a lambda definition." 2114 2115 global lambda_index 2116 lambda_index += 1 2117 return lambda_index 2118 2119 # Special representations. 2120 2121 class AtLeast: 2122 2123 "A special representation for numbers of a given value or greater." 2124 2125 def __init__(self, count): 2126 self.count = count 2127 2128 def __eq__(self, other): 2129 return 0 2130 2131 __lt__ = __le__ = __eq__ 2132 2133 def __ne__(self, other): 2134 return 1 2135 2136 def __gt__(self, other): 2137 if isinstance(other, AtLeast): 2138 return 0 2139 else: 2140 return self.count > other 2141 2142 def __ge__(self, other): 2143 if isinstance(other, AtLeast): 2144 return 0 2145 else: 2146 return self.count >= other 2147 2148 def __iadd__(self, other): 2149 if isinstance(other, AtLeast): 2150 self.count += other.count 2151 else: 2152 self.count += other 2153 return self 2154 2155 def __radd__(self, other): 2156 if isinstance(other, AtLeast): 2157 return AtLeast(self.count + other.count) 2158 else: 2159 return AtLeast(self.count + other) 2160 2161 def __repr__(self): 2162 return "AtLeast(%r)" % self.count 2163 2164 # vim: tabstop=4 expandtab shiftwidth=4