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