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@57 | 27 | To use this module, the easiest approach is to use the annotate function: |
paulb@57 | 28 | |
paulb@57 | 29 | annotate(module, builtins) |
paulb@57 | 30 | |
paulb@57 | 31 | The more complicated approach involves obtaining an Annotator: |
paulb@50 | 32 | |
paulb@50 | 33 | annotator = Annotator() |
paulb@50 | 34 | |
paulb@57 | 35 | Then, processing an existing module with it: |
paulb@50 | 36 | |
paulb@57 | 37 | annotator.process(module) |
paulb@50 | 38 | |
paulb@50 | 39 | If a module containing built-in classes and functions has already been |
paulb@50 | 40 | annotated, such a module should be passed in as an additional argument: |
paulb@50 | 41 | |
paulb@57 | 42 | annotator.process(module, builtins) |
paulb@19 | 43 | """ |
paulb@19 | 44 | |
paulb@19 | 45 | from simplified import * |
paulb@19 | 46 | import compiler |
paulb@19 | 47 | |
paulb@24 | 48 | class System: |
paulb@24 | 49 | |
paulb@50 | 50 | """ |
paulb@50 | 51 | A class maintaining the state of the annotation system. When the system |
paulb@50 | 52 | counter can no longer be incremented by any annotation operation, the |
paulb@50 | 53 | system may be considered stable and fully annotated. |
paulb@50 | 54 | """ |
paulb@24 | 55 | |
paulb@24 | 56 | def __init__(self): |
paulb@24 | 57 | self.count = 0 |
paulb@146 | 58 | |
paulb@24 | 59 | def init(self, node): |
paulb@150 | 60 | |
paulb@150 | 61 | "Initialise a 'node' for annotation." |
paulb@150 | 62 | |
paulb@24 | 63 | if not hasattr(node, "types"): |
paulb@24 | 64 | node.types = [] |
paulb@146 | 65 | |
paulb@24 | 66 | def annotate(self, node, types): |
paulb@150 | 67 | |
paulb@150 | 68 | "Annotate the given 'node' with the given 'types'." |
paulb@150 | 69 | |
paulb@24 | 70 | self.init(node) |
paulb@146 | 71 | self.combine(node.types, types) |
paulb@146 | 72 | |
paulb@146 | 73 | def combine(self, target, types): |
paulb@150 | 74 | |
paulb@150 | 75 | """ |
paulb@150 | 76 | Combine the 'target' list with the given 'types', counting new members. |
paulb@150 | 77 | """ |
paulb@150 | 78 | |
paulb@24 | 79 | for type in types: |
paulb@146 | 80 | if type not in target: |
paulb@146 | 81 | target.append(type) |
paulb@24 | 82 | self.count += 1 |
paulb@24 | 83 | |
paulb@24 | 84 | system = System() |
paulb@24 | 85 | |
paulb@58 | 86 | # Exceptions. |
paulb@58 | 87 | |
paulb@83 | 88 | class AnnotationError(SimplifiedError): |
paulb@83 | 89 | |
paulb@83 | 90 | "An error in the annotation process." |
paulb@83 | 91 | |
paulb@83 | 92 | pass |
paulb@58 | 93 | |
paulb@67 | 94 | class AnnotationMessage(Exception): |
paulb@83 | 95 | |
paulb@83 | 96 | "A lesser annotation error." |
paulb@83 | 97 | |
paulb@67 | 98 | pass |
paulb@67 | 99 | |
paulb@24 | 100 | # Annotation. |
paulb@19 | 101 | |
paulb@20 | 102 | class Annotator(Visitor): |
paulb@24 | 103 | |
paulb@24 | 104 | """ |
paulb@24 | 105 | The type annotator which traverses the program nodes, typically depth-first, |
paulb@24 | 106 | and maintains a record of the current set of types applying to the currently |
paulb@24 | 107 | considered operation. Such types are also recorded on the nodes, and a |
paulb@24 | 108 | special "system" record is maintained to monitor the level of annotation |
paulb@24 | 109 | activity with a view to recognising when no more annotations are possible. |
paulb@50 | 110 | |
paulb@50 | 111 | Throughout the annotation activity, type information consists of lists of |
paulb@50 | 112 | Attribute objects where such objects retain information about the context of |
paulb@50 | 113 | the type (since a value in the program may be associated with an object or |
paulb@50 | 114 | class) and the actual type of the value being manipulated. Upon accessing |
paulb@50 | 115 | attribute information on namespaces, additional accessor information is also |
paulb@50 | 116 | exchanged - this provides a means of distinguishing between the different |
paulb@50 | 117 | types possible when the means of constructing the namespace may depend on |
paulb@50 | 118 | run-time behaviour. |
paulb@151 | 119 | |
paulb@151 | 120 | Covered: Assign, CheckExc, Conditional, InvokeBlock, InvokeFunction, |
paulb@151 | 121 | LoadAttr, LoadExc, LoadName, LoadRef, LoadTemp, Module, Not, Pass, |
paulb@151 | 122 | Raise, ReleaseTemp, ReturnFromBlock, ReturnFromFunction, StoreAttr, |
paulb@151 | 123 | StoreName, StoreTemp, Subprogram, Try. |
paulb@151 | 124 | |
paulb@154 | 125 | Missing: Global, Import. |
paulb@24 | 126 | """ |
paulb@24 | 127 | |
paulb@19 | 128 | def __init__(self): |
paulb@50 | 129 | |
paulb@50 | 130 | "Initialise the visitor." |
paulb@50 | 131 | |
paulb@19 | 132 | Visitor.__init__(self) |
paulb@24 | 133 | self.system = system |
paulb@19 | 134 | |
paulb@30 | 135 | # Satisfy visitor issues. |
paulb@30 | 136 | |
paulb@30 | 137 | self.visitor = self |
paulb@30 | 138 | |
paulb@57 | 139 | def process(self, module, builtins=None): |
paulb@45 | 140 | |
paulb@45 | 141 | """ |
paulb@57 | 142 | Process the given 'module', using the optional 'builtins' to access |
paulb@57 | 143 | built-in classes and functions. |
paulb@45 | 144 | """ |
paulb@45 | 145 | |
paulb@45 | 146 | self.subprograms = [] |
paulb@45 | 147 | self.current_subprograms = [] |
paulb@45 | 148 | self.current_namespaces = [] |
paulb@115 | 149 | self.namespace = None |
paulb@121 | 150 | self.module = module |
paulb@34 | 151 | |
paulb@34 | 152 | # Give constants their own namespace. |
paulb@34 | 153 | |
paulb@57 | 154 | for value, constant in module.simplifier.constants.items(): |
paulb@34 | 155 | constant.namespace = Namespace() |
paulb@34 | 156 | |
paulb@34 | 157 | # Process the module, supplying builtins if possible. |
paulb@34 | 158 | |
paulb@80 | 159 | self.builtins = builtins |
paulb@45 | 160 | self.global_namespace = Namespace() |
paulb@45 | 161 | |
paulb@57 | 162 | if builtins is not None: |
paulb@57 | 163 | self.builtins_namespace = builtins.namespace |
paulb@34 | 164 | else: |
paulb@45 | 165 | self.builtins_namespace = self.global_namespace |
paulb@34 | 166 | |
paulb@132 | 167 | return self.process_node(module, self.global_namespace) |
paulb@45 | 168 | |
paulb@132 | 169 | def process_node(self, node, locals): |
paulb@24 | 170 | |
paulb@24 | 171 | """ |
paulb@132 | 172 | Process a subprogram or module 'node', indicating the initial 'locals'. |
paulb@34 | 173 | Return an annotated subprogram or module. Note that this method may |
paulb@34 | 174 | mutate nodes in the original program. |
paulb@24 | 175 | """ |
paulb@24 | 176 | |
paulb@115 | 177 | # Record the current subprogram and namespace. |
paulb@115 | 178 | |
paulb@115 | 179 | self.current_subprograms.append(node) |
paulb@115 | 180 | |
paulb@67 | 181 | # Determine the namespace. |
paulb@67 | 182 | |
paulb@132 | 183 | self.current_namespaces.append(self.namespace) |
paulb@132 | 184 | self.namespace = locals |
paulb@45 | 185 | |
paulb@25 | 186 | # Add namespace details to any structure involved. |
paulb@25 | 187 | |
paulb@34 | 188 | if getattr(node, "structure", None) is not None: |
paulb@34 | 189 | node.structure.namespace = Namespace() |
paulb@25 | 190 | |
paulb@30 | 191 | # Initialise bases where appropriate. |
paulb@19 | 192 | |
paulb@30 | 193 | if hasattr(node.structure, "bases"): |
paulb@30 | 194 | base_refs = [] |
paulb@30 | 195 | for base in node.structure.bases: |
paulb@30 | 196 | self.dispatch(base) |
paulb@49 | 197 | base_refs.append(self.namespace.types) |
paulb@30 | 198 | node.structure.base_refs = base_refs |
paulb@30 | 199 | |
paulb@30 | 200 | # Dispatch to the code itself. |
paulb@30 | 201 | |
paulb@49 | 202 | node.namespace = self.namespace |
paulb@20 | 203 | result = self.dispatch(node) |
paulb@49 | 204 | result.namespace = self.namespace |
paulb@49 | 205 | |
paulb@49 | 206 | # Obtain the return values. |
paulb@49 | 207 | |
paulb@49 | 208 | self.last_returns = self.namespace.returns |
paulb@116 | 209 | self.last_raises = self.namespace.raises |
paulb@49 | 210 | self.returned_locals = self.namespace.return_locals |
paulb@45 | 211 | |
paulb@45 | 212 | # Restore the previous subprogram and namespace. |
paulb@45 | 213 | |
paulb@132 | 214 | self.namespace = self.current_namespaces.pop() |
paulb@45 | 215 | self.current_subprograms.pop() |
paulb@45 | 216 | |
paulb@20 | 217 | return result |
paulb@20 | 218 | |
paulb@83 | 219 | def annotate(self, node, types=None): |
paulb@24 | 220 | |
paulb@83 | 221 | """ |
paulb@83 | 222 | Annotate the given 'node' in the system, using either the optional |
paulb@83 | 223 | 'types' or the namespace's current type information. |
paulb@83 | 224 | """ |
paulb@24 | 225 | |
paulb@83 | 226 | self.system.annotate(node, types or self.namespace.types) |
paulb@34 | 227 | |
paulb@146 | 228 | def annotate_parameters(self, node, items): |
paulb@146 | 229 | |
paulb@146 | 230 | """ |
paulb@146 | 231 | Annotate the given 'node' using the given 'items' and updating the |
paulb@146 | 232 | system's annotation counter. |
paulb@146 | 233 | """ |
paulb@146 | 234 | |
paulb@146 | 235 | if not hasattr(node, "paramtypes"): |
paulb@146 | 236 | node.paramtypes = {} |
paulb@146 | 237 | |
paulb@146 | 238 | for param, types in items: |
paulb@146 | 239 | if not node.paramtypes.has_key(param): |
paulb@146 | 240 | node.paramtypes[param] = [] |
paulb@146 | 241 | self.system.combine(node.paramtypes[param], types) |
paulb@146 | 242 | |
paulb@25 | 243 | # Visitor methods. |
paulb@25 | 244 | |
paulb@19 | 245 | def default(self, node): |
paulb@24 | 246 | |
paulb@24 | 247 | """ |
paulb@24 | 248 | Process the given 'node', given that it does not have a specific |
paulb@24 | 249 | handler. |
paulb@24 | 250 | """ |
paulb@24 | 251 | |
paulb@115 | 252 | raise AnnotationMessage, "Node '%s' not supported." % node |
paulb@20 | 253 | |
paulb@20 | 254 | def dispatch(self, node, *args): |
paulb@34 | 255 | try: |
paulb@34 | 256 | return Visitor.dispatch(self, node, *args) |
paulb@67 | 257 | except AnnotationError, exc: |
paulb@58 | 258 | exc.add(node) |
paulb@34 | 259 | raise |
paulb@67 | 260 | except AnnotationMessage, exc: |
paulb@67 | 261 | raise AnnotationError(exc, node) |
paulb@19 | 262 | |
paulb@150 | 263 | # Specific node methods. |
paulb@110 | 264 | |
paulb@115 | 265 | def visitAssign(self, assign): |
paulb@150 | 266 | |
paulb@150 | 267 | """ |
paulb@150 | 268 | Return the 'assign' node whose contents (merely a group of nodes) have |
paulb@150 | 269 | been processed. |
paulb@150 | 270 | """ |
paulb@150 | 271 | |
paulb@115 | 272 | assign.code = self.dispatches(assign.code) |
paulb@115 | 273 | return assign |
paulb@115 | 274 | |
paulb@150 | 275 | def visitCheckExc(self, checkexc): |
paulb@150 | 276 | |
paulb@150 | 277 | """ |
paulb@150 | 278 | Return the 'checkexc' node, processing the expression to find the |
paulb@150 | 279 | possible types of the exception, and processing each choice to build a |
paulb@150 | 280 | list of checked types for the exception. |
paulb@150 | 281 | """ |
paulb@150 | 282 | |
paulb@150 | 283 | checkexc.expr = self.dispatch(checkexc.expr) |
paulb@150 | 284 | expr_types = self.namespace.types |
paulb@150 | 285 | choice_types = [] |
paulb@150 | 286 | choices = [] |
paulb@150 | 287 | for choice in checkexc.choices: |
paulb@150 | 288 | choices.append(self.dispatch(choice)) |
paulb@150 | 289 | choice_types += self.namespace.types |
paulb@150 | 290 | for expr_type in expr_types: |
paulb@150 | 291 | if expr_type.type.get_class() not in choice_types: |
paulb@150 | 292 | self._prune_non_accesses(checkexc.expr, expr_type) |
paulb@150 | 293 | return checkexc |
paulb@150 | 294 | |
paulb@110 | 295 | def visitConditional(self, conditional): |
paulb@19 | 296 | |
paulb@150 | 297 | """ |
paulb@150 | 298 | Return the 'conditional' node, processing the test, body and else |
paulb@150 | 299 | clauses and recording their processed forms. The body and else clauses |
paulb@150 | 300 | are processed within their own namespaces, and the test is also |
paulb@150 | 301 | processed in its own namespace if 'isolate_test' is set on the |
paulb@150 | 302 | 'conditional' node. |
paulb@150 | 303 | """ |
paulb@150 | 304 | |
paulb@110 | 305 | # Conditionals keep local namespace changes isolated. |
paulb@110 | 306 | # With Return nodes inside the body/else sections, the changes are |
paulb@110 | 307 | # communicated to the caller. |
paulb@19 | 308 | |
paulb@133 | 309 | is_module = self.namespace is self.module.namespace |
paulb@133 | 310 | |
paulb@133 | 311 | # Where the test is closely associated with the body, save the namespace |
paulb@133 | 312 | # before entering the test. |
paulb@133 | 313 | |
paulb@133 | 314 | if conditional.isolate_test: |
paulb@133 | 315 | saved_namespace = self.namespace |
paulb@133 | 316 | self.namespace = Namespace() |
paulb@133 | 317 | if is_module: |
paulb@133 | 318 | self.module.namespace = self.namespace |
paulb@133 | 319 | self.namespace.merge_namespace(saved_namespace) |
paulb@19 | 320 | |
paulb@133 | 321 | conditional.test = self.dispatch(conditional.test) |
paulb@133 | 322 | |
paulb@133 | 323 | # Where the test may affect the body and the else clause, save the |
paulb@133 | 324 | # namespace after processing the test. |
paulb@133 | 325 | |
paulb@133 | 326 | if not conditional.isolate_test: |
paulb@133 | 327 | saved_namespace = self.namespace |
paulb@133 | 328 | self.namespace = Namespace() |
paulb@133 | 329 | if is_module: |
paulb@133 | 330 | self.module.namespace = self.namespace |
paulb@133 | 331 | self.namespace.merge_namespace(saved_namespace) |
paulb@133 | 332 | |
paulb@133 | 333 | # Process the body clause. |
paulb@133 | 334 | |
paulb@110 | 335 | conditional.body = self.dispatches(conditional.body) |
paulb@110 | 336 | body_namespace = self.namespace |
paulb@19 | 337 | |
paulb@133 | 338 | # Use the saved namespace as a template for the else clause. |
paulb@133 | 339 | |
paulb@110 | 340 | self.namespace = Namespace() |
paulb@133 | 341 | if is_module: |
paulb@133 | 342 | self.module.namespace = self.namespace |
paulb@110 | 343 | self.namespace.merge_namespace(saved_namespace) |
paulb@133 | 344 | |
paulb@133 | 345 | # Process the else clause. |
paulb@133 | 346 | |
paulb@110 | 347 | conditional.else_ = self.dispatches(conditional.else_) |
paulb@110 | 348 | else_namespace = self.namespace |
paulb@19 | 349 | |
paulb@133 | 350 | # Merge the body and else namespaces. |
paulb@133 | 351 | |
paulb@110 | 352 | self.namespace = Namespace() |
paulb@133 | 353 | if is_module: |
paulb@133 | 354 | self.module.namespace = self.namespace |
paulb@110 | 355 | self.namespace.merge_namespace(body_namespace) |
paulb@110 | 356 | self.namespace.merge_namespace(else_namespace) |
paulb@110 | 357 | |
paulb@110 | 358 | return conditional |
paulb@110 | 359 | |
paulb@151 | 360 | def _visitInvoke(self, invoke, invocation_types, have_args): |
paulb@151 | 361 | |
paulb@151 | 362 | """ |
paulb@151 | 363 | Return the processed 'invoke' node, using the given 'invocation_types' |
paulb@151 | 364 | as the list of callables to be investigated for instantiation or for the |
paulb@151 | 365 | invocation of functions or blocks. If 'have_args' is a true value, any |
paulb@151 | 366 | invocation or instantiation will involve arguments. |
paulb@151 | 367 | """ |
paulb@151 | 368 | |
paulb@151 | 369 | # Now locate and invoke the subprogram. This can be complicated because |
paulb@151 | 370 | # the target may be a class or object, and there may be many different |
paulb@151 | 371 | # related subprograms. |
paulb@151 | 372 | |
paulb@151 | 373 | invocations = [] |
paulb@151 | 374 | |
paulb@151 | 375 | # Visit each callable in turn, finding subprograms. |
paulb@151 | 376 | |
paulb@151 | 377 | for attr in invocation_types: |
paulb@151 | 378 | |
paulb@151 | 379 | # Deal with class invocations by providing instance objects. |
paulb@151 | 380 | # Here, each class is queried for the __init__ method, which may |
paulb@151 | 381 | # exist for some combinations of classes in a hierarchy but not for |
paulb@151 | 382 | # others. |
paulb@151 | 383 | |
paulb@151 | 384 | if isinstance(attr.type, Class): |
paulb@151 | 385 | attributes = get_attributes(attr.type, "__init__") |
paulb@151 | 386 | |
paulb@151 | 387 | # Deal with object invocations by using __call__ methods. |
paulb@151 | 388 | |
paulb@151 | 389 | elif isinstance(attr.type, Instance): |
paulb@151 | 390 | attributes = get_attributes(attr.type, "__call__") |
paulb@151 | 391 | |
paulb@151 | 392 | # Normal functions or methods are more straightforward. |
paulb@151 | 393 | # Here, we model them using an attribute with no context and with |
paulb@151 | 394 | # no associated accessor. |
paulb@151 | 395 | |
paulb@151 | 396 | else: |
paulb@151 | 397 | attributes = [(attr, None)] |
paulb@151 | 398 | |
paulb@151 | 399 | # Inspect each attribute and extract the subprogram. |
paulb@151 | 400 | |
paulb@151 | 401 | for attribute, accessor in attributes: |
paulb@151 | 402 | |
paulb@151 | 403 | # If a class is involved, presume that it must create a new |
paulb@151 | 404 | # object. |
paulb@151 | 405 | |
paulb@151 | 406 | if isinstance(attr.type, Class): |
paulb@151 | 407 | |
paulb@151 | 408 | # Instantiate the class. |
paulb@151 | 409 | # NOTE: Should probably only allocate a single instance. |
paulb@151 | 410 | |
paulb@151 | 411 | instance = self.new_instance(invoke, "new", attr.type.full_name(), attr.type) |
paulb@151 | 412 | |
paulb@151 | 413 | # For instantiations, switch the context. |
paulb@151 | 414 | |
paulb@151 | 415 | if attribute is not None: |
paulb@151 | 416 | attribute = Attribute(instance, attribute.type) |
paulb@151 | 417 | |
paulb@151 | 418 | # Skip cases where no callable is found. |
paulb@151 | 419 | |
paulb@151 | 420 | if attribute is not None: |
paulb@151 | 421 | |
paulb@151 | 422 | # If a subprogram is defined, invoke it. |
paulb@151 | 423 | |
paulb@151 | 424 | self.invoke_subprogram(invoke, attribute) |
paulb@151 | 425 | if attribute.type not in invocations: |
paulb@151 | 426 | invocations.append(attribute.type) |
paulb@151 | 427 | |
paulb@151 | 428 | elif not isinstance(attr.type, Class): |
paulb@151 | 429 | print "Invocation type is None for", accessor |
paulb@151 | 430 | |
paulb@151 | 431 | else: |
paulb@151 | 432 | |
paulb@151 | 433 | # Test to see if no arguments were supplied in cases where no |
paulb@151 | 434 | # initialiser was found. |
paulb@151 | 435 | |
paulb@151 | 436 | if have_args: |
paulb@151 | 437 | raise AnnotationMessage, "No initialiser found for '%s' with arguments." % attr.type |
paulb@151 | 438 | |
paulb@151 | 439 | # Special case: initialisation. |
paulb@151 | 440 | |
paulb@151 | 441 | if isinstance(attr.type, Class): |
paulb@151 | 442 | |
paulb@151 | 443 | # Associate the instance with the result of this invocation. |
paulb@151 | 444 | |
paulb@151 | 445 | self.namespace.set_types([Attribute(None, instance)]) |
paulb@151 | 446 | self.annotate(invoke) |
paulb@151 | 447 | |
paulb@151 | 448 | # Remember the invocations that were found, along with the return type |
paulb@151 | 449 | # information. |
paulb@151 | 450 | |
paulb@151 | 451 | invoke.invocations = invocations |
paulb@151 | 452 | self.namespace.set_types(getattr(invoke, "types", [])) |
paulb@151 | 453 | return invoke |
paulb@151 | 454 | |
paulb@151 | 455 | def visitInvokeBlock(self, invoke): |
paulb@151 | 456 | |
paulb@151 | 457 | """ |
paulb@151 | 458 | Return the processed 'invoke' node, first finding the callables |
paulb@151 | 459 | indicated by the expression. |
paulb@151 | 460 | """ |
paulb@151 | 461 | |
paulb@151 | 462 | invoke.expr = self.dispatch(invoke.expr) |
paulb@151 | 463 | invocation_types = self.namespace.types |
paulb@151 | 464 | return self._visitInvoke(invoke, invocation_types, have_args=0) |
paulb@151 | 465 | |
paulb@151 | 466 | def visitInvokeFunction(self, invoke): |
paulb@151 | 467 | |
paulb@151 | 468 | """ |
paulb@151 | 469 | Return the processed 'invoke' node, first finding the callables |
paulb@151 | 470 | indicated by the expression. |
paulb@151 | 471 | """ |
paulb@151 | 472 | |
paulb@151 | 473 | invoke.expr = self.dispatch(invoke.expr) |
paulb@151 | 474 | invocation_types = self.namespace.types |
paulb@151 | 475 | |
paulb@151 | 476 | # Invocation processing starts with making sure that the arguments have |
paulb@151 | 477 | # been processed. |
paulb@151 | 478 | |
paulb@151 | 479 | return self._visitInvoke(invoke, invocation_types, have_args=self.process_args(invoke)) |
paulb@151 | 480 | |
paulb@150 | 481 | def visitLoadAttr(self, loadattr): |
paulb@150 | 482 | |
paulb@150 | 483 | """ |
paulb@150 | 484 | Return the 'loadattr' node, processing and storing the expression, and |
paulb@150 | 485 | using the expression's types to construct records of accesses and |
paulb@150 | 486 | non-accesses using the stated attribute name. |
paulb@150 | 487 | """ |
paulb@150 | 488 | |
paulb@150 | 489 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@150 | 490 | types = [] |
paulb@150 | 491 | non_accesses = [] |
paulb@150 | 492 | accesses = {} |
paulb@150 | 493 | for attr in self.namespace.types: |
paulb@150 | 494 | attributes = get_attributes(attr.type, loadattr.name) |
paulb@150 | 495 | if not attributes: |
paulb@150 | 496 | if not attr.type in non_accesses: |
paulb@150 | 497 | non_accesses.append(attr) |
paulb@150 | 498 | |
paulb@150 | 499 | # Revoke this type from any name involved. |
paulb@150 | 500 | |
paulb@150 | 501 | self._prune_non_accesses(loadattr.expr, attr) |
paulb@150 | 502 | |
paulb@150 | 503 | for attribute, accessor in attributes: |
paulb@150 | 504 | if attribute is not None: |
paulb@150 | 505 | types.append(attribute) |
paulb@150 | 506 | if not accesses.has_key(attr.type): |
paulb@150 | 507 | accesses[attr.type] = [] |
paulb@150 | 508 | if not (attribute, accessor) in accesses[attr.type]: |
paulb@150 | 509 | accesses[attr.type].append((attribute, accessor)) |
paulb@150 | 510 | else: |
paulb@150 | 511 | if not attr in non_accesses: |
paulb@150 | 512 | non_accesses.append(attr) |
paulb@150 | 513 | |
paulb@150 | 514 | # Revoke this type from any name involved. |
paulb@150 | 515 | |
paulb@150 | 516 | self._prune_non_accesses(loadattr.expr, attr) |
paulb@150 | 517 | |
paulb@150 | 518 | if not types: |
paulb@150 | 519 | print "No attribute found for", loadattr.name, "given", self.namespace.types |
paulb@150 | 520 | self.namespace.set_types(types) |
paulb@150 | 521 | loadattr.non_accesses = non_accesses |
paulb@150 | 522 | loadattr.accesses = accesses |
paulb@150 | 523 | self.annotate(loadattr) |
paulb@150 | 524 | return loadattr |
paulb@150 | 525 | |
paulb@150 | 526 | def _prune_non_accesses(self, expr, attr): |
paulb@150 | 527 | |
paulb@150 | 528 | """ |
paulb@150 | 529 | Prune type information from 'expr' where the given 'attr' has been |
paulb@150 | 530 | shown to be a non-access. |
paulb@150 | 531 | """ |
paulb@150 | 532 | |
paulb@150 | 533 | if isinstance(expr, LoadName): |
paulb@150 | 534 | self.namespace.revoke(expr.name, attr) |
paulb@150 | 535 | elif isinstance(expr, LoadAttr): |
paulb@150 | 536 | for expr_attr in expr.expr.types: |
paulb@150 | 537 | if hasattr(expr_attr.type, "namespace"): |
paulb@150 | 538 | expr_attr.type.namespace.revoke(expr.name, attr) |
paulb@150 | 539 | elif isinstance(expr, LoadExc): |
paulb@150 | 540 | self.namespace.revoke_exception_type(attr) |
paulb@150 | 541 | |
paulb@150 | 542 | def visitLoadExc(self, loadexc): |
paulb@150 | 543 | |
paulb@150 | 544 | """ |
paulb@150 | 545 | Return the 'loadexc' node, discovering the possible exception types |
paulb@150 | 546 | raised. |
paulb@150 | 547 | """ |
paulb@150 | 548 | |
paulb@150 | 549 | self.namespace.types = self.namespace.raises[:] |
paulb@150 | 550 | self.annotate(loadexc) |
paulb@150 | 551 | return loadexc |
paulb@150 | 552 | |
paulb@150 | 553 | def visitLoadName(self, loadname): |
paulb@150 | 554 | |
paulb@150 | 555 | """ |
paulb@150 | 556 | Return the 'loadname' node, processing the name information on the node |
paulb@150 | 557 | to determine which types are involved with the name. |
paulb@150 | 558 | """ |
paulb@150 | 559 | |
paulb@150 | 560 | self.namespace.set_types(self.namespace.load(loadname.name)) |
paulb@150 | 561 | result = loadname |
paulb@150 | 562 | self.annotate(result) |
paulb@150 | 563 | return result |
paulb@150 | 564 | |
paulb@150 | 565 | def visitLoadRef(self, loadref): |
paulb@150 | 566 | |
paulb@150 | 567 | """ |
paulb@150 | 568 | Return the 'loadref' node, obtaining type information about the |
paulb@150 | 569 | reference stated on the node. |
paulb@150 | 570 | """ |
paulb@150 | 571 | |
paulb@150 | 572 | self.namespace.set_types([Attribute(None, loadref.ref)]) |
paulb@150 | 573 | self.annotate(loadref) |
paulb@150 | 574 | return loadref |
paulb@150 | 575 | |
paulb@150 | 576 | def visitLoadTemp(self, loadtemp): |
paulb@150 | 577 | |
paulb@150 | 578 | """ |
paulb@150 | 579 | Return the 'loadtemp' node, obtaining type information about the |
paulb@150 | 580 | temporary variable accessed, and removing variable information where the |
paulb@150 | 581 | 'release' attribute has been set on the node. |
paulb@150 | 582 | """ |
paulb@150 | 583 | |
paulb@150 | 584 | index = getattr(loadtemp, "index", None) |
paulb@150 | 585 | try: |
paulb@150 | 586 | if getattr(loadtemp, "release", 0): |
paulb@150 | 587 | self.namespace.set_types(self.namespace.temp[index].pop()) |
paulb@150 | 588 | else: |
paulb@150 | 589 | self.namespace.set_types(self.namespace.temp[index][-1]) |
paulb@150 | 590 | except KeyError: |
paulb@150 | 591 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@150 | 592 | self.annotate(loadtemp) |
paulb@150 | 593 | return loadtemp |
paulb@150 | 594 | |
paulb@115 | 595 | def visitModule(self, module): |
paulb@150 | 596 | |
paulb@150 | 597 | """ |
paulb@150 | 598 | Return the processed 'module' whose contents (merely a group of nodes) |
paulb@150 | 599 | are processed. |
paulb@150 | 600 | """ |
paulb@150 | 601 | |
paulb@115 | 602 | module.code = self.dispatches(module.code) |
paulb@115 | 603 | return module |
paulb@115 | 604 | |
paulb@150 | 605 | def visitNot(self, not_): |
paulb@150 | 606 | |
paulb@150 | 607 | "Return the 'not_' node whose expression is processed." |
paulb@150 | 608 | |
paulb@150 | 609 | not_.expr = self.dispatch(not_.expr) |
paulb@150 | 610 | return not_ |
paulb@150 | 611 | |
paulb@115 | 612 | def visitPass(self, pass_): |
paulb@150 | 613 | |
paulb@150 | 614 | "Return the unprocessed 'pass_' node." |
paulb@150 | 615 | |
paulb@115 | 616 | return pass_ |
paulb@115 | 617 | |
paulb@150 | 618 | def visitRaise(self, raise_): |
paulb@150 | 619 | |
paulb@150 | 620 | """ |
paulb@150 | 621 | Return the 'raise_' node, processing any traceback information along |
paulb@150 | 622 | with the raised exception expression, converting the node into a kind of |
paulb@150 | 623 | invocation where the expression is found not to be an invocation itself. |
paulb@150 | 624 | This node affects the namespace, adding exception types to the list of |
paulb@150 | 625 | those raised in the namespace. |
paulb@150 | 626 | """ |
paulb@150 | 627 | |
paulb@150 | 628 | if getattr(raise_, "traceback", None) is not None: |
paulb@150 | 629 | raise_.traceback = self.dispatch(raise_.traceback) |
paulb@150 | 630 | raise_.expr = self.dispatch(raise_.expr) |
paulb@150 | 631 | |
paulb@150 | 632 | # Handle bare name exceptions by converting any classes to instances. |
paulb@150 | 633 | |
paulb@150 | 634 | if not isinstance(raise_.expr, InvokeFunction): |
paulb@150 | 635 | raise_.pos_args = [] |
paulb@150 | 636 | raise_.kw_args = {} |
paulb@150 | 637 | raise_.star = None |
paulb@150 | 638 | raise_.dstar = None |
paulb@150 | 639 | types = [] |
paulb@150 | 640 | for attr in self.namespace.types: |
paulb@150 | 641 | if isinstance(attr.type, Class): |
paulb@150 | 642 | self._visitInvoke(raise_, [attr], have_args=0) |
paulb@150 | 643 | types += self.namespace.types |
paulb@150 | 644 | else: |
paulb@150 | 645 | types = self.namespace.types |
paulb@150 | 646 | |
paulb@150 | 647 | combine(self.namespace.raises, types) |
paulb@150 | 648 | return raise_ |
paulb@150 | 649 | |
paulb@150 | 650 | def visitReleaseTemp(self, releasetemp): |
paulb@150 | 651 | |
paulb@150 | 652 | """ |
paulb@150 | 653 | Return the 'releasetemp' node, removing temporary variable information |
paulb@150 | 654 | from the current namespace. |
paulb@150 | 655 | """ |
paulb@150 | 656 | |
paulb@150 | 657 | index = getattr(releasetemp, "index", None) |
paulb@150 | 658 | try: |
paulb@150 | 659 | self.namespace.temp[index].pop() |
paulb@150 | 660 | except KeyError: |
paulb@150 | 661 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@150 | 662 | except IndexError: |
paulb@150 | 663 | pass #raise AnnotationMessage, "Temporary store index '%s' is empty." % index |
paulb@150 | 664 | return releasetemp |
paulb@150 | 665 | |
paulb@150 | 666 | def visitReturn(self, return_): |
paulb@150 | 667 | |
paulb@150 | 668 | """ |
paulb@150 | 669 | Return the 'return_' node, processing any expression and obtaining type |
paulb@150 | 670 | information to be accumulated in the current namespace's list of return |
paulb@150 | 671 | types. A snapshot of the namespace is taken for the purposes of |
paulb@150 | 672 | reconciling or merging namespaces where subprograms actually share |
paulb@150 | 673 | locals with their callers. |
paulb@150 | 674 | """ |
paulb@150 | 675 | |
paulb@150 | 676 | if hasattr(return_, "expr"): |
paulb@150 | 677 | return_.expr = self.dispatch(return_.expr) |
paulb@150 | 678 | combine(self.namespace.returns, self.namespace.types) |
paulb@150 | 679 | self.annotate(return_) |
paulb@150 | 680 | self.namespace.snapshot() |
paulb@150 | 681 | return return_ |
paulb@150 | 682 | |
paulb@150 | 683 | visitReturnFromBlock = visitReturn |
paulb@150 | 684 | visitReturnFromFunction = visitReturn |
paulb@150 | 685 | |
paulb@150 | 686 | def visitStoreAttr(self, storeattr): |
paulb@150 | 687 | |
paulb@150 | 688 | """ |
paulb@150 | 689 | Return the 'storeattr' node, processing the expression and target, and |
paulb@150 | 690 | using the type information obtained to build records of legitimate |
paulb@150 | 691 | writes to the stated attribute, along with "impossible" non-writes to |
paulb@150 | 692 | the attribute. |
paulb@150 | 693 | """ |
paulb@150 | 694 | |
paulb@150 | 695 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@150 | 696 | expr = self.namespace.types |
paulb@150 | 697 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@150 | 698 | writes = {} |
paulb@150 | 699 | non_writes = [] |
paulb@150 | 700 | for attr in self.namespace.types: |
paulb@154 | 701 | # NOTE: Impose "atomic" constraints on certain types. |
paulb@150 | 702 | if attr is None: |
paulb@150 | 703 | if not attr in non_writes: |
paulb@150 | 704 | non_writes.append(attr) |
paulb@150 | 705 | continue |
paulb@150 | 706 | attr.type.namespace.add(storeattr.name, expr) |
paulb@150 | 707 | writes[attr.type] = attr.type.namespace.load(storeattr.name) |
paulb@150 | 708 | if not writes: |
paulb@150 | 709 | print "Unable to store attribute", storeattr.name, "given", self.namespace.types |
paulb@150 | 710 | storeattr.writes = writes |
paulb@150 | 711 | storeattr.non_writes = non_writes |
paulb@150 | 712 | return storeattr |
paulb@150 | 713 | |
paulb@150 | 714 | def visitStoreName(self, storename): |
paulb@150 | 715 | |
paulb@150 | 716 | """ |
paulb@150 | 717 | Return the 'storename' node, processing the expression on the node and |
paulb@150 | 718 | associating the type information obtained with the stated name in the |
paulb@150 | 719 | current namespace. |
paulb@150 | 720 | """ |
paulb@150 | 721 | |
paulb@150 | 722 | storename.expr = self.dispatch(storename.expr) |
paulb@150 | 723 | self.namespace.store(storename.name, self.namespace.types) |
paulb@150 | 724 | return storename |
paulb@150 | 725 | |
paulb@150 | 726 | def visitStoreTemp(self, storetemp): |
paulb@150 | 727 | |
paulb@150 | 728 | """ |
paulb@150 | 729 | Return the 'storetemp' node, processing the expression on the node and |
paulb@150 | 730 | associating the type information obtained with a temporary variable in |
paulb@150 | 731 | the current namespace. |
paulb@150 | 732 | """ |
paulb@150 | 733 | |
paulb@150 | 734 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@150 | 735 | index = getattr(storetemp, "index", None) |
paulb@150 | 736 | if not self.namespace.temp.has_key(index): |
paulb@150 | 737 | self.namespace.temp[index] = [] |
paulb@150 | 738 | self.namespace.temp[index].append(self.namespace.types) |
paulb@150 | 739 | return storetemp |
paulb@150 | 740 | |
paulb@115 | 741 | def visitSubprogram(self, subprogram): |
paulb@150 | 742 | |
paulb@150 | 743 | """ |
paulb@150 | 744 | Return the 'subprogram' node, processing its contents (a group of nodes |
paulb@150 | 745 | comprising the subprogram). |
paulb@150 | 746 | """ |
paulb@150 | 747 | |
paulb@115 | 748 | subprogram.code = self.dispatches(subprogram.code) |
paulb@115 | 749 | return subprogram |
paulb@115 | 750 | |
paulb@115 | 751 | def visitTry(self, try_): |
paulb@150 | 752 | |
paulb@150 | 753 | """ |
paulb@150 | 754 | Return the 'try_' node, processing the body clause in its own namespace |
paulb@150 | 755 | derived from the current namespace, processing any handler clause using |
paulb@150 | 756 | the namespace information accumulated in the body, and processing any |
paulb@150 | 757 | else and finally clauses, attempting to supply each with appropriate |
paulb@150 | 758 | namespace information. |
paulb@150 | 759 | """ |
paulb@150 | 760 | |
paulb@133 | 761 | is_module = self.namespace is self.module.namespace |
paulb@133 | 762 | |
paulb@115 | 763 | try_.body = self.dispatches(try_.body) |
paulb@133 | 764 | |
paulb@133 | 765 | # Save the namespace from the body. |
paulb@133 | 766 | |
paulb@133 | 767 | body_namespace = Namespace() |
paulb@133 | 768 | body_namespace.merge_namespace(self.namespace) |
paulb@133 | 769 | |
paulb@133 | 770 | # Process the handler. |
paulb@133 | 771 | |
paulb@133 | 772 | if hasattr(try_, "handler"): |
paulb@133 | 773 | try_.handler = self.dispatches(try_.handler) |
paulb@133 | 774 | |
paulb@133 | 775 | # Save the namespace from the handler. |
paulb@133 | 776 | |
paulb@133 | 777 | handler_namespace = Namespace() |
paulb@133 | 778 | handler_namespace.merge_namespace(self.namespace) |
paulb@133 | 779 | |
paulb@133 | 780 | # Remember the raised exceptions encountered so far. |
paulb@133 | 781 | |
paulb@121 | 782 | raises = self.namespace.raises |
paulb@121 | 783 | |
paulb@133 | 784 | # Process the else clause. |
paulb@133 | 785 | |
paulb@133 | 786 | if hasattr(try_, "else_"): |
paulb@133 | 787 | |
paulb@133 | 788 | # Restore the body namespace for the else clause. |
paulb@133 | 789 | |
paulb@133 | 790 | self.namespace = body_namespace |
paulb@133 | 791 | if is_module: |
paulb@133 | 792 | self.module.namespace = self.namespace |
paulb@133 | 793 | |
paulb@133 | 794 | # Empty the raised exceptions for the else clause. |
paulb@121 | 795 | |
paulb@133 | 796 | self.namespace.raises = [] |
paulb@133 | 797 | try_.else_ = self.dispatches(try_.else_) |
paulb@133 | 798 | self.namespace.raises = raises |
paulb@133 | 799 | |
paulb@133 | 800 | # Merge the namespaces. |
paulb@133 | 801 | |
paulb@133 | 802 | self.namespace = Namespace() |
paulb@133 | 803 | if is_module: |
paulb@133 | 804 | self.module.namespace = self.namespace |
paulb@133 | 805 | self.namespace.merge_namespace(body_namespace) |
paulb@133 | 806 | self.namespace.merge_namespace(handler_namespace) |
paulb@133 | 807 | |
paulb@133 | 808 | # Process the finally clause, if any. |
paulb@121 | 809 | |
paulb@115 | 810 | try_.finally_ = self.dispatches(try_.finally_) |
paulb@115 | 811 | return try_ |
paulb@115 | 812 | |
paulb@25 | 813 | # Utility methods. |
paulb@25 | 814 | |
paulb@79 | 815 | def new_instance(self, node, reason, target, type): |
paulb@79 | 816 | |
paulb@79 | 817 | "Create, on the given 'node', a new instance with the given 'type'." |
paulb@79 | 818 | |
paulb@79 | 819 | if not hasattr(node, "instances"): |
paulb@79 | 820 | node.instances = {} |
paulb@79 | 821 | |
paulb@79 | 822 | if not node.instances.has_key((reason, target, type)): |
paulb@91 | 823 | |
paulb@91 | 824 | # Insist on a single instance per type. |
paulb@91 | 825 | # NOTE: Strategy-dependent instantiation. |
paulb@91 | 826 | |
paulb@91 | 827 | if len(type.instances) == 0: |
paulb@91 | 828 | instance = Instance() |
paulb@91 | 829 | instance.namespace = Namespace() |
paulb@91 | 830 | instance.namespace.store("__class__", [Attribute(None, type)]) |
paulb@91 | 831 | type.instances.append(instance) |
paulb@91 | 832 | else: |
paulb@91 | 833 | instance = type.instances[0] |
paulb@91 | 834 | |
paulb@79 | 835 | node.instances[(reason, target, type)] = instance |
paulb@79 | 836 | |
paulb@79 | 837 | return node.instances[(reason, target, type)] |
paulb@79 | 838 | |
paulb@151 | 839 | def invoke_subprogram(self, invoke, attribute): |
paulb@34 | 840 | |
paulb@151 | 841 | """ |
paulb@151 | 842 | Invoke using the given 'invoke' node the subprogram represented by the |
paulb@151 | 843 | given 'attribute'. |
paulb@151 | 844 | """ |
paulb@34 | 845 | |
paulb@54 | 846 | # Test for context information, making it into a real attribute. |
paulb@34 | 847 | |
paulb@151 | 848 | if attribute.context is not None: |
paulb@151 | 849 | context = Attribute(None, attribute.context) |
paulb@151 | 850 | target = attribute.type |
paulb@34 | 851 | else: |
paulb@34 | 852 | context = None |
paulb@151 | 853 | target = attribute.type |
paulb@50 | 854 | |
paulb@154 | 855 | # Test to see if anything has changed. |
paulb@154 | 856 | |
paulb@154 | 857 | if hasattr(invoke, "syscount") and invoke.syscount == self.system.count: |
paulb@154 | 858 | return |
paulb@154 | 859 | |
paulb@154 | 860 | # Remember the state of the system. |
paulb@154 | 861 | |
paulb@154 | 862 | else: |
paulb@154 | 863 | invoke.syscount = self.system.count |
paulb@154 | 864 | |
paulb@34 | 865 | # Provide the correct namespace for the invocation. |
paulb@34 | 866 | |
paulb@112 | 867 | if getattr(invoke, "share_locals", 0): |
paulb@34 | 868 | namespace = Namespace() |
paulb@134 | 869 | namespace.merge_namespace(self.namespace, everything=0) |
paulb@132 | 870 | using_module_namespace = self.namespace is self.module.namespace |
paulb@112 | 871 | elif getattr(target, "structure", None): |
paulb@112 | 872 | namespace = Namespace() |
paulb@132 | 873 | using_module_namespace = 0 |
paulb@34 | 874 | else: |
paulb@34 | 875 | items = self.make_items(invoke, target, context) |
paulb@150 | 876 | namespace = Namespace() |
paulb@150 | 877 | namespace.merge_items(items) |
paulb@132 | 878 | using_module_namespace = 0 |
paulb@34 | 879 | |
paulb@34 | 880 | # Process the subprogram. |
paulb@132 | 881 | # In order to keep global accesses working, the module namespace must be |
paulb@132 | 882 | # adjusted. |
paulb@132 | 883 | |
paulb@132 | 884 | if using_module_namespace: |
paulb@132 | 885 | self.module.namespace = namespace |
paulb@34 | 886 | |
paulb@53 | 887 | self.process_node(target, namespace) |
paulb@34 | 888 | |
paulb@45 | 889 | # NOTE: Improve and verify this. |
paulb@91 | 890 | # If the invocation returns a value, acquire the return types. |
paulb@34 | 891 | |
paulb@53 | 892 | if getattr(target, "returns_value", 0): |
paulb@49 | 893 | self.namespace.set_types(self.last_returns) |
paulb@34 | 894 | self.annotate(invoke) |
paulb@34 | 895 | |
paulb@132 | 896 | # If it is a normal block, merge the locals. |
paulb@132 | 897 | # This can happen in addition to the above because for things like |
paulb@132 | 898 | # logical expressions, the namespace can be modified whilst values are |
paulb@132 | 899 | # returned as results. |
paulb@91 | 900 | |
paulb@132 | 901 | if getattr(invoke, "share_locals", 0): |
paulb@132 | 902 | self.namespace.reset() |
paulb@135 | 903 | |
paulb@135 | 904 | # Merge the locals snapshots. |
paulb@135 | 905 | |
paulb@45 | 906 | for locals in self.returned_locals: |
paulb@135 | 907 | |
paulb@135 | 908 | # For blocks returning values (such as operations), do not merge |
paulb@135 | 909 | # snapshots or results. |
paulb@135 | 910 | |
paulb@135 | 911 | if getattr(target, "returns_value", 0): |
paulb@135 | 912 | self.namespace.merge_namespace(locals, everything=0) |
paulb@135 | 913 | |
paulb@135 | 914 | # For blocks not returning values (such as loops), merge |
paulb@135 | 915 | # snapshots and results since they contain details of genuine |
paulb@135 | 916 | # returns. |
paulb@135 | 917 | |
paulb@135 | 918 | else: |
paulb@135 | 919 | self.namespace.merge_namespace(locals) |
paulb@34 | 920 | |
paulb@116 | 921 | # Incorporate any raised exceptions. |
paulb@116 | 922 | |
paulb@116 | 923 | combine(self.namespace.raises, self.last_raises) |
paulb@116 | 924 | |
paulb@132 | 925 | # In order to keep global accesses working, the module namespace must be |
paulb@132 | 926 | # adjusted. |
paulb@132 | 927 | |
paulb@132 | 928 | if using_module_namespace: |
paulb@132 | 929 | self.module.namespace = self.namespace |
paulb@132 | 930 | |
paulb@83 | 931 | def process_args(self, invocation): |
paulb@83 | 932 | |
paulb@112 | 933 | """ |
paulb@112 | 934 | Process the arguments associated with an 'invocation'. Return whether |
paulb@112 | 935 | any arguments were processed. |
paulb@112 | 936 | """ |
paulb@54 | 937 | |
paulb@93 | 938 | invocation.pos_args = self.dispatches(invocation.pos_args) |
paulb@93 | 939 | invocation.kw_args = self.dispatch_dict(invocation.kw_args) |
paulb@54 | 940 | |
paulb@54 | 941 | # Get type information for star and dstar arguments. |
paulb@54 | 942 | |
paulb@83 | 943 | if invocation.star is not None: |
paulb@83 | 944 | param, default = invocation.star |
paulb@54 | 945 | default = self.dispatch(default) |
paulb@83 | 946 | invocation.star = param, default |
paulb@54 | 947 | |
paulb@83 | 948 | if invocation.dstar is not None: |
paulb@83 | 949 | param, default = invocation.dstar |
paulb@54 | 950 | default = self.dispatch(default) |
paulb@83 | 951 | invocation.dstar = param, default |
paulb@54 | 952 | |
paulb@112 | 953 | if invocation.pos_args or invocation.kw_args or invocation.star or invocation.dstar: |
paulb@112 | 954 | return 1 |
paulb@112 | 955 | else: |
paulb@112 | 956 | return 0 |
paulb@112 | 957 | |
paulb@34 | 958 | def make_items(self, invocation, subprogram, context): |
paulb@34 | 959 | |
paulb@34 | 960 | """ |
paulb@34 | 961 | Make an items mapping for the 'invocation' of the 'subprogram' using the |
paulb@34 | 962 | given 'context' (which may be None). |
paulb@34 | 963 | """ |
paulb@34 | 964 | |
paulb@34 | 965 | if context is not None: |
paulb@93 | 966 | pos_args = [Self(context)] + invocation.pos_args |
paulb@34 | 967 | else: |
paulb@93 | 968 | pos_args = invocation.pos_args |
paulb@154 | 969 | |
paulb@154 | 970 | # Duplicate the keyword arguments - we remove them in processing below. |
paulb@154 | 971 | |
paulb@154 | 972 | kw_args = {} |
paulb@154 | 973 | kw_args.update(invocation.kw_args) |
paulb@34 | 974 | |
paulb@67 | 975 | # Sort the arguments into positional and keyword arguments. |
paulb@67 | 976 | |
paulb@24 | 977 | params = subprogram.params |
paulb@24 | 978 | items = [] |
paulb@67 | 979 | star_args = [] |
paulb@24 | 980 | |
paulb@67 | 981 | # Match each positional argument, taking excess arguments as star args. |
paulb@67 | 982 | |
paulb@67 | 983 | for arg in pos_args: |
paulb@67 | 984 | if params: |
paulb@24 | 985 | param, default = params[0] |
paulb@24 | 986 | if arg is None: |
paulb@54 | 987 | arg = default |
paulb@140 | 988 | if hasattr(arg, "types"): |
paulb@140 | 989 | items.append((param, arg.types)) |
paulb@140 | 990 | else: |
paulb@140 | 991 | items.append((param, [])) # Annotation has not succeeded. |
paulb@67 | 992 | params = params[1:] |
paulb@24 | 993 | else: |
paulb@67 | 994 | star_args.append(arg) |
paulb@24 | 995 | |
paulb@24 | 996 | # Collect the remaining defaults. |
paulb@24 | 997 | |
paulb@24 | 998 | while params: |
paulb@24 | 999 | param, default = params[0] |
paulb@67 | 1000 | if kw_args.has_key(param): |
paulb@67 | 1001 | arg = kw_args[param] |
paulb@73 | 1002 | del kw_args[param] |
paulb@93 | 1003 | elif default is not None: |
paulb@93 | 1004 | arg = self.dispatch(default) |
paulb@93 | 1005 | else: |
paulb@67 | 1006 | raise AnnotationMessage, "No argument supplied in '%s' for parameter '%s'." % (subprogram, param) |
paulb@140 | 1007 | if hasattr(arg, "types"): |
paulb@140 | 1008 | items.append((param, arg.types)) |
paulb@140 | 1009 | else: |
paulb@140 | 1010 | items.append((param, [])) # Annotation has not succeeded. |
paulb@24 | 1011 | params = params[1:] |
paulb@24 | 1012 | |
paulb@93 | 1013 | dstar_args = kw_args.values() |
paulb@73 | 1014 | |
paulb@73 | 1015 | # Construct temporary objects. |
paulb@73 | 1016 | |
paulb@73 | 1017 | if star_args: |
paulb@80 | 1018 | star_invocation = self.make_star_args(invocation, subprogram, star_args) |
paulb@80 | 1019 | self.dispatch(star_invocation) |
paulb@80 | 1020 | star_types = star_invocation.types |
paulb@73 | 1021 | else: |
paulb@73 | 1022 | star_types = None |
paulb@73 | 1023 | |
paulb@73 | 1024 | if dstar_args: |
paulb@83 | 1025 | dstar_invocation = self.make_dstar_args(invocation, subprogram, dstar_args) # NOTE: To be written! |
paulb@80 | 1026 | self.dispatch(dstar_invocation) |
paulb@80 | 1027 | dstar_types = dstar_invocation.types |
paulb@73 | 1028 | else: |
paulb@73 | 1029 | dstar_types = None |
paulb@73 | 1030 | |
paulb@73 | 1031 | # NOTE: Merge the objects properly. |
paulb@73 | 1032 | |
paulb@73 | 1033 | star_types = star_types or invocation.star and invocation.star.types |
paulb@73 | 1034 | dstar_types = dstar_types or invocation.dstar and invocation.dstar.types |
paulb@73 | 1035 | |
paulb@24 | 1036 | # Add star and dstar. |
paulb@24 | 1037 | |
paulb@73 | 1038 | if star_types is not None: |
paulb@24 | 1039 | if subprogram.star is not None: |
paulb@24 | 1040 | param, default = subprogram.star |
paulb@73 | 1041 | items.append((param, star_types)) |
paulb@24 | 1042 | else: |
paulb@67 | 1043 | raise AnnotationMessage, "Invocation provides unwanted *args." |
paulb@24 | 1044 | elif subprogram.star is not None: |
paulb@24 | 1045 | param, default = subprogram.star |
paulb@154 | 1046 | if not hasattr(default, "types"): |
paulb@154 | 1047 | subprogram.star = param, self.dispatch(default) # NOTE: Review reprocessing. |
paulb@154 | 1048 | items.append((param, default.types)) |
paulb@24 | 1049 | |
paulb@73 | 1050 | if dstar_types is not None: |
paulb@24 | 1051 | if subprogram.dstar is not None: |
paulb@24 | 1052 | param, default = subprogram.dstar |
paulb@73 | 1053 | items.append((param, dstar_types)) |
paulb@24 | 1054 | else: |
paulb@67 | 1055 | raise AnnotationMessage, "Invocation provides unwanted **args." |
paulb@24 | 1056 | elif subprogram.dstar is not None: |
paulb@24 | 1057 | param, default = subprogram.dstar |
paulb@154 | 1058 | if not hasattr(default, "types"): |
paulb@154 | 1059 | subprogram.dstar = param, self.dispatch(default) # NOTE: Review reprocessing. |
paulb@154 | 1060 | items.append((param, default.types)) |
paulb@24 | 1061 | |
paulb@83 | 1062 | # Record the parameter types. |
paulb@83 | 1063 | |
paulb@146 | 1064 | self.annotate_parameters(subprogram, items) |
paulb@146 | 1065 | return subprogram.paramtypes.items() |
paulb@24 | 1066 | |
paulb@80 | 1067 | def make_star_args(self, invocation, subprogram, star_args): |
paulb@80 | 1068 | |
paulb@80 | 1069 | "Make a subprogram which initialises a list containing 'star_args'." |
paulb@80 | 1070 | |
paulb@80 | 1071 | if not hasattr(invocation, "stars"): |
paulb@80 | 1072 | invocation.stars = {} |
paulb@80 | 1073 | |
paulb@80 | 1074 | if not invocation.stars.has_key(subprogram.full_name()): |
paulb@80 | 1075 | code=[ |
paulb@80 | 1076 | StoreTemp( |
paulb@80 | 1077 | expr=InvokeFunction( |
paulb@80 | 1078 | expr=LoadAttr( |
paulb@80 | 1079 | expr=LoadRef( |
paulb@80 | 1080 | ref=self.builtins |
paulb@80 | 1081 | ), |
paulb@80 | 1082 | name="list", |
paulb@80 | 1083 | nstype="module", |
paulb@80 | 1084 | ), |
paulb@80 | 1085 | args=[], |
paulb@80 | 1086 | star=None, |
paulb@80 | 1087 | dstar=None |
paulb@80 | 1088 | ) |
paulb@80 | 1089 | ) |
paulb@80 | 1090 | ] |
paulb@80 | 1091 | |
paulb@80 | 1092 | for arg in star_args: |
paulb@80 | 1093 | code.append( |
paulb@80 | 1094 | InvokeFunction( |
paulb@80 | 1095 | expr=LoadAttr( |
paulb@80 | 1096 | expr=LoadTemp(), |
paulb@80 | 1097 | name="append" |
paulb@80 | 1098 | ), |
paulb@80 | 1099 | args=[arg], |
paulb@80 | 1100 | star=None, |
paulb@80 | 1101 | dstar=None |
paulb@80 | 1102 | ) |
paulb@80 | 1103 | ) |
paulb@80 | 1104 | |
paulb@80 | 1105 | code += [ |
paulb@80 | 1106 | Return(expr=LoadTemp(release=1)) |
paulb@80 | 1107 | ] |
paulb@80 | 1108 | |
paulb@80 | 1109 | invocation.stars[subprogram.full_name()] = InvokeBlock( |
paulb@80 | 1110 | produces_result=1, |
paulb@80 | 1111 | expr=LoadRef( |
paulb@80 | 1112 | ref=Subprogram( |
paulb@80 | 1113 | name=None, |
paulb@80 | 1114 | returns_value=1, |
paulb@80 | 1115 | params=[], |
paulb@80 | 1116 | star=None, |
paulb@80 | 1117 | dstar=None, |
paulb@80 | 1118 | code=code |
paulb@80 | 1119 | ) |
paulb@80 | 1120 | ) |
paulb@80 | 1121 | ) |
paulb@80 | 1122 | |
paulb@80 | 1123 | return invocation.stars[subprogram.full_name()] |
paulb@80 | 1124 | |
paulb@48 | 1125 | # Namespace-related abstractions. |
paulb@48 | 1126 | |
paulb@48 | 1127 | class Namespace: |
paulb@48 | 1128 | |
paulb@48 | 1129 | """ |
paulb@48 | 1130 | A local namespace which may either relate to a genuine set of function |
paulb@50 | 1131 | locals or the initialisation of a structure or module. |
paulb@48 | 1132 | """ |
paulb@48 | 1133 | |
paulb@48 | 1134 | def __init__(self): |
paulb@50 | 1135 | |
paulb@50 | 1136 | """ |
paulb@50 | 1137 | Initialise the namespace with a mapping of local names to possible |
paulb@50 | 1138 | types, a list of return values and of possible returned local |
paulb@50 | 1139 | namespaces. The namespace also tracks the "current" types and a mapping |
paulb@50 | 1140 | of temporary value names to types. |
paulb@50 | 1141 | """ |
paulb@50 | 1142 | |
paulb@48 | 1143 | self.names = {} |
paulb@49 | 1144 | self.returns = [] |
paulb@49 | 1145 | self.return_locals = [] |
paulb@112 | 1146 | self.raises = [] |
paulb@49 | 1147 | self.temp = {} |
paulb@49 | 1148 | self.types = [] |
paulb@49 | 1149 | |
paulb@49 | 1150 | def set_types(self, types): |
paulb@49 | 1151 | self.types = types |
paulb@48 | 1152 | |
paulb@140 | 1153 | def add(self, name, types): |
paulb@140 | 1154 | if self.names.has_key(name): |
paulb@140 | 1155 | combine(self.names[name], types) |
paulb@140 | 1156 | else: |
paulb@140 | 1157 | self.store(name, types) |
paulb@140 | 1158 | |
paulb@48 | 1159 | def store(self, name, types): |
paulb@48 | 1160 | self.names[name] = types |
paulb@48 | 1161 | |
paulb@86 | 1162 | __setitem__ = store |
paulb@74 | 1163 | |
paulb@48 | 1164 | def load(self, name): |
paulb@48 | 1165 | return self.names[name] |
paulb@48 | 1166 | |
paulb@86 | 1167 | __getitem__ = load |
paulb@74 | 1168 | |
paulb@131 | 1169 | def revoke(self, name, type): |
paulb@131 | 1170 | self.names[name].remove(type) |
paulb@131 | 1171 | |
paulb@133 | 1172 | def revoke_exception_type(self, type): |
paulb@133 | 1173 | self.raises.remove(type) |
paulb@133 | 1174 | |
paulb@134 | 1175 | def merge_namespace(self, namespace, everything=1): |
paulb@68 | 1176 | self.merge_items(namespace.names.items()) |
paulb@134 | 1177 | if everything: |
paulb@134 | 1178 | combine(self.returns, namespace.returns) |
paulb@132 | 1179 | combine(self.return_locals, namespace.return_locals) |
paulb@115 | 1180 | combine(self.raises, namespace.raises) |
paulb@134 | 1181 | for name, values in namespace.temp.items(): |
paulb@134 | 1182 | if values: |
paulb@134 | 1183 | if not self.temp.has_key(name) or not self.temp[name]: |
paulb@134 | 1184 | self.temp[name] = [[]] |
paulb@134 | 1185 | combine(self.temp[name][-1], values[-1]) |
paulb@68 | 1186 | |
paulb@68 | 1187 | def merge_items(self, items): |
paulb@68 | 1188 | for name, types in items: |
paulb@68 | 1189 | self.merge(name, types) |
paulb@68 | 1190 | |
paulb@48 | 1191 | def merge(self, name, types): |
paulb@48 | 1192 | if not self.names.has_key(name): |
paulb@48 | 1193 | self.names[name] = types[:] |
paulb@48 | 1194 | else: |
paulb@48 | 1195 | existing = self.names[name] |
paulb@73 | 1196 | combine(existing, types) |
paulb@48 | 1197 | |
paulb@49 | 1198 | def snapshot(self): |
paulb@49 | 1199 | |
paulb@49 | 1200 | "Make a snapshot of the locals and remember them." |
paulb@49 | 1201 | |
paulb@49 | 1202 | namespace = Namespace() |
paulb@49 | 1203 | namespace.merge_namespace(self) |
paulb@49 | 1204 | self.return_locals.append(namespace) |
paulb@49 | 1205 | |
paulb@132 | 1206 | def reset(self): |
paulb@132 | 1207 | |
paulb@132 | 1208 | "Reset a namespace in preparation for merging with returned locals." |
paulb@132 | 1209 | |
paulb@132 | 1210 | self.names = {} |
paulb@132 | 1211 | |
paulb@48 | 1212 | def __repr__(self): |
paulb@48 | 1213 | return repr(self.names) |
paulb@48 | 1214 | |
paulb@48 | 1215 | class Attribute: |
paulb@48 | 1216 | |
paulb@48 | 1217 | """ |
paulb@48 | 1218 | An attribute abstraction, indicating the type of the attribute along with |
paulb@48 | 1219 | its context or origin. |
paulb@48 | 1220 | """ |
paulb@48 | 1221 | |
paulb@48 | 1222 | def __init__(self, context, type): |
paulb@48 | 1223 | self.context = context |
paulb@48 | 1224 | self.type = type |
paulb@48 | 1225 | |
paulb@48 | 1226 | def __eq__(self, other): |
paulb@48 | 1227 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@48 | 1228 | |
paulb@48 | 1229 | def __repr__(self): |
paulb@54 | 1230 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@48 | 1231 | |
paulb@53 | 1232 | class Self: |
paulb@91 | 1233 | |
paulb@116 | 1234 | """ |
paulb@116 | 1235 | A program node encapsulating object/context information in an argument list. |
paulb@116 | 1236 | This is not particularly like Attribute, Class, Instance or other such |
paulb@116 | 1237 | things, since it actually appears in the program representation. |
paulb@116 | 1238 | """ |
paulb@91 | 1239 | |
paulb@53 | 1240 | def __init__(self, attribute): |
paulb@53 | 1241 | self.types = [attribute] |
paulb@53 | 1242 | |
paulb@73 | 1243 | def combine(target, additions): |
paulb@91 | 1244 | |
paulb@91 | 1245 | """ |
paulb@91 | 1246 | Merge into the 'target' sequence the given 'additions', preventing duplicate |
paulb@91 | 1247 | items. |
paulb@91 | 1248 | """ |
paulb@91 | 1249 | |
paulb@73 | 1250 | for addition in additions: |
paulb@73 | 1251 | if addition not in target: |
paulb@73 | 1252 | target.append(addition) |
paulb@73 | 1253 | |
paulb@48 | 1254 | def find_attributes(structure, name): |
paulb@48 | 1255 | |
paulb@48 | 1256 | """ |
paulb@48 | 1257 | Find for the given 'structure' all attributes for the given 'name', visiting |
paulb@48 | 1258 | base classes where appropriate and returning the attributes in order of |
paulb@48 | 1259 | descending precedence for all possible base classes. |
paulb@48 | 1260 | |
paulb@48 | 1261 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1262 | the structure involved in accessing the attribute. |
paulb@48 | 1263 | """ |
paulb@48 | 1264 | |
paulb@48 | 1265 | # First attempt to search the instance/class namespace. |
paulb@48 | 1266 | |
paulb@48 | 1267 | try: |
paulb@48 | 1268 | l = structure.namespace.load(name) |
paulb@48 | 1269 | attributes = [] |
paulb@48 | 1270 | for attribute in l: |
paulb@48 | 1271 | attributes.append((attribute, structure)) |
paulb@48 | 1272 | |
paulb@48 | 1273 | # If that does not work, attempt to investigate any class or base classes. |
paulb@48 | 1274 | |
paulb@48 | 1275 | except KeyError: |
paulb@48 | 1276 | attributes = [] |
paulb@48 | 1277 | |
paulb@48 | 1278 | # Investigate any instance's implementing class. |
paulb@48 | 1279 | |
paulb@48 | 1280 | if isinstance(structure, Instance): |
paulb@73 | 1281 | for attr in structure.namespace.load("__class__"): |
paulb@73 | 1282 | cls = attr.type |
paulb@50 | 1283 | l = get_attributes(cls, name) |
paulb@73 | 1284 | combine(attributes, l) |
paulb@48 | 1285 | |
paulb@48 | 1286 | # Investigate any class's base classes. |
paulb@48 | 1287 | |
paulb@48 | 1288 | elif isinstance(structure, Class): |
paulb@48 | 1289 | |
paulb@48 | 1290 | # If no base classes exist, return an indicator that no attribute |
paulb@48 | 1291 | # exists. |
paulb@48 | 1292 | |
paulb@48 | 1293 | if not structure.base_refs: |
paulb@48 | 1294 | return [(None, structure)] |
paulb@48 | 1295 | |
paulb@48 | 1296 | # Otherwise, find all possible base classes. |
paulb@48 | 1297 | |
paulb@48 | 1298 | for base_refs in structure.base_refs: |
paulb@48 | 1299 | base_attributes = [] |
paulb@48 | 1300 | |
paulb@48 | 1301 | # For each base class, find attributes either in the base |
paulb@48 | 1302 | # class or its own base classes. |
paulb@48 | 1303 | |
paulb@48 | 1304 | for base_ref in base_refs: |
paulb@50 | 1305 | l = get_attributes(base_ref, name) |
paulb@73 | 1306 | combine(base_attributes, l) |
paulb@48 | 1307 | |
paulb@73 | 1308 | combine(attributes, base_attributes) |
paulb@48 | 1309 | |
paulb@49 | 1310 | return attributes |
paulb@48 | 1311 | |
paulb@48 | 1312 | def get_attributes(structure, name): |
paulb@48 | 1313 | |
paulb@48 | 1314 | """ |
paulb@48 | 1315 | Return all possible attributes for the given 'structure' having the given |
paulb@48 | 1316 | 'name', wrapping each attribute in an Attribute object which includes |
paulb@48 | 1317 | context information for the attribute access. |
paulb@48 | 1318 | |
paulb@48 | 1319 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1320 | the structure involved in accessing the attribute. |
paulb@48 | 1321 | """ |
paulb@48 | 1322 | |
paulb@48 | 1323 | if isinstance(structure, Attribute): |
paulb@48 | 1324 | structure = structure.type |
paulb@53 | 1325 | results = [] |
paulb@53 | 1326 | for attribute, accessor in find_attributes(structure, name): |
paulb@53 | 1327 | if attribute is not None and isinstance(structure, Structure): |
paulb@53 | 1328 | results.append((Attribute(structure, attribute.type), accessor)) |
paulb@53 | 1329 | else: |
paulb@53 | 1330 | results.append((attribute, accessor)) |
paulb@53 | 1331 | return results |
paulb@48 | 1332 | |
paulb@57 | 1333 | # Convenience functions. |
paulb@57 | 1334 | |
paulb@57 | 1335 | def annotate(module, builtins=None): |
paulb@57 | 1336 | |
paulb@57 | 1337 | """ |
paulb@57 | 1338 | Annotate the given 'module', also employing the optional 'builtins' module, |
paulb@57 | 1339 | if specified. |
paulb@57 | 1340 | """ |
paulb@57 | 1341 | |
paulb@57 | 1342 | annotator = Annotator() |
paulb@57 | 1343 | if builtins is not None: |
paulb@57 | 1344 | annotator.process(module, builtins) |
paulb@57 | 1345 | else: |
paulb@57 | 1346 | annotator.process(module) |
paulb@57 | 1347 | |
paulb@57 | 1348 | def annotate_all(modules, builtins): |
paulb@57 | 1349 | annotate(builtins) |
paulb@57 | 1350 | for module in modules: |
paulb@57 | 1351 | annotate(module, builtins) |
paulb@57 | 1352 | |
paulb@19 | 1353 | # vim: tabstop=4 expandtab shiftwidth=4 |