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 | import compiler |
paulb@31 | 28 | |
paulb@31 | 29 | # Fixing of name-related operations. |
paulb@31 | 30 | |
paulb@31 | 31 | class Fixer(Visitor): |
paulb@31 | 32 | |
paulb@31 | 33 | """ |
paulb@31 | 34 | The name fixer which traverses the program nodes, typically depth-first, |
paulb@31 | 35 | and maintains a record of name usage in the different namespaces. As a |
paulb@31 | 36 | consequence of various observations, some parts of the program node tree are |
paulb@31 | 37 | modified with different operations employed to those originally defined. |
paulb@31 | 38 | """ |
paulb@31 | 39 | |
paulb@31 | 40 | def __init__(self): |
paulb@31 | 41 | Visitor.__init__(self) |
paulb@31 | 42 | |
paulb@31 | 43 | # Satisfy visitor issues. |
paulb@31 | 44 | |
paulb@31 | 45 | self.visitor = self |
paulb@31 | 46 | |
paulb@31 | 47 | def process_all(self, visitor): |
paulb@31 | 48 | subprograms = [] |
paulb@31 | 49 | for subprogram in visitor.subprograms: |
paulb@31 | 50 | subprograms.append(self.process(subprogram)) |
paulb@31 | 51 | visitor.subprograms = subprograms |
paulb@31 | 52 | visitor.result = self.process(visitor.result) |
paulb@31 | 53 | return visitor |
paulb@31 | 54 | |
paulb@31 | 55 | def process(self, node): |
paulb@31 | 56 | |
paulb@31 | 57 | """ |
paulb@31 | 58 | Process a subprogram or module 'node', indicating any initial 'locals' |
paulb@31 | 59 | and 'globals' if either are defined. Return an annotated subprogram or |
paulb@31 | 60 | module. Note that this method may mutate nodes in the original program. |
paulb@31 | 61 | """ |
paulb@31 | 62 | |
paulb@31 | 63 | # Obtain a namespace either based on locals or on a structure. |
paulb@31 | 64 | |
paulb@34 | 65 | self.namespace = NameOrganiser(structure=getattr(node, "structure", None)) |
paulb@31 | 66 | |
paulb@31 | 67 | # Add namespace details to any structure involved. |
paulb@31 | 68 | |
paulb@31 | 69 | if hasattr(node, "structure") and node.structure is not None: |
paulb@31 | 70 | |
paulb@31 | 71 | # Initialise bases where appropriate. |
paulb@31 | 72 | |
paulb@31 | 73 | if hasattr(node.structure, "bases"): |
paulb@31 | 74 | bases = [] |
paulb@31 | 75 | for base in node.structure.bases: |
paulb@31 | 76 | bases.append(self.dispatch(base)) |
paulb@31 | 77 | node.structure.bases = bases |
paulb@31 | 78 | |
paulb@31 | 79 | # Dispatch to the code itself. |
paulb@31 | 80 | |
paulb@31 | 81 | result = self.dispatch(node) |
paulb@31 | 82 | return result |
paulb@31 | 83 | |
paulb@31 | 84 | # Visitor methods. |
paulb@31 | 85 | |
paulb@31 | 86 | def default(self, node): |
paulb@31 | 87 | |
paulb@31 | 88 | """ |
paulb@31 | 89 | Process the given 'node', given that it does not have a specific |
paulb@31 | 90 | handler. |
paulb@31 | 91 | """ |
paulb@31 | 92 | |
paulb@31 | 93 | for attr in ("args",): |
paulb@31 | 94 | value = getattr(node, attr, None) |
paulb@31 | 95 | if value is not None: |
paulb@31 | 96 | setattr(node, attr, self.dispatches(value)) |
paulb@36 | 97 | for attr in ("expr", "lvalue", "test", "star", "dstar"): |
paulb@31 | 98 | value = getattr(node, attr, None) |
paulb@31 | 99 | if value is not None: |
paulb@31 | 100 | setattr(node, attr, self.dispatch(value)) |
paulb@36 | 101 | for attr in ("body", "else_", "handler", "finally_", "code", "choices"): |
paulb@31 | 102 | value = getattr(node, attr, None) |
paulb@31 | 103 | if value is not None: |
paulb@31 | 104 | setattr(node, attr, self.dispatches(value)) |
paulb@31 | 105 | return node |
paulb@31 | 106 | |
paulb@31 | 107 | def dispatch(self, node, *args): |
paulb@31 | 108 | return Visitor.dispatch(self, node, *args) |
paulb@31 | 109 | |
paulb@31 | 110 | def visitGlobal(self, global_): |
paulb@31 | 111 | for name in global_.names: |
paulb@31 | 112 | self.namespace.make_global(name) |
paulb@31 | 113 | return global_ |
paulb@31 | 114 | |
paulb@31 | 115 | def visitLoadName(self, loadname): |
paulb@31 | 116 | scope = self.namespace.find_for_load(loadname.name) |
paulb@31 | 117 | if scope == "structure": |
paulb@31 | 118 | result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.namespace.structure), name=loadname.name)) |
paulb@31 | 119 | elif scope == "global": |
paulb@31 | 120 | result = self.dispatch(LoadGlobal(name=loadname.name)) |
paulb@31 | 121 | else: |
paulb@31 | 122 | result = loadname |
paulb@31 | 123 | return result |
paulb@31 | 124 | |
paulb@31 | 125 | def visitStoreName(self, storename): |
paulb@31 | 126 | scope = self.namespace.find_for_store(storename.name) |
paulb@31 | 127 | if scope == "structure": |
paulb@31 | 128 | return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.namespace.structure), name=storename.name, expr=storename.expr)) |
paulb@31 | 129 | elif scope == "global": |
paulb@31 | 130 | return self.dispatch(StoreGlobal(name=storename.name, expr=storename.expr)) |
paulb@31 | 131 | else: |
paulb@31 | 132 | storename.expr = self.dispatch(storename.expr) |
paulb@31 | 133 | self.namespace.store(storename.name) |
paulb@31 | 134 | return storename |
paulb@31 | 135 | |
paulb@34 | 136 | class NameOrganiser: |
paulb@32 | 137 | |
paulb@32 | 138 | """ |
paulb@32 | 139 | A local namespace which may either relate to a genuine set of function |
paulb@32 | 140 | locals or the initialisation of a structure. |
paulb@32 | 141 | """ |
paulb@32 | 142 | |
paulb@32 | 143 | def __init__(self, structure=None): |
paulb@32 | 144 | self.structure = structure |
paulb@32 | 145 | if structure is not None: |
paulb@32 | 146 | self.local = "structure" |
paulb@32 | 147 | else: |
paulb@32 | 148 | self.local = "local" |
paulb@32 | 149 | self.names = {} |
paulb@32 | 150 | self.not_local = [] |
paulb@32 | 151 | |
paulb@32 | 152 | def make_global(self, name): |
paulb@32 | 153 | if name not in self.not_local: |
paulb@32 | 154 | self.not_local.append(name) |
paulb@32 | 155 | |
paulb@32 | 156 | def find_for_store(self, name): |
paulb@32 | 157 | if name not in self.not_local: |
paulb@32 | 158 | return self.local |
paulb@32 | 159 | else: |
paulb@32 | 160 | return "global" |
paulb@32 | 161 | |
paulb@32 | 162 | def find_for_load(self, name): |
paulb@32 | 163 | if name not in self.not_local and self.names.has_key(name): |
paulb@32 | 164 | return self.local |
paulb@32 | 165 | else: |
paulb@32 | 166 | return "global" |
paulb@32 | 167 | |
paulb@32 | 168 | def store(self, name, types=None): |
paulb@32 | 169 | if name not in self.not_local: |
paulb@32 | 170 | self.names[name] = types |
paulb@32 | 171 | else: |
paulb@32 | 172 | raise KeyError, name |
paulb@32 | 173 | |
paulb@32 | 174 | def load(self, name): |
paulb@32 | 175 | if name in self.not_local or not self.names.has_key(name): |
paulb@32 | 176 | raise KeyError, name |
paulb@32 | 177 | else: |
paulb@32 | 178 | return self.names[name] |
paulb@32 | 179 | |
paulb@31 | 180 | # vim: tabstop=4 expandtab shiftwidth=4 |