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