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 # Add the usage to all current users. 617 618 if users.has_key(name): 619 for user in users[name]: 620 values = user._attrnames[name] 621 if values is None: 622 values = user._attrnames[name] = ObjectSet() 623 624 # Add an entry for the attribute, optionally with an assigned 625 # value. 626 627 values.add(attrname) 628 if value is not None: 629 values[attrname].add(value) 630 631 return users[name] 632 else: 633 return [] 634 635 def _define_attribute_user(self, node): 636 637 """ 638 Define 'node' as the user of attributes, indicating the point where the 639 user is defined. 640 """ 641 642 name = node.name 643 self._define_attribute_user_for_name(node, name) 644 645 def _define_attribute_user_for_name(self, node, name): 646 647 "Define 'node' as the user of attributes for the given 'name'." 648 649 users = self.attribute_users[-1] 650 651 # This node overrides previous definitions. 652 653 users[name] = set([node]) 654 655 # Record the attribute combinations for the name. 656 657 self._init_attribute_user_for_name(node, name) 658 659 # Remember this user. 660 661 self.all_attribute_users.add(node) 662 663 def _init_attribute_user_for_name(self, node, name): 664 665 "Make sure that 'node' is initialised for 'name'." 666 667 self._init_attribute_user(node) 668 node._attrnames[name] = None 669 670 def _init_attribute_user(self, node): 671 672 # Attribute usage for names. 673 674 if not hasattr(node, "_attrnames"): 675 node._attrnames = {} 676 node._attrmerged = {} 677 678 # Branches contributing usage to this node. 679 680 if not hasattr(node, "_attrbranches"): 681 node._attrbranches = [] 682 683 # Definitions receiving usage from this node. 684 685 if not hasattr(node, "_attrdefs"): 686 node._attrdefs = [] 687 688 def _define_attribute_accessor(self, name, attrname, node, value): 689 690 # NOTE: Revisiting of nodes may occur for loops. 691 692 if not hasattr(node, "_attrusers"): 693 node._attrusers = set() 694 695 node._attrusers.update(self.use_attribute(name, attrname, value)) 696 node._username = name 697 698 # Branch management methods. 699 700 def _new_branchpoint(self, loop_node=None): 701 702 """ 703 Establish a new branchpoint where several control-flow branches diverge 704 and subsequently converge. 705 """ 706 707 self.attribute_user_shelves.append([]) 708 self.scope_shelves.append([]) 709 710 if loop_node is not None: 711 self.suspended_broken_users.append([]) 712 self.suspended_continuing_users.append((loop_node, [])) 713 714 def _new_branch(self, node): 715 716 """ 717 Establish a new control-flow branch, transferring attribute usage to 718 the new branch so that it may be augmented for each name locally. 719 720 Add the given 'node' as an active user to be informed of attribute 721 usage. 722 """ 723 724 attribute_users = self.attribute_users[-1] 725 726 # Define this node as the active attribute user for all currently 727 # defined names. 728 729 new_users = {} 730 731 for name in attribute_users.keys(): 732 new_users[name] = [node] 733 self._init_attribute_user_for_name(node, name) 734 735 self._init_attribute_user(node) 736 self.attribute_users.append(new_users) 737 738 # Add this user as a contributor to the previously active users. 739 740 self._connect_users_to_branch(attribute_users, node) 741 742 # Retain a record of scope usage. 743 744 scope_usage = {} 745 scope_usage.update(self.scope_usage[-1]) 746 self.scope_usage.append(scope_usage) 747 748 # Retain a record of abandoned branch users. 749 750 self.abandoned_users.append([]) 751 752 def _connect_users_to_branch(self, attribute_users, node): 753 754 """ 755 Given the 'attribute_users' mapping, connect the users referenced in the 756 mapping to the given branch 'node'. 757 """ 758 759 all_users = set() 760 761 for users in attribute_users.values(): 762 all_users.update(users) 763 764 for user in all_users: 765 self._init_attribute_user(user) 766 user._attrbranches.append(node) 767 768 def _abandon_branch(self, retain_branch=True): 769 770 """ 771 Abandon scope usage, permitting locally different scopes for names, 772 provided these cannot "escape" from the branch. 773 """ 774 775 attribute_users = self.attribute_users[-1] 776 777 self.attribute_users[-1] = {} 778 self.scope_usage[-1] = abandoned_branch_scope 779 780 if retain_branch: 781 self.abandoned_users[-1].append(attribute_users) 782 783 def _suspend_broken_branch(self): 784 785 """ 786 Suspend a branch for resumption after the current loop. 787 """ 788 789 attribute_users = self.attribute_users[-1] 790 791 users = self.suspended_broken_users[-1] 792 users.append(attribute_users) 793 self._abandon_branch(False) 794 795 def _suspend_continuing_branch(self): 796 797 """ 798 Suspend a branch for resumption after the current iteration. 799 """ 800 801 attribute_users = self.attribute_users[-1] 802 803 loop_node, users = self.suspended_continuing_users[-1] 804 users.append(attribute_users) 805 self._abandon_branch(False) 806 807 def _shelve_branch(self): 808 809 """ 810 Shelve the current control-flow branch, recording the attribute usage 811 for subsequent merging. If this branch should be abandoned, the usage 812 observations are still recorded but will not contribute to subsequent 813 observations after a merge. 814 """ 815 816 users = self.attribute_users.pop() 817 self.attribute_user_shelves[-1].append(users) 818 819 scope_usage = self.scope_usage.pop() 820 self.scope_shelves[-1].append(scope_usage) 821 822 def _merge_branches(self): 823 824 """ 825 Merge control-flow branches. This should find the users active within 826 each branch, which have been "shelved", and update the active users 827 dictionary with these contributions. 828 """ 829 830 # Combine the attribute users. This ensures that a list of users 831 # affected by attribute usage is maintained for the current branch. 832 833 all_shelved_users = self.attribute_user_shelves.pop() 834 new_users = merge_mapping_dicts(all_shelved_users) 835 self.attribute_users[-1] = new_users 836 837 # Abandoned users are retained for exception handling purposes. 838 839 all_abandoned_users = self.abandoned_users.pop() 840 new_abandoned_users = merge_mapping_dicts(all_abandoned_users) 841 self.abandoned_users[-1].append(new_abandoned_users) 842 843 # Combine the scope usage. 844 845 scope_usage = self.scope_usage[-1] 846 new_scope_usage = {} 847 848 all_scope_usage = self.scope_shelves.pop() 849 all_scope_names = set() 850 851 # Find all the names for whom scope information has been defined. 852 853 for shelved_usage in all_scope_usage: 854 all_scope_names.update(shelved_usage.keys()) 855 856 for shelved_usage in all_scope_usage: 857 for name in all_scope_names: 858 859 # Find the recorded scope for the name. 860 861 if shelved_usage.has_key(name): 862 scope = shelved_usage[name] 863 elif scope_usage.has_key(name): 864 scope = scope_usage[name] 865 866 # For abandoned branches, no scope is asserted for a name. 867 868 elif isinstance(shelved_usage, AbandonedBranchScope): 869 scope = None 870 871 # If no scope is recorded, find a suitable external source. 872 873 else: 874 attr, scope, full_name = self._get_with_scope(name, external=1) 875 876 # Attempt to record the scope, testing for conflicts. 877 878 if scope: 879 if not new_scope_usage.has_key(name): 880 new_scope_usage[name] = scope 881 else: 882 new_scope = new_scope_usage[name] 883 if new_scope != scope: 884 if isinstance(new_scope, ScopeConflict): 885 if isinstance(scope, ScopeConflict): 886 scopes = scope.scopes.union(new_scope.scopes) 887 else: 888 scopes = new_scope.scopes.union(set([scope])) 889 elif isinstance(scope, ScopeConflict): 890 scopes = scope.scopes.union(set([new_scope])) 891 else: 892 scopes = set([scope, new_scope]) 893 new_scope_usage[name] = ScopeConflict(scopes) 894 895 self.scope_usage[-1] = new_scope_usage 896 897 def _resume_broken_branches(self): 898 899 """ 900 Incorporate users from suspended broken branches into the current set of 901 active users. 902 """ 903 904 suspended_users = self.suspended_broken_users.pop() 905 current_users = self.attribute_users[-1] 906 new_users = merge_mapping_dicts(suspended_users + [current_users]) 907 self.attribute_users[-1] = new_users 908 909 def _resume_continuing_branches(self): 910 911 """ 912 Incorporate users from suspended continuing branches into the current 913 set of active users, merging usage from the latter with the former. 914 """ 915 916 loop_node, suspended_users = self.suspended_continuing_users.pop() 917 current_users = self.attribute_users[-1] 918 919 # Connect the suspended users to the loop node. 920 921 for users in suspended_users: 922 self._connect_users_to_branch(users, loop_node) 923 924 # Merge suspended branches with the current branch. 925 926 new_users = merge_mapping_dicts(suspended_users + [current_users]) 927 self.attribute_users[-1] = new_users 928 929 def _resume_abandoned_branches(self): 930 931 """ 932 Incorporate users from abandoned branches into the current set of active 933 users. The abandoned branches are taken from the containing branch. 934 """ 935 936 current_users = self.attribute_users[-1] 937 abandoned_users = self.abandoned_users[-2] 938 new_users = merge_mapping_dicts(abandoned_users + [current_users]) 939 self.attribute_users[-1] = new_users 940 941 # Scope usage methods. 942 943 def define_scope(self, name, scope): 944 945 """ 946 Define 'name' as being from the given 'scope' in the current namespace. 947 """ 948 949 self.scope_usage[-1][name] = scope 950 951 def note_scope(self, name, scope): 952 953 """ 954 Note usage of 'name' from the given 'scope' in the current namespace. 955 If a conflict has been recorded previously, raise an exception. 956 """ 957 958 scope_usage = self.scope_usage[-1] 959 960 if scope_usage.has_key(name): 961 found_scope = scope_usage[name] 962 if isinstance(found_scope, ScopeConflict): 963 raise InspectError("Scope conflict for %r: defined as %s." % ( 964 name, ", ".join(found_scope.scopes))) 965 966 scope_usage[name] = scope 967 968 def used_in_scope(self, name, scope): 969 970 """ 971 Return whether 'name' is used from the given 'scope' in the current 972 namespace. 973 """ 974 975 scope_usage = self.scope_usage[-1] 976 return scope_usage.get(name) == scope 977 978 # Special helper classes for usage and scope resolution. 979 980 class EmptyDict: 981 982 "A class providing dictionaries which retain no information." 983 984 def has_key(self, name): 985 return 0 986 987 def __setitem__(self, name, value): 988 pass 989 990 def __getitem__(self, name): 991 raise KeyError, name 992 993 def get(self, name, default=None): 994 return default 995 996 def keys(self): 997 return [] 998 999 values = items = keys 1000 1001 class AbandonedBranchScope(EmptyDict): 1002 1003 """ 1004 A class providing a value or state for an abandoned branch distinct from an 1005 empty scope dictionary. 1006 """ 1007 1008 pass 1009 1010 abandoned_branch_scope = AbandonedBranchScope() 1011 1012 class ScopeConflict: 1013 1014 """ 1015 A scope conflict caused when different code branches contribute different 1016 sources of names. 1017 """ 1018 1019 def __init__(self, scopes): 1020 self.scopes = scopes 1021 1022 class NullBranch: 1023 1024 "A class representing an attribute user for a non-existent branch." 1025 1026 pass 1027 1028 # Program data structures. 1029 1030 class Attr: 1031 1032 "An attribute entry having a context." 1033 1034 def __init__(self, position, parent, name): 1035 1036 """ 1037 Initialise the attribute with the given 'position' within the collection 1038 of attributes of its 'parent', indicating its 'name'. 1039 """ 1040 1041 self.position = position 1042 self.parent = parent 1043 self.name = name 1044 1045 # Possible values. 1046 1047 self.context_values = set() 1048 1049 # Number of assignments per name. 1050 1051 self.assignments = None 1052 1053 # Value-related methods. 1054 1055 def get_contexts(self): 1056 return [c for (c, v) in self.context_values] 1057 1058 def get_values(self): 1059 return [v for (c, v) in self.context_values] 1060 1061 def get_context(self): 1062 if len(self.context_values) == 1: 1063 return self.get_contexts()[0] 1064 else: 1065 return None 1066 1067 def get_value(self): 1068 if len(self.context_values) == 1: 1069 return self.get_values()[0] 1070 else: 1071 return None 1072 1073 __call__ = get_value # convenient access to any single value 1074 1075 def update(self, context_values, single_assignment): 1076 1077 """ 1078 Update the attribute, adding the 'context_values' provided to the 1079 known details associated with the attribute, changing the number of 1080 assignments according to the 'single_assignment' status of the 1081 operation, where a true value indicates that only one assignment is 1082 associated with the update, and a false value indicates that potentially 1083 many assignments may be involved. 1084 """ 1085 1086 if self.context_values.issuperset(context_values) and \ 1087 not (make_instance(), make_instance()) in context_values: 1088 return 1089 1090 if self.assignments is None: 1091 if single_assignment: 1092 self.assignments = len(set(context_values)) 1093 else: 1094 self.assignments = AtLeast(len(set(context_values))) 1095 else: 1096 if single_assignment: 1097 self.assignments += 1 1098 else: 1099 self.assignments += AtLeast(1) 1100 1101 self.context_values.update(context_values) 1102 1103 def is_constant(self): 1104 1105 """ 1106 Return whether this attribute references something that can be regarded 1107 as being constant within a particular scope. 1108 """ 1109 1110 return self.assignments == 1 1111 1112 def is_strict_constant(self): 1113 1114 """ 1115 Return whether this attribute references something that can be regarded 1116 as being constant. 1117 """ 1118 1119 value = self.get_value() 1120 return not (value is None or (isinstance(value, Instance) and not isinstance(value, Constant))) 1121 1122 def is_static_attribute(self): 1123 1124 """ 1125 Return whether this attribute is defined on a fixed/static object such 1126 as a class or a module. 1127 """ 1128 1129 return isinstance(self.parent, (Class, Module)) 1130 1131 def defines_ambiguous_class(self): 1132 1133 "Return whether this attribute defines more than one class." 1134 1135 if self.assignments > 1: 1136 have_class = 0 1137 for obj in self.get_values(): 1138 if isinstance(obj, Class): 1139 if have_class: 1140 return 1 1141 have_class = 1 1142 1143 return 0 1144 1145 def defined_within_hierarchy(self): 1146 1147 """ 1148 Return whether the parent and context of the attribute belong to the 1149 same class hierarchy. 1150 """ 1151 1152 # Must be defined within a class. 1153 1154 if isinstance(self.parent, Class): 1155 1156 # To be sure, all contexts must be classes and be the same as the 1157 # parent, or be a superclass of the parent, or be a subclass of the 1158 # parent. 1159 1160 for context in self.get_contexts(): 1161 if not ( 1162 isinstance(context, Class) and ( 1163 context is self.parent or 1164 context.has_subclass(self.parent) or 1165 self.parent.has_subclass(context)) 1166 ): 1167 return 0 1168 1169 return 1 1170 1171 # Instance attributes are not defined within a hierarchy. 1172 1173 else: 1174 return 0 1175 1176 def defined_outside_hierarchy(self): 1177 1178 """ 1179 Return whether the parent and context of the attribute never belong to 1180 the same class hierarchy. 1181 """ 1182 1183 # Must be defined within a class. 1184 1185 if isinstance(self.parent, Class): 1186 1187 # To be sure, all contexts must be classes and be the same as the 1188 # parent, or be a superclass of the parent, or be a subclass of the 1189 # parent. 1190 1191 for context in self.get_contexts(): 1192 if not ( 1193 isinstance(context, Class) and not ( 1194 context is self.parent or 1195 context.has_subclass(self.parent) or 1196 self.parent.has_subclass(context)) 1197 ): 1198 return 0 1199 1200 return 1 1201 1202 # Instance attributes are not defined within a hierarchy. 1203 1204 else: 1205 return 0 1206 1207 def __repr__(self): 1208 if self.position is not None: 1209 position = "at %r, " % self.position 1210 else: 1211 position = "" 1212 return "<attribute %s.%s (%sassigned %r)>" % ( 1213 shortrepr(self.parent), self.name, 1214 position, self.assignments 1215 ) 1216 1217 def __shortrepr__(self): 1218 return "%s.%s (at %r)" % (shortrepr(self.parent), self.name, self.position) 1219 1220 def _context_values_str(self): 1221 l = [] 1222 for (c, v) in self.context_values: 1223 l.append("(c=%s, v=%s)" % (shortrepr(c), shortrepr(v))) 1224 return ", ".join(l) 1225 1226 class Class(NamespaceDict, Naming, Constant): 1227 1228 "A base class for common/normal classes and the type class." 1229 1230 def __init__(self, name, parent=None, module=None, node=None): 1231 1232 """ 1233 Initialise the class with the given 'name', optional 'parent' object, 1234 'module' and AST 'node'. The optional information must be set at a later 1235 point using the 'set_context' method if omitted. 1236 """ 1237 1238 NamespaceDict.__init__(self, module) 1239 self.name = name 1240 self.parent = parent 1241 self.astnode = node 1242 1243 # Superclasses, descendants and attributes. 1244 1245 self.bases = [] 1246 self.descendants = set() 1247 self.instattr = set() # instance attributes 1248 self.instattr_tentative = set() # tentative/suspected instance attributes 1249 self.relocated = set() # attributes which do not have the same 1250 # position as those of the same name in 1251 # some superclasses 1252 1253 # Caches. 1254 1255 self.reset_caches() 1256 1257 # Image generation details. 1258 1259 self.location = None 1260 self.code_location = None 1261 self.code_body_location = None # corresponds to the instantiator 1262 1263 self.instantiator = None 1264 self.instance_template_location = None # for creating instances at run-time 1265 1266 # Program-related details. 1267 1268 self.blocks = None 1269 self.temp_usage = 0 1270 self.local_usage = 0 1271 self.all_local_usage = 0 1272 1273 # Add an attribute to this class for use by instances. 1274 1275 self.set("__class__", self) 1276 1277 def set_context(self, parent, module, node): 1278 1279 "Set the 'parent', 'module' and 'node' of a class created in advance." 1280 1281 self.parent = parent 1282 self.module = module 1283 self.astnode = node 1284 1285 def reset_caches(self): 1286 1287 "Reset the caches." 1288 1289 self.all_instattr = None # cache for instance_attributes 1290 self.all_instattr_names = None # from all_instattr 1291 self.all_classattr = None # cache for all_class_attributes 1292 self.all_classattr_names = None # from all_classattr 1293 self.allattr = None # cache for all_attributes 1294 self.allattr_names = None # from allattr 1295 1296 def __repr__(self): 1297 if self.location is not None: 1298 return "<class %s (at %r)>" % (shortrepr(self), self.location) 1299 else: 1300 return "<class %s>" % shortrepr(self) 1301 1302 def __shortrepr__(self): 1303 return "%s.%s" % (shortrepr(self.parent), self.name) 1304 1305 def get_body_block(self): 1306 return self.get_instantiator().blocks[0] 1307 1308 # Namespace-related methods. 1309 1310 def get_updated_context_values(self, context_values): 1311 1312 """ 1313 Adapt the contexts found in the given 'context_values', returning a new 1314 set. 1315 See: docs/assignment.txt 1316 """ 1317 1318 results = set() 1319 1320 for context, value in context_values: 1321 1322 # Change the ownership of functions. 1323 1324 if context is ReplaceableContext and value is not None and isinstance(value, Function): 1325 results.add((self, value)) 1326 else: 1327 results.add((context, value)) 1328 1329 return NamespaceDict.get_updated_context_values(self, results) 1330 1331 # Administrative methods. 1332 1333 def items_for_vacuum(self): 1334 1335 "Consider both class and instance attributes for vacuuming." 1336 1337 items = [] 1338 for name in self.instattr: 1339 items.append((name, None)) 1340 return NamespaceDict.items_for_vacuum(self) + items 1341 1342 def vacuum_item(self, name): 1343 1344 "Vacuum 'name' from the class or instance attribute collections." 1345 1346 # NOTE: Hack to prevent damage to exceptions. 1347 1348 if name == "_pc": 1349 return 0 1350 1351 if not NamespaceDict.vacuum_item(self, name): 1352 self.instattr.remove(name) 1353 return 1 1354 1355 def finalise_attributes(self): 1356 1357 "Make sure that all attributes are fully defined." 1358 1359 if self.finalised: 1360 return 1361 1362 self.finalise_class_attributes() 1363 self.finalise_instance_attributes() 1364 self.finalised = 1 1365 1366 def unfinalise_attributes(self): 1367 1368 "Open attribute definitions to editing and subsequent finalisation." 1369 1370 self.reset_caches() 1371 self.finalised = 0 1372 1373 # Convenience methods for accessing functions and methods. 1374 1375 def get_instantiator(self): 1376 1377 "Return a function which can be used to instantiate the class." 1378 1379 if self.instantiator is None: 1380 self.instantiator = self.get_init_method().as_instantiator() 1381 return self.instantiator 1382 1383 def get_init_method(self): 1384 return self.all_class_attributes()["__init__"].get_value() 1385 1386 # Class-specific methods. 1387 1388 def add_base(self, base): 1389 self.bases.append(base) 1390 base.add_descendant(self) 1391 1392 def add_instance_attribute(self, name, tentative=False): 1393 if tentative: 1394 self.instattr_tentative.add(name) 1395 else: 1396 self.instattr.add(name) 1397 1398 def add_descendant(self, cls): 1399 self.descendants.add(cls) 1400 for base in self.bases: 1401 base.add_descendant(cls) 1402 1403 def has_subclass(self, other): 1404 return other in self.descendants 1405 1406 def all_descendants(self): 1407 d = {} 1408 for cls in self.descendants: 1409 d[cls.full_name()] = cls 1410 return d 1411 1412 "Return the attribute names provided by this class only." 1413 1414 class_attribute_names = NamespaceDict.keys 1415 1416 def class_attributes(self): 1417 1418 "Return class attributes provided by this class only." 1419 1420 return dict(self) 1421 1422 def all_class_attribute_names(self): 1423 1424 "Return the attribute names provided by classes in this hierarchy." 1425 1426 if self.all_classattr_names is None: 1427 self.all_class_attributes() 1428 self.all_classattr_names = self.all_classattr.keys() 1429 return self.all_classattr_names 1430 1431 def all_class_attributes(self): 1432 1433 "Return all class attributes, indicating the class which provides them." 1434 1435 self.finalise_class_attributes() 1436 return self.all_classattr 1437 1438 def finalise_class_attributes(self): 1439 1440 "Make sure that the class attributes are fully defined." 1441 1442 if self.all_classattr is None: 1443 self.all_classattr = {} 1444 clsattr = {} 1445 1446 # Record provisional position information for attributes of this 1447 # class. 1448 1449 for name in self.class_attributes().keys(): 1450 1451 # Special case: __class__ has to be at position 0. 1452 1453 if name == "__class__": 1454 clsattr[name] = set([0]) 1455 else: 1456 clsattr[name] = set() # position not yet defined 1457 1458 reversed_bases = self.bases[:] 1459 reversed_bases.reverse() 1460 1461 # For the bases in reverse order, acquire class attribute details. 1462 1463 for cls in reversed_bases: 1464 for name, attr in cls.all_class_attributes().items(): 1465 self.all_classattr[name] = attr 1466 1467 # Record previous attribute information. 1468 1469 if clsattr.has_key(name): 1470 clsattr[name].add(attr.position) 1471 1472 # Record class attributes provided by this class and its bases, 1473 # along with their positions. 1474 1475 self.all_classattr.update(self.class_attributes()) 1476 1477 if clsattr: 1478 for i, name in enumerate(self._get_position_list(clsattr)): 1479 self.all_classattr[name].position = i 1480 1481 return self.all_classattr 1482 1483 def instance_attribute_names(self): 1484 1485 "Return the instance attribute names provided by the class." 1486 1487 if self.all_instattr_names is None: 1488 self.instance_attributes() 1489 return self.all_instattr_names 1490 1491 def instance_attributes(self): 1492 1493 "Return instance-only attributes for instances of this class." 1494 1495 self.finalise_instance_attributes() 1496 return self.all_instattr 1497 1498 def finalise_instance_attributes(self): 1499 1500 "Make sure that the instance attributes are fully defined." 1501 1502 # Eliminate tentative instance attributes that are actually class 1503 # attributes. 1504 1505 for attrname in self.all_class_attributes().keys(): 1506 if attrname in self.instattr_tentative: 1507 self.instattr_tentative.remove(attrname) 1508 1509 for cls in self.descendants: 1510 for attrname in cls.class_attribute_names(): 1511 if attrname in self.instattr_tentative: 1512 self.instattr_tentative.remove(attrname) 1513 1514 for attrname in self.instattr_tentative: 1515 self.instattr.add(attrname) 1516 1517 # Cache the attributes by converting the positioned attributes into a 1518 # dictionary. 1519 1520 if self.all_instattr is None: 1521 self.all_instattr = self._get_attributes() 1522 self.all_instattr_names = self.all_instattr.keys() 1523 1524 return self.all_instattr 1525 1526 def _get_attributes(self): 1527 1528 """ 1529 Return a dictionary mapping names to Attr instances incorporating 1530 information about their positions in the final instance structure. 1531 """ 1532 1533 instattr = {} 1534 1535 # Record provisional position information for attributes of this 1536 # instance. 1537 1538 for name in self.instattr: 1539 instattr[name] = set() # position not yet defined 1540 1541 reversed_bases = self.bases[:] 1542 reversed_bases.reverse() 1543 1544 # For the bases in reverse order, acquire instance attribute 1545 # details. 1546 1547 for cls in reversed_bases: 1548 for name, attr in cls.instance_attributes().items(): 1549 1550 # Record previous attribute information. 1551 1552 if instattr.has_key(name): 1553 instattr[name].add(attr.position) 1554 else: 1555 instattr[name] = set([attr.position]) 1556 1557 # Build the dictionary of attributes using the existing positions known 1558 # for each name. 1559 1560 d = {} 1561 for i, name in enumerate(self._get_position_list(instattr)): 1562 d[name] = Attr(i, make_instance(), name) 1563 return d 1564 1565 def _get_position_list(self, positions): 1566 1567 """ 1568 Return a list of attribute names for the given 'positions' mapping from 1569 names to positions, indicating the positions of the attributes in the 1570 final instance structure. 1571 """ 1572 1573 position_items = positions.items() 1574 namearray = [None] * len(position_items) 1575 1576 # Get the positions in ascending order of list size, with lists 1577 # of the same size ordered according to their smallest position 1578 # value. 1579 1580 position_items.sort(self._cmp_positions) 1581 1582 # Get the names in position order. 1583 1584 held = [] 1585 1586 for name, pos in position_items: 1587 pos = list(pos) 1588 pos.sort() 1589 if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: 1590 namearray[pos[0]] = name 1591 else: 1592 if pos: 1593 self.relocated.add(name) 1594 held.append((name, pos)) 1595 1596 for i, attr in enumerate(namearray): 1597 if attr is None: 1598 name, pos = held.pop() 1599 namearray[i] = name 1600 1601 return namearray 1602 1603 def _cmp_positions(self, a, b): 1604 1605 "Compare name plus position list operands 'a' and 'b'." 1606 1607 name_a, list_a = a 1608 name_b, list_b = b 1609 if len(list_a) < len(list_b): 1610 return -1 1611 elif len(list_a) > len(list_b): 1612 return 1 1613 elif not list_a: 1614 return 0 1615 else: 1616 return cmp(min(list_a), min(list_b)) 1617 1618 def all_attribute_names(self): 1619 1620 """ 1621 Return the names of all attributes provided by instances of this class. 1622 """ 1623 1624 self.allattr_names = self.allattr_names or self.all_attributes().keys() 1625 return self.allattr_names 1626 1627 def all_attributes(self): 1628 1629 """ 1630 Return all attributes for an instance, indicating either the class which 1631 provides them or that the instance itself provides them. 1632 1633 Note that __class__ acts like a class attribute for both instances and 1634 classes, and must be able to convey distinct values. 1635 """ 1636 1637 if self.allattr is None: 1638 self.allattr = {} 1639 self.allattr.update(self.all_class_attributes()) 1640 for name, attr in self.instance_attributes().items(): 1641 if self.allattr.has_key(name) and name != "__class__": 1642 print >>sys.stderr, "Warning: instance attribute %r in %r overrides class attribute." % (name, self) 1643 self.allattr[name] = attr 1644 return self.allattr 1645 1646 class TypeClass(Class): 1647 1648 "A special class for the type class." 1649 1650 pass 1651 1652 class CommonClass(Class): 1653 1654 "An inspected class." 1655 1656 pass 1657 1658 class Function(NamespaceDict, Naming, Constant): 1659 1660 "An inspected function." 1661 1662 def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, 1663 dynamic_def=0, module=None, node=None): 1664 1665 """ 1666 Initialise the function with the given 'name', 'parent', list of 1667 'argnames', list of 'defaults', the 'has_star' flag (indicating the 1668 presence of a * parameter), the 'has_dstar' flag (indicating the 1669 presence of a ** parameter), optional 'dynamic_def' (indicating that the 1670 function must be handled dynamically), optional 'module', and optional 1671 AST 'node'. 1672 """ 1673 1674 NamespaceDict.__init__(self, module) 1675 1676 if name is None: 1677 self.name = "lambda#%d" % new_lambda() 1678 self._is_lambda = 1 1679 else: 1680 self.name = name 1681 self._is_lambda = 0 1682 1683 self.parent = parent 1684 self.argnames = argnames 1685 self.defaults = defaults 1686 self.has_star = has_star 1687 self.has_dstar = has_dstar 1688 self.dynamic_def = dynamic_def 1689 self.astnode = node 1690 1691 # Initialise the positional names. 1692 1693 self.positional_names = self.argnames[:] 1694 if has_dstar: 1695 self.dstar_name = self.positional_names[-1] 1696 del self.positional_names[-1] 1697 if has_star: 1698 self.star_name = self.positional_names[-1] 1699 del self.positional_names[-1] 1700 1701 # Initialise default storage. 1702 # NOTE: This must be initialised separately due to the reliance on node 1703 # NOTE: visiting. 1704 1705 self.default_attrs = [] 1706 1707 # Initialise attribute usage. 1708 1709 if node is not None: 1710 for arg in argnames: 1711 1712 # Define attribute users. 1713 1714 self._define_attribute_user_for_name(node, arg) 1715 1716 # Caches. 1717 1718 self.localnames = None # cache for locals 1719 1720 # Add parameters to the namespace. 1721 1722 self._add_parameters(argnames) 1723 1724 # Image generation details. 1725 1726 self.dynamic = None 1727 self.location = None 1728 self.code_location = None 1729 self.code_body_location = None 1730 1731 # Program-related details. 1732 1733 self.blocks = None 1734 self.body_block = None 1735 1736 self.temp_usage = 0 1737 self.local_usage = 0 1738 self.all_local_usage = 0 1739 1740 def _add_parameters(self, argnames): 1741 1742 "Add 'argnames' to the namespace." 1743 1744 for name in argnames: 1745 self.set(name, make_instance()) 1746 1747 for name, top_level in self._flattened_parameters(argnames): 1748 if not top_level: 1749 self.set(name, make_instance()) 1750 1751 def _flattened_parameters(self, argnames, top_level=1): 1752 l = [] 1753 for name in argnames: 1754 if isinstance(name, tuple): 1755 l += self._flattened_parameters(name, 0) 1756 else: 1757 l.append((name, top_level)) 1758 return l 1759 1760 def __repr__(self): 1761 if self.location is not None: 1762 return "<function %s (at %r, code at %r)>" % ( 1763 shortrepr(self), self.location, self.code_location 1764 ) 1765 else: 1766 return "<function %s>" % shortrepr(self) 1767 1768 def __shortrepr__(self): 1769 return "%s.%s(%s)" % (shortrepr(self.parent), self.name, ", ".join(self.argnames)) 1770 1771 def get_body_block(self): 1772 return self.body_block 1773 1774 def is_lambda(self): 1775 return self._is_lambda 1776 1777 # Defaults-related methods. 1778 1779 def store_default(self, attr_or_value): 1780 1781 """ 1782 Reserve space for defaults, set outside the function, potentially on a 1783 dynamic basis, using the 'attr_or_value'. 1784 """ 1785 1786 attr = Attr(None, self, None) 1787 self._set_using_attr(attr, attr_or_value) 1788 self.default_attrs.append(attr) 1789 1790 def make_dynamic(self): 1791 1792 "Return whether this function must be handled using a dynamic object." 1793 1794 if self.dynamic is None: 1795 for attr in self.default_attrs: 1796 if not attr.is_strict_constant() and self.dynamic_def: 1797 self.dynamic = 1 1798 self._make_dynamic() 1799 break 1800 else: 1801 self.dynamic = 0 1802 1803 return self.dynamic 1804 1805 is_dynamic = make_dynamic 1806 1807 def _make_dynamic(self): 1808 1809 "Where functions have dynamic defaults, add a context argument." 1810 1811 name = "<context>" 1812 self.argnames.insert(0, name) 1813 self.positional_names.insert(0, name) 1814 self.set(name, make_instance()) 1815 1816 # Namespace-related methods. 1817 1818 def make_global(self, name): 1819 1820 "Declare 'name' as a global in the current namespace." 1821 1822 if name not in self.argnames and not self.has_key(name): 1823 self.globals.add(name) 1824 return 1 1825 else: 1826 return 0 1827 1828 def parameters(self): 1829 1830 """ 1831 Return a dictionary mapping parameter names to their position in the 1832 parameter list. 1833 """ 1834 1835 parameters = {} 1836 for i, name in enumerate(self.argnames): 1837 parameters[name] = i 1838 return parameters 1839 1840 def tuple_parameters(self, argnames=None): 1841 1842 """ 1843 Return a list of (position, parameter) entries corresponding to tuple 1844 parameters, where each parameter may either be a string or another such 1845 list of entries. 1846 """ 1847 1848 names = argnames or self.argnames 1849 1850 l = [] 1851 for i, name in enumerate(names): 1852 if isinstance(name, tuple): 1853 l.append((i, self.tuple_parameters(name))) 1854 elif argnames: 1855 l.append((i, name)) 1856 return l 1857 1858 def all_locals(self): 1859 1860 "Return a dictionary mapping names to local and parameter details." 1861 1862 return dict(self) 1863 1864 def locals(self): 1865 1866 "Return a dictionary mapping names to local details." 1867 1868 if self.localnames is None: 1869 self.localnames = {} 1870 self.localnames.update(self.all_locals()) 1871 for name in self.argnames: 1872 del self.localnames[name] 1873 return self.localnames 1874 1875 def is_method(self): 1876 1877 """ 1878 Return whether this function is a method explicitly defined in a class. 1879 """ 1880 1881 return isinstance(self.parent, Class) 1882 1883 def is_relocated(self, name): 1884 1885 """ 1886 Determine whether the given attribute 'name' is relocated for instances 1887 having this function as a method. 1888 """ 1889 1890 for cls in self.parent.descendants: 1891 if name in cls.relocated: 1892 return 1 1893 return 0 1894 1895 # Administrative methods. 1896 1897 def items_for_vacuum(self): 1898 return self.lambdas.items() 1899 1900 def vacuum_item(self, name): 1901 del self.lambdas[name] 1902 return 1 1903 1904 def finalise_attributes(self): 1905 1906 """ 1907 Make sure all attributes (locals) are fully defined. Note that locals 1908 are not attributes in the sense of class, module or instance attributes. 1909 Defaults are also finalised by this method. 1910 """ 1911 1912 if self.finalised: 1913 return 1914 1915 # Defaults. 1916 1917 for i, default in enumerate(self.default_attrs): 1918 default.position = i 1919 1920 # Parameters. 1921 1922 i = self._finalise_parameters() 1923 1924 if i is not None: 1925 nparams = i + 1 1926 else: 1927 nparams = 0 1928 1929 # Locals (and tuple parameter names). 1930 1931 i = None 1932 for i, attr in enumerate(self.locals().values()): 1933 attr.position = i + nparams 1934 1935 if i is not None: 1936 nothers = i + 1 1937 else: 1938 nothers = 0 1939 1940 self.local_usage = nothers 1941 self.all_local_usage = nparams + nothers 1942 self.finalised = 1 1943 1944 def _finalise_parameters(self): 1945 if not self.argnames: 1946 return None 1947 1948 for i, name in enumerate(self.argnames): 1949 self[name].position = i 1950 1951 return i 1952 1953 def as_instantiator(self): 1954 1955 "Make an instantiator function from a method, keeping all arguments." 1956 1957 function = Function(self.parent.name, self.parent.parent, self.argnames, self.defaults, 1958 self.has_star, self.has_dstar, self.dynamic_def, self.module) 1959 function.default_attrs = self.default_attrs 1960 return function 1961 1962 class UnresolvedName(NamespaceDict, Constant): 1963 1964 "A module, class or function which was mentioned but could not be imported." 1965 1966 def __init__(self, name, parent_name, module=None): 1967 NamespaceDict.__init__(self, module) 1968 self.name = name 1969 self.parent_name = parent_name 1970 self.parent = None 1971 1972 self.descendants = set() 1973 1974 def add_descendant(self, cls): 1975 self.descendants.add(cls) 1976 1977 def all_attributes(self): 1978 return {} 1979 1980 def all_attribute_names(self): 1981 return [] 1982 1983 all_class_attributes = class_attributes = instance_attributes = all_attributes 1984 all_class_attribute_names = class_attribute_names = instance_attribute_names = all_attribute_names 1985 1986 def __repr__(self): 1987 return "<unknown %s>" % shortrepr(self) 1988 1989 def __shortrepr__(self): 1990 return "%s.%s" % (self.parent_name, self.name) 1991 1992 def full_name(self): 1993 if self.name is not None: 1994 return self.parent_name + "." + self.name 1995 else: 1996 return self.parent_name 1997 1998 class Module(NamespaceDict, Constant): 1999 2000 "An inspected module's core details." 2001 2002 def __init__(self, name, importer): 2003 NamespaceDict.__init__(self, self) 2004 self.name = name 2005 self.importer = importer 2006 self.parent = None 2007 2008 # Original location details. 2009 2010 self.astnode = None 2011 2012 # Complete lists of classes and functions. 2013 2014 self.all_objects = set() 2015 2016 # Keyword records. 2017 2018 self.keyword_names = set() 2019 2020 # Image generation details. 2021 2022 self.location = None 2023 self.code_location = None 2024 2025 # Program-related details. 2026 2027 self.blocks = None 2028 self.temp_usage = 0 2029 self.local_usage = 0 2030 self.all_local_usage = 0 2031 2032 def full_name(self): 2033 return self.name 2034 2035 def __repr__(self): 2036 if self.location is not None: 2037 return "<module %s (at %r)>" % (self.name, self.location) 2038 else: 2039 return "<module %s>" % shortrepr(self) 2040 2041 def __shortrepr__(self): 2042 return self.name 2043 2044 # Attribute methods. 2045 2046 "Return the module attribute names provided by the module." 2047 2048 module_attribute_names = NamespaceDict.keys 2049 2050 def module_attributes(self): 2051 2052 "Return a dictionary mapping names to module attributes." 2053 2054 return dict(self) 2055 2056 # Attribute usage methods that do not apply to module globals. 2057 2058 def _define_attribute_user(self, node): 2059 pass 2060 2061 def _use_attribute(self, name, attrname, value=None): 2062 2063 """ 2064 Record usage for 'name' of 'attrname' (and optional assignment 'value') 2065 by recording general name usage. 2066 """ 2067 2068 self.importer.use_name(attrname, self.full_name(), value) 2069 2070 def _init_attribute_user(self, node): 2071 pass 2072 2073 def _define_attribute_accessor(self, name, attrname, node, value): 2074 pass 2075 2076 # Pre-made instances. 2077 2078 type_class = TypeClass("type") # details to be filled in later 2079 2080 # Class construction. 2081 2082 def get_class(name, parent, module, node): 2083 2084 """ 2085 Return a Class instance for the class with the given 'name', 'parent', 2086 'module' and 'node'. 2087 """ 2088 2089 if name == "type" and module.full_name() == "__builtins__": 2090 type_class.set_context(parent, module, node) 2091 return type_class 2092 else: 2093 return CommonClass(name, parent, module, node) 2094 2095 # Lambda sequence numbering. 2096 2097 lambda_index = 0 2098 2099 def new_lambda(): 2100 2101 "Return a new sequence number for a lambda definition." 2102 2103 global lambda_index 2104 lambda_index += 1 2105 return lambda_index 2106 2107 # Special representations. 2108 2109 class AtLeast: 2110 2111 "A special representation for numbers of a given value or greater." 2112 2113 def __init__(self, count): 2114 self.count = count 2115 2116 def __eq__(self, other): 2117 return 0 2118 2119 __lt__ = __le__ = __eq__ 2120 2121 def __ne__(self, other): 2122 return 1 2123 2124 def __gt__(self, other): 2125 if isinstance(other, AtLeast): 2126 return 0 2127 else: 2128 return self.count > other 2129 2130 def __ge__(self, other): 2131 if isinstance(other, AtLeast): 2132 return 0 2133 else: 2134 return self.count >= other 2135 2136 def __iadd__(self, other): 2137 if isinstance(other, AtLeast): 2138 self.count += other.count 2139 else: 2140 self.count += other 2141 return self 2142 2143 def __radd__(self, other): 2144 if isinstance(other, AtLeast): 2145 return AtLeast(self.count + other.count) 2146 else: 2147 return AtLeast(self.count + other) 2148 2149 def __repr__(self): 2150 return "AtLeast(%r)" % self.count 2151 2152 # vim: tabstop=4 expandtab shiftwidth=4