paulb@19 | 1 | #!/usr/bin/env python |
paulb@19 | 2 | |
paulb@19 | 3 | """ |
paulb@34 | 4 | Annotate program node 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@50 | 24 | |
paulb@50 | 25 | -------- |
paulb@50 | 26 | |
paulb@57 | 27 | To use this module, the easiest approach is to use the annotate function: |
paulb@57 | 28 | |
paulb@57 | 29 | annotate(module, builtins) |
paulb@57 | 30 | |
paulb@57 | 31 | The more complicated approach involves obtaining an Annotator: |
paulb@50 | 32 | |
paulb@50 | 33 | annotator = Annotator() |
paulb@50 | 34 | |
paulb@57 | 35 | Then, processing an existing module with it: |
paulb@50 | 36 | |
paulb@57 | 37 | annotator.process(module) |
paulb@50 | 38 | |
paulb@50 | 39 | If a module containing built-in classes and functions has already been |
paulb@50 | 40 | annotated, such a module should be passed in as an additional argument: |
paulb@50 | 41 | |
paulb@57 | 42 | annotator.process(module, builtins) |
paulb@19 | 43 | """ |
paulb@19 | 44 | |
paulb@19 | 45 | from simplified import * |
paulb@19 | 46 | import compiler |
paulb@19 | 47 | |
paulb@24 | 48 | class System: |
paulb@24 | 49 | |
paulb@50 | 50 | """ |
paulb@50 | 51 | A class maintaining the state of the annotation system. When the system |
paulb@50 | 52 | counter can no longer be incremented by any annotation operation, the |
paulb@50 | 53 | system may be considered stable and fully annotated. |
paulb@50 | 54 | """ |
paulb@24 | 55 | |
paulb@24 | 56 | def __init__(self): |
paulb@24 | 57 | self.count = 0 |
paulb@24 | 58 | def init(self, node): |
paulb@24 | 59 | if not hasattr(node, "types"): |
paulb@24 | 60 | node.types = [] |
paulb@24 | 61 | def annotate(self, node, types): |
paulb@24 | 62 | self.init(node) |
paulb@24 | 63 | for type in types: |
paulb@24 | 64 | if type not in node.types: |
paulb@24 | 65 | node.types.append(type) |
paulb@24 | 66 | self.count += 1 |
paulb@24 | 67 | |
paulb@24 | 68 | system = System() |
paulb@24 | 69 | |
paulb@58 | 70 | # Exceptions. |
paulb@58 | 71 | |
paulb@83 | 72 | class AnnotationError(SimplifiedError): |
paulb@83 | 73 | |
paulb@83 | 74 | "An error in the annotation process." |
paulb@83 | 75 | |
paulb@83 | 76 | pass |
paulb@58 | 77 | |
paulb@67 | 78 | class AnnotationMessage(Exception): |
paulb@83 | 79 | |
paulb@83 | 80 | "A lesser annotation error." |
paulb@83 | 81 | |
paulb@67 | 82 | pass |
paulb@67 | 83 | |
paulb@24 | 84 | # Annotation. |
paulb@19 | 85 | |
paulb@20 | 86 | class Annotator(Visitor): |
paulb@24 | 87 | |
paulb@24 | 88 | """ |
paulb@24 | 89 | The type annotator which traverses the program nodes, typically depth-first, |
paulb@24 | 90 | and maintains a record of the current set of types applying to the currently |
paulb@24 | 91 | considered operation. Such types are also recorded on the nodes, and a |
paulb@24 | 92 | special "system" record is maintained to monitor the level of annotation |
paulb@24 | 93 | activity with a view to recognising when no more annotations are possible. |
paulb@50 | 94 | |
paulb@50 | 95 | Throughout the annotation activity, type information consists of lists of |
paulb@50 | 96 | Attribute objects where such objects retain information about the context of |
paulb@50 | 97 | the type (since a value in the program may be associated with an object or |
paulb@50 | 98 | class) and the actual type of the value being manipulated. Upon accessing |
paulb@50 | 99 | attribute information on namespaces, additional accessor information is also |
paulb@50 | 100 | exchanged - this provides a means of distinguishing between the different |
paulb@50 | 101 | types possible when the means of constructing the namespace may depend on |
paulb@50 | 102 | run-time behaviour. |
paulb@24 | 103 | """ |
paulb@24 | 104 | |
paulb@19 | 105 | def __init__(self): |
paulb@50 | 106 | |
paulb@50 | 107 | "Initialise the visitor." |
paulb@50 | 108 | |
paulb@19 | 109 | Visitor.__init__(self) |
paulb@24 | 110 | self.system = system |
paulb@19 | 111 | |
paulb@30 | 112 | # Satisfy visitor issues. |
paulb@30 | 113 | |
paulb@30 | 114 | self.visitor = self |
paulb@30 | 115 | |
paulb@57 | 116 | def process(self, module, builtins=None): |
paulb@45 | 117 | |
paulb@45 | 118 | """ |
paulb@57 | 119 | Process the given 'module', using the optional 'builtins' to access |
paulb@57 | 120 | built-in classes and functions. |
paulb@45 | 121 | """ |
paulb@45 | 122 | |
paulb@45 | 123 | self.subprograms = [] |
paulb@45 | 124 | self.current_subprograms = [] |
paulb@45 | 125 | self.current_namespaces = [] |
paulb@34 | 126 | |
paulb@34 | 127 | # Give constants their own namespace. |
paulb@34 | 128 | |
paulb@57 | 129 | for value, constant in module.simplifier.constants.items(): |
paulb@34 | 130 | constant.namespace = Namespace() |
paulb@34 | 131 | |
paulb@34 | 132 | # Process the module, supplying builtins if possible. |
paulb@34 | 133 | |
paulb@80 | 134 | self.builtins = builtins |
paulb@45 | 135 | self.global_namespace = Namespace() |
paulb@45 | 136 | |
paulb@57 | 137 | if builtins is not None: |
paulb@57 | 138 | self.builtins_namespace = builtins.namespace |
paulb@34 | 139 | else: |
paulb@45 | 140 | self.builtins_namespace = self.global_namespace |
paulb@34 | 141 | |
paulb@57 | 142 | return self.process_node(module) |
paulb@45 | 143 | |
paulb@45 | 144 | def process_node(self, node, locals=None): |
paulb@24 | 145 | |
paulb@24 | 146 | """ |
paulb@34 | 147 | Process a subprogram or module 'node', indicating any initial 'locals'. |
paulb@34 | 148 | Return an annotated subprogram or module. Note that this method may |
paulb@34 | 149 | mutate nodes in the original program. |
paulb@24 | 150 | """ |
paulb@24 | 151 | |
paulb@67 | 152 | # Determine the namespace. |
paulb@67 | 153 | |
paulb@76 | 154 | if locals is not None: |
paulb@45 | 155 | self.namespace = locals |
paulb@45 | 156 | else: |
paulb@45 | 157 | self.namespace = self.global_namespace |
paulb@45 | 158 | |
paulb@45 | 159 | # Record the current subprogram and namespace. |
paulb@20 | 160 | |
paulb@45 | 161 | self.current_subprograms.append(node) |
paulb@45 | 162 | self.current_namespaces.append(self.namespace) |
paulb@30 | 163 | |
paulb@25 | 164 | # Add namespace details to any structure involved. |
paulb@25 | 165 | |
paulb@34 | 166 | if getattr(node, "structure", None) is not None: |
paulb@34 | 167 | node.structure.namespace = Namespace() |
paulb@25 | 168 | |
paulb@30 | 169 | # Initialise bases where appropriate. |
paulb@19 | 170 | |
paulb@30 | 171 | if hasattr(node.structure, "bases"): |
paulb@30 | 172 | base_refs = [] |
paulb@30 | 173 | for base in node.structure.bases: |
paulb@30 | 174 | self.dispatch(base) |
paulb@49 | 175 | base_refs.append(self.namespace.types) |
paulb@30 | 176 | node.structure.base_refs = base_refs |
paulb@30 | 177 | |
paulb@30 | 178 | # Dispatch to the code itself. |
paulb@30 | 179 | |
paulb@49 | 180 | node.namespace = self.namespace |
paulb@20 | 181 | result = self.dispatch(node) |
paulb@49 | 182 | result.namespace = self.namespace |
paulb@49 | 183 | |
paulb@49 | 184 | # Obtain the return values. |
paulb@49 | 185 | |
paulb@49 | 186 | self.last_returns = self.namespace.returns |
paulb@49 | 187 | self.returned_locals = self.namespace.return_locals |
paulb@45 | 188 | |
paulb@45 | 189 | # Restore the previous subprogram and namespace. |
paulb@45 | 190 | |
paulb@45 | 191 | self.current_namespaces.pop() |
paulb@45 | 192 | if self.current_namespaces: |
paulb@45 | 193 | self.namespace = self.current_namespaces[-1] |
paulb@45 | 194 | |
paulb@45 | 195 | self.current_subprograms.pop() |
paulb@45 | 196 | |
paulb@20 | 197 | return result |
paulb@20 | 198 | |
paulb@83 | 199 | def annotate(self, node, types=None): |
paulb@24 | 200 | |
paulb@83 | 201 | """ |
paulb@83 | 202 | Annotate the given 'node' in the system, using either the optional |
paulb@83 | 203 | 'types' or the namespace's current type information. |
paulb@83 | 204 | """ |
paulb@24 | 205 | |
paulb@83 | 206 | self.system.annotate(node, types or self.namespace.types) |
paulb@34 | 207 | |
paulb@25 | 208 | # Visitor methods. |
paulb@25 | 209 | |
paulb@19 | 210 | def default(self, node): |
paulb@24 | 211 | |
paulb@24 | 212 | """ |
paulb@24 | 213 | Process the given 'node', given that it does not have a specific |
paulb@24 | 214 | handler. |
paulb@24 | 215 | """ |
paulb@24 | 216 | |
paulb@25 | 217 | for attr in ("expr", "lvalue", "test", "handler"): |
paulb@19 | 218 | value = getattr(node, attr, None) |
paulb@19 | 219 | if value is not None: |
paulb@20 | 220 | setattr(node, attr, self.dispatch(value)) |
paulb@19 | 221 | for attr in ("body", "else_", "finally_", "code"): |
paulb@19 | 222 | value = getattr(node, attr, None) |
paulb@19 | 223 | if value is not None: |
paulb@20 | 224 | setattr(node, attr, self.dispatches(value)) |
paulb@20 | 225 | return node |
paulb@20 | 226 | |
paulb@20 | 227 | def dispatch(self, node, *args): |
paulb@34 | 228 | try: |
paulb@34 | 229 | return Visitor.dispatch(self, node, *args) |
paulb@67 | 230 | except AnnotationError, exc: |
paulb@58 | 231 | exc.add(node) |
paulb@34 | 232 | raise |
paulb@67 | 233 | except AnnotationMessage, exc: |
paulb@67 | 234 | raise AnnotationError(exc, node) |
paulb@19 | 235 | |
paulb@110 | 236 | # Program structure/control-flow. |
paulb@110 | 237 | |
paulb@110 | 238 | def visitConditional(self, conditional): |
paulb@19 | 239 | |
paulb@110 | 240 | # Conditionals keep local namespace changes isolated. |
paulb@110 | 241 | # With Return nodes inside the body/else sections, the changes are |
paulb@110 | 242 | # communicated to the caller. |
paulb@19 | 243 | |
paulb@110 | 244 | conditional.test = self.dispatch(conditional.test) |
paulb@110 | 245 | saved_namespace = self.namespace |
paulb@19 | 246 | |
paulb@110 | 247 | self.namespace = Namespace() |
paulb@110 | 248 | self.namespace.merge_namespace(saved_namespace) |
paulb@110 | 249 | conditional.body = self.dispatches(conditional.body) |
paulb@110 | 250 | body_namespace = self.namespace |
paulb@19 | 251 | |
paulb@110 | 252 | self.namespace = Namespace() |
paulb@110 | 253 | self.namespace.merge_namespace(saved_namespace) |
paulb@110 | 254 | conditional.else_ = self.dispatches(conditional.else_) |
paulb@110 | 255 | else_namespace = self.namespace |
paulb@19 | 256 | |
paulb@110 | 257 | self.namespace = Namespace() |
paulb@110 | 258 | self.namespace.merge_namespace(body_namespace) |
paulb@110 | 259 | self.namespace.merge_namespace(else_namespace) |
paulb@110 | 260 | |
paulb@110 | 261 | return conditional |
paulb@110 | 262 | |
paulb@110 | 263 | # Namespace operations. |
paulb@20 | 264 | |
paulb@20 | 265 | def visitLoadAttr(self, loadattr): |
paulb@21 | 266 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@20 | 267 | types = [] |
paulb@39 | 268 | accesses = {} |
paulb@79 | 269 | non_accesses = {} |
paulb@49 | 270 | for attr in self.namespace.types: |
paulb@103 | 271 | attributes = get_attributes(attr.type, loadattr.name) |
paulb@103 | 272 | if not attributes: |
paulb@103 | 273 | print "No attributes for", loadattr.name, "in", attr.type |
paulb@103 | 274 | for attribute, accessor in attributes: |
paulb@49 | 275 | if attribute is not None: |
paulb@49 | 276 | types.append(attribute) |
paulb@79 | 277 | if not accesses.has_key(attr.type): |
paulb@79 | 278 | accesses[attr.type] = [] |
paulb@83 | 279 | if not (attribute, accessor) in accesses[attr.type]: |
paulb@83 | 280 | accesses[attr.type].append((attribute, accessor)) |
paulb@49 | 281 | else: |
paulb@79 | 282 | print "Empty attribute", loadattr.name, "via accessor", accessor |
paulb@79 | 283 | if not non_accesses.has_key(attr.type): |
paulb@79 | 284 | non_accesses[attr.type] = [] |
paulb@83 | 285 | if not (attribute, accessor) in non_accesses[attr.type]: |
paulb@83 | 286 | non_accesses[attr.type].append((attribute, accessor)) |
paulb@49 | 287 | self.namespace.set_types(types) |
paulb@39 | 288 | loadattr.accesses = accesses |
paulb@79 | 289 | loadattr.non_accesses = non_accesses |
paulb@24 | 290 | self.annotate(loadattr) |
paulb@20 | 291 | return loadattr |
paulb@20 | 292 | |
paulb@110 | 293 | def visitLoadName(self, loadname): |
paulb@110 | 294 | self.namespace.set_types(self.namespace.load(loadname.name)) |
paulb@110 | 295 | result = loadname |
paulb@110 | 296 | self.annotate(result) |
paulb@110 | 297 | return result |
paulb@110 | 298 | |
paulb@110 | 299 | def visitLoadRef(self, loadref): |
paulb@110 | 300 | self.namespace.set_types([Attribute(None, loadref.ref)]) |
paulb@110 | 301 | self.annotate(loadref) |
paulb@110 | 302 | return loadref |
paulb@110 | 303 | |
paulb@110 | 304 | def visitLoadTemp(self, loadtemp): |
paulb@110 | 305 | index = getattr(loadtemp, "index", None) |
paulb@110 | 306 | try: |
paulb@110 | 307 | if getattr(loadtemp, "release", 0): |
paulb@110 | 308 | self.namespace.set_types(self.namespace.temp[index].pop()) |
paulb@110 | 309 | else: |
paulb@110 | 310 | self.namespace.set_types(self.namespace.temp[index][-1]) |
paulb@110 | 311 | except KeyError: |
paulb@110 | 312 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@110 | 313 | self.annotate(loadtemp) |
paulb@110 | 314 | return loadtemp |
paulb@110 | 315 | |
paulb@110 | 316 | def visitReleaseTemp(self, releasetemp): |
paulb@110 | 317 | index = getattr(releasetemp, "index", None) |
paulb@110 | 318 | try: |
paulb@110 | 319 | self.namespace.temp[index].pop() |
paulb@110 | 320 | except KeyError: |
paulb@110 | 321 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@110 | 322 | except IndexError: |
paulb@110 | 323 | pass #raise AnnotationMessage, "Temporary store index '%s' is empty." % index |
paulb@110 | 324 | return releasetemp |
paulb@110 | 325 | |
paulb@110 | 326 | def visitReturn(self, return_): |
paulb@110 | 327 | if hasattr(return_, "expr"): |
paulb@110 | 328 | return_.expr = self.dispatch(return_.expr) |
paulb@110 | 329 | combine(self.namespace.returns, self.namespace.types) |
paulb@110 | 330 | self.annotate(return_) |
paulb@110 | 331 | self.namespace.snapshot() |
paulb@110 | 332 | return return_ |
paulb@110 | 333 | |
paulb@20 | 334 | def visitStoreAttr(self, storeattr): |
paulb@21 | 335 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@49 | 336 | expr = self.namespace.types |
paulb@21 | 337 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@57 | 338 | writes = {} |
paulb@49 | 339 | for attr in self.namespace.types: |
paulb@49 | 340 | if attr is None: |
paulb@49 | 341 | print "Empty attribute storage attempt" |
paulb@49 | 342 | continue |
paulb@49 | 343 | attr.type.namespace.store(storeattr.name, expr) |
paulb@57 | 344 | writes[attr.type] = attr.type.namespace.load(storeattr.name) |
paulb@57 | 345 | storeattr.writes = writes |
paulb@20 | 346 | return storeattr |
paulb@19 | 347 | |
paulb@110 | 348 | def visitStoreName(self, storename): |
paulb@110 | 349 | storename.expr = self.dispatch(storename.expr) |
paulb@110 | 350 | self.namespace.store(storename.name, self.namespace.types) |
paulb@110 | 351 | return storename |
paulb@34 | 352 | |
paulb@110 | 353 | def visitStoreTemp(self, storetemp): |
paulb@110 | 354 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@110 | 355 | index = getattr(storetemp, "index", None) |
paulb@110 | 356 | if not self.namespace.temp.has_key(index): |
paulb@110 | 357 | self.namespace.temp[index] = [] |
paulb@110 | 358 | self.namespace.temp[index].append(self.namespace.types) |
paulb@110 | 359 | return storetemp |
paulb@91 | 360 | |
paulb@110 | 361 | # Invocations are a chapter of their own. |
paulb@30 | 362 | |
paulb@24 | 363 | def visitInvoke(self, invoke): |
paulb@83 | 364 | |
paulb@83 | 365 | # First find the callables. |
paulb@83 | 366 | |
paulb@24 | 367 | invoke.expr = self.dispatch(invoke.expr) |
paulb@49 | 368 | invocation_types = self.namespace.types |
paulb@25 | 369 | |
paulb@83 | 370 | # Invocation processing starts with making sure that the arguments have |
paulb@83 | 371 | # been processed. |
paulb@83 | 372 | |
paulb@67 | 373 | if isinstance(invoke, InvokeFunction): |
paulb@67 | 374 | self.process_args(invoke) |
paulb@24 | 375 | |
paulb@34 | 376 | # Now locate and invoke the subprogram. This can be complicated because |
paulb@34 | 377 | # the target may be a class or object, and there may be many different |
paulb@34 | 378 | # related subprograms. |
paulb@24 | 379 | |
paulb@68 | 380 | invocations = [] |
paulb@34 | 381 | |
paulb@39 | 382 | # Visit each callable in turn, finding subprograms. |
paulb@34 | 383 | |
paulb@49 | 384 | for attr in invocation_types: |
paulb@30 | 385 | |
paulb@34 | 386 | # Deal with class invocations by providing instance objects. |
paulb@34 | 387 | # Here, each class is queried for the __init__ method, which may |
paulb@34 | 388 | # exist for some combinations of classes in a hierarchy but not for |
paulb@34 | 389 | # others. |
paulb@34 | 390 | |
paulb@49 | 391 | if isinstance(attr.type, Class): |
paulb@49 | 392 | attributes = get_attributes(attr.type, "__init__") |
paulb@30 | 393 | |
paulb@34 | 394 | # Deal with object invocations by using __call__ methods. |
paulb@34 | 395 | |
paulb@49 | 396 | elif isinstance(attr.type, Instance): |
paulb@49 | 397 | attributes = get_attributes(attr.type, "__call__") |
paulb@34 | 398 | |
paulb@34 | 399 | # Normal functions or methods are more straightforward. |
paulb@39 | 400 | # Here, we model them using an attribute with no context and with |
paulb@39 | 401 | # no associated accessor. |
paulb@34 | 402 | |
paulb@27 | 403 | else: |
paulb@49 | 404 | attributes = [(attr, None)] |
paulb@39 | 405 | |
paulb@39 | 406 | # Inspect each attribute and extract the subprogram. |
paulb@25 | 407 | |
paulb@39 | 408 | for attribute, accessor in attributes: |
paulb@38 | 409 | |
paulb@39 | 410 | # If a class is involved, presume that it must create a new |
paulb@39 | 411 | # object. |
paulb@39 | 412 | |
paulb@49 | 413 | if isinstance(attr.type, Class): |
paulb@53 | 414 | |
paulb@53 | 415 | # Instantiate the class. |
paulb@53 | 416 | # NOTE: Should probably only allocate a single instance. |
paulb@53 | 417 | |
paulb@79 | 418 | instance = self.new_instance(invoke, "new", attr.type.full_name(), attr.type) |
paulb@53 | 419 | |
paulb@53 | 420 | # For instantiations, switch the context. |
paulb@53 | 421 | |
paulb@53 | 422 | if attribute is not None: |
paulb@53 | 423 | attribute = Attribute(instance, attribute.type) |
paulb@53 | 424 | |
paulb@53 | 425 | # Skip cases where no callable is found. |
paulb@53 | 426 | |
paulb@53 | 427 | if attribute is not None: |
paulb@53 | 428 | |
paulb@53 | 429 | # If a subprogram is defined, invoke it. |
paulb@53 | 430 | |
paulb@53 | 431 | self.invoke_subprogram(invoke, attribute) |
paulb@68 | 432 | if attribute.type not in invocations: |
paulb@68 | 433 | invocations.append(attribute.type) |
paulb@53 | 434 | |
paulb@79 | 435 | elif not isinstance(attr.type, Class): |
paulb@68 | 436 | print "Invocation type is None for", accessor |
paulb@53 | 437 | |
paulb@79 | 438 | # Special case: initialisation. |
paulb@79 | 439 | |
paulb@53 | 440 | if isinstance(attr.type, Class): |
paulb@53 | 441 | |
paulb@53 | 442 | # Associate the instance with the result of this invocation. |
paulb@53 | 443 | |
paulb@54 | 444 | self.namespace.set_types([Attribute(None, instance)]) |
paulb@39 | 445 | self.annotate(invoke) |
paulb@39 | 446 | |
paulb@83 | 447 | # Remember the invocations that were found, along with the return type |
paulb@83 | 448 | # information. |
paulb@83 | 449 | |
paulb@38 | 450 | invoke.invocations = invocations |
paulb@80 | 451 | self.namespace.set_types(getattr(invoke, "types", [])) |
paulb@24 | 452 | return invoke |
paulb@24 | 453 | |
paulb@67 | 454 | visitInvokeFunction = visitInvoke |
paulb@67 | 455 | visitInvokeBlock = visitInvoke |
paulb@67 | 456 | |
paulb@25 | 457 | # Utility methods. |
paulb@25 | 458 | |
paulb@79 | 459 | def new_instance(self, node, reason, target, type): |
paulb@79 | 460 | |
paulb@79 | 461 | "Create, on the given 'node', a new instance with the given 'type'." |
paulb@79 | 462 | |
paulb@79 | 463 | if not hasattr(node, "instances"): |
paulb@79 | 464 | node.instances = {} |
paulb@79 | 465 | |
paulb@79 | 466 | if not node.instances.has_key((reason, target, type)): |
paulb@91 | 467 | |
paulb@91 | 468 | # Insist on a single instance per type. |
paulb@91 | 469 | # NOTE: Strategy-dependent instantiation. |
paulb@91 | 470 | |
paulb@91 | 471 | if len(type.instances) == 0: |
paulb@91 | 472 | instance = Instance() |
paulb@91 | 473 | instance.namespace = Namespace() |
paulb@91 | 474 | instance.namespace.store("__class__", [Attribute(None, type)]) |
paulb@91 | 475 | type.instances.append(instance) |
paulb@91 | 476 | else: |
paulb@91 | 477 | instance = type.instances[0] |
paulb@91 | 478 | |
paulb@79 | 479 | node.instances[(reason, target, type)] = instance |
paulb@79 | 480 | |
paulb@79 | 481 | return node.instances[(reason, target, type)] |
paulb@79 | 482 | |
paulb@34 | 483 | def invoke_subprogram(self, invoke, subprogram): |
paulb@34 | 484 | |
paulb@79 | 485 | "Invoke using the given 'invoke' node the given 'subprogram'." |
paulb@34 | 486 | |
paulb@42 | 487 | # Test to see if anything has changed. |
paulb@42 | 488 | |
paulb@42 | 489 | if hasattr(invoke, "syscount") and invoke.syscount == self.system.count: |
paulb@42 | 490 | return |
paulb@42 | 491 | |
paulb@79 | 492 | # Remember the state of the system. |
paulb@79 | 493 | |
paulb@79 | 494 | else: |
paulb@79 | 495 | invoke.syscount = self.system.count |
paulb@79 | 496 | |
paulb@54 | 497 | # Test for context information, making it into a real attribute. |
paulb@34 | 498 | |
paulb@54 | 499 | if subprogram.context is not None: |
paulb@54 | 500 | context = Attribute(None, subprogram.context) |
paulb@34 | 501 | target = subprogram.type |
paulb@34 | 502 | else: |
paulb@34 | 503 | context = None |
paulb@54 | 504 | target = subprogram.type |
paulb@50 | 505 | |
paulb@34 | 506 | # Provide the correct namespace for the invocation. |
paulb@34 | 507 | |
paulb@57 | 508 | if isinstance(invoke, InvokeBlock): |
paulb@34 | 509 | namespace = Namespace() |
paulb@34 | 510 | namespace.merge_namespace(self.namespace) |
paulb@34 | 511 | else: |
paulb@34 | 512 | items = self.make_items(invoke, target, context) |
paulb@34 | 513 | namespace = self.make_namespace(items) |
paulb@34 | 514 | |
paulb@34 | 515 | # Process the subprogram. |
paulb@34 | 516 | |
paulb@53 | 517 | self.process_node(target, namespace) |
paulb@34 | 518 | |
paulb@45 | 519 | # NOTE: Improve and verify this. |
paulb@91 | 520 | # If the invocation returns a value, acquire the return types. |
paulb@34 | 521 | |
paulb@53 | 522 | if getattr(target, "returns_value", 0): |
paulb@49 | 523 | self.namespace.set_types(self.last_returns) |
paulb@34 | 524 | self.annotate(invoke) |
paulb@34 | 525 | |
paulb@91 | 526 | # Otherwise, if it is a normal block, merge the locals. |
paulb@91 | 527 | |
paulb@91 | 528 | elif isinstance(invoke, InvokeBlock): |
paulb@45 | 529 | for locals in self.returned_locals: |
paulb@34 | 530 | self.namespace.merge_namespace(locals) |
paulb@34 | 531 | |
paulb@83 | 532 | def process_args(self, invocation): |
paulb@83 | 533 | |
paulb@83 | 534 | "Process the arguments associated with an 'invocation'." |
paulb@54 | 535 | |
paulb@93 | 536 | invocation.pos_args = self.dispatches(invocation.pos_args) |
paulb@93 | 537 | invocation.kw_args = self.dispatch_dict(invocation.kw_args) |
paulb@54 | 538 | |
paulb@54 | 539 | # Get type information for star and dstar arguments. |
paulb@54 | 540 | |
paulb@83 | 541 | if invocation.star is not None: |
paulb@83 | 542 | param, default = invocation.star |
paulb@54 | 543 | default = self.dispatch(default) |
paulb@83 | 544 | invocation.star = param, default |
paulb@54 | 545 | |
paulb@83 | 546 | if invocation.dstar is not None: |
paulb@83 | 547 | param, default = invocation.dstar |
paulb@54 | 548 | default = self.dispatch(default) |
paulb@83 | 549 | invocation.dstar = param, default |
paulb@54 | 550 | |
paulb@34 | 551 | def make_items(self, invocation, subprogram, context): |
paulb@34 | 552 | |
paulb@34 | 553 | """ |
paulb@34 | 554 | Make an items mapping for the 'invocation' of the 'subprogram' using the |
paulb@34 | 555 | given 'context' (which may be None). |
paulb@34 | 556 | """ |
paulb@34 | 557 | |
paulb@34 | 558 | if context is not None: |
paulb@93 | 559 | pos_args = [Self(context)] + invocation.pos_args |
paulb@34 | 560 | else: |
paulb@93 | 561 | pos_args = invocation.pos_args |
paulb@93 | 562 | kw_args = invocation.kw_args |
paulb@34 | 563 | |
paulb@67 | 564 | # Sort the arguments into positional and keyword arguments. |
paulb@67 | 565 | |
paulb@24 | 566 | params = subprogram.params |
paulb@24 | 567 | items = [] |
paulb@67 | 568 | star_args = [] |
paulb@24 | 569 | |
paulb@67 | 570 | # Match each positional argument, taking excess arguments as star args. |
paulb@67 | 571 | |
paulb@67 | 572 | for arg in pos_args: |
paulb@67 | 573 | if params: |
paulb@24 | 574 | param, default = params[0] |
paulb@24 | 575 | if arg is None: |
paulb@54 | 576 | arg = default |
paulb@67 | 577 | items.append((param, arg.types)) |
paulb@67 | 578 | params = params[1:] |
paulb@24 | 579 | else: |
paulb@67 | 580 | star_args.append(arg) |
paulb@24 | 581 | |
paulb@24 | 582 | # Collect the remaining defaults. |
paulb@24 | 583 | |
paulb@24 | 584 | while params: |
paulb@24 | 585 | param, default = params[0] |
paulb@67 | 586 | if kw_args.has_key(param): |
paulb@67 | 587 | arg = kw_args[param] |
paulb@73 | 588 | del kw_args[param] |
paulb@93 | 589 | elif default is not None: |
paulb@93 | 590 | arg = self.dispatch(default) |
paulb@93 | 591 | else: |
paulb@67 | 592 | raise AnnotationMessage, "No argument supplied in '%s' for parameter '%s'." % (subprogram, param) |
paulb@24 | 593 | items.append((param, arg.types)) |
paulb@24 | 594 | params = params[1:] |
paulb@24 | 595 | |
paulb@93 | 596 | dstar_args = kw_args.values() |
paulb@73 | 597 | |
paulb@73 | 598 | # Construct temporary objects. |
paulb@73 | 599 | |
paulb@73 | 600 | if star_args: |
paulb@80 | 601 | star_invocation = self.make_star_args(invocation, subprogram, star_args) |
paulb@80 | 602 | self.dispatch(star_invocation) |
paulb@80 | 603 | star_types = star_invocation.types |
paulb@73 | 604 | else: |
paulb@73 | 605 | star_types = None |
paulb@73 | 606 | |
paulb@73 | 607 | if dstar_args: |
paulb@83 | 608 | dstar_invocation = self.make_dstar_args(invocation, subprogram, dstar_args) # NOTE: To be written! |
paulb@80 | 609 | self.dispatch(dstar_invocation) |
paulb@80 | 610 | dstar_types = dstar_invocation.types |
paulb@73 | 611 | else: |
paulb@73 | 612 | dstar_types = None |
paulb@73 | 613 | |
paulb@73 | 614 | # NOTE: Merge the objects properly. |
paulb@73 | 615 | |
paulb@73 | 616 | star_types = star_types or invocation.star and invocation.star.types |
paulb@73 | 617 | dstar_types = dstar_types or invocation.dstar and invocation.dstar.types |
paulb@73 | 618 | |
paulb@24 | 619 | # Add star and dstar. |
paulb@24 | 620 | |
paulb@73 | 621 | if star_types is not None: |
paulb@24 | 622 | if subprogram.star is not None: |
paulb@24 | 623 | param, default = subprogram.star |
paulb@73 | 624 | items.append((param, star_types)) |
paulb@24 | 625 | else: |
paulb@67 | 626 | raise AnnotationMessage, "Invocation provides unwanted *args." |
paulb@24 | 627 | elif subprogram.star is not None: |
paulb@24 | 628 | param, default = subprogram.star |
paulb@54 | 629 | arg = self.dispatch(default) # NOTE: Review reprocessing. |
paulb@54 | 630 | items.append((param, arg.types)) |
paulb@24 | 631 | |
paulb@73 | 632 | if dstar_types is not None: |
paulb@24 | 633 | if subprogram.dstar is not None: |
paulb@24 | 634 | param, default = subprogram.dstar |
paulb@73 | 635 | items.append((param, dstar_types)) |
paulb@24 | 636 | else: |
paulb@67 | 637 | raise AnnotationMessage, "Invocation provides unwanted **args." |
paulb@24 | 638 | elif subprogram.dstar is not None: |
paulb@24 | 639 | param, default = subprogram.dstar |
paulb@54 | 640 | arg = self.dispatch(default) # NOTE: Review reprocessing. |
paulb@54 | 641 | items.append((param, arg.types)) |
paulb@24 | 642 | |
paulb@83 | 643 | # Record the parameter types. |
paulb@83 | 644 | |
paulb@83 | 645 | subprogram.paramtypes = {} |
paulb@83 | 646 | for param, types in items: |
paulb@83 | 647 | subprogram.paramtypes[param] = types |
paulb@83 | 648 | |
paulb@24 | 649 | return items |
paulb@24 | 650 | |
paulb@80 | 651 | def make_star_args(self, invocation, subprogram, star_args): |
paulb@80 | 652 | |
paulb@80 | 653 | "Make a subprogram which initialises a list containing 'star_args'." |
paulb@80 | 654 | |
paulb@80 | 655 | if not hasattr(invocation, "stars"): |
paulb@80 | 656 | invocation.stars = {} |
paulb@80 | 657 | |
paulb@80 | 658 | if not invocation.stars.has_key(subprogram.full_name()): |
paulb@80 | 659 | code=[ |
paulb@80 | 660 | StoreTemp( |
paulb@80 | 661 | expr=InvokeFunction( |
paulb@80 | 662 | expr=LoadAttr( |
paulb@80 | 663 | expr=LoadRef( |
paulb@80 | 664 | ref=self.builtins |
paulb@80 | 665 | ), |
paulb@80 | 666 | name="list", |
paulb@80 | 667 | nstype="module", |
paulb@80 | 668 | ), |
paulb@80 | 669 | args=[], |
paulb@80 | 670 | star=None, |
paulb@80 | 671 | dstar=None |
paulb@80 | 672 | ) |
paulb@80 | 673 | ) |
paulb@80 | 674 | ] |
paulb@80 | 675 | |
paulb@80 | 676 | for arg in star_args: |
paulb@80 | 677 | code.append( |
paulb@80 | 678 | InvokeFunction( |
paulb@80 | 679 | expr=LoadAttr( |
paulb@80 | 680 | expr=LoadTemp(), |
paulb@80 | 681 | name="append" |
paulb@80 | 682 | ), |
paulb@80 | 683 | args=[arg], |
paulb@80 | 684 | star=None, |
paulb@80 | 685 | dstar=None |
paulb@80 | 686 | ) |
paulb@80 | 687 | ) |
paulb@80 | 688 | |
paulb@80 | 689 | code += [ |
paulb@80 | 690 | Return(expr=LoadTemp(release=1)) |
paulb@80 | 691 | ] |
paulb@80 | 692 | |
paulb@80 | 693 | invocation.stars[subprogram.full_name()] = InvokeBlock( |
paulb@80 | 694 | produces_result=1, |
paulb@80 | 695 | expr=LoadRef( |
paulb@80 | 696 | ref=Subprogram( |
paulb@80 | 697 | name=None, |
paulb@80 | 698 | returns_value=1, |
paulb@80 | 699 | params=[], |
paulb@80 | 700 | star=None, |
paulb@80 | 701 | dstar=None, |
paulb@80 | 702 | code=code |
paulb@80 | 703 | ) |
paulb@80 | 704 | ) |
paulb@80 | 705 | ) |
paulb@80 | 706 | |
paulb@80 | 707 | return invocation.stars[subprogram.full_name()] |
paulb@80 | 708 | |
paulb@24 | 709 | def make_namespace(self, items): |
paulb@24 | 710 | namespace = Namespace() |
paulb@24 | 711 | namespace.merge_items(items) |
paulb@24 | 712 | return namespace |
paulb@24 | 713 | |
paulb@48 | 714 | # Namespace-related abstractions. |
paulb@48 | 715 | |
paulb@48 | 716 | class Namespace: |
paulb@48 | 717 | |
paulb@48 | 718 | """ |
paulb@48 | 719 | A local namespace which may either relate to a genuine set of function |
paulb@50 | 720 | locals or the initialisation of a structure or module. |
paulb@48 | 721 | """ |
paulb@48 | 722 | |
paulb@48 | 723 | def __init__(self): |
paulb@50 | 724 | |
paulb@50 | 725 | """ |
paulb@50 | 726 | Initialise the namespace with a mapping of local names to possible |
paulb@50 | 727 | types, a list of return values and of possible returned local |
paulb@50 | 728 | namespaces. The namespace also tracks the "current" types and a mapping |
paulb@50 | 729 | of temporary value names to types. |
paulb@50 | 730 | """ |
paulb@50 | 731 | |
paulb@48 | 732 | self.names = {} |
paulb@49 | 733 | self.returns = [] |
paulb@49 | 734 | self.return_locals = [] |
paulb@49 | 735 | self.temp = {} |
paulb@49 | 736 | self.types = [] |
paulb@49 | 737 | |
paulb@49 | 738 | def set_types(self, types): |
paulb@49 | 739 | self.types = types |
paulb@48 | 740 | |
paulb@48 | 741 | def store(self, name, types): |
paulb@48 | 742 | self.names[name] = types |
paulb@48 | 743 | |
paulb@86 | 744 | __setitem__ = store |
paulb@74 | 745 | |
paulb@48 | 746 | def load(self, name): |
paulb@48 | 747 | return self.names[name] |
paulb@48 | 748 | |
paulb@86 | 749 | __getitem__ = load |
paulb@74 | 750 | |
paulb@68 | 751 | def merge_namespace(self, namespace): |
paulb@68 | 752 | self.merge_items(namespace.names.items()) |
paulb@73 | 753 | combine(self.returns, namespace.returns) |
paulb@73 | 754 | self.temp = namespace.temp |
paulb@68 | 755 | |
paulb@68 | 756 | def merge_items(self, items): |
paulb@68 | 757 | for name, types in items: |
paulb@68 | 758 | self.merge(name, types) |
paulb@68 | 759 | |
paulb@48 | 760 | def merge(self, name, types): |
paulb@48 | 761 | if not self.names.has_key(name): |
paulb@48 | 762 | self.names[name] = types[:] |
paulb@48 | 763 | else: |
paulb@48 | 764 | existing = self.names[name] |
paulb@73 | 765 | combine(existing, types) |
paulb@48 | 766 | |
paulb@49 | 767 | def snapshot(self): |
paulb@49 | 768 | |
paulb@49 | 769 | "Make a snapshot of the locals and remember them." |
paulb@49 | 770 | |
paulb@49 | 771 | namespace = Namespace() |
paulb@49 | 772 | namespace.merge_namespace(self) |
paulb@49 | 773 | self.return_locals.append(namespace) |
paulb@49 | 774 | |
paulb@48 | 775 | def __repr__(self): |
paulb@48 | 776 | return repr(self.names) |
paulb@48 | 777 | |
paulb@48 | 778 | class Attribute: |
paulb@48 | 779 | |
paulb@48 | 780 | """ |
paulb@48 | 781 | An attribute abstraction, indicating the type of the attribute along with |
paulb@48 | 782 | its context or origin. |
paulb@48 | 783 | """ |
paulb@48 | 784 | |
paulb@48 | 785 | def __init__(self, context, type): |
paulb@48 | 786 | self.context = context |
paulb@48 | 787 | self.type = type |
paulb@48 | 788 | |
paulb@48 | 789 | def __eq__(self, other): |
paulb@48 | 790 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@48 | 791 | |
paulb@48 | 792 | def __repr__(self): |
paulb@54 | 793 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@48 | 794 | |
paulb@53 | 795 | class Self: |
paulb@91 | 796 | |
paulb@91 | 797 | "A node encapsulating object/context information in an argument list." |
paulb@91 | 798 | |
paulb@53 | 799 | def __init__(self, attribute): |
paulb@53 | 800 | self.types = [attribute] |
paulb@53 | 801 | |
paulb@73 | 802 | def combine(target, additions): |
paulb@91 | 803 | |
paulb@91 | 804 | """ |
paulb@91 | 805 | Merge into the 'target' sequence the given 'additions', preventing duplicate |
paulb@91 | 806 | items. |
paulb@91 | 807 | """ |
paulb@91 | 808 | |
paulb@73 | 809 | for addition in additions: |
paulb@73 | 810 | if addition not in target: |
paulb@73 | 811 | target.append(addition) |
paulb@73 | 812 | |
paulb@48 | 813 | def find_attributes(structure, name): |
paulb@48 | 814 | |
paulb@48 | 815 | """ |
paulb@48 | 816 | Find for the given 'structure' all attributes for the given 'name', visiting |
paulb@48 | 817 | base classes where appropriate and returning the attributes in order of |
paulb@48 | 818 | descending precedence for all possible base classes. |
paulb@48 | 819 | |
paulb@48 | 820 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 821 | the structure involved in accessing the attribute. |
paulb@48 | 822 | """ |
paulb@48 | 823 | |
paulb@48 | 824 | # First attempt to search the instance/class namespace. |
paulb@48 | 825 | |
paulb@48 | 826 | try: |
paulb@48 | 827 | l = structure.namespace.load(name) |
paulb@48 | 828 | attributes = [] |
paulb@48 | 829 | for attribute in l: |
paulb@48 | 830 | attributes.append((attribute, structure)) |
paulb@48 | 831 | |
paulb@48 | 832 | # If that does not work, attempt to investigate any class or base classes. |
paulb@48 | 833 | |
paulb@48 | 834 | except KeyError: |
paulb@48 | 835 | attributes = [] |
paulb@48 | 836 | |
paulb@48 | 837 | # Investigate any instance's implementing class. |
paulb@48 | 838 | |
paulb@48 | 839 | if isinstance(structure, Instance): |
paulb@73 | 840 | for attr in structure.namespace.load("__class__"): |
paulb@73 | 841 | cls = attr.type |
paulb@50 | 842 | l = get_attributes(cls, name) |
paulb@73 | 843 | combine(attributes, l) |
paulb@48 | 844 | |
paulb@48 | 845 | # Investigate any class's base classes. |
paulb@48 | 846 | |
paulb@48 | 847 | elif isinstance(structure, Class): |
paulb@48 | 848 | |
paulb@48 | 849 | # If no base classes exist, return an indicator that no attribute |
paulb@48 | 850 | # exists. |
paulb@48 | 851 | |
paulb@48 | 852 | if not structure.base_refs: |
paulb@48 | 853 | return [(None, structure)] |
paulb@48 | 854 | |
paulb@48 | 855 | # Otherwise, find all possible base classes. |
paulb@48 | 856 | |
paulb@48 | 857 | for base_refs in structure.base_refs: |
paulb@48 | 858 | base_attributes = [] |
paulb@48 | 859 | |
paulb@48 | 860 | # For each base class, find attributes either in the base |
paulb@48 | 861 | # class or its own base classes. |
paulb@48 | 862 | |
paulb@48 | 863 | for base_ref in base_refs: |
paulb@50 | 864 | l = get_attributes(base_ref, name) |
paulb@73 | 865 | combine(base_attributes, l) |
paulb@48 | 866 | |
paulb@73 | 867 | combine(attributes, base_attributes) |
paulb@48 | 868 | |
paulb@49 | 869 | return attributes |
paulb@48 | 870 | |
paulb@48 | 871 | def get_attributes(structure, name): |
paulb@48 | 872 | |
paulb@48 | 873 | """ |
paulb@48 | 874 | Return all possible attributes for the given 'structure' having the given |
paulb@48 | 875 | 'name', wrapping each attribute in an Attribute object which includes |
paulb@48 | 876 | context information for the attribute access. |
paulb@48 | 877 | |
paulb@48 | 878 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 879 | the structure involved in accessing the attribute. |
paulb@48 | 880 | """ |
paulb@48 | 881 | |
paulb@48 | 882 | if isinstance(structure, Attribute): |
paulb@48 | 883 | structure = structure.type |
paulb@53 | 884 | results = [] |
paulb@53 | 885 | for attribute, accessor in find_attributes(structure, name): |
paulb@53 | 886 | if attribute is not None and isinstance(structure, Structure): |
paulb@53 | 887 | results.append((Attribute(structure, attribute.type), accessor)) |
paulb@53 | 888 | else: |
paulb@53 | 889 | results.append((attribute, accessor)) |
paulb@53 | 890 | return results |
paulb@48 | 891 | |
paulb@57 | 892 | # Convenience functions. |
paulb@57 | 893 | |
paulb@57 | 894 | def annotate(module, builtins=None): |
paulb@57 | 895 | |
paulb@57 | 896 | """ |
paulb@57 | 897 | Annotate the given 'module', also employing the optional 'builtins' module, |
paulb@57 | 898 | if specified. |
paulb@57 | 899 | """ |
paulb@57 | 900 | |
paulb@57 | 901 | annotator = Annotator() |
paulb@57 | 902 | if builtins is not None: |
paulb@57 | 903 | annotator.process(module, builtins) |
paulb@57 | 904 | else: |
paulb@57 | 905 | annotator.process(module) |
paulb@57 | 906 | |
paulb@57 | 907 | def annotate_all(modules, builtins): |
paulb@57 | 908 | annotate(builtins) |
paulb@57 | 909 | for module in modules: |
paulb@57 | 910 | annotate(module, builtins) |
paulb@57 | 911 | |
paulb@19 | 912 | # vim: tabstop=4 expandtab shiftwidth=4 |