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_node(self, node): 113 114 """ 115 Process a subprogram or module 'node', discovering from attributes on 116 'node' any initial locals. Return a modified subprogram or module. 117 """ 118 119 # Do not process subprograms already being processed. 120 121 if node in self.current_subprograms: 122 return None 123 124 # Record the current subprogram. 125 126 self.current_subprograms.append(node) 127 128 # Dispatch to the code itself. 129 130 result = self.dispatch(node) 131 132 # Restore the previous subprogram and namespace. 133 134 self.current_subprograms.pop() 135 136 return node 137 138 # Visitor methods. 139 140 def default(self, node): 141 142 """ 143 Process the given 'node', given that it does not have a specific 144 handler. 145 """ 146 147 # Process annotations. 148 149 for name in ("non_accesses", "non_writes", "raises", "returns", "types", "invocations"): 150 if hasattr(node, name): 151 attrs = getattr(node, name) 152 setattr(node, name, self._replace(attrs, name)) 153 for name in ("accesses", "writes", "paramtypes"): 154 if hasattr(node, name): 155 d = getattr(node, name) 156 new_d = {} 157 for expr, attrs in d.items(): 158 new_d[self._get_replacement(expr)] = self._replace(attrs, name) 159 setattr(node, name, new_d) 160 161 # Visit program nodes. 162 163 for attr in ("pos_args",): 164 if hasattr(node, attr): 165 self.dispatches(getattr(node, attr)) 166 for attr in ("kw_args",): 167 if hasattr(node, attr): 168 self.dispatch_dict(getattr(node, attr)) 169 for attr in ("expr", "lvalue", "test", "star", "dstar"): 170 if hasattr(node, attr): 171 self.dispatch(getattr(node, attr)) 172 for attr in ("body", "else_", "handler", "finally_", "code", "choices", "nodes"): 173 if hasattr(node, attr): 174 self.dispatches(getattr(node, attr)) 175 if hasattr(node, "params"): 176 for param, default in node.params: 177 self.dispatch(default) 178 for attr in ("star", "dstar"): 179 if getattr(node, attr, None): 180 param, default = getattr(node, attr) 181 self.dispatch(default) 182 183 return node 184 185 def _replace(self, items, name=None): 186 187 """ 188 Produce a new list or set for the given 'items', acquired from the 189 annotation having the given 'name'. 190 """ 191 192 if name == "accesses": 193 new_items = [] 194 else: 195 new_items = set() 196 197 for item in list(items): 198 if name == "accesses": 199 attr, accessor = item 200 value = attr.type 201 new_items.append((Attribute(self._get_replacement(attr.context), self._get_replacement(value)), self._get_replacement(accessor))) 202 elif name == "invocations": 203 new_items.add(self._get_replacement(item)) 204 else: 205 attr = item 206 value = attr.type 207 new_items.add(Attribute(self._get_replacement(attr.context), self._get_replacement(value))) 208 209 return new_items 210 211 def _get_replacement(self, value): 212 213 "Get a replacement for the given 'value'." 214 215 # Find the distinct instance for any given instance. 216 217 if isinstance(value, Instance): 218 distinct_instances = value.get_class().get_distinct_instances() 219 return distinct_instances[value] 220 221 # For subprograms, find the distinct instance's copy for the owner 222 # instance and assert that the signatures are the same; otherwise, 223 # return the original subprogram. 224 # NOTE: This needs to be verified in a somewhat more formal fashion. 225 226 elif isinstance(value, Subprogram): 227 if hasattr(value, "copy_of") and hasattr(value, "instance"): 228 cls = value.instance.get_class() 229 distinct = cls.get_distinct_instances() 230 instance = distinct[value.instance] 231 if value.copy_of.copies.has_key(instance): 232 subprogram = value.copy_of.copies[instance] 233 if subprogram.paramtypes == value.paramtypes: 234 return subprogram 235 236 return value 237 238 # Return all other values as they are. 239 240 else: 241 return value 242 243 def visitInvokeFunction(self, invoke): 244 245 "Transform the 'invoke' node, performing processing on subprograms." 246 247 return self.default(invoke) 248 249 def visitInvokeRef(self, invoke): 250 251 "Transform the 'invoke' node, performing processing on subprograms." 252 253 # The special case of internal subprogram invocation is addressed by 254 # propagating namespace information to the subprogram and processing it. 255 256 self.process_node(invoke.ref) 257 return invoke 258 259 # Convenience functions. 260 261 def fix_structures(module): 262 263 "Fix the structures in the given 'module'." 264 265 fixer = Fixer() 266 fixer.process_structures(module) 267 268 def fix(module): 269 270 "Fix the structure references in the given 'module'." 271 272 fixer = Fixer() 273 fixer.process(module) 274 275 # vim: tabstop=4 expandtab shiftwidth=4