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