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@31 | 24 | """ |
paulb@31 | 25 | |
paulb@31 | 26 | from simplified import * |
paulb@31 | 27 | |
paulb@31 | 28 | # Fixing of name-related operations. |
paulb@31 | 29 | |
paulb@31 | 30 | class Fixer(Visitor): |
paulb@31 | 31 | |
paulb@31 | 32 | """ |
paulb@31 | 33 | The name fixer which traverses the program nodes, typically depth-first, |
paulb@31 | 34 | and maintains a record of name usage in the different namespaces. As a |
paulb@31 | 35 | consequence of various observations, some parts of the program node tree are |
paulb@31 | 36 | modified with different operations employed to those originally defined. |
paulb@31 | 37 | """ |
paulb@31 | 38 | |
paulb@31 | 39 | def __init__(self): |
paulb@45 | 40 | |
paulb@45 | 41 | "Initialise the name fixer." |
paulb@45 | 42 | |
paulb@31 | 43 | Visitor.__init__(self) |
paulb@31 | 44 | |
paulb@31 | 45 | # Satisfy visitor issues. |
paulb@31 | 46 | |
paulb@31 | 47 | self.visitor = self |
paulb@31 | 48 | |
paulb@45 | 49 | def process(self, visitor): |
paulb@45 | 50 | |
paulb@45 | 51 | "Process the resources of the given 'visitor'." |
paulb@45 | 52 | |
paulb@45 | 53 | self.subprograms = [] |
paulb@45 | 54 | self.current_subprograms = [] |
paulb@45 | 55 | self.current_namespaces = [] |
paulb@45 | 56 | |
paulb@45 | 57 | # First, process the top-level code, finding out which names are |
paulb@45 | 58 | # defined at that level. |
paulb@45 | 59 | |
paulb@45 | 60 | self.global_namespace = None |
paulb@45 | 61 | visitor.result = self.process_node(visitor.result) |
paulb@45 | 62 | |
paulb@45 | 63 | # Then, process all functions and methods, providing a global namespace. |
paulb@45 | 64 | |
paulb@45 | 65 | self.global_namespace = self.namespace |
paulb@45 | 66 | |
paulb@31 | 67 | for subprogram in visitor.subprograms: |
paulb@45 | 68 | |
paulb@45 | 69 | # Internal subprograms are skipped here and processed specially via |
paulb@45 | 70 | # Invoke nodes. |
paulb@45 | 71 | |
paulb@45 | 72 | if not getattr(subprogram, "acquire_locals", 0): |
paulb@45 | 73 | self.subprograms.append(self.process_node(subprogram)) |
paulb@45 | 74 | |
paulb@45 | 75 | visitor.subprograms = self.subprograms |
paulb@31 | 76 | return visitor |
paulb@31 | 77 | |
paulb@45 | 78 | def process_node(self, node, namespace=None): |
paulb@31 | 79 | |
paulb@31 | 80 | """ |
paulb@43 | 81 | Process a subprogram or module 'node', discovering from attributes on |
paulb@43 | 82 | 'node' any initial locals. Return a modified subprogram or module. |
paulb@31 | 83 | """ |
paulb@31 | 84 | |
paulb@45 | 85 | # Do not process subprograms already being processed. |
paulb@45 | 86 | |
paulb@45 | 87 | if node in self.current_subprograms: |
paulb@45 | 88 | return None |
paulb@45 | 89 | |
paulb@31 | 90 | # Obtain a namespace either based on locals or on a structure. |
paulb@31 | 91 | |
paulb@45 | 92 | structure = structure=getattr(node, "structure", None) |
paulb@45 | 93 | self.namespace = NameOrganiser(structure) |
paulb@45 | 94 | |
paulb@45 | 95 | # Record the current subprogram and namespace. |
paulb@45 | 96 | |
paulb@45 | 97 | self.current_subprograms.append(node) |
paulb@45 | 98 | self.current_namespaces.append(self.namespace) |
paulb@45 | 99 | |
paulb@45 | 100 | # If passed some namespace, merge its contents into this namespace. |
paulb@45 | 101 | |
paulb@45 | 102 | if namespace is not None: |
paulb@45 | 103 | self.namespace.merge_namespace(namespace) |
paulb@31 | 104 | |
paulb@43 | 105 | # NOTE: Check this. |
paulb@43 | 106 | |
paulb@43 | 107 | if hasattr(node, "params"): |
paulb@43 | 108 | for param, default in node.params: |
paulb@43 | 109 | self.namespace.store(param) |
paulb@45 | 110 | if getattr(node, "star", None): |
paulb@45 | 111 | param, default = node.star |
paulb@43 | 112 | self.namespace.store(param) |
paulb@45 | 113 | if getattr(node, "dstar", None): |
paulb@45 | 114 | param, default = node.dstar |
paulb@43 | 115 | self.namespace.store(param) |
paulb@43 | 116 | |
paulb@31 | 117 | # Add namespace details to any structure involved. |
paulb@31 | 118 | |
paulb@31 | 119 | if hasattr(node, "structure") and node.structure is not None: |
paulb@31 | 120 | |
paulb@31 | 121 | # Initialise bases where appropriate. |
paulb@31 | 122 | |
paulb@31 | 123 | if hasattr(node.structure, "bases"): |
paulb@31 | 124 | bases = [] |
paulb@31 | 125 | for base in node.structure.bases: |
paulb@31 | 126 | bases.append(self.dispatch(base)) |
paulb@31 | 127 | node.structure.bases = bases |
paulb@31 | 128 | |
paulb@31 | 129 | # Dispatch to the code itself. |
paulb@31 | 130 | |
paulb@31 | 131 | result = self.dispatch(node) |
paulb@45 | 132 | |
paulb@45 | 133 | # Restore the previous subprogram and namespace. |
paulb@45 | 134 | |
paulb@45 | 135 | self.current_namespaces.pop() |
paulb@45 | 136 | if self.current_namespaces: |
paulb@45 | 137 | self.namespace = self.current_namespaces[-1] |
paulb@45 | 138 | self.current_subprograms.pop() |
paulb@45 | 139 | |
paulb@31 | 140 | return result |
paulb@31 | 141 | |
paulb@31 | 142 | # Visitor methods. |
paulb@31 | 143 | |
paulb@31 | 144 | def default(self, node): |
paulb@31 | 145 | |
paulb@31 | 146 | """ |
paulb@31 | 147 | Process the given 'node', given that it does not have a specific |
paulb@31 | 148 | handler. |
paulb@31 | 149 | """ |
paulb@31 | 150 | |
paulb@31 | 151 | for attr in ("args",): |
paulb@31 | 152 | value = getattr(node, attr, None) |
paulb@31 | 153 | if value is not None: |
paulb@31 | 154 | setattr(node, attr, self.dispatches(value)) |
paulb@36 | 155 | for attr in ("expr", "lvalue", "test", "star", "dstar"): |
paulb@31 | 156 | value = getattr(node, attr, None) |
paulb@31 | 157 | if value is not None: |
paulb@31 | 158 | setattr(node, attr, self.dispatch(value)) |
paulb@36 | 159 | for attr in ("body", "else_", "handler", "finally_", "code", "choices"): |
paulb@31 | 160 | value = getattr(node, attr, None) |
paulb@31 | 161 | if value is not None: |
paulb@31 | 162 | setattr(node, attr, self.dispatches(value)) |
paulb@31 | 163 | return node |
paulb@31 | 164 | |
paulb@31 | 165 | def dispatch(self, node, *args): |
paulb@31 | 166 | return Visitor.dispatch(self, node, *args) |
paulb@31 | 167 | |
paulb@31 | 168 | def visitGlobal(self, global_): |
paulb@31 | 169 | for name in global_.names: |
paulb@31 | 170 | self.namespace.make_global(name) |
paulb@31 | 171 | return global_ |
paulb@31 | 172 | |
paulb@31 | 173 | def visitLoadName(self, loadname): |
paulb@45 | 174 | |
paulb@45 | 175 | "Transform the 'loadname' node to a specific, scope-sensitive node." |
paulb@45 | 176 | |
paulb@31 | 177 | scope = self.namespace.find_for_load(loadname.name) |
paulb@45 | 178 | |
paulb@45 | 179 | # For structure namespaces, load an attribute. |
paulb@45 | 180 | |
paulb@31 | 181 | if scope == "structure": |
paulb@31 | 182 | result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.namespace.structure), name=loadname.name)) |
paulb@45 | 183 | |
paulb@45 | 184 | # For global accesses (ie. those outside the local namespace)... |
paulb@45 | 185 | |
paulb@31 | 186 | elif scope == "global": |
paulb@45 | 187 | |
paulb@45 | 188 | # Where a distinct global namespace exists, examine it. |
paulb@45 | 189 | |
paulb@45 | 190 | if self.global_namespace is not None: |
paulb@45 | 191 | scope = self.global_namespace.find_for_load(loadname.name) |
paulb@45 | 192 | |
paulb@45 | 193 | # Where the name is outside the global namespace, it must be a |
paulb@45 | 194 | # built-in. |
paulb@45 | 195 | |
paulb@45 | 196 | if scope == "global": |
paulb@45 | 197 | result = self.dispatch(LoadBuiltin(name=loadname.name)) |
paulb@45 | 198 | |
paulb@45 | 199 | # Otherwise, it is within the global namespace and must be a |
paulb@45 | 200 | # global. |
paulb@45 | 201 | |
paulb@45 | 202 | else: |
paulb@45 | 203 | result = self.dispatch(LoadGlobal(name=loadname.name)) |
paulb@45 | 204 | |
paulb@45 | 205 | # Where no global namespace exists, we are at the module level and |
paulb@45 | 206 | # must be accessing a built-in. |
paulb@45 | 207 | |
paulb@45 | 208 | else: |
paulb@45 | 209 | result = self.dispatch(LoadBuiltin(name=loadname.name)) |
paulb@45 | 210 | |
paulb@45 | 211 | # For local accesses... |
paulb@45 | 212 | |
paulb@31 | 213 | else: |
paulb@45 | 214 | |
paulb@45 | 215 | # Where a distinct global namespace exists, it must be a local. |
paulb@45 | 216 | |
paulb@45 | 217 | if self.global_namespace is not None: |
paulb@45 | 218 | result = loadname |
paulb@45 | 219 | |
paulb@45 | 220 | # Otherwise, we must be accessing a global (which is local at the |
paulb@45 | 221 | # module level). |
paulb@45 | 222 | |
paulb@45 | 223 | else: |
paulb@45 | 224 | result = self.dispatch(LoadGlobal(name=loadname.name)) |
paulb@45 | 225 | |
paulb@31 | 226 | return result |
paulb@31 | 227 | |
paulb@31 | 228 | def visitStoreName(self, storename): |
paulb@45 | 229 | |
paulb@45 | 230 | "Transform the 'storename' node to a specific, scope-sensitive node." |
paulb@45 | 231 | |
paulb@31 | 232 | scope = self.namespace.find_for_store(storename.name) |
paulb@45 | 233 | |
paulb@45 | 234 | # For structure namespaces, store an attribute. |
paulb@45 | 235 | |
paulb@31 | 236 | if scope == "structure": |
paulb@31 | 237 | return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.namespace.structure), name=storename.name, expr=storename.expr)) |
paulb@45 | 238 | |
paulb@45 | 239 | # Where the name is outside the local namespace, disallow any built-in |
paulb@45 | 240 | # assignment and store the name globally. |
paulb@45 | 241 | |
paulb@31 | 242 | elif scope == "global": |
paulb@31 | 243 | return self.dispatch(StoreGlobal(name=storename.name, expr=storename.expr)) |
paulb@45 | 244 | |
paulb@45 | 245 | # For local namespace accesses... |
paulb@45 | 246 | |
paulb@31 | 247 | else: |
paulb@31 | 248 | self.namespace.store(storename.name) |
paulb@45 | 249 | |
paulb@45 | 250 | # If a distinct global namespace exists, it must be a local access. |
paulb@45 | 251 | |
paulb@45 | 252 | if self.global_namespace is not None: |
paulb@45 | 253 | return storename |
paulb@45 | 254 | |
paulb@45 | 255 | # Otherwise, the name is being set at the module level and is |
paulb@45 | 256 | # considered global. |
paulb@45 | 257 | |
paulb@45 | 258 | else: |
paulb@45 | 259 | return self.dispatch(StoreGlobal(name=storename.name, expr=storename.expr)) |
paulb@45 | 260 | |
paulb@45 | 261 | def visitInvoke(self, invoke): |
paulb@45 | 262 | |
paulb@45 | 263 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@45 | 264 | |
paulb@45 | 265 | # The special case of internal subprogram invocation is addressed by |
paulb@45 | 266 | # propagating namespace information to the subprogram and processing it. |
paulb@45 | 267 | |
paulb@45 | 268 | if getattr(invoke, "same_frame", 0): |
paulb@45 | 269 | subprogram = self.process_node(invoke.expr.ref, self.namespace) |
paulb@45 | 270 | if subprogram is not None: |
paulb@45 | 271 | self.subprograms.append(subprogram) |
paulb@45 | 272 | return invoke |
paulb@45 | 273 | else: |
paulb@45 | 274 | return self.default(invoke) |
paulb@31 | 275 | |
paulb@34 | 276 | class NameOrganiser: |
paulb@32 | 277 | |
paulb@32 | 278 | """ |
paulb@32 | 279 | A local namespace which may either relate to a genuine set of function |
paulb@32 | 280 | locals or the initialisation of a structure. |
paulb@32 | 281 | """ |
paulb@32 | 282 | |
paulb@32 | 283 | def __init__(self, structure=None): |
paulb@32 | 284 | self.structure = structure |
paulb@32 | 285 | if structure is not None: |
paulb@32 | 286 | self.local = "structure" |
paulb@32 | 287 | else: |
paulb@32 | 288 | self.local = "local" |
paulb@32 | 289 | self.names = {} |
paulb@32 | 290 | self.not_local = [] |
paulb@32 | 291 | |
paulb@32 | 292 | def make_global(self, name): |
paulb@32 | 293 | if name not in self.not_local: |
paulb@32 | 294 | self.not_local.append(name) |
paulb@32 | 295 | |
paulb@32 | 296 | def find_for_store(self, name): |
paulb@32 | 297 | if name not in self.not_local: |
paulb@32 | 298 | return self.local |
paulb@32 | 299 | else: |
paulb@32 | 300 | return "global" |
paulb@32 | 301 | |
paulb@32 | 302 | def find_for_load(self, name): |
paulb@32 | 303 | if name not in self.not_local and self.names.has_key(name): |
paulb@32 | 304 | return self.local |
paulb@32 | 305 | else: |
paulb@32 | 306 | return "global" |
paulb@32 | 307 | |
paulb@45 | 308 | def store(self, name): |
paulb@32 | 309 | if name not in self.not_local: |
paulb@45 | 310 | self.names[name] = None |
paulb@32 | 311 | else: |
paulb@32 | 312 | raise KeyError, name |
paulb@32 | 313 | |
paulb@32 | 314 | def load(self, name): |
paulb@32 | 315 | if name in self.not_local or not self.names.has_key(name): |
paulb@32 | 316 | raise KeyError, name |
paulb@32 | 317 | else: |
paulb@32 | 318 | return self.names[name] |
paulb@32 | 319 | |
paulb@45 | 320 | def merge(self, name): |
paulb@45 | 321 | if not self.names.has_key(name): |
paulb@45 | 322 | self.names[name] = None |
paulb@45 | 323 | |
paulb@45 | 324 | def merge_namespace(self, namespace): |
paulb@45 | 325 | self.merge_names(namespace.names.keys()) |
paulb@45 | 326 | |
paulb@45 | 327 | def merge_names(self, names): |
paulb@45 | 328 | for name in names: |
paulb@45 | 329 | self.merge(name) |
paulb@45 | 330 | |
paulb@43 | 331 | def __repr__(self): |
paulb@43 | 332 | return repr(self.names) |
paulb@43 | 333 | |
paulb@31 | 334 | # vim: tabstop=4 expandtab shiftwidth=4 |