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