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