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@178 | 8 | Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@19 | 9 | |
paulb@260 | 10 | This program is free software; you can redistribute it and/or modify it under |
paulb@260 | 11 | the terms of the GNU General Public License as published by the Free Software |
paulb@260 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paulb@260 | 13 | version. |
paulb@19 | 14 | |
paulb@260 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@260 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@260 | 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paulb@260 | 18 | details. |
paulb@19 | 19 | |
paulb@260 | 20 | You should have received a copy of the GNU General Public License along with |
paulb@260 | 21 | this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@50 | 22 | |
paulb@50 | 23 | -------- |
paulb@50 | 24 | |
paulb@249 | 25 | To use this module, the easiest approach is to use the annotate function which |
paulb@249 | 26 | provides support for annotating modules already processed by simplify and |
paulb@249 | 27 | fixnames: |
paulb@156 | 28 | |
paulb@249 | 29 | annotate(builtins, None, importer) |
paulb@249 | 30 | annotate(module, builtins, importer) |
paulb@156 | 31 | |
paulb@249 | 32 | At the most basic level, the most intricate approach involves obtaining an |
paulb@156 | 33 | Annotator object: |
paulb@50 | 34 | |
paulb@249 | 35 | annotator = Annotator(importer) |
paulb@50 | 36 | |
paulb@57 | 37 | Then, processing an existing module with it: |
paulb@50 | 38 | |
paulb@57 | 39 | annotator.process(module) |
paulb@50 | 40 | |
paulb@50 | 41 | If a module containing built-in classes and functions has already been |
paulb@50 | 42 | annotated, such a module should be passed in as an additional argument: |
paulb@50 | 43 | |
paulb@57 | 44 | annotator.process(module, builtins) |
paulb@19 | 45 | """ |
paulb@19 | 46 | |
paulb@220 | 47 | from simplify.simplified import * |
paulb@19 | 48 | import compiler |
paulb@156 | 49 | import os |
paulb@19 | 50 | |
paulb@24 | 51 | class System: |
paulb@24 | 52 | |
paulb@50 | 53 | """ |
paulb@50 | 54 | A class maintaining the state of the annotation system. When the system |
paulb@50 | 55 | counter can no longer be incremented by any annotation operation, the |
paulb@50 | 56 | system may be considered stable and fully annotated. |
paulb@50 | 57 | """ |
paulb@24 | 58 | |
paulb@24 | 59 | def __init__(self): |
paulb@24 | 60 | self.count = 0 |
paulb@146 | 61 | |
paulb@212 | 62 | def init(self, node, attr="types"): |
paulb@150 | 63 | |
paulb@150 | 64 | "Initialise a 'node' for annotation." |
paulb@150 | 65 | |
paulb@212 | 66 | if not hasattr(node, attr): |
paulb@213 | 67 | setattr(node, attr, set()) |
paulb@146 | 68 | |
paulb@212 | 69 | def annotate(self, node, types, attr="types"): |
paulb@150 | 70 | |
paulb@150 | 71 | "Annotate the given 'node' with the given 'types'." |
paulb@150 | 72 | |
paulb@212 | 73 | self.init(node, attr) |
paulb@212 | 74 | self.combine(getattr(node, attr), types) |
paulb@225 | 75 | node.annotated = 1 |
paulb@146 | 76 | |
paulb@146 | 77 | def combine(self, target, types): |
paulb@150 | 78 | |
paulb@150 | 79 | """ |
paulb@150 | 80 | Combine the 'target' list with the given 'types', counting new members. |
paulb@150 | 81 | """ |
paulb@150 | 82 | |
paulb@24 | 83 | for type in types: |
paulb@146 | 84 | if type not in target: |
paulb@213 | 85 | target.add(type) |
paulb@24 | 86 | self.count += 1 |
paulb@24 | 87 | |
paulb@24 | 88 | system = System() |
paulb@24 | 89 | |
paulb@58 | 90 | # Exceptions. |
paulb@58 | 91 | |
paulb@83 | 92 | class AnnotationError(SimplifiedError): |
paulb@83 | 93 | |
paulb@83 | 94 | "An error in the annotation process." |
paulb@83 | 95 | |
paulb@83 | 96 | pass |
paulb@58 | 97 | |
paulb@67 | 98 | class AnnotationMessage(Exception): |
paulb@83 | 99 | |
paulb@83 | 100 | "A lesser annotation error." |
paulb@83 | 101 | |
paulb@67 | 102 | pass |
paulb@67 | 103 | |
paulb@24 | 104 | # Annotation. |
paulb@19 | 105 | |
paulb@20 | 106 | class Annotator(Visitor): |
paulb@24 | 107 | |
paulb@24 | 108 | """ |
paulb@24 | 109 | The type annotator which traverses the program nodes, typically depth-first, |
paulb@24 | 110 | and maintains a record of the current set of types applying to the currently |
paulb@24 | 111 | considered operation. Such types are also recorded on the nodes, and a |
paulb@24 | 112 | special "system" record is maintained to monitor the level of annotation |
paulb@24 | 113 | activity with a view to recognising when no more annotations are possible. |
paulb@50 | 114 | |
paulb@50 | 115 | Throughout the annotation activity, type information consists of lists of |
paulb@50 | 116 | Attribute objects where such objects retain information about the context of |
paulb@50 | 117 | the type (since a value in the program may be associated with an object or |
paulb@50 | 118 | class) and the actual type of the value being manipulated. Upon accessing |
paulb@50 | 119 | attribute information on namespaces, additional accessor information is also |
paulb@50 | 120 | exchanged - this provides a means of distinguishing between the different |
paulb@50 | 121 | types possible when the means of constructing the namespace may depend on |
paulb@50 | 122 | run-time behaviour. |
paulb@151 | 123 | |
paulb@274 | 124 | Covered: Assign, CheckType, Conditional, Constant, Global, Import, |
paulb@274 | 125 | InvokeRef, InvokeFunction, LoadAttr, LoadExc, LoadName, LoadRef, |
paulb@274 | 126 | LoadTemp, Module, Not, Pass, Raise, ReleaseTemp, ReturnFromBlock, |
paulb@156 | 127 | ReturnFromFunction, StoreAttr, StoreName, StoreTemp, Subprogram, |
paulb@156 | 128 | Try. |
paulb@24 | 129 | """ |
paulb@24 | 130 | |
paulb@249 | 131 | def __init__(self, importer): |
paulb@50 | 132 | |
paulb@249 | 133 | "Initialise the visitor with an 'importer'." |
paulb@50 | 134 | |
paulb@19 | 135 | Visitor.__init__(self) |
paulb@24 | 136 | self.system = system |
paulb@249 | 137 | self.importer = importer |
paulb@19 | 138 | |
paulb@30 | 139 | # Satisfy visitor issues. |
paulb@30 | 140 | |
paulb@30 | 141 | self.visitor = self |
paulb@30 | 142 | |
paulb@57 | 143 | def process(self, module, builtins=None): |
paulb@45 | 144 | |
paulb@45 | 145 | """ |
paulb@57 | 146 | Process the given 'module', using the optional 'builtins' to access |
paulb@57 | 147 | built-in classes and functions. |
paulb@45 | 148 | """ |
paulb@45 | 149 | |
paulb@247 | 150 | self.current_subprograms = set() |
paulb@45 | 151 | self.current_namespaces = [] |
paulb@204 | 152 | self.rerun_subprograms = {} |
paulb@115 | 153 | self.namespace = None |
paulb@121 | 154 | self.module = module |
paulb@34 | 155 | |
paulb@34 | 156 | # Process the module, supplying builtins if possible. |
paulb@34 | 157 | |
paulb@80 | 158 | self.builtins = builtins |
paulb@45 | 159 | self.global_namespace = Namespace() |
paulb@45 | 160 | |
paulb@57 | 161 | if builtins is not None: |
paulb@57 | 162 | self.builtins_namespace = builtins.namespace |
paulb@34 | 163 | else: |
paulb@45 | 164 | self.builtins_namespace = self.global_namespace |
paulb@34 | 165 | |
paulb@207 | 166 | # NOTE: Not declaring module namespace usage, even though it is used. |
paulb@45 | 167 | |
paulb@213 | 168 | self.process_node(module, self.global_namespace, 0) |
paulb@207 | 169 | |
paulb@207 | 170 | def process_node(self, node, locals, using_module_namespace): |
paulb@24 | 171 | |
paulb@24 | 172 | """ |
paulb@132 | 173 | Process a subprogram or module 'node', indicating the initial 'locals'. |
paulb@213 | 174 | Note that this method may mutate nodes in the original program. |
paulb@24 | 175 | """ |
paulb@24 | 176 | |
paulb@204 | 177 | # Recursion test. |
paulb@204 | 178 | |
paulb@204 | 179 | if node in self.current_subprograms: |
paulb@204 | 180 | if not self.rerun_subprograms.has_key(node): |
paulb@204 | 181 | self.rerun_subprograms[node] = [] |
paulb@204 | 182 | self.rerun_subprograms[node].append(locals) |
paulb@213 | 183 | return |
paulb@204 | 184 | |
paulb@115 | 185 | # Record the current subprogram and namespace. |
paulb@115 | 186 | |
paulb@247 | 187 | self.current_subprograms.add(node) |
paulb@115 | 188 | |
paulb@67 | 189 | # Determine the namespace. |
paulb@67 | 190 | |
paulb@132 | 191 | self.current_namespaces.append(self.namespace) |
paulb@132 | 192 | self.namespace = locals |
paulb@45 | 193 | |
paulb@25 | 194 | # Add namespace details to any structure involved. |
paulb@25 | 195 | |
paulb@268 | 196 | if node.structure is not None: |
paulb@34 | 197 | node.structure.namespace = Namespace() |
paulb@25 | 198 | |
paulb@30 | 199 | # Initialise bases where appropriate. |
paulb@19 | 200 | |
paulb@30 | 201 | if hasattr(node.structure, "bases"): |
paulb@30 | 202 | base_refs = [] |
paulb@30 | 203 | for base in node.structure.bases: |
paulb@30 | 204 | self.dispatch(base) |
paulb@49 | 205 | base_refs.append(self.namespace.types) |
paulb@30 | 206 | node.structure.base_refs = base_refs |
paulb@30 | 207 | |
paulb@30 | 208 | # Dispatch to the code itself. |
paulb@30 | 209 | |
paulb@49 | 210 | node.namespace = self.namespace |
paulb@207 | 211 | self.set_module_namespace(using_module_namespace) |
paulb@207 | 212 | |
paulb@213 | 213 | self.dispatch(node) |
paulb@213 | 214 | self.extract_results(node) |
paulb@204 | 215 | |
paulb@247 | 216 | # We may need to "re-run" subprograms because they were called |
paulb@247 | 217 | # recursively with potentially different arguments. These argument types |
paulb@247 | 218 | # are now tried out here. |
paulb@247 | 219 | |
paulb@204 | 220 | while self.rerun_subprograms.has_key(node): |
paulb@204 | 221 | all_rerun_locals = self.rerun_subprograms[node] |
paulb@204 | 222 | del self.rerun_subprograms[node] |
paulb@204 | 223 | for rerun_locals in all_rerun_locals: |
paulb@209 | 224 | #print "Re-running", node, "with", rerun_locals |
paulb@204 | 225 | |
paulb@207 | 226 | self.namespace = rerun_locals |
paulb@207 | 227 | node.namespace = rerun_locals |
paulb@207 | 228 | self.set_module_namespace(using_module_namespace) |
paulb@49 | 229 | |
paulb@213 | 230 | self.dispatch(node) |
paulb@213 | 231 | self.extract_results(node) |
paulb@45 | 232 | |
paulb@45 | 233 | # Restore the previous subprogram and namespace. |
paulb@45 | 234 | |
paulb@132 | 235 | self.namespace = self.current_namespaces.pop() |
paulb@247 | 236 | self.current_subprograms.remove(node) |
paulb@207 | 237 | self.reset_module_namespace(using_module_namespace) |
paulb@45 | 238 | |
paulb@207 | 239 | def set_module_namespace(self, using_module_namespace): |
paulb@207 | 240 | |
paulb@207 | 241 | """ |
paulb@207 | 242 | In order to keep global accesses working, the module namespace must be |
paulb@207 | 243 | adjusted. |
paulb@207 | 244 | """ |
paulb@207 | 245 | |
paulb@207 | 246 | if using_module_namespace: |
paulb@207 | 247 | self.module.namespace = self.namespace |
paulb@207 | 248 | |
paulb@207 | 249 | def reset_module_namespace(self, using_module_namespace): |
paulb@207 | 250 | |
paulb@207 | 251 | """ |
paulb@207 | 252 | In order to keep global accesses working, the module namespace must be |
paulb@207 | 253 | reset. |
paulb@207 | 254 | """ |
paulb@207 | 255 | |
paulb@207 | 256 | if using_module_namespace: |
paulb@207 | 257 | self.module.namespace = self.namespace |
paulb@207 | 258 | |
paulb@213 | 259 | def extract_results(self, node): |
paulb@207 | 260 | |
paulb@207 | 261 | "Extract results from the namespace." |
paulb@207 | 262 | |
paulb@213 | 263 | node.namespace = self.namespace |
paulb@213 | 264 | self.system.annotate(node, self.namespace.raises, "raises") |
paulb@213 | 265 | self.system.annotate(node, self.namespace.returns, "returns") |
paulb@213 | 266 | if hasattr(node, "return_locals"): |
paulb@213 | 267 | node.return_locals.update(self.namespace.return_locals) |
paulb@207 | 268 | |
paulb@83 | 269 | def annotate(self, node, types=None): |
paulb@24 | 270 | |
paulb@83 | 271 | """ |
paulb@83 | 272 | Annotate the given 'node' in the system, using either the optional |
paulb@83 | 273 | 'types' or the namespace's current type information. |
paulb@83 | 274 | """ |
paulb@24 | 275 | |
paulb@207 | 276 | if types is None: |
paulb@207 | 277 | self.system.annotate(node, self.namespace.types) |
paulb@207 | 278 | else: |
paulb@207 | 279 | self.system.annotate(node, types) |
paulb@34 | 280 | |
paulb@146 | 281 | def annotate_parameters(self, node, items): |
paulb@146 | 282 | |
paulb@146 | 283 | """ |
paulb@146 | 284 | Annotate the given 'node' using the given 'items' and updating the |
paulb@146 | 285 | system's annotation counter. |
paulb@146 | 286 | """ |
paulb@146 | 287 | |
paulb@146 | 288 | for param, types in items: |
paulb@146 | 289 | if not node.paramtypes.has_key(param): |
paulb@213 | 290 | node.paramtypes[param] = set() |
paulb@146 | 291 | self.system.combine(node.paramtypes[param], types) |
paulb@146 | 292 | |
paulb@25 | 293 | # Visitor methods. |
paulb@25 | 294 | |
paulb@19 | 295 | def default(self, node): |
paulb@24 | 296 | |
paulb@24 | 297 | """ |
paulb@24 | 298 | Process the given 'node', given that it does not have a specific |
paulb@24 | 299 | handler. |
paulb@24 | 300 | """ |
paulb@24 | 301 | |
paulb@115 | 302 | raise AnnotationMessage, "Node '%s' not supported." % node |
paulb@20 | 303 | |
paulb@20 | 304 | def dispatch(self, node, *args): |
paulb@34 | 305 | try: |
paulb@213 | 306 | Visitor.dispatch(self, node, *args) |
paulb@67 | 307 | except AnnotationError, exc: |
paulb@58 | 308 | exc.add(node) |
paulb@34 | 309 | raise |
paulb@67 | 310 | except AnnotationMessage, exc: |
paulb@67 | 311 | raise AnnotationError(exc, node) |
paulb@19 | 312 | |
paulb@150 | 313 | # Specific node methods. |
paulb@110 | 314 | |
paulb@115 | 315 | def visitAssign(self, assign): |
paulb@150 | 316 | |
paulb@150 | 317 | """ |
paulb@213 | 318 | Process the 'assign' node and its contents. |
paulb@150 | 319 | """ |
paulb@150 | 320 | |
paulb@213 | 321 | self.dispatches(assign.code) |
paulb@115 | 322 | |
paulb@193 | 323 | def visitCheckType(self, checktype): |
paulb@150 | 324 | |
paulb@150 | 325 | """ |
paulb@213 | 326 | Process the 'checktype' node, finding the possible types of the |
paulb@213 | 327 | exception, and processing each choice to build a list of checked types |
paulb@213 | 328 | for the exception. |
paulb@150 | 329 | """ |
paulb@150 | 330 | |
paulb@193 | 331 | inverted = getattr(checktype, "inverted", 0) |
paulb@213 | 332 | self.dispatch(checktype.expr) |
paulb@194 | 333 | |
paulb@150 | 334 | expr_types = self.namespace.types |
paulb@213 | 335 | choice_types = set() |
paulb@150 | 336 | choices = [] |
paulb@194 | 337 | |
paulb@193 | 338 | for choice in checktype.choices: |
paulb@150 | 339 | choices.append(self.dispatch(choice)) |
paulb@213 | 340 | choice_types.update(self.namespace.types) |
paulb@194 | 341 | |
paulb@150 | 342 | for expr_type in expr_types: |
paulb@193 | 343 | in_choices = expr_type.type.get_class() in choice_types |
paulb@194 | 344 | |
paulb@194 | 345 | # Filter out types not in the choices list unless the operation is |
paulb@194 | 346 | # inverted; in which case, filter out types in the choices list. |
paulb@194 | 347 | |
paulb@193 | 348 | if not inverted and not in_choices or inverted and in_choices: |
paulb@193 | 349 | self._prune_non_accesses(checktype.expr, expr_type) |
paulb@194 | 350 | |
paulb@110 | 351 | def visitConditional(self, conditional): |
paulb@19 | 352 | |
paulb@150 | 353 | """ |
paulb@213 | 354 | Process the 'conditional' node, processing the test, body and else |
paulb@150 | 355 | clauses and recording their processed forms. The body and else clauses |
paulb@150 | 356 | are processed within their own namespaces, and the test is also |
paulb@150 | 357 | processed in its own namespace if 'isolate_test' is set on the |
paulb@150 | 358 | 'conditional' node. |
paulb@150 | 359 | """ |
paulb@150 | 360 | |
paulb@110 | 361 | # Conditionals keep local namespace changes isolated. |
paulb@110 | 362 | # With Return nodes inside the body/else sections, the changes are |
paulb@110 | 363 | # communicated to the caller. |
paulb@19 | 364 | |
paulb@133 | 365 | is_module = self.namespace is self.module.namespace |
paulb@133 | 366 | |
paulb@133 | 367 | # Where the test is closely associated with the body, save the namespace |
paulb@133 | 368 | # before entering the test. |
paulb@133 | 369 | |
paulb@133 | 370 | if conditional.isolate_test: |
paulb@133 | 371 | saved_namespace = self.namespace |
paulb@133 | 372 | self.namespace = Namespace() |
paulb@133 | 373 | if is_module: |
paulb@133 | 374 | self.module.namespace = self.namespace |
paulb@133 | 375 | self.namespace.merge_namespace(saved_namespace) |
paulb@19 | 376 | |
paulb@213 | 377 | self.dispatch(conditional.test) |
paulb@133 | 378 | |
paulb@133 | 379 | # Where the test may affect the body and the else clause, save the |
paulb@133 | 380 | # namespace after processing the test. |
paulb@133 | 381 | |
paulb@133 | 382 | if not conditional.isolate_test: |
paulb@133 | 383 | saved_namespace = self.namespace |
paulb@133 | 384 | self.namespace = Namespace() |
paulb@133 | 385 | if is_module: |
paulb@133 | 386 | self.module.namespace = self.namespace |
paulb@133 | 387 | self.namespace.merge_namespace(saved_namespace) |
paulb@133 | 388 | |
paulb@190 | 389 | # NOTE: Exception recording. |
paulb@190 | 390 | |
paulb@190 | 391 | else: |
paulb@213 | 392 | test_raises = set() |
paulb@213 | 393 | test_raises.update(self.namespace.raises) |
paulb@190 | 394 | |
paulb@133 | 395 | # Process the body clause. |
paulb@133 | 396 | |
paulb@213 | 397 | self.dispatches(conditional.body) |
paulb@110 | 398 | body_namespace = self.namespace |
paulb@19 | 399 | |
paulb@133 | 400 | # Use the saved namespace as a template for the else clause. |
paulb@133 | 401 | |
paulb@110 | 402 | self.namespace = Namespace() |
paulb@133 | 403 | if is_module: |
paulb@133 | 404 | self.module.namespace = self.namespace |
paulb@110 | 405 | self.namespace.merge_namespace(saved_namespace) |
paulb@133 | 406 | |
paulb@133 | 407 | # Process the else clause. |
paulb@133 | 408 | |
paulb@213 | 409 | self.dispatches(conditional.else_) |
paulb@110 | 410 | else_namespace = self.namespace |
paulb@19 | 411 | |
paulb@133 | 412 | # Merge the body and else namespaces. |
paulb@133 | 413 | |
paulb@110 | 414 | self.namespace = Namespace() |
paulb@133 | 415 | if is_module: |
paulb@133 | 416 | self.module.namespace = self.namespace |
paulb@110 | 417 | self.namespace.merge_namespace(body_namespace) |
paulb@110 | 418 | self.namespace.merge_namespace(else_namespace) |
paulb@110 | 419 | |
paulb@166 | 420 | # NOTE: Test of exception type pruning based on the test/body. |
paulb@190 | 421 | # Note that the checked exceptions are tested for re-raising. |
paulb@166 | 422 | |
paulb@166 | 423 | if conditional.isolate_test: |
paulb@190 | 424 | for exc_type in test_raises: |
paulb@190 | 425 | if exc_type not in body_namespace.raises: |
paulb@190 | 426 | self.namespace.revoke_exception_type(exc_type) |
paulb@166 | 427 | |
paulb@274 | 428 | def visitConstant(self, const): |
paulb@274 | 429 | |
paulb@274 | 430 | """ |
paulb@274 | 431 | Process the 'const' node, producing the appropriate type information |
paulb@274 | 432 | for the value represented. |
paulb@274 | 433 | """ |
paulb@274 | 434 | |
paulb@274 | 435 | attrs = self.get_builtin_instances(const, const.typename) |
paulb@274 | 436 | self.namespace.set_types(attrs) |
paulb@274 | 437 | self.annotate(const) |
paulb@274 | 438 | |
paulb@155 | 439 | def visitGlobal(self, global_): |
paulb@155 | 440 | |
paulb@155 | 441 | """ |
paulb@213 | 442 | Leave the 'global_' node unprocessed since namespaces should have |
paulb@155 | 443 | already been altered to take global names into consideration. |
paulb@155 | 444 | """ |
paulb@155 | 445 | |
paulb@213 | 446 | pass |
paulb@155 | 447 | |
paulb@156 | 448 | def visitImport(self, import_): |
paulb@156 | 449 | |
paulb@156 | 450 | """ |
paulb@213 | 451 | Process the 'import_' node, importing the module with the stated name |
paulb@156 | 452 | and storing details on the node. |
paulb@156 | 453 | """ |
paulb@156 | 454 | |
paulb@268 | 455 | module = self.importer.load(import_.name, self.builtins, import_.alias) |
paulb@156 | 456 | if module is not None: |
paulb@213 | 457 | self.namespace.set_types(set([module])) |
paulb@156 | 458 | else: |
paulb@213 | 459 | self.namespace.set_types(set()) |
paulb@157 | 460 | self.annotate(import_) # mainly for viewing purposes |
paulb@156 | 461 | |
paulb@151 | 462 | def _visitInvoke(self, invoke, invocation_types, have_args): |
paulb@151 | 463 | |
paulb@151 | 464 | """ |
paulb@213 | 465 | Process the 'invoke' node, using the given 'invocation_types' as the |
paulb@213 | 466 | list of callables to be investigated for instantiation or for the |
paulb@151 | 467 | invocation of functions or blocks. If 'have_args' is a true value, any |
paulb@151 | 468 | invocation or instantiation will involve arguments. |
paulb@151 | 469 | """ |
paulb@151 | 470 | |
paulb@151 | 471 | # Now locate and invoke the subprogram. This can be complicated because |
paulb@151 | 472 | # the target may be a class or object, and there may be many different |
paulb@151 | 473 | # related subprograms. |
paulb@151 | 474 | |
paulb@270 | 475 | invocations = set() |
paulb@151 | 476 | |
paulb@151 | 477 | # Visit each callable in turn, finding subprograms. |
paulb@151 | 478 | |
paulb@151 | 479 | for attr in invocation_types: |
paulb@151 | 480 | |
paulb@151 | 481 | # Deal with class invocations by providing instance objects. |
paulb@151 | 482 | # Here, each class is queried for the __init__ method, which may |
paulb@151 | 483 | # exist for some combinations of classes in a hierarchy but not for |
paulb@151 | 484 | # others. |
paulb@151 | 485 | |
paulb@249 | 486 | if isinstance(attr.type, GeneralClass): |
paulb@151 | 487 | attributes = get_attributes(attr.type, "__init__") |
paulb@151 | 488 | |
paulb@151 | 489 | # Deal with object invocations by using __call__ methods. |
paulb@151 | 490 | |
paulb@151 | 491 | elif isinstance(attr.type, Instance): |
paulb@151 | 492 | attributes = get_attributes(attr.type, "__call__") |
paulb@151 | 493 | |
paulb@151 | 494 | # Normal functions or methods are more straightforward. |
paulb@151 | 495 | # Here, we model them using an attribute with no context and with |
paulb@151 | 496 | # no associated accessor. |
paulb@151 | 497 | |
paulb@151 | 498 | else: |
paulb@151 | 499 | attributes = [(attr, None)] |
paulb@151 | 500 | |
paulb@151 | 501 | # Inspect each attribute and extract the subprogram. |
paulb@151 | 502 | |
paulb@151 | 503 | for attribute, accessor in attributes: |
paulb@151 | 504 | |
paulb@151 | 505 | # If a class is involved, presume that it must create a new |
paulb@151 | 506 | # object. |
paulb@151 | 507 | |
paulb@249 | 508 | if isinstance(attr.type, GeneralClass): |
paulb@151 | 509 | |
paulb@151 | 510 | # Instantiate the class. |
paulb@151 | 511 | |
paulb@185 | 512 | instance = self.new_instance(invoke, attr.type) |
paulb@151 | 513 | |
paulb@151 | 514 | # For instantiations, switch the context. |
paulb@151 | 515 | |
paulb@151 | 516 | if attribute is not None: |
paulb@151 | 517 | attribute = Attribute(instance, attribute.type) |
paulb@151 | 518 | |
paulb@204 | 519 | # Request an instance-specific initialiser. |
paulb@204 | 520 | |
paulb@204 | 521 | attribute = attr.type.get_attribute_for_instance(attribute, instance) |
paulb@204 | 522 | |
paulb@151 | 523 | # Skip cases where no callable is found. |
paulb@151 | 524 | |
paulb@151 | 525 | if attribute is not None: |
paulb@151 | 526 | |
paulb@151 | 527 | # If a subprogram is defined, invoke it. |
paulb@151 | 528 | |
paulb@151 | 529 | self.invoke_subprogram(invoke, attribute) |
paulb@270 | 530 | invocations.add(attribute.type) |
paulb@151 | 531 | |
paulb@249 | 532 | elif not isinstance(attr.type, GeneralClass): |
paulb@151 | 533 | print "Invocation type is None for", accessor |
paulb@151 | 534 | |
paulb@151 | 535 | else: |
paulb@151 | 536 | |
paulb@151 | 537 | # Test to see if no arguments were supplied in cases where no |
paulb@151 | 538 | # initialiser was found. |
paulb@151 | 539 | |
paulb@151 | 540 | if have_args: |
paulb@151 | 541 | raise AnnotationMessage, "No initialiser found for '%s' with arguments." % attr.type |
paulb@151 | 542 | |
paulb@151 | 543 | # Special case: initialisation. |
paulb@151 | 544 | |
paulb@249 | 545 | if isinstance(attr.type, GeneralClass): |
paulb@151 | 546 | |
paulb@151 | 547 | # Associate the instance with the result of this invocation. |
paulb@151 | 548 | |
paulb@213 | 549 | self.namespace.set_types(set([Attribute(None, instance)])) |
paulb@151 | 550 | self.annotate(invoke) |
paulb@151 | 551 | |
paulb@151 | 552 | # Remember the invocations that were found, along with the return type |
paulb@151 | 553 | # information. |
paulb@151 | 554 | |
paulb@151 | 555 | invoke.invocations = invocations |
paulb@213 | 556 | self.namespace.set_types(getattr(invoke, "types", set())) |
paulb@151 | 557 | |
paulb@204 | 558 | def visitInvokeRef(self, invoke): |
paulb@151 | 559 | |
paulb@151 | 560 | """ |
paulb@213 | 561 | Process the 'invoke' node, first finding the callables indicated by the |
paulb@213 | 562 | reference. |
paulb@151 | 563 | """ |
paulb@151 | 564 | |
paulb@204 | 565 | # Where the invocation belongs to an instance but the invoked subprogram |
paulb@204 | 566 | # does not, request a special copy. |
paulb@204 | 567 | |
paulb@204 | 568 | instance = getattr(invoke, "instance", None) |
paulb@204 | 569 | if instance is not None and getattr(invoke.ref, "instance", None) is None: |
paulb@204 | 570 | if invoke.ref.copies.has_key(instance): |
paulb@204 | 571 | invoke.ref = invoke.ref.copies[instance] |
paulb@204 | 572 | else: |
paulb@204 | 573 | invoke.ref = invoke.ref.copy(instance) |
paulb@209 | 574 | #print "Created", invoke.ref, "for", getattr(invoke.ref, "instance", None) |
paulb@204 | 575 | invoke.ref.module.simplifier.subnames[invoke.ref.full_name()] = invoke.ref |
paulb@204 | 576 | invocation_types = [Attribute(None, invoke.ref)] |
paulb@213 | 577 | self._visitInvoke(invoke, invocation_types, have_args=0) |
paulb@151 | 578 | |
paulb@151 | 579 | def visitInvokeFunction(self, invoke): |
paulb@151 | 580 | |
paulb@151 | 581 | """ |
paulb@213 | 582 | Process the 'invoke' node, first finding the callables indicated by the |
paulb@213 | 583 | expression. |
paulb@151 | 584 | """ |
paulb@151 | 585 | |
paulb@213 | 586 | self.dispatch(invoke.expr) |
paulb@151 | 587 | invocation_types = self.namespace.types |
paulb@151 | 588 | |
paulb@151 | 589 | # Invocation processing starts with making sure that the arguments have |
paulb@151 | 590 | # been processed. |
paulb@151 | 591 | |
paulb@213 | 592 | self._visitInvoke(invoke, invocation_types, have_args=self.process_args(invoke)) |
paulb@151 | 593 | |
paulb@150 | 594 | def visitLoadAttr(self, loadattr): |
paulb@150 | 595 | |
paulb@150 | 596 | """ |
paulb@213 | 597 | Process the 'loadattr' node, processing and storing the expression, and |
paulb@150 | 598 | using the expression's types to construct records of accesses and |
paulb@150 | 599 | non-accesses using the stated attribute name. |
paulb@150 | 600 | """ |
paulb@150 | 601 | |
paulb@213 | 602 | self.dispatch(loadattr.expr) |
paulb@213 | 603 | types = set() |
paulb@216 | 604 | raises = set() |
paulb@232 | 605 | non_accesses = set() |
paulb@150 | 606 | accesses = {} |
paulb@213 | 607 | |
paulb@213 | 608 | # For each expression type... |
paulb@213 | 609 | |
paulb@150 | 610 | for attr in self.namespace.types: |
paulb@213 | 611 | |
paulb@213 | 612 | # Find types for the named attribute. |
paulb@213 | 613 | |
paulb@150 | 614 | attributes = get_attributes(attr.type, loadattr.name) |
paulb@213 | 615 | |
paulb@213 | 616 | # Where no attributes exist... |
paulb@213 | 617 | |
paulb@150 | 618 | if not attributes: |
paulb@213 | 619 | |
paulb@213 | 620 | # Register new invalid accesses and mark a possible exception. |
paulb@213 | 621 | |
paulb@161 | 622 | if not attr in non_accesses: |
paulb@232 | 623 | non_accesses.add(attr) |
paulb@216 | 624 | exc = self.get_builtin_instances(loadattr, "AttributeError") |
paulb@216 | 625 | raises.update(exc) |
paulb@216 | 626 | self.namespace.raises.update(exc) |
paulb@150 | 627 | |
paulb@150 | 628 | # Revoke this type from any name involved. |
paulb@150 | 629 | |
paulb@150 | 630 | self._prune_non_accesses(loadattr.expr, attr) |
paulb@150 | 631 | |
paulb@213 | 632 | # For each type found... |
paulb@213 | 633 | |
paulb@150 | 634 | for attribute, accessor in attributes: |
paulb@213 | 635 | |
paulb@213 | 636 | # For actual attributes, register the type and remember the |
paulb@213 | 637 | # access. |
paulb@213 | 638 | |
paulb@150 | 639 | if attribute is not None: |
paulb@213 | 640 | types.add(attribute) |
paulb@150 | 641 | if not accesses.has_key(attr.type): |
paulb@150 | 642 | accesses[attr.type] = [] |
paulb@150 | 643 | if not (attribute, accessor) in accesses[attr.type]: |
paulb@150 | 644 | accesses[attr.type].append((attribute, accessor)) |
paulb@213 | 645 | |
paulb@213 | 646 | # Otherwise, register new invalid accesses and note a possible |
paulb@213 | 647 | # exception. |
paulb@213 | 648 | |
paulb@150 | 649 | else: |
paulb@150 | 650 | if not attr in non_accesses: |
paulb@232 | 651 | non_accesses.add(attr) |
paulb@216 | 652 | exc = self.get_builtin_instances(loadattr, "AttributeError") |
paulb@216 | 653 | raises.update(exc) |
paulb@216 | 654 | self.namespace.raises.update(exc) |
paulb@150 | 655 | |
paulb@150 | 656 | # Revoke this type from any name involved. |
paulb@150 | 657 | |
paulb@150 | 658 | self._prune_non_accesses(loadattr.expr, attr) |
paulb@150 | 659 | |
paulb@150 | 660 | if not types: |
paulb@150 | 661 | print "No attribute found for", loadattr.name, "given", self.namespace.types |
paulb@213 | 662 | |
paulb@213 | 663 | # Remember the result types. |
paulb@213 | 664 | |
paulb@150 | 665 | self.namespace.set_types(types) |
paulb@150 | 666 | loadattr.non_accesses = non_accesses |
paulb@150 | 667 | loadattr.accesses = accesses |
paulb@216 | 668 | loadattr.raises = raises |
paulb@150 | 669 | self.annotate(loadattr) |
paulb@150 | 670 | |
paulb@150 | 671 | def _prune_non_accesses(self, expr, attr): |
paulb@150 | 672 | |
paulb@150 | 673 | """ |
paulb@150 | 674 | Prune type information from 'expr' where the given 'attr' has been |
paulb@150 | 675 | shown to be a non-access. |
paulb@150 | 676 | """ |
paulb@150 | 677 | |
paulb@150 | 678 | if isinstance(expr, LoadName): |
paulb@150 | 679 | self.namespace.revoke(expr.name, attr) |
paulb@150 | 680 | elif isinstance(expr, LoadExc): |
paulb@150 | 681 | self.namespace.revoke_exception_type(attr) |
paulb@159 | 682 | elif isinstance(expr, LoadTemp): |
paulb@162 | 683 | self.namespace.revoke_temp_type(getattr(expr, "index", None), attr) |
paulb@150 | 684 | |
paulb@161 | 685 | # LoadAttr cannot be pruned since this might unintentionally prune |
paulb@161 | 686 | # legitimate types from other applications of the referenced type, it |
paulb@161 | 687 | # almost certainly doesn't take "concurrent" mutation into |
paulb@161 | 688 | # consideration (where in a running program, the pruned type is actually |
paulb@161 | 689 | # reintroduced, making the pruning invalid), and there is no easy way of |
paulb@161 | 690 | # preserving the meaning of a namespace without either creating lots of |
paulb@161 | 691 | # specialised instances, and even then... |
paulb@161 | 692 | |
paulb@161 | 693 | #elif isinstance(expr, LoadAttr): |
paulb@161 | 694 | # for expr_attr in expr.expr.types: |
paulb@161 | 695 | # if hasattr(expr_attr.type, "namespace"): |
paulb@161 | 696 | # expr_attr.type.namespace.revoke(expr.name, attr) |
paulb@161 | 697 | |
paulb@150 | 698 | def visitLoadExc(self, loadexc): |
paulb@150 | 699 | |
paulb@150 | 700 | """ |
paulb@213 | 701 | Process the 'loadexc' node, discovering the possible exception types |
paulb@150 | 702 | raised. |
paulb@150 | 703 | """ |
paulb@150 | 704 | |
paulb@207 | 705 | self.namespace.set_types(self.namespace.raises) |
paulb@150 | 706 | self.annotate(loadexc) |
paulb@150 | 707 | |
paulb@150 | 708 | def visitLoadName(self, loadname): |
paulb@150 | 709 | |
paulb@150 | 710 | """ |
paulb@213 | 711 | Process the 'loadname' node, processing the name information on the node |
paulb@150 | 712 | to determine which types are involved with the name. |
paulb@150 | 713 | """ |
paulb@150 | 714 | |
paulb@150 | 715 | self.namespace.set_types(self.namespace.load(loadname.name)) |
paulb@213 | 716 | self.annotate(loadname) |
paulb@150 | 717 | |
paulb@150 | 718 | def visitLoadRef(self, loadref): |
paulb@150 | 719 | |
paulb@150 | 720 | """ |
paulb@213 | 721 | Process the 'loadref' node, obtaining type information about the |
paulb@150 | 722 | reference stated on the node. |
paulb@150 | 723 | """ |
paulb@150 | 724 | |
paulb@213 | 725 | self.namespace.set_types(set([Attribute(None, loadref.ref)])) |
paulb@150 | 726 | self.annotate(loadref) |
paulb@150 | 727 | |
paulb@150 | 728 | def visitLoadTemp(self, loadtemp): |
paulb@150 | 729 | |
paulb@150 | 730 | """ |
paulb@213 | 731 | Process the 'loadtemp' node, obtaining type information about the |
paulb@150 | 732 | temporary variable accessed, and removing variable information where the |
paulb@150 | 733 | 'release' attribute has been set on the node. |
paulb@150 | 734 | """ |
paulb@150 | 735 | |
paulb@150 | 736 | index = getattr(loadtemp, "index", None) |
paulb@150 | 737 | try: |
paulb@150 | 738 | if getattr(loadtemp, "release", 0): |
paulb@150 | 739 | self.namespace.set_types(self.namespace.temp[index].pop()) |
paulb@150 | 740 | else: |
paulb@150 | 741 | self.namespace.set_types(self.namespace.temp[index][-1]) |
paulb@150 | 742 | except KeyError: |
paulb@150 | 743 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@150 | 744 | self.annotate(loadtemp) |
paulb@150 | 745 | |
paulb@219 | 746 | def visitMakeTuple(self, maketuple): |
paulb@219 | 747 | |
paulb@219 | 748 | """ |
paulb@219 | 749 | Process the 'maketuple' node and its contents. |
paulb@219 | 750 | """ |
paulb@219 | 751 | |
paulb@219 | 752 | # Get a tuple and populate it with type information for the contents. |
paulb@219 | 753 | |
paulb@219 | 754 | tuples = self.get_builtin_instances(maketuple, "tuple") |
paulb@219 | 755 | |
paulb@219 | 756 | # NOTE: This is dependent on the tuple definition in the builtins. |
paulb@219 | 757 | |
paulb@257 | 758 | for i, node in enumerate(maketuple.nodes): |
paulb@219 | 759 | self.dispatch(node) |
paulb@219 | 760 | for t in tuples: |
paulb@219 | 761 | t.type.namespace.add("value", self.namespace.types) |
paulb@257 | 762 | t.type.namespace.add("__value__%d" % i, self.namespace.types) |
paulb@219 | 763 | |
paulb@219 | 764 | self.namespace.set_types(tuples) |
paulb@219 | 765 | self.annotate(maketuple) |
paulb@219 | 766 | |
paulb@115 | 767 | def visitModule(self, module): |
paulb@150 | 768 | |
paulb@150 | 769 | """ |
paulb@213 | 770 | Process the 'module' and its contents. |
paulb@150 | 771 | """ |
paulb@150 | 772 | |
paulb@213 | 773 | self.dispatches(module.code) |
paulb@115 | 774 | |
paulb@150 | 775 | def visitNot(self, not_): |
paulb@150 | 776 | |
paulb@213 | 777 | "Process the 'not_' node and its expression." |
paulb@150 | 778 | |
paulb@213 | 779 | self.dispatch(not_.expr) |
paulb@150 | 780 | |
paulb@115 | 781 | def visitPass(self, pass_): |
paulb@150 | 782 | |
paulb@213 | 783 | "Leave the 'pass_' node unprocessed." |
paulb@150 | 784 | |
paulb@213 | 785 | pass |
paulb@115 | 786 | |
paulb@150 | 787 | def visitRaise(self, raise_): |
paulb@150 | 788 | |
paulb@150 | 789 | """ |
paulb@213 | 790 | Process the 'raise_' node, processing any traceback information along |
paulb@150 | 791 | with the raised exception expression, converting the node into a kind of |
paulb@150 | 792 | invocation where the expression is found not to be an invocation itself. |
paulb@150 | 793 | This node affects the namespace, adding exception types to the list of |
paulb@150 | 794 | those raised in the namespace. |
paulb@150 | 795 | """ |
paulb@150 | 796 | |
paulb@268 | 797 | if raise_.traceback is not None: |
paulb@213 | 798 | self.dispatch(raise_.traceback) |
paulb@213 | 799 | self.dispatch(raise_.expr) |
paulb@150 | 800 | |
paulb@150 | 801 | # Handle bare name exceptions by converting any classes to instances. |
paulb@150 | 802 | |
paulb@150 | 803 | if not isinstance(raise_.expr, InvokeFunction): |
paulb@150 | 804 | raise_.pos_args = [] |
paulb@150 | 805 | raise_.kw_args = {} |
paulb@150 | 806 | raise_.star = None |
paulb@150 | 807 | raise_.dstar = None |
paulb@213 | 808 | types = set() |
paulb@150 | 809 | for attr in self.namespace.types: |
paulb@249 | 810 | if isinstance(attr.type, GeneralClass): |
paulb@150 | 811 | self._visitInvoke(raise_, [attr], have_args=0) |
paulb@213 | 812 | types.update(self.namespace.types) |
paulb@150 | 813 | else: |
paulb@150 | 814 | types = self.namespace.types |
paulb@150 | 815 | |
paulb@213 | 816 | self.namespace.raises.update(types) |
paulb@150 | 817 | |
paulb@150 | 818 | def visitReleaseTemp(self, releasetemp): |
paulb@150 | 819 | |
paulb@150 | 820 | """ |
paulb@213 | 821 | Process the 'releasetemp' node, removing temporary variable information |
paulb@150 | 822 | from the current namespace. |
paulb@150 | 823 | """ |
paulb@150 | 824 | |
paulb@150 | 825 | index = getattr(releasetemp, "index", None) |
paulb@150 | 826 | try: |
paulb@150 | 827 | self.namespace.temp[index].pop() |
paulb@150 | 828 | except KeyError: |
paulb@150 | 829 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@150 | 830 | except IndexError: |
paulb@150 | 831 | pass #raise AnnotationMessage, "Temporary store index '%s' is empty." % index |
paulb@150 | 832 | |
paulb@190 | 833 | def visitResetExc(self, resetexc): |
paulb@213 | 834 | self.namespace.raises = set() |
paulb@190 | 835 | |
paulb@150 | 836 | def visitReturn(self, return_): |
paulb@150 | 837 | |
paulb@150 | 838 | """ |
paulb@213 | 839 | Process the 'return_' node, processing any expression and obtaining type |
paulb@150 | 840 | information to be accumulated in the current namespace's list of return |
paulb@150 | 841 | types. A snapshot of the namespace is taken for the purposes of |
paulb@150 | 842 | reconciling or merging namespaces where subprograms actually share |
paulb@150 | 843 | locals with their callers. |
paulb@150 | 844 | """ |
paulb@150 | 845 | |
paulb@150 | 846 | if hasattr(return_, "expr"): |
paulb@213 | 847 | self.dispatch(return_.expr) |
paulb@213 | 848 | self.namespace.returns.update(self.namespace.types) |
paulb@150 | 849 | self.annotate(return_) |
paulb@150 | 850 | self.namespace.snapshot() |
paulb@150 | 851 | |
paulb@150 | 852 | visitReturnFromBlock = visitReturn |
paulb@150 | 853 | visitReturnFromFunction = visitReturn |
paulb@150 | 854 | |
paulb@150 | 855 | def visitStoreAttr(self, storeattr): |
paulb@150 | 856 | |
paulb@150 | 857 | """ |
paulb@213 | 858 | Process the 'storeattr' node, processing the expression and target, and |
paulb@150 | 859 | using the type information obtained to build records of legitimate |
paulb@150 | 860 | writes to the stated attribute, along with "impossible" non-writes to |
paulb@150 | 861 | the attribute. |
paulb@150 | 862 | """ |
paulb@150 | 863 | |
paulb@213 | 864 | self.dispatch(storeattr.expr) |
paulb@150 | 865 | expr = self.namespace.types |
paulb@213 | 866 | self.dispatch(storeattr.lvalue) |
paulb@150 | 867 | writes = {} |
paulb@232 | 868 | non_writes = set() |
paulb@150 | 869 | for attr in self.namespace.types: |
paulb@154 | 870 | # NOTE: Impose "atomic" constraints on certain types. |
paulb@150 | 871 | if attr is None: |
paulb@150 | 872 | if not attr in non_writes: |
paulb@232 | 873 | non_writes.add(attr) |
paulb@150 | 874 | continue |
paulb@150 | 875 | attr.type.namespace.add(storeattr.name, expr) |
paulb@150 | 876 | writes[attr.type] = attr.type.namespace.load(storeattr.name) |
paulb@150 | 877 | if not writes: |
paulb@150 | 878 | print "Unable to store attribute", storeattr.name, "given", self.namespace.types |
paulb@150 | 879 | storeattr.writes = writes |
paulb@150 | 880 | storeattr.non_writes = non_writes |
paulb@150 | 881 | |
paulb@150 | 882 | def visitStoreName(self, storename): |
paulb@150 | 883 | |
paulb@150 | 884 | """ |
paulb@213 | 885 | Process the 'storename' node, processing the expression on the node and |
paulb@150 | 886 | associating the type information obtained with the stated name in the |
paulb@150 | 887 | current namespace. |
paulb@150 | 888 | """ |
paulb@150 | 889 | |
paulb@213 | 890 | self.dispatch(storename.expr) |
paulb@150 | 891 | self.namespace.store(storename.name, self.namespace.types) |
paulb@190 | 892 | self.annotate(storename) |
paulb@150 | 893 | |
paulb@150 | 894 | def visitStoreTemp(self, storetemp): |
paulb@150 | 895 | |
paulb@150 | 896 | """ |
paulb@213 | 897 | Process the 'storetemp' node, processing the expression on the node and |
paulb@150 | 898 | associating the type information obtained with a temporary variable in |
paulb@150 | 899 | the current namespace. |
paulb@150 | 900 | """ |
paulb@150 | 901 | |
paulb@213 | 902 | self.dispatch(storetemp.expr) |
paulb@150 | 903 | index = getattr(storetemp, "index", None) |
paulb@150 | 904 | if not self.namespace.temp.has_key(index): |
paulb@150 | 905 | self.namespace.temp[index] = [] |
paulb@150 | 906 | self.namespace.temp[index].append(self.namespace.types) |
paulb@150 | 907 | |
paulb@115 | 908 | def visitSubprogram(self, subprogram): |
paulb@150 | 909 | |
paulb@150 | 910 | """ |
paulb@213 | 911 | Process the 'subprogram' node, processing its contents (a group of nodes |
paulb@150 | 912 | comprising the subprogram). |
paulb@150 | 913 | """ |
paulb@150 | 914 | |
paulb@213 | 915 | self.dispatches(subprogram.code) |
paulb@115 | 916 | |
paulb@115 | 917 | def visitTry(self, try_): |
paulb@150 | 918 | |
paulb@150 | 919 | """ |
paulb@213 | 920 | Process the 'try_' node, processing the body clause in its own namespace |
paulb@150 | 921 | derived from the current namespace, processing any handler clause using |
paulb@150 | 922 | the namespace information accumulated in the body, and processing any |
paulb@150 | 923 | else and finally clauses, attempting to supply each with appropriate |
paulb@150 | 924 | namespace information. |
paulb@150 | 925 | """ |
paulb@150 | 926 | |
paulb@133 | 927 | is_module = self.namespace is self.module.namespace |
paulb@133 | 928 | |
paulb@213 | 929 | self.dispatches(try_.body) |
paulb@133 | 930 | |
paulb@133 | 931 | # Save the namespace from the body. |
paulb@133 | 932 | |
paulb@133 | 933 | body_namespace = Namespace() |
paulb@133 | 934 | body_namespace.merge_namespace(self.namespace) |
paulb@133 | 935 | |
paulb@133 | 936 | # Process the handler. |
paulb@133 | 937 | |
paulb@133 | 938 | if hasattr(try_, "handler"): |
paulb@213 | 939 | self.dispatches(try_.handler) |
paulb@133 | 940 | |
paulb@133 | 941 | # Save the namespace from the handler. |
paulb@133 | 942 | |
paulb@133 | 943 | handler_namespace = Namespace() |
paulb@133 | 944 | handler_namespace.merge_namespace(self.namespace) |
paulb@133 | 945 | |
paulb@133 | 946 | # Remember the raised exceptions encountered so far. |
paulb@133 | 947 | |
paulb@121 | 948 | raises = self.namespace.raises |
paulb@121 | 949 | |
paulb@133 | 950 | # Process the else clause. |
paulb@133 | 951 | |
paulb@133 | 952 | if hasattr(try_, "else_"): |
paulb@133 | 953 | |
paulb@133 | 954 | # Restore the body namespace for the else clause. |
paulb@133 | 955 | |
paulb@133 | 956 | self.namespace = body_namespace |
paulb@133 | 957 | if is_module: |
paulb@133 | 958 | self.module.namespace = self.namespace |
paulb@133 | 959 | |
paulb@133 | 960 | # Empty the raised exceptions for the else clause. |
paulb@121 | 961 | |
paulb@213 | 962 | self.namespace.raises = set() |
paulb@213 | 963 | self.dispatches(try_.else_) |
paulb@133 | 964 | self.namespace.raises = raises |
paulb@133 | 965 | |
paulb@133 | 966 | # Merge the namespaces. |
paulb@133 | 967 | |
paulb@133 | 968 | self.namespace = Namespace() |
paulb@133 | 969 | if is_module: |
paulb@133 | 970 | self.module.namespace = self.namespace |
paulb@133 | 971 | self.namespace.merge_namespace(body_namespace) |
paulb@133 | 972 | self.namespace.merge_namespace(handler_namespace) |
paulb@133 | 973 | |
paulb@133 | 974 | # Process the finally clause, if any. |
paulb@121 | 975 | |
paulb@213 | 976 | self.dispatches(try_.finally_) |
paulb@115 | 977 | |
paulb@203 | 978 | def visitYield(self, yield_): |
paulb@203 | 979 | raise NotImplementedError, "The yield statement is not currently supported." |
paulb@203 | 980 | |
paulb@25 | 981 | # Utility methods. |
paulb@25 | 982 | |
paulb@176 | 983 | def get_builtin_instances(self, node, name): |
paulb@257 | 984 | return set([Attribute(None, self.new_instance(node, attr.type)) for attr in self.builtins_namespace[name]]) |
paulb@79 | 985 | |
paulb@185 | 986 | def new_instance(self, node, type): |
paulb@168 | 987 | |
paulb@176 | 988 | "For the given 'node', obtain an instance from the given 'type'." |
paulb@168 | 989 | |
paulb@176 | 990 | if not type.has_instance(node): |
paulb@168 | 991 | instance = Instance() |
paulb@168 | 992 | instance.namespace = Namespace() |
paulb@213 | 993 | instance.namespace.store("__class__", set([Attribute(None, type)])) |
paulb@176 | 994 | type.add_instance(node, instance) |
paulb@168 | 995 | else: |
paulb@176 | 996 | instance = type.get_instance(node) |
paulb@168 | 997 | |
paulb@168 | 998 | return instance |
paulb@168 | 999 | |
paulb@151 | 1000 | def invoke_subprogram(self, invoke, attribute): |
paulb@34 | 1001 | |
paulb@151 | 1002 | """ |
paulb@151 | 1003 | Invoke using the given 'invoke' node the subprogram represented by the |
paulb@151 | 1004 | given 'attribute'. |
paulb@151 | 1005 | """ |
paulb@34 | 1006 | |
paulb@54 | 1007 | # Test for context information, making it into a real attribute. |
paulb@34 | 1008 | |
paulb@151 | 1009 | if attribute.context is not None: |
paulb@151 | 1010 | context = Attribute(None, attribute.context) |
paulb@151 | 1011 | target = attribute.type |
paulb@34 | 1012 | else: |
paulb@34 | 1013 | context = None |
paulb@151 | 1014 | target = attribute.type |
paulb@50 | 1015 | |
paulb@154 | 1016 | # Test to see if anything has changed. |
paulb@154 | 1017 | |
paulb@179 | 1018 | if hasattr(invoke, "syscount") and invoke.syscount.has_key(target) and invoke.syscount[target] == self.system.count: |
paulb@154 | 1019 | return |
paulb@154 | 1020 | |
paulb@154 | 1021 | # Remember the state of the system. |
paulb@154 | 1022 | |
paulb@154 | 1023 | else: |
paulb@179 | 1024 | if not hasattr(invoke, "syscount"): |
paulb@179 | 1025 | invoke.syscount = {} |
paulb@179 | 1026 | invoke.syscount[target] = self.system.count |
paulb@154 | 1027 | |
paulb@34 | 1028 | # Provide the correct namespace for the invocation. |
paulb@156 | 1029 | # This may be a "shared" namespace... |
paulb@34 | 1030 | |
paulb@268 | 1031 | if invoke.share_locals: |
paulb@34 | 1032 | namespace = Namespace() |
paulb@134 | 1033 | namespace.merge_namespace(self.namespace, everything=0) |
paulb@132 | 1034 | using_module_namespace = self.namespace is self.module.namespace |
paulb@156 | 1035 | |
paulb@156 | 1036 | # Or it may be a structure... |
paulb@156 | 1037 | |
paulb@268 | 1038 | elif target.structure: |
paulb@112 | 1039 | namespace = Namespace() |
paulb@132 | 1040 | using_module_namespace = 0 |
paulb@156 | 1041 | |
paulb@156 | 1042 | # Or it may be a new namespace populated with the supplied parameters. |
paulb@156 | 1043 | |
paulb@34 | 1044 | else: |
paulb@34 | 1045 | items = self.make_items(invoke, target, context) |
paulb@150 | 1046 | namespace = Namespace() |
paulb@150 | 1047 | namespace.merge_items(items) |
paulb@132 | 1048 | using_module_namespace = 0 |
paulb@34 | 1049 | |
paulb@169 | 1050 | # NOTE: Avoid PEP 227 (nested scopes) whilst permitting references to a |
paulb@172 | 1051 | # NOTE: subprogram within itself. Do not define the name of the function |
paulb@172 | 1052 | # NOTE: within a method definition. |
paulb@169 | 1053 | |
paulb@268 | 1054 | if target.name is not None and not target.is_method: |
paulb@213 | 1055 | namespace.store(target.name, set([Attribute(None, target)])) |
paulb@169 | 1056 | |
paulb@34 | 1057 | # Process the subprogram. |
paulb@132 | 1058 | |
paulb@207 | 1059 | self.process_node(target, namespace, using_module_namespace) |
paulb@34 | 1060 | |
paulb@45 | 1061 | # NOTE: Improve and verify this. |
paulb@91 | 1062 | # If the invocation returns a value, acquire the return types. |
paulb@34 | 1063 | |
paulb@268 | 1064 | if target.returns_value: |
paulb@207 | 1065 | self.namespace.set_types(target.returns) |
paulb@34 | 1066 | self.annotate(invoke) |
paulb@34 | 1067 | |
paulb@132 | 1068 | # If it is a normal block, merge the locals. |
paulb@132 | 1069 | # This can happen in addition to the above because for things like |
paulb@132 | 1070 | # logical expressions, the namespace can be modified whilst values are |
paulb@132 | 1071 | # returned as results. |
paulb@91 | 1072 | |
paulb@268 | 1073 | if invoke.share_locals: |
paulb@132 | 1074 | self.namespace.reset() |
paulb@135 | 1075 | |
paulb@135 | 1076 | # Merge the locals snapshots. |
paulb@135 | 1077 | |
paulb@207 | 1078 | for locals in target.return_locals: |
paulb@135 | 1079 | |
paulb@135 | 1080 | # For blocks returning values (such as operations), do not merge |
paulb@135 | 1081 | # snapshots or results. |
paulb@135 | 1082 | |
paulb@268 | 1083 | if target.returns_value: |
paulb@250 | 1084 | self.namespace.merge_namespace(locals, everything=0, temp=0) |
paulb@135 | 1085 | |
paulb@135 | 1086 | # For blocks not returning values (such as loops), merge |
paulb@135 | 1087 | # snapshots and results since they contain details of genuine |
paulb@135 | 1088 | # returns. |
paulb@135 | 1089 | |
paulb@135 | 1090 | else: |
paulb@250 | 1091 | self.namespace.merge_namespace(locals, temp=0) |
paulb@34 | 1092 | |
paulb@116 | 1093 | # Incorporate any raised exceptions. |
paulb@116 | 1094 | |
paulb@213 | 1095 | invoke.raises.update(target.raises) |
paulb@213 | 1096 | self.namespace.raises.update(target.raises) |
paulb@132 | 1097 | |
paulb@83 | 1098 | def process_args(self, invocation): |
paulb@83 | 1099 | |
paulb@112 | 1100 | """ |
paulb@112 | 1101 | Process the arguments associated with an 'invocation'. Return whether |
paulb@112 | 1102 | any arguments were processed. |
paulb@112 | 1103 | """ |
paulb@54 | 1104 | |
paulb@213 | 1105 | self.dispatches(invocation.pos_args) |
paulb@213 | 1106 | self.dispatch_dict(invocation.kw_args) |
paulb@54 | 1107 | |
paulb@54 | 1108 | # Get type information for star and dstar arguments. |
paulb@54 | 1109 | |
paulb@83 | 1110 | if invocation.star is not None: |
paulb@83 | 1111 | param, default = invocation.star |
paulb@213 | 1112 | self.dispatch(default) |
paulb@83 | 1113 | invocation.star = param, default |
paulb@54 | 1114 | |
paulb@83 | 1115 | if invocation.dstar is not None: |
paulb@83 | 1116 | param, default = invocation.dstar |
paulb@213 | 1117 | self.dispatch(default) |
paulb@83 | 1118 | invocation.dstar = param, default |
paulb@54 | 1119 | |
paulb@112 | 1120 | if invocation.pos_args or invocation.kw_args or invocation.star or invocation.dstar: |
paulb@112 | 1121 | return 1 |
paulb@112 | 1122 | else: |
paulb@112 | 1123 | return 0 |
paulb@112 | 1124 | |
paulb@34 | 1125 | def make_items(self, invocation, subprogram, context): |
paulb@34 | 1126 | |
paulb@34 | 1127 | """ |
paulb@34 | 1128 | Make an items mapping for the 'invocation' of the 'subprogram' using the |
paulb@34 | 1129 | given 'context' (which may be None). |
paulb@34 | 1130 | """ |
paulb@34 | 1131 | |
paulb@213 | 1132 | # NOTE: Support class methods! |
paulb@213 | 1133 | |
paulb@213 | 1134 | if context is not None and isinstance(context.type, Instance): |
paulb@93 | 1135 | pos_args = [Self(context)] + invocation.pos_args |
paulb@34 | 1136 | else: |
paulb@93 | 1137 | pos_args = invocation.pos_args |
paulb@154 | 1138 | |
paulb@154 | 1139 | # Duplicate the keyword arguments - we remove them in processing below. |
paulb@154 | 1140 | |
paulb@154 | 1141 | kw_args = {} |
paulb@154 | 1142 | kw_args.update(invocation.kw_args) |
paulb@34 | 1143 | |
paulb@67 | 1144 | # Sort the arguments into positional and keyword arguments. |
paulb@67 | 1145 | |
paulb@24 | 1146 | params = subprogram.params |
paulb@24 | 1147 | items = [] |
paulb@67 | 1148 | star_args = [] |
paulb@272 | 1149 | consumed_args = [] |
paulb@24 | 1150 | |
paulb@67 | 1151 | # Match each positional argument, taking excess arguments as star args. |
paulb@67 | 1152 | |
paulb@67 | 1153 | for arg in pos_args: |
paulb@67 | 1154 | if params: |
paulb@24 | 1155 | param, default = params[0] |
paulb@140 | 1156 | if hasattr(arg, "types"): |
paulb@140 | 1157 | items.append((param, arg.types)) |
paulb@140 | 1158 | else: |
paulb@213 | 1159 | items.append((param, set())) # Annotation has not succeeded. |
paulb@272 | 1160 | |
paulb@272 | 1161 | # Record consumption order. |
paulb@272 | 1162 | |
paulb@272 | 1163 | consumed_args.append(arg) |
paulb@67 | 1164 | params = params[1:] |
paulb@24 | 1165 | else: |
paulb@67 | 1166 | star_args.append(arg) |
paulb@24 | 1167 | |
paulb@24 | 1168 | # Collect the remaining defaults. |
paulb@24 | 1169 | |
paulb@24 | 1170 | while params: |
paulb@24 | 1171 | param, default = params[0] |
paulb@67 | 1172 | if kw_args.has_key(param): |
paulb@67 | 1173 | arg = kw_args[param] |
paulb@73 | 1174 | del kw_args[param] |
paulb@93 | 1175 | elif default is not None: |
paulb@213 | 1176 | self.dispatch(default) |
paulb@213 | 1177 | arg = default |
paulb@93 | 1178 | else: |
paulb@67 | 1179 | raise AnnotationMessage, "No argument supplied in '%s' for parameter '%s'." % (subprogram, param) |
paulb@140 | 1180 | if hasattr(arg, "types"): |
paulb@140 | 1181 | items.append((param, arg.types)) |
paulb@140 | 1182 | else: |
paulb@213 | 1183 | items.append((param, set())) # Annotation has not succeeded. |
paulb@272 | 1184 | |
paulb@272 | 1185 | # Record consumption order (this is not the Keyword node, however). |
paulb@272 | 1186 | |
paulb@272 | 1187 | consumed_args.append(arg) |
paulb@24 | 1188 | params = params[1:] |
paulb@24 | 1189 | |
paulb@162 | 1190 | dstar_args = kw_args.items() |
paulb@73 | 1191 | |
paulb@73 | 1192 | # Construct temporary objects. |
paulb@73 | 1193 | |
paulb@73 | 1194 | if star_args: |
paulb@80 | 1195 | star_invocation = self.make_star_args(invocation, subprogram, star_args) |
paulb@80 | 1196 | self.dispatch(star_invocation) |
paulb@80 | 1197 | star_types = star_invocation.types |
paulb@275 | 1198 | consumed_args.append(star_args) |
paulb@73 | 1199 | else: |
paulb@73 | 1200 | star_types = None |
paulb@275 | 1201 | consumed_args.append(None) |
paulb@73 | 1202 | |
paulb@73 | 1203 | if dstar_args: |
paulb@162 | 1204 | dstar_invocation = self.make_dstar_args(invocation, subprogram, dstar_args) |
paulb@80 | 1205 | self.dispatch(dstar_invocation) |
paulb@80 | 1206 | dstar_types = dstar_invocation.types |
paulb@275 | 1207 | consumed_args.append(dstar_args) |
paulb@73 | 1208 | else: |
paulb@73 | 1209 | dstar_types = None |
paulb@275 | 1210 | consumed_args.append(None) |
paulb@73 | 1211 | |
paulb@73 | 1212 | # NOTE: Merge the objects properly. |
paulb@73 | 1213 | |
paulb@73 | 1214 | star_types = star_types or invocation.star and invocation.star.types |
paulb@73 | 1215 | dstar_types = dstar_types or invocation.dstar and invocation.dstar.types |
paulb@73 | 1216 | |
paulb@24 | 1217 | # Add star and dstar. |
paulb@24 | 1218 | |
paulb@73 | 1219 | if star_types is not None: |
paulb@24 | 1220 | if subprogram.star is not None: |
paulb@24 | 1221 | param, default = subprogram.star |
paulb@73 | 1222 | items.append((param, star_types)) |
paulb@24 | 1223 | else: |
paulb@67 | 1224 | raise AnnotationMessage, "Invocation provides unwanted *args." |
paulb@24 | 1225 | elif subprogram.star is not None: |
paulb@24 | 1226 | param, default = subprogram.star |
paulb@264 | 1227 | if not hasattr(default, "types") or not default.types: |
paulb@264 | 1228 | self.dispatch(default) # NOTE: Review reprocessing. |
paulb@154 | 1229 | items.append((param, default.types)) |
paulb@24 | 1230 | |
paulb@73 | 1231 | if dstar_types is not None: |
paulb@24 | 1232 | if subprogram.dstar is not None: |
paulb@24 | 1233 | param, default = subprogram.dstar |
paulb@73 | 1234 | items.append((param, dstar_types)) |
paulb@24 | 1235 | else: |
paulb@67 | 1236 | raise AnnotationMessage, "Invocation provides unwanted **args." |
paulb@24 | 1237 | elif subprogram.dstar is not None: |
paulb@24 | 1238 | param, default = subprogram.dstar |
paulb@264 | 1239 | if not hasattr(default, "types") or not default.types: |
paulb@264 | 1240 | self.dispatch(default) # NOTE: Review reprocessing. |
paulb@154 | 1241 | items.append((param, default.types)) |
paulb@24 | 1242 | |
paulb@83 | 1243 | # Record the parameter types. |
paulb@83 | 1244 | |
paulb@146 | 1245 | self.annotate_parameters(subprogram, items) |
paulb@272 | 1246 | invocation.consumed_args[subprogram] = consumed_args |
paulb@146 | 1247 | return subprogram.paramtypes.items() |
paulb@24 | 1248 | |
paulb@80 | 1249 | def make_star_args(self, invocation, subprogram, star_args): |
paulb@80 | 1250 | |
paulb@80 | 1251 | "Make a subprogram which initialises a list containing 'star_args'." |
paulb@80 | 1252 | |
paulb@80 | 1253 | if not hasattr(invocation, "stars"): |
paulb@80 | 1254 | invocation.stars = {} |
paulb@80 | 1255 | |
paulb@80 | 1256 | if not invocation.stars.has_key(subprogram.full_name()): |
paulb@204 | 1257 | instance = getattr(invocation, "instance", None) |
paulb@204 | 1258 | |
paulb@219 | 1259 | code = [ |
paulb@204 | 1260 | Return( |
paulb@204 | 1261 | instance=instance, |
paulb@219 | 1262 | expr=MakeTuple( |
paulb@204 | 1263 | instance=instance, |
paulb@219 | 1264 | nodes=star_args |
paulb@204 | 1265 | ) |
paulb@204 | 1266 | ) |
paulb@80 | 1267 | ] |
paulb@80 | 1268 | |
paulb@207 | 1269 | new_subprogram = Subprogram( |
paulb@207 | 1270 | instance=instance, |
paulb@207 | 1271 | name=None, |
paulb@207 | 1272 | returns_value=1, |
paulb@207 | 1273 | params=[], |
paulb@207 | 1274 | star=None, |
paulb@207 | 1275 | dstar=None, |
paulb@207 | 1276 | code=code |
paulb@207 | 1277 | ) |
paulb@219 | 1278 | |
paulb@207 | 1279 | subprogram.module.simplifier.subnames[new_subprogram.full_name()] = new_subprogram |
paulb@207 | 1280 | |
paulb@204 | 1281 | invocation.stars[subprogram.full_name()] = InvokeRef( |
paulb@185 | 1282 | invocation.original, |
paulb@204 | 1283 | instance=instance, |
paulb@80 | 1284 | produces_result=1, |
paulb@207 | 1285 | ref=new_subprogram |
paulb@80 | 1286 | ) |
paulb@80 | 1287 | |
paulb@80 | 1288 | return invocation.stars[subprogram.full_name()] |
paulb@80 | 1289 | |
paulb@162 | 1290 | def make_dstar_args(self, invocation, subprogram, dstar_args): |
paulb@162 | 1291 | |
paulb@162 | 1292 | """ |
paulb@162 | 1293 | Make a subprogram which initialises a dictionary built from the given |
paulb@162 | 1294 | 'dstar_args'. |
paulb@162 | 1295 | """ |
paulb@162 | 1296 | |
paulb@162 | 1297 | if not hasattr(invocation, "dstars"): |
paulb@162 | 1298 | invocation.dstars = {} |
paulb@162 | 1299 | |
paulb@162 | 1300 | if not invocation.dstars.has_key(subprogram.full_name()): |
paulb@204 | 1301 | instance = getattr(invocation, "instance", None) |
paulb@204 | 1302 | |
paulb@162 | 1303 | code=[ |
paulb@162 | 1304 | StoreTemp( |
paulb@204 | 1305 | instance=instance, |
paulb@162 | 1306 | expr=InvokeFunction( |
paulb@185 | 1307 | invocation.original, |
paulb@204 | 1308 | instance=instance, |
paulb@162 | 1309 | expr=LoadAttr( |
paulb@204 | 1310 | instance=instance, |
paulb@162 | 1311 | expr=LoadRef( |
paulb@204 | 1312 | instance=instance, |
paulb@162 | 1313 | ref=self.builtins |
paulb@162 | 1314 | ), |
paulb@162 | 1315 | name="dict", |
paulb@162 | 1316 | nstype="module", |
paulb@162 | 1317 | ) |
paulb@162 | 1318 | ) |
paulb@162 | 1319 | ) |
paulb@162 | 1320 | ] |
paulb@162 | 1321 | |
paulb@162 | 1322 | for arg, value in dstar_args: |
paulb@162 | 1323 | |
paulb@162 | 1324 | # NOTE: Constant not added to table. |
paulb@162 | 1325 | |
paulb@162 | 1326 | code += [ |
paulb@162 | 1327 | StoreTemp( |
paulb@204 | 1328 | instance=instance, |
paulb@274 | 1329 | expr=Constant( |
paulb@204 | 1330 | instance=instance, |
paulb@274 | 1331 | name=repr(arg), |
paulb@274 | 1332 | value=arg |
paulb@162 | 1333 | ), |
paulb@162 | 1334 | index="const" |
paulb@162 | 1335 | ), |
paulb@162 | 1336 | InvokeFunction( |
paulb@185 | 1337 | invocation.original, |
paulb@204 | 1338 | instance=instance, |
paulb@162 | 1339 | expr=LoadAttr( |
paulb@204 | 1340 | instance=instance, |
paulb@204 | 1341 | expr=LoadTemp( |
paulb@204 | 1342 | instance=instance |
paulb@204 | 1343 | ), |
paulb@162 | 1344 | name="__setitem__" |
paulb@162 | 1345 | ), |
paulb@162 | 1346 | args=[ |
paulb@162 | 1347 | LoadTemp( |
paulb@204 | 1348 | instance=instance, |
paulb@162 | 1349 | index="const", |
paulb@162 | 1350 | release=1 |
paulb@162 | 1351 | ), |
paulb@162 | 1352 | value |
paulb@162 | 1353 | ] |
paulb@162 | 1354 | ) |
paulb@162 | 1355 | ] |
paulb@162 | 1356 | |
paulb@162 | 1357 | code += [ |
paulb@204 | 1358 | Return( |
paulb@204 | 1359 | instance=instance, |
paulb@204 | 1360 | expr=LoadTemp( |
paulb@204 | 1361 | instance=instance, |
paulb@204 | 1362 | release=1 |
paulb@204 | 1363 | ) |
paulb@204 | 1364 | ) |
paulb@162 | 1365 | ] |
paulb@162 | 1366 | |
paulb@207 | 1367 | new_subprogram = Subprogram( |
paulb@207 | 1368 | instance=instance, |
paulb@207 | 1369 | name=None, |
paulb@207 | 1370 | returns_value=1, |
paulb@207 | 1371 | params=[], |
paulb@207 | 1372 | star=None, |
paulb@207 | 1373 | dstar=None, |
paulb@207 | 1374 | code=code |
paulb@207 | 1375 | ) |
paulb@207 | 1376 | subprogram.module.simplifier.subnames[new_subprogram.full_name()] = new_subprogram |
paulb@207 | 1377 | |
paulb@204 | 1378 | invocation.dstars[subprogram.full_name()] = InvokeRef( |
paulb@185 | 1379 | invocation.original, |
paulb@204 | 1380 | instance=instance, |
paulb@162 | 1381 | produces_result=1, |
paulb@207 | 1382 | ref=new_subprogram |
paulb@162 | 1383 | ) |
paulb@162 | 1384 | |
paulb@162 | 1385 | return invocation.dstars[subprogram.full_name()] |
paulb@162 | 1386 | |
paulb@48 | 1387 | # Namespace-related abstractions. |
paulb@48 | 1388 | |
paulb@73 | 1389 | def combine(target, additions): |
paulb@91 | 1390 | |
paulb@91 | 1391 | """ |
paulb@91 | 1392 | Merge into the 'target' sequence the given 'additions', preventing duplicate |
paulb@91 | 1393 | items. |
paulb@91 | 1394 | """ |
paulb@91 | 1395 | |
paulb@73 | 1396 | for addition in additions: |
paulb@73 | 1397 | if addition not in target: |
paulb@73 | 1398 | target.append(addition) |
paulb@73 | 1399 | |
paulb@48 | 1400 | def find_attributes(structure, name): |
paulb@48 | 1401 | |
paulb@48 | 1402 | """ |
paulb@48 | 1403 | Find for the given 'structure' all attributes for the given 'name', visiting |
paulb@48 | 1404 | base classes where appropriate and returning the attributes in order of |
paulb@48 | 1405 | descending precedence for all possible base classes. |
paulb@48 | 1406 | |
paulb@48 | 1407 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1408 | the structure involved in accessing the attribute. |
paulb@48 | 1409 | """ |
paulb@48 | 1410 | |
paulb@48 | 1411 | # First attempt to search the instance/class namespace. |
paulb@48 | 1412 | |
paulb@48 | 1413 | try: |
paulb@48 | 1414 | l = structure.namespace.load(name) |
paulb@48 | 1415 | attributes = [] |
paulb@48 | 1416 | for attribute in l: |
paulb@48 | 1417 | attributes.append((attribute, structure)) |
paulb@48 | 1418 | |
paulb@48 | 1419 | # If that does not work, attempt to investigate any class or base classes. |
paulb@48 | 1420 | |
paulb@48 | 1421 | except KeyError: |
paulb@48 | 1422 | attributes = [] |
paulb@48 | 1423 | |
paulb@48 | 1424 | # Investigate any instance's implementing class. |
paulb@48 | 1425 | |
paulb@48 | 1426 | if isinstance(structure, Instance): |
paulb@73 | 1427 | for attr in structure.namespace.load("__class__"): |
paulb@73 | 1428 | cls = attr.type |
paulb@50 | 1429 | l = get_attributes(cls, name) |
paulb@73 | 1430 | combine(attributes, l) |
paulb@48 | 1431 | |
paulb@48 | 1432 | # Investigate any class's base classes. |
paulb@48 | 1433 | |
paulb@249 | 1434 | elif isinstance(structure, GeneralClass): |
paulb@48 | 1435 | |
paulb@48 | 1436 | # If no base classes exist, return an indicator that no attribute |
paulb@48 | 1437 | # exists. |
paulb@48 | 1438 | |
paulb@48 | 1439 | if not structure.base_refs: |
paulb@48 | 1440 | return [(None, structure)] |
paulb@48 | 1441 | |
paulb@48 | 1442 | # Otherwise, find all possible base classes. |
paulb@48 | 1443 | |
paulb@48 | 1444 | for base_refs in structure.base_refs: |
paulb@48 | 1445 | base_attributes = [] |
paulb@48 | 1446 | |
paulb@48 | 1447 | # For each base class, find attributes either in the base |
paulb@48 | 1448 | # class or its own base classes. |
paulb@48 | 1449 | |
paulb@48 | 1450 | for base_ref in base_refs: |
paulb@50 | 1451 | l = get_attributes(base_ref, name) |
paulb@73 | 1452 | combine(base_attributes, l) |
paulb@48 | 1453 | |
paulb@73 | 1454 | combine(attributes, base_attributes) |
paulb@48 | 1455 | |
paulb@49 | 1456 | return attributes |
paulb@48 | 1457 | |
paulb@48 | 1458 | def get_attributes(structure, name): |
paulb@48 | 1459 | |
paulb@48 | 1460 | """ |
paulb@48 | 1461 | Return all possible attributes for the given 'structure' having the given |
paulb@48 | 1462 | 'name', wrapping each attribute in an Attribute object which includes |
paulb@48 | 1463 | context information for the attribute access. |
paulb@48 | 1464 | |
paulb@48 | 1465 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1466 | the structure involved in accessing the attribute. |
paulb@48 | 1467 | """ |
paulb@48 | 1468 | |
paulb@48 | 1469 | if isinstance(structure, Attribute): |
paulb@48 | 1470 | structure = structure.type |
paulb@244 | 1471 | |
paulb@53 | 1472 | results = [] |
paulb@53 | 1473 | for attribute, accessor in find_attributes(structure, name): |
paulb@178 | 1474 | |
paulb@178 | 1475 | # Detect class attribute access via instances. |
paulb@178 | 1476 | |
paulb@249 | 1477 | if attribute is not None and isinstance(structure, Instance) and isinstance(accessor, GeneralClass): |
paulb@178 | 1478 | attribute = accessor.get_attribute_for_instance(attribute, structure) |
paulb@178 | 1479 | |
paulb@178 | 1480 | # Produce an attribute with the appropriate context. |
paulb@244 | 1481 | # Modules should not affect the context. |
paulb@178 | 1482 | |
paulb@244 | 1483 | if attribute is not None and isinstance(structure, Module): |
paulb@244 | 1484 | results.append((attribute, accessor)) |
paulb@244 | 1485 | |
paulb@244 | 1486 | # Access via classes and instances should set those as the context. |
paulb@244 | 1487 | |
paulb@244 | 1488 | elif attribute is not None and isinstance(structure, Structure): |
paulb@53 | 1489 | results.append((Attribute(structure, attribute.type), accessor)) |
paulb@244 | 1490 | |
paulb@244 | 1491 | # Other accesses are returned unmodified. |
paulb@244 | 1492 | |
paulb@53 | 1493 | else: |
paulb@53 | 1494 | results.append((attribute, accessor)) |
paulb@178 | 1495 | |
paulb@53 | 1496 | return results |
paulb@48 | 1497 | |
paulb@207 | 1498 | def prompt(vars): |
paulb@207 | 1499 | try: |
paulb@207 | 1500 | while 1: |
paulb@207 | 1501 | s = raw_input("> ") |
paulb@213 | 1502 | print eval(s, vars) |
paulb@207 | 1503 | except EOFError: |
paulb@207 | 1504 | pass |
paulb@207 | 1505 | |
paulb@57 | 1506 | # Convenience functions. |
paulb@57 | 1507 | |
paulb@249 | 1508 | def annotate(module, builtins, importer): |
paulb@156 | 1509 | |
paulb@156 | 1510 | """ |
paulb@249 | 1511 | Annotate the given 'module', also employing the 'builtins' module which may |
paulb@249 | 1512 | be specified as None. Use the 'importer' to find and load modules. |
paulb@57 | 1513 | """ |
paulb@57 | 1514 | |
paulb@156 | 1515 | annotator = Annotator(importer) |
paulb@57 | 1516 | if builtins is not None: |
paulb@57 | 1517 | annotator.process(module, builtins) |
paulb@57 | 1518 | else: |
paulb@57 | 1519 | annotator.process(module) |
paulb@57 | 1520 | |
paulb@19 | 1521 | # vim: tabstop=4 expandtab shiftwidth=4 |