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@235 | 9 | This software is free software; you can redistribute it and/or |
paulb@235 | 10 | modify it under the terms of the GNU General Public License as |
paulb@235 | 11 | published by the Free Software Foundation; either version 2 of |
paulb@235 | 12 | the License, or (at your option) any later version. |
paulb@235 | 13 | |
paulb@235 | 14 | This software is distributed in the hope that it will be useful, |
paulb@235 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@235 | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@235 | 17 | GNU General Public License for more details. |
paulb@235 | 18 | |
paulb@235 | 19 | You should have received a copy of the GNU General Public |
paulb@235 | 20 | License along with this library; see the file LICENCE.txt |
paulb@235 | 21 | If not, write to the Free Software Foundation, Inc., |
paulb@235 | 22 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@235 | 23 | |
paulb@235 | 24 | -------- |
paulb@235 | 25 | |
paulb@235 | 26 | To use this module, the easiest approach is to use the fix function: |
paulb@235 | 27 | |
paulb@235 | 28 | fix(module) |
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@235 | 36 | fixer.process(module) |
paulb@235 | 37 | """ |
paulb@235 | 38 | |
paulb@235 | 39 | from simplify.simplified import * |
paulb@235 | 40 | |
paulb@235 | 41 | # Fixing of instance information. |
paulb@235 | 42 | |
paulb@235 | 43 | class Fixer(Visitor): |
paulb@235 | 44 | |
paulb@235 | 45 | """ |
paulb@235 | 46 | The name fixer which traverses the program nodes in a module, typically |
paulb@235 | 47 | depth-first, and eliminates references to superfluous instances, replacing |
paulb@235 | 48 | them with those from each class's distinct list, if necessary. |
paulb@235 | 49 | |
paulb@235 | 50 | See the simplify.fixnames.Fixer class for a description of the |
paulb@235 | 51 | """ |
paulb@235 | 52 | |
paulb@235 | 53 | def __init__(self): |
paulb@235 | 54 | |
paulb@235 | 55 | "Initialise the name fixer." |
paulb@235 | 56 | |
paulb@235 | 57 | Visitor.__init__(self) |
paulb@235 | 58 | |
paulb@235 | 59 | # Satisfy visitor issues. |
paulb@235 | 60 | |
paulb@235 | 61 | self.visitor = self |
paulb@235 | 62 | |
paulb@235 | 63 | def process(self, module): |
paulb@235 | 64 | |
paulb@235 | 65 | "Process the given 'module'." |
paulb@235 | 66 | |
paulb@235 | 67 | # The fixer maintains a list of transformed subprograms (added for each |
paulb@235 | 68 | # of the processing "roots" and also for each invoked internal |
paulb@235 | 69 | # subprogram), along with a list of current subprograms (used to avoid |
paulb@235 | 70 | # recursion issues) and a list of current namespaces (used to recall |
paulb@235 | 71 | # namespaces upon invoking internal subprograms). |
paulb@235 | 72 | |
paulb@235 | 73 | self.subprograms = [] |
paulb@235 | 74 | self.current_subprograms = [] |
paulb@235 | 75 | |
paulb@235 | 76 | self.module = module |
paulb@248 | 77 | self.process_node(module) |
paulb@235 | 78 | |
paulb@235 | 79 | # Then, 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@235 | 86 | if not getattr(subprogram, "internal", 0): |
paulb@235 | 87 | for specialised in subprogram.active(): |
paulb@235 | 88 | self.subprograms.append(self.process_node(specialised)) |
paulb@235 | 89 | |
paulb@248 | 90 | def process_structures(self, module): |
paulb@248 | 91 | |
paulb@248 | 92 | "Process the structures of the given 'module'." |
paulb@248 | 93 | |
paulb@248 | 94 | self.module = module |
paulb@248 | 95 | |
paulb@248 | 96 | # Visit structures and instances. |
paulb@248 | 97 | |
paulb@248 | 98 | for structure in self.module.simplifier.structures: |
paulb@248 | 99 | for instance in structure.get_instances(): |
paulb@248 | 100 | for name, attrs in instance.namespace.items(): |
paulb@248 | 101 | instance.namespace[name] = self._replace(attrs) |
paulb@248 | 102 | |
paulb@236 | 103 | def process_node(self, node): |
paulb@235 | 104 | |
paulb@235 | 105 | """ |
paulb@235 | 106 | Process a subprogram or module 'node', discovering from attributes on |
paulb@235 | 107 | 'node' any initial locals. Return a modified subprogram or module. |
paulb@235 | 108 | """ |
paulb@235 | 109 | |
paulb@235 | 110 | # Do not process subprograms already being processed. |
paulb@235 | 111 | |
paulb@235 | 112 | if node in self.current_subprograms: |
paulb@235 | 113 | return None |
paulb@235 | 114 | |
paulb@235 | 115 | # Record the current subprogram. |
paulb@235 | 116 | |
paulb@235 | 117 | self.current_subprograms.append(node) |
paulb@235 | 118 | |
paulb@235 | 119 | # Dispatch to the code itself. |
paulb@235 | 120 | |
paulb@235 | 121 | result = self.dispatch(node) |
paulb@235 | 122 | |
paulb@235 | 123 | # Restore the previous subprogram and namespace. |
paulb@235 | 124 | |
paulb@235 | 125 | self.current_subprograms.pop() |
paulb@235 | 126 | |
paulb@235 | 127 | return node |
paulb@235 | 128 | |
paulb@235 | 129 | # Visitor methods. |
paulb@235 | 130 | |
paulb@235 | 131 | def default(self, node): |
paulb@235 | 132 | |
paulb@235 | 133 | """ |
paulb@235 | 134 | Process the given 'node', given that it does not have a specific |
paulb@235 | 135 | handler. |
paulb@235 | 136 | """ |
paulb@235 | 137 | |
paulb@237 | 138 | # Process annotations. |
paulb@237 | 139 | |
paulb@235 | 140 | for name in ("non_accesses", "non_writes", "raises", "returns", "types"): |
paulb@235 | 141 | if hasattr(node, name): |
paulb@235 | 142 | attrs = getattr(node, name) |
paulb@244 | 143 | setattr(node, name, self._replace(attrs)) |
paulb@236 | 144 | for name in ("accesses", "writes", "paramtypes"): |
paulb@235 | 145 | if hasattr(node, name): |
paulb@235 | 146 | d = getattr(node, name) |
paulb@248 | 147 | new_d = {} |
paulb@235 | 148 | for expr, attrs in d.items(): |
paulb@248 | 149 | new_d[self._get_replacement(expr)] = self._replace(attrs, name) |
paulb@248 | 150 | setattr(node, name, new_d) |
paulb@235 | 151 | |
paulb@237 | 152 | # Visit program nodes. |
paulb@237 | 153 | |
paulb@235 | 154 | for attr in ("pos_args",): |
paulb@235 | 155 | if hasattr(node, attr): |
paulb@235 | 156 | self.dispatches(getattr(node, attr)) |
paulb@235 | 157 | for attr in ("kw_args",): |
paulb@235 | 158 | if hasattr(node, attr): |
paulb@235 | 159 | self.dispatch_dict(getattr(node, attr)) |
paulb@235 | 160 | for attr in ("expr", "lvalue", "test", "star", "dstar"): |
paulb@235 | 161 | if hasattr(node, attr): |
paulb@235 | 162 | self.dispatch(getattr(node, attr)) |
paulb@235 | 163 | for attr in ("body", "else_", "handler", "finally_", "code", "choices", "nodes"): |
paulb@235 | 164 | if hasattr(node, attr): |
paulb@235 | 165 | self.dispatches(getattr(node, attr)) |
paulb@236 | 166 | if hasattr(node, "params"): |
paulb@236 | 167 | for param, default in node.params: |
paulb@236 | 168 | self.dispatch(default) |
paulb@236 | 169 | for attr in ("star", "dstar"): |
paulb@236 | 170 | if getattr(node, attr, None): |
paulb@236 | 171 | param, default = getattr(node, attr) |
paulb@236 | 172 | self.dispatch(default) |
paulb@235 | 173 | |
paulb@235 | 174 | return node |
paulb@235 | 175 | |
paulb@244 | 176 | def _replace(self, items, name=None): |
paulb@244 | 177 | if name == "accesses": |
paulb@244 | 178 | new_items = [] |
paulb@244 | 179 | else: |
paulb@244 | 180 | new_items = set() |
paulb@244 | 181 | |
paulb@241 | 182 | for item in list(items): |
paulb@235 | 183 | if name == "accesses": |
paulb@236 | 184 | attr, accessor = item |
paulb@236 | 185 | value = attr.type |
paulb@244 | 186 | new_items.append((Attribute(self._get_replacement(attr.context), self._get_replacement(value)), self._get_replacement(accessor))) |
paulb@235 | 187 | else: |
paulb@236 | 188 | attr = item |
paulb@235 | 189 | value = attr.type |
paulb@244 | 190 | new_items.add(Attribute(self._get_replacement(attr.context), self._get_replacement(value))) |
paulb@236 | 191 | |
paulb@244 | 192 | return new_items |
paulb@236 | 193 | |
paulb@241 | 194 | def _get_replacement(self, value): |
paulb@241 | 195 | if isinstance(value, Instance): |
paulb@241 | 196 | distinct_instances = value.get_class().get_distinct_instances() |
paulb@241 | 197 | return distinct_instances[value] |
paulb@241 | 198 | else: |
paulb@241 | 199 | return value |
paulb@235 | 200 | |
paulb@235 | 201 | def dispatch(self, node, *args): |
paulb@235 | 202 | return Visitor.dispatch(self, node, *args) |
paulb@235 | 203 | |
paulb@235 | 204 | def visitInvokeFunction(self, invoke): |
paulb@235 | 205 | |
paulb@235 | 206 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@235 | 207 | |
paulb@235 | 208 | return self.default(invoke) |
paulb@235 | 209 | |
paulb@235 | 210 | def visitInvokeRef(self, invoke): |
paulb@235 | 211 | |
paulb@235 | 212 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@235 | 213 | |
paulb@235 | 214 | # The special case of internal subprogram invocation is addressed by |
paulb@235 | 215 | # propagating namespace information to the subprogram and processing it. |
paulb@235 | 216 | |
paulb@236 | 217 | subprogram = self.process_node(invoke.ref) |
paulb@235 | 218 | |
paulb@235 | 219 | if subprogram is not None: |
paulb@235 | 220 | self.subprograms.append(subprogram) |
paulb@235 | 221 | return invoke |
paulb@235 | 222 | |
paulb@235 | 223 | # Convenience functions. |
paulb@235 | 224 | |
paulb@248 | 225 | def fix_structures(module): |
paulb@248 | 226 | |
paulb@248 | 227 | "Fix the structures in the given 'module'." |
paulb@248 | 228 | |
paulb@248 | 229 | fixer = Fixer() |
paulb@248 | 230 | fixer.process_structures(module) |
paulb@248 | 231 | |
paulb@235 | 232 | def fix(module): |
paulb@235 | 233 | |
paulb@248 | 234 | "Fix the structure references in the given 'module'." |
paulb@235 | 235 | |
paulb@235 | 236 | fixer = Fixer() |
paulb@235 | 237 | fixer.process(module) |
paulb@235 | 238 | |
paulb@235 | 239 | # vim: tabstop=4 expandtab shiftwidth=4 |