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 program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 22 -------- 23 24 To use this module, the easiest approach is to use the fix and fix_structures 25 functions: 26 27 fix_structures(module) # to fix the structures 28 fix(module) # to fix references to the structures 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_structures(module) 37 fixer.process(module) 38 """ 39 40 from simplify.simplified import * 41 42 # Fixing of instance information. 43 44 class Fixer(Visitor): 45 46 """ 47 The name fixer which traverses the program nodes in a module, typically 48 depth-first, and eliminates references to superfluous instances, replacing 49 them with those from each class's distinct list, if necessary. 50 51 See the simplify.fixnames.Fixer class for a description of the mechanisms 52 used to deal with subprograms. 53 """ 54 55 def __init__(self): 56 57 "Initialise the name fixer." 58 59 Visitor.__init__(self) 60 61 # Satisfy visitor issues. 62 63 self.visitor = self 64 65 def process(self, module): 66 67 "Process the given 'module'." 68 69 # The fixer maintains a list of transformed subprograms (added for each 70 # of the processing "roots" and also for each invoked internal 71 # subprogram), along with a list of current subprograms (used to avoid 72 # recursion issues) and a list of current namespaces (used to recall 73 # namespaces upon invoking internal subprograms). 74 75 self.current_subprograms = [] 76 77 self.module = module 78 79 # 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 subprogram.internal: 87 for specialised in subprogram.active(): 88 self.process_node(specialised) 89 90 self.process_node(module) 91 92 # Fix the simplifier's subprograms list itself. 93 94 #subprograms = set() 95 #for subprogram in self.module.simplifier.subprograms: 96 # subprograms.add(self._get_replacement(subprogram)) 97 #self.module.simplifier.subprograms = subprograms 98 99 def process_structures(self, module): 100 101 "Process the structures of the given 'module'." 102 103 self.module = module 104 105 # Visit structures and instances. 106 107 for structure in self.module.simplifier.structures: 108 for instance in structure.get_instances(): 109 for name, attrs in instance.namespace.items(): 110 instance.namespace[name] = self._replace(attrs) 111 112 def process_signatures(self, module): 113 114 "Process the signatures of subprograms in this 'module'." 115 116 self.module = module 117 118 # Visit each subprogram, updating the signatures. 119 120 for subprogram in self.module.simplifier.subprograms: 121 for specialisation in subprogram.active(): 122 self._replace_dict(specialisation, "paramtypes") 123 124 def process_node(self, node): 125 126 """ 127 Process a subprogram or module 'node', discovering from attributes on 128 'node' any initial locals. Return a modified subprogram or module. 129 """ 130 131 # Do not process subprograms already being processed. 132 133 if node in self.current_subprograms: 134 return None 135 136 # Record the current subprogram. 137 138 self.current_subprograms.append(node) 139 140 # Dispatch to the code itself. 141 142 result = self.dispatch(node) 143 144 # Restore the previous subprogram and namespace. 145 146 self.current_subprograms.pop() 147 148 return node 149 150 # Visitor methods. 151 152 def default(self, node): 153 154 """ 155 Process the given 'node', given that it does not have a specific 156 handler. 157 """ 158 159 # Process annotations. 160 161 for name in ("non_accesses", "non_writes", "raises", "returns", "types", "invocations"): 162 if hasattr(node, name): 163 self._replace_list(node, name) 164 for name in ("accesses", "writes", "paramtypes"): 165 if hasattr(node, name): 166 self._replace_dict(node, name) 167 168 # Visit program nodes. 169 170 for attr in ("pos_args",): 171 if hasattr(node, attr): 172 self.dispatches(getattr(node, attr)) 173 for attr in ("kw_args",): 174 if hasattr(node, attr): 175 self.dispatch_dict(getattr(node, attr)) 176 for attr in ("expr", "lvalue", "test", "star", "dstar"): 177 if hasattr(node, attr): 178 self.dispatch(getattr(node, attr)) 179 for attr in ("body", "else_", "handler", "finally_", "code", "choices", "nodes"): 180 if hasattr(node, attr): 181 self.dispatches(getattr(node, attr)) 182 if hasattr(node, "params"): 183 for param, default in node.params: 184 self.dispatch(default) 185 for attr in ("star", "dstar"): 186 if getattr(node, attr, None): 187 param, default = getattr(node, attr) 188 self.dispatch(default) 189 190 return node 191 192 def _replace_list(self, node, name): 193 attrs = getattr(node, name) 194 setattr(node, name, self._replace(attrs, name)) 195 196 def _replace_dict(self, node, name): 197 d = getattr(node, name) 198 new_d = {} 199 for expr, attrs in d.items(): 200 new_d[self._get_replacement(expr)] = self._replace(attrs, name) 201 setattr(node, name, new_d) 202 203 def _replace(self, items, name=None): 204 205 """ 206 Produce a new list or set for the given 'items', acquired from the 207 annotation having the given 'name'. 208 """ 209 210 if name == "accesses": 211 new_items = [] 212 else: 213 new_items = set() 214 215 for item in list(items): 216 if name == "accesses": 217 attr, accessor = item 218 value = attr.type 219 new_items.append((Attribute(self._get_replacement(attr.context), self._get_replacement(value)), self._get_replacement(accessor))) 220 elif name == "invocations": 221 new_items.add(self._get_replacement(item)) 222 else: 223 attr = item 224 value = attr.type 225 new_items.add(Attribute(self._get_replacement(attr.context), self._get_replacement(value))) 226 227 return new_items 228 229 def _get_replacement(self, value): 230 231 "Get a replacement for the given 'value'." 232 233 # Find the distinct instance for any given instance. 234 235 if isinstance(value, Instance): 236 distinct_instances = value.get_class().get_distinct_instances() 237 return distinct_instances[value] 238 239 # For subprograms, find the distinct instance's copy for the owner 240 # instance and assert that the signatures are the same; otherwise, 241 # return the original subprogram. 242 # NOTE: This needs to be verified in a somewhat more formal fashion. 243 244 elif isinstance(value, Subprogram): 245 if hasattr(value, "copy_of") and hasattr(value, "instance"): 246 cls = value.instance.get_class() 247 distinct = cls.get_distinct_instances() 248 instance = distinct[value.instance] 249 if value.copy_of.copies.has_key(instance): 250 subprogram = value.copy_of.copies[instance] 251 if subprogram.paramtypes == value.paramtypes: 252 return subprogram 253 254 return value 255 256 # Return all other values as they are. 257 258 else: 259 return value 260 261 def visitInvokeFunction(self, invoke): 262 263 "Transform the 'invoke' node, performing processing on subprograms." 264 265 return self.default(invoke) 266 267 def visitInvokeRef(self, invoke): 268 269 "Transform the 'invoke' node, performing processing on subprograms." 270 271 # The special case of internal subprogram invocation is addressed by 272 # propagating namespace information to the subprogram and processing it. 273 274 self.process_node(invoke.ref) 275 return invoke 276 277 # Convenience functions. 278 279 def fix_structures(module): 280 281 "Fix the structures in the given 'module'." 282 283 fixer = Fixer() 284 fixer.process_structures(module) 285 286 def fix_signatures(module): 287 288 "Fix the signatures in the given 'module'." 289 290 fixer = Fixer() 291 fixer.process_signatures(module) 292 293 def fix(module): 294 295 "Fix the structure references in the given 'module'." 296 297 fixer = Fixer() 298 fixer.process(module) 299 300 # vim: tabstop=4 expandtab shiftwidth=4