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