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 | |
paulb@235 | 42 | # Fixing of instance information. |
paulb@235 | 43 | |
paulb@235 | 44 | class Fixer(Visitor): |
paulb@235 | 45 | |
paulb@235 | 46 | """ |
paulb@235 | 47 | The name fixer which traverses the program nodes in a module, typically |
paulb@235 | 48 | depth-first, and eliminates references to superfluous instances, replacing |
paulb@235 | 49 | them with those from each class's distinct list, if necessary. |
paulb@235 | 50 | |
paulb@261 | 51 | See the simplify.fixnames.Fixer class for a description of the mechanisms |
paulb@261 | 52 | used to deal with subprograms. |
paulb@235 | 53 | """ |
paulb@235 | 54 | |
paulb@235 | 55 | def __init__(self): |
paulb@235 | 56 | |
paulb@235 | 57 | "Initialise the name fixer." |
paulb@235 | 58 | |
paulb@235 | 59 | Visitor.__init__(self) |
paulb@235 | 60 | |
paulb@235 | 61 | # Satisfy visitor issues. |
paulb@235 | 62 | |
paulb@235 | 63 | self.visitor = self |
paulb@235 | 64 | |
paulb@235 | 65 | def process(self, module): |
paulb@235 | 66 | |
paulb@235 | 67 | "Process the given 'module'." |
paulb@235 | 68 | |
paulb@235 | 69 | # The fixer maintains a list of transformed subprograms (added for each |
paulb@235 | 70 | # of the processing "roots" and also for each invoked internal |
paulb@235 | 71 | # subprogram), along with a list of current subprograms (used to avoid |
paulb@235 | 72 | # recursion issues) and a list of current namespaces (used to recall |
paulb@235 | 73 | # namespaces upon invoking internal subprograms). |
paulb@235 | 74 | |
paulb@235 | 75 | self.current_subprograms = [] |
paulb@235 | 76 | |
paulb@235 | 77 | self.module = module |
paulb@235 | 78 | |
paulb@266 | 79 | # Process all functions and methods. |
paulb@235 | 80 | |
paulb@235 | 81 | for subprogram in self.module.simplifier.subprograms: |
paulb@235 | 82 | |
paulb@235 | 83 | # Internal subprograms are skipped here and processed specially via |
paulb@235 | 84 | # Invoke nodes. |
paulb@235 | 85 | |
paulb@269 | 86 | if not subprogram.internal: |
paulb@235 | 87 | for specialised in subprogram.active(): |
paulb@266 | 88 | self.process_node(specialised) |
paulb@266 | 89 | |
paulb@266 | 90 | self.process_node(module) |
paulb@266 | 91 | |
paulb@266 | 92 | # Fix the simplifier's subprograms list itself. |
paulb@266 | 93 | |
paulb@266 | 94 | #subprograms = set() |
paulb@266 | 95 | #for subprogram in self.module.simplifier.subprograms: |
paulb@266 | 96 | # subprograms.add(self._get_replacement(subprogram)) |
paulb@266 | 97 | #self.module.simplifier.subprograms = subprograms |
paulb@235 | 98 | |
paulb@248 | 99 | def process_structures(self, module): |
paulb@248 | 100 | |
paulb@248 | 101 | "Process the structures of the given 'module'." |
paulb@248 | 102 | |
paulb@248 | 103 | self.module = module |
paulb@248 | 104 | |
paulb@248 | 105 | # Visit structures and instances. |
paulb@248 | 106 | |
paulb@248 | 107 | for structure in self.module.simplifier.structures: |
paulb@248 | 108 | for instance in structure.get_instances(): |
paulb@248 | 109 | for name, attrs in instance.namespace.items(): |
paulb@248 | 110 | instance.namespace[name] = self._replace(attrs) |
paulb@248 | 111 | |
paulb@236 | 112 | def process_node(self, node): |
paulb@235 | 113 | |
paulb@235 | 114 | """ |
paulb@235 | 115 | Process a subprogram or module 'node', discovering from attributes on |
paulb@235 | 116 | 'node' any initial locals. Return a modified subprogram or module. |
paulb@235 | 117 | """ |
paulb@235 | 118 | |
paulb@235 | 119 | # Do not process subprograms already being processed. |
paulb@235 | 120 | |
paulb@235 | 121 | if node in self.current_subprograms: |
paulb@235 | 122 | return None |
paulb@235 | 123 | |
paulb@235 | 124 | # Record the current subprogram. |
paulb@235 | 125 | |
paulb@235 | 126 | self.current_subprograms.append(node) |
paulb@235 | 127 | |
paulb@235 | 128 | # Dispatch to the code itself. |
paulb@235 | 129 | |
paulb@235 | 130 | result = self.dispatch(node) |
paulb@235 | 131 | |
paulb@235 | 132 | # Restore the previous subprogram and namespace. |
paulb@235 | 133 | |
paulb@235 | 134 | self.current_subprograms.pop() |
paulb@235 | 135 | |
paulb@235 | 136 | return node |
paulb@235 | 137 | |
paulb@235 | 138 | # Visitor methods. |
paulb@235 | 139 | |
paulb@235 | 140 | def default(self, node): |
paulb@235 | 141 | |
paulb@235 | 142 | """ |
paulb@235 | 143 | Process the given 'node', given that it does not have a specific |
paulb@235 | 144 | handler. |
paulb@235 | 145 | """ |
paulb@235 | 146 | |
paulb@237 | 147 | # Process annotations. |
paulb@237 | 148 | |
paulb@270 | 149 | for name in ("non_accesses", "non_writes", "raises", "returns", "types", "invocations"): |
paulb@235 | 150 | if hasattr(node, name): |
paulb@235 | 151 | attrs = getattr(node, name) |
paulb@270 | 152 | setattr(node, name, self._replace(attrs, name)) |
paulb@236 | 153 | for name in ("accesses", "writes", "paramtypes"): |
paulb@235 | 154 | if hasattr(node, name): |
paulb@235 | 155 | d = getattr(node, name) |
paulb@248 | 156 | new_d = {} |
paulb@235 | 157 | for expr, attrs in d.items(): |
paulb@248 | 158 | new_d[self._get_replacement(expr)] = self._replace(attrs, name) |
paulb@248 | 159 | setattr(node, name, new_d) |
paulb@235 | 160 | |
paulb@237 | 161 | # Visit program nodes. |
paulb@237 | 162 | |
paulb@235 | 163 | for attr in ("pos_args",): |
paulb@235 | 164 | if hasattr(node, attr): |
paulb@235 | 165 | self.dispatches(getattr(node, attr)) |
paulb@235 | 166 | for attr in ("kw_args",): |
paulb@235 | 167 | if hasattr(node, attr): |
paulb@235 | 168 | self.dispatch_dict(getattr(node, attr)) |
paulb@235 | 169 | for attr in ("expr", "lvalue", "test", "star", "dstar"): |
paulb@235 | 170 | if hasattr(node, attr): |
paulb@235 | 171 | self.dispatch(getattr(node, attr)) |
paulb@235 | 172 | for attr in ("body", "else_", "handler", "finally_", "code", "choices", "nodes"): |
paulb@235 | 173 | if hasattr(node, attr): |
paulb@235 | 174 | self.dispatches(getattr(node, attr)) |
paulb@236 | 175 | if hasattr(node, "params"): |
paulb@236 | 176 | for param, default in node.params: |
paulb@236 | 177 | self.dispatch(default) |
paulb@236 | 178 | for attr in ("star", "dstar"): |
paulb@236 | 179 | if getattr(node, attr, None): |
paulb@236 | 180 | param, default = getattr(node, attr) |
paulb@236 | 181 | self.dispatch(default) |
paulb@235 | 182 | |
paulb@235 | 183 | return node |
paulb@235 | 184 | |
paulb@244 | 185 | def _replace(self, items, name=None): |
paulb@249 | 186 | |
paulb@249 | 187 | """ |
paulb@249 | 188 | Produce a new list or set for the given 'items', acquired from the |
paulb@249 | 189 | annotation having the given 'name'. |
paulb@249 | 190 | """ |
paulb@249 | 191 | |
paulb@244 | 192 | if name == "accesses": |
paulb@244 | 193 | new_items = [] |
paulb@244 | 194 | else: |
paulb@244 | 195 | new_items = set() |
paulb@244 | 196 | |
paulb@241 | 197 | for item in list(items): |
paulb@235 | 198 | if name == "accesses": |
paulb@236 | 199 | attr, accessor = item |
paulb@236 | 200 | value = attr.type |
paulb@244 | 201 | new_items.append((Attribute(self._get_replacement(attr.context), self._get_replacement(value)), self._get_replacement(accessor))) |
paulb@270 | 202 | elif name == "invocations": |
paulb@270 | 203 | new_items.add(self._get_replacement(item)) |
paulb@235 | 204 | else: |
paulb@236 | 205 | attr = item |
paulb@235 | 206 | value = attr.type |
paulb@244 | 207 | new_items.add(Attribute(self._get_replacement(attr.context), self._get_replacement(value))) |
paulb@236 | 208 | |
paulb@244 | 209 | return new_items |
paulb@236 | 210 | |
paulb@241 | 211 | def _get_replacement(self, value): |
paulb@249 | 212 | |
paulb@249 | 213 | "Get a replacement for the given 'value'." |
paulb@249 | 214 | |
paulb@249 | 215 | # Find the distinct instance for any given instance. |
paulb@249 | 216 | |
paulb@241 | 217 | if isinstance(value, Instance): |
paulb@241 | 218 | distinct_instances = value.get_class().get_distinct_instances() |
paulb@241 | 219 | return distinct_instances[value] |
paulb@249 | 220 | |
paulb@249 | 221 | # For subprograms, find the distinct instance's copy for the owner |
paulb@259 | 222 | # instance and assert that the signatures are the same; otherwise, |
paulb@259 | 223 | # return the original subprogram. |
paulb@259 | 224 | # NOTE: This needs to be verified in a somewhat more formal fashion. |
paulb@249 | 225 | |
paulb@249 | 226 | elif isinstance(value, Subprogram): |
paulb@249 | 227 | if hasattr(value, "copy_of") and hasattr(value, "instance"): |
paulb@249 | 228 | cls = value.instance.get_class() |
paulb@249 | 229 | distinct = cls.get_distinct_instances() |
paulb@249 | 230 | instance = distinct[value.instance] |
paulb@259 | 231 | if value.copy_of.copies.has_key(instance): |
paulb@259 | 232 | subprogram = value.copy_of.copies[instance] |
paulb@259 | 233 | if subprogram.paramtypes == value.paramtypes: |
paulb@259 | 234 | return subprogram |
paulb@259 | 235 | |
paulb@259 | 236 | return value |
paulb@249 | 237 | |
paulb@249 | 238 | # Return all other values as they are. |
paulb@249 | 239 | |
paulb@241 | 240 | else: |
paulb@241 | 241 | return value |
paulb@235 | 242 | |
paulb@235 | 243 | def visitInvokeFunction(self, invoke): |
paulb@235 | 244 | |
paulb@235 | 245 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@235 | 246 | |
paulb@235 | 247 | return self.default(invoke) |
paulb@235 | 248 | |
paulb@235 | 249 | def visitInvokeRef(self, invoke): |
paulb@235 | 250 | |
paulb@235 | 251 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@235 | 252 | |
paulb@235 | 253 | # The special case of internal subprogram invocation is addressed by |
paulb@235 | 254 | # propagating namespace information to the subprogram and processing it. |
paulb@235 | 255 | |
paulb@266 | 256 | self.process_node(invoke.ref) |
paulb@235 | 257 | return invoke |
paulb@235 | 258 | |
paulb@235 | 259 | # Convenience functions. |
paulb@235 | 260 | |
paulb@248 | 261 | def fix_structures(module): |
paulb@248 | 262 | |
paulb@248 | 263 | "Fix the structures in the given 'module'." |
paulb@248 | 264 | |
paulb@248 | 265 | fixer = Fixer() |
paulb@248 | 266 | fixer.process_structures(module) |
paulb@248 | 267 | |
paulb@235 | 268 | def fix(module): |
paulb@235 | 269 | |
paulb@248 | 270 | "Fix the structure references in the given 'module'." |
paulb@235 | 271 | |
paulb@235 | 272 | fixer = Fixer() |
paulb@235 | 273 | fixer.process(module) |
paulb@235 | 274 | |
paulb@235 | 275 | # vim: tabstop=4 expandtab shiftwidth=4 |