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