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@45 | 159 | if locals: |
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@49 | 256 | self.namespace.set_types(self.namespace.temp[index][-1]) |
paulb@24 | 257 | self.annotate(loadtemp) |
paulb@20 | 258 | return loadtemp |
paulb@19 | 259 | |
paulb@19 | 260 | def visitStoreTemp(self, storetemp): |
paulb@21 | 261 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@19 | 262 | index = getattr(storetemp, "index", None) |
paulb@49 | 263 | if not self.namespace.temp.has_key(index): |
paulb@49 | 264 | self.namespace.temp[index] = [] |
paulb@49 | 265 | self.namespace.temp[index].append(self.namespace.types) |
paulb@20 | 266 | return storetemp |
paulb@19 | 267 | |
paulb@19 | 268 | def visitReleaseTemp(self, releasetemp): |
paulb@19 | 269 | index = getattr(releasetemp, "index", None) |
paulb@49 | 270 | self.namespace.temp[index].pop() |
paulb@20 | 271 | return releasetemp |
paulb@20 | 272 | |
paulb@20 | 273 | def visitLoadAttr(self, loadattr): |
paulb@21 | 274 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@20 | 275 | types = [] |
paulb@39 | 276 | accesses = {} |
paulb@49 | 277 | for attr in self.namespace.types: |
paulb@49 | 278 | if not accesses.has_key(attr.type): |
paulb@49 | 279 | accesses[attr.type] = [] |
paulb@49 | 280 | for attribute, accessor in get_attributes(attr.type, loadattr.name): |
paulb@49 | 281 | if attribute is not None: |
paulb@49 | 282 | types.append(attribute) |
paulb@49 | 283 | else: |
paulb@49 | 284 | print "Empty attribute via accessor", accessor |
paulb@49 | 285 | accesses[attr.type].append((attribute, accessor)) |
paulb@49 | 286 | self.namespace.set_types(types) |
paulb@39 | 287 | loadattr.accesses = accesses |
paulb@24 | 288 | self.annotate(loadattr) |
paulb@20 | 289 | return loadattr |
paulb@20 | 290 | |
paulb@20 | 291 | def visitStoreAttr(self, storeattr): |
paulb@21 | 292 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@49 | 293 | expr = self.namespace.types |
paulb@21 | 294 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@57 | 295 | writes = {} |
paulb@49 | 296 | for attr in self.namespace.types: |
paulb@49 | 297 | if attr is None: |
paulb@49 | 298 | print "Empty attribute storage attempt" |
paulb@49 | 299 | continue |
paulb@49 | 300 | attr.type.namespace.store(storeattr.name, expr) |
paulb@57 | 301 | writes[attr.type] = attr.type.namespace.load(storeattr.name) |
paulb@57 | 302 | storeattr.writes = writes |
paulb@20 | 303 | return storeattr |
paulb@19 | 304 | |
paulb@34 | 305 | def visitConditional(self, conditional): |
paulb@34 | 306 | |
paulb@34 | 307 | # Conditionals keep local namespace changes isolated. |
paulb@34 | 308 | # With Return nodes inside the body/else sections, the changes are |
paulb@34 | 309 | # communicated to the caller. |
paulb@34 | 310 | |
paulb@34 | 311 | conditional.test = self.dispatch(conditional.test) |
paulb@34 | 312 | saved_namespace = self.namespace |
paulb@34 | 313 | |
paulb@34 | 314 | self.namespace = Namespace() |
paulb@34 | 315 | self.namespace.merge_namespace(saved_namespace) |
paulb@34 | 316 | conditional.body = self.dispatches(conditional.body) |
paulb@38 | 317 | body_namespace = self.namespace |
paulb@34 | 318 | |
paulb@34 | 319 | self.namespace = Namespace() |
paulb@34 | 320 | self.namespace.merge_namespace(saved_namespace) |
paulb@34 | 321 | conditional.else_ = self.dispatches(conditional.else_) |
paulb@38 | 322 | else_namespace = self.namespace |
paulb@34 | 323 | |
paulb@38 | 324 | self.namespace = Namespace() |
paulb@38 | 325 | self.namespace.merge_namespace(body_namespace) |
paulb@38 | 326 | self.namespace.merge_namespace(else_namespace) |
paulb@34 | 327 | return conditional |
paulb@34 | 328 | |
paulb@30 | 329 | def visitReturn(self, return_): |
paulb@30 | 330 | if hasattr(return_, "expr"): |
paulb@30 | 331 | return_.expr = self.dispatch(return_.expr) |
paulb@49 | 332 | self.namespace.returns += self.namespace.types |
paulb@68 | 333 | self.annotate(return_) |
paulb@49 | 334 | self.namespace.snapshot() |
paulb@30 | 335 | return return_ |
paulb@30 | 336 | |
paulb@24 | 337 | def visitInvoke(self, invoke): |
paulb@24 | 338 | invoke.expr = self.dispatch(invoke.expr) |
paulb@49 | 339 | invocation_types = self.namespace.types |
paulb@25 | 340 | |
paulb@67 | 341 | if isinstance(invoke, InvokeFunction): |
paulb@67 | 342 | self.process_args(invoke) |
paulb@24 | 343 | |
paulb@34 | 344 | # Now locate and invoke the subprogram. This can be complicated because |
paulb@34 | 345 | # the target may be a class or object, and there may be many different |
paulb@34 | 346 | # related subprograms. |
paulb@24 | 347 | |
paulb@68 | 348 | invocations = [] |
paulb@34 | 349 | |
paulb@39 | 350 | # Visit each callable in turn, finding subprograms. |
paulb@34 | 351 | |
paulb@49 | 352 | for attr in invocation_types: |
paulb@30 | 353 | |
paulb@34 | 354 | # Deal with class invocations by providing instance objects. |
paulb@34 | 355 | # Here, each class is queried for the __init__ method, which may |
paulb@34 | 356 | # exist for some combinations of classes in a hierarchy but not for |
paulb@34 | 357 | # others. |
paulb@34 | 358 | |
paulb@49 | 359 | if isinstance(attr.type, Class): |
paulb@49 | 360 | attributes = get_attributes(attr.type, "__init__") |
paulb@30 | 361 | |
paulb@34 | 362 | # Deal with object invocations by using __call__ methods. |
paulb@34 | 363 | |
paulb@49 | 364 | elif isinstance(attr.type, Instance): |
paulb@49 | 365 | attributes = get_attributes(attr.type, "__call__") |
paulb@34 | 366 | |
paulb@34 | 367 | # Normal functions or methods are more straightforward. |
paulb@39 | 368 | # Here, we model them using an attribute with no context and with |
paulb@39 | 369 | # no associated accessor. |
paulb@34 | 370 | |
paulb@27 | 371 | else: |
paulb@49 | 372 | attributes = [(attr, None)] |
paulb@39 | 373 | |
paulb@39 | 374 | # Inspect each attribute and extract the subprogram. |
paulb@25 | 375 | |
paulb@39 | 376 | for attribute, accessor in attributes: |
paulb@38 | 377 | |
paulb@39 | 378 | # If a class is involved, presume that it must create a new |
paulb@39 | 379 | # object. |
paulb@39 | 380 | |
paulb@49 | 381 | if isinstance(attr.type, Class): |
paulb@53 | 382 | |
paulb@53 | 383 | # Instantiate the class. |
paulb@53 | 384 | # NOTE: Should probably only allocate a single instance. |
paulb@53 | 385 | |
paulb@53 | 386 | instance = Instance() |
paulb@53 | 387 | instance.namespace = Namespace() |
paulb@53 | 388 | instance.namespace.store("__class__", [attr.type]) |
paulb@53 | 389 | |
paulb@53 | 390 | # For instantiations, switch the context. |
paulb@53 | 391 | |
paulb@53 | 392 | if attribute is not None: |
paulb@53 | 393 | attribute = Attribute(instance, attribute.type) |
paulb@53 | 394 | |
paulb@53 | 395 | # Skip cases where no callable is found. |
paulb@53 | 396 | |
paulb@53 | 397 | if attribute is not None: |
paulb@53 | 398 | |
paulb@53 | 399 | # If a subprogram is defined, invoke it. |
paulb@53 | 400 | |
paulb@53 | 401 | self.invoke_subprogram(invoke, attribute) |
paulb@68 | 402 | if attribute.type not in invocations: |
paulb@68 | 403 | invocations.append(attribute.type) |
paulb@53 | 404 | |
paulb@53 | 405 | else: |
paulb@68 | 406 | print "Invocation type is None for", accessor |
paulb@53 | 407 | |
paulb@53 | 408 | if isinstance(attr.type, Class): |
paulb@53 | 409 | |
paulb@53 | 410 | # Associate the instance with the result of this invocation. |
paulb@53 | 411 | |
paulb@54 | 412 | self.namespace.set_types([Attribute(None, instance)]) |
paulb@39 | 413 | self.annotate(invoke) |
paulb@39 | 414 | |
paulb@38 | 415 | invoke.invocations = invocations |
paulb@24 | 416 | |
paulb@24 | 417 | return invoke |
paulb@24 | 418 | |
paulb@67 | 419 | visitInvokeFunction = visitInvoke |
paulb@67 | 420 | visitInvokeBlock = visitInvoke |
paulb@67 | 421 | |
paulb@25 | 422 | # Utility methods. |
paulb@25 | 423 | |
paulb@34 | 424 | def invoke_subprogram(self, invoke, subprogram): |
paulb@34 | 425 | |
paulb@34 | 426 | """ |
paulb@34 | 427 | Invoke using the given 'invoke' node the given 'subprogram'. |
paulb@34 | 428 | """ |
paulb@34 | 429 | |
paulb@42 | 430 | # Test to see if anything has changed. |
paulb@42 | 431 | |
paulb@42 | 432 | if hasattr(invoke, "syscount") and invoke.syscount == self.system.count: |
paulb@42 | 433 | return |
paulb@42 | 434 | |
paulb@54 | 435 | # Test for context information, making it into a real attribute. |
paulb@34 | 436 | |
paulb@54 | 437 | if subprogram.context is not None: |
paulb@54 | 438 | context = Attribute(None, subprogram.context) |
paulb@34 | 439 | target = subprogram.type |
paulb@34 | 440 | else: |
paulb@34 | 441 | context = None |
paulb@54 | 442 | target = subprogram.type |
paulb@50 | 443 | |
paulb@34 | 444 | # Provide the correct namespace for the invocation. |
paulb@34 | 445 | |
paulb@57 | 446 | if isinstance(invoke, InvokeBlock): |
paulb@34 | 447 | namespace = Namespace() |
paulb@34 | 448 | namespace.merge_namespace(self.namespace) |
paulb@34 | 449 | else: |
paulb@34 | 450 | items = self.make_items(invoke, target, context) |
paulb@34 | 451 | namespace = self.make_namespace(items) |
paulb@34 | 452 | |
paulb@34 | 453 | # Process the subprogram. |
paulb@34 | 454 | |
paulb@53 | 455 | self.process_node(target, namespace) |
paulb@34 | 456 | |
paulb@45 | 457 | # NOTE: Improve and verify this. |
paulb@34 | 458 | |
paulb@53 | 459 | if getattr(target, "returns_value", 0): |
paulb@49 | 460 | self.namespace.set_types(self.last_returns) |
paulb@34 | 461 | self.annotate(invoke) |
paulb@34 | 462 | |
paulb@57 | 463 | if isinstance(invoke, InvokeBlock): |
paulb@45 | 464 | for locals in self.returned_locals: |
paulb@34 | 465 | self.namespace.merge_namespace(locals) |
paulb@34 | 466 | |
paulb@42 | 467 | # Remember the state of the system. |
paulb@42 | 468 | |
paulb@42 | 469 | invoke.syscount = self.system.count |
paulb@42 | 470 | |
paulb@54 | 471 | def process_args(self, invoke): |
paulb@54 | 472 | |
paulb@54 | 473 | # NOTE: Consider initialiser invocation for classes. |
paulb@54 | 474 | |
paulb@54 | 475 | types = [] |
paulb@54 | 476 | args = [] |
paulb@54 | 477 | |
paulb@54 | 478 | # Get type information for regular arguments. |
paulb@54 | 479 | |
paulb@54 | 480 | for arg in invoke.args: |
paulb@54 | 481 | args.append(self.dispatch(arg)) |
paulb@54 | 482 | types.append(self.namespace.types) |
paulb@54 | 483 | |
paulb@54 | 484 | # Get type information for star and dstar arguments. |
paulb@54 | 485 | |
paulb@54 | 486 | if invoke.star is not None: |
paulb@54 | 487 | param, default = invoke.star |
paulb@54 | 488 | default = self.dispatch(default) |
paulb@54 | 489 | invoke.star = param, default |
paulb@54 | 490 | types.append(default.types) |
paulb@54 | 491 | |
paulb@54 | 492 | if invoke.dstar is not None: |
paulb@54 | 493 | param, default = invoke.dstar |
paulb@54 | 494 | default = self.dispatch(default) |
paulb@54 | 495 | invoke.dstar = param, default |
paulb@54 | 496 | types.append(default.types) |
paulb@54 | 497 | |
paulb@54 | 498 | invoke.args = args |
paulb@54 | 499 | |
paulb@34 | 500 | def make_items(self, invocation, subprogram, context): |
paulb@34 | 501 | |
paulb@34 | 502 | """ |
paulb@34 | 503 | Make an items mapping for the 'invocation' of the 'subprogram' using the |
paulb@34 | 504 | given 'context' (which may be None). |
paulb@34 | 505 | """ |
paulb@34 | 506 | |
paulb@34 | 507 | if context is not None: |
paulb@53 | 508 | args = [Self(context)] + invocation.args |
paulb@34 | 509 | else: |
paulb@34 | 510 | args = invocation.args |
paulb@34 | 511 | |
paulb@67 | 512 | # Sort the arguments into positional and keyword arguments. |
paulb@67 | 513 | |
paulb@67 | 514 | pos_args = [] |
paulb@67 | 515 | kw_args = [] |
paulb@67 | 516 | add_kw = 0 |
paulb@67 | 517 | for arg in args: |
paulb@67 | 518 | if not add_kw: |
paulb@67 | 519 | if not isinstance(arg, Keyword): |
paulb@67 | 520 | pos_args.append(arg) |
paulb@67 | 521 | else: |
paulb@67 | 522 | add_kw = 1 |
paulb@67 | 523 | if add_kw: |
paulb@67 | 524 | if isinstance(arg, Keyword): |
paulb@67 | 525 | kw_args.append(arg) |
paulb@67 | 526 | else: |
paulb@67 | 527 | raise AnnotationMessage, "Positional argument appears after keyword arguments in '%s'." % callfunc |
paulb@67 | 528 | |
paulb@24 | 529 | params = subprogram.params |
paulb@24 | 530 | items = [] |
paulb@67 | 531 | star_args = [] |
paulb@24 | 532 | |
paulb@67 | 533 | # Match each positional argument, taking excess arguments as star args. |
paulb@67 | 534 | |
paulb@67 | 535 | for arg in pos_args: |
paulb@67 | 536 | if params: |
paulb@24 | 537 | param, default = params[0] |
paulb@24 | 538 | if arg is None: |
paulb@54 | 539 | arg = default |
paulb@67 | 540 | items.append((param, arg.types)) |
paulb@67 | 541 | params = params[1:] |
paulb@24 | 542 | else: |
paulb@67 | 543 | star_args.append(arg) |
paulb@24 | 544 | |
paulb@24 | 545 | # Collect the remaining defaults. |
paulb@24 | 546 | |
paulb@24 | 547 | while params: |
paulb@24 | 548 | param, default = params[0] |
paulb@67 | 549 | if kw_args.has_key(param): |
paulb@67 | 550 | arg = kw_args[param] |
paulb@67 | 551 | elif default is None: |
paulb@67 | 552 | raise AnnotationMessage, "No argument supplied in '%s' for parameter '%s'." % (subprogram, param) |
paulb@24 | 553 | items.append((param, arg.types)) |
paulb@24 | 554 | params = params[1:] |
paulb@24 | 555 | |
paulb@24 | 556 | # Add star and dstar. |
paulb@24 | 557 | |
paulb@67 | 558 | if star_args or invocation.star is not None: |
paulb@24 | 559 | if subprogram.star is not None: |
paulb@24 | 560 | param, default = subprogram.star |
paulb@24 | 561 | items.append((param, invocation.star.types)) |
paulb@24 | 562 | else: |
paulb@67 | 563 | raise AnnotationMessage, "Invocation provides unwanted *args." |
paulb@24 | 564 | elif subprogram.star is not None: |
paulb@24 | 565 | param, default = subprogram.star |
paulb@54 | 566 | arg = self.dispatch(default) # NOTE: Review reprocessing. |
paulb@54 | 567 | items.append((param, arg.types)) |
paulb@24 | 568 | |
paulb@67 | 569 | if kw_args or invocation.dstar is not None: |
paulb@24 | 570 | if subprogram.dstar is not None: |
paulb@24 | 571 | param, default = subprogram.dstar |
paulb@24 | 572 | items.append((param, invocation.dstar.types)) |
paulb@24 | 573 | else: |
paulb@67 | 574 | raise AnnotationMessage, "Invocation provides unwanted **args." |
paulb@24 | 575 | elif subprogram.dstar is not None: |
paulb@24 | 576 | param, default = subprogram.dstar |
paulb@54 | 577 | arg = self.dispatch(default) # NOTE: Review reprocessing. |
paulb@54 | 578 | items.append((param, arg.types)) |
paulb@24 | 579 | |
paulb@24 | 580 | return items |
paulb@24 | 581 | |
paulb@24 | 582 | def make_namespace(self, items): |
paulb@24 | 583 | namespace = Namespace() |
paulb@24 | 584 | namespace.merge_items(items) |
paulb@24 | 585 | return namespace |
paulb@24 | 586 | |
paulb@48 | 587 | # Namespace-related abstractions. |
paulb@48 | 588 | |
paulb@48 | 589 | class Namespace: |
paulb@48 | 590 | |
paulb@48 | 591 | """ |
paulb@48 | 592 | A local namespace which may either relate to a genuine set of function |
paulb@50 | 593 | locals or the initialisation of a structure or module. |
paulb@48 | 594 | """ |
paulb@48 | 595 | |
paulb@48 | 596 | def __init__(self): |
paulb@50 | 597 | |
paulb@50 | 598 | """ |
paulb@50 | 599 | Initialise the namespace with a mapping of local names to possible |
paulb@50 | 600 | types, a list of return values and of possible returned local |
paulb@50 | 601 | namespaces. The namespace also tracks the "current" types and a mapping |
paulb@50 | 602 | of temporary value names to types. |
paulb@50 | 603 | """ |
paulb@50 | 604 | |
paulb@48 | 605 | self.names = {} |
paulb@49 | 606 | self.returns = [] |
paulb@49 | 607 | self.return_locals = [] |
paulb@49 | 608 | self.temp = {} |
paulb@49 | 609 | self.types = [] |
paulb@49 | 610 | |
paulb@49 | 611 | def set_types(self, types): |
paulb@49 | 612 | self.types = types |
paulb@48 | 613 | |
paulb@48 | 614 | def store(self, name, types): |
paulb@48 | 615 | self.names[name] = types |
paulb@48 | 616 | |
paulb@48 | 617 | def load(self, name): |
paulb@48 | 618 | return self.names[name] |
paulb@48 | 619 | |
paulb@68 | 620 | def merge_namespace(self, namespace): |
paulb@68 | 621 | self.merge_items(namespace.names.items()) |
paulb@68 | 622 | self.returns += namespace.returns |
paulb@68 | 623 | |
paulb@68 | 624 | def merge_items(self, items): |
paulb@68 | 625 | for name, types in items: |
paulb@68 | 626 | self.merge(name, types) |
paulb@68 | 627 | |
paulb@48 | 628 | def merge(self, name, types): |
paulb@48 | 629 | if not self.names.has_key(name): |
paulb@48 | 630 | self.names[name] = types[:] |
paulb@48 | 631 | else: |
paulb@48 | 632 | existing = self.names[name] |
paulb@48 | 633 | for type in types: |
paulb@48 | 634 | if type not in existing: |
paulb@48 | 635 | existing.append(type) |
paulb@48 | 636 | |
paulb@49 | 637 | def snapshot(self): |
paulb@49 | 638 | |
paulb@49 | 639 | "Make a snapshot of the locals and remember them." |
paulb@49 | 640 | |
paulb@49 | 641 | namespace = Namespace() |
paulb@49 | 642 | namespace.merge_namespace(self) |
paulb@49 | 643 | self.return_locals.append(namespace) |
paulb@49 | 644 | |
paulb@48 | 645 | def __repr__(self): |
paulb@48 | 646 | return repr(self.names) |
paulb@48 | 647 | |
paulb@48 | 648 | class Attribute: |
paulb@48 | 649 | |
paulb@48 | 650 | """ |
paulb@48 | 651 | An attribute abstraction, indicating the type of the attribute along with |
paulb@48 | 652 | its context or origin. |
paulb@48 | 653 | """ |
paulb@48 | 654 | |
paulb@48 | 655 | def __init__(self, context, type): |
paulb@48 | 656 | self.context = context |
paulb@48 | 657 | self.type = type |
paulb@48 | 658 | |
paulb@48 | 659 | def __eq__(self, other): |
paulb@48 | 660 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@48 | 661 | |
paulb@48 | 662 | def __repr__(self): |
paulb@54 | 663 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@48 | 664 | |
paulb@53 | 665 | class Self: |
paulb@53 | 666 | def __init__(self, attribute): |
paulb@53 | 667 | self.types = [attribute] |
paulb@53 | 668 | |
paulb@48 | 669 | def find_attributes(structure, name): |
paulb@48 | 670 | |
paulb@48 | 671 | """ |
paulb@48 | 672 | Find for the given 'structure' all attributes for the given 'name', visiting |
paulb@48 | 673 | base classes where appropriate and returning the attributes in order of |
paulb@48 | 674 | descending precedence for all possible base classes. |
paulb@48 | 675 | |
paulb@48 | 676 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 677 | the structure involved in accessing the attribute. |
paulb@48 | 678 | """ |
paulb@48 | 679 | |
paulb@48 | 680 | # First attempt to search the instance/class namespace. |
paulb@48 | 681 | |
paulb@48 | 682 | try: |
paulb@48 | 683 | l = structure.namespace.load(name) |
paulb@48 | 684 | attributes = [] |
paulb@48 | 685 | for attribute in l: |
paulb@48 | 686 | attributes.append((attribute, structure)) |
paulb@48 | 687 | |
paulb@48 | 688 | # If that does not work, attempt to investigate any class or base classes. |
paulb@48 | 689 | |
paulb@48 | 690 | except KeyError: |
paulb@48 | 691 | attributes = [] |
paulb@48 | 692 | |
paulb@48 | 693 | # Investigate any instance's implementing class. |
paulb@48 | 694 | |
paulb@48 | 695 | if isinstance(structure, Instance): |
paulb@48 | 696 | for cls in structure.namespace.load("__class__"): |
paulb@50 | 697 | l = get_attributes(cls, name) |
paulb@48 | 698 | for attribute in l: |
paulb@48 | 699 | if attribute not in attributes: |
paulb@48 | 700 | attributes.append(attribute) |
paulb@48 | 701 | |
paulb@48 | 702 | # Investigate any class's base classes. |
paulb@48 | 703 | |
paulb@48 | 704 | elif isinstance(structure, Class): |
paulb@48 | 705 | |
paulb@48 | 706 | # If no base classes exist, return an indicator that no attribute |
paulb@48 | 707 | # exists. |
paulb@48 | 708 | |
paulb@48 | 709 | if not structure.base_refs: |
paulb@48 | 710 | return [(None, structure)] |
paulb@48 | 711 | |
paulb@48 | 712 | # Otherwise, find all possible base classes. |
paulb@48 | 713 | |
paulb@48 | 714 | for base_refs in structure.base_refs: |
paulb@48 | 715 | base_attributes = [] |
paulb@48 | 716 | |
paulb@48 | 717 | # For each base class, find attributes either in the base |
paulb@48 | 718 | # class or its own base classes. |
paulb@48 | 719 | |
paulb@48 | 720 | for base_ref in base_refs: |
paulb@50 | 721 | l = get_attributes(base_ref, name) |
paulb@48 | 722 | for attribute in l: |
paulb@48 | 723 | if attribute not in base_attributes: |
paulb@48 | 724 | base_attributes.append(attribute) |
paulb@48 | 725 | |
paulb@48 | 726 | attributes += base_attributes |
paulb@48 | 727 | |
paulb@49 | 728 | return attributes |
paulb@48 | 729 | |
paulb@48 | 730 | def get_attributes(structure, name): |
paulb@48 | 731 | |
paulb@48 | 732 | """ |
paulb@48 | 733 | Return all possible attributes for the given 'structure' having the given |
paulb@48 | 734 | 'name', wrapping each attribute in an Attribute object which includes |
paulb@48 | 735 | context information for the attribute access. |
paulb@48 | 736 | |
paulb@48 | 737 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 738 | the structure involved in accessing the attribute. |
paulb@48 | 739 | """ |
paulb@48 | 740 | |
paulb@48 | 741 | if isinstance(structure, Attribute): |
paulb@48 | 742 | structure = structure.type |
paulb@53 | 743 | results = [] |
paulb@53 | 744 | for attribute, accessor in find_attributes(structure, name): |
paulb@53 | 745 | if attribute is not None and isinstance(structure, Structure): |
paulb@53 | 746 | results.append((Attribute(structure, attribute.type), accessor)) |
paulb@53 | 747 | else: |
paulb@53 | 748 | results.append((attribute, accessor)) |
paulb@53 | 749 | return results |
paulb@48 | 750 | |
paulb@57 | 751 | # Convenience functions. |
paulb@57 | 752 | |
paulb@57 | 753 | def annotate(module, builtins=None): |
paulb@57 | 754 | |
paulb@57 | 755 | """ |
paulb@57 | 756 | Annotate the given 'module', also employing the optional 'builtins' module, |
paulb@57 | 757 | if specified. |
paulb@57 | 758 | """ |
paulb@57 | 759 | |
paulb@57 | 760 | annotator = Annotator() |
paulb@57 | 761 | if builtins is not None: |
paulb@57 | 762 | annotator.process(module, builtins) |
paulb@57 | 763 | else: |
paulb@57 | 764 | annotator.process(module) |
paulb@57 | 765 | |
paulb@57 | 766 | def annotate_all(modules, builtins): |
paulb@57 | 767 | annotate(builtins) |
paulb@57 | 768 | for module in modules: |
paulb@57 | 769 | annotate(module, builtins) |
paulb@57 | 770 | |
paulb@19 | 771 | # vim: tabstop=4 expandtab shiftwidth=4 |