1 #!/usr/bin/env python 2 3 """ 4 Fix instances, removing those which are not part of the distinct set for a given 5 class. 6 7 Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> 8 9 This software is free software; you can redistribute it and/or 10 modify it under the terms of the GNU General Public License as 11 published by the Free Software Foundation; either version 2 of 12 the License, or (at your option) any later version. 13 14 This software is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public 20 License along with this library; see the file LICENCE.txt 21 If not, write to the Free Software Foundation, Inc., 22 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 23 24 -------- 25 26 To use this module, the easiest approach is to use the fix function: 27 28 fix(module) 29 30 The more complicated approach involves instantiating a Fixer object: 31 32 fixer = Fixer() 33 34 Then, applying the fixer to an existing module: 35 36 fixer.process(module) 37 """ 38 39 from simplify.simplified import * 40 41 # Fixing of instance information. 42 43 class Fixer(Visitor): 44 45 """ 46 The name fixer which traverses the program nodes in a module, typically 47 depth-first, and eliminates references to superfluous instances, replacing 48 them with those from each class's distinct list, if necessary. 49 50 See the simplify.fixnames.Fixer class for a description of the 51 """ 52 53 def __init__(self): 54 55 "Initialise the name fixer." 56 57 Visitor.__init__(self) 58 59 # Satisfy visitor issues. 60 61 self.visitor = self 62 63 def process(self, module): 64 65 "Process the given 'module'." 66 67 # The fixer maintains a list of transformed subprograms (added for each 68 # of the processing "roots" and also for each invoked internal 69 # subprogram), along with a list of current subprograms (used to avoid 70 # recursion issues) and a list of current namespaces (used to recall 71 # namespaces upon invoking internal subprograms). 72 73 self.subprograms = [] 74 self.current_subprograms = [] 75 76 self.module = module 77 self.process_node(self.module) 78 79 # Then, process all functions and methods. 80 81 for subprogram in self.module.simplifier.subprograms: 82 83 # Internal subprograms are skipped here and processed specially via 84 # Invoke nodes. 85 86 if not getattr(subprogram, "internal", 0): 87 for specialised in subprogram.active(): 88 self.subprograms.append(self.process_node(specialised)) 89 90 def process_node(self, node, namespace=None): 91 92 """ 93 Process a subprogram or module 'node', discovering from attributes on 94 'node' any initial locals. Return a modified subprogram or module. 95 """ 96 97 # Do not process subprograms already being processed. 98 99 if node in self.current_subprograms: 100 return None 101 102 # Record the current subprogram. 103 104 self.current_subprograms.append(node) 105 106 # Dispatch to the code itself. 107 108 result = self.dispatch(node) 109 110 # Restore the previous subprogram and namespace. 111 112 self.current_subprograms.pop() 113 114 return node 115 116 # Visitor methods. 117 118 def default(self, node): 119 120 """ 121 Process the given 'node', given that it does not have a specific 122 handler. 123 """ 124 125 for name in ("non_accesses", "non_writes", "raises", "returns", "types"): 126 if hasattr(node, name): 127 attrs = getattr(node, name) 128 self._replace(attrs) 129 for name in ("accesses", "writes"): 130 if hasattr(node, name): 131 d = getattr(node, name) 132 for expr, attrs in d.items(): 133 self._replace(attrs, name) 134 135 for attr in ("pos_args",): 136 if hasattr(node, attr): 137 self.dispatches(getattr(node, attr)) 138 for attr in ("kw_args",): 139 if hasattr(node, attr): 140 self.dispatch_dict(getattr(node, attr)) 141 for attr in ("expr", "lvalue", "test", "star", "dstar"): 142 if hasattr(node, attr): 143 self.dispatch(getattr(node, attr)) 144 for attr in ("body", "else_", "handler", "finally_", "code", "choices", "nodes"): 145 if hasattr(node, attr): 146 self.dispatches(getattr(node, attr)) 147 148 return node 149 150 def _replace(self, attrs, name=None): 151 to_replace = {} 152 for attr in attrs: 153 if name == "accesses": 154 _attr, accessor = attr 155 value = _attr.type 156 else: 157 value = attr.type 158 if isinstance(value, Instance) and not to_replace.has_key(value): 159 distinct_instances = value.get_class().get_distinct_instances() 160 if distinct_instances.has_key(value): 161 attr.type = distinct_instances[value] 162 163 def dispatch(self, node, *args): 164 return Visitor.dispatch(self, node, *args) 165 166 def visitInvokeFunction(self, invoke): 167 168 "Transform the 'invoke' node, performing processing on subprograms." 169 170 return self.default(invoke) 171 172 def visitInvokeRef(self, invoke): 173 174 "Transform the 'invoke' node, performing processing on subprograms." 175 176 # The special case of internal subprogram invocation is addressed by 177 # propagating namespace information to the subprogram and processing it. 178 179 if invoke.share_locals: 180 subprogram = self.process_node(invoke.ref, self.namespace) 181 else: 182 subprogram = self.process_node(invoke.ref) 183 184 if subprogram is not None: 185 self.subprograms.append(subprogram) 186 return invoke 187 188 # Convenience functions. 189 190 def fix(module): 191 192 "Fix the instances in the given 'module'." 193 194 fixer = Fixer() 195 fixer.process(module) 196 197 # vim: tabstop=4 expandtab shiftwidth=4