paulb@31 | 1 | #!/usr/bin/env python |
paulb@31 | 2 | |
paulb@31 | 3 | """ |
paulb@31 | 4 | Fix name-related operations. The code in this module operates upon nodes which |
paulb@31 | 5 | are produced when simplifying AST node trees originating from the compiler |
paulb@31 | 6 | module. |
paulb@31 | 7 | |
paulb@31 | 8 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@31 | 9 | |
paulb@31 | 10 | This software is free software; you can redistribute it and/or |
paulb@31 | 11 | modify it under the terms of the GNU General Public License as |
paulb@31 | 12 | published by the Free Software Foundation; either version 2 of |
paulb@31 | 13 | the License, or (at your option) any later version. |
paulb@31 | 14 | |
paulb@31 | 15 | This software is distributed in the hope that it will be useful, |
paulb@31 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@31 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@31 | 18 | GNU General Public License for more details. |
paulb@31 | 19 | |
paulb@31 | 20 | You should have received a copy of the GNU General Public |
paulb@31 | 21 | License along with this library; see the file LICENCE.txt |
paulb@31 | 22 | If not, write to the Free Software Foundation, Inc., |
paulb@31 | 23 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@46 | 24 | |
paulb@46 | 25 | -------- |
paulb@46 | 26 | |
paulb@46 | 27 | To use this module, first instantiate a Fixer object: |
paulb@46 | 28 | |
paulb@46 | 29 | fixer = Fixer() |
paulb@46 | 30 | |
paulb@46 | 31 | Then, apply the fixer to an existing Simplifier object: |
paulb@46 | 32 | |
paulb@46 | 33 | simplifier = ... |
paulb@46 | 34 | fixer.process(simplifier) |
paulb@46 | 35 | |
paulb@46 | 36 | If an existing simplifier has been used to process a module containing built-in |
paulb@46 | 37 | classes and functions, apply the fixer as follows: |
paulb@46 | 38 | |
paulb@46 | 39 | fixer.process(simplifier, builtins_simplifier) |
paulb@31 | 40 | """ |
paulb@31 | 41 | |
paulb@31 | 42 | from simplified import * |
paulb@31 | 43 | |
paulb@31 | 44 | # Fixing of name-related operations. |
paulb@31 | 45 | |
paulb@31 | 46 | class Fixer(Visitor): |
paulb@31 | 47 | |
paulb@31 | 48 | """ |
paulb@31 | 49 | The name fixer which traverses the program nodes, typically depth-first, |
paulb@31 | 50 | and maintains a record of name usage in the different namespaces. As a |
paulb@31 | 51 | consequence of various observations, some parts of the program node tree are |
paulb@31 | 52 | modified with different operations employed to those originally defined. |
paulb@31 | 53 | """ |
paulb@31 | 54 | |
paulb@31 | 55 | def __init__(self): |
paulb@45 | 56 | |
paulb@45 | 57 | "Initialise the name fixer." |
paulb@45 | 58 | |
paulb@31 | 59 | Visitor.__init__(self) |
paulb@31 | 60 | |
paulb@31 | 61 | # Satisfy visitor issues. |
paulb@31 | 62 | |
paulb@31 | 63 | self.visitor = self |
paulb@31 | 64 | |
paulb@46 | 65 | def process(self, visitor, builtins_visitor=None): |
paulb@45 | 66 | |
paulb@46 | 67 | """ |
paulb@46 | 68 | Process the resources of the given 'visitor' optionally using a |
paulb@46 | 69 | 'builtins_visitor' to reference built-in objects. |
paulb@46 | 70 | """ |
paulb@45 | 71 | |
paulb@45 | 72 | self.subprograms = [] |
paulb@45 | 73 | self.current_subprograms = [] |
paulb@45 | 74 | self.current_namespaces = [] |
paulb@45 | 75 | |
paulb@45 | 76 | # First, process the top-level code, finding out which names are |
paulb@45 | 77 | # defined at that level. |
paulb@45 | 78 | |
paulb@45 | 79 | self.global_namespace = None |
paulb@46 | 80 | self.module = visitor.result |
paulb@46 | 81 | if builtins_visitor is not None: |
paulb@46 | 82 | self.builtins_module = builtins_visitor.result |
paulb@46 | 83 | else: |
paulb@46 | 84 | self.builtins_module = None |
paulb@46 | 85 | self.process_node(visitor.result) |
paulb@45 | 86 | |
paulb@45 | 87 | # Then, process all functions and methods, providing a global namespace. |
paulb@45 | 88 | |
paulb@45 | 89 | self.global_namespace = self.namespace |
paulb@45 | 90 | |
paulb@31 | 91 | for subprogram in visitor.subprograms: |
paulb@45 | 92 | |
paulb@45 | 93 | # Internal subprograms are skipped here and processed specially via |
paulb@45 | 94 | # Invoke nodes. |
paulb@45 | 95 | |
paulb@45 | 96 | if not getattr(subprogram, "acquire_locals", 0): |
paulb@45 | 97 | self.subprograms.append(self.process_node(subprogram)) |
paulb@45 | 98 | |
paulb@45 | 99 | visitor.subprograms = self.subprograms |
paulb@31 | 100 | return visitor |
paulb@31 | 101 | |
paulb@45 | 102 | def process_node(self, node, namespace=None): |
paulb@31 | 103 | |
paulb@31 | 104 | """ |
paulb@43 | 105 | Process a subprogram or module 'node', discovering from attributes on |
paulb@43 | 106 | 'node' any initial locals. Return a modified subprogram or module. |
paulb@31 | 107 | """ |
paulb@31 | 108 | |
paulb@45 | 109 | # Do not process subprograms already being processed. |
paulb@45 | 110 | |
paulb@45 | 111 | if node in self.current_subprograms: |
paulb@45 | 112 | return None |
paulb@45 | 113 | |
paulb@31 | 114 | # Obtain a namespace either based on locals or on a structure. |
paulb@31 | 115 | |
paulb@45 | 116 | structure = structure=getattr(node, "structure", None) |
paulb@45 | 117 | self.namespace = NameOrganiser(structure) |
paulb@45 | 118 | |
paulb@45 | 119 | # Record the current subprogram and namespace. |
paulb@45 | 120 | |
paulb@45 | 121 | self.current_subprograms.append(node) |
paulb@45 | 122 | self.current_namespaces.append(self.namespace) |
paulb@45 | 123 | |
paulb@45 | 124 | # If passed some namespace, merge its contents into this namespace. |
paulb@45 | 125 | |
paulb@45 | 126 | if namespace is not None: |
paulb@45 | 127 | self.namespace.merge_namespace(namespace) |
paulb@31 | 128 | |
paulb@43 | 129 | # NOTE: Check this. |
paulb@43 | 130 | |
paulb@43 | 131 | if hasattr(node, "params"): |
paulb@43 | 132 | for param, default in node.params: |
paulb@43 | 133 | self.namespace.store(param) |
paulb@45 | 134 | if getattr(node, "star", None): |
paulb@45 | 135 | param, default = node.star |
paulb@43 | 136 | self.namespace.store(param) |
paulb@45 | 137 | if getattr(node, "dstar", None): |
paulb@45 | 138 | param, default = node.dstar |
paulb@43 | 139 | self.namespace.store(param) |
paulb@43 | 140 | |
paulb@31 | 141 | # Add namespace details to any structure involved. |
paulb@31 | 142 | |
paulb@31 | 143 | if hasattr(node, "structure") and node.structure is not None: |
paulb@31 | 144 | |
paulb@31 | 145 | # Initialise bases where appropriate. |
paulb@31 | 146 | |
paulb@31 | 147 | if hasattr(node.structure, "bases"): |
paulb@31 | 148 | bases = [] |
paulb@31 | 149 | for base in node.structure.bases: |
paulb@31 | 150 | bases.append(self.dispatch(base)) |
paulb@31 | 151 | node.structure.bases = bases |
paulb@31 | 152 | |
paulb@31 | 153 | # Dispatch to the code itself. |
paulb@31 | 154 | |
paulb@31 | 155 | result = self.dispatch(node) |
paulb@45 | 156 | |
paulb@45 | 157 | # Restore the previous subprogram and namespace. |
paulb@45 | 158 | |
paulb@45 | 159 | self.current_namespaces.pop() |
paulb@45 | 160 | if self.current_namespaces: |
paulb@45 | 161 | self.namespace = self.current_namespaces[-1] |
paulb@45 | 162 | self.current_subprograms.pop() |
paulb@45 | 163 | |
paulb@31 | 164 | return result |
paulb@31 | 165 | |
paulb@31 | 166 | # Visitor methods. |
paulb@31 | 167 | |
paulb@31 | 168 | def default(self, node): |
paulb@31 | 169 | |
paulb@31 | 170 | """ |
paulb@31 | 171 | Process the given 'node', given that it does not have a specific |
paulb@31 | 172 | handler. |
paulb@31 | 173 | """ |
paulb@31 | 174 | |
paulb@31 | 175 | for attr in ("args",): |
paulb@31 | 176 | value = getattr(node, attr, None) |
paulb@31 | 177 | if value is not None: |
paulb@31 | 178 | setattr(node, attr, self.dispatches(value)) |
paulb@36 | 179 | for attr in ("expr", "lvalue", "test", "star", "dstar"): |
paulb@31 | 180 | value = getattr(node, attr, None) |
paulb@31 | 181 | if value is not None: |
paulb@31 | 182 | setattr(node, attr, self.dispatch(value)) |
paulb@36 | 183 | for attr in ("body", "else_", "handler", "finally_", "code", "choices"): |
paulb@31 | 184 | value = getattr(node, attr, None) |
paulb@31 | 185 | if value is not None: |
paulb@31 | 186 | setattr(node, attr, self.dispatches(value)) |
paulb@31 | 187 | return node |
paulb@31 | 188 | |
paulb@31 | 189 | def dispatch(self, node, *args): |
paulb@31 | 190 | return Visitor.dispatch(self, node, *args) |
paulb@31 | 191 | |
paulb@31 | 192 | def visitGlobal(self, global_): |
paulb@31 | 193 | for name in global_.names: |
paulb@31 | 194 | self.namespace.make_global(name) |
paulb@31 | 195 | return global_ |
paulb@31 | 196 | |
paulb@31 | 197 | def visitLoadName(self, loadname): |
paulb@45 | 198 | |
paulb@45 | 199 | "Transform the 'loadname' node to a specific, scope-sensitive node." |
paulb@45 | 200 | |
paulb@46 | 201 | scope = self.namespace.find(loadname.name) |
paulb@45 | 202 | |
paulb@45 | 203 | # For structure namespaces, load an attribute. |
paulb@45 | 204 | |
paulb@31 | 205 | if scope == "structure": |
paulb@31 | 206 | result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.namespace.structure), name=loadname.name)) |
paulb@45 | 207 | |
paulb@45 | 208 | # For global accesses (ie. those outside the local namespace)... |
paulb@45 | 209 | |
paulb@31 | 210 | elif scope == "global": |
paulb@45 | 211 | |
paulb@45 | 212 | # Where a distinct global namespace exists, examine it. |
paulb@45 | 213 | |
paulb@45 | 214 | if self.global_namespace is not None: |
paulb@46 | 215 | scope = self.global_namespace.find(loadname.name) |
paulb@45 | 216 | |
paulb@45 | 217 | # Where the name is outside the global namespace, it must be a |
paulb@45 | 218 | # built-in. |
paulb@45 | 219 | |
paulb@45 | 220 | if scope == "global": |
paulb@46 | 221 | result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.builtins_module), name=loadname.name)) |
paulb@45 | 222 | |
paulb@45 | 223 | # Otherwise, it is within the global namespace and must be a |
paulb@45 | 224 | # global. |
paulb@45 | 225 | |
paulb@45 | 226 | else: |
paulb@46 | 227 | result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.module), name=loadname.name)) |
paulb@45 | 228 | |
paulb@45 | 229 | # Where no global namespace exists, we are at the module level and |
paulb@45 | 230 | # must be accessing a built-in. |
paulb@45 | 231 | |
paulb@45 | 232 | else: |
paulb@46 | 233 | result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.builtins_module), name=loadname.name)) |
paulb@45 | 234 | |
paulb@45 | 235 | # For local accesses... |
paulb@45 | 236 | |
paulb@31 | 237 | else: |
paulb@45 | 238 | |
paulb@45 | 239 | # Where a distinct global namespace exists, it must be a local. |
paulb@45 | 240 | |
paulb@45 | 241 | if self.global_namespace is not None: |
paulb@45 | 242 | result = loadname |
paulb@45 | 243 | |
paulb@45 | 244 | # Otherwise, we must be accessing a global (which is local at the |
paulb@45 | 245 | # module level). |
paulb@45 | 246 | |
paulb@45 | 247 | else: |
paulb@46 | 248 | result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.module), name=loadname.name)) |
paulb@45 | 249 | |
paulb@31 | 250 | return result |
paulb@31 | 251 | |
paulb@31 | 252 | def visitStoreName(self, storename): |
paulb@45 | 253 | |
paulb@45 | 254 | "Transform the 'storename' node to a specific, scope-sensitive node." |
paulb@45 | 255 | |
paulb@46 | 256 | scope = self.namespace.find(storename.name) |
paulb@45 | 257 | |
paulb@45 | 258 | # For structure namespaces, store an attribute. |
paulb@45 | 259 | |
paulb@31 | 260 | if scope == "structure": |
paulb@31 | 261 | return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.namespace.structure), name=storename.name, expr=storename.expr)) |
paulb@45 | 262 | |
paulb@45 | 263 | # Where the name is outside the local namespace, disallow any built-in |
paulb@45 | 264 | # assignment and store the name globally. |
paulb@45 | 265 | |
paulb@31 | 266 | elif scope == "global": |
paulb@46 | 267 | return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.module), name=storename.name, expr=storename.expr)) |
paulb@45 | 268 | |
paulb@45 | 269 | # For local namespace accesses... |
paulb@45 | 270 | |
paulb@31 | 271 | else: |
paulb@31 | 272 | self.namespace.store(storename.name) |
paulb@45 | 273 | |
paulb@45 | 274 | # If a distinct global namespace exists, it must be a local access. |
paulb@45 | 275 | |
paulb@45 | 276 | if self.global_namespace is not None: |
paulb@45 | 277 | return storename |
paulb@45 | 278 | |
paulb@45 | 279 | # Otherwise, the name is being set at the module level and is |
paulb@45 | 280 | # considered global. |
paulb@45 | 281 | |
paulb@45 | 282 | else: |
paulb@46 | 283 | return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.module), name=storename.name, expr=storename.expr)) |
paulb@45 | 284 | |
paulb@45 | 285 | def visitInvoke(self, invoke): |
paulb@45 | 286 | |
paulb@45 | 287 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@45 | 288 | |
paulb@45 | 289 | # The special case of internal subprogram invocation is addressed by |
paulb@45 | 290 | # propagating namespace information to the subprogram and processing it. |
paulb@45 | 291 | |
paulb@45 | 292 | if getattr(invoke, "same_frame", 0): |
paulb@45 | 293 | subprogram = self.process_node(invoke.expr.ref, self.namespace) |
paulb@45 | 294 | if subprogram is not None: |
paulb@45 | 295 | self.subprograms.append(subprogram) |
paulb@45 | 296 | return invoke |
paulb@45 | 297 | else: |
paulb@45 | 298 | return self.default(invoke) |
paulb@31 | 299 | |
paulb@46 | 300 | class ScopeMismatch(Exception): |
paulb@46 | 301 | pass |
paulb@46 | 302 | |
paulb@34 | 303 | class NameOrganiser: |
paulb@32 | 304 | |
paulb@32 | 305 | """ |
paulb@32 | 306 | A local namespace which may either relate to a genuine set of function |
paulb@32 | 307 | locals or the initialisation of a structure. |
paulb@32 | 308 | """ |
paulb@32 | 309 | |
paulb@32 | 310 | def __init__(self, structure=None): |
paulb@46 | 311 | |
paulb@46 | 312 | "Initialise the namespace with an optional 'structure'." |
paulb@46 | 313 | |
paulb@32 | 314 | self.structure = structure |
paulb@32 | 315 | if structure is not None: |
paulb@32 | 316 | self.local = "structure" |
paulb@32 | 317 | else: |
paulb@32 | 318 | self.local = "local" |
paulb@46 | 319 | |
paulb@46 | 320 | # Names may be self.local or "global". |
paulb@46 | 321 | |
paulb@32 | 322 | self.names = {} |
paulb@32 | 323 | |
paulb@32 | 324 | def make_global(self, name): |
paulb@46 | 325 | if not self.names.has_key(name): |
paulb@46 | 326 | self.names[name] = "global" |
paulb@46 | 327 | elif self.names[name] == self.local: |
paulb@46 | 328 | raise ScopeMismatch, "Name '%s' already considered as %s." % (name, self.local) |
paulb@32 | 329 | |
paulb@46 | 330 | def find(self, name): |
paulb@46 | 331 | return self.names.get(name, "global") |
paulb@32 | 332 | |
paulb@45 | 333 | def store(self, name): |
paulb@46 | 334 | if self.names.get(name) != "global": |
paulb@46 | 335 | self.names[name] = self.local |
paulb@32 | 336 | else: |
paulb@46 | 337 | raise ScopeMismatch, "Name '%s' already considered as global." % name |
paulb@32 | 338 | |
paulb@46 | 339 | def merge(self, name, scope): |
paulb@46 | 340 | if self.names.get(name) in (None, scope): |
paulb@46 | 341 | self.names[name] = scope |
paulb@32 | 342 | else: |
paulb@46 | 343 | raise ScopeMismatch, "Name '%s' already considered as %s." % (name, self.names[name]) |
paulb@45 | 344 | |
paulb@45 | 345 | def merge_namespace(self, namespace): |
paulb@46 | 346 | self.merge_items(namespace.names.items()) |
paulb@45 | 347 | |
paulb@46 | 348 | def merge_items(self, items): |
paulb@46 | 349 | for name, scope in items: |
paulb@46 | 350 | self.merge(name, scope) |
paulb@45 | 351 | |
paulb@43 | 352 | def __repr__(self): |
paulb@43 | 353 | return repr(self.names) |
paulb@43 | 354 | |
paulb@31 | 355 | # vim: tabstop=4 expandtab shiftwidth=4 |