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@32 | 49 | class Namespace: |
paulb@32 | 50 | |
paulb@32 | 51 | """ |
paulb@32 | 52 | A local namespace which may either relate to a genuine set of function |
paulb@32 | 53 | locals or the initialisation of a structure. |
paulb@32 | 54 | """ |
paulb@32 | 55 | |
paulb@32 | 56 | def __init__(self, structure=None): |
paulb@32 | 57 | self.structure = structure |
paulb@32 | 58 | self.names = {} |
paulb@32 | 59 | |
paulb@32 | 60 | def store(self, name, types): |
paulb@32 | 61 | self.names[name] = types |
paulb@32 | 62 | |
paulb@32 | 63 | def load(self, name): |
paulb@32 | 64 | return self.names[name] |
paulb@32 | 65 | |
paulb@32 | 66 | def merge(self, name, types): |
paulb@32 | 67 | if not self.names.has_key(name): |
paulb@32 | 68 | self.names[name] = types |
paulb@32 | 69 | else: |
paulb@32 | 70 | existing = self.names[name] |
paulb@32 | 71 | for type in types: |
paulb@32 | 72 | if type not in existing: |
paulb@32 | 73 | existing.append(type) |
paulb@32 | 74 | |
paulb@32 | 75 | def merge_namespace(self, namespace): |
paulb@32 | 76 | self.merge_items(namespace.names.items()) |
paulb@32 | 77 | |
paulb@32 | 78 | def merge_items(self, items): |
paulb@32 | 79 | for name, types in items: |
paulb@32 | 80 | self.merge(name, types) |
paulb@32 | 81 | |
paulb@24 | 82 | class Attribute: |
paulb@24 | 83 | |
paulb@24 | 84 | """ |
paulb@24 | 85 | An attribute abstraction, indicating the type of the attribute along with |
paulb@24 | 86 | its context or origin. |
paulb@24 | 87 | """ |
paulb@24 | 88 | |
paulb@24 | 89 | def __init__(self, context, type): |
paulb@24 | 90 | self.context = context |
paulb@24 | 91 | self.type = type |
paulb@24 | 92 | |
paulb@24 | 93 | def __eq__(self, other): |
paulb@24 | 94 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@24 | 95 | |
paulb@24 | 96 | # Annotation. |
paulb@19 | 97 | |
paulb@20 | 98 | class Annotator(Visitor): |
paulb@24 | 99 | |
paulb@24 | 100 | """ |
paulb@24 | 101 | The type annotator which traverses the program nodes, typically depth-first, |
paulb@24 | 102 | and maintains a record of the current set of types applying to the currently |
paulb@24 | 103 | considered operation. Such types are also recorded on the nodes, and a |
paulb@24 | 104 | special "system" record is maintained to monitor the level of annotation |
paulb@24 | 105 | activity with a view to recognising when no more annotations are possible. |
paulb@24 | 106 | """ |
paulb@24 | 107 | |
paulb@19 | 108 | def __init__(self): |
paulb@19 | 109 | Visitor.__init__(self) |
paulb@24 | 110 | self.system = system |
paulb@19 | 111 | self.types = None |
paulb@19 | 112 | self.temp = {} |
paulb@19 | 113 | |
paulb@30 | 114 | # Satisfy visitor issues. |
paulb@30 | 115 | |
paulb@30 | 116 | self.visitor = self |
paulb@30 | 117 | |
paulb@24 | 118 | def process(self, node, locals=None, globals=None): |
paulb@24 | 119 | |
paulb@24 | 120 | """ |
paulb@24 | 121 | Process a subprogram or module 'node', indicating any initial 'locals' |
paulb@24 | 122 | and 'globals' if either are defined. Return an annotated subprogram or |
paulb@24 | 123 | module. Note that this method may mutate nodes in the original program. |
paulb@24 | 124 | """ |
paulb@24 | 125 | |
paulb@25 | 126 | # Obtain a namespace either based on locals or on a structure. |
paulb@20 | 127 | |
paulb@25 | 128 | self.namespace = Namespace(structure=getattr(node, "structure", None)) |
paulb@24 | 129 | if locals is not None: |
paulb@32 | 130 | self.namespace.merge_namespace(locals) |
paulb@24 | 131 | |
paulb@25 | 132 | # Determine the global namespace. |
paulb@25 | 133 | |
paulb@24 | 134 | self.global_namespace = globals or self.namespace # NOTE: Improve this. |
paulb@25 | 135 | node.namespace = self.namespace |
paulb@21 | 136 | |
paulb@30 | 137 | # Remember return values. |
paulb@30 | 138 | |
paulb@30 | 139 | self.returns = [] |
paulb@30 | 140 | |
paulb@25 | 141 | # Add namespace details to any structure involved. |
paulb@25 | 142 | |
paulb@25 | 143 | if hasattr(node, "structure") and node.structure is not None: |
paulb@19 | 144 | node.structure.namespace = self.namespace |
paulb@25 | 145 | |
paulb@30 | 146 | # Initialise bases where appropriate. |
paulb@19 | 147 | |
paulb@30 | 148 | if hasattr(node.structure, "bases"): |
paulb@30 | 149 | base_refs = [] |
paulb@30 | 150 | for base in node.structure.bases: |
paulb@30 | 151 | self.dispatch(base) |
paulb@30 | 152 | base_refs.append(self.types) |
paulb@30 | 153 | node.structure.base_refs = base_refs |
paulb@30 | 154 | |
paulb@30 | 155 | # Dispatch to the code itself. |
paulb@30 | 156 | |
paulb@20 | 157 | result = self.dispatch(node) |
paulb@20 | 158 | |
paulb@20 | 159 | return result |
paulb@20 | 160 | |
paulb@24 | 161 | def annotate(self, node): |
paulb@24 | 162 | |
paulb@24 | 163 | "Annotate the given 'node' in the system." |
paulb@24 | 164 | |
paulb@24 | 165 | self.system.annotate(node, self.types) |
paulb@24 | 166 | |
paulb@25 | 167 | # Visitor methods. |
paulb@25 | 168 | |
paulb@19 | 169 | def default(self, node): |
paulb@24 | 170 | |
paulb@24 | 171 | """ |
paulb@24 | 172 | Process the given 'node', given that it does not have a specific |
paulb@24 | 173 | handler. |
paulb@24 | 174 | """ |
paulb@24 | 175 | |
paulb@25 | 176 | for attr in ("expr", "lvalue", "test", "handler"): |
paulb@19 | 177 | value = getattr(node, attr, None) |
paulb@19 | 178 | if value is not None: |
paulb@20 | 179 | setattr(node, attr, self.dispatch(value)) |
paulb@19 | 180 | for attr in ("body", "else_", "finally_", "code"): |
paulb@19 | 181 | value = getattr(node, attr, None) |
paulb@19 | 182 | if value is not None: |
paulb@20 | 183 | setattr(node, attr, self.dispatches(value)) |
paulb@20 | 184 | return node |
paulb@20 | 185 | |
paulb@20 | 186 | def dispatch(self, node, *args): |
paulb@20 | 187 | return Visitor.dispatch(self, node, *args) |
paulb@19 | 188 | |
paulb@19 | 189 | def visitLoadRef(self, loadref): |
paulb@19 | 190 | self.types = [loadref.ref] |
paulb@24 | 191 | self.annotate(loadref) |
paulb@20 | 192 | return loadref |
paulb@19 | 193 | |
paulb@19 | 194 | def visitLoadName(self, loadname): |
paulb@31 | 195 | self.types = self.namespace.load(loadname.name) |
paulb@31 | 196 | result = loadname |
paulb@31 | 197 | self.annotate(result) |
paulb@24 | 198 | return result |
paulb@19 | 199 | |
paulb@19 | 200 | def visitStoreName(self, storename): |
paulb@31 | 201 | storename.expr = self.dispatch(storename.expr) |
paulb@31 | 202 | self.namespace.store(storename.name, self.types) |
paulb@31 | 203 | return storename |
paulb@19 | 204 | |
paulb@21 | 205 | def visitLoadGlobal(self, loadglobal): |
paulb@21 | 206 | self.types = self.global_namespace.load(loadglobal.name) |
paulb@24 | 207 | self.annotate(loadglobal) |
paulb@21 | 208 | return loadglobal |
paulb@21 | 209 | |
paulb@21 | 210 | def visitStoreGlobal(self, storeglobal): |
paulb@21 | 211 | storeglobal.expr = self.dispatch(storeglobal.expr) |
paulb@32 | 212 | self.global_namespace.merge(storeglobal.name, self.types) |
paulb@21 | 213 | return storeglobal |
paulb@21 | 214 | |
paulb@19 | 215 | def visitLoadTemp(self, loadtemp): |
paulb@19 | 216 | index = getattr(loadtemp, "index", None) |
paulb@19 | 217 | self.types = self.temp[index] |
paulb@24 | 218 | self.annotate(loadtemp) |
paulb@20 | 219 | return loadtemp |
paulb@19 | 220 | |
paulb@19 | 221 | def visitStoreTemp(self, storetemp): |
paulb@21 | 222 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@19 | 223 | index = getattr(storetemp, "index", None) |
paulb@19 | 224 | self.temp[index] = self.types |
paulb@20 | 225 | return storetemp |
paulb@19 | 226 | |
paulb@19 | 227 | def visitReleaseTemp(self, releasetemp): |
paulb@19 | 228 | index = getattr(releasetemp, "index", None) |
paulb@19 | 229 | del self.temp[index] |
paulb@20 | 230 | return releasetemp |
paulb@20 | 231 | |
paulb@20 | 232 | def visitLoadAttr(self, loadattr): |
paulb@21 | 233 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@20 | 234 | types = [] |
paulb@20 | 235 | for ref in self.types: |
paulb@24 | 236 | for type in ref.namespace.load(loadattr.name): |
paulb@24 | 237 | types.append(Attribute(ref, type)) |
paulb@20 | 238 | self.types = types |
paulb@24 | 239 | self.annotate(loadattr) |
paulb@20 | 240 | return loadattr |
paulb@20 | 241 | |
paulb@20 | 242 | def visitStoreAttr(self, storeattr): |
paulb@21 | 243 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@20 | 244 | expr = self.types |
paulb@21 | 245 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@20 | 246 | for ref in self.types: |
paulb@20 | 247 | ref.namespace.store(storeattr.name, expr) |
paulb@20 | 248 | return storeattr |
paulb@19 | 249 | |
paulb@30 | 250 | def visitReturn(self, return_): |
paulb@30 | 251 | if hasattr(return_, "expr"): |
paulb@30 | 252 | return_.expr = self.dispatch(return_.expr) |
paulb@30 | 253 | self.returns += self.types |
paulb@30 | 254 | return return_ |
paulb@30 | 255 | |
paulb@24 | 256 | def visitInvoke(self, invoke): |
paulb@24 | 257 | invoke.expr = self.dispatch(invoke.expr) |
paulb@24 | 258 | expr = self.types |
paulb@25 | 259 | |
paulb@25 | 260 | # NOTE: Consider initialiser invocation for classes. |
paulb@25 | 261 | |
paulb@24 | 262 | types = [] |
paulb@24 | 263 | args = [] |
paulb@24 | 264 | |
paulb@24 | 265 | # Get type information for regular arguments. |
paulb@24 | 266 | |
paulb@24 | 267 | for arg in invoke.args: |
paulb@24 | 268 | args.append(self.dispatch(arg)) |
paulb@24 | 269 | types.append(self.types) |
paulb@24 | 270 | |
paulb@24 | 271 | # Get type information for star and dstar arguments. |
paulb@24 | 272 | |
paulb@24 | 273 | if invoke.star is not None: |
paulb@24 | 274 | param, default = invoke.star |
paulb@24 | 275 | default = self.dispatch(default) |
paulb@24 | 276 | invoke.star = param, default |
paulb@24 | 277 | types.append(default.types) |
paulb@24 | 278 | |
paulb@24 | 279 | if invoke.dstar is not None: |
paulb@24 | 280 | param, default = invoke.dstar |
paulb@24 | 281 | default = self.dispatch(default) |
paulb@24 | 282 | invoke.dstar = param, default |
paulb@24 | 283 | types.append(default.types) |
paulb@24 | 284 | |
paulb@24 | 285 | invoke.args = args |
paulb@24 | 286 | invoke.types = expr |
paulb@24 | 287 | |
paulb@24 | 288 | # NOTE: Now locate and invoke the subprogram. |
paulb@24 | 289 | |
paulb@24 | 290 | for subprogram in expr: |
paulb@30 | 291 | |
paulb@30 | 292 | # NOTE: Deal with class invocations by providing instance objects, |
paulb@30 | 293 | # NOTE: and with object invocations by using __call__ methods. |
paulb@30 | 294 | |
paulb@27 | 295 | if hasattr(invoke, "same_frame") and invoke.same_frame: |
paulb@27 | 296 | namespace = self.namespace |
paulb@27 | 297 | else: |
paulb@27 | 298 | items = self.make_items(invoke, subprogram) |
paulb@27 | 299 | namespace = self.make_namespace(items) |
paulb@27 | 300 | |
paulb@25 | 301 | annotator = Annotator() |
paulb@27 | 302 | annotator.process(subprogram, namespace, self.global_namespace) |
paulb@25 | 303 | |
paulb@25 | 304 | # NOTE: Annotate the node with invocation details. |
paulb@30 | 305 | # NOTE: This should really be as part of a table of alternatives. |
paulb@30 | 306 | |
paulb@30 | 307 | if hasattr(subprogram, "returns_value") and subprogram.returns_value: |
paulb@30 | 308 | self.types = annotator.returns |
paulb@30 | 309 | self.annotate(invoke) |
paulb@24 | 310 | |
paulb@24 | 311 | return invoke |
paulb@24 | 312 | |
paulb@25 | 313 | # Utility methods. |
paulb@25 | 314 | |
paulb@24 | 315 | def make_items(self, invocation, subprogram): |
paulb@24 | 316 | # NOTE: Support star and dstar. |
paulb@24 | 317 | args = invocation.args |
paulb@24 | 318 | params = subprogram.params |
paulb@24 | 319 | items = [] |
paulb@24 | 320 | keywords = {} |
paulb@24 | 321 | |
paulb@24 | 322 | # Process the specified arguments. |
paulb@24 | 323 | |
paulb@24 | 324 | for arg in args: |
paulb@24 | 325 | if isinstance(arg, Keyword): |
paulb@24 | 326 | keywords[arg.name] = arg.expr |
paulb@24 | 327 | continue |
paulb@24 | 328 | elif params: |
paulb@24 | 329 | param, default = params[0] |
paulb@24 | 330 | if arg is None: |
paulb@24 | 331 | arg = self.dispatch(default) |
paulb@24 | 332 | else: |
paulb@24 | 333 | raise TypeError, "Invocation has too many arguments." |
paulb@24 | 334 | items.append((param, arg.types)) |
paulb@24 | 335 | params = params[1:] |
paulb@24 | 336 | |
paulb@24 | 337 | # Collect the remaining defaults. |
paulb@24 | 338 | |
paulb@24 | 339 | while params: |
paulb@24 | 340 | param, default = params[0] |
paulb@24 | 341 | if keywords.has_key(param): |
paulb@24 | 342 | arg = keywords[param] |
paulb@24 | 343 | else: |
paulb@24 | 344 | arg = self.dispatch(default) |
paulb@24 | 345 | items.append((param, arg.types)) |
paulb@24 | 346 | params = params[1:] |
paulb@24 | 347 | |
paulb@24 | 348 | # Add star and dstar. |
paulb@24 | 349 | |
paulb@24 | 350 | if invocation.star is not None: |
paulb@24 | 351 | if subprogram.star is not None: |
paulb@24 | 352 | param, default = subprogram.star |
paulb@24 | 353 | items.append((param, invocation.star.types)) |
paulb@24 | 354 | else: |
paulb@24 | 355 | raise TypeError, "Invocation provides unwanted *args." |
paulb@24 | 356 | elif subprogram.star is not None: |
paulb@24 | 357 | param, default = subprogram.star |
paulb@24 | 358 | items.append((param, self.dispatch(default))) |
paulb@24 | 359 | |
paulb@24 | 360 | if invocation.dstar is not None: |
paulb@24 | 361 | if subprogram.dstar is not None: |
paulb@24 | 362 | param, default = subprogram.dstar |
paulb@24 | 363 | items.append((param, invocation.dstar.types)) |
paulb@24 | 364 | else: |
paulb@24 | 365 | raise TypeError, "Invocation provides unwanted **args." |
paulb@24 | 366 | elif subprogram.dstar is not None: |
paulb@24 | 367 | param, default = subprogram.dstar |
paulb@24 | 368 | items.append((param, self.dispatch(default))) |
paulb@24 | 369 | |
paulb@24 | 370 | return items |
paulb@24 | 371 | |
paulb@24 | 372 | def make_namespace(self, items): |
paulb@24 | 373 | namespace = Namespace() |
paulb@24 | 374 | namespace.merge_items(items) |
paulb@24 | 375 | return namespace |
paulb@24 | 376 | |
paulb@19 | 377 | # vim: tabstop=4 expandtab shiftwidth=4 |