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@235 | 77 | self.process_node(self.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@236 | 90 | # Visit structures and instances. |
paulb@236 | 91 | |
paulb@236 | 92 | for structure in self.module.simplifier.structures: |
paulb@236 | 93 | for instance in structure.instances.values(): |
paulb@236 | 94 | for attrs in instance.namespace.values(): |
paulb@236 | 95 | self._replace(attrs) |
paulb@236 | 96 | |
paulb@236 | 97 | def process_node(self, node): |
paulb@235 | 98 | |
paulb@235 | 99 | """ |
paulb@235 | 100 | Process a subprogram or module 'node', discovering from attributes on |
paulb@235 | 101 | 'node' any initial locals. Return a modified subprogram or module. |
paulb@235 | 102 | """ |
paulb@235 | 103 | |
paulb@235 | 104 | # Do not process subprograms already being processed. |
paulb@235 | 105 | |
paulb@235 | 106 | if node in self.current_subprograms: |
paulb@235 | 107 | return None |
paulb@235 | 108 | |
paulb@235 | 109 | # Record the current subprogram. |
paulb@235 | 110 | |
paulb@235 | 111 | self.current_subprograms.append(node) |
paulb@235 | 112 | |
paulb@235 | 113 | # Dispatch to the code itself. |
paulb@235 | 114 | |
paulb@235 | 115 | result = self.dispatch(node) |
paulb@235 | 116 | |
paulb@235 | 117 | # Restore the previous subprogram and namespace. |
paulb@235 | 118 | |
paulb@235 | 119 | self.current_subprograms.pop() |
paulb@235 | 120 | |
paulb@235 | 121 | return node |
paulb@235 | 122 | |
paulb@235 | 123 | # Visitor methods. |
paulb@235 | 124 | |
paulb@235 | 125 | def default(self, node): |
paulb@235 | 126 | |
paulb@235 | 127 | """ |
paulb@235 | 128 | Process the given 'node', given that it does not have a specific |
paulb@235 | 129 | handler. |
paulb@235 | 130 | """ |
paulb@235 | 131 | |
paulb@237 | 132 | # Process annotations. |
paulb@237 | 133 | |
paulb@235 | 134 | for name in ("non_accesses", "non_writes", "raises", "returns", "types"): |
paulb@235 | 135 | if hasattr(node, name): |
paulb@235 | 136 | attrs = getattr(node, name) |
paulb@235 | 137 | self._replace(attrs) |
paulb@236 | 138 | for name in ("accesses", "writes", "paramtypes"): |
paulb@235 | 139 | if hasattr(node, name): |
paulb@235 | 140 | d = getattr(node, name) |
paulb@235 | 141 | for expr, attrs in d.items(): |
paulb@241 | 142 | self._replace(attrs, name, d, expr) |
paulb@235 | 143 | |
paulb@237 | 144 | # Visit program nodes. |
paulb@237 | 145 | |
paulb@235 | 146 | for attr in ("pos_args",): |
paulb@235 | 147 | if hasattr(node, attr): |
paulb@235 | 148 | self.dispatches(getattr(node, attr)) |
paulb@235 | 149 | for attr in ("kw_args",): |
paulb@235 | 150 | if hasattr(node, attr): |
paulb@235 | 151 | self.dispatch_dict(getattr(node, attr)) |
paulb@235 | 152 | for attr in ("expr", "lvalue", "test", "star", "dstar"): |
paulb@235 | 153 | if hasattr(node, attr): |
paulb@235 | 154 | self.dispatch(getattr(node, attr)) |
paulb@235 | 155 | for attr in ("body", "else_", "handler", "finally_", "code", "choices", "nodes"): |
paulb@235 | 156 | if hasattr(node, attr): |
paulb@235 | 157 | self.dispatches(getattr(node, attr)) |
paulb@236 | 158 | if hasattr(node, "params"): |
paulb@236 | 159 | for param, default in node.params: |
paulb@236 | 160 | self.dispatch(default) |
paulb@236 | 161 | for attr in ("star", "dstar"): |
paulb@236 | 162 | if getattr(node, attr, None): |
paulb@236 | 163 | param, default = getattr(node, attr) |
paulb@236 | 164 | self.dispatch(default) |
paulb@235 | 165 | |
paulb@235 | 166 | return node |
paulb@235 | 167 | |
paulb@241 | 168 | def _replace(self, items, name=None, container=None, key=None): |
paulb@241 | 169 | for item in list(items): |
paulb@241 | 170 | items.remove(item) |
paulb@235 | 171 | if name == "accesses": |
paulb@236 | 172 | attr, accessor = item |
paulb@236 | 173 | value = attr.type |
paulb@241 | 174 | items.append((Attribute(self._get_replacement(attr.context), self._get_replacement(value)), self._get_replacement(accessor))) |
paulb@235 | 175 | else: |
paulb@236 | 176 | attr = item |
paulb@235 | 177 | value = attr.type |
paulb@241 | 178 | items.add(Attribute(self._get_replacement(attr.context), self._get_replacement(value))) |
paulb@236 | 179 | |
paulb@241 | 180 | if name == "accesses": |
paulb@241 | 181 | del container[key] |
paulb@241 | 182 | container[self._get_replacement(key)] = items |
paulb@236 | 183 | |
paulb@241 | 184 | def _get_replacement(self, value): |
paulb@241 | 185 | if isinstance(value, Instance): |
paulb@241 | 186 | distinct_instances = value.get_class().get_distinct_instances() |
paulb@241 | 187 | return distinct_instances[value] |
paulb@241 | 188 | else: |
paulb@241 | 189 | return value |
paulb@235 | 190 | |
paulb@235 | 191 | def dispatch(self, node, *args): |
paulb@235 | 192 | return Visitor.dispatch(self, node, *args) |
paulb@235 | 193 | |
paulb@235 | 194 | def visitInvokeFunction(self, invoke): |
paulb@235 | 195 | |
paulb@235 | 196 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@235 | 197 | |
paulb@235 | 198 | return self.default(invoke) |
paulb@235 | 199 | |
paulb@235 | 200 | def visitInvokeRef(self, invoke): |
paulb@235 | 201 | |
paulb@235 | 202 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@235 | 203 | |
paulb@235 | 204 | # The special case of internal subprogram invocation is addressed by |
paulb@235 | 205 | # propagating namespace information to the subprogram and processing it. |
paulb@235 | 206 | |
paulb@236 | 207 | subprogram = self.process_node(invoke.ref) |
paulb@235 | 208 | |
paulb@235 | 209 | if subprogram is not None: |
paulb@235 | 210 | self.subprograms.append(subprogram) |
paulb@235 | 211 | return invoke |
paulb@235 | 212 | |
paulb@235 | 213 | # Convenience functions. |
paulb@235 | 214 | |
paulb@235 | 215 | def fix(module): |
paulb@235 | 216 | |
paulb@235 | 217 | "Fix the instances in the given 'module'." |
paulb@235 | 218 | |
paulb@235 | 219 | fixer = Fixer() |
paulb@235 | 220 | fixer.process(module) |
paulb@235 | 221 | |
paulb@235 | 222 | # vim: tabstop=4 expandtab shiftwidth=4 |