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@115 | 126 | self.namespace = None |
paulb@121 | 127 | self.module = module |
paulb@34 | 128 | |
paulb@34 | 129 | # Give constants their own namespace. |
paulb@34 | 130 | |
paulb@57 | 131 | for value, constant in module.simplifier.constants.items(): |
paulb@34 | 132 | constant.namespace = Namespace() |
paulb@34 | 133 | |
paulb@34 | 134 | # Process the module, supplying builtins if possible. |
paulb@34 | 135 | |
paulb@80 | 136 | self.builtins = builtins |
paulb@45 | 137 | self.global_namespace = Namespace() |
paulb@45 | 138 | |
paulb@57 | 139 | if builtins is not None: |
paulb@57 | 140 | self.builtins_namespace = builtins.namespace |
paulb@34 | 141 | else: |
paulb@45 | 142 | self.builtins_namespace = self.global_namespace |
paulb@34 | 143 | |
paulb@132 | 144 | return self.process_node(module, self.global_namespace) |
paulb@45 | 145 | |
paulb@132 | 146 | def process_node(self, node, locals): |
paulb@24 | 147 | |
paulb@24 | 148 | """ |
paulb@132 | 149 | Process a subprogram or module 'node', indicating the initial 'locals'. |
paulb@34 | 150 | Return an annotated subprogram or module. Note that this method may |
paulb@34 | 151 | mutate nodes in the original program. |
paulb@24 | 152 | """ |
paulb@24 | 153 | |
paulb@115 | 154 | # Record the current subprogram and namespace. |
paulb@115 | 155 | |
paulb@115 | 156 | self.current_subprograms.append(node) |
paulb@115 | 157 | |
paulb@67 | 158 | # Determine the namespace. |
paulb@67 | 159 | |
paulb@132 | 160 | self.current_namespaces.append(self.namespace) |
paulb@132 | 161 | self.namespace = locals |
paulb@45 | 162 | |
paulb@25 | 163 | # Add namespace details to any structure involved. |
paulb@25 | 164 | |
paulb@34 | 165 | if getattr(node, "structure", None) is not None: |
paulb@34 | 166 | node.structure.namespace = Namespace() |
paulb@25 | 167 | |
paulb@30 | 168 | # Initialise bases where appropriate. |
paulb@19 | 169 | |
paulb@30 | 170 | if hasattr(node.structure, "bases"): |
paulb@30 | 171 | base_refs = [] |
paulb@30 | 172 | for base in node.structure.bases: |
paulb@30 | 173 | self.dispatch(base) |
paulb@49 | 174 | base_refs.append(self.namespace.types) |
paulb@30 | 175 | node.structure.base_refs = base_refs |
paulb@30 | 176 | |
paulb@30 | 177 | # Dispatch to the code itself. |
paulb@30 | 178 | |
paulb@49 | 179 | node.namespace = self.namespace |
paulb@20 | 180 | result = self.dispatch(node) |
paulb@49 | 181 | result.namespace = self.namespace |
paulb@49 | 182 | |
paulb@49 | 183 | # Obtain the return values. |
paulb@49 | 184 | |
paulb@49 | 185 | self.last_returns = self.namespace.returns |
paulb@116 | 186 | self.last_raises = self.namespace.raises |
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@132 | 191 | self.namespace = self.current_namespaces.pop() |
paulb@45 | 192 | self.current_subprograms.pop() |
paulb@45 | 193 | |
paulb@20 | 194 | return result |
paulb@20 | 195 | |
paulb@83 | 196 | def annotate(self, node, types=None): |
paulb@24 | 197 | |
paulb@83 | 198 | """ |
paulb@83 | 199 | Annotate the given 'node' in the system, using either the optional |
paulb@83 | 200 | 'types' or the namespace's current type information. |
paulb@83 | 201 | """ |
paulb@24 | 202 | |
paulb@83 | 203 | self.system.annotate(node, types or self.namespace.types) |
paulb@34 | 204 | |
paulb@25 | 205 | # Visitor methods. |
paulb@25 | 206 | |
paulb@19 | 207 | def default(self, node): |
paulb@24 | 208 | |
paulb@24 | 209 | """ |
paulb@24 | 210 | Process the given 'node', given that it does not have a specific |
paulb@24 | 211 | handler. |
paulb@24 | 212 | """ |
paulb@24 | 213 | |
paulb@115 | 214 | raise AnnotationMessage, "Node '%s' not supported." % node |
paulb@20 | 215 | |
paulb@20 | 216 | def dispatch(self, node, *args): |
paulb@34 | 217 | try: |
paulb@34 | 218 | return Visitor.dispatch(self, node, *args) |
paulb@67 | 219 | except AnnotationError, exc: |
paulb@58 | 220 | exc.add(node) |
paulb@34 | 221 | raise |
paulb@67 | 222 | except AnnotationMessage, exc: |
paulb@67 | 223 | raise AnnotationError(exc, node) |
paulb@19 | 224 | |
paulb@110 | 225 | # Program structure/control-flow. |
paulb@110 | 226 | |
paulb@115 | 227 | def visitAssign(self, assign): |
paulb@115 | 228 | assign.code = self.dispatches(assign.code) |
paulb@115 | 229 | return assign |
paulb@115 | 230 | |
paulb@110 | 231 | def visitConditional(self, conditional): |
paulb@19 | 232 | |
paulb@110 | 233 | # Conditionals keep local namespace changes isolated. |
paulb@110 | 234 | # With Return nodes inside the body/else sections, the changes are |
paulb@110 | 235 | # communicated to the caller. |
paulb@19 | 236 | |
paulb@133 | 237 | is_module = self.namespace is self.module.namespace |
paulb@133 | 238 | |
paulb@133 | 239 | # Where the test is closely associated with the body, save the namespace |
paulb@133 | 240 | # before entering the test. |
paulb@133 | 241 | |
paulb@133 | 242 | if conditional.isolate_test: |
paulb@133 | 243 | saved_namespace = self.namespace |
paulb@133 | 244 | self.namespace = Namespace() |
paulb@133 | 245 | if is_module: |
paulb@133 | 246 | self.module.namespace = self.namespace |
paulb@133 | 247 | self.namespace.merge_namespace(saved_namespace) |
paulb@19 | 248 | |
paulb@133 | 249 | conditional.test = self.dispatch(conditional.test) |
paulb@133 | 250 | |
paulb@133 | 251 | # Where the test may affect the body and the else clause, save the |
paulb@133 | 252 | # namespace after processing the test. |
paulb@133 | 253 | |
paulb@133 | 254 | if not conditional.isolate_test: |
paulb@133 | 255 | saved_namespace = self.namespace |
paulb@133 | 256 | self.namespace = Namespace() |
paulb@133 | 257 | if is_module: |
paulb@133 | 258 | self.module.namespace = self.namespace |
paulb@133 | 259 | self.namespace.merge_namespace(saved_namespace) |
paulb@133 | 260 | |
paulb@133 | 261 | # Process the body clause. |
paulb@133 | 262 | |
paulb@110 | 263 | conditional.body = self.dispatches(conditional.body) |
paulb@110 | 264 | body_namespace = self.namespace |
paulb@19 | 265 | |
paulb@133 | 266 | # Use the saved namespace as a template for the else clause. |
paulb@133 | 267 | |
paulb@110 | 268 | self.namespace = Namespace() |
paulb@133 | 269 | if is_module: |
paulb@133 | 270 | self.module.namespace = self.namespace |
paulb@110 | 271 | self.namespace.merge_namespace(saved_namespace) |
paulb@133 | 272 | |
paulb@133 | 273 | # Process the else clause. |
paulb@133 | 274 | |
paulb@110 | 275 | conditional.else_ = self.dispatches(conditional.else_) |
paulb@110 | 276 | else_namespace = self.namespace |
paulb@19 | 277 | |
paulb@133 | 278 | # Merge the body and else namespaces. |
paulb@133 | 279 | |
paulb@110 | 280 | self.namespace = Namespace() |
paulb@133 | 281 | if is_module: |
paulb@133 | 282 | self.module.namespace = self.namespace |
paulb@110 | 283 | self.namespace.merge_namespace(body_namespace) |
paulb@110 | 284 | self.namespace.merge_namespace(else_namespace) |
paulb@110 | 285 | |
paulb@110 | 286 | return conditional |
paulb@110 | 287 | |
paulb@115 | 288 | def visitModule(self, module): |
paulb@115 | 289 | module.code = self.dispatches(module.code) |
paulb@115 | 290 | return module |
paulb@115 | 291 | |
paulb@115 | 292 | def visitPass(self, pass_): |
paulb@115 | 293 | return pass_ |
paulb@115 | 294 | |
paulb@115 | 295 | def visitSubprogram(self, subprogram): |
paulb@115 | 296 | subprogram.code = self.dispatches(subprogram.code) |
paulb@115 | 297 | return subprogram |
paulb@115 | 298 | |
paulb@115 | 299 | def visitTry(self, try_): |
paulb@133 | 300 | is_module = self.namespace is self.module.namespace |
paulb@133 | 301 | |
paulb@115 | 302 | try_.body = self.dispatches(try_.body) |
paulb@133 | 303 | |
paulb@133 | 304 | # Save the namespace from the body. |
paulb@133 | 305 | |
paulb@133 | 306 | body_namespace = Namespace() |
paulb@133 | 307 | body_namespace.merge_namespace(self.namespace) |
paulb@133 | 308 | |
paulb@133 | 309 | # Process the handler. |
paulb@133 | 310 | |
paulb@133 | 311 | if hasattr(try_, "handler"): |
paulb@133 | 312 | try_.handler = self.dispatches(try_.handler) |
paulb@133 | 313 | |
paulb@133 | 314 | # Save the namespace from the handler. |
paulb@133 | 315 | |
paulb@133 | 316 | handler_namespace = Namespace() |
paulb@133 | 317 | handler_namespace.merge_namespace(self.namespace) |
paulb@133 | 318 | |
paulb@133 | 319 | # Remember the raised exceptions encountered so far. |
paulb@133 | 320 | |
paulb@121 | 321 | raises = self.namespace.raises |
paulb@121 | 322 | |
paulb@133 | 323 | # Process the else clause. |
paulb@133 | 324 | |
paulb@133 | 325 | if hasattr(try_, "else_"): |
paulb@133 | 326 | |
paulb@133 | 327 | # Restore the body namespace for the else clause. |
paulb@133 | 328 | |
paulb@133 | 329 | self.namespace = body_namespace |
paulb@133 | 330 | if is_module: |
paulb@133 | 331 | self.module.namespace = self.namespace |
paulb@133 | 332 | |
paulb@133 | 333 | # Empty the raised exceptions for the else clause. |
paulb@121 | 334 | |
paulb@133 | 335 | self.namespace.raises = [] |
paulb@133 | 336 | try_.else_ = self.dispatches(try_.else_) |
paulb@133 | 337 | self.namespace.raises = raises |
paulb@133 | 338 | |
paulb@133 | 339 | # Merge the namespaces. |
paulb@133 | 340 | |
paulb@133 | 341 | self.namespace = Namespace() |
paulb@133 | 342 | if is_module: |
paulb@133 | 343 | self.module.namespace = self.namespace |
paulb@133 | 344 | self.namespace.merge_namespace(body_namespace) |
paulb@133 | 345 | self.namespace.merge_namespace(handler_namespace) |
paulb@133 | 346 | |
paulb@133 | 347 | # Process the finally clause, if any. |
paulb@121 | 348 | |
paulb@115 | 349 | try_.finally_ = self.dispatches(try_.finally_) |
paulb@115 | 350 | return try_ |
paulb@115 | 351 | |
paulb@110 | 352 | # Namespace operations. |
paulb@20 | 353 | |
paulb@133 | 354 | def visitCheckExc(self, checkexc): |
paulb@133 | 355 | checkexc.expr = self.dispatch(checkexc.expr) |
paulb@133 | 356 | expr_types = self.namespace.types |
paulb@133 | 357 | choice_types = [] |
paulb@133 | 358 | choices = [] |
paulb@133 | 359 | for choice in checkexc.choices: |
paulb@133 | 360 | choices.append(self.dispatch(choice)) |
paulb@133 | 361 | choice_types += self.namespace.types |
paulb@133 | 362 | for expr_type in expr_types: |
paulb@133 | 363 | if expr_type.type.get_class() not in choice_types: |
paulb@133 | 364 | self._prune_non_accesses(checkexc.expr, expr_type) |
paulb@133 | 365 | return checkexc |
paulb@133 | 366 | |
paulb@20 | 367 | def visitLoadAttr(self, loadattr): |
paulb@21 | 368 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@20 | 369 | types = [] |
paulb@124 | 370 | non_accesses = [] |
paulb@39 | 371 | accesses = {} |
paulb@49 | 372 | for attr in self.namespace.types: |
paulb@103 | 373 | attributes = get_attributes(attr.type, loadattr.name) |
paulb@103 | 374 | if not attributes: |
paulb@124 | 375 | if not attr.type in non_accesses: |
paulb@124 | 376 | non_accesses.append(attr) |
paulb@131 | 377 | |
paulb@131 | 378 | # Revoke this type from any name involved. |
paulb@131 | 379 | |
paulb@133 | 380 | self._prune_non_accesses(loadattr.expr, attr) |
paulb@131 | 381 | |
paulb@103 | 382 | for attribute, accessor in attributes: |
paulb@49 | 383 | if attribute is not None: |
paulb@49 | 384 | types.append(attribute) |
paulb@79 | 385 | if not accesses.has_key(attr.type): |
paulb@79 | 386 | accesses[attr.type] = [] |
paulb@83 | 387 | if not (attribute, accessor) in accesses[attr.type]: |
paulb@83 | 388 | accesses[attr.type].append((attribute, accessor)) |
paulb@49 | 389 | else: |
paulb@126 | 390 | if not attr in non_accesses: |
paulb@124 | 391 | non_accesses.append(attr) |
paulb@131 | 392 | |
paulb@131 | 393 | # Revoke this type from any name involved. |
paulb@131 | 394 | |
paulb@133 | 395 | self._prune_non_accesses(loadattr.expr, attr) |
paulb@131 | 396 | |
paulb@124 | 397 | if not types: |
paulb@124 | 398 | print "No attribute found for", loadattr.name, "given", self.namespace.types |
paulb@49 | 399 | self.namespace.set_types(types) |
paulb@124 | 400 | loadattr.non_accesses = non_accesses |
paulb@39 | 401 | loadattr.accesses = accesses |
paulb@24 | 402 | self.annotate(loadattr) |
paulb@20 | 403 | return loadattr |
paulb@20 | 404 | |
paulb@133 | 405 | def _prune_non_accesses(self, expr, attr): |
paulb@133 | 406 | if isinstance(expr, LoadName): |
paulb@133 | 407 | self.namespace.revoke(expr.name, attr) |
paulb@133 | 408 | elif isinstance(expr, LoadAttr): |
paulb@133 | 409 | for expr_attr in expr.expr.types: |
paulb@131 | 410 | if hasattr(expr_attr.type, "namespace"): |
paulb@133 | 411 | expr_attr.type.namespace.revoke(expr.name, attr) |
paulb@133 | 412 | elif isinstance(expr, LoadExc): |
paulb@133 | 413 | self.namespace.revoke_exception_type(attr) |
paulb@131 | 414 | |
paulb@115 | 415 | def visitLoadExc(self, loadexc): |
paulb@115 | 416 | self.namespace.types = self.namespace.raises[:] |
paulb@115 | 417 | self.annotate(loadexc) |
paulb@115 | 418 | return loadexc |
paulb@115 | 419 | |
paulb@110 | 420 | def visitLoadName(self, loadname): |
paulb@110 | 421 | self.namespace.set_types(self.namespace.load(loadname.name)) |
paulb@110 | 422 | result = loadname |
paulb@110 | 423 | self.annotate(result) |
paulb@110 | 424 | return result |
paulb@110 | 425 | |
paulb@110 | 426 | def visitLoadRef(self, loadref): |
paulb@110 | 427 | self.namespace.set_types([Attribute(None, loadref.ref)]) |
paulb@110 | 428 | self.annotate(loadref) |
paulb@110 | 429 | return loadref |
paulb@110 | 430 | |
paulb@110 | 431 | def visitLoadTemp(self, loadtemp): |
paulb@110 | 432 | index = getattr(loadtemp, "index", None) |
paulb@110 | 433 | try: |
paulb@110 | 434 | if getattr(loadtemp, "release", 0): |
paulb@110 | 435 | self.namespace.set_types(self.namespace.temp[index].pop()) |
paulb@110 | 436 | else: |
paulb@110 | 437 | self.namespace.set_types(self.namespace.temp[index][-1]) |
paulb@110 | 438 | except KeyError: |
paulb@110 | 439 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@110 | 440 | self.annotate(loadtemp) |
paulb@110 | 441 | return loadtemp |
paulb@110 | 442 | |
paulb@115 | 443 | def visitNot(self, not_): |
paulb@115 | 444 | not_.expr = self.dispatch(not_.expr) |
paulb@115 | 445 | return not_ |
paulb@115 | 446 | |
paulb@112 | 447 | def visitRaise(self, raise_): |
paulb@115 | 448 | if getattr(raise_, "traceback", None) is not None: |
paulb@115 | 449 | raise_.traceback = self.dispatch(raise_.traceback) |
paulb@112 | 450 | raise_.expr = self.dispatch(raise_.expr) |
paulb@112 | 451 | |
paulb@112 | 452 | # Handle bare name exceptions by converting any classes to instances. |
paulb@112 | 453 | |
paulb@112 | 454 | if not isinstance(raise_.expr, InvokeFunction): |
paulb@112 | 455 | raise_.pos_args = [] |
paulb@112 | 456 | raise_.kw_args = {} |
paulb@112 | 457 | raise_.star = None |
paulb@112 | 458 | raise_.dstar = None |
paulb@112 | 459 | types = [] |
paulb@112 | 460 | for attr in self.namespace.types: |
paulb@112 | 461 | if isinstance(attr.type, Class): |
paulb@112 | 462 | self._visitInvoke(raise_, [attr], have_args=0) |
paulb@112 | 463 | types += self.namespace.types |
paulb@112 | 464 | else: |
paulb@112 | 465 | types = self.namespace.types |
paulb@112 | 466 | |
paulb@112 | 467 | combine(self.namespace.raises, types) |
paulb@112 | 468 | return raise_ |
paulb@112 | 469 | |
paulb@110 | 470 | def visitReleaseTemp(self, releasetemp): |
paulb@110 | 471 | index = getattr(releasetemp, "index", None) |
paulb@110 | 472 | try: |
paulb@110 | 473 | self.namespace.temp[index].pop() |
paulb@110 | 474 | except KeyError: |
paulb@110 | 475 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@110 | 476 | except IndexError: |
paulb@110 | 477 | pass #raise AnnotationMessage, "Temporary store index '%s' is empty." % index |
paulb@110 | 478 | return releasetemp |
paulb@110 | 479 | |
paulb@110 | 480 | def visitReturn(self, return_): |
paulb@110 | 481 | if hasattr(return_, "expr"): |
paulb@110 | 482 | return_.expr = self.dispatch(return_.expr) |
paulb@110 | 483 | combine(self.namespace.returns, self.namespace.types) |
paulb@110 | 484 | self.annotate(return_) |
paulb@110 | 485 | self.namespace.snapshot() |
paulb@110 | 486 | return return_ |
paulb@110 | 487 | |
paulb@135 | 488 | visitReturnFromBlock = visitReturn |
paulb@135 | 489 | visitReturnFromFunction = visitReturn |
paulb@135 | 490 | |
paulb@20 | 491 | def visitStoreAttr(self, storeattr): |
paulb@21 | 492 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@49 | 493 | expr = self.namespace.types |
paulb@21 | 494 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@57 | 495 | writes = {} |
paulb@125 | 496 | non_writes = [] |
paulb@49 | 497 | for attr in self.namespace.types: |
paulb@49 | 498 | if attr is None: |
paulb@125 | 499 | if not attr in non_writes: |
paulb@125 | 500 | non_writes.append(attr) |
paulb@49 | 501 | continue |
paulb@140 | 502 | attr.type.namespace.add(storeattr.name, expr) |
paulb@57 | 503 | writes[attr.type] = attr.type.namespace.load(storeattr.name) |
paulb@125 | 504 | if not writes: |
paulb@125 | 505 | print "Unable to store attribute", storeattr.name, "given", self.namespace.types |
paulb@57 | 506 | storeattr.writes = writes |
paulb@125 | 507 | storeattr.non_writes = non_writes |
paulb@20 | 508 | return storeattr |
paulb@19 | 509 | |
paulb@110 | 510 | def visitStoreName(self, storename): |
paulb@110 | 511 | storename.expr = self.dispatch(storename.expr) |
paulb@110 | 512 | self.namespace.store(storename.name, self.namespace.types) |
paulb@110 | 513 | return storename |
paulb@34 | 514 | |
paulb@110 | 515 | def visitStoreTemp(self, storetemp): |
paulb@110 | 516 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@110 | 517 | index = getattr(storetemp, "index", None) |
paulb@110 | 518 | if not self.namespace.temp.has_key(index): |
paulb@110 | 519 | self.namespace.temp[index] = [] |
paulb@110 | 520 | self.namespace.temp[index].append(self.namespace.types) |
paulb@110 | 521 | return storetemp |
paulb@91 | 522 | |
paulb@110 | 523 | # Invocations are a chapter of their own. |
paulb@30 | 524 | |
paulb@112 | 525 | def visitInvokeBlock(self, invoke): |
paulb@112 | 526 | |
paulb@112 | 527 | # First find the callables. |
paulb@112 | 528 | |
paulb@112 | 529 | invoke.expr = self.dispatch(invoke.expr) |
paulb@112 | 530 | invocation_types = self.namespace.types |
paulb@112 | 531 | return self._visitInvoke(invoke, invocation_types, have_args=0) |
paulb@112 | 532 | |
paulb@112 | 533 | def visitInvokeFunction(self, invoke): |
paulb@83 | 534 | |
paulb@83 | 535 | # First find the callables. |
paulb@83 | 536 | |
paulb@24 | 537 | invoke.expr = self.dispatch(invoke.expr) |
paulb@49 | 538 | invocation_types = self.namespace.types |
paulb@25 | 539 | |
paulb@83 | 540 | # Invocation processing starts with making sure that the arguments have |
paulb@83 | 541 | # been processed. |
paulb@83 | 542 | |
paulb@112 | 543 | return self._visitInvoke(invoke, invocation_types, have_args=self.process_args(invoke)) |
paulb@112 | 544 | |
paulb@112 | 545 | def _visitInvoke(self, invoke, invocation_types, have_args): |
paulb@24 | 546 | |
paulb@34 | 547 | # Now locate and invoke the subprogram. This can be complicated because |
paulb@34 | 548 | # the target may be a class or object, and there may be many different |
paulb@34 | 549 | # related subprograms. |
paulb@24 | 550 | |
paulb@68 | 551 | invocations = [] |
paulb@34 | 552 | |
paulb@39 | 553 | # Visit each callable in turn, finding subprograms. |
paulb@34 | 554 | |
paulb@49 | 555 | for attr in invocation_types: |
paulb@30 | 556 | |
paulb@34 | 557 | # Deal with class invocations by providing instance objects. |
paulb@34 | 558 | # Here, each class is queried for the __init__ method, which may |
paulb@34 | 559 | # exist for some combinations of classes in a hierarchy but not for |
paulb@34 | 560 | # others. |
paulb@34 | 561 | |
paulb@49 | 562 | if isinstance(attr.type, Class): |
paulb@49 | 563 | attributes = get_attributes(attr.type, "__init__") |
paulb@30 | 564 | |
paulb@34 | 565 | # Deal with object invocations by using __call__ methods. |
paulb@34 | 566 | |
paulb@49 | 567 | elif isinstance(attr.type, Instance): |
paulb@49 | 568 | attributes = get_attributes(attr.type, "__call__") |
paulb@34 | 569 | |
paulb@34 | 570 | # Normal functions or methods are more straightforward. |
paulb@39 | 571 | # Here, we model them using an attribute with no context and with |
paulb@39 | 572 | # no associated accessor. |
paulb@34 | 573 | |
paulb@27 | 574 | else: |
paulb@49 | 575 | attributes = [(attr, None)] |
paulb@39 | 576 | |
paulb@39 | 577 | # Inspect each attribute and extract the subprogram. |
paulb@25 | 578 | |
paulb@39 | 579 | for attribute, accessor in attributes: |
paulb@38 | 580 | |
paulb@39 | 581 | # If a class is involved, presume that it must create a new |
paulb@39 | 582 | # object. |
paulb@39 | 583 | |
paulb@49 | 584 | if isinstance(attr.type, Class): |
paulb@53 | 585 | |
paulb@53 | 586 | # Instantiate the class. |
paulb@53 | 587 | # NOTE: Should probably only allocate a single instance. |
paulb@53 | 588 | |
paulb@79 | 589 | instance = self.new_instance(invoke, "new", attr.type.full_name(), attr.type) |
paulb@53 | 590 | |
paulb@53 | 591 | # For instantiations, switch the context. |
paulb@53 | 592 | |
paulb@53 | 593 | if attribute is not None: |
paulb@53 | 594 | attribute = Attribute(instance, attribute.type) |
paulb@53 | 595 | |
paulb@53 | 596 | # Skip cases where no callable is found. |
paulb@53 | 597 | |
paulb@53 | 598 | if attribute is not None: |
paulb@53 | 599 | |
paulb@53 | 600 | # If a subprogram is defined, invoke it. |
paulb@53 | 601 | |
paulb@53 | 602 | self.invoke_subprogram(invoke, attribute) |
paulb@68 | 603 | if attribute.type not in invocations: |
paulb@68 | 604 | invocations.append(attribute.type) |
paulb@53 | 605 | |
paulb@79 | 606 | elif not isinstance(attr.type, Class): |
paulb@68 | 607 | print "Invocation type is None for", accessor |
paulb@53 | 608 | |
paulb@112 | 609 | else: |
paulb@79 | 610 | |
paulb@112 | 611 | # Test to see if no arguments were supplied in cases where no |
paulb@112 | 612 | # initialiser was found. |
paulb@112 | 613 | |
paulb@112 | 614 | if have_args: |
paulb@112 | 615 | raise AnnotationMessage, "No initialiser found for '%s' with arguments." % attr.type |
paulb@53 | 616 | |
paulb@112 | 617 | # Special case: initialisation. |
paulb@112 | 618 | |
paulb@112 | 619 | if isinstance(attr.type, Class): |
paulb@53 | 620 | |
paulb@112 | 621 | # Associate the instance with the result of this invocation. |
paulb@112 | 622 | |
paulb@112 | 623 | self.namespace.set_types([Attribute(None, instance)]) |
paulb@112 | 624 | self.annotate(invoke) |
paulb@39 | 625 | |
paulb@83 | 626 | # Remember the invocations that were found, along with the return type |
paulb@83 | 627 | # information. |
paulb@83 | 628 | |
paulb@38 | 629 | invoke.invocations = invocations |
paulb@80 | 630 | self.namespace.set_types(getattr(invoke, "types", [])) |
paulb@24 | 631 | return invoke |
paulb@24 | 632 | |
paulb@25 | 633 | # Utility methods. |
paulb@25 | 634 | |
paulb@79 | 635 | def new_instance(self, node, reason, target, type): |
paulb@79 | 636 | |
paulb@79 | 637 | "Create, on the given 'node', a new instance with the given 'type'." |
paulb@79 | 638 | |
paulb@79 | 639 | if not hasattr(node, "instances"): |
paulb@79 | 640 | node.instances = {} |
paulb@79 | 641 | |
paulb@79 | 642 | if not node.instances.has_key((reason, target, type)): |
paulb@91 | 643 | |
paulb@91 | 644 | # Insist on a single instance per type. |
paulb@91 | 645 | # NOTE: Strategy-dependent instantiation. |
paulb@91 | 646 | |
paulb@91 | 647 | if len(type.instances) == 0: |
paulb@91 | 648 | instance = Instance() |
paulb@91 | 649 | instance.namespace = Namespace() |
paulb@91 | 650 | instance.namespace.store("__class__", [Attribute(None, type)]) |
paulb@91 | 651 | type.instances.append(instance) |
paulb@91 | 652 | else: |
paulb@91 | 653 | instance = type.instances[0] |
paulb@91 | 654 | |
paulb@116 | 655 | #instance = Instance() |
paulb@116 | 656 | #instance.namespace = Namespace() |
paulb@116 | 657 | #instance.namespace.store("__class__", [Attribute(None, type)]) |
paulb@116 | 658 | #type.instances.append(instance) |
paulb@116 | 659 | |
paulb@79 | 660 | node.instances[(reason, target, type)] = instance |
paulb@79 | 661 | |
paulb@79 | 662 | return node.instances[(reason, target, type)] |
paulb@79 | 663 | |
paulb@34 | 664 | def invoke_subprogram(self, invoke, subprogram): |
paulb@34 | 665 | |
paulb@79 | 666 | "Invoke using the given 'invoke' node the given 'subprogram'." |
paulb@34 | 667 | |
paulb@42 | 668 | # Test to see if anything has changed. |
paulb@42 | 669 | |
paulb@42 | 670 | if hasattr(invoke, "syscount") and invoke.syscount == self.system.count: |
paulb@42 | 671 | return |
paulb@42 | 672 | |
paulb@79 | 673 | # Remember the state of the system. |
paulb@79 | 674 | |
paulb@79 | 675 | else: |
paulb@79 | 676 | invoke.syscount = self.system.count |
paulb@79 | 677 | |
paulb@54 | 678 | # Test for context information, making it into a real attribute. |
paulb@34 | 679 | |
paulb@54 | 680 | if subprogram.context is not None: |
paulb@54 | 681 | context = Attribute(None, subprogram.context) |
paulb@34 | 682 | target = subprogram.type |
paulb@34 | 683 | else: |
paulb@34 | 684 | context = None |
paulb@54 | 685 | target = subprogram.type |
paulb@50 | 686 | |
paulb@34 | 687 | # Provide the correct namespace for the invocation. |
paulb@34 | 688 | |
paulb@112 | 689 | if getattr(invoke, "share_locals", 0): |
paulb@34 | 690 | namespace = Namespace() |
paulb@134 | 691 | namespace.merge_namespace(self.namespace, everything=0) |
paulb@132 | 692 | using_module_namespace = self.namespace is self.module.namespace |
paulb@112 | 693 | elif getattr(target, "structure", None): |
paulb@112 | 694 | namespace = Namespace() |
paulb@132 | 695 | using_module_namespace = 0 |
paulb@34 | 696 | else: |
paulb@34 | 697 | items = self.make_items(invoke, target, context) |
paulb@34 | 698 | namespace = self.make_namespace(items) |
paulb@132 | 699 | using_module_namespace = 0 |
paulb@34 | 700 | |
paulb@34 | 701 | # Process the subprogram. |
paulb@132 | 702 | # In order to keep global accesses working, the module namespace must be |
paulb@132 | 703 | # adjusted. |
paulb@132 | 704 | |
paulb@132 | 705 | if using_module_namespace: |
paulb@132 | 706 | self.module.namespace = namespace |
paulb@34 | 707 | |
paulb@53 | 708 | self.process_node(target, namespace) |
paulb@34 | 709 | |
paulb@45 | 710 | # NOTE: Improve and verify this. |
paulb@91 | 711 | # If the invocation returns a value, acquire the return types. |
paulb@34 | 712 | |
paulb@53 | 713 | if getattr(target, "returns_value", 0): |
paulb@49 | 714 | self.namespace.set_types(self.last_returns) |
paulb@34 | 715 | self.annotate(invoke) |
paulb@34 | 716 | |
paulb@132 | 717 | # If it is a normal block, merge the locals. |
paulb@132 | 718 | # This can happen in addition to the above because for things like |
paulb@132 | 719 | # logical expressions, the namespace can be modified whilst values are |
paulb@132 | 720 | # returned as results. |
paulb@91 | 721 | |
paulb@132 | 722 | if getattr(invoke, "share_locals", 0): |
paulb@132 | 723 | self.namespace.reset() |
paulb@135 | 724 | |
paulb@135 | 725 | # Merge the locals snapshots. |
paulb@135 | 726 | |
paulb@45 | 727 | for locals in self.returned_locals: |
paulb@135 | 728 | |
paulb@135 | 729 | # For blocks returning values (such as operations), do not merge |
paulb@135 | 730 | # snapshots or results. |
paulb@135 | 731 | |
paulb@135 | 732 | if getattr(target, "returns_value", 0): |
paulb@135 | 733 | self.namespace.merge_namespace(locals, everything=0) |
paulb@135 | 734 | |
paulb@135 | 735 | # For blocks not returning values (such as loops), merge |
paulb@135 | 736 | # snapshots and results since they contain details of genuine |
paulb@135 | 737 | # returns. |
paulb@135 | 738 | |
paulb@135 | 739 | else: |
paulb@135 | 740 | self.namespace.merge_namespace(locals) |
paulb@34 | 741 | |
paulb@116 | 742 | # Incorporate any raised exceptions. |
paulb@116 | 743 | |
paulb@116 | 744 | combine(self.namespace.raises, self.last_raises) |
paulb@116 | 745 | |
paulb@132 | 746 | # In order to keep global accesses working, the module namespace must be |
paulb@132 | 747 | # adjusted. |
paulb@132 | 748 | |
paulb@132 | 749 | if using_module_namespace: |
paulb@132 | 750 | self.module.namespace = self.namespace |
paulb@132 | 751 | |
paulb@83 | 752 | def process_args(self, invocation): |
paulb@83 | 753 | |
paulb@112 | 754 | """ |
paulb@112 | 755 | Process the arguments associated with an 'invocation'. Return whether |
paulb@112 | 756 | any arguments were processed. |
paulb@112 | 757 | """ |
paulb@54 | 758 | |
paulb@93 | 759 | invocation.pos_args = self.dispatches(invocation.pos_args) |
paulb@93 | 760 | invocation.kw_args = self.dispatch_dict(invocation.kw_args) |
paulb@54 | 761 | |
paulb@54 | 762 | # Get type information for star and dstar arguments. |
paulb@54 | 763 | |
paulb@83 | 764 | if invocation.star is not None: |
paulb@83 | 765 | param, default = invocation.star |
paulb@54 | 766 | default = self.dispatch(default) |
paulb@83 | 767 | invocation.star = param, default |
paulb@54 | 768 | |
paulb@83 | 769 | if invocation.dstar is not None: |
paulb@83 | 770 | param, default = invocation.dstar |
paulb@54 | 771 | default = self.dispatch(default) |
paulb@83 | 772 | invocation.dstar = param, default |
paulb@54 | 773 | |
paulb@112 | 774 | if invocation.pos_args or invocation.kw_args or invocation.star or invocation.dstar: |
paulb@112 | 775 | return 1 |
paulb@112 | 776 | else: |
paulb@112 | 777 | return 0 |
paulb@112 | 778 | |
paulb@34 | 779 | def make_items(self, invocation, subprogram, context): |
paulb@34 | 780 | |
paulb@34 | 781 | """ |
paulb@34 | 782 | Make an items mapping for the 'invocation' of the 'subprogram' using the |
paulb@34 | 783 | given 'context' (which may be None). |
paulb@34 | 784 | """ |
paulb@34 | 785 | |
paulb@34 | 786 | if context is not None: |
paulb@93 | 787 | pos_args = [Self(context)] + invocation.pos_args |
paulb@34 | 788 | else: |
paulb@93 | 789 | pos_args = invocation.pos_args |
paulb@93 | 790 | kw_args = invocation.kw_args |
paulb@34 | 791 | |
paulb@67 | 792 | # Sort the arguments into positional and keyword arguments. |
paulb@67 | 793 | |
paulb@24 | 794 | params = subprogram.params |
paulb@24 | 795 | items = [] |
paulb@67 | 796 | star_args = [] |
paulb@24 | 797 | |
paulb@67 | 798 | # Match each positional argument, taking excess arguments as star args. |
paulb@67 | 799 | |
paulb@67 | 800 | for arg in pos_args: |
paulb@67 | 801 | if params: |
paulb@24 | 802 | param, default = params[0] |
paulb@24 | 803 | if arg is None: |
paulb@54 | 804 | arg = default |
paulb@140 | 805 | if hasattr(arg, "types"): |
paulb@140 | 806 | items.append((param, arg.types)) |
paulb@140 | 807 | else: |
paulb@140 | 808 | items.append((param, [])) # Annotation has not succeeded. |
paulb@67 | 809 | params = params[1:] |
paulb@24 | 810 | else: |
paulb@67 | 811 | star_args.append(arg) |
paulb@24 | 812 | |
paulb@24 | 813 | # Collect the remaining defaults. |
paulb@24 | 814 | |
paulb@24 | 815 | while params: |
paulb@24 | 816 | param, default = params[0] |
paulb@67 | 817 | if kw_args.has_key(param): |
paulb@67 | 818 | arg = kw_args[param] |
paulb@73 | 819 | del kw_args[param] |
paulb@93 | 820 | elif default is not None: |
paulb@93 | 821 | arg = self.dispatch(default) |
paulb@93 | 822 | else: |
paulb@67 | 823 | raise AnnotationMessage, "No argument supplied in '%s' for parameter '%s'." % (subprogram, param) |
paulb@140 | 824 | if hasattr(arg, "types"): |
paulb@140 | 825 | items.append((param, arg.types)) |
paulb@140 | 826 | else: |
paulb@140 | 827 | items.append((param, [])) # Annotation has not succeeded. |
paulb@24 | 828 | params = params[1:] |
paulb@24 | 829 | |
paulb@93 | 830 | dstar_args = kw_args.values() |
paulb@73 | 831 | |
paulb@73 | 832 | # Construct temporary objects. |
paulb@73 | 833 | |
paulb@73 | 834 | if star_args: |
paulb@80 | 835 | star_invocation = self.make_star_args(invocation, subprogram, star_args) |
paulb@80 | 836 | self.dispatch(star_invocation) |
paulb@80 | 837 | star_types = star_invocation.types |
paulb@73 | 838 | else: |
paulb@73 | 839 | star_types = None |
paulb@73 | 840 | |
paulb@73 | 841 | if dstar_args: |
paulb@83 | 842 | dstar_invocation = self.make_dstar_args(invocation, subprogram, dstar_args) # NOTE: To be written! |
paulb@80 | 843 | self.dispatch(dstar_invocation) |
paulb@80 | 844 | dstar_types = dstar_invocation.types |
paulb@73 | 845 | else: |
paulb@73 | 846 | dstar_types = None |
paulb@73 | 847 | |
paulb@73 | 848 | # NOTE: Merge the objects properly. |
paulb@73 | 849 | |
paulb@73 | 850 | star_types = star_types or invocation.star and invocation.star.types |
paulb@73 | 851 | dstar_types = dstar_types or invocation.dstar and invocation.dstar.types |
paulb@73 | 852 | |
paulb@24 | 853 | # Add star and dstar. |
paulb@24 | 854 | |
paulb@73 | 855 | if star_types is not None: |
paulb@24 | 856 | if subprogram.star is not None: |
paulb@24 | 857 | param, default = subprogram.star |
paulb@73 | 858 | items.append((param, star_types)) |
paulb@24 | 859 | else: |
paulb@67 | 860 | raise AnnotationMessage, "Invocation provides unwanted *args." |
paulb@24 | 861 | elif subprogram.star is not None: |
paulb@24 | 862 | param, default = subprogram.star |
paulb@54 | 863 | arg = self.dispatch(default) # NOTE: Review reprocessing. |
paulb@54 | 864 | items.append((param, arg.types)) |
paulb@24 | 865 | |
paulb@73 | 866 | if dstar_types is not None: |
paulb@24 | 867 | if subprogram.dstar is not None: |
paulb@24 | 868 | param, default = subprogram.dstar |
paulb@73 | 869 | items.append((param, dstar_types)) |
paulb@24 | 870 | else: |
paulb@67 | 871 | raise AnnotationMessage, "Invocation provides unwanted **args." |
paulb@24 | 872 | elif subprogram.dstar is not None: |
paulb@24 | 873 | param, default = subprogram.dstar |
paulb@54 | 874 | arg = self.dispatch(default) # NOTE: Review reprocessing. |
paulb@54 | 875 | items.append((param, arg.types)) |
paulb@24 | 876 | |
paulb@83 | 877 | # Record the parameter types. |
paulb@83 | 878 | |
paulb@132 | 879 | if not hasattr(subprogram, "paramtypes"): |
paulb@132 | 880 | subprogram.paramtypes = {} |
paulb@132 | 881 | |
paulb@83 | 882 | for param, types in items: |
paulb@132 | 883 | if not subprogram.paramtypes.has_key(param): |
paulb@132 | 884 | subprogram.paramtypes[param] = [] |
paulb@132 | 885 | combine(subprogram.paramtypes[param], types) |
paulb@83 | 886 | |
paulb@24 | 887 | return items |
paulb@24 | 888 | |
paulb@80 | 889 | def make_star_args(self, invocation, subprogram, star_args): |
paulb@80 | 890 | |
paulb@80 | 891 | "Make a subprogram which initialises a list containing 'star_args'." |
paulb@80 | 892 | |
paulb@80 | 893 | if not hasattr(invocation, "stars"): |
paulb@80 | 894 | invocation.stars = {} |
paulb@80 | 895 | |
paulb@80 | 896 | if not invocation.stars.has_key(subprogram.full_name()): |
paulb@80 | 897 | code=[ |
paulb@80 | 898 | StoreTemp( |
paulb@80 | 899 | expr=InvokeFunction( |
paulb@80 | 900 | expr=LoadAttr( |
paulb@80 | 901 | expr=LoadRef( |
paulb@80 | 902 | ref=self.builtins |
paulb@80 | 903 | ), |
paulb@80 | 904 | name="list", |
paulb@80 | 905 | nstype="module", |
paulb@80 | 906 | ), |
paulb@80 | 907 | args=[], |
paulb@80 | 908 | star=None, |
paulb@80 | 909 | dstar=None |
paulb@80 | 910 | ) |
paulb@80 | 911 | ) |
paulb@80 | 912 | ] |
paulb@80 | 913 | |
paulb@80 | 914 | for arg in star_args: |
paulb@80 | 915 | code.append( |
paulb@80 | 916 | InvokeFunction( |
paulb@80 | 917 | expr=LoadAttr( |
paulb@80 | 918 | expr=LoadTemp(), |
paulb@80 | 919 | name="append" |
paulb@80 | 920 | ), |
paulb@80 | 921 | args=[arg], |
paulb@80 | 922 | star=None, |
paulb@80 | 923 | dstar=None |
paulb@80 | 924 | ) |
paulb@80 | 925 | ) |
paulb@80 | 926 | |
paulb@80 | 927 | code += [ |
paulb@80 | 928 | Return(expr=LoadTemp(release=1)) |
paulb@80 | 929 | ] |
paulb@80 | 930 | |
paulb@80 | 931 | invocation.stars[subprogram.full_name()] = InvokeBlock( |
paulb@80 | 932 | produces_result=1, |
paulb@80 | 933 | expr=LoadRef( |
paulb@80 | 934 | ref=Subprogram( |
paulb@80 | 935 | name=None, |
paulb@80 | 936 | returns_value=1, |
paulb@80 | 937 | params=[], |
paulb@80 | 938 | star=None, |
paulb@80 | 939 | dstar=None, |
paulb@80 | 940 | code=code |
paulb@80 | 941 | ) |
paulb@80 | 942 | ) |
paulb@80 | 943 | ) |
paulb@80 | 944 | |
paulb@80 | 945 | return invocation.stars[subprogram.full_name()] |
paulb@80 | 946 | |
paulb@24 | 947 | def make_namespace(self, items): |
paulb@24 | 948 | namespace = Namespace() |
paulb@24 | 949 | namespace.merge_items(items) |
paulb@24 | 950 | return namespace |
paulb@24 | 951 | |
paulb@48 | 952 | # Namespace-related abstractions. |
paulb@48 | 953 | |
paulb@48 | 954 | class Namespace: |
paulb@48 | 955 | |
paulb@48 | 956 | """ |
paulb@48 | 957 | A local namespace which may either relate to a genuine set of function |
paulb@50 | 958 | locals or the initialisation of a structure or module. |
paulb@48 | 959 | """ |
paulb@48 | 960 | |
paulb@48 | 961 | def __init__(self): |
paulb@50 | 962 | |
paulb@50 | 963 | """ |
paulb@50 | 964 | Initialise the namespace with a mapping of local names to possible |
paulb@50 | 965 | types, a list of return values and of possible returned local |
paulb@50 | 966 | namespaces. The namespace also tracks the "current" types and a mapping |
paulb@50 | 967 | of temporary value names to types. |
paulb@50 | 968 | """ |
paulb@50 | 969 | |
paulb@48 | 970 | self.names = {} |
paulb@49 | 971 | self.returns = [] |
paulb@49 | 972 | self.return_locals = [] |
paulb@112 | 973 | self.raises = [] |
paulb@49 | 974 | self.temp = {} |
paulb@49 | 975 | self.types = [] |
paulb@49 | 976 | |
paulb@49 | 977 | def set_types(self, types): |
paulb@49 | 978 | self.types = types |
paulb@48 | 979 | |
paulb@140 | 980 | def add(self, name, types): |
paulb@140 | 981 | if self.names.has_key(name): |
paulb@140 | 982 | combine(self.names[name], types) |
paulb@140 | 983 | else: |
paulb@140 | 984 | self.store(name, types) |
paulb@140 | 985 | |
paulb@48 | 986 | def store(self, name, types): |
paulb@48 | 987 | self.names[name] = types |
paulb@48 | 988 | |
paulb@86 | 989 | __setitem__ = store |
paulb@74 | 990 | |
paulb@48 | 991 | def load(self, name): |
paulb@48 | 992 | return self.names[name] |
paulb@48 | 993 | |
paulb@86 | 994 | __getitem__ = load |
paulb@74 | 995 | |
paulb@131 | 996 | def revoke(self, name, type): |
paulb@131 | 997 | self.names[name].remove(type) |
paulb@131 | 998 | |
paulb@133 | 999 | def revoke_exception_type(self, type): |
paulb@133 | 1000 | self.raises.remove(type) |
paulb@133 | 1001 | |
paulb@134 | 1002 | def merge_namespace(self, namespace, everything=1): |
paulb@68 | 1003 | self.merge_items(namespace.names.items()) |
paulb@134 | 1004 | if everything: |
paulb@134 | 1005 | combine(self.returns, namespace.returns) |
paulb@132 | 1006 | combine(self.return_locals, namespace.return_locals) |
paulb@115 | 1007 | combine(self.raises, namespace.raises) |
paulb@134 | 1008 | for name, values in namespace.temp.items(): |
paulb@134 | 1009 | if values: |
paulb@134 | 1010 | if not self.temp.has_key(name) or not self.temp[name]: |
paulb@134 | 1011 | self.temp[name] = [[]] |
paulb@134 | 1012 | combine(self.temp[name][-1], values[-1]) |
paulb@68 | 1013 | |
paulb@68 | 1014 | def merge_items(self, items): |
paulb@68 | 1015 | for name, types in items: |
paulb@68 | 1016 | self.merge(name, types) |
paulb@68 | 1017 | |
paulb@48 | 1018 | def merge(self, name, types): |
paulb@48 | 1019 | if not self.names.has_key(name): |
paulb@48 | 1020 | self.names[name] = types[:] |
paulb@48 | 1021 | else: |
paulb@48 | 1022 | existing = self.names[name] |
paulb@73 | 1023 | combine(existing, types) |
paulb@48 | 1024 | |
paulb@49 | 1025 | def snapshot(self): |
paulb@49 | 1026 | |
paulb@49 | 1027 | "Make a snapshot of the locals and remember them." |
paulb@49 | 1028 | |
paulb@49 | 1029 | namespace = Namespace() |
paulb@49 | 1030 | namespace.merge_namespace(self) |
paulb@49 | 1031 | self.return_locals.append(namespace) |
paulb@49 | 1032 | |
paulb@132 | 1033 | def reset(self): |
paulb@132 | 1034 | |
paulb@132 | 1035 | "Reset a namespace in preparation for merging with returned locals." |
paulb@132 | 1036 | |
paulb@132 | 1037 | self.names = {} |
paulb@132 | 1038 | |
paulb@48 | 1039 | def __repr__(self): |
paulb@48 | 1040 | return repr(self.names) |
paulb@48 | 1041 | |
paulb@48 | 1042 | class Attribute: |
paulb@48 | 1043 | |
paulb@48 | 1044 | """ |
paulb@48 | 1045 | An attribute abstraction, indicating the type of the attribute along with |
paulb@48 | 1046 | its context or origin. |
paulb@48 | 1047 | """ |
paulb@48 | 1048 | |
paulb@48 | 1049 | def __init__(self, context, type): |
paulb@48 | 1050 | self.context = context |
paulb@48 | 1051 | self.type = type |
paulb@48 | 1052 | |
paulb@48 | 1053 | def __eq__(self, other): |
paulb@48 | 1054 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@48 | 1055 | |
paulb@48 | 1056 | def __repr__(self): |
paulb@54 | 1057 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@48 | 1058 | |
paulb@53 | 1059 | class Self: |
paulb@91 | 1060 | |
paulb@116 | 1061 | """ |
paulb@116 | 1062 | A program node encapsulating object/context information in an argument list. |
paulb@116 | 1063 | This is not particularly like Attribute, Class, Instance or other such |
paulb@116 | 1064 | things, since it actually appears in the program representation. |
paulb@116 | 1065 | """ |
paulb@91 | 1066 | |
paulb@53 | 1067 | def __init__(self, attribute): |
paulb@53 | 1068 | self.types = [attribute] |
paulb@53 | 1069 | |
paulb@73 | 1070 | def combine(target, additions): |
paulb@91 | 1071 | |
paulb@91 | 1072 | """ |
paulb@91 | 1073 | Merge into the 'target' sequence the given 'additions', preventing duplicate |
paulb@91 | 1074 | items. |
paulb@91 | 1075 | """ |
paulb@91 | 1076 | |
paulb@73 | 1077 | for addition in additions: |
paulb@73 | 1078 | if addition not in target: |
paulb@73 | 1079 | target.append(addition) |
paulb@73 | 1080 | |
paulb@48 | 1081 | def find_attributes(structure, name): |
paulb@48 | 1082 | |
paulb@48 | 1083 | """ |
paulb@48 | 1084 | Find for the given 'structure' all attributes for the given 'name', visiting |
paulb@48 | 1085 | base classes where appropriate and returning the attributes in order of |
paulb@48 | 1086 | descending precedence for all possible base classes. |
paulb@48 | 1087 | |
paulb@48 | 1088 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1089 | the structure involved in accessing the attribute. |
paulb@48 | 1090 | """ |
paulb@48 | 1091 | |
paulb@48 | 1092 | # First attempt to search the instance/class namespace. |
paulb@48 | 1093 | |
paulb@48 | 1094 | try: |
paulb@48 | 1095 | l = structure.namespace.load(name) |
paulb@48 | 1096 | attributes = [] |
paulb@48 | 1097 | for attribute in l: |
paulb@48 | 1098 | attributes.append((attribute, structure)) |
paulb@48 | 1099 | |
paulb@48 | 1100 | # If that does not work, attempt to investigate any class or base classes. |
paulb@48 | 1101 | |
paulb@48 | 1102 | except KeyError: |
paulb@48 | 1103 | attributes = [] |
paulb@48 | 1104 | |
paulb@48 | 1105 | # Investigate any instance's implementing class. |
paulb@48 | 1106 | |
paulb@48 | 1107 | if isinstance(structure, Instance): |
paulb@73 | 1108 | for attr in structure.namespace.load("__class__"): |
paulb@73 | 1109 | cls = attr.type |
paulb@50 | 1110 | l = get_attributes(cls, name) |
paulb@73 | 1111 | combine(attributes, l) |
paulb@48 | 1112 | |
paulb@48 | 1113 | # Investigate any class's base classes. |
paulb@48 | 1114 | |
paulb@48 | 1115 | elif isinstance(structure, Class): |
paulb@48 | 1116 | |
paulb@48 | 1117 | # If no base classes exist, return an indicator that no attribute |
paulb@48 | 1118 | # exists. |
paulb@48 | 1119 | |
paulb@48 | 1120 | if not structure.base_refs: |
paulb@48 | 1121 | return [(None, structure)] |
paulb@48 | 1122 | |
paulb@48 | 1123 | # Otherwise, find all possible base classes. |
paulb@48 | 1124 | |
paulb@48 | 1125 | for base_refs in structure.base_refs: |
paulb@48 | 1126 | base_attributes = [] |
paulb@48 | 1127 | |
paulb@48 | 1128 | # For each base class, find attributes either in the base |
paulb@48 | 1129 | # class or its own base classes. |
paulb@48 | 1130 | |
paulb@48 | 1131 | for base_ref in base_refs: |
paulb@50 | 1132 | l = get_attributes(base_ref, name) |
paulb@73 | 1133 | combine(base_attributes, l) |
paulb@48 | 1134 | |
paulb@73 | 1135 | combine(attributes, base_attributes) |
paulb@48 | 1136 | |
paulb@49 | 1137 | return attributes |
paulb@48 | 1138 | |
paulb@48 | 1139 | def get_attributes(structure, name): |
paulb@48 | 1140 | |
paulb@48 | 1141 | """ |
paulb@48 | 1142 | Return all possible attributes for the given 'structure' having the given |
paulb@48 | 1143 | 'name', wrapping each attribute in an Attribute object which includes |
paulb@48 | 1144 | context information for the attribute access. |
paulb@48 | 1145 | |
paulb@48 | 1146 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1147 | the structure involved in accessing the attribute. |
paulb@48 | 1148 | """ |
paulb@48 | 1149 | |
paulb@48 | 1150 | if isinstance(structure, Attribute): |
paulb@48 | 1151 | structure = structure.type |
paulb@53 | 1152 | results = [] |
paulb@53 | 1153 | for attribute, accessor in find_attributes(structure, name): |
paulb@53 | 1154 | if attribute is not None and isinstance(structure, Structure): |
paulb@53 | 1155 | results.append((Attribute(structure, attribute.type), accessor)) |
paulb@53 | 1156 | else: |
paulb@53 | 1157 | results.append((attribute, accessor)) |
paulb@53 | 1158 | return results |
paulb@48 | 1159 | |
paulb@57 | 1160 | # Convenience functions. |
paulb@57 | 1161 | |
paulb@57 | 1162 | def annotate(module, builtins=None): |
paulb@57 | 1163 | |
paulb@57 | 1164 | """ |
paulb@57 | 1165 | Annotate the given 'module', also employing the optional 'builtins' module, |
paulb@57 | 1166 | if specified. |
paulb@57 | 1167 | """ |
paulb@57 | 1168 | |
paulb@57 | 1169 | annotator = Annotator() |
paulb@57 | 1170 | if builtins is not None: |
paulb@57 | 1171 | annotator.process(module, builtins) |
paulb@57 | 1172 | else: |
paulb@57 | 1173 | annotator.process(module) |
paulb@57 | 1174 | |
paulb@57 | 1175 | def annotate_all(modules, builtins): |
paulb@57 | 1176 | annotate(builtins) |
paulb@57 | 1177 | for module in modules: |
paulb@57 | 1178 | annotate(module, builtins) |
paulb@57 | 1179 | |
paulb@19 | 1180 | # vim: tabstop=4 expandtab shiftwidth=4 |