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@19 | 10 | This software is free software; you can redistribute it and/or |
paulb@19 | 11 | modify it under the terms of the GNU General Public License as |
paulb@19 | 12 | published by the Free Software Foundation; either version 2 of |
paulb@19 | 13 | the License, or (at your option) any later version. |
paulb@19 | 14 | |
paulb@19 | 15 | This software is distributed in the hope that it will be useful, |
paulb@19 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@19 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@19 | 18 | GNU General Public License for more details. |
paulb@19 | 19 | |
paulb@19 | 20 | You should have received a copy of the GNU General Public |
paulb@19 | 21 | License along with this library; see the file LICENCE.txt |
paulb@19 | 22 | If not, write to the Free Software Foundation, Inc., |
paulb@19 | 23 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@50 | 24 | |
paulb@50 | 25 | -------- |
paulb@50 | 26 | |
paulb@156 | 27 | To use this module, the easiest approach is to use the load function: |
paulb@156 | 28 | |
paulb@156 | 29 | load(filename, builtins) |
paulb@156 | 30 | |
paulb@156 | 31 | To control module importing, an importer should be constructed and employed. |
paulb@156 | 32 | Here, the standard path for module searching is used: |
paulb@156 | 33 | |
paulb@156 | 34 | importer = Importer(sys.path) |
paulb@156 | 35 | load(filename, builtins, importer) |
paulb@156 | 36 | |
paulb@156 | 37 | Underneath the load function, the annotate function provides support for |
paulb@156 | 38 | annotating modules already processed by simplify and fixnames: |
paulb@57 | 39 | |
paulb@57 | 40 | annotate(module, builtins) |
paulb@57 | 41 | |
paulb@156 | 42 | And at the most basic level, the most intricate approach involves obtaining an |
paulb@156 | 43 | Annotator object: |
paulb@50 | 44 | |
paulb@50 | 45 | annotator = Annotator() |
paulb@50 | 46 | |
paulb@57 | 47 | Then, processing an existing module with it: |
paulb@50 | 48 | |
paulb@57 | 49 | annotator.process(module) |
paulb@50 | 50 | |
paulb@50 | 51 | If a module containing built-in classes and functions has already been |
paulb@50 | 52 | annotated, such a module should be passed in as an additional argument: |
paulb@50 | 53 | |
paulb@57 | 54 | annotator.process(module, builtins) |
paulb@19 | 55 | """ |
paulb@19 | 56 | |
paulb@220 | 57 | from simplify.simplified import * |
paulb@220 | 58 | import simplify, simplify.fixnames # for the load function |
paulb@19 | 59 | import compiler |
paulb@156 | 60 | import os |
paulb@19 | 61 | |
paulb@24 | 62 | class System: |
paulb@24 | 63 | |
paulb@50 | 64 | """ |
paulb@50 | 65 | A class maintaining the state of the annotation system. When the system |
paulb@50 | 66 | counter can no longer be incremented by any annotation operation, the |
paulb@50 | 67 | system may be considered stable and fully annotated. |
paulb@50 | 68 | """ |
paulb@24 | 69 | |
paulb@24 | 70 | def __init__(self): |
paulb@24 | 71 | self.count = 0 |
paulb@146 | 72 | |
paulb@212 | 73 | def init(self, node, attr="types"): |
paulb@150 | 74 | |
paulb@150 | 75 | "Initialise a 'node' for annotation." |
paulb@150 | 76 | |
paulb@212 | 77 | if not hasattr(node, attr): |
paulb@213 | 78 | setattr(node, attr, set()) |
paulb@146 | 79 | |
paulb@212 | 80 | def annotate(self, node, types, attr="types"): |
paulb@150 | 81 | |
paulb@150 | 82 | "Annotate the given 'node' with the given 'types'." |
paulb@150 | 83 | |
paulb@212 | 84 | self.init(node, attr) |
paulb@212 | 85 | self.combine(getattr(node, attr), types) |
paulb@146 | 86 | |
paulb@146 | 87 | def combine(self, target, types): |
paulb@150 | 88 | |
paulb@150 | 89 | """ |
paulb@150 | 90 | Combine the 'target' list with the given 'types', counting new members. |
paulb@150 | 91 | """ |
paulb@150 | 92 | |
paulb@24 | 93 | for type in types: |
paulb@146 | 94 | if type not in target: |
paulb@213 | 95 | target.add(type) |
paulb@24 | 96 | self.count += 1 |
paulb@24 | 97 | |
paulb@24 | 98 | system = System() |
paulb@24 | 99 | |
paulb@58 | 100 | # Exceptions. |
paulb@58 | 101 | |
paulb@83 | 102 | class AnnotationError(SimplifiedError): |
paulb@83 | 103 | |
paulb@83 | 104 | "An error in the annotation process." |
paulb@83 | 105 | |
paulb@83 | 106 | pass |
paulb@58 | 107 | |
paulb@67 | 108 | class AnnotationMessage(Exception): |
paulb@83 | 109 | |
paulb@83 | 110 | "A lesser annotation error." |
paulb@83 | 111 | |
paulb@67 | 112 | pass |
paulb@67 | 113 | |
paulb@24 | 114 | # Annotation. |
paulb@19 | 115 | |
paulb@20 | 116 | class Annotator(Visitor): |
paulb@24 | 117 | |
paulb@24 | 118 | """ |
paulb@24 | 119 | The type annotator which traverses the program nodes, typically depth-first, |
paulb@24 | 120 | and maintains a record of the current set of types applying to the currently |
paulb@24 | 121 | considered operation. Such types are also recorded on the nodes, and a |
paulb@24 | 122 | special "system" record is maintained to monitor the level of annotation |
paulb@24 | 123 | activity with a view to recognising when no more annotations are possible. |
paulb@50 | 124 | |
paulb@50 | 125 | Throughout the annotation activity, type information consists of lists of |
paulb@50 | 126 | Attribute objects where such objects retain information about the context of |
paulb@50 | 127 | the type (since a value in the program may be associated with an object or |
paulb@50 | 128 | class) and the actual type of the value being manipulated. Upon accessing |
paulb@50 | 129 | attribute information on namespaces, additional accessor information is also |
paulb@50 | 130 | exchanged - this provides a means of distinguishing between the different |
paulb@50 | 131 | types possible when the means of constructing the namespace may depend on |
paulb@50 | 132 | run-time behaviour. |
paulb@151 | 133 | |
paulb@204 | 134 | Covered: Assign, CheckType, Conditional, Global, Import, InvokeRef, |
paulb@156 | 135 | InvokeFunction, LoadAttr, LoadExc, LoadName, LoadRef, LoadTemp, |
paulb@156 | 136 | Module, Not, Pass, Raise, ReleaseTemp, ReturnFromBlock, |
paulb@156 | 137 | ReturnFromFunction, StoreAttr, StoreName, StoreTemp, Subprogram, |
paulb@156 | 138 | Try. |
paulb@24 | 139 | """ |
paulb@24 | 140 | |
paulb@156 | 141 | def __init__(self, importer=None): |
paulb@50 | 142 | |
paulb@156 | 143 | "Initialise the visitor with an optional 'importer'." |
paulb@50 | 144 | |
paulb@19 | 145 | Visitor.__init__(self) |
paulb@24 | 146 | self.system = system |
paulb@156 | 147 | self.importer = importer or Importer() |
paulb@19 | 148 | |
paulb@30 | 149 | # Satisfy visitor issues. |
paulb@30 | 150 | |
paulb@30 | 151 | self.visitor = self |
paulb@30 | 152 | |
paulb@57 | 153 | def process(self, module, builtins=None): |
paulb@45 | 154 | |
paulb@45 | 155 | """ |
paulb@57 | 156 | Process the given 'module', using the optional 'builtins' to access |
paulb@57 | 157 | built-in classes and functions. |
paulb@45 | 158 | """ |
paulb@45 | 159 | |
paulb@45 | 160 | self.subprograms = [] |
paulb@45 | 161 | self.current_subprograms = [] |
paulb@45 | 162 | self.current_namespaces = [] |
paulb@204 | 163 | self.rerun_subprograms = {} |
paulb@115 | 164 | self.namespace = None |
paulb@121 | 165 | self.module = module |
paulb@34 | 166 | |
paulb@34 | 167 | # Process the module, supplying builtins if possible. |
paulb@34 | 168 | |
paulb@80 | 169 | self.builtins = builtins |
paulb@45 | 170 | self.global_namespace = Namespace() |
paulb@45 | 171 | |
paulb@57 | 172 | if builtins is not None: |
paulb@57 | 173 | self.builtins_namespace = builtins.namespace |
paulb@34 | 174 | else: |
paulb@45 | 175 | self.builtins_namespace = self.global_namespace |
paulb@34 | 176 | |
paulb@207 | 177 | # NOTE: Not declaring module namespace usage, even though it is used. |
paulb@45 | 178 | |
paulb@213 | 179 | self.process_node(module, self.global_namespace, 0) |
paulb@207 | 180 | |
paulb@207 | 181 | def process_node(self, node, locals, using_module_namespace): |
paulb@24 | 182 | |
paulb@24 | 183 | """ |
paulb@132 | 184 | Process a subprogram or module 'node', indicating the initial 'locals'. |
paulb@213 | 185 | Note that this method may mutate nodes in the original program. |
paulb@24 | 186 | """ |
paulb@24 | 187 | |
paulb@204 | 188 | # Recursion test. |
paulb@204 | 189 | |
paulb@204 | 190 | if node in self.current_subprograms: |
paulb@204 | 191 | if not self.rerun_subprograms.has_key(node): |
paulb@204 | 192 | self.rerun_subprograms[node] = [] |
paulb@204 | 193 | self.rerun_subprograms[node].append(locals) |
paulb@213 | 194 | return |
paulb@204 | 195 | |
paulb@115 | 196 | # Record the current subprogram and namespace. |
paulb@115 | 197 | |
paulb@115 | 198 | self.current_subprograms.append(node) |
paulb@115 | 199 | |
paulb@67 | 200 | # Determine the namespace. |
paulb@67 | 201 | |
paulb@132 | 202 | self.current_namespaces.append(self.namespace) |
paulb@132 | 203 | self.namespace = locals |
paulb@45 | 204 | |
paulb@25 | 205 | # Add namespace details to any structure involved. |
paulb@25 | 206 | |
paulb@34 | 207 | if getattr(node, "structure", None) is not None: |
paulb@34 | 208 | node.structure.namespace = Namespace() |
paulb@25 | 209 | |
paulb@30 | 210 | # Initialise bases where appropriate. |
paulb@19 | 211 | |
paulb@30 | 212 | if hasattr(node.structure, "bases"): |
paulb@30 | 213 | base_refs = [] |
paulb@30 | 214 | for base in node.structure.bases: |
paulb@30 | 215 | self.dispatch(base) |
paulb@49 | 216 | base_refs.append(self.namespace.types) |
paulb@30 | 217 | node.structure.base_refs = base_refs |
paulb@30 | 218 | |
paulb@30 | 219 | # Dispatch to the code itself. |
paulb@30 | 220 | |
paulb@49 | 221 | node.namespace = self.namespace |
paulb@207 | 222 | self.set_module_namespace(using_module_namespace) |
paulb@207 | 223 | |
paulb@213 | 224 | self.dispatch(node) |
paulb@213 | 225 | self.extract_results(node) |
paulb@204 | 226 | |
paulb@204 | 227 | while self.rerun_subprograms.has_key(node): |
paulb@204 | 228 | all_rerun_locals = self.rerun_subprograms[node] |
paulb@204 | 229 | del self.rerun_subprograms[node] |
paulb@204 | 230 | for rerun_locals in all_rerun_locals: |
paulb@209 | 231 | #print "Re-running", node, "with", rerun_locals |
paulb@204 | 232 | |
paulb@207 | 233 | self.namespace = rerun_locals |
paulb@207 | 234 | node.namespace = rerun_locals |
paulb@207 | 235 | self.set_module_namespace(using_module_namespace) |
paulb@49 | 236 | |
paulb@213 | 237 | self.dispatch(node) |
paulb@213 | 238 | self.extract_results(node) |
paulb@45 | 239 | |
paulb@45 | 240 | # Restore the previous subprogram and namespace. |
paulb@45 | 241 | |
paulb@132 | 242 | self.namespace = self.current_namespaces.pop() |
paulb@45 | 243 | self.current_subprograms.pop() |
paulb@207 | 244 | self.reset_module_namespace(using_module_namespace) |
paulb@45 | 245 | |
paulb@207 | 246 | def set_module_namespace(self, using_module_namespace): |
paulb@207 | 247 | |
paulb@207 | 248 | """ |
paulb@207 | 249 | In order to keep global accesses working, the module namespace must be |
paulb@207 | 250 | adjusted. |
paulb@207 | 251 | """ |
paulb@207 | 252 | |
paulb@207 | 253 | if using_module_namespace: |
paulb@207 | 254 | self.module.namespace = self.namespace |
paulb@207 | 255 | |
paulb@207 | 256 | def reset_module_namespace(self, using_module_namespace): |
paulb@207 | 257 | |
paulb@207 | 258 | """ |
paulb@207 | 259 | In order to keep global accesses working, the module namespace must be |
paulb@207 | 260 | reset. |
paulb@207 | 261 | """ |
paulb@207 | 262 | |
paulb@207 | 263 | if using_module_namespace: |
paulb@207 | 264 | self.module.namespace = self.namespace |
paulb@207 | 265 | |
paulb@213 | 266 | def extract_results(self, node): |
paulb@207 | 267 | |
paulb@207 | 268 | "Extract results from the namespace." |
paulb@207 | 269 | |
paulb@213 | 270 | node.namespace = self.namespace |
paulb@213 | 271 | self.system.annotate(node, self.namespace.raises, "raises") |
paulb@213 | 272 | self.system.annotate(node, self.namespace.returns, "returns") |
paulb@213 | 273 | if hasattr(node, "return_locals"): |
paulb@213 | 274 | node.return_locals.update(self.namespace.return_locals) |
paulb@207 | 275 | |
paulb@83 | 276 | def annotate(self, node, types=None): |
paulb@24 | 277 | |
paulb@83 | 278 | """ |
paulb@83 | 279 | Annotate the given 'node' in the system, using either the optional |
paulb@83 | 280 | 'types' or the namespace's current type information. |
paulb@83 | 281 | """ |
paulb@24 | 282 | |
paulb@207 | 283 | if types is None: |
paulb@207 | 284 | self.system.annotate(node, self.namespace.types) |
paulb@207 | 285 | else: |
paulb@207 | 286 | self.system.annotate(node, types) |
paulb@34 | 287 | |
paulb@146 | 288 | def annotate_parameters(self, node, items): |
paulb@146 | 289 | |
paulb@146 | 290 | """ |
paulb@146 | 291 | Annotate the given 'node' using the given 'items' and updating the |
paulb@146 | 292 | system's annotation counter. |
paulb@146 | 293 | """ |
paulb@146 | 294 | |
paulb@146 | 295 | if not hasattr(node, "paramtypes"): |
paulb@146 | 296 | node.paramtypes = {} |
paulb@146 | 297 | |
paulb@146 | 298 | for param, types in items: |
paulb@146 | 299 | if not node.paramtypes.has_key(param): |
paulb@213 | 300 | node.paramtypes[param] = set() |
paulb@146 | 301 | self.system.combine(node.paramtypes[param], types) |
paulb@146 | 302 | |
paulb@25 | 303 | # Visitor methods. |
paulb@25 | 304 | |
paulb@19 | 305 | def default(self, node): |
paulb@24 | 306 | |
paulb@24 | 307 | """ |
paulb@24 | 308 | Process the given 'node', given that it does not have a specific |
paulb@24 | 309 | handler. |
paulb@24 | 310 | """ |
paulb@24 | 311 | |
paulb@115 | 312 | raise AnnotationMessage, "Node '%s' not supported." % node |
paulb@20 | 313 | |
paulb@20 | 314 | def dispatch(self, node, *args): |
paulb@34 | 315 | try: |
paulb@213 | 316 | Visitor.dispatch(self, node, *args) |
paulb@67 | 317 | except AnnotationError, exc: |
paulb@58 | 318 | exc.add(node) |
paulb@34 | 319 | raise |
paulb@67 | 320 | except AnnotationMessage, exc: |
paulb@67 | 321 | raise AnnotationError(exc, node) |
paulb@19 | 322 | |
paulb@150 | 323 | # Specific node methods. |
paulb@110 | 324 | |
paulb@115 | 325 | def visitAssign(self, assign): |
paulb@150 | 326 | |
paulb@150 | 327 | """ |
paulb@213 | 328 | Process the 'assign' node and its contents. |
paulb@150 | 329 | """ |
paulb@150 | 330 | |
paulb@213 | 331 | self.dispatches(assign.code) |
paulb@115 | 332 | |
paulb@193 | 333 | def visitCheckType(self, checktype): |
paulb@150 | 334 | |
paulb@150 | 335 | """ |
paulb@213 | 336 | Process the 'checktype' node, finding the possible types of the |
paulb@213 | 337 | exception, and processing each choice to build a list of checked types |
paulb@213 | 338 | for the exception. |
paulb@150 | 339 | """ |
paulb@150 | 340 | |
paulb@193 | 341 | inverted = getattr(checktype, "inverted", 0) |
paulb@213 | 342 | self.dispatch(checktype.expr) |
paulb@194 | 343 | |
paulb@150 | 344 | expr_types = self.namespace.types |
paulb@213 | 345 | choice_types = set() |
paulb@150 | 346 | choices = [] |
paulb@194 | 347 | |
paulb@193 | 348 | for choice in checktype.choices: |
paulb@150 | 349 | choices.append(self.dispatch(choice)) |
paulb@213 | 350 | choice_types.update(self.namespace.types) |
paulb@194 | 351 | |
paulb@150 | 352 | for expr_type in expr_types: |
paulb@193 | 353 | in_choices = expr_type.type.get_class() in choice_types |
paulb@194 | 354 | |
paulb@194 | 355 | # Filter out types not in the choices list unless the operation is |
paulb@194 | 356 | # inverted; in which case, filter out types in the choices list. |
paulb@194 | 357 | |
paulb@193 | 358 | if not inverted and not in_choices or inverted and in_choices: |
paulb@193 | 359 | self._prune_non_accesses(checktype.expr, expr_type) |
paulb@194 | 360 | |
paulb@110 | 361 | def visitConditional(self, conditional): |
paulb@19 | 362 | |
paulb@150 | 363 | """ |
paulb@213 | 364 | Process the 'conditional' node, processing the test, body and else |
paulb@150 | 365 | clauses and recording their processed forms. The body and else clauses |
paulb@150 | 366 | are processed within their own namespaces, and the test is also |
paulb@150 | 367 | processed in its own namespace if 'isolate_test' is set on the |
paulb@150 | 368 | 'conditional' node. |
paulb@150 | 369 | """ |
paulb@150 | 370 | |
paulb@110 | 371 | # Conditionals keep local namespace changes isolated. |
paulb@110 | 372 | # With Return nodes inside the body/else sections, the changes are |
paulb@110 | 373 | # communicated to the caller. |
paulb@19 | 374 | |
paulb@133 | 375 | is_module = self.namespace is self.module.namespace |
paulb@133 | 376 | |
paulb@133 | 377 | # Where the test is closely associated with the body, save the namespace |
paulb@133 | 378 | # before entering the test. |
paulb@133 | 379 | |
paulb@133 | 380 | if conditional.isolate_test: |
paulb@133 | 381 | saved_namespace = self.namespace |
paulb@133 | 382 | self.namespace = Namespace() |
paulb@133 | 383 | if is_module: |
paulb@133 | 384 | self.module.namespace = self.namespace |
paulb@133 | 385 | self.namespace.merge_namespace(saved_namespace) |
paulb@19 | 386 | |
paulb@213 | 387 | self.dispatch(conditional.test) |
paulb@133 | 388 | |
paulb@133 | 389 | # Where the test may affect the body and the else clause, save the |
paulb@133 | 390 | # namespace after processing the test. |
paulb@133 | 391 | |
paulb@133 | 392 | if not conditional.isolate_test: |
paulb@133 | 393 | saved_namespace = self.namespace |
paulb@133 | 394 | self.namespace = Namespace() |
paulb@133 | 395 | if is_module: |
paulb@133 | 396 | self.module.namespace = self.namespace |
paulb@133 | 397 | self.namespace.merge_namespace(saved_namespace) |
paulb@133 | 398 | |
paulb@190 | 399 | # NOTE: Exception recording. |
paulb@190 | 400 | |
paulb@190 | 401 | else: |
paulb@213 | 402 | test_raises = set() |
paulb@213 | 403 | test_raises.update(self.namespace.raises) |
paulb@190 | 404 | |
paulb@133 | 405 | # Process the body clause. |
paulb@133 | 406 | |
paulb@213 | 407 | self.dispatches(conditional.body) |
paulb@110 | 408 | body_namespace = self.namespace |
paulb@19 | 409 | |
paulb@133 | 410 | # Use the saved namespace as a template for the else clause. |
paulb@133 | 411 | |
paulb@110 | 412 | self.namespace = Namespace() |
paulb@133 | 413 | if is_module: |
paulb@133 | 414 | self.module.namespace = self.namespace |
paulb@110 | 415 | self.namespace.merge_namespace(saved_namespace) |
paulb@133 | 416 | |
paulb@133 | 417 | # Process the else clause. |
paulb@133 | 418 | |
paulb@213 | 419 | self.dispatches(conditional.else_) |
paulb@110 | 420 | else_namespace = self.namespace |
paulb@19 | 421 | |
paulb@133 | 422 | # Merge the body and else namespaces. |
paulb@133 | 423 | |
paulb@110 | 424 | self.namespace = Namespace() |
paulb@133 | 425 | if is_module: |
paulb@133 | 426 | self.module.namespace = self.namespace |
paulb@110 | 427 | self.namespace.merge_namespace(body_namespace) |
paulb@110 | 428 | self.namespace.merge_namespace(else_namespace) |
paulb@110 | 429 | |
paulb@166 | 430 | # NOTE: Test of exception type pruning based on the test/body. |
paulb@190 | 431 | # Note that the checked exceptions are tested for re-raising. |
paulb@166 | 432 | |
paulb@166 | 433 | if conditional.isolate_test: |
paulb@190 | 434 | for exc_type in test_raises: |
paulb@190 | 435 | if exc_type not in body_namespace.raises: |
paulb@190 | 436 | self.namespace.revoke_exception_type(exc_type) |
paulb@166 | 437 | |
paulb@155 | 438 | def visitGlobal(self, global_): |
paulb@155 | 439 | |
paulb@155 | 440 | """ |
paulb@213 | 441 | Leave the 'global_' node unprocessed since namespaces should have |
paulb@155 | 442 | already been altered to take global names into consideration. |
paulb@155 | 443 | """ |
paulb@155 | 444 | |
paulb@213 | 445 | pass |
paulb@155 | 446 | |
paulb@156 | 447 | def visitImport(self, import_): |
paulb@156 | 448 | |
paulb@156 | 449 | """ |
paulb@213 | 450 | Process the 'import_' node, importing the module with the stated name |
paulb@156 | 451 | and storing details on the node. |
paulb@156 | 452 | """ |
paulb@156 | 453 | |
paulb@158 | 454 | module = self.importer.load(import_.name, self.builtins, getattr(import_, "alias", None)) |
paulb@156 | 455 | if module is not None: |
paulb@213 | 456 | self.namespace.set_types(set([module])) |
paulb@156 | 457 | else: |
paulb@213 | 458 | self.namespace.set_types(set()) |
paulb@157 | 459 | self.annotate(import_) # mainly for viewing purposes |
paulb@156 | 460 | |
paulb@151 | 461 | def _visitInvoke(self, invoke, invocation_types, have_args): |
paulb@151 | 462 | |
paulb@151 | 463 | """ |
paulb@213 | 464 | Process the 'invoke' node, using the given 'invocation_types' as the |
paulb@213 | 465 | list of callables to be investigated for instantiation or for the |
paulb@151 | 466 | invocation of functions or blocks. If 'have_args' is a true value, any |
paulb@151 | 467 | invocation or instantiation will involve arguments. |
paulb@151 | 468 | """ |
paulb@151 | 469 | |
paulb@151 | 470 | # Now locate and invoke the subprogram. This can be complicated because |
paulb@151 | 471 | # the target may be a class or object, and there may be many different |
paulb@151 | 472 | # related subprograms. |
paulb@151 | 473 | |
paulb@151 | 474 | invocations = [] |
paulb@151 | 475 | |
paulb@151 | 476 | # Visit each callable in turn, finding subprograms. |
paulb@151 | 477 | |
paulb@151 | 478 | for attr in invocation_types: |
paulb@151 | 479 | |
paulb@151 | 480 | # Deal with class invocations by providing instance objects. |
paulb@151 | 481 | # Here, each class is queried for the __init__ method, which may |
paulb@151 | 482 | # exist for some combinations of classes in a hierarchy but not for |
paulb@151 | 483 | # others. |
paulb@151 | 484 | |
paulb@151 | 485 | if isinstance(attr.type, Class): |
paulb@151 | 486 | attributes = get_attributes(attr.type, "__init__") |
paulb@151 | 487 | |
paulb@151 | 488 | # Deal with object invocations by using __call__ methods. |
paulb@151 | 489 | |
paulb@151 | 490 | elif isinstance(attr.type, Instance): |
paulb@151 | 491 | attributes = get_attributes(attr.type, "__call__") |
paulb@151 | 492 | |
paulb@151 | 493 | # Normal functions or methods are more straightforward. |
paulb@151 | 494 | # Here, we model them using an attribute with no context and with |
paulb@151 | 495 | # no associated accessor. |
paulb@151 | 496 | |
paulb@151 | 497 | else: |
paulb@151 | 498 | attributes = [(attr, None)] |
paulb@151 | 499 | |
paulb@151 | 500 | # Inspect each attribute and extract the subprogram. |
paulb@151 | 501 | |
paulb@151 | 502 | for attribute, accessor in attributes: |
paulb@151 | 503 | |
paulb@151 | 504 | # If a class is involved, presume that it must create a new |
paulb@151 | 505 | # object. |
paulb@151 | 506 | |
paulb@151 | 507 | if isinstance(attr.type, Class): |
paulb@151 | 508 | |
paulb@151 | 509 | # Instantiate the class. |
paulb@151 | 510 | |
paulb@185 | 511 | instance = self.new_instance(invoke, attr.type) |
paulb@151 | 512 | |
paulb@151 | 513 | # For instantiations, switch the context. |
paulb@151 | 514 | |
paulb@151 | 515 | if attribute is not None: |
paulb@151 | 516 | attribute = Attribute(instance, attribute.type) |
paulb@151 | 517 | |
paulb@204 | 518 | # Request an instance-specific initialiser. |
paulb@204 | 519 | |
paulb@204 | 520 | attribute = attr.type.get_attribute_for_instance(attribute, instance) |
paulb@204 | 521 | |
paulb@151 | 522 | # Skip cases where no callable is found. |
paulb@151 | 523 | |
paulb@151 | 524 | if attribute is not None: |
paulb@151 | 525 | |
paulb@151 | 526 | # If a subprogram is defined, invoke it. |
paulb@151 | 527 | |
paulb@151 | 528 | self.invoke_subprogram(invoke, attribute) |
paulb@151 | 529 | if attribute.type not in invocations: |
paulb@151 | 530 | invocations.append(attribute.type) |
paulb@151 | 531 | |
paulb@151 | 532 | elif not isinstance(attr.type, Class): |
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@151 | 545 | if isinstance(attr.type, Class): |
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@150 | 605 | non_accesses = [] |
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@150 | 623 | non_accesses.append(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@150 | 651 | non_accesses.append(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@219 | 758 | for node in 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@219 | 762 | |
paulb@219 | 763 | self.namespace.set_types(tuples) |
paulb@219 | 764 | self.annotate(maketuple) |
paulb@219 | 765 | |
paulb@115 | 766 | def visitModule(self, module): |
paulb@150 | 767 | |
paulb@150 | 768 | """ |
paulb@213 | 769 | Process the 'module' and its contents. |
paulb@150 | 770 | """ |
paulb@150 | 771 | |
paulb@213 | 772 | self.dispatches(module.code) |
paulb@115 | 773 | |
paulb@150 | 774 | def visitNot(self, not_): |
paulb@150 | 775 | |
paulb@213 | 776 | "Process the 'not_' node and its expression." |
paulb@150 | 777 | |
paulb@213 | 778 | self.dispatch(not_.expr) |
paulb@150 | 779 | |
paulb@115 | 780 | def visitPass(self, pass_): |
paulb@150 | 781 | |
paulb@213 | 782 | "Leave the 'pass_' node unprocessed." |
paulb@150 | 783 | |
paulb@213 | 784 | pass |
paulb@115 | 785 | |
paulb@150 | 786 | def visitRaise(self, raise_): |
paulb@150 | 787 | |
paulb@150 | 788 | """ |
paulb@213 | 789 | Process the 'raise_' node, processing any traceback information along |
paulb@150 | 790 | with the raised exception expression, converting the node into a kind of |
paulb@150 | 791 | invocation where the expression is found not to be an invocation itself. |
paulb@150 | 792 | This node affects the namespace, adding exception types to the list of |
paulb@150 | 793 | those raised in the namespace. |
paulb@150 | 794 | """ |
paulb@150 | 795 | |
paulb@150 | 796 | if getattr(raise_, "traceback", None) is not None: |
paulb@213 | 797 | self.dispatch(raise_.traceback) |
paulb@213 | 798 | self.dispatch(raise_.expr) |
paulb@150 | 799 | |
paulb@150 | 800 | # Handle bare name exceptions by converting any classes to instances. |
paulb@150 | 801 | |
paulb@150 | 802 | if not isinstance(raise_.expr, InvokeFunction): |
paulb@150 | 803 | raise_.pos_args = [] |
paulb@150 | 804 | raise_.kw_args = {} |
paulb@150 | 805 | raise_.star = None |
paulb@150 | 806 | raise_.dstar = None |
paulb@213 | 807 | types = set() |
paulb@150 | 808 | for attr in self.namespace.types: |
paulb@150 | 809 | if isinstance(attr.type, Class): |
paulb@150 | 810 | self._visitInvoke(raise_, [attr], have_args=0) |
paulb@213 | 811 | types.update(self.namespace.types) |
paulb@150 | 812 | else: |
paulb@150 | 813 | types = self.namespace.types |
paulb@150 | 814 | |
paulb@213 | 815 | self.namespace.raises.update(types) |
paulb@150 | 816 | |
paulb@150 | 817 | def visitReleaseTemp(self, releasetemp): |
paulb@150 | 818 | |
paulb@150 | 819 | """ |
paulb@213 | 820 | Process the 'releasetemp' node, removing temporary variable information |
paulb@150 | 821 | from the current namespace. |
paulb@150 | 822 | """ |
paulb@150 | 823 | |
paulb@150 | 824 | index = getattr(releasetemp, "index", None) |
paulb@150 | 825 | try: |
paulb@150 | 826 | self.namespace.temp[index].pop() |
paulb@150 | 827 | except KeyError: |
paulb@150 | 828 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@150 | 829 | except IndexError: |
paulb@150 | 830 | pass #raise AnnotationMessage, "Temporary store index '%s' is empty." % index |
paulb@150 | 831 | |
paulb@190 | 832 | def visitResetExc(self, resetexc): |
paulb@213 | 833 | self.namespace.raises = set() |
paulb@190 | 834 | |
paulb@150 | 835 | def visitReturn(self, return_): |
paulb@150 | 836 | |
paulb@150 | 837 | """ |
paulb@213 | 838 | Process the 'return_' node, processing any expression and obtaining type |
paulb@150 | 839 | information to be accumulated in the current namespace's list of return |
paulb@150 | 840 | types. A snapshot of the namespace is taken for the purposes of |
paulb@150 | 841 | reconciling or merging namespaces where subprograms actually share |
paulb@150 | 842 | locals with their callers. |
paulb@150 | 843 | """ |
paulb@150 | 844 | |
paulb@150 | 845 | if hasattr(return_, "expr"): |
paulb@213 | 846 | self.dispatch(return_.expr) |
paulb@213 | 847 | self.namespace.returns.update(self.namespace.types) |
paulb@150 | 848 | self.annotate(return_) |
paulb@150 | 849 | self.namespace.snapshot() |
paulb@150 | 850 | |
paulb@150 | 851 | visitReturnFromBlock = visitReturn |
paulb@150 | 852 | visitReturnFromFunction = visitReturn |
paulb@150 | 853 | |
paulb@150 | 854 | def visitStoreAttr(self, storeattr): |
paulb@150 | 855 | |
paulb@150 | 856 | """ |
paulb@213 | 857 | Process the 'storeattr' node, processing the expression and target, and |
paulb@150 | 858 | using the type information obtained to build records of legitimate |
paulb@150 | 859 | writes to the stated attribute, along with "impossible" non-writes to |
paulb@150 | 860 | the attribute. |
paulb@150 | 861 | """ |
paulb@150 | 862 | |
paulb@213 | 863 | self.dispatch(storeattr.expr) |
paulb@150 | 864 | expr = self.namespace.types |
paulb@213 | 865 | self.dispatch(storeattr.lvalue) |
paulb@150 | 866 | writes = {} |
paulb@150 | 867 | non_writes = [] |
paulb@150 | 868 | for attr in self.namespace.types: |
paulb@154 | 869 | # NOTE: Impose "atomic" constraints on certain types. |
paulb@150 | 870 | if attr is None: |
paulb@150 | 871 | if not attr in non_writes: |
paulb@150 | 872 | non_writes.append(attr) |
paulb@150 | 873 | continue |
paulb@150 | 874 | attr.type.namespace.add(storeattr.name, expr) |
paulb@150 | 875 | writes[attr.type] = attr.type.namespace.load(storeattr.name) |
paulb@150 | 876 | if not writes: |
paulb@150 | 877 | print "Unable to store attribute", storeattr.name, "given", self.namespace.types |
paulb@150 | 878 | storeattr.writes = writes |
paulb@150 | 879 | storeattr.non_writes = non_writes |
paulb@150 | 880 | |
paulb@150 | 881 | def visitStoreName(self, storename): |
paulb@150 | 882 | |
paulb@150 | 883 | """ |
paulb@213 | 884 | Process the 'storename' node, processing the expression on the node and |
paulb@150 | 885 | associating the type information obtained with the stated name in the |
paulb@150 | 886 | current namespace. |
paulb@150 | 887 | """ |
paulb@150 | 888 | |
paulb@213 | 889 | self.dispatch(storename.expr) |
paulb@150 | 890 | self.namespace.store(storename.name, self.namespace.types) |
paulb@190 | 891 | self.annotate(storename) |
paulb@150 | 892 | |
paulb@150 | 893 | def visitStoreTemp(self, storetemp): |
paulb@150 | 894 | |
paulb@150 | 895 | """ |
paulb@213 | 896 | Process the 'storetemp' node, processing the expression on the node and |
paulb@150 | 897 | associating the type information obtained with a temporary variable in |
paulb@150 | 898 | the current namespace. |
paulb@150 | 899 | """ |
paulb@150 | 900 | |
paulb@213 | 901 | self.dispatch(storetemp.expr) |
paulb@150 | 902 | index = getattr(storetemp, "index", None) |
paulb@150 | 903 | if not self.namespace.temp.has_key(index): |
paulb@150 | 904 | self.namespace.temp[index] = [] |
paulb@150 | 905 | self.namespace.temp[index].append(self.namespace.types) |
paulb@150 | 906 | |
paulb@115 | 907 | def visitSubprogram(self, subprogram): |
paulb@150 | 908 | |
paulb@150 | 909 | """ |
paulb@213 | 910 | Process the 'subprogram' node, processing its contents (a group of nodes |
paulb@150 | 911 | comprising the subprogram). |
paulb@150 | 912 | """ |
paulb@150 | 913 | |
paulb@213 | 914 | self.dispatches(subprogram.code) |
paulb@115 | 915 | |
paulb@115 | 916 | def visitTry(self, try_): |
paulb@150 | 917 | |
paulb@150 | 918 | """ |
paulb@213 | 919 | Process the 'try_' node, processing the body clause in its own namespace |
paulb@150 | 920 | derived from the current namespace, processing any handler clause using |
paulb@150 | 921 | the namespace information accumulated in the body, and processing any |
paulb@150 | 922 | else and finally clauses, attempting to supply each with appropriate |
paulb@150 | 923 | namespace information. |
paulb@150 | 924 | """ |
paulb@150 | 925 | |
paulb@133 | 926 | is_module = self.namespace is self.module.namespace |
paulb@133 | 927 | |
paulb@213 | 928 | self.dispatches(try_.body) |
paulb@133 | 929 | |
paulb@133 | 930 | # Save the namespace from the body. |
paulb@133 | 931 | |
paulb@133 | 932 | body_namespace = Namespace() |
paulb@133 | 933 | body_namespace.merge_namespace(self.namespace) |
paulb@133 | 934 | |
paulb@133 | 935 | # Process the handler. |
paulb@133 | 936 | |
paulb@133 | 937 | if hasattr(try_, "handler"): |
paulb@213 | 938 | self.dispatches(try_.handler) |
paulb@133 | 939 | |
paulb@133 | 940 | # Save the namespace from the handler. |
paulb@133 | 941 | |
paulb@133 | 942 | handler_namespace = Namespace() |
paulb@133 | 943 | handler_namespace.merge_namespace(self.namespace) |
paulb@133 | 944 | |
paulb@133 | 945 | # Remember the raised exceptions encountered so far. |
paulb@133 | 946 | |
paulb@121 | 947 | raises = self.namespace.raises |
paulb@121 | 948 | |
paulb@133 | 949 | # Process the else clause. |
paulb@133 | 950 | |
paulb@133 | 951 | if hasattr(try_, "else_"): |
paulb@133 | 952 | |
paulb@133 | 953 | # Restore the body namespace for the else clause. |
paulb@133 | 954 | |
paulb@133 | 955 | self.namespace = body_namespace |
paulb@133 | 956 | if is_module: |
paulb@133 | 957 | self.module.namespace = self.namespace |
paulb@133 | 958 | |
paulb@133 | 959 | # Empty the raised exceptions for the else clause. |
paulb@121 | 960 | |
paulb@213 | 961 | self.namespace.raises = set() |
paulb@213 | 962 | self.dispatches(try_.else_) |
paulb@133 | 963 | self.namespace.raises = raises |
paulb@133 | 964 | |
paulb@133 | 965 | # Merge the namespaces. |
paulb@133 | 966 | |
paulb@133 | 967 | self.namespace = Namespace() |
paulb@133 | 968 | if is_module: |
paulb@133 | 969 | self.module.namespace = self.namespace |
paulb@133 | 970 | self.namespace.merge_namespace(body_namespace) |
paulb@133 | 971 | self.namespace.merge_namespace(handler_namespace) |
paulb@133 | 972 | |
paulb@133 | 973 | # Process the finally clause, if any. |
paulb@121 | 974 | |
paulb@213 | 975 | self.dispatches(try_.finally_) |
paulb@115 | 976 | |
paulb@203 | 977 | def visitYield(self, yield_): |
paulb@203 | 978 | raise NotImplementedError, "The yield statement is not currently supported." |
paulb@203 | 979 | |
paulb@25 | 980 | # Utility methods. |
paulb@25 | 981 | |
paulb@176 | 982 | def get_builtin_instances(self, node, name): |
paulb@219 | 983 | return set([Attribute(None, self.new_instance(node, attr.type)) for attr in self.builtins.namespace[name]]) |
paulb@79 | 984 | |
paulb@185 | 985 | def new_instance(self, node, type): |
paulb@168 | 986 | |
paulb@176 | 987 | "For the given 'node', obtain an instance from the given 'type'." |
paulb@168 | 988 | |
paulb@176 | 989 | if not type.has_instance(node): |
paulb@168 | 990 | instance = Instance() |
paulb@168 | 991 | instance.namespace = Namespace() |
paulb@213 | 992 | instance.namespace.store("__class__", set([Attribute(None, type)])) |
paulb@176 | 993 | type.add_instance(node, instance) |
paulb@168 | 994 | else: |
paulb@176 | 995 | instance = type.get_instance(node) |
paulb@168 | 996 | |
paulb@168 | 997 | return instance |
paulb@168 | 998 | |
paulb@151 | 999 | def invoke_subprogram(self, invoke, attribute): |
paulb@34 | 1000 | |
paulb@151 | 1001 | """ |
paulb@151 | 1002 | Invoke using the given 'invoke' node the subprogram represented by the |
paulb@151 | 1003 | given 'attribute'. |
paulb@151 | 1004 | """ |
paulb@34 | 1005 | |
paulb@54 | 1006 | # Test for context information, making it into a real attribute. |
paulb@34 | 1007 | |
paulb@151 | 1008 | if attribute.context is not None: |
paulb@151 | 1009 | context = Attribute(None, attribute.context) |
paulb@151 | 1010 | target = attribute.type |
paulb@34 | 1011 | else: |
paulb@34 | 1012 | context = None |
paulb@151 | 1013 | target = attribute.type |
paulb@50 | 1014 | |
paulb@154 | 1015 | # Test to see if anything has changed. |
paulb@154 | 1016 | |
paulb@179 | 1017 | if hasattr(invoke, "syscount") and invoke.syscount.has_key(target) and invoke.syscount[target] == self.system.count: |
paulb@154 | 1018 | return |
paulb@154 | 1019 | |
paulb@154 | 1020 | # Remember the state of the system. |
paulb@154 | 1021 | |
paulb@154 | 1022 | else: |
paulb@179 | 1023 | if not hasattr(invoke, "syscount"): |
paulb@179 | 1024 | invoke.syscount = {} |
paulb@179 | 1025 | invoke.syscount[target] = self.system.count |
paulb@154 | 1026 | |
paulb@34 | 1027 | # Provide the correct namespace for the invocation. |
paulb@156 | 1028 | # This may be a "shared" namespace... |
paulb@34 | 1029 | |
paulb@112 | 1030 | if getattr(invoke, "share_locals", 0): |
paulb@34 | 1031 | namespace = Namespace() |
paulb@134 | 1032 | namespace.merge_namespace(self.namespace, everything=0) |
paulb@132 | 1033 | using_module_namespace = self.namespace is self.module.namespace |
paulb@156 | 1034 | |
paulb@156 | 1035 | # Or it may be a structure... |
paulb@156 | 1036 | |
paulb@112 | 1037 | elif getattr(target, "structure", None): |
paulb@112 | 1038 | namespace = Namespace() |
paulb@132 | 1039 | using_module_namespace = 0 |
paulb@156 | 1040 | |
paulb@156 | 1041 | # Or it may be a new namespace populated with the supplied parameters. |
paulb@156 | 1042 | |
paulb@34 | 1043 | else: |
paulb@34 | 1044 | items = self.make_items(invoke, target, context) |
paulb@150 | 1045 | namespace = Namespace() |
paulb@150 | 1046 | namespace.merge_items(items) |
paulb@132 | 1047 | using_module_namespace = 0 |
paulb@34 | 1048 | |
paulb@169 | 1049 | # NOTE: Avoid PEP 227 (nested scopes) whilst permitting references to a |
paulb@172 | 1050 | # NOTE: subprogram within itself. Do not define the name of the function |
paulb@172 | 1051 | # NOTE: within a method definition. |
paulb@169 | 1052 | |
paulb@172 | 1053 | if getattr(target, "name", None) is not None and not getattr(target, "is_method", 0): |
paulb@213 | 1054 | namespace.store(target.name, set([Attribute(None, target)])) |
paulb@169 | 1055 | |
paulb@34 | 1056 | # Process the subprogram. |
paulb@132 | 1057 | |
paulb@207 | 1058 | self.process_node(target, namespace, using_module_namespace) |
paulb@34 | 1059 | |
paulb@45 | 1060 | # NOTE: Improve and verify this. |
paulb@91 | 1061 | # If the invocation returns a value, acquire the return types. |
paulb@34 | 1062 | |
paulb@53 | 1063 | if getattr(target, "returns_value", 0): |
paulb@207 | 1064 | self.namespace.set_types(target.returns) |
paulb@34 | 1065 | self.annotate(invoke) |
paulb@34 | 1066 | |
paulb@132 | 1067 | # If it is a normal block, merge the locals. |
paulb@132 | 1068 | # This can happen in addition to the above because for things like |
paulb@132 | 1069 | # logical expressions, the namespace can be modified whilst values are |
paulb@132 | 1070 | # returned as results. |
paulb@91 | 1071 | |
paulb@132 | 1072 | if getattr(invoke, "share_locals", 0): |
paulb@132 | 1073 | self.namespace.reset() |
paulb@135 | 1074 | |
paulb@135 | 1075 | # Merge the locals snapshots. |
paulb@135 | 1076 | |
paulb@207 | 1077 | for locals in target.return_locals: |
paulb@135 | 1078 | |
paulb@135 | 1079 | # For blocks returning values (such as operations), do not merge |
paulb@135 | 1080 | # snapshots or results. |
paulb@135 | 1081 | |
paulb@135 | 1082 | if getattr(target, "returns_value", 0): |
paulb@135 | 1083 | self.namespace.merge_namespace(locals, everything=0) |
paulb@135 | 1084 | |
paulb@135 | 1085 | # For blocks not returning values (such as loops), merge |
paulb@135 | 1086 | # snapshots and results since they contain details of genuine |
paulb@135 | 1087 | # returns. |
paulb@135 | 1088 | |
paulb@135 | 1089 | else: |
paulb@135 | 1090 | self.namespace.merge_namespace(locals) |
paulb@34 | 1091 | |
paulb@116 | 1092 | # Incorporate any raised exceptions. |
paulb@116 | 1093 | |
paulb@191 | 1094 | if not hasattr(invoke, "raises"): |
paulb@213 | 1095 | invoke.raises = set() |
paulb@213 | 1096 | invoke.raises.update(target.raises) |
paulb@213 | 1097 | self.namespace.raises.update(target.raises) |
paulb@132 | 1098 | |
paulb@83 | 1099 | def process_args(self, invocation): |
paulb@83 | 1100 | |
paulb@112 | 1101 | """ |
paulb@112 | 1102 | Process the arguments associated with an 'invocation'. Return whether |
paulb@112 | 1103 | any arguments were processed. |
paulb@112 | 1104 | """ |
paulb@54 | 1105 | |
paulb@213 | 1106 | self.dispatches(invocation.pos_args) |
paulb@213 | 1107 | self.dispatch_dict(invocation.kw_args) |
paulb@54 | 1108 | |
paulb@54 | 1109 | # Get type information for star and dstar arguments. |
paulb@54 | 1110 | |
paulb@83 | 1111 | if invocation.star is not None: |
paulb@83 | 1112 | param, default = invocation.star |
paulb@213 | 1113 | self.dispatch(default) |
paulb@83 | 1114 | invocation.star = param, default |
paulb@54 | 1115 | |
paulb@83 | 1116 | if invocation.dstar is not None: |
paulb@83 | 1117 | param, default = invocation.dstar |
paulb@213 | 1118 | self.dispatch(default) |
paulb@83 | 1119 | invocation.dstar = param, default |
paulb@54 | 1120 | |
paulb@112 | 1121 | if invocation.pos_args or invocation.kw_args or invocation.star or invocation.dstar: |
paulb@112 | 1122 | return 1 |
paulb@112 | 1123 | else: |
paulb@112 | 1124 | return 0 |
paulb@112 | 1125 | |
paulb@34 | 1126 | def make_items(self, invocation, subprogram, context): |
paulb@34 | 1127 | |
paulb@34 | 1128 | """ |
paulb@34 | 1129 | Make an items mapping for the 'invocation' of the 'subprogram' using the |
paulb@34 | 1130 | given 'context' (which may be None). |
paulb@34 | 1131 | """ |
paulb@34 | 1132 | |
paulb@213 | 1133 | # NOTE: Support class methods! |
paulb@213 | 1134 | |
paulb@213 | 1135 | if context is not None and isinstance(context.type, Instance): |
paulb@93 | 1136 | pos_args = [Self(context)] + invocation.pos_args |
paulb@34 | 1137 | else: |
paulb@93 | 1138 | pos_args = invocation.pos_args |
paulb@154 | 1139 | |
paulb@154 | 1140 | # Duplicate the keyword arguments - we remove them in processing below. |
paulb@154 | 1141 | |
paulb@154 | 1142 | kw_args = {} |
paulb@154 | 1143 | kw_args.update(invocation.kw_args) |
paulb@34 | 1144 | |
paulb@67 | 1145 | # Sort the arguments into positional and keyword arguments. |
paulb@67 | 1146 | |
paulb@24 | 1147 | params = subprogram.params |
paulb@24 | 1148 | items = [] |
paulb@67 | 1149 | star_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@24 | 1156 | if arg is None: |
paulb@54 | 1157 | arg = default |
paulb@140 | 1158 | if hasattr(arg, "types"): |
paulb@140 | 1159 | items.append((param, arg.types)) |
paulb@140 | 1160 | else: |
paulb@213 | 1161 | items.append((param, set())) # Annotation has not succeeded. |
paulb@67 | 1162 | params = params[1:] |
paulb@24 | 1163 | else: |
paulb@67 | 1164 | star_args.append(arg) |
paulb@24 | 1165 | |
paulb@24 | 1166 | # Collect the remaining defaults. |
paulb@24 | 1167 | |
paulb@24 | 1168 | while params: |
paulb@24 | 1169 | param, default = params[0] |
paulb@67 | 1170 | if kw_args.has_key(param): |
paulb@67 | 1171 | arg = kw_args[param] |
paulb@73 | 1172 | del kw_args[param] |
paulb@93 | 1173 | elif default is not None: |
paulb@213 | 1174 | self.dispatch(default) |
paulb@213 | 1175 | arg = default |
paulb@93 | 1176 | else: |
paulb@67 | 1177 | raise AnnotationMessage, "No argument supplied in '%s' for parameter '%s'." % (subprogram, param) |
paulb@140 | 1178 | if hasattr(arg, "types"): |
paulb@140 | 1179 | items.append((param, arg.types)) |
paulb@140 | 1180 | else: |
paulb@213 | 1181 | items.append((param, set())) # Annotation has not succeeded. |
paulb@24 | 1182 | params = params[1:] |
paulb@24 | 1183 | |
paulb@162 | 1184 | dstar_args = kw_args.items() |
paulb@73 | 1185 | |
paulb@73 | 1186 | # Construct temporary objects. |
paulb@73 | 1187 | |
paulb@73 | 1188 | if star_args: |
paulb@80 | 1189 | star_invocation = self.make_star_args(invocation, subprogram, star_args) |
paulb@80 | 1190 | self.dispatch(star_invocation) |
paulb@80 | 1191 | star_types = star_invocation.types |
paulb@73 | 1192 | else: |
paulb@73 | 1193 | star_types = None |
paulb@73 | 1194 | |
paulb@73 | 1195 | if dstar_args: |
paulb@162 | 1196 | dstar_invocation = self.make_dstar_args(invocation, subprogram, dstar_args) |
paulb@80 | 1197 | self.dispatch(dstar_invocation) |
paulb@80 | 1198 | dstar_types = dstar_invocation.types |
paulb@73 | 1199 | else: |
paulb@73 | 1200 | dstar_types = None |
paulb@73 | 1201 | |
paulb@73 | 1202 | # NOTE: Merge the objects properly. |
paulb@73 | 1203 | |
paulb@73 | 1204 | star_types = star_types or invocation.star and invocation.star.types |
paulb@73 | 1205 | dstar_types = dstar_types or invocation.dstar and invocation.dstar.types |
paulb@73 | 1206 | |
paulb@24 | 1207 | # Add star and dstar. |
paulb@24 | 1208 | |
paulb@73 | 1209 | if star_types is not None: |
paulb@24 | 1210 | if subprogram.star is not None: |
paulb@24 | 1211 | param, default = subprogram.star |
paulb@73 | 1212 | items.append((param, star_types)) |
paulb@24 | 1213 | else: |
paulb@67 | 1214 | raise AnnotationMessage, "Invocation provides unwanted *args." |
paulb@24 | 1215 | elif subprogram.star is not None: |
paulb@24 | 1216 | param, default = subprogram.star |
paulb@154 | 1217 | if not hasattr(default, "types"): |
paulb@154 | 1218 | subprogram.star = param, self.dispatch(default) # NOTE: Review reprocessing. |
paulb@154 | 1219 | items.append((param, default.types)) |
paulb@24 | 1220 | |
paulb@73 | 1221 | if dstar_types is not None: |
paulb@24 | 1222 | if subprogram.dstar is not None: |
paulb@24 | 1223 | param, default = subprogram.dstar |
paulb@73 | 1224 | items.append((param, dstar_types)) |
paulb@24 | 1225 | else: |
paulb@67 | 1226 | raise AnnotationMessage, "Invocation provides unwanted **args." |
paulb@24 | 1227 | elif subprogram.dstar is not None: |
paulb@24 | 1228 | param, default = subprogram.dstar |
paulb@154 | 1229 | if not hasattr(default, "types"): |
paulb@154 | 1230 | subprogram.dstar = param, self.dispatch(default) # NOTE: Review reprocessing. |
paulb@154 | 1231 | items.append((param, default.types)) |
paulb@24 | 1232 | |
paulb@83 | 1233 | # Record the parameter types. |
paulb@83 | 1234 | |
paulb@146 | 1235 | self.annotate_parameters(subprogram, items) |
paulb@146 | 1236 | return subprogram.paramtypes.items() |
paulb@24 | 1237 | |
paulb@80 | 1238 | def make_star_args(self, invocation, subprogram, star_args): |
paulb@80 | 1239 | |
paulb@80 | 1240 | "Make a subprogram which initialises a list containing 'star_args'." |
paulb@80 | 1241 | |
paulb@80 | 1242 | if not hasattr(invocation, "stars"): |
paulb@80 | 1243 | invocation.stars = {} |
paulb@80 | 1244 | |
paulb@80 | 1245 | if not invocation.stars.has_key(subprogram.full_name()): |
paulb@204 | 1246 | instance = getattr(invocation, "instance", None) |
paulb@204 | 1247 | |
paulb@219 | 1248 | code = [ |
paulb@204 | 1249 | Return( |
paulb@204 | 1250 | instance=instance, |
paulb@219 | 1251 | expr=MakeTuple( |
paulb@204 | 1252 | instance=instance, |
paulb@219 | 1253 | nodes=star_args |
paulb@204 | 1254 | ) |
paulb@204 | 1255 | ) |
paulb@80 | 1256 | ] |
paulb@80 | 1257 | |
paulb@207 | 1258 | new_subprogram = Subprogram( |
paulb@207 | 1259 | instance=instance, |
paulb@207 | 1260 | name=None, |
paulb@207 | 1261 | returns_value=1, |
paulb@207 | 1262 | params=[], |
paulb@207 | 1263 | star=None, |
paulb@207 | 1264 | dstar=None, |
paulb@207 | 1265 | code=code |
paulb@207 | 1266 | ) |
paulb@219 | 1267 | |
paulb@207 | 1268 | subprogram.module.simplifier.subnames[new_subprogram.full_name()] = new_subprogram |
paulb@207 | 1269 | |
paulb@204 | 1270 | invocation.stars[subprogram.full_name()] = InvokeRef( |
paulb@185 | 1271 | invocation.original, |
paulb@204 | 1272 | instance=instance, |
paulb@80 | 1273 | produces_result=1, |
paulb@207 | 1274 | ref=new_subprogram |
paulb@80 | 1275 | ) |
paulb@80 | 1276 | |
paulb@80 | 1277 | return invocation.stars[subprogram.full_name()] |
paulb@80 | 1278 | |
paulb@162 | 1279 | def make_dstar_args(self, invocation, subprogram, dstar_args): |
paulb@162 | 1280 | |
paulb@162 | 1281 | """ |
paulb@162 | 1282 | Make a subprogram which initialises a dictionary built from the given |
paulb@162 | 1283 | 'dstar_args'. |
paulb@162 | 1284 | """ |
paulb@162 | 1285 | |
paulb@162 | 1286 | if not hasattr(invocation, "dstars"): |
paulb@162 | 1287 | invocation.dstars = {} |
paulb@162 | 1288 | |
paulb@162 | 1289 | if not invocation.dstars.has_key(subprogram.full_name()): |
paulb@204 | 1290 | instance = getattr(invocation, "instance", None) |
paulb@204 | 1291 | |
paulb@162 | 1292 | code=[ |
paulb@162 | 1293 | StoreTemp( |
paulb@204 | 1294 | instance=instance, |
paulb@162 | 1295 | expr=InvokeFunction( |
paulb@185 | 1296 | invocation.original, |
paulb@204 | 1297 | instance=instance, |
paulb@162 | 1298 | expr=LoadAttr( |
paulb@204 | 1299 | instance=instance, |
paulb@162 | 1300 | expr=LoadRef( |
paulb@204 | 1301 | instance=instance, |
paulb@162 | 1302 | ref=self.builtins |
paulb@162 | 1303 | ), |
paulb@162 | 1304 | name="dict", |
paulb@162 | 1305 | nstype="module", |
paulb@162 | 1306 | ) |
paulb@162 | 1307 | ) |
paulb@162 | 1308 | ) |
paulb@162 | 1309 | ] |
paulb@162 | 1310 | |
paulb@162 | 1311 | for arg, value in dstar_args: |
paulb@162 | 1312 | |
paulb@162 | 1313 | # NOTE: Constant not added to table. |
paulb@162 | 1314 | |
paulb@212 | 1315 | constant = Constant(name=repr(arg), value=arg) |
paulb@162 | 1316 | code += [ |
paulb@162 | 1317 | StoreTemp( |
paulb@204 | 1318 | instance=instance, |
paulb@212 | 1319 | expr=InvokeFunction( |
paulb@204 | 1320 | instance=instance, |
paulb@212 | 1321 | expr=LoadName( |
paulb@212 | 1322 | instance=instance, |
paulb@212 | 1323 | name=constant.typename |
paulb@212 | 1324 | ) |
paulb@162 | 1325 | ), |
paulb@162 | 1326 | index="const" |
paulb@162 | 1327 | ), |
paulb@162 | 1328 | InvokeFunction( |
paulb@185 | 1329 | invocation.original, |
paulb@204 | 1330 | instance=instance, |
paulb@162 | 1331 | expr=LoadAttr( |
paulb@204 | 1332 | instance=instance, |
paulb@204 | 1333 | expr=LoadTemp( |
paulb@204 | 1334 | instance=instance |
paulb@204 | 1335 | ), |
paulb@162 | 1336 | name="__setitem__" |
paulb@162 | 1337 | ), |
paulb@162 | 1338 | args=[ |
paulb@162 | 1339 | LoadTemp( |
paulb@204 | 1340 | instance=instance, |
paulb@162 | 1341 | index="const", |
paulb@162 | 1342 | release=1 |
paulb@162 | 1343 | ), |
paulb@162 | 1344 | value |
paulb@162 | 1345 | ] |
paulb@162 | 1346 | ) |
paulb@162 | 1347 | ] |
paulb@162 | 1348 | |
paulb@162 | 1349 | code += [ |
paulb@204 | 1350 | Return( |
paulb@204 | 1351 | instance=instance, |
paulb@204 | 1352 | expr=LoadTemp( |
paulb@204 | 1353 | instance=instance, |
paulb@204 | 1354 | release=1 |
paulb@204 | 1355 | ) |
paulb@204 | 1356 | ) |
paulb@162 | 1357 | ] |
paulb@162 | 1358 | |
paulb@207 | 1359 | new_subprogram = Subprogram( |
paulb@207 | 1360 | instance=instance, |
paulb@207 | 1361 | name=None, |
paulb@207 | 1362 | returns_value=1, |
paulb@207 | 1363 | params=[], |
paulb@207 | 1364 | star=None, |
paulb@207 | 1365 | dstar=None, |
paulb@207 | 1366 | code=code |
paulb@207 | 1367 | ) |
paulb@207 | 1368 | subprogram.module.simplifier.subnames[new_subprogram.full_name()] = new_subprogram |
paulb@207 | 1369 | |
paulb@204 | 1370 | invocation.dstars[subprogram.full_name()] = InvokeRef( |
paulb@185 | 1371 | invocation.original, |
paulb@204 | 1372 | instance=instance, |
paulb@162 | 1373 | produces_result=1, |
paulb@207 | 1374 | ref=new_subprogram |
paulb@162 | 1375 | ) |
paulb@162 | 1376 | |
paulb@162 | 1377 | return invocation.dstars[subprogram.full_name()] |
paulb@162 | 1378 | |
paulb@48 | 1379 | # Namespace-related abstractions. |
paulb@48 | 1380 | |
paulb@48 | 1381 | class Namespace: |
paulb@48 | 1382 | |
paulb@48 | 1383 | """ |
paulb@48 | 1384 | A local namespace which may either relate to a genuine set of function |
paulb@50 | 1385 | locals or the initialisation of a structure or module. |
paulb@48 | 1386 | """ |
paulb@48 | 1387 | |
paulb@48 | 1388 | def __init__(self): |
paulb@50 | 1389 | |
paulb@50 | 1390 | """ |
paulb@50 | 1391 | Initialise the namespace with a mapping of local names to possible |
paulb@50 | 1392 | types, a list of return values and of possible returned local |
paulb@50 | 1393 | namespaces. The namespace also tracks the "current" types and a mapping |
paulb@50 | 1394 | of temporary value names to types. |
paulb@50 | 1395 | """ |
paulb@50 | 1396 | |
paulb@48 | 1397 | self.names = {} |
paulb@213 | 1398 | self.returns = set() |
paulb@213 | 1399 | self.return_locals = set() |
paulb@213 | 1400 | self.raises = set() |
paulb@49 | 1401 | self.temp = {} |
paulb@213 | 1402 | self.types = set() |
paulb@49 | 1403 | |
paulb@49 | 1404 | def set_types(self, types): |
paulb@156 | 1405 | |
paulb@156 | 1406 | "Set the current collection of 'types'." |
paulb@156 | 1407 | |
paulb@213 | 1408 | self.types = types.copy() |
paulb@48 | 1409 | |
paulb@140 | 1410 | def add(self, name, types): |
paulb@156 | 1411 | |
paulb@156 | 1412 | "Add to the entry with the given 'name' the specified 'types'." |
paulb@156 | 1413 | |
paulb@140 | 1414 | if self.names.has_key(name): |
paulb@213 | 1415 | self.names[name].update(types) |
paulb@140 | 1416 | else: |
paulb@140 | 1417 | self.store(name, types) |
paulb@140 | 1418 | |
paulb@48 | 1419 | def store(self, name, types): |
paulb@156 | 1420 | |
paulb@156 | 1421 | "Store in (or associate with) the given 'name' the specified 'types'." |
paulb@156 | 1422 | |
paulb@213 | 1423 | self.names[name] = types.copy() |
paulb@48 | 1424 | |
paulb@86 | 1425 | __setitem__ = store |
paulb@74 | 1426 | |
paulb@48 | 1427 | def load(self, name): |
paulb@156 | 1428 | |
paulb@156 | 1429 | "Load the types associated with the given 'name'." |
paulb@156 | 1430 | |
paulb@48 | 1431 | return self.names[name] |
paulb@48 | 1432 | |
paulb@86 | 1433 | __getitem__ = load |
paulb@74 | 1434 | |
paulb@198 | 1435 | def has_key(self, name): |
paulb@198 | 1436 | return self.names.has_key(name) |
paulb@198 | 1437 | |
paulb@131 | 1438 | def revoke(self, name, type): |
paulb@156 | 1439 | |
paulb@156 | 1440 | "Revoke from the entry for the given 'name' the specified 'type'." |
paulb@156 | 1441 | |
paulb@213 | 1442 | new_types = self.names[name].copy() |
paulb@161 | 1443 | new_types.remove(type) |
paulb@161 | 1444 | self.names[name] = new_types |
paulb@131 | 1445 | |
paulb@133 | 1446 | def revoke_exception_type(self, type): |
paulb@156 | 1447 | |
paulb@156 | 1448 | "Revoke the given 'type' from the collection of exception types." |
paulb@156 | 1449 | |
paulb@133 | 1450 | self.raises.remove(type) |
paulb@133 | 1451 | |
paulb@159 | 1452 | def revoke_temp_type(self, index, type): |
paulb@159 | 1453 | |
paulb@159 | 1454 | "Revoke from the temporary variable 'index' the given 'type'." |
paulb@159 | 1455 | |
paulb@213 | 1456 | new_types = self.temp[index][-1].copy() |
paulb@159 | 1457 | new_types.remove(type) |
paulb@159 | 1458 | self.temp[index][-1] = new_types |
paulb@159 | 1459 | |
paulb@134 | 1460 | def merge_namespace(self, namespace, everything=1): |
paulb@156 | 1461 | |
paulb@156 | 1462 | """ |
paulb@156 | 1463 | Merge items from the given 'namespace' with this namespace. When the |
paulb@156 | 1464 | optional 'everything' parameter is set to a false value (unlike the |
paulb@156 | 1465 | default), return values and locals snapshots will not be copied to this |
paulb@156 | 1466 | namespace. |
paulb@156 | 1467 | """ |
paulb@156 | 1468 | |
paulb@68 | 1469 | self.merge_items(namespace.names.items()) |
paulb@213 | 1470 | self.raises.update(namespace.raises) |
paulb@134 | 1471 | if everything: |
paulb@213 | 1472 | self.returns.update(namespace.returns) |
paulb@213 | 1473 | self.return_locals.update(namespace.return_locals) |
paulb@134 | 1474 | for name, values in namespace.temp.items(): |
paulb@134 | 1475 | if values: |
paulb@134 | 1476 | if not self.temp.has_key(name) or not self.temp[name]: |
paulb@213 | 1477 | self.temp[name] = [set()] |
paulb@213 | 1478 | self.temp[name][-1].update(values[-1]) |
paulb@68 | 1479 | |
paulb@68 | 1480 | def merge_items(self, items): |
paulb@156 | 1481 | |
paulb@156 | 1482 | "Merge the given 'items' with this namespace." |
paulb@156 | 1483 | |
paulb@68 | 1484 | for name, types in items: |
paulb@68 | 1485 | self.merge(name, types) |
paulb@68 | 1486 | |
paulb@48 | 1487 | def merge(self, name, types): |
paulb@156 | 1488 | |
paulb@156 | 1489 | "Merge the entry for the given 'name' and 'types' with this namespace." |
paulb@156 | 1490 | |
paulb@48 | 1491 | if not self.names.has_key(name): |
paulb@213 | 1492 | self.names[name] = types.copy() |
paulb@48 | 1493 | else: |
paulb@48 | 1494 | existing = self.names[name] |
paulb@213 | 1495 | existing.update(types) |
paulb@48 | 1496 | |
paulb@49 | 1497 | def snapshot(self): |
paulb@49 | 1498 | |
paulb@49 | 1499 | "Make a snapshot of the locals and remember them." |
paulb@49 | 1500 | |
paulb@49 | 1501 | namespace = Namespace() |
paulb@49 | 1502 | namespace.merge_namespace(self) |
paulb@213 | 1503 | self.return_locals.add(namespace) |
paulb@49 | 1504 | |
paulb@132 | 1505 | def reset(self): |
paulb@132 | 1506 | |
paulb@132 | 1507 | "Reset a namespace in preparation for merging with returned locals." |
paulb@132 | 1508 | |
paulb@132 | 1509 | self.names = {} |
paulb@132 | 1510 | |
paulb@48 | 1511 | def __repr__(self): |
paulb@207 | 1512 | return repr(self.names) + " (temp) " + repr(self.temp) |
paulb@48 | 1513 | |
paulb@156 | 1514 | class Importer: |
paulb@156 | 1515 | |
paulb@156 | 1516 | "An import machine, searching for and loading modules." |
paulb@156 | 1517 | |
paulb@156 | 1518 | def __init__(self, path=None): |
paulb@156 | 1519 | |
paulb@156 | 1520 | """ |
paulb@156 | 1521 | Initialise the importer with the given search 'path' - a list of |
paulb@156 | 1522 | directories to search for Python modules. |
paulb@156 | 1523 | """ |
paulb@156 | 1524 | |
paulb@156 | 1525 | self.path = path or [os.getcwd()] |
paulb@208 | 1526 | self.path.append(libdir) |
paulb@158 | 1527 | self.modules = {} |
paulb@156 | 1528 | |
paulb@156 | 1529 | def find_in_path(self, name): |
paulb@156 | 1530 | |
paulb@156 | 1531 | """ |
paulb@156 | 1532 | Find the given module 'name' in the search path, returning None where no |
paulb@156 | 1533 | such module could be found, or a 2-tuple from the 'find' method |
paulb@156 | 1534 | otherwise. |
paulb@156 | 1535 | """ |
paulb@156 | 1536 | |
paulb@156 | 1537 | for d in self.path: |
paulb@156 | 1538 | m = self.find(d, name) |
paulb@156 | 1539 | if m: return m |
paulb@156 | 1540 | return None |
paulb@156 | 1541 | |
paulb@156 | 1542 | def find(self, d, name): |
paulb@156 | 1543 | |
paulb@156 | 1544 | """ |
paulb@156 | 1545 | In the directory 'd', find the given module 'name', where 'name' can |
paulb@156 | 1546 | either refer to a single file module or to a package. Return None if the |
paulb@156 | 1547 | 'name' cannot be associated with either a file or a package directory, |
paulb@156 | 1548 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paulb@156 | 1549 | """ |
paulb@156 | 1550 | |
paulb@156 | 1551 | m = self._find_package(d, name) |
paulb@156 | 1552 | if m: return m |
paulb@156 | 1553 | m = self._find_module(d, name) |
paulb@156 | 1554 | if m: return m |
paulb@156 | 1555 | return None |
paulb@156 | 1556 | |
paulb@156 | 1557 | def _find_module(self, d, name): |
paulb@156 | 1558 | |
paulb@156 | 1559 | """ |
paulb@156 | 1560 | In the directory 'd', find the given module 'name', returning None where |
paulb@156 | 1561 | no suitable file exists in the directory, or a 2-tuple consisting of |
paulb@156 | 1562 | None (indicating that no package directory is involved) and a filename |
paulb@156 | 1563 | indicating the location of the module. |
paulb@156 | 1564 | """ |
paulb@156 | 1565 | |
paulb@156 | 1566 | name_py = name + os.extsep + "py" |
paulb@156 | 1567 | filename = self._find_file(d, name_py) |
paulb@156 | 1568 | if filename: |
paulb@156 | 1569 | return None, filename |
paulb@156 | 1570 | return None |
paulb@156 | 1571 | |
paulb@156 | 1572 | def _find_package(self, d, name): |
paulb@156 | 1573 | |
paulb@156 | 1574 | """ |
paulb@156 | 1575 | In the directory 'd', find the given package 'name', returning None |
paulb@156 | 1576 | where no suitable package directory exists, or a 2-tuple consisting of |
paulb@156 | 1577 | a directory (indicating the location of the package directory itself) |
paulb@156 | 1578 | and a filename indicating the location of the __init__.py module which |
paulb@156 | 1579 | declares the package's top-level contents. |
paulb@156 | 1580 | """ |
paulb@156 | 1581 | |
paulb@156 | 1582 | filename = self._find_file(d, name) |
paulb@156 | 1583 | if filename: |
paulb@156 | 1584 | init_py = "__init__" + os.path.extsep + "py" |
paulb@156 | 1585 | init_py_filename = self._find_file(filename, init_py) |
paulb@156 | 1586 | if init_py_filename: |
paulb@156 | 1587 | return filename, init_py_filename |
paulb@156 | 1588 | return None |
paulb@156 | 1589 | |
paulb@156 | 1590 | def _find_file(self, d, filename): |
paulb@156 | 1591 | |
paulb@156 | 1592 | """ |
paulb@156 | 1593 | Return the filename obtained when searching the directory 'd' for the |
paulb@156 | 1594 | given 'filename', or None if no actual file exists for the filename. |
paulb@156 | 1595 | """ |
paulb@156 | 1596 | |
paulb@156 | 1597 | filename = os.path.join(d, filename) |
paulb@156 | 1598 | if os.path.exists(filename): |
paulb@156 | 1599 | return filename |
paulb@156 | 1600 | else: |
paulb@156 | 1601 | return None |
paulb@156 | 1602 | |
paulb@158 | 1603 | def load(self, name, builtins, alias=None): |
paulb@156 | 1604 | |
paulb@156 | 1605 | """ |
paulb@156 | 1606 | Load the module or package with the given 'name' and using the specified |
paulb@156 | 1607 | 'builtins'. Return an Attribute object referencing the loaded module or |
paulb@156 | 1608 | package, or None if no such module or package exists. |
paulb@156 | 1609 | """ |
paulb@156 | 1610 | |
paulb@203 | 1611 | if self.modules.has_key(name): |
paulb@203 | 1612 | return Attribute(None, self.modules[name]) |
paulb@203 | 1613 | |
paulb@156 | 1614 | path = name.split(".") |
paulb@156 | 1615 | m = self.find_in_path(path[0]) |
paulb@156 | 1616 | if not m: |
paulb@156 | 1617 | return None # NOTE: Import error. |
paulb@156 | 1618 | d, filename = m |
paulb@203 | 1619 | |
paulb@203 | 1620 | if self.modules.has_key(path[0]): |
paulb@203 | 1621 | top = module = self.modules[path[0]] |
paulb@203 | 1622 | else: |
paulb@203 | 1623 | top = module = self.modules[path[0]] = load(filename, builtins, path[0], self, no_annotate=1) |
paulb@203 | 1624 | annotate(module, builtins, self) |
paulb@156 | 1625 | |
paulb@156 | 1626 | if len(path) > 1: |
paulb@158 | 1627 | path_so_far = path[:1] |
paulb@156 | 1628 | for p in path[1:]: |
paulb@158 | 1629 | path_so_far.append(p) |
paulb@156 | 1630 | m = self.find(d, p) |
paulb@156 | 1631 | if not m: |
paulb@156 | 1632 | return None # NOTE: Import error. |
paulb@156 | 1633 | d, filename = m |
paulb@158 | 1634 | module_name = ".".join(path_so_far) |
paulb@203 | 1635 | |
paulb@203 | 1636 | if self.modules.has_key(module_name): |
paulb@203 | 1637 | submodule = self.modules[module_name] |
paulb@203 | 1638 | else: |
paulb@203 | 1639 | submodule = self.modules[module_name] = load(filename, builtins, module_name, self, no_annotate=1) |
paulb@203 | 1640 | annotate(submodule, builtins, self) |
paulb@156 | 1641 | |
paulb@156 | 1642 | # Store the submodule within its parent module. |
paulb@156 | 1643 | |
paulb@156 | 1644 | module.namespace[p] = [Attribute(None, submodule)] |
paulb@156 | 1645 | module = submodule |
paulb@156 | 1646 | |
paulb@158 | 1647 | if alias: |
paulb@158 | 1648 | return Attribute(None, module) |
paulb@158 | 1649 | else: |
paulb@158 | 1650 | return Attribute(None, top) |
paulb@156 | 1651 | |
paulb@73 | 1652 | def combine(target, additions): |
paulb@91 | 1653 | |
paulb@91 | 1654 | """ |
paulb@91 | 1655 | Merge into the 'target' sequence the given 'additions', preventing duplicate |
paulb@91 | 1656 | items. |
paulb@91 | 1657 | """ |
paulb@91 | 1658 | |
paulb@73 | 1659 | for addition in additions: |
paulb@73 | 1660 | if addition not in target: |
paulb@73 | 1661 | target.append(addition) |
paulb@73 | 1662 | |
paulb@48 | 1663 | def find_attributes(structure, name): |
paulb@48 | 1664 | |
paulb@48 | 1665 | """ |
paulb@48 | 1666 | Find for the given 'structure' all attributes for the given 'name', visiting |
paulb@48 | 1667 | base classes where appropriate and returning the attributes in order of |
paulb@48 | 1668 | descending precedence for all possible base classes. |
paulb@48 | 1669 | |
paulb@48 | 1670 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1671 | the structure involved in accessing the attribute. |
paulb@48 | 1672 | """ |
paulb@48 | 1673 | |
paulb@48 | 1674 | # First attempt to search the instance/class namespace. |
paulb@48 | 1675 | |
paulb@48 | 1676 | try: |
paulb@48 | 1677 | l = structure.namespace.load(name) |
paulb@48 | 1678 | attributes = [] |
paulb@48 | 1679 | for attribute in l: |
paulb@48 | 1680 | attributes.append((attribute, structure)) |
paulb@48 | 1681 | |
paulb@48 | 1682 | # If that does not work, attempt to investigate any class or base classes. |
paulb@48 | 1683 | |
paulb@48 | 1684 | except KeyError: |
paulb@48 | 1685 | attributes = [] |
paulb@48 | 1686 | |
paulb@48 | 1687 | # Investigate any instance's implementing class. |
paulb@48 | 1688 | |
paulb@48 | 1689 | if isinstance(structure, Instance): |
paulb@73 | 1690 | for attr in structure.namespace.load("__class__"): |
paulb@73 | 1691 | cls = attr.type |
paulb@50 | 1692 | l = get_attributes(cls, name) |
paulb@73 | 1693 | combine(attributes, l) |
paulb@48 | 1694 | |
paulb@48 | 1695 | # Investigate any class's base classes. |
paulb@48 | 1696 | |
paulb@48 | 1697 | elif isinstance(structure, Class): |
paulb@48 | 1698 | |
paulb@48 | 1699 | # If no base classes exist, return an indicator that no attribute |
paulb@48 | 1700 | # exists. |
paulb@48 | 1701 | |
paulb@48 | 1702 | if not structure.base_refs: |
paulb@48 | 1703 | return [(None, structure)] |
paulb@48 | 1704 | |
paulb@48 | 1705 | # Otherwise, find all possible base classes. |
paulb@48 | 1706 | |
paulb@48 | 1707 | for base_refs in structure.base_refs: |
paulb@48 | 1708 | base_attributes = [] |
paulb@48 | 1709 | |
paulb@48 | 1710 | # For each base class, find attributes either in the base |
paulb@48 | 1711 | # class or its own base classes. |
paulb@48 | 1712 | |
paulb@48 | 1713 | for base_ref in base_refs: |
paulb@50 | 1714 | l = get_attributes(base_ref, name) |
paulb@73 | 1715 | combine(base_attributes, l) |
paulb@48 | 1716 | |
paulb@73 | 1717 | combine(attributes, base_attributes) |
paulb@48 | 1718 | |
paulb@49 | 1719 | return attributes |
paulb@48 | 1720 | |
paulb@48 | 1721 | def get_attributes(structure, name): |
paulb@48 | 1722 | |
paulb@48 | 1723 | """ |
paulb@48 | 1724 | Return all possible attributes for the given 'structure' having the given |
paulb@48 | 1725 | 'name', wrapping each attribute in an Attribute object which includes |
paulb@48 | 1726 | context information for the attribute access. |
paulb@48 | 1727 | |
paulb@48 | 1728 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1729 | the structure involved in accessing the attribute. |
paulb@48 | 1730 | """ |
paulb@48 | 1731 | |
paulb@48 | 1732 | if isinstance(structure, Attribute): |
paulb@48 | 1733 | structure = structure.type |
paulb@53 | 1734 | results = [] |
paulb@53 | 1735 | for attribute, accessor in find_attributes(structure, name): |
paulb@178 | 1736 | |
paulb@178 | 1737 | # Detect class attribute access via instances. |
paulb@178 | 1738 | |
paulb@178 | 1739 | if attribute is not None and isinstance(structure, Instance) and isinstance(accessor, Class): |
paulb@178 | 1740 | attribute = accessor.get_attribute_for_instance(attribute, structure) |
paulb@178 | 1741 | |
paulb@178 | 1742 | # Produce an attribute with the appropriate context. |
paulb@178 | 1743 | |
paulb@53 | 1744 | if attribute is not None and isinstance(structure, Structure): |
paulb@53 | 1745 | results.append((Attribute(structure, attribute.type), accessor)) |
paulb@53 | 1746 | else: |
paulb@53 | 1747 | results.append((attribute, accessor)) |
paulb@178 | 1748 | |
paulb@53 | 1749 | return results |
paulb@48 | 1750 | |
paulb@207 | 1751 | def prompt(vars): |
paulb@207 | 1752 | try: |
paulb@207 | 1753 | while 1: |
paulb@207 | 1754 | s = raw_input("> ") |
paulb@213 | 1755 | print eval(s, vars) |
paulb@207 | 1756 | except EOFError: |
paulb@207 | 1757 | pass |
paulb@207 | 1758 | |
paulb@57 | 1759 | # Convenience functions. |
paulb@57 | 1760 | |
paulb@203 | 1761 | def load(name, builtins=None, module_name=None, importer=None, no_annotate=0): |
paulb@156 | 1762 | |
paulb@156 | 1763 | """ |
paulb@156 | 1764 | Load the module with the given 'name' (which may be a full module path), |
paulb@156 | 1765 | using the optional 'builtins' to resolve built-in names, and using the |
paulb@156 | 1766 | optional 'importer' to provide a means of finding and loading modules. |
paulb@156 | 1767 | """ |
paulb@156 | 1768 | |
paulb@158 | 1769 | module = simplify.simplify(name, builtins is None, module_name) |
paulb@220 | 1770 | simplify.fixnames.fix(module, builtins) |
paulb@203 | 1771 | if not no_annotate: |
paulb@203 | 1772 | annotate(module, builtins, importer) |
paulb@156 | 1773 | return module |
paulb@156 | 1774 | |
paulb@156 | 1775 | def annotate(module, builtins=None, importer=None): |
paulb@57 | 1776 | |
paulb@57 | 1777 | """ |
paulb@57 | 1778 | Annotate the given 'module', also employing the optional 'builtins' module, |
paulb@156 | 1779 | if specified. If the optional 'importer' is given, use that to find and load |
paulb@156 | 1780 | modules. |
paulb@57 | 1781 | """ |
paulb@57 | 1782 | |
paulb@156 | 1783 | annotator = Annotator(importer) |
paulb@57 | 1784 | if builtins is not None: |
paulb@57 | 1785 | annotator.process(module, builtins) |
paulb@57 | 1786 | else: |
paulb@57 | 1787 | annotator.process(module) |
paulb@57 | 1788 | |
paulb@19 | 1789 | # vim: tabstop=4 expandtab shiftwidth=4 |