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