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