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@110 | 372 | return conditional |
paulb@110 | 373 | |
paulb@155 | 374 | def visitGlobal(self, global_): |
paulb@155 | 375 | |
paulb@155 | 376 | """ |
paulb@155 | 377 | Return the 'global_' node unprocessed since namespaces should have |
paulb@155 | 378 | already been altered to take global names into consideration. |
paulb@155 | 379 | """ |
paulb@155 | 380 | |
paulb@155 | 381 | return global_ |
paulb@155 | 382 | |
paulb@156 | 383 | def visitImport(self, import_): |
paulb@156 | 384 | |
paulb@156 | 385 | """ |
paulb@156 | 386 | Return the 'import_' node, importing the module with the stated name |
paulb@156 | 387 | and storing details on the node. |
paulb@156 | 388 | """ |
paulb@156 | 389 | |
paulb@156 | 390 | module = self.importer.load(import_.name, self.builtins) |
paulb@156 | 391 | if module is not None: |
paulb@156 | 392 | self.namespace.set_types([module]) |
paulb@156 | 393 | else: |
paulb@156 | 394 | self.namespace.set_types([]) |
paulb@157 | 395 | self.annotate(import_) # mainly for viewing purposes |
paulb@156 | 396 | return import_ |
paulb@156 | 397 | |
paulb@151 | 398 | def _visitInvoke(self, invoke, invocation_types, have_args): |
paulb@151 | 399 | |
paulb@151 | 400 | """ |
paulb@151 | 401 | Return the processed 'invoke' node, using the given 'invocation_types' |
paulb@151 | 402 | as the list of callables to be investigated for instantiation or for the |
paulb@151 | 403 | invocation of functions or blocks. If 'have_args' is a true value, any |
paulb@151 | 404 | invocation or instantiation will involve arguments. |
paulb@151 | 405 | """ |
paulb@151 | 406 | |
paulb@151 | 407 | # Now locate and invoke the subprogram. This can be complicated because |
paulb@151 | 408 | # the target may be a class or object, and there may be many different |
paulb@151 | 409 | # related subprograms. |
paulb@151 | 410 | |
paulb@151 | 411 | invocations = [] |
paulb@151 | 412 | |
paulb@151 | 413 | # Visit each callable in turn, finding subprograms. |
paulb@151 | 414 | |
paulb@151 | 415 | for attr in invocation_types: |
paulb@151 | 416 | |
paulb@151 | 417 | # Deal with class invocations by providing instance objects. |
paulb@151 | 418 | # Here, each class is queried for the __init__ method, which may |
paulb@151 | 419 | # exist for some combinations of classes in a hierarchy but not for |
paulb@151 | 420 | # others. |
paulb@151 | 421 | |
paulb@151 | 422 | if isinstance(attr.type, Class): |
paulb@151 | 423 | attributes = get_attributes(attr.type, "__init__") |
paulb@151 | 424 | |
paulb@151 | 425 | # Deal with object invocations by using __call__ methods. |
paulb@151 | 426 | |
paulb@151 | 427 | elif isinstance(attr.type, Instance): |
paulb@151 | 428 | attributes = get_attributes(attr.type, "__call__") |
paulb@151 | 429 | |
paulb@151 | 430 | # Normal functions or methods are more straightforward. |
paulb@151 | 431 | # Here, we model them using an attribute with no context and with |
paulb@151 | 432 | # no associated accessor. |
paulb@151 | 433 | |
paulb@151 | 434 | else: |
paulb@151 | 435 | attributes = [(attr, None)] |
paulb@151 | 436 | |
paulb@151 | 437 | # Inspect each attribute and extract the subprogram. |
paulb@151 | 438 | |
paulb@151 | 439 | for attribute, accessor in attributes: |
paulb@151 | 440 | |
paulb@151 | 441 | # If a class is involved, presume that it must create a new |
paulb@151 | 442 | # object. |
paulb@151 | 443 | |
paulb@151 | 444 | if isinstance(attr.type, Class): |
paulb@151 | 445 | |
paulb@151 | 446 | # Instantiate the class. |
paulb@151 | 447 | # NOTE: Should probably only allocate a single instance. |
paulb@151 | 448 | |
paulb@151 | 449 | instance = self.new_instance(invoke, "new", attr.type.full_name(), attr.type) |
paulb@151 | 450 | |
paulb@151 | 451 | # For instantiations, switch the context. |
paulb@151 | 452 | |
paulb@151 | 453 | if attribute is not None: |
paulb@151 | 454 | attribute = Attribute(instance, attribute.type) |
paulb@151 | 455 | |
paulb@151 | 456 | # Skip cases where no callable is found. |
paulb@151 | 457 | |
paulb@151 | 458 | if attribute is not None: |
paulb@151 | 459 | |
paulb@151 | 460 | # If a subprogram is defined, invoke it. |
paulb@151 | 461 | |
paulb@151 | 462 | self.invoke_subprogram(invoke, attribute) |
paulb@151 | 463 | if attribute.type not in invocations: |
paulb@151 | 464 | invocations.append(attribute.type) |
paulb@151 | 465 | |
paulb@151 | 466 | elif not isinstance(attr.type, Class): |
paulb@151 | 467 | print "Invocation type is None for", accessor |
paulb@151 | 468 | |
paulb@151 | 469 | else: |
paulb@151 | 470 | |
paulb@151 | 471 | # Test to see if no arguments were supplied in cases where no |
paulb@151 | 472 | # initialiser was found. |
paulb@151 | 473 | |
paulb@151 | 474 | if have_args: |
paulb@151 | 475 | raise AnnotationMessage, "No initialiser found for '%s' with arguments." % attr.type |
paulb@151 | 476 | |
paulb@151 | 477 | # Special case: initialisation. |
paulb@151 | 478 | |
paulb@151 | 479 | if isinstance(attr.type, Class): |
paulb@151 | 480 | |
paulb@151 | 481 | # Associate the instance with the result of this invocation. |
paulb@151 | 482 | |
paulb@151 | 483 | self.namespace.set_types([Attribute(None, instance)]) |
paulb@151 | 484 | self.annotate(invoke) |
paulb@151 | 485 | |
paulb@151 | 486 | # Remember the invocations that were found, along with the return type |
paulb@151 | 487 | # information. |
paulb@151 | 488 | |
paulb@151 | 489 | invoke.invocations = invocations |
paulb@151 | 490 | self.namespace.set_types(getattr(invoke, "types", [])) |
paulb@151 | 491 | return invoke |
paulb@151 | 492 | |
paulb@151 | 493 | def visitInvokeBlock(self, invoke): |
paulb@151 | 494 | |
paulb@151 | 495 | """ |
paulb@151 | 496 | Return the processed 'invoke' node, first finding the callables |
paulb@151 | 497 | indicated by the expression. |
paulb@151 | 498 | """ |
paulb@151 | 499 | |
paulb@151 | 500 | invoke.expr = self.dispatch(invoke.expr) |
paulb@151 | 501 | invocation_types = self.namespace.types |
paulb@151 | 502 | return self._visitInvoke(invoke, invocation_types, have_args=0) |
paulb@151 | 503 | |
paulb@151 | 504 | def visitInvokeFunction(self, invoke): |
paulb@151 | 505 | |
paulb@151 | 506 | """ |
paulb@151 | 507 | Return the processed 'invoke' node, first finding the callables |
paulb@151 | 508 | indicated by the expression. |
paulb@151 | 509 | """ |
paulb@151 | 510 | |
paulb@151 | 511 | invoke.expr = self.dispatch(invoke.expr) |
paulb@151 | 512 | invocation_types = self.namespace.types |
paulb@151 | 513 | |
paulb@151 | 514 | # Invocation processing starts with making sure that the arguments have |
paulb@151 | 515 | # been processed. |
paulb@151 | 516 | |
paulb@151 | 517 | return self._visitInvoke(invoke, invocation_types, have_args=self.process_args(invoke)) |
paulb@151 | 518 | |
paulb@150 | 519 | def visitLoadAttr(self, loadattr): |
paulb@150 | 520 | |
paulb@150 | 521 | """ |
paulb@150 | 522 | Return the 'loadattr' node, processing and storing the expression, and |
paulb@150 | 523 | using the expression's types to construct records of accesses and |
paulb@150 | 524 | non-accesses using the stated attribute name. |
paulb@150 | 525 | """ |
paulb@150 | 526 | |
paulb@150 | 527 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@150 | 528 | types = [] |
paulb@150 | 529 | non_accesses = [] |
paulb@150 | 530 | accesses = {} |
paulb@150 | 531 | for attr in self.namespace.types: |
paulb@150 | 532 | attributes = get_attributes(attr.type, loadattr.name) |
paulb@150 | 533 | if not attributes: |
paulb@150 | 534 | if not attr.type in non_accesses: |
paulb@150 | 535 | non_accesses.append(attr) |
paulb@150 | 536 | |
paulb@150 | 537 | # Revoke this type from any name involved. |
paulb@150 | 538 | |
paulb@150 | 539 | self._prune_non_accesses(loadattr.expr, attr) |
paulb@150 | 540 | |
paulb@150 | 541 | for attribute, accessor in attributes: |
paulb@150 | 542 | if attribute is not None: |
paulb@150 | 543 | types.append(attribute) |
paulb@150 | 544 | if not accesses.has_key(attr.type): |
paulb@150 | 545 | accesses[attr.type] = [] |
paulb@150 | 546 | if not (attribute, accessor) in accesses[attr.type]: |
paulb@150 | 547 | accesses[attr.type].append((attribute, accessor)) |
paulb@150 | 548 | else: |
paulb@150 | 549 | if not attr in non_accesses: |
paulb@150 | 550 | non_accesses.append(attr) |
paulb@150 | 551 | |
paulb@150 | 552 | # Revoke this type from any name involved. |
paulb@150 | 553 | |
paulb@150 | 554 | self._prune_non_accesses(loadattr.expr, attr) |
paulb@150 | 555 | |
paulb@150 | 556 | if not types: |
paulb@150 | 557 | print "No attribute found for", loadattr.name, "given", self.namespace.types |
paulb@150 | 558 | self.namespace.set_types(types) |
paulb@150 | 559 | loadattr.non_accesses = non_accesses |
paulb@150 | 560 | loadattr.accesses = accesses |
paulb@150 | 561 | self.annotate(loadattr) |
paulb@150 | 562 | return loadattr |
paulb@150 | 563 | |
paulb@150 | 564 | def _prune_non_accesses(self, expr, attr): |
paulb@150 | 565 | |
paulb@150 | 566 | """ |
paulb@150 | 567 | Prune type information from 'expr' where the given 'attr' has been |
paulb@150 | 568 | shown to be a non-access. |
paulb@150 | 569 | """ |
paulb@150 | 570 | |
paulb@150 | 571 | if isinstance(expr, LoadName): |
paulb@150 | 572 | self.namespace.revoke(expr.name, attr) |
paulb@150 | 573 | elif isinstance(expr, LoadAttr): |
paulb@150 | 574 | for expr_attr in expr.expr.types: |
paulb@150 | 575 | if hasattr(expr_attr.type, "namespace"): |
paulb@150 | 576 | expr_attr.type.namespace.revoke(expr.name, attr) |
paulb@150 | 577 | elif isinstance(expr, LoadExc): |
paulb@150 | 578 | self.namespace.revoke_exception_type(attr) |
paulb@150 | 579 | |
paulb@150 | 580 | def visitLoadExc(self, loadexc): |
paulb@150 | 581 | |
paulb@150 | 582 | """ |
paulb@150 | 583 | Return the 'loadexc' node, discovering the possible exception types |
paulb@150 | 584 | raised. |
paulb@150 | 585 | """ |
paulb@150 | 586 | |
paulb@156 | 587 | self.namespace.set_types(self.namespace.raises[:]) |
paulb@150 | 588 | self.annotate(loadexc) |
paulb@150 | 589 | return loadexc |
paulb@150 | 590 | |
paulb@150 | 591 | def visitLoadName(self, loadname): |
paulb@150 | 592 | |
paulb@150 | 593 | """ |
paulb@150 | 594 | Return the 'loadname' node, processing the name information on the node |
paulb@150 | 595 | to determine which types are involved with the name. |
paulb@150 | 596 | """ |
paulb@150 | 597 | |
paulb@150 | 598 | self.namespace.set_types(self.namespace.load(loadname.name)) |
paulb@150 | 599 | result = loadname |
paulb@150 | 600 | self.annotate(result) |
paulb@150 | 601 | return result |
paulb@150 | 602 | |
paulb@150 | 603 | def visitLoadRef(self, loadref): |
paulb@150 | 604 | |
paulb@150 | 605 | """ |
paulb@150 | 606 | Return the 'loadref' node, obtaining type information about the |
paulb@150 | 607 | reference stated on the node. |
paulb@150 | 608 | """ |
paulb@150 | 609 | |
paulb@150 | 610 | self.namespace.set_types([Attribute(None, loadref.ref)]) |
paulb@150 | 611 | self.annotate(loadref) |
paulb@150 | 612 | return loadref |
paulb@150 | 613 | |
paulb@150 | 614 | def visitLoadTemp(self, loadtemp): |
paulb@150 | 615 | |
paulb@150 | 616 | """ |
paulb@150 | 617 | Return the 'loadtemp' node, obtaining type information about the |
paulb@150 | 618 | temporary variable accessed, and removing variable information where the |
paulb@150 | 619 | 'release' attribute has been set on the node. |
paulb@150 | 620 | """ |
paulb@150 | 621 | |
paulb@150 | 622 | index = getattr(loadtemp, "index", None) |
paulb@150 | 623 | try: |
paulb@150 | 624 | if getattr(loadtemp, "release", 0): |
paulb@150 | 625 | self.namespace.set_types(self.namespace.temp[index].pop()) |
paulb@150 | 626 | else: |
paulb@150 | 627 | self.namespace.set_types(self.namespace.temp[index][-1]) |
paulb@150 | 628 | except KeyError: |
paulb@150 | 629 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@150 | 630 | self.annotate(loadtemp) |
paulb@150 | 631 | return loadtemp |
paulb@150 | 632 | |
paulb@115 | 633 | def visitModule(self, module): |
paulb@150 | 634 | |
paulb@150 | 635 | """ |
paulb@150 | 636 | Return the processed 'module' whose contents (merely a group of nodes) |
paulb@150 | 637 | are processed. |
paulb@150 | 638 | """ |
paulb@150 | 639 | |
paulb@115 | 640 | module.code = self.dispatches(module.code) |
paulb@115 | 641 | return module |
paulb@115 | 642 | |
paulb@150 | 643 | def visitNot(self, not_): |
paulb@150 | 644 | |
paulb@150 | 645 | "Return the 'not_' node whose expression is processed." |
paulb@150 | 646 | |
paulb@150 | 647 | not_.expr = self.dispatch(not_.expr) |
paulb@150 | 648 | return not_ |
paulb@150 | 649 | |
paulb@115 | 650 | def visitPass(self, pass_): |
paulb@150 | 651 | |
paulb@150 | 652 | "Return the unprocessed 'pass_' node." |
paulb@150 | 653 | |
paulb@115 | 654 | return pass_ |
paulb@115 | 655 | |
paulb@150 | 656 | def visitRaise(self, raise_): |
paulb@150 | 657 | |
paulb@150 | 658 | """ |
paulb@150 | 659 | Return the 'raise_' node, processing any traceback information along |
paulb@150 | 660 | with the raised exception expression, converting the node into a kind of |
paulb@150 | 661 | invocation where the expression is found not to be an invocation itself. |
paulb@150 | 662 | This node affects the namespace, adding exception types to the list of |
paulb@150 | 663 | those raised in the namespace. |
paulb@150 | 664 | """ |
paulb@150 | 665 | |
paulb@150 | 666 | if getattr(raise_, "traceback", None) is not None: |
paulb@150 | 667 | raise_.traceback = self.dispatch(raise_.traceback) |
paulb@150 | 668 | raise_.expr = self.dispatch(raise_.expr) |
paulb@150 | 669 | |
paulb@150 | 670 | # Handle bare name exceptions by converting any classes to instances. |
paulb@150 | 671 | |
paulb@150 | 672 | if not isinstance(raise_.expr, InvokeFunction): |
paulb@150 | 673 | raise_.pos_args = [] |
paulb@150 | 674 | raise_.kw_args = {} |
paulb@150 | 675 | raise_.star = None |
paulb@150 | 676 | raise_.dstar = None |
paulb@150 | 677 | types = [] |
paulb@150 | 678 | for attr in self.namespace.types: |
paulb@150 | 679 | if isinstance(attr.type, Class): |
paulb@150 | 680 | self._visitInvoke(raise_, [attr], have_args=0) |
paulb@150 | 681 | types += self.namespace.types |
paulb@150 | 682 | else: |
paulb@150 | 683 | types = self.namespace.types |
paulb@150 | 684 | |
paulb@150 | 685 | combine(self.namespace.raises, types) |
paulb@150 | 686 | return raise_ |
paulb@150 | 687 | |
paulb@150 | 688 | def visitReleaseTemp(self, releasetemp): |
paulb@150 | 689 | |
paulb@150 | 690 | """ |
paulb@150 | 691 | Return the 'releasetemp' node, removing temporary variable information |
paulb@150 | 692 | from the current namespace. |
paulb@150 | 693 | """ |
paulb@150 | 694 | |
paulb@150 | 695 | index = getattr(releasetemp, "index", None) |
paulb@150 | 696 | try: |
paulb@150 | 697 | self.namespace.temp[index].pop() |
paulb@150 | 698 | except KeyError: |
paulb@150 | 699 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@150 | 700 | except IndexError: |
paulb@150 | 701 | pass #raise AnnotationMessage, "Temporary store index '%s' is empty." % index |
paulb@150 | 702 | return releasetemp |
paulb@150 | 703 | |
paulb@150 | 704 | def visitReturn(self, return_): |
paulb@150 | 705 | |
paulb@150 | 706 | """ |
paulb@150 | 707 | Return the 'return_' node, processing any expression and obtaining type |
paulb@150 | 708 | information to be accumulated in the current namespace's list of return |
paulb@150 | 709 | types. A snapshot of the namespace is taken for the purposes of |
paulb@150 | 710 | reconciling or merging namespaces where subprograms actually share |
paulb@150 | 711 | locals with their callers. |
paulb@150 | 712 | """ |
paulb@150 | 713 | |
paulb@150 | 714 | if hasattr(return_, "expr"): |
paulb@150 | 715 | return_.expr = self.dispatch(return_.expr) |
paulb@150 | 716 | combine(self.namespace.returns, self.namespace.types) |
paulb@150 | 717 | self.annotate(return_) |
paulb@150 | 718 | self.namespace.snapshot() |
paulb@150 | 719 | return return_ |
paulb@150 | 720 | |
paulb@150 | 721 | visitReturnFromBlock = visitReturn |
paulb@150 | 722 | visitReturnFromFunction = visitReturn |
paulb@150 | 723 | |
paulb@150 | 724 | def visitStoreAttr(self, storeattr): |
paulb@150 | 725 | |
paulb@150 | 726 | """ |
paulb@150 | 727 | Return the 'storeattr' node, processing the expression and target, and |
paulb@150 | 728 | using the type information obtained to build records of legitimate |
paulb@150 | 729 | writes to the stated attribute, along with "impossible" non-writes to |
paulb@150 | 730 | the attribute. |
paulb@150 | 731 | """ |
paulb@150 | 732 | |
paulb@150 | 733 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@150 | 734 | expr = self.namespace.types |
paulb@150 | 735 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@150 | 736 | writes = {} |
paulb@150 | 737 | non_writes = [] |
paulb@150 | 738 | for attr in self.namespace.types: |
paulb@154 | 739 | # NOTE: Impose "atomic" constraints on certain types. |
paulb@150 | 740 | if attr is None: |
paulb@150 | 741 | if not attr in non_writes: |
paulb@150 | 742 | non_writes.append(attr) |
paulb@150 | 743 | continue |
paulb@150 | 744 | attr.type.namespace.add(storeattr.name, expr) |
paulb@150 | 745 | writes[attr.type] = attr.type.namespace.load(storeattr.name) |
paulb@150 | 746 | if not writes: |
paulb@150 | 747 | print "Unable to store attribute", storeattr.name, "given", self.namespace.types |
paulb@150 | 748 | storeattr.writes = writes |
paulb@150 | 749 | storeattr.non_writes = non_writes |
paulb@150 | 750 | return storeattr |
paulb@150 | 751 | |
paulb@150 | 752 | def visitStoreName(self, storename): |
paulb@150 | 753 | |
paulb@150 | 754 | """ |
paulb@150 | 755 | Return the 'storename' node, processing the expression on the node and |
paulb@150 | 756 | associating the type information obtained with the stated name in the |
paulb@150 | 757 | current namespace. |
paulb@150 | 758 | """ |
paulb@150 | 759 | |
paulb@150 | 760 | storename.expr = self.dispatch(storename.expr) |
paulb@150 | 761 | self.namespace.store(storename.name, self.namespace.types) |
paulb@150 | 762 | return storename |
paulb@150 | 763 | |
paulb@150 | 764 | def visitStoreTemp(self, storetemp): |
paulb@150 | 765 | |
paulb@150 | 766 | """ |
paulb@150 | 767 | Return the 'storetemp' node, processing the expression on the node and |
paulb@150 | 768 | associating the type information obtained with a temporary variable in |
paulb@150 | 769 | the current namespace. |
paulb@150 | 770 | """ |
paulb@150 | 771 | |
paulb@150 | 772 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@150 | 773 | index = getattr(storetemp, "index", None) |
paulb@150 | 774 | if not self.namespace.temp.has_key(index): |
paulb@150 | 775 | self.namespace.temp[index] = [] |
paulb@150 | 776 | self.namespace.temp[index].append(self.namespace.types) |
paulb@150 | 777 | return storetemp |
paulb@150 | 778 | |
paulb@115 | 779 | def visitSubprogram(self, subprogram): |
paulb@150 | 780 | |
paulb@150 | 781 | """ |
paulb@150 | 782 | Return the 'subprogram' node, processing its contents (a group of nodes |
paulb@150 | 783 | comprising the subprogram). |
paulb@150 | 784 | """ |
paulb@150 | 785 | |
paulb@115 | 786 | subprogram.code = self.dispatches(subprogram.code) |
paulb@115 | 787 | return subprogram |
paulb@115 | 788 | |
paulb@115 | 789 | def visitTry(self, try_): |
paulb@150 | 790 | |
paulb@150 | 791 | """ |
paulb@150 | 792 | Return the 'try_' node, processing the body clause in its own namespace |
paulb@150 | 793 | derived from the current namespace, processing any handler clause using |
paulb@150 | 794 | the namespace information accumulated in the body, and processing any |
paulb@150 | 795 | else and finally clauses, attempting to supply each with appropriate |
paulb@150 | 796 | namespace information. |
paulb@150 | 797 | """ |
paulb@150 | 798 | |
paulb@133 | 799 | is_module = self.namespace is self.module.namespace |
paulb@133 | 800 | |
paulb@115 | 801 | try_.body = self.dispatches(try_.body) |
paulb@133 | 802 | |
paulb@133 | 803 | # Save the namespace from the body. |
paulb@133 | 804 | |
paulb@133 | 805 | body_namespace = Namespace() |
paulb@133 | 806 | body_namespace.merge_namespace(self.namespace) |
paulb@133 | 807 | |
paulb@133 | 808 | # Process the handler. |
paulb@133 | 809 | |
paulb@133 | 810 | if hasattr(try_, "handler"): |
paulb@133 | 811 | try_.handler = self.dispatches(try_.handler) |
paulb@133 | 812 | |
paulb@133 | 813 | # Save the namespace from the handler. |
paulb@133 | 814 | |
paulb@133 | 815 | handler_namespace = Namespace() |
paulb@133 | 816 | handler_namespace.merge_namespace(self.namespace) |
paulb@133 | 817 | |
paulb@133 | 818 | # Remember the raised exceptions encountered so far. |
paulb@133 | 819 | |
paulb@121 | 820 | raises = self.namespace.raises |
paulb@121 | 821 | |
paulb@133 | 822 | # Process the else clause. |
paulb@133 | 823 | |
paulb@133 | 824 | if hasattr(try_, "else_"): |
paulb@133 | 825 | |
paulb@133 | 826 | # Restore the body namespace for the else clause. |
paulb@133 | 827 | |
paulb@133 | 828 | self.namespace = body_namespace |
paulb@133 | 829 | if is_module: |
paulb@133 | 830 | self.module.namespace = self.namespace |
paulb@133 | 831 | |
paulb@133 | 832 | # Empty the raised exceptions for the else clause. |
paulb@121 | 833 | |
paulb@133 | 834 | self.namespace.raises = [] |
paulb@133 | 835 | try_.else_ = self.dispatches(try_.else_) |
paulb@133 | 836 | self.namespace.raises = raises |
paulb@133 | 837 | |
paulb@133 | 838 | # Merge the namespaces. |
paulb@133 | 839 | |
paulb@133 | 840 | self.namespace = Namespace() |
paulb@133 | 841 | if is_module: |
paulb@133 | 842 | self.module.namespace = self.namespace |
paulb@133 | 843 | self.namespace.merge_namespace(body_namespace) |
paulb@133 | 844 | self.namespace.merge_namespace(handler_namespace) |
paulb@133 | 845 | |
paulb@133 | 846 | # Process the finally clause, if any. |
paulb@121 | 847 | |
paulb@115 | 848 | try_.finally_ = self.dispatches(try_.finally_) |
paulb@115 | 849 | return try_ |
paulb@115 | 850 | |
paulb@25 | 851 | # Utility methods. |
paulb@25 | 852 | |
paulb@79 | 853 | def new_instance(self, node, reason, target, type): |
paulb@79 | 854 | |
paulb@79 | 855 | "Create, on the given 'node', a new instance with the given 'type'." |
paulb@79 | 856 | |
paulb@79 | 857 | if not hasattr(node, "instances"): |
paulb@79 | 858 | node.instances = {} |
paulb@79 | 859 | |
paulb@79 | 860 | if not node.instances.has_key((reason, target, type)): |
paulb@91 | 861 | |
paulb@91 | 862 | # Insist on a single instance per type. |
paulb@91 | 863 | # NOTE: Strategy-dependent instantiation. |
paulb@91 | 864 | |
paulb@91 | 865 | if len(type.instances) == 0: |
paulb@91 | 866 | instance = Instance() |
paulb@91 | 867 | instance.namespace = Namespace() |
paulb@91 | 868 | instance.namespace.store("__class__", [Attribute(None, type)]) |
paulb@91 | 869 | type.instances.append(instance) |
paulb@91 | 870 | else: |
paulb@91 | 871 | instance = type.instances[0] |
paulb@91 | 872 | |
paulb@79 | 873 | node.instances[(reason, target, type)] = instance |
paulb@79 | 874 | |
paulb@79 | 875 | return node.instances[(reason, target, type)] |
paulb@79 | 876 | |
paulb@151 | 877 | def invoke_subprogram(self, invoke, attribute): |
paulb@34 | 878 | |
paulb@151 | 879 | """ |
paulb@151 | 880 | Invoke using the given 'invoke' node the subprogram represented by the |
paulb@151 | 881 | given 'attribute'. |
paulb@151 | 882 | """ |
paulb@34 | 883 | |
paulb@54 | 884 | # Test for context information, making it into a real attribute. |
paulb@34 | 885 | |
paulb@151 | 886 | if attribute.context is not None: |
paulb@151 | 887 | context = Attribute(None, attribute.context) |
paulb@151 | 888 | target = attribute.type |
paulb@34 | 889 | else: |
paulb@34 | 890 | context = None |
paulb@151 | 891 | target = attribute.type |
paulb@50 | 892 | |
paulb@154 | 893 | # Test to see if anything has changed. |
paulb@154 | 894 | |
paulb@154 | 895 | if hasattr(invoke, "syscount") and invoke.syscount == self.system.count: |
paulb@154 | 896 | return |
paulb@154 | 897 | |
paulb@154 | 898 | # Remember the state of the system. |
paulb@154 | 899 | |
paulb@154 | 900 | else: |
paulb@154 | 901 | invoke.syscount = self.system.count |
paulb@154 | 902 | |
paulb@34 | 903 | # Provide the correct namespace for the invocation. |
paulb@156 | 904 | # This may be a "shared" namespace... |
paulb@34 | 905 | |
paulb@112 | 906 | if getattr(invoke, "share_locals", 0): |
paulb@34 | 907 | namespace = Namespace() |
paulb@134 | 908 | namespace.merge_namespace(self.namespace, everything=0) |
paulb@132 | 909 | using_module_namespace = self.namespace is self.module.namespace |
paulb@156 | 910 | |
paulb@156 | 911 | # Or it may be a structure... |
paulb@156 | 912 | |
paulb@112 | 913 | elif getattr(target, "structure", None): |
paulb@112 | 914 | namespace = Namespace() |
paulb@132 | 915 | using_module_namespace = 0 |
paulb@156 | 916 | |
paulb@156 | 917 | # Or it may be a new namespace populated with the supplied parameters. |
paulb@156 | 918 | |
paulb@34 | 919 | else: |
paulb@34 | 920 | items = self.make_items(invoke, target, context) |
paulb@150 | 921 | namespace = Namespace() |
paulb@150 | 922 | namespace.merge_items(items) |
paulb@132 | 923 | using_module_namespace = 0 |
paulb@34 | 924 | |
paulb@34 | 925 | # Process the subprogram. |
paulb@132 | 926 | # In order to keep global accesses working, the module namespace must be |
paulb@132 | 927 | # adjusted. |
paulb@132 | 928 | |
paulb@132 | 929 | if using_module_namespace: |
paulb@132 | 930 | self.module.namespace = namespace |
paulb@34 | 931 | |
paulb@53 | 932 | self.process_node(target, namespace) |
paulb@34 | 933 | |
paulb@45 | 934 | # NOTE: Improve and verify this. |
paulb@91 | 935 | # If the invocation returns a value, acquire the return types. |
paulb@34 | 936 | |
paulb@53 | 937 | if getattr(target, "returns_value", 0): |
paulb@49 | 938 | self.namespace.set_types(self.last_returns) |
paulb@34 | 939 | self.annotate(invoke) |
paulb@34 | 940 | |
paulb@132 | 941 | # If it is a normal block, merge the locals. |
paulb@132 | 942 | # This can happen in addition to the above because for things like |
paulb@132 | 943 | # logical expressions, the namespace can be modified whilst values are |
paulb@132 | 944 | # returned as results. |
paulb@91 | 945 | |
paulb@132 | 946 | if getattr(invoke, "share_locals", 0): |
paulb@132 | 947 | self.namespace.reset() |
paulb@135 | 948 | |
paulb@135 | 949 | # Merge the locals snapshots. |
paulb@135 | 950 | |
paulb@45 | 951 | for locals in self.returned_locals: |
paulb@135 | 952 | |
paulb@135 | 953 | # For blocks returning values (such as operations), do not merge |
paulb@135 | 954 | # snapshots or results. |
paulb@135 | 955 | |
paulb@135 | 956 | if getattr(target, "returns_value", 0): |
paulb@135 | 957 | self.namespace.merge_namespace(locals, everything=0) |
paulb@135 | 958 | |
paulb@135 | 959 | # For blocks not returning values (such as loops), merge |
paulb@135 | 960 | # snapshots and results since they contain details of genuine |
paulb@135 | 961 | # returns. |
paulb@135 | 962 | |
paulb@135 | 963 | else: |
paulb@135 | 964 | self.namespace.merge_namespace(locals) |
paulb@34 | 965 | |
paulb@116 | 966 | # Incorporate any raised exceptions. |
paulb@116 | 967 | |
paulb@116 | 968 | combine(self.namespace.raises, self.last_raises) |
paulb@116 | 969 | |
paulb@132 | 970 | # In order to keep global accesses working, the module namespace must be |
paulb@132 | 971 | # adjusted. |
paulb@132 | 972 | |
paulb@132 | 973 | if using_module_namespace: |
paulb@132 | 974 | self.module.namespace = self.namespace |
paulb@132 | 975 | |
paulb@83 | 976 | def process_args(self, invocation): |
paulb@83 | 977 | |
paulb@112 | 978 | """ |
paulb@112 | 979 | Process the arguments associated with an 'invocation'. Return whether |
paulb@112 | 980 | any arguments were processed. |
paulb@112 | 981 | """ |
paulb@54 | 982 | |
paulb@93 | 983 | invocation.pos_args = self.dispatches(invocation.pos_args) |
paulb@93 | 984 | invocation.kw_args = self.dispatch_dict(invocation.kw_args) |
paulb@54 | 985 | |
paulb@54 | 986 | # Get type information for star and dstar arguments. |
paulb@54 | 987 | |
paulb@83 | 988 | if invocation.star is not None: |
paulb@83 | 989 | param, default = invocation.star |
paulb@54 | 990 | default = self.dispatch(default) |
paulb@83 | 991 | invocation.star = param, default |
paulb@54 | 992 | |
paulb@83 | 993 | if invocation.dstar is not None: |
paulb@83 | 994 | param, default = invocation.dstar |
paulb@54 | 995 | default = self.dispatch(default) |
paulb@83 | 996 | invocation.dstar = param, default |
paulb@54 | 997 | |
paulb@112 | 998 | if invocation.pos_args or invocation.kw_args or invocation.star or invocation.dstar: |
paulb@112 | 999 | return 1 |
paulb@112 | 1000 | else: |
paulb@112 | 1001 | return 0 |
paulb@112 | 1002 | |
paulb@34 | 1003 | def make_items(self, invocation, subprogram, context): |
paulb@34 | 1004 | |
paulb@34 | 1005 | """ |
paulb@34 | 1006 | Make an items mapping for the 'invocation' of the 'subprogram' using the |
paulb@34 | 1007 | given 'context' (which may be None). |
paulb@34 | 1008 | """ |
paulb@34 | 1009 | |
paulb@34 | 1010 | if context is not None: |
paulb@93 | 1011 | pos_args = [Self(context)] + invocation.pos_args |
paulb@34 | 1012 | else: |
paulb@93 | 1013 | pos_args = invocation.pos_args |
paulb@154 | 1014 | |
paulb@154 | 1015 | # Duplicate the keyword arguments - we remove them in processing below. |
paulb@154 | 1016 | |
paulb@154 | 1017 | kw_args = {} |
paulb@154 | 1018 | kw_args.update(invocation.kw_args) |
paulb@34 | 1019 | |
paulb@67 | 1020 | # Sort the arguments into positional and keyword arguments. |
paulb@67 | 1021 | |
paulb@24 | 1022 | params = subprogram.params |
paulb@24 | 1023 | items = [] |
paulb@67 | 1024 | star_args = [] |
paulb@24 | 1025 | |
paulb@67 | 1026 | # Match each positional argument, taking excess arguments as star args. |
paulb@67 | 1027 | |
paulb@67 | 1028 | for arg in pos_args: |
paulb@67 | 1029 | if params: |
paulb@24 | 1030 | param, default = params[0] |
paulb@24 | 1031 | if arg is None: |
paulb@54 | 1032 | arg = default |
paulb@140 | 1033 | if hasattr(arg, "types"): |
paulb@140 | 1034 | items.append((param, arg.types)) |
paulb@140 | 1035 | else: |
paulb@140 | 1036 | items.append((param, [])) # Annotation has not succeeded. |
paulb@67 | 1037 | params = params[1:] |
paulb@24 | 1038 | else: |
paulb@67 | 1039 | star_args.append(arg) |
paulb@24 | 1040 | |
paulb@24 | 1041 | # Collect the remaining defaults. |
paulb@24 | 1042 | |
paulb@24 | 1043 | while params: |
paulb@24 | 1044 | param, default = params[0] |
paulb@67 | 1045 | if kw_args.has_key(param): |
paulb@67 | 1046 | arg = kw_args[param] |
paulb@73 | 1047 | del kw_args[param] |
paulb@93 | 1048 | elif default is not None: |
paulb@93 | 1049 | arg = self.dispatch(default) |
paulb@93 | 1050 | else: |
paulb@67 | 1051 | raise AnnotationMessage, "No argument supplied in '%s' for parameter '%s'." % (subprogram, param) |
paulb@140 | 1052 | if hasattr(arg, "types"): |
paulb@140 | 1053 | items.append((param, arg.types)) |
paulb@140 | 1054 | else: |
paulb@140 | 1055 | items.append((param, [])) # Annotation has not succeeded. |
paulb@24 | 1056 | params = params[1:] |
paulb@24 | 1057 | |
paulb@93 | 1058 | dstar_args = kw_args.values() |
paulb@73 | 1059 | |
paulb@73 | 1060 | # Construct temporary objects. |
paulb@73 | 1061 | |
paulb@73 | 1062 | if star_args: |
paulb@80 | 1063 | star_invocation = self.make_star_args(invocation, subprogram, star_args) |
paulb@80 | 1064 | self.dispatch(star_invocation) |
paulb@80 | 1065 | star_types = star_invocation.types |
paulb@73 | 1066 | else: |
paulb@73 | 1067 | star_types = None |
paulb@73 | 1068 | |
paulb@73 | 1069 | if dstar_args: |
paulb@83 | 1070 | dstar_invocation = self.make_dstar_args(invocation, subprogram, dstar_args) # NOTE: To be written! |
paulb@80 | 1071 | self.dispatch(dstar_invocation) |
paulb@80 | 1072 | dstar_types = dstar_invocation.types |
paulb@73 | 1073 | else: |
paulb@73 | 1074 | dstar_types = None |
paulb@73 | 1075 | |
paulb@73 | 1076 | # NOTE: Merge the objects properly. |
paulb@73 | 1077 | |
paulb@73 | 1078 | star_types = star_types or invocation.star and invocation.star.types |
paulb@73 | 1079 | dstar_types = dstar_types or invocation.dstar and invocation.dstar.types |
paulb@73 | 1080 | |
paulb@24 | 1081 | # Add star and dstar. |
paulb@24 | 1082 | |
paulb@73 | 1083 | if star_types is not None: |
paulb@24 | 1084 | if subprogram.star is not None: |
paulb@24 | 1085 | param, default = subprogram.star |
paulb@73 | 1086 | items.append((param, star_types)) |
paulb@24 | 1087 | else: |
paulb@67 | 1088 | raise AnnotationMessage, "Invocation provides unwanted *args." |
paulb@24 | 1089 | elif subprogram.star is not None: |
paulb@24 | 1090 | param, default = subprogram.star |
paulb@154 | 1091 | if not hasattr(default, "types"): |
paulb@154 | 1092 | subprogram.star = param, self.dispatch(default) # NOTE: Review reprocessing. |
paulb@154 | 1093 | items.append((param, default.types)) |
paulb@24 | 1094 | |
paulb@73 | 1095 | if dstar_types is not None: |
paulb@24 | 1096 | if subprogram.dstar is not None: |
paulb@24 | 1097 | param, default = subprogram.dstar |
paulb@73 | 1098 | items.append((param, dstar_types)) |
paulb@24 | 1099 | else: |
paulb@67 | 1100 | raise AnnotationMessage, "Invocation provides unwanted **args." |
paulb@24 | 1101 | elif subprogram.dstar is not None: |
paulb@24 | 1102 | param, default = subprogram.dstar |
paulb@154 | 1103 | if not hasattr(default, "types"): |
paulb@154 | 1104 | subprogram.dstar = param, self.dispatch(default) # NOTE: Review reprocessing. |
paulb@154 | 1105 | items.append((param, default.types)) |
paulb@24 | 1106 | |
paulb@83 | 1107 | # Record the parameter types. |
paulb@83 | 1108 | |
paulb@146 | 1109 | self.annotate_parameters(subprogram, items) |
paulb@146 | 1110 | return subprogram.paramtypes.items() |
paulb@24 | 1111 | |
paulb@80 | 1112 | def make_star_args(self, invocation, subprogram, star_args): |
paulb@80 | 1113 | |
paulb@80 | 1114 | "Make a subprogram which initialises a list containing 'star_args'." |
paulb@80 | 1115 | |
paulb@80 | 1116 | if not hasattr(invocation, "stars"): |
paulb@80 | 1117 | invocation.stars = {} |
paulb@80 | 1118 | |
paulb@80 | 1119 | if not invocation.stars.has_key(subprogram.full_name()): |
paulb@80 | 1120 | code=[ |
paulb@80 | 1121 | StoreTemp( |
paulb@80 | 1122 | expr=InvokeFunction( |
paulb@80 | 1123 | expr=LoadAttr( |
paulb@80 | 1124 | expr=LoadRef( |
paulb@80 | 1125 | ref=self.builtins |
paulb@80 | 1126 | ), |
paulb@80 | 1127 | name="list", |
paulb@80 | 1128 | nstype="module", |
paulb@80 | 1129 | ), |
paulb@80 | 1130 | args=[], |
paulb@80 | 1131 | star=None, |
paulb@80 | 1132 | dstar=None |
paulb@80 | 1133 | ) |
paulb@80 | 1134 | ) |
paulb@80 | 1135 | ] |
paulb@80 | 1136 | |
paulb@80 | 1137 | for arg in star_args: |
paulb@80 | 1138 | code.append( |
paulb@80 | 1139 | InvokeFunction( |
paulb@80 | 1140 | expr=LoadAttr( |
paulb@80 | 1141 | expr=LoadTemp(), |
paulb@80 | 1142 | name="append" |
paulb@80 | 1143 | ), |
paulb@80 | 1144 | args=[arg], |
paulb@80 | 1145 | star=None, |
paulb@80 | 1146 | dstar=None |
paulb@80 | 1147 | ) |
paulb@80 | 1148 | ) |
paulb@80 | 1149 | |
paulb@80 | 1150 | code += [ |
paulb@80 | 1151 | Return(expr=LoadTemp(release=1)) |
paulb@80 | 1152 | ] |
paulb@80 | 1153 | |
paulb@80 | 1154 | invocation.stars[subprogram.full_name()] = InvokeBlock( |
paulb@80 | 1155 | produces_result=1, |
paulb@80 | 1156 | expr=LoadRef( |
paulb@80 | 1157 | ref=Subprogram( |
paulb@80 | 1158 | name=None, |
paulb@80 | 1159 | returns_value=1, |
paulb@80 | 1160 | params=[], |
paulb@80 | 1161 | star=None, |
paulb@80 | 1162 | dstar=None, |
paulb@80 | 1163 | code=code |
paulb@80 | 1164 | ) |
paulb@80 | 1165 | ) |
paulb@80 | 1166 | ) |
paulb@80 | 1167 | |
paulb@80 | 1168 | return invocation.stars[subprogram.full_name()] |
paulb@80 | 1169 | |
paulb@48 | 1170 | # Namespace-related abstractions. |
paulb@48 | 1171 | |
paulb@48 | 1172 | class Namespace: |
paulb@48 | 1173 | |
paulb@48 | 1174 | """ |
paulb@48 | 1175 | A local namespace which may either relate to a genuine set of function |
paulb@50 | 1176 | locals or the initialisation of a structure or module. |
paulb@48 | 1177 | """ |
paulb@48 | 1178 | |
paulb@48 | 1179 | def __init__(self): |
paulb@50 | 1180 | |
paulb@50 | 1181 | """ |
paulb@50 | 1182 | Initialise the namespace with a mapping of local names to possible |
paulb@50 | 1183 | types, a list of return values and of possible returned local |
paulb@50 | 1184 | namespaces. The namespace also tracks the "current" types and a mapping |
paulb@50 | 1185 | of temporary value names to types. |
paulb@50 | 1186 | """ |
paulb@50 | 1187 | |
paulb@48 | 1188 | self.names = {} |
paulb@49 | 1189 | self.returns = [] |
paulb@49 | 1190 | self.return_locals = [] |
paulb@112 | 1191 | self.raises = [] |
paulb@49 | 1192 | self.temp = {} |
paulb@49 | 1193 | self.types = [] |
paulb@49 | 1194 | |
paulb@49 | 1195 | def set_types(self, types): |
paulb@156 | 1196 | |
paulb@156 | 1197 | "Set the current collection of 'types'." |
paulb@156 | 1198 | |
paulb@49 | 1199 | self.types = types |
paulb@48 | 1200 | |
paulb@140 | 1201 | def add(self, name, types): |
paulb@156 | 1202 | |
paulb@156 | 1203 | "Add to the entry with the given 'name' the specified 'types'." |
paulb@156 | 1204 | |
paulb@140 | 1205 | if self.names.has_key(name): |
paulb@140 | 1206 | combine(self.names[name], types) |
paulb@140 | 1207 | else: |
paulb@140 | 1208 | self.store(name, types) |
paulb@140 | 1209 | |
paulb@48 | 1210 | def store(self, name, types): |
paulb@156 | 1211 | |
paulb@156 | 1212 | "Store in (or associate with) the given 'name' the specified 'types'." |
paulb@156 | 1213 | |
paulb@48 | 1214 | self.names[name] = types |
paulb@48 | 1215 | |
paulb@86 | 1216 | __setitem__ = store |
paulb@74 | 1217 | |
paulb@48 | 1218 | def load(self, name): |
paulb@156 | 1219 | |
paulb@156 | 1220 | "Load the types associated with the given 'name'." |
paulb@156 | 1221 | |
paulb@48 | 1222 | return self.names[name] |
paulb@48 | 1223 | |
paulb@86 | 1224 | __getitem__ = load |
paulb@74 | 1225 | |
paulb@131 | 1226 | def revoke(self, name, type): |
paulb@156 | 1227 | |
paulb@156 | 1228 | "Revoke from the entry for the given 'name' the specified 'type'." |
paulb@156 | 1229 | |
paulb@131 | 1230 | self.names[name].remove(type) |
paulb@131 | 1231 | |
paulb@133 | 1232 | def revoke_exception_type(self, type): |
paulb@156 | 1233 | |
paulb@156 | 1234 | "Revoke the given 'type' from the collection of exception types." |
paulb@156 | 1235 | |
paulb@133 | 1236 | self.raises.remove(type) |
paulb@133 | 1237 | |
paulb@134 | 1238 | def merge_namespace(self, namespace, everything=1): |
paulb@156 | 1239 | |
paulb@156 | 1240 | """ |
paulb@156 | 1241 | Merge items from the given 'namespace' with this namespace. When the |
paulb@156 | 1242 | optional 'everything' parameter is set to a false value (unlike the |
paulb@156 | 1243 | default), return values and locals snapshots will not be copied to this |
paulb@156 | 1244 | namespace. |
paulb@156 | 1245 | """ |
paulb@156 | 1246 | |
paulb@68 | 1247 | self.merge_items(namespace.names.items()) |
paulb@134 | 1248 | if everything: |
paulb@134 | 1249 | combine(self.returns, namespace.returns) |
paulb@132 | 1250 | combine(self.return_locals, namespace.return_locals) |
paulb@115 | 1251 | combine(self.raises, namespace.raises) |
paulb@134 | 1252 | for name, values in namespace.temp.items(): |
paulb@134 | 1253 | if values: |
paulb@134 | 1254 | if not self.temp.has_key(name) or not self.temp[name]: |
paulb@134 | 1255 | self.temp[name] = [[]] |
paulb@134 | 1256 | combine(self.temp[name][-1], values[-1]) |
paulb@68 | 1257 | |
paulb@68 | 1258 | def merge_items(self, items): |
paulb@156 | 1259 | |
paulb@156 | 1260 | "Merge the given 'items' with this namespace." |
paulb@156 | 1261 | |
paulb@68 | 1262 | for name, types in items: |
paulb@68 | 1263 | self.merge(name, types) |
paulb@68 | 1264 | |
paulb@48 | 1265 | def merge(self, name, types): |
paulb@156 | 1266 | |
paulb@156 | 1267 | "Merge the entry for the given 'name' and 'types' with this namespace." |
paulb@156 | 1268 | |
paulb@48 | 1269 | if not self.names.has_key(name): |
paulb@48 | 1270 | self.names[name] = types[:] |
paulb@48 | 1271 | else: |
paulb@48 | 1272 | existing = self.names[name] |
paulb@73 | 1273 | combine(existing, types) |
paulb@48 | 1274 | |
paulb@49 | 1275 | def snapshot(self): |
paulb@49 | 1276 | |
paulb@49 | 1277 | "Make a snapshot of the locals and remember them." |
paulb@49 | 1278 | |
paulb@49 | 1279 | namespace = Namespace() |
paulb@49 | 1280 | namespace.merge_namespace(self) |
paulb@49 | 1281 | self.return_locals.append(namespace) |
paulb@49 | 1282 | |
paulb@132 | 1283 | def reset(self): |
paulb@132 | 1284 | |
paulb@132 | 1285 | "Reset a namespace in preparation for merging with returned locals." |
paulb@132 | 1286 | |
paulb@132 | 1287 | self.names = {} |
paulb@132 | 1288 | |
paulb@48 | 1289 | def __repr__(self): |
paulb@48 | 1290 | return repr(self.names) |
paulb@48 | 1291 | |
paulb@48 | 1292 | class Attribute: |
paulb@48 | 1293 | |
paulb@48 | 1294 | """ |
paulb@48 | 1295 | An attribute abstraction, indicating the type of the attribute along with |
paulb@48 | 1296 | its context or origin. |
paulb@48 | 1297 | """ |
paulb@48 | 1298 | |
paulb@48 | 1299 | def __init__(self, context, type): |
paulb@48 | 1300 | self.context = context |
paulb@48 | 1301 | self.type = type |
paulb@48 | 1302 | |
paulb@48 | 1303 | def __eq__(self, other): |
paulb@48 | 1304 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@48 | 1305 | |
paulb@48 | 1306 | def __repr__(self): |
paulb@54 | 1307 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@48 | 1308 | |
paulb@53 | 1309 | class Self: |
paulb@91 | 1310 | |
paulb@116 | 1311 | """ |
paulb@116 | 1312 | A program node encapsulating object/context information in an argument list. |
paulb@116 | 1313 | This is not particularly like Attribute, Class, Instance or other such |
paulb@116 | 1314 | things, since it actually appears in the program representation. |
paulb@116 | 1315 | """ |
paulb@91 | 1316 | |
paulb@53 | 1317 | def __init__(self, attribute): |
paulb@53 | 1318 | self.types = [attribute] |
paulb@53 | 1319 | |
paulb@156 | 1320 | class Importer: |
paulb@156 | 1321 | |
paulb@156 | 1322 | "An import machine, searching for and loading modules." |
paulb@156 | 1323 | |
paulb@156 | 1324 | def __init__(self, path=None): |
paulb@156 | 1325 | |
paulb@156 | 1326 | """ |
paulb@156 | 1327 | Initialise the importer with the given search 'path' - a list of |
paulb@156 | 1328 | directories to search for Python modules. |
paulb@156 | 1329 | """ |
paulb@156 | 1330 | |
paulb@156 | 1331 | self.path = path or [os.getcwd()] |
paulb@156 | 1332 | self.modules = [] |
paulb@156 | 1333 | |
paulb@156 | 1334 | def find_in_path(self, name): |
paulb@156 | 1335 | |
paulb@156 | 1336 | """ |
paulb@156 | 1337 | Find the given module 'name' in the search path, returning None where no |
paulb@156 | 1338 | such module could be found, or a 2-tuple from the 'find' method |
paulb@156 | 1339 | otherwise. |
paulb@156 | 1340 | """ |
paulb@156 | 1341 | |
paulb@156 | 1342 | for d in self.path: |
paulb@156 | 1343 | m = self.find(d, name) |
paulb@156 | 1344 | if m: return m |
paulb@156 | 1345 | return None |
paulb@156 | 1346 | |
paulb@156 | 1347 | def find(self, d, name): |
paulb@156 | 1348 | |
paulb@156 | 1349 | """ |
paulb@156 | 1350 | In the directory 'd', find the given module 'name', where 'name' can |
paulb@156 | 1351 | either refer to a single file module or to a package. Return None if the |
paulb@156 | 1352 | 'name' cannot be associated with either a file or a package directory, |
paulb@156 | 1353 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paulb@156 | 1354 | """ |
paulb@156 | 1355 | |
paulb@156 | 1356 | m = self._find_package(d, name) |
paulb@156 | 1357 | if m: return m |
paulb@156 | 1358 | m = self._find_module(d, name) |
paulb@156 | 1359 | if m: return m |
paulb@156 | 1360 | return None |
paulb@156 | 1361 | |
paulb@156 | 1362 | def _find_module(self, d, name): |
paulb@156 | 1363 | |
paulb@156 | 1364 | """ |
paulb@156 | 1365 | In the directory 'd', find the given module 'name', returning None where |
paulb@156 | 1366 | no suitable file exists in the directory, or a 2-tuple consisting of |
paulb@156 | 1367 | None (indicating that no package directory is involved) and a filename |
paulb@156 | 1368 | indicating the location of the module. |
paulb@156 | 1369 | """ |
paulb@156 | 1370 | |
paulb@156 | 1371 | name_py = name + os.extsep + "py" |
paulb@156 | 1372 | filename = self._find_file(d, name_py) |
paulb@156 | 1373 | if filename: |
paulb@156 | 1374 | return None, filename |
paulb@156 | 1375 | return None |
paulb@156 | 1376 | |
paulb@156 | 1377 | def _find_package(self, d, name): |
paulb@156 | 1378 | |
paulb@156 | 1379 | """ |
paulb@156 | 1380 | In the directory 'd', find the given package 'name', returning None |
paulb@156 | 1381 | where no suitable package directory exists, or a 2-tuple consisting of |
paulb@156 | 1382 | a directory (indicating the location of the package directory itself) |
paulb@156 | 1383 | and a filename indicating the location of the __init__.py module which |
paulb@156 | 1384 | declares the package's top-level contents. |
paulb@156 | 1385 | """ |
paulb@156 | 1386 | |
paulb@156 | 1387 | filename = self._find_file(d, name) |
paulb@156 | 1388 | if filename: |
paulb@156 | 1389 | init_py = "__init__" + os.path.extsep + "py" |
paulb@156 | 1390 | init_py_filename = self._find_file(filename, init_py) |
paulb@156 | 1391 | if init_py_filename: |
paulb@156 | 1392 | return filename, init_py_filename |
paulb@156 | 1393 | return None |
paulb@156 | 1394 | |
paulb@156 | 1395 | def _find_file(self, d, filename): |
paulb@156 | 1396 | |
paulb@156 | 1397 | """ |
paulb@156 | 1398 | Return the filename obtained when searching the directory 'd' for the |
paulb@156 | 1399 | given 'filename', or None if no actual file exists for the filename. |
paulb@156 | 1400 | """ |
paulb@156 | 1401 | |
paulb@156 | 1402 | filename = os.path.join(d, filename) |
paulb@156 | 1403 | if os.path.exists(filename): |
paulb@156 | 1404 | return filename |
paulb@156 | 1405 | else: |
paulb@156 | 1406 | return None |
paulb@156 | 1407 | |
paulb@156 | 1408 | def load(self, name, builtins): |
paulb@156 | 1409 | |
paulb@156 | 1410 | """ |
paulb@156 | 1411 | Load the module or package with the given 'name' and using the specified |
paulb@156 | 1412 | 'builtins'. Return an Attribute object referencing the loaded module or |
paulb@156 | 1413 | package, or None if no such module or package exists. |
paulb@156 | 1414 | """ |
paulb@156 | 1415 | |
paulb@156 | 1416 | path = name.split(".") |
paulb@156 | 1417 | m = self.find_in_path(path[0]) |
paulb@156 | 1418 | if not m: |
paulb@156 | 1419 | return None # NOTE: Import error. |
paulb@156 | 1420 | d, filename = m |
paulb@156 | 1421 | module = load(filename, builtins) |
paulb@156 | 1422 | self.modules.append(module) |
paulb@156 | 1423 | |
paulb@156 | 1424 | if len(path) > 1: |
paulb@156 | 1425 | for p in path[1:]: |
paulb@156 | 1426 | m = self.find(d, p) |
paulb@156 | 1427 | if not m: |
paulb@156 | 1428 | return None # NOTE: Import error. |
paulb@156 | 1429 | d, filename = m |
paulb@156 | 1430 | submodule = load(filename, builtins) |
paulb@156 | 1431 | self.modules.append(submodule) |
paulb@156 | 1432 | |
paulb@156 | 1433 | # Store the submodule within its parent module. |
paulb@156 | 1434 | |
paulb@156 | 1435 | module.namespace[p] = [Attribute(None, submodule)] |
paulb@156 | 1436 | module = submodule |
paulb@156 | 1437 | |
paulb@156 | 1438 | return Attribute(None, module) |
paulb@156 | 1439 | |
paulb@73 | 1440 | def combine(target, additions): |
paulb@91 | 1441 | |
paulb@91 | 1442 | """ |
paulb@91 | 1443 | Merge into the 'target' sequence the given 'additions', preventing duplicate |
paulb@91 | 1444 | items. |
paulb@91 | 1445 | """ |
paulb@91 | 1446 | |
paulb@73 | 1447 | for addition in additions: |
paulb@73 | 1448 | if addition not in target: |
paulb@73 | 1449 | target.append(addition) |
paulb@73 | 1450 | |
paulb@48 | 1451 | def find_attributes(structure, name): |
paulb@48 | 1452 | |
paulb@48 | 1453 | """ |
paulb@48 | 1454 | Find for the given 'structure' all attributes for the given 'name', visiting |
paulb@48 | 1455 | base classes where appropriate and returning the attributes in order of |
paulb@48 | 1456 | descending precedence for all possible base classes. |
paulb@48 | 1457 | |
paulb@48 | 1458 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1459 | the structure involved in accessing the attribute. |
paulb@48 | 1460 | """ |
paulb@48 | 1461 | |
paulb@48 | 1462 | # First attempt to search the instance/class namespace. |
paulb@48 | 1463 | |
paulb@48 | 1464 | try: |
paulb@48 | 1465 | l = structure.namespace.load(name) |
paulb@48 | 1466 | attributes = [] |
paulb@48 | 1467 | for attribute in l: |
paulb@48 | 1468 | attributes.append((attribute, structure)) |
paulb@48 | 1469 | |
paulb@48 | 1470 | # If that does not work, attempt to investigate any class or base classes. |
paulb@48 | 1471 | |
paulb@48 | 1472 | except KeyError: |
paulb@48 | 1473 | attributes = [] |
paulb@48 | 1474 | |
paulb@48 | 1475 | # Investigate any instance's implementing class. |
paulb@48 | 1476 | |
paulb@48 | 1477 | if isinstance(structure, Instance): |
paulb@73 | 1478 | for attr in structure.namespace.load("__class__"): |
paulb@73 | 1479 | cls = attr.type |
paulb@50 | 1480 | l = get_attributes(cls, name) |
paulb@73 | 1481 | combine(attributes, l) |
paulb@48 | 1482 | |
paulb@48 | 1483 | # Investigate any class's base classes. |
paulb@48 | 1484 | |
paulb@48 | 1485 | elif isinstance(structure, Class): |
paulb@48 | 1486 | |
paulb@48 | 1487 | # If no base classes exist, return an indicator that no attribute |
paulb@48 | 1488 | # exists. |
paulb@48 | 1489 | |
paulb@48 | 1490 | if not structure.base_refs: |
paulb@48 | 1491 | return [(None, structure)] |
paulb@48 | 1492 | |
paulb@48 | 1493 | # Otherwise, find all possible base classes. |
paulb@48 | 1494 | |
paulb@48 | 1495 | for base_refs in structure.base_refs: |
paulb@48 | 1496 | base_attributes = [] |
paulb@48 | 1497 | |
paulb@48 | 1498 | # For each base class, find attributes either in the base |
paulb@48 | 1499 | # class or its own base classes. |
paulb@48 | 1500 | |
paulb@48 | 1501 | for base_ref in base_refs: |
paulb@50 | 1502 | l = get_attributes(base_ref, name) |
paulb@73 | 1503 | combine(base_attributes, l) |
paulb@48 | 1504 | |
paulb@73 | 1505 | combine(attributes, base_attributes) |
paulb@48 | 1506 | |
paulb@49 | 1507 | return attributes |
paulb@48 | 1508 | |
paulb@48 | 1509 | def get_attributes(structure, name): |
paulb@48 | 1510 | |
paulb@48 | 1511 | """ |
paulb@48 | 1512 | Return all possible attributes for the given 'structure' having the given |
paulb@48 | 1513 | 'name', wrapping each attribute in an Attribute object which includes |
paulb@48 | 1514 | context information for the attribute access. |
paulb@48 | 1515 | |
paulb@48 | 1516 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1517 | the structure involved in accessing the attribute. |
paulb@48 | 1518 | """ |
paulb@48 | 1519 | |
paulb@48 | 1520 | if isinstance(structure, Attribute): |
paulb@48 | 1521 | structure = structure.type |
paulb@53 | 1522 | results = [] |
paulb@53 | 1523 | for attribute, accessor in find_attributes(structure, name): |
paulb@53 | 1524 | if attribute is not None and isinstance(structure, Structure): |
paulb@53 | 1525 | results.append((Attribute(structure, attribute.type), accessor)) |
paulb@53 | 1526 | else: |
paulb@53 | 1527 | results.append((attribute, accessor)) |
paulb@53 | 1528 | return results |
paulb@48 | 1529 | |
paulb@57 | 1530 | # Convenience functions. |
paulb@57 | 1531 | |
paulb@156 | 1532 | def load(name, builtins=None, importer=None): |
paulb@156 | 1533 | |
paulb@156 | 1534 | """ |
paulb@156 | 1535 | Load the module with the given 'name' (which may be a full module path), |
paulb@156 | 1536 | using the optional 'builtins' to resolve built-in names, and using the |
paulb@156 | 1537 | optional 'importer' to provide a means of finding and loading modules. |
paulb@156 | 1538 | """ |
paulb@156 | 1539 | |
paulb@156 | 1540 | module = simplify.simplify(name, builtins is None) |
paulb@156 | 1541 | fixnames.fix(module, builtins) |
paulb@156 | 1542 | annotate(module, builtins, importer) |
paulb@156 | 1543 | return module |
paulb@156 | 1544 | |
paulb@156 | 1545 | def annotate(module, builtins=None, importer=None): |
paulb@57 | 1546 | |
paulb@57 | 1547 | """ |
paulb@57 | 1548 | Annotate the given 'module', also employing the optional 'builtins' module, |
paulb@156 | 1549 | if specified. If the optional 'importer' is given, use that to find and load |
paulb@156 | 1550 | modules. |
paulb@57 | 1551 | """ |
paulb@57 | 1552 | |
paulb@156 | 1553 | annotator = Annotator(importer) |
paulb@57 | 1554 | if builtins is not None: |
paulb@57 | 1555 | annotator.process(module, builtins) |
paulb@57 | 1556 | else: |
paulb@57 | 1557 | annotator.process(module) |
paulb@57 | 1558 | |
paulb@19 | 1559 | # vim: tabstop=4 expandtab shiftwidth=4 |