paulb@19 | 1 | #!/usr/bin/env python |
paulb@19 | 2 | |
paulb@19 | 3 | """ |
paulb@24 | 4 | Annotate simplified AST structures. The code in this module operates upon nodes |
paulb@24 | 5 | which are produced when simplifying AST node trees originating from the compiler |
paulb@24 | 6 | module. |
paulb@19 | 7 | |
paulb@19 | 8 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@19 | 9 | |
paulb@19 | 10 | This software is free software; you can redistribute it and/or |
paulb@19 | 11 | modify it under the terms of the GNU General Public License as |
paulb@19 | 12 | published by the Free Software Foundation; either version 2 of |
paulb@19 | 13 | the License, or (at your option) any later version. |
paulb@19 | 14 | |
paulb@19 | 15 | This software is distributed in the hope that it will be useful, |
paulb@19 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@19 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@19 | 18 | GNU General Public License for more details. |
paulb@19 | 19 | |
paulb@19 | 20 | You should have received a copy of the GNU General Public |
paulb@19 | 21 | License along with this library; see the file LICENCE.txt |
paulb@19 | 22 | If not, write to the Free Software Foundation, Inc., |
paulb@19 | 23 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@19 | 24 | """ |
paulb@19 | 25 | |
paulb@19 | 26 | from simplified import * |
paulb@19 | 27 | import compiler |
paulb@19 | 28 | |
paulb@24 | 29 | class System: |
paulb@24 | 30 | |
paulb@24 | 31 | "A class maintaining the state of the annotation system." |
paulb@24 | 32 | |
paulb@24 | 33 | def __init__(self): |
paulb@24 | 34 | self.count = 0 |
paulb@24 | 35 | def init(self, node): |
paulb@24 | 36 | if not hasattr(node, "types"): |
paulb@24 | 37 | node.types = [] |
paulb@24 | 38 | def annotate(self, node, types): |
paulb@24 | 39 | self.init(node) |
paulb@24 | 40 | for type in types: |
paulb@24 | 41 | if type not in node.types: |
paulb@24 | 42 | node.types.append(type) |
paulb@24 | 43 | self.count += 1 |
paulb@24 | 44 | |
paulb@24 | 45 | system = System() |
paulb@24 | 46 | |
paulb@24 | 47 | # Namespaces and related abstractions. |
paulb@24 | 48 | |
paulb@24 | 49 | class Attribute: |
paulb@24 | 50 | |
paulb@24 | 51 | """ |
paulb@24 | 52 | An attribute abstraction, indicating the type of the attribute along with |
paulb@24 | 53 | its context or origin. |
paulb@24 | 54 | """ |
paulb@24 | 55 | |
paulb@24 | 56 | def __init__(self, context, type): |
paulb@24 | 57 | self.context = context |
paulb@24 | 58 | self.type = type |
paulb@24 | 59 | |
paulb@24 | 60 | def __eq__(self, other): |
paulb@24 | 61 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@24 | 62 | |
paulb@24 | 63 | # Annotation. |
paulb@19 | 64 | |
paulb@20 | 65 | class Annotator(Visitor): |
paulb@24 | 66 | |
paulb@24 | 67 | """ |
paulb@24 | 68 | The type annotator which traverses the program nodes, typically depth-first, |
paulb@24 | 69 | and maintains a record of the current set of types applying to the currently |
paulb@24 | 70 | considered operation. Such types are also recorded on the nodes, and a |
paulb@24 | 71 | special "system" record is maintained to monitor the level of annotation |
paulb@24 | 72 | activity with a view to recognising when no more annotations are possible. |
paulb@24 | 73 | """ |
paulb@24 | 74 | |
paulb@19 | 75 | def __init__(self): |
paulb@19 | 76 | Visitor.__init__(self) |
paulb@24 | 77 | self.system = system |
paulb@19 | 78 | self.types = None |
paulb@19 | 79 | self.temp = {} |
paulb@19 | 80 | |
paulb@30 | 81 | # Satisfy visitor issues. |
paulb@30 | 82 | |
paulb@30 | 83 | self.visitor = self |
paulb@30 | 84 | |
paulb@24 | 85 | def process(self, node, locals=None, globals=None): |
paulb@24 | 86 | |
paulb@24 | 87 | """ |
paulb@24 | 88 | Process a subprogram or module 'node', indicating any initial 'locals' |
paulb@24 | 89 | and 'globals' if either are defined. Return an annotated subprogram or |
paulb@24 | 90 | module. Note that this method may mutate nodes in the original program. |
paulb@24 | 91 | """ |
paulb@24 | 92 | |
paulb@25 | 93 | # Obtain a namespace either based on locals or on a structure. |
paulb@20 | 94 | |
paulb@25 | 95 | self.namespace = Namespace(structure=getattr(node, "structure", None)) |
paulb@24 | 96 | if locals is not None: |
paulb@24 | 97 | self.namespace.merge(locals) |
paulb@24 | 98 | |
paulb@25 | 99 | # Determine the global namespace. |
paulb@25 | 100 | |
paulb@24 | 101 | self.global_namespace = globals or self.namespace # NOTE: Improve this. |
paulb@25 | 102 | node.namespace = self.namespace |
paulb@21 | 103 | |
paulb@30 | 104 | # Remember return values. |
paulb@30 | 105 | |
paulb@30 | 106 | self.returns = [] |
paulb@30 | 107 | |
paulb@25 | 108 | # Add namespace details to any structure involved. |
paulb@25 | 109 | |
paulb@25 | 110 | if hasattr(node, "structure") and node.structure is not None: |
paulb@19 | 111 | node.structure.namespace = self.namespace |
paulb@25 | 112 | |
paulb@30 | 113 | # Initialise bases where appropriate. |
paulb@19 | 114 | |
paulb@30 | 115 | if hasattr(node.structure, "bases"): |
paulb@30 | 116 | base_refs = [] |
paulb@30 | 117 | for base in node.structure.bases: |
paulb@30 | 118 | self.dispatch(base) |
paulb@30 | 119 | base_refs.append(self.types) |
paulb@30 | 120 | node.structure.base_refs = base_refs |
paulb@30 | 121 | |
paulb@30 | 122 | # Dispatch to the code itself. |
paulb@30 | 123 | |
paulb@20 | 124 | result = self.dispatch(node) |
paulb@20 | 125 | |
paulb@20 | 126 | return result |
paulb@20 | 127 | |
paulb@24 | 128 | def annotate(self, node): |
paulb@24 | 129 | |
paulb@24 | 130 | "Annotate the given 'node' in the system." |
paulb@24 | 131 | |
paulb@24 | 132 | self.system.annotate(node, self.types) |
paulb@24 | 133 | |
paulb@25 | 134 | # Visitor methods. |
paulb@25 | 135 | |
paulb@19 | 136 | def default(self, node): |
paulb@24 | 137 | |
paulb@24 | 138 | """ |
paulb@24 | 139 | Process the given 'node', given that it does not have a specific |
paulb@24 | 140 | handler. |
paulb@24 | 141 | """ |
paulb@24 | 142 | |
paulb@25 | 143 | for attr in ("expr", "lvalue", "test", "handler"): |
paulb@19 | 144 | value = getattr(node, attr, None) |
paulb@19 | 145 | if value is not None: |
paulb@20 | 146 | setattr(node, attr, self.dispatch(value)) |
paulb@19 | 147 | for attr in ("body", "else_", "finally_", "code"): |
paulb@19 | 148 | value = getattr(node, attr, None) |
paulb@19 | 149 | if value is not None: |
paulb@20 | 150 | setattr(node, attr, self.dispatches(value)) |
paulb@20 | 151 | return node |
paulb@20 | 152 | |
paulb@20 | 153 | def dispatch(self, node, *args): |
paulb@20 | 154 | return Visitor.dispatch(self, node, *args) |
paulb@19 | 155 | |
paulb@19 | 156 | def visitGlobal(self, global_): |
paulb@19 | 157 | for name in global_.names: |
paulb@21 | 158 | self.namespace.make_global(name) |
paulb@20 | 159 | return global_ |
paulb@19 | 160 | |
paulb@19 | 161 | def visitLoadRef(self, loadref): |
paulb@19 | 162 | self.types = [loadref.ref] |
paulb@24 | 163 | self.annotate(loadref) |
paulb@20 | 164 | return loadref |
paulb@19 | 165 | |
paulb@19 | 166 | def visitLoadName(self, loadname): |
paulb@31 | 167 | self.types = self.namespace.load(loadname.name) |
paulb@31 | 168 | result = loadname |
paulb@31 | 169 | self.annotate(result) |
paulb@24 | 170 | return result |
paulb@19 | 171 | |
paulb@19 | 172 | def visitStoreName(self, storename): |
paulb@31 | 173 | storename.expr = self.dispatch(storename.expr) |
paulb@31 | 174 | self.namespace.store(storename.name, self.types) |
paulb@31 | 175 | return storename |
paulb@19 | 176 | |
paulb@21 | 177 | def visitLoadGlobal(self, loadglobal): |
paulb@21 | 178 | self.types = self.global_namespace.load(loadglobal.name) |
paulb@24 | 179 | self.annotate(loadglobal) |
paulb@21 | 180 | return loadglobal |
paulb@21 | 181 | |
paulb@21 | 182 | def visitStoreGlobal(self, storeglobal): |
paulb@21 | 183 | storeglobal.expr = self.dispatch(storeglobal.expr) |
paulb@21 | 184 | |
paulb@21 | 185 | # NOTE: This may always be a merge operation. |
paulb@21 | 186 | |
paulb@21 | 187 | self.global_namespace.store(storeglobal.name, self.types) |
paulb@21 | 188 | return storeglobal |
paulb@21 | 189 | |
paulb@19 | 190 | def visitLoadTemp(self, loadtemp): |
paulb@19 | 191 | index = getattr(loadtemp, "index", None) |
paulb@19 | 192 | self.types = self.temp[index] |
paulb@24 | 193 | self.annotate(loadtemp) |
paulb@20 | 194 | return loadtemp |
paulb@19 | 195 | |
paulb@19 | 196 | def visitStoreTemp(self, storetemp): |
paulb@21 | 197 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@19 | 198 | index = getattr(storetemp, "index", None) |
paulb@19 | 199 | self.temp[index] = self.types |
paulb@20 | 200 | return storetemp |
paulb@19 | 201 | |
paulb@19 | 202 | def visitReleaseTemp(self, releasetemp): |
paulb@19 | 203 | index = getattr(releasetemp, "index", None) |
paulb@19 | 204 | del self.temp[index] |
paulb@20 | 205 | return releasetemp |
paulb@20 | 206 | |
paulb@20 | 207 | def visitLoadAttr(self, loadattr): |
paulb@21 | 208 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@20 | 209 | types = [] |
paulb@20 | 210 | for ref in self.types: |
paulb@24 | 211 | for type in ref.namespace.load(loadattr.name): |
paulb@24 | 212 | types.append(Attribute(ref, type)) |
paulb@20 | 213 | self.types = types |
paulb@24 | 214 | self.annotate(loadattr) |
paulb@20 | 215 | return loadattr |
paulb@20 | 216 | |
paulb@20 | 217 | def visitStoreAttr(self, storeattr): |
paulb@21 | 218 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@20 | 219 | expr = self.types |
paulb@21 | 220 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@20 | 221 | for ref in self.types: |
paulb@20 | 222 | ref.namespace.store(storeattr.name, expr) |
paulb@20 | 223 | return storeattr |
paulb@19 | 224 | |
paulb@30 | 225 | def visitReturn(self, return_): |
paulb@30 | 226 | if hasattr(return_, "expr"): |
paulb@30 | 227 | return_.expr = self.dispatch(return_.expr) |
paulb@30 | 228 | self.returns += self.types |
paulb@30 | 229 | return return_ |
paulb@30 | 230 | |
paulb@24 | 231 | def visitInvoke(self, invoke): |
paulb@24 | 232 | invoke.expr = self.dispatch(invoke.expr) |
paulb@24 | 233 | expr = self.types |
paulb@25 | 234 | |
paulb@25 | 235 | # NOTE: Consider initialiser invocation for classes. |
paulb@25 | 236 | |
paulb@24 | 237 | types = [] |
paulb@24 | 238 | args = [] |
paulb@24 | 239 | |
paulb@24 | 240 | # Get type information for regular arguments. |
paulb@24 | 241 | |
paulb@24 | 242 | for arg in invoke.args: |
paulb@24 | 243 | args.append(self.dispatch(arg)) |
paulb@24 | 244 | types.append(self.types) |
paulb@24 | 245 | |
paulb@24 | 246 | # Get type information for star and dstar arguments. |
paulb@24 | 247 | |
paulb@24 | 248 | if invoke.star is not None: |
paulb@24 | 249 | param, default = invoke.star |
paulb@24 | 250 | default = self.dispatch(default) |
paulb@24 | 251 | invoke.star = param, default |
paulb@24 | 252 | types.append(default.types) |
paulb@24 | 253 | |
paulb@24 | 254 | if invoke.dstar is not None: |
paulb@24 | 255 | param, default = invoke.dstar |
paulb@24 | 256 | default = self.dispatch(default) |
paulb@24 | 257 | invoke.dstar = param, default |
paulb@24 | 258 | types.append(default.types) |
paulb@24 | 259 | |
paulb@24 | 260 | invoke.args = args |
paulb@24 | 261 | invoke.types = expr |
paulb@24 | 262 | |
paulb@24 | 263 | # NOTE: Now locate and invoke the subprogram. |
paulb@24 | 264 | |
paulb@24 | 265 | for subprogram in expr: |
paulb@30 | 266 | |
paulb@30 | 267 | # NOTE: Deal with class invocations by providing instance objects, |
paulb@30 | 268 | # NOTE: and with object invocations by using __call__ methods. |
paulb@30 | 269 | |
paulb@27 | 270 | if hasattr(invoke, "same_frame") and invoke.same_frame: |
paulb@27 | 271 | namespace = self.namespace |
paulb@27 | 272 | else: |
paulb@27 | 273 | items = self.make_items(invoke, subprogram) |
paulb@27 | 274 | namespace = self.make_namespace(items) |
paulb@27 | 275 | |
paulb@25 | 276 | annotator = Annotator() |
paulb@27 | 277 | annotator.process(subprogram, namespace, self.global_namespace) |
paulb@25 | 278 | |
paulb@25 | 279 | # NOTE: Annotate the node with invocation details. |
paulb@30 | 280 | # NOTE: This should really be as part of a table of alternatives. |
paulb@30 | 281 | |
paulb@30 | 282 | if hasattr(subprogram, "returns_value") and subprogram.returns_value: |
paulb@30 | 283 | self.types = annotator.returns |
paulb@30 | 284 | self.annotate(invoke) |
paulb@24 | 285 | |
paulb@24 | 286 | return invoke |
paulb@24 | 287 | |
paulb@25 | 288 | # Utility methods. |
paulb@25 | 289 | |
paulb@24 | 290 | def make_items(self, invocation, subprogram): |
paulb@24 | 291 | # NOTE: Support star and dstar. |
paulb@24 | 292 | args = invocation.args |
paulb@24 | 293 | params = subprogram.params |
paulb@24 | 294 | items = [] |
paulb@24 | 295 | keywords = {} |
paulb@24 | 296 | |
paulb@24 | 297 | # Process the specified arguments. |
paulb@24 | 298 | |
paulb@24 | 299 | for arg in args: |
paulb@24 | 300 | if isinstance(arg, Keyword): |
paulb@24 | 301 | keywords[arg.name] = arg.expr |
paulb@24 | 302 | continue |
paulb@24 | 303 | elif params: |
paulb@24 | 304 | param, default = params[0] |
paulb@24 | 305 | if arg is None: |
paulb@24 | 306 | arg = self.dispatch(default) |
paulb@24 | 307 | else: |
paulb@24 | 308 | raise TypeError, "Invocation has too many arguments." |
paulb@24 | 309 | items.append((param, arg.types)) |
paulb@24 | 310 | params = params[1:] |
paulb@24 | 311 | |
paulb@24 | 312 | # Collect the remaining defaults. |
paulb@24 | 313 | |
paulb@24 | 314 | while params: |
paulb@24 | 315 | param, default = params[0] |
paulb@24 | 316 | if keywords.has_key(param): |
paulb@24 | 317 | arg = keywords[param] |
paulb@24 | 318 | else: |
paulb@24 | 319 | arg = self.dispatch(default) |
paulb@24 | 320 | items.append((param, arg.types)) |
paulb@24 | 321 | params = params[1:] |
paulb@24 | 322 | |
paulb@24 | 323 | # Add star and dstar. |
paulb@24 | 324 | |
paulb@24 | 325 | if invocation.star is not None: |
paulb@24 | 326 | if subprogram.star is not None: |
paulb@24 | 327 | param, default = subprogram.star |
paulb@24 | 328 | items.append((param, invocation.star.types)) |
paulb@24 | 329 | else: |
paulb@24 | 330 | raise TypeError, "Invocation provides unwanted *args." |
paulb@24 | 331 | elif subprogram.star is not None: |
paulb@24 | 332 | param, default = subprogram.star |
paulb@24 | 333 | items.append((param, self.dispatch(default))) |
paulb@24 | 334 | |
paulb@24 | 335 | if invocation.dstar is not None: |
paulb@24 | 336 | if subprogram.dstar is not None: |
paulb@24 | 337 | param, default = subprogram.dstar |
paulb@24 | 338 | items.append((param, invocation.dstar.types)) |
paulb@24 | 339 | else: |
paulb@24 | 340 | raise TypeError, "Invocation provides unwanted **args." |
paulb@24 | 341 | elif subprogram.dstar is not None: |
paulb@24 | 342 | param, default = subprogram.dstar |
paulb@24 | 343 | items.append((param, self.dispatch(default))) |
paulb@24 | 344 | |
paulb@24 | 345 | return items |
paulb@24 | 346 | |
paulb@24 | 347 | def make_namespace(self, items): |
paulb@24 | 348 | namespace = Namespace() |
paulb@24 | 349 | namespace.merge_items(items) |
paulb@24 | 350 | return namespace |
paulb@24 | 351 | |
paulb@19 | 352 | # vim: tabstop=4 expandtab shiftwidth=4 |