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