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@24 | 58 | def init(self, node): |
paulb@24 | 59 | if not hasattr(node, "types"): |
paulb@24 | 60 | node.types = [] |
paulb@24 | 61 | def annotate(self, node, types): |
paulb@24 | 62 | self.init(node) |
paulb@24 | 63 | for type in types: |
paulb@24 | 64 | if type not in node.types: |
paulb@24 | 65 | node.types.append(type) |
paulb@24 | 66 | self.count += 1 |
paulb@24 | 67 | |
paulb@24 | 68 | system = System() |
paulb@24 | 69 | |
paulb@58 | 70 | # Exceptions. |
paulb@58 | 71 | |
paulb@83 | 72 | class AnnotationError(SimplifiedError): |
paulb@83 | 73 | |
paulb@83 | 74 | "An error in the annotation process." |
paulb@83 | 75 | |
paulb@83 | 76 | pass |
paulb@58 | 77 | |
paulb@67 | 78 | class AnnotationMessage(Exception): |
paulb@83 | 79 | |
paulb@83 | 80 | "A lesser annotation error." |
paulb@83 | 81 | |
paulb@67 | 82 | pass |
paulb@67 | 83 | |
paulb@24 | 84 | # Annotation. |
paulb@19 | 85 | |
paulb@20 | 86 | class Annotator(Visitor): |
paulb@24 | 87 | |
paulb@24 | 88 | """ |
paulb@24 | 89 | The type annotator which traverses the program nodes, typically depth-first, |
paulb@24 | 90 | and maintains a record of the current set of types applying to the currently |
paulb@24 | 91 | considered operation. Such types are also recorded on the nodes, and a |
paulb@24 | 92 | special "system" record is maintained to monitor the level of annotation |
paulb@24 | 93 | activity with a view to recognising when no more annotations are possible. |
paulb@50 | 94 | |
paulb@50 | 95 | Throughout the annotation activity, type information consists of lists of |
paulb@50 | 96 | Attribute objects where such objects retain information about the context of |
paulb@50 | 97 | the type (since a value in the program may be associated with an object or |
paulb@50 | 98 | class) and the actual type of the value being manipulated. Upon accessing |
paulb@50 | 99 | attribute information on namespaces, additional accessor information is also |
paulb@50 | 100 | exchanged - this provides a means of distinguishing between the different |
paulb@50 | 101 | types possible when the means of constructing the namespace may depend on |
paulb@50 | 102 | run-time behaviour. |
paulb@24 | 103 | """ |
paulb@24 | 104 | |
paulb@19 | 105 | def __init__(self): |
paulb@50 | 106 | |
paulb@50 | 107 | "Initialise the visitor." |
paulb@50 | 108 | |
paulb@19 | 109 | Visitor.__init__(self) |
paulb@24 | 110 | self.system = system |
paulb@19 | 111 | |
paulb@30 | 112 | # Satisfy visitor issues. |
paulb@30 | 113 | |
paulb@30 | 114 | self.visitor = self |
paulb@30 | 115 | |
paulb@57 | 116 | def process(self, module, builtins=None): |
paulb@45 | 117 | |
paulb@45 | 118 | """ |
paulb@57 | 119 | Process the given 'module', using the optional 'builtins' to access |
paulb@57 | 120 | built-in classes and functions. |
paulb@45 | 121 | """ |
paulb@45 | 122 | |
paulb@45 | 123 | self.subprograms = [] |
paulb@45 | 124 | self.current_subprograms = [] |
paulb@45 | 125 | self.current_namespaces = [] |
paulb@115 | 126 | self.namespace = None |
paulb@121 | 127 | self.module = module |
paulb@34 | 128 | |
paulb@34 | 129 | # Give constants their own namespace. |
paulb@34 | 130 | |
paulb@57 | 131 | for value, constant in module.simplifier.constants.items(): |
paulb@34 | 132 | constant.namespace = Namespace() |
paulb@34 | 133 | |
paulb@34 | 134 | # Process the module, supplying builtins if possible. |
paulb@34 | 135 | |
paulb@80 | 136 | self.builtins = builtins |
paulb@45 | 137 | self.global_namespace = Namespace() |
paulb@45 | 138 | |
paulb@57 | 139 | if builtins is not None: |
paulb@57 | 140 | self.builtins_namespace = builtins.namespace |
paulb@34 | 141 | else: |
paulb@45 | 142 | self.builtins_namespace = self.global_namespace |
paulb@34 | 143 | |
paulb@132 | 144 | return self.process_node(module, self.global_namespace) |
paulb@45 | 145 | |
paulb@132 | 146 | def process_node(self, node, locals): |
paulb@24 | 147 | |
paulb@24 | 148 | """ |
paulb@132 | 149 | Process a subprogram or module 'node', indicating the initial 'locals'. |
paulb@34 | 150 | Return an annotated subprogram or module. Note that this method may |
paulb@34 | 151 | mutate nodes in the original program. |
paulb@24 | 152 | """ |
paulb@24 | 153 | |
paulb@115 | 154 | # Record the current subprogram and namespace. |
paulb@115 | 155 | |
paulb@115 | 156 | self.current_subprograms.append(node) |
paulb@115 | 157 | |
paulb@67 | 158 | # Determine the namespace. |
paulb@67 | 159 | |
paulb@132 | 160 | self.current_namespaces.append(self.namespace) |
paulb@132 | 161 | self.namespace = locals |
paulb@45 | 162 | |
paulb@25 | 163 | # Add namespace details to any structure involved. |
paulb@25 | 164 | |
paulb@34 | 165 | if getattr(node, "structure", None) is not None: |
paulb@34 | 166 | node.structure.namespace = Namespace() |
paulb@25 | 167 | |
paulb@30 | 168 | # Initialise bases where appropriate. |
paulb@19 | 169 | |
paulb@30 | 170 | if hasattr(node.structure, "bases"): |
paulb@30 | 171 | base_refs = [] |
paulb@30 | 172 | for base in node.structure.bases: |
paulb@30 | 173 | self.dispatch(base) |
paulb@49 | 174 | base_refs.append(self.namespace.types) |
paulb@30 | 175 | node.structure.base_refs = base_refs |
paulb@30 | 176 | |
paulb@30 | 177 | # Dispatch to the code itself. |
paulb@30 | 178 | |
paulb@49 | 179 | node.namespace = self.namespace |
paulb@20 | 180 | result = self.dispatch(node) |
paulb@49 | 181 | result.namespace = self.namespace |
paulb@49 | 182 | |
paulb@49 | 183 | # Obtain the return values. |
paulb@49 | 184 | |
paulb@49 | 185 | self.last_returns = self.namespace.returns |
paulb@116 | 186 | self.last_raises = self.namespace.raises |
paulb@49 | 187 | self.returned_locals = self.namespace.return_locals |
paulb@45 | 188 | |
paulb@45 | 189 | # Restore the previous subprogram and namespace. |
paulb@45 | 190 | |
paulb@132 | 191 | self.namespace = self.current_namespaces.pop() |
paulb@45 | 192 | self.current_subprograms.pop() |
paulb@45 | 193 | |
paulb@20 | 194 | return result |
paulb@20 | 195 | |
paulb@83 | 196 | def annotate(self, node, types=None): |
paulb@24 | 197 | |
paulb@83 | 198 | """ |
paulb@83 | 199 | Annotate the given 'node' in the system, using either the optional |
paulb@83 | 200 | 'types' or the namespace's current type information. |
paulb@83 | 201 | """ |
paulb@24 | 202 | |
paulb@83 | 203 | self.system.annotate(node, types or self.namespace.types) |
paulb@34 | 204 | |
paulb@25 | 205 | # Visitor methods. |
paulb@25 | 206 | |
paulb@19 | 207 | def default(self, node): |
paulb@24 | 208 | |
paulb@24 | 209 | """ |
paulb@24 | 210 | Process the given 'node', given that it does not have a specific |
paulb@24 | 211 | handler. |
paulb@24 | 212 | """ |
paulb@24 | 213 | |
paulb@115 | 214 | raise AnnotationMessage, "Node '%s' not supported." % node |
paulb@20 | 215 | |
paulb@20 | 216 | def dispatch(self, node, *args): |
paulb@34 | 217 | try: |
paulb@34 | 218 | return Visitor.dispatch(self, node, *args) |
paulb@67 | 219 | except AnnotationError, exc: |
paulb@58 | 220 | exc.add(node) |
paulb@34 | 221 | raise |
paulb@67 | 222 | except AnnotationMessage, exc: |
paulb@67 | 223 | raise AnnotationError(exc, node) |
paulb@19 | 224 | |
paulb@110 | 225 | # Program structure/control-flow. |
paulb@110 | 226 | |
paulb@115 | 227 | def visitAssign(self, assign): |
paulb@115 | 228 | assign.code = self.dispatches(assign.code) |
paulb@115 | 229 | return assign |
paulb@115 | 230 | |
paulb@110 | 231 | def visitConditional(self, conditional): |
paulb@19 | 232 | |
paulb@110 | 233 | # Conditionals keep local namespace changes isolated. |
paulb@110 | 234 | # With Return nodes inside the body/else sections, the changes are |
paulb@110 | 235 | # communicated to the caller. |
paulb@19 | 236 | |
paulb@110 | 237 | conditional.test = self.dispatch(conditional.test) |
paulb@110 | 238 | saved_namespace = self.namespace |
paulb@121 | 239 | is_global = self.namespace is self.global_namespace |
paulb@19 | 240 | |
paulb@110 | 241 | self.namespace = Namespace() |
paulb@121 | 242 | if is_global: |
paulb@121 | 243 | self.module.namespace = self.global_namespace = self.namespace |
paulb@110 | 244 | self.namespace.merge_namespace(saved_namespace) |
paulb@110 | 245 | conditional.body = self.dispatches(conditional.body) |
paulb@110 | 246 | body_namespace = self.namespace |
paulb@19 | 247 | |
paulb@110 | 248 | self.namespace = Namespace() |
paulb@121 | 249 | if is_global: |
paulb@121 | 250 | self.module.namespace = self.global_namespace = self.namespace |
paulb@110 | 251 | self.namespace.merge_namespace(saved_namespace) |
paulb@110 | 252 | conditional.else_ = self.dispatches(conditional.else_) |
paulb@110 | 253 | else_namespace = self.namespace |
paulb@19 | 254 | |
paulb@110 | 255 | self.namespace = Namespace() |
paulb@121 | 256 | if is_global: |
paulb@121 | 257 | self.module.namespace = self.global_namespace = self.namespace |
paulb@110 | 258 | self.namespace.merge_namespace(body_namespace) |
paulb@110 | 259 | self.namespace.merge_namespace(else_namespace) |
paulb@110 | 260 | |
paulb@110 | 261 | return conditional |
paulb@110 | 262 | |
paulb@115 | 263 | def visitModule(self, module): |
paulb@115 | 264 | module.code = self.dispatches(module.code) |
paulb@115 | 265 | return module |
paulb@115 | 266 | |
paulb@115 | 267 | def visitPass(self, pass_): |
paulb@115 | 268 | return pass_ |
paulb@115 | 269 | |
paulb@115 | 270 | def visitSubprogram(self, subprogram): |
paulb@115 | 271 | subprogram.code = self.dispatches(subprogram.code) |
paulb@115 | 272 | return subprogram |
paulb@115 | 273 | |
paulb@115 | 274 | def visitTry(self, try_): |
paulb@115 | 275 | try_.body = self.dispatches(try_.body) |
paulb@115 | 276 | try_.handler = self.dispatches(try_.handler) |
paulb@121 | 277 | raises = self.namespace.raises |
paulb@121 | 278 | |
paulb@121 | 279 | # Empty the raised exceptions for the else clause. |
paulb@121 | 280 | |
paulb@121 | 281 | self.namespace.raises = [] |
paulb@115 | 282 | try_.else_ = self.dispatches(try_.else_) |
paulb@121 | 283 | self.namespace.raises = raises |
paulb@121 | 284 | |
paulb@115 | 285 | try_.finally_ = self.dispatches(try_.finally_) |
paulb@115 | 286 | return try_ |
paulb@115 | 287 | |
paulb@110 | 288 | # Namespace operations. |
paulb@20 | 289 | |
paulb@20 | 290 | def visitLoadAttr(self, loadattr): |
paulb@21 | 291 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@20 | 292 | types = [] |
paulb@124 | 293 | non_accesses = [] |
paulb@39 | 294 | accesses = {} |
paulb@49 | 295 | for attr in self.namespace.types: |
paulb@103 | 296 | attributes = get_attributes(attr.type, loadattr.name) |
paulb@103 | 297 | if not attributes: |
paulb@124 | 298 | if not attr.type in non_accesses: |
paulb@124 | 299 | non_accesses.append(attr) |
paulb@131 | 300 | |
paulb@131 | 301 | # Revoke this type from any name involved. |
paulb@131 | 302 | |
paulb@131 | 303 | self._prune_non_accesses(loadattr, attr) |
paulb@131 | 304 | |
paulb@103 | 305 | for attribute, accessor in attributes: |
paulb@49 | 306 | if attribute is not None: |
paulb@49 | 307 | types.append(attribute) |
paulb@79 | 308 | if not accesses.has_key(attr.type): |
paulb@79 | 309 | accesses[attr.type] = [] |
paulb@83 | 310 | if not (attribute, accessor) in accesses[attr.type]: |
paulb@83 | 311 | accesses[attr.type].append((attribute, accessor)) |
paulb@49 | 312 | else: |
paulb@126 | 313 | if not attr in non_accesses: |
paulb@124 | 314 | non_accesses.append(attr) |
paulb@131 | 315 | |
paulb@131 | 316 | # Revoke this type from any name involved. |
paulb@131 | 317 | |
paulb@131 | 318 | self._prune_non_accesses(loadattr, attr) |
paulb@131 | 319 | |
paulb@124 | 320 | if not types: |
paulb@124 | 321 | print "No attribute found for", loadattr.name, "given", self.namespace.types |
paulb@49 | 322 | self.namespace.set_types(types) |
paulb@124 | 323 | loadattr.non_accesses = non_accesses |
paulb@39 | 324 | loadattr.accesses = accesses |
paulb@24 | 325 | self.annotate(loadattr) |
paulb@20 | 326 | return loadattr |
paulb@20 | 327 | |
paulb@131 | 328 | def _prune_non_accesses(self, loadattr, attr): |
paulb@131 | 329 | if isinstance(loadattr.expr, LoadName): |
paulb@131 | 330 | self.namespace.revoke(loadattr.expr.name, attr) |
paulb@131 | 331 | elif isinstance(loadattr.expr, LoadAttr): |
paulb@131 | 332 | for expr_attr in loadattr.expr.expr.types: |
paulb@131 | 333 | if hasattr(expr_attr.type, "namespace"): |
paulb@131 | 334 | expr_attr.type.namespace.revoke(loadattr.expr.name, attr) |
paulb@131 | 335 | |
paulb@115 | 336 | def visitLoadExc(self, loadexc): |
paulb@115 | 337 | self.namespace.types = self.namespace.raises[:] |
paulb@115 | 338 | self.annotate(loadexc) |
paulb@115 | 339 | return loadexc |
paulb@115 | 340 | |
paulb@110 | 341 | def visitLoadName(self, loadname): |
paulb@110 | 342 | self.namespace.set_types(self.namespace.load(loadname.name)) |
paulb@110 | 343 | result = loadname |
paulb@110 | 344 | self.annotate(result) |
paulb@110 | 345 | return result |
paulb@110 | 346 | |
paulb@110 | 347 | def visitLoadRef(self, loadref): |
paulb@110 | 348 | self.namespace.set_types([Attribute(None, loadref.ref)]) |
paulb@110 | 349 | self.annotate(loadref) |
paulb@110 | 350 | return loadref |
paulb@110 | 351 | |
paulb@110 | 352 | def visitLoadTemp(self, loadtemp): |
paulb@110 | 353 | index = getattr(loadtemp, "index", None) |
paulb@110 | 354 | try: |
paulb@110 | 355 | if getattr(loadtemp, "release", 0): |
paulb@110 | 356 | self.namespace.set_types(self.namespace.temp[index].pop()) |
paulb@110 | 357 | else: |
paulb@110 | 358 | self.namespace.set_types(self.namespace.temp[index][-1]) |
paulb@110 | 359 | except KeyError: |
paulb@110 | 360 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@110 | 361 | self.annotate(loadtemp) |
paulb@110 | 362 | return loadtemp |
paulb@110 | 363 | |
paulb@115 | 364 | def visitNot(self, not_): |
paulb@115 | 365 | not_.expr = self.dispatch(not_.expr) |
paulb@115 | 366 | return not_ |
paulb@115 | 367 | |
paulb@112 | 368 | def visitRaise(self, raise_): |
paulb@115 | 369 | if getattr(raise_, "traceback", None) is not None: |
paulb@115 | 370 | raise_.traceback = self.dispatch(raise_.traceback) |
paulb@112 | 371 | raise_.expr = self.dispatch(raise_.expr) |
paulb@112 | 372 | |
paulb@112 | 373 | # Handle bare name exceptions by converting any classes to instances. |
paulb@112 | 374 | |
paulb@112 | 375 | if not isinstance(raise_.expr, InvokeFunction): |
paulb@112 | 376 | raise_.pos_args = [] |
paulb@112 | 377 | raise_.kw_args = {} |
paulb@112 | 378 | raise_.star = None |
paulb@112 | 379 | raise_.dstar = None |
paulb@112 | 380 | types = [] |
paulb@112 | 381 | for attr in self.namespace.types: |
paulb@112 | 382 | if isinstance(attr.type, Class): |
paulb@112 | 383 | self._visitInvoke(raise_, [attr], have_args=0) |
paulb@112 | 384 | types += self.namespace.types |
paulb@112 | 385 | else: |
paulb@112 | 386 | types = self.namespace.types |
paulb@112 | 387 | |
paulb@112 | 388 | combine(self.namespace.raises, types) |
paulb@112 | 389 | return raise_ |
paulb@112 | 390 | |
paulb@110 | 391 | def visitReleaseTemp(self, releasetemp): |
paulb@110 | 392 | index = getattr(releasetemp, "index", None) |
paulb@110 | 393 | try: |
paulb@110 | 394 | self.namespace.temp[index].pop() |
paulb@110 | 395 | except KeyError: |
paulb@110 | 396 | raise AnnotationMessage, "Temporary store index '%s' not defined." % index |
paulb@110 | 397 | except IndexError: |
paulb@110 | 398 | pass #raise AnnotationMessage, "Temporary store index '%s' is empty." % index |
paulb@110 | 399 | return releasetemp |
paulb@110 | 400 | |
paulb@110 | 401 | def visitReturn(self, return_): |
paulb@110 | 402 | if hasattr(return_, "expr"): |
paulb@110 | 403 | return_.expr = self.dispatch(return_.expr) |
paulb@110 | 404 | combine(self.namespace.returns, self.namespace.types) |
paulb@110 | 405 | self.annotate(return_) |
paulb@110 | 406 | self.namespace.snapshot() |
paulb@110 | 407 | return return_ |
paulb@110 | 408 | |
paulb@20 | 409 | def visitStoreAttr(self, storeattr): |
paulb@21 | 410 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@49 | 411 | expr = self.namespace.types |
paulb@21 | 412 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@57 | 413 | writes = {} |
paulb@125 | 414 | non_writes = [] |
paulb@49 | 415 | for attr in self.namespace.types: |
paulb@49 | 416 | if attr is None: |
paulb@125 | 417 | if not attr in non_writes: |
paulb@125 | 418 | non_writes.append(attr) |
paulb@49 | 419 | continue |
paulb@49 | 420 | attr.type.namespace.store(storeattr.name, expr) |
paulb@57 | 421 | writes[attr.type] = attr.type.namespace.load(storeattr.name) |
paulb@125 | 422 | if not writes: |
paulb@125 | 423 | print "Unable to store attribute", storeattr.name, "given", self.namespace.types |
paulb@57 | 424 | storeattr.writes = writes |
paulb@125 | 425 | storeattr.non_writes = non_writes |
paulb@20 | 426 | return storeattr |
paulb@19 | 427 | |
paulb@110 | 428 | def visitStoreName(self, storename): |
paulb@110 | 429 | storename.expr = self.dispatch(storename.expr) |
paulb@110 | 430 | self.namespace.store(storename.name, self.namespace.types) |
paulb@110 | 431 | return storename |
paulb@34 | 432 | |
paulb@110 | 433 | def visitStoreTemp(self, storetemp): |
paulb@110 | 434 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@110 | 435 | index = getattr(storetemp, "index", None) |
paulb@110 | 436 | if not self.namespace.temp.has_key(index): |
paulb@110 | 437 | self.namespace.temp[index] = [] |
paulb@110 | 438 | self.namespace.temp[index].append(self.namespace.types) |
paulb@110 | 439 | return storetemp |
paulb@91 | 440 | |
paulb@110 | 441 | # Invocations are a chapter of their own. |
paulb@30 | 442 | |
paulb@112 | 443 | def visitInvokeBlock(self, invoke): |
paulb@112 | 444 | |
paulb@112 | 445 | # First find the callables. |
paulb@112 | 446 | |
paulb@112 | 447 | invoke.expr = self.dispatch(invoke.expr) |
paulb@112 | 448 | invocation_types = self.namespace.types |
paulb@112 | 449 | return self._visitInvoke(invoke, invocation_types, have_args=0) |
paulb@112 | 450 | |
paulb@112 | 451 | def visitInvokeFunction(self, invoke): |
paulb@83 | 452 | |
paulb@83 | 453 | # First find the callables. |
paulb@83 | 454 | |
paulb@24 | 455 | invoke.expr = self.dispatch(invoke.expr) |
paulb@49 | 456 | invocation_types = self.namespace.types |
paulb@25 | 457 | |
paulb@83 | 458 | # Invocation processing starts with making sure that the arguments have |
paulb@83 | 459 | # been processed. |
paulb@83 | 460 | |
paulb@112 | 461 | return self._visitInvoke(invoke, invocation_types, have_args=self.process_args(invoke)) |
paulb@112 | 462 | |
paulb@112 | 463 | def _visitInvoke(self, invoke, invocation_types, have_args): |
paulb@24 | 464 | |
paulb@34 | 465 | # Now locate and invoke the subprogram. This can be complicated because |
paulb@34 | 466 | # the target may be a class or object, and there may be many different |
paulb@34 | 467 | # related subprograms. |
paulb@24 | 468 | |
paulb@68 | 469 | invocations = [] |
paulb@34 | 470 | |
paulb@39 | 471 | # Visit each callable in turn, finding subprograms. |
paulb@34 | 472 | |
paulb@49 | 473 | for attr in invocation_types: |
paulb@30 | 474 | |
paulb@34 | 475 | # Deal with class invocations by providing instance objects. |
paulb@34 | 476 | # Here, each class is queried for the __init__ method, which may |
paulb@34 | 477 | # exist for some combinations of classes in a hierarchy but not for |
paulb@34 | 478 | # others. |
paulb@34 | 479 | |
paulb@49 | 480 | if isinstance(attr.type, Class): |
paulb@49 | 481 | attributes = get_attributes(attr.type, "__init__") |
paulb@30 | 482 | |
paulb@34 | 483 | # Deal with object invocations by using __call__ methods. |
paulb@34 | 484 | |
paulb@49 | 485 | elif isinstance(attr.type, Instance): |
paulb@49 | 486 | attributes = get_attributes(attr.type, "__call__") |
paulb@34 | 487 | |
paulb@34 | 488 | # Normal functions or methods are more straightforward. |
paulb@39 | 489 | # Here, we model them using an attribute with no context and with |
paulb@39 | 490 | # no associated accessor. |
paulb@34 | 491 | |
paulb@27 | 492 | else: |
paulb@49 | 493 | attributes = [(attr, None)] |
paulb@39 | 494 | |
paulb@39 | 495 | # Inspect each attribute and extract the subprogram. |
paulb@25 | 496 | |
paulb@39 | 497 | for attribute, accessor in attributes: |
paulb@38 | 498 | |
paulb@39 | 499 | # If a class is involved, presume that it must create a new |
paulb@39 | 500 | # object. |
paulb@39 | 501 | |
paulb@49 | 502 | if isinstance(attr.type, Class): |
paulb@53 | 503 | |
paulb@53 | 504 | # Instantiate the class. |
paulb@53 | 505 | # NOTE: Should probably only allocate a single instance. |
paulb@53 | 506 | |
paulb@79 | 507 | instance = self.new_instance(invoke, "new", attr.type.full_name(), attr.type) |
paulb@53 | 508 | |
paulb@53 | 509 | # For instantiations, switch the context. |
paulb@53 | 510 | |
paulb@53 | 511 | if attribute is not None: |
paulb@53 | 512 | attribute = Attribute(instance, attribute.type) |
paulb@53 | 513 | |
paulb@53 | 514 | # Skip cases where no callable is found. |
paulb@53 | 515 | |
paulb@53 | 516 | if attribute is not None: |
paulb@53 | 517 | |
paulb@53 | 518 | # If a subprogram is defined, invoke it. |
paulb@53 | 519 | |
paulb@53 | 520 | self.invoke_subprogram(invoke, attribute) |
paulb@68 | 521 | if attribute.type not in invocations: |
paulb@68 | 522 | invocations.append(attribute.type) |
paulb@53 | 523 | |
paulb@79 | 524 | elif not isinstance(attr.type, Class): |
paulb@68 | 525 | print "Invocation type is None for", accessor |
paulb@53 | 526 | |
paulb@112 | 527 | else: |
paulb@79 | 528 | |
paulb@112 | 529 | # Test to see if no arguments were supplied in cases where no |
paulb@112 | 530 | # initialiser was found. |
paulb@112 | 531 | |
paulb@112 | 532 | if have_args: |
paulb@112 | 533 | raise AnnotationMessage, "No initialiser found for '%s' with arguments." % attr.type |
paulb@53 | 534 | |
paulb@112 | 535 | # Special case: initialisation. |
paulb@112 | 536 | |
paulb@112 | 537 | if isinstance(attr.type, Class): |
paulb@53 | 538 | |
paulb@112 | 539 | # Associate the instance with the result of this invocation. |
paulb@112 | 540 | |
paulb@112 | 541 | self.namespace.set_types([Attribute(None, instance)]) |
paulb@112 | 542 | self.annotate(invoke) |
paulb@39 | 543 | |
paulb@83 | 544 | # Remember the invocations that were found, along with the return type |
paulb@83 | 545 | # information. |
paulb@83 | 546 | |
paulb@38 | 547 | invoke.invocations = invocations |
paulb@80 | 548 | self.namespace.set_types(getattr(invoke, "types", [])) |
paulb@24 | 549 | return invoke |
paulb@24 | 550 | |
paulb@25 | 551 | # Utility methods. |
paulb@25 | 552 | |
paulb@79 | 553 | def new_instance(self, node, reason, target, type): |
paulb@79 | 554 | |
paulb@79 | 555 | "Create, on the given 'node', a new instance with the given 'type'." |
paulb@79 | 556 | |
paulb@79 | 557 | if not hasattr(node, "instances"): |
paulb@79 | 558 | node.instances = {} |
paulb@79 | 559 | |
paulb@79 | 560 | if not node.instances.has_key((reason, target, type)): |
paulb@91 | 561 | |
paulb@91 | 562 | # Insist on a single instance per type. |
paulb@91 | 563 | # NOTE: Strategy-dependent instantiation. |
paulb@91 | 564 | |
paulb@91 | 565 | if len(type.instances) == 0: |
paulb@91 | 566 | instance = Instance() |
paulb@91 | 567 | instance.namespace = Namespace() |
paulb@91 | 568 | instance.namespace.store("__class__", [Attribute(None, type)]) |
paulb@91 | 569 | type.instances.append(instance) |
paulb@91 | 570 | else: |
paulb@91 | 571 | instance = type.instances[0] |
paulb@91 | 572 | |
paulb@116 | 573 | #instance = Instance() |
paulb@116 | 574 | #instance.namespace = Namespace() |
paulb@116 | 575 | #instance.namespace.store("__class__", [Attribute(None, type)]) |
paulb@116 | 576 | #type.instances.append(instance) |
paulb@116 | 577 | |
paulb@79 | 578 | node.instances[(reason, target, type)] = instance |
paulb@79 | 579 | |
paulb@79 | 580 | return node.instances[(reason, target, type)] |
paulb@79 | 581 | |
paulb@34 | 582 | def invoke_subprogram(self, invoke, subprogram): |
paulb@34 | 583 | |
paulb@79 | 584 | "Invoke using the given 'invoke' node the given 'subprogram'." |
paulb@34 | 585 | |
paulb@42 | 586 | # Test to see if anything has changed. |
paulb@42 | 587 | |
paulb@42 | 588 | if hasattr(invoke, "syscount") and invoke.syscount == self.system.count: |
paulb@42 | 589 | return |
paulb@42 | 590 | |
paulb@79 | 591 | # Remember the state of the system. |
paulb@79 | 592 | |
paulb@79 | 593 | else: |
paulb@79 | 594 | invoke.syscount = self.system.count |
paulb@79 | 595 | |
paulb@54 | 596 | # Test for context information, making it into a real attribute. |
paulb@34 | 597 | |
paulb@54 | 598 | if subprogram.context is not None: |
paulb@54 | 599 | context = Attribute(None, subprogram.context) |
paulb@34 | 600 | target = subprogram.type |
paulb@34 | 601 | else: |
paulb@34 | 602 | context = None |
paulb@54 | 603 | target = subprogram.type |
paulb@50 | 604 | |
paulb@34 | 605 | # Provide the correct namespace for the invocation. |
paulb@34 | 606 | |
paulb@112 | 607 | if getattr(invoke, "share_locals", 0): |
paulb@34 | 608 | namespace = Namespace() |
paulb@132 | 609 | namespace.merge_namespace(self.namespace, no_return_locals=1) |
paulb@132 | 610 | using_module_namespace = self.namespace is self.module.namespace |
paulb@112 | 611 | elif getattr(target, "structure", None): |
paulb@112 | 612 | namespace = Namespace() |
paulb@132 | 613 | using_module_namespace = 0 |
paulb@34 | 614 | else: |
paulb@34 | 615 | items = self.make_items(invoke, target, context) |
paulb@34 | 616 | namespace = self.make_namespace(items) |
paulb@132 | 617 | using_module_namespace = 0 |
paulb@34 | 618 | |
paulb@34 | 619 | # Process the subprogram. |
paulb@132 | 620 | # In order to keep global accesses working, the module namespace must be |
paulb@132 | 621 | # adjusted. |
paulb@132 | 622 | |
paulb@132 | 623 | if using_module_namespace: |
paulb@132 | 624 | self.module.namespace = namespace |
paulb@34 | 625 | |
paulb@53 | 626 | self.process_node(target, namespace) |
paulb@34 | 627 | |
paulb@45 | 628 | # NOTE: Improve and verify this. |
paulb@91 | 629 | # If the invocation returns a value, acquire the return types. |
paulb@34 | 630 | |
paulb@53 | 631 | if getattr(target, "returns_value", 0): |
paulb@49 | 632 | self.namespace.set_types(self.last_returns) |
paulb@34 | 633 | self.annotate(invoke) |
paulb@34 | 634 | |
paulb@132 | 635 | # If it is a normal block, merge the locals. |
paulb@132 | 636 | # This can happen in addition to the above because for things like |
paulb@132 | 637 | # logical expressions, the namespace can be modified whilst values are |
paulb@132 | 638 | # returned as results. |
paulb@91 | 639 | |
paulb@132 | 640 | if getattr(invoke, "share_locals", 0): |
paulb@132 | 641 | self.namespace.reset() |
paulb@45 | 642 | for locals in self.returned_locals: |
paulb@132 | 643 | self.namespace.merge_namespace(locals, no_return_locals=1) |
paulb@34 | 644 | |
paulb@116 | 645 | # Incorporate any raised exceptions. |
paulb@116 | 646 | |
paulb@116 | 647 | combine(self.namespace.raises, self.last_raises) |
paulb@116 | 648 | |
paulb@132 | 649 | # In order to keep global accesses working, the module namespace must be |
paulb@132 | 650 | # adjusted. |
paulb@132 | 651 | |
paulb@132 | 652 | if using_module_namespace: |
paulb@132 | 653 | self.module.namespace = self.namespace |
paulb@132 | 654 | |
paulb@83 | 655 | def process_args(self, invocation): |
paulb@83 | 656 | |
paulb@112 | 657 | """ |
paulb@112 | 658 | Process the arguments associated with an 'invocation'. Return whether |
paulb@112 | 659 | any arguments were processed. |
paulb@112 | 660 | """ |
paulb@54 | 661 | |
paulb@93 | 662 | invocation.pos_args = self.dispatches(invocation.pos_args) |
paulb@93 | 663 | invocation.kw_args = self.dispatch_dict(invocation.kw_args) |
paulb@54 | 664 | |
paulb@54 | 665 | # Get type information for star and dstar arguments. |
paulb@54 | 666 | |
paulb@83 | 667 | if invocation.star is not None: |
paulb@83 | 668 | param, default = invocation.star |
paulb@54 | 669 | default = self.dispatch(default) |
paulb@83 | 670 | invocation.star = param, default |
paulb@54 | 671 | |
paulb@83 | 672 | if invocation.dstar is not None: |
paulb@83 | 673 | param, default = invocation.dstar |
paulb@54 | 674 | default = self.dispatch(default) |
paulb@83 | 675 | invocation.dstar = param, default |
paulb@54 | 676 | |
paulb@112 | 677 | if invocation.pos_args or invocation.kw_args or invocation.star or invocation.dstar: |
paulb@112 | 678 | return 1 |
paulb@112 | 679 | else: |
paulb@112 | 680 | return 0 |
paulb@112 | 681 | |
paulb@34 | 682 | def make_items(self, invocation, subprogram, context): |
paulb@34 | 683 | |
paulb@34 | 684 | """ |
paulb@34 | 685 | Make an items mapping for the 'invocation' of the 'subprogram' using the |
paulb@34 | 686 | given 'context' (which may be None). |
paulb@34 | 687 | """ |
paulb@34 | 688 | |
paulb@34 | 689 | if context is not None: |
paulb@93 | 690 | pos_args = [Self(context)] + invocation.pos_args |
paulb@34 | 691 | else: |
paulb@93 | 692 | pos_args = invocation.pos_args |
paulb@93 | 693 | kw_args = invocation.kw_args |
paulb@34 | 694 | |
paulb@67 | 695 | # Sort the arguments into positional and keyword arguments. |
paulb@67 | 696 | |
paulb@24 | 697 | params = subprogram.params |
paulb@24 | 698 | items = [] |
paulb@67 | 699 | star_args = [] |
paulb@24 | 700 | |
paulb@67 | 701 | # Match each positional argument, taking excess arguments as star args. |
paulb@67 | 702 | |
paulb@67 | 703 | for arg in pos_args: |
paulb@67 | 704 | if params: |
paulb@24 | 705 | param, default = params[0] |
paulb@24 | 706 | if arg is None: |
paulb@54 | 707 | arg = default |
paulb@67 | 708 | items.append((param, arg.types)) |
paulb@67 | 709 | params = params[1:] |
paulb@24 | 710 | else: |
paulb@67 | 711 | star_args.append(arg) |
paulb@24 | 712 | |
paulb@24 | 713 | # Collect the remaining defaults. |
paulb@24 | 714 | |
paulb@24 | 715 | while params: |
paulb@24 | 716 | param, default = params[0] |
paulb@67 | 717 | if kw_args.has_key(param): |
paulb@67 | 718 | arg = kw_args[param] |
paulb@73 | 719 | del kw_args[param] |
paulb@93 | 720 | elif default is not None: |
paulb@93 | 721 | arg = self.dispatch(default) |
paulb@93 | 722 | else: |
paulb@67 | 723 | raise AnnotationMessage, "No argument supplied in '%s' for parameter '%s'." % (subprogram, param) |
paulb@24 | 724 | items.append((param, arg.types)) |
paulb@24 | 725 | params = params[1:] |
paulb@24 | 726 | |
paulb@93 | 727 | dstar_args = kw_args.values() |
paulb@73 | 728 | |
paulb@73 | 729 | # Construct temporary objects. |
paulb@73 | 730 | |
paulb@73 | 731 | if star_args: |
paulb@80 | 732 | star_invocation = self.make_star_args(invocation, subprogram, star_args) |
paulb@80 | 733 | self.dispatch(star_invocation) |
paulb@80 | 734 | star_types = star_invocation.types |
paulb@73 | 735 | else: |
paulb@73 | 736 | star_types = None |
paulb@73 | 737 | |
paulb@73 | 738 | if dstar_args: |
paulb@83 | 739 | dstar_invocation = self.make_dstar_args(invocation, subprogram, dstar_args) # NOTE: To be written! |
paulb@80 | 740 | self.dispatch(dstar_invocation) |
paulb@80 | 741 | dstar_types = dstar_invocation.types |
paulb@73 | 742 | else: |
paulb@73 | 743 | dstar_types = None |
paulb@73 | 744 | |
paulb@73 | 745 | # NOTE: Merge the objects properly. |
paulb@73 | 746 | |
paulb@73 | 747 | star_types = star_types or invocation.star and invocation.star.types |
paulb@73 | 748 | dstar_types = dstar_types or invocation.dstar and invocation.dstar.types |
paulb@73 | 749 | |
paulb@24 | 750 | # Add star and dstar. |
paulb@24 | 751 | |
paulb@73 | 752 | if star_types is not None: |
paulb@24 | 753 | if subprogram.star is not None: |
paulb@24 | 754 | param, default = subprogram.star |
paulb@73 | 755 | items.append((param, star_types)) |
paulb@24 | 756 | else: |
paulb@67 | 757 | raise AnnotationMessage, "Invocation provides unwanted *args." |
paulb@24 | 758 | elif subprogram.star is not None: |
paulb@24 | 759 | param, default = subprogram.star |
paulb@54 | 760 | arg = self.dispatch(default) # NOTE: Review reprocessing. |
paulb@54 | 761 | items.append((param, arg.types)) |
paulb@24 | 762 | |
paulb@73 | 763 | if dstar_types is not None: |
paulb@24 | 764 | if subprogram.dstar is not None: |
paulb@24 | 765 | param, default = subprogram.dstar |
paulb@73 | 766 | items.append((param, dstar_types)) |
paulb@24 | 767 | else: |
paulb@67 | 768 | raise AnnotationMessage, "Invocation provides unwanted **args." |
paulb@24 | 769 | elif subprogram.dstar is not None: |
paulb@24 | 770 | param, default = subprogram.dstar |
paulb@54 | 771 | arg = self.dispatch(default) # NOTE: Review reprocessing. |
paulb@54 | 772 | items.append((param, arg.types)) |
paulb@24 | 773 | |
paulb@83 | 774 | # Record the parameter types. |
paulb@83 | 775 | |
paulb@132 | 776 | if not hasattr(subprogram, "paramtypes"): |
paulb@132 | 777 | subprogram.paramtypes = {} |
paulb@132 | 778 | |
paulb@83 | 779 | for param, types in items: |
paulb@132 | 780 | if not subprogram.paramtypes.has_key(param): |
paulb@132 | 781 | subprogram.paramtypes[param] = [] |
paulb@132 | 782 | combine(subprogram.paramtypes[param], types) |
paulb@83 | 783 | |
paulb@24 | 784 | return items |
paulb@24 | 785 | |
paulb@80 | 786 | def make_star_args(self, invocation, subprogram, star_args): |
paulb@80 | 787 | |
paulb@80 | 788 | "Make a subprogram which initialises a list containing 'star_args'." |
paulb@80 | 789 | |
paulb@80 | 790 | if not hasattr(invocation, "stars"): |
paulb@80 | 791 | invocation.stars = {} |
paulb@80 | 792 | |
paulb@80 | 793 | if not invocation.stars.has_key(subprogram.full_name()): |
paulb@80 | 794 | code=[ |
paulb@80 | 795 | StoreTemp( |
paulb@80 | 796 | expr=InvokeFunction( |
paulb@80 | 797 | expr=LoadAttr( |
paulb@80 | 798 | expr=LoadRef( |
paulb@80 | 799 | ref=self.builtins |
paulb@80 | 800 | ), |
paulb@80 | 801 | name="list", |
paulb@80 | 802 | nstype="module", |
paulb@80 | 803 | ), |
paulb@80 | 804 | args=[], |
paulb@80 | 805 | star=None, |
paulb@80 | 806 | dstar=None |
paulb@80 | 807 | ) |
paulb@80 | 808 | ) |
paulb@80 | 809 | ] |
paulb@80 | 810 | |
paulb@80 | 811 | for arg in star_args: |
paulb@80 | 812 | code.append( |
paulb@80 | 813 | InvokeFunction( |
paulb@80 | 814 | expr=LoadAttr( |
paulb@80 | 815 | expr=LoadTemp(), |
paulb@80 | 816 | name="append" |
paulb@80 | 817 | ), |
paulb@80 | 818 | args=[arg], |
paulb@80 | 819 | star=None, |
paulb@80 | 820 | dstar=None |
paulb@80 | 821 | ) |
paulb@80 | 822 | ) |
paulb@80 | 823 | |
paulb@80 | 824 | code += [ |
paulb@80 | 825 | Return(expr=LoadTemp(release=1)) |
paulb@80 | 826 | ] |
paulb@80 | 827 | |
paulb@80 | 828 | invocation.stars[subprogram.full_name()] = InvokeBlock( |
paulb@80 | 829 | produces_result=1, |
paulb@80 | 830 | expr=LoadRef( |
paulb@80 | 831 | ref=Subprogram( |
paulb@80 | 832 | name=None, |
paulb@80 | 833 | returns_value=1, |
paulb@80 | 834 | params=[], |
paulb@80 | 835 | star=None, |
paulb@80 | 836 | dstar=None, |
paulb@80 | 837 | code=code |
paulb@80 | 838 | ) |
paulb@80 | 839 | ) |
paulb@80 | 840 | ) |
paulb@80 | 841 | |
paulb@80 | 842 | return invocation.stars[subprogram.full_name()] |
paulb@80 | 843 | |
paulb@24 | 844 | def make_namespace(self, items): |
paulb@24 | 845 | namespace = Namespace() |
paulb@24 | 846 | namespace.merge_items(items) |
paulb@24 | 847 | return namespace |
paulb@24 | 848 | |
paulb@48 | 849 | # Namespace-related abstractions. |
paulb@48 | 850 | |
paulb@48 | 851 | class Namespace: |
paulb@48 | 852 | |
paulb@48 | 853 | """ |
paulb@48 | 854 | A local namespace which may either relate to a genuine set of function |
paulb@50 | 855 | locals or the initialisation of a structure or module. |
paulb@48 | 856 | """ |
paulb@48 | 857 | |
paulb@48 | 858 | def __init__(self): |
paulb@50 | 859 | |
paulb@50 | 860 | """ |
paulb@50 | 861 | Initialise the namespace with a mapping of local names to possible |
paulb@50 | 862 | types, a list of return values and of possible returned local |
paulb@50 | 863 | namespaces. The namespace also tracks the "current" types and a mapping |
paulb@50 | 864 | of temporary value names to types. |
paulb@50 | 865 | """ |
paulb@50 | 866 | |
paulb@48 | 867 | self.names = {} |
paulb@49 | 868 | self.returns = [] |
paulb@49 | 869 | self.return_locals = [] |
paulb@112 | 870 | self.raises = [] |
paulb@49 | 871 | self.temp = {} |
paulb@49 | 872 | self.types = [] |
paulb@49 | 873 | |
paulb@49 | 874 | def set_types(self, types): |
paulb@49 | 875 | self.types = types |
paulb@48 | 876 | |
paulb@48 | 877 | def store(self, name, types): |
paulb@48 | 878 | self.names[name] = types |
paulb@48 | 879 | |
paulb@86 | 880 | __setitem__ = store |
paulb@74 | 881 | |
paulb@48 | 882 | def load(self, name): |
paulb@48 | 883 | return self.names[name] |
paulb@48 | 884 | |
paulb@86 | 885 | __getitem__ = load |
paulb@74 | 886 | |
paulb@131 | 887 | def revoke(self, name, type): |
paulb@131 | 888 | self.names[name].remove(type) |
paulb@131 | 889 | |
paulb@132 | 890 | def merge_namespace(self, namespace, no_return_locals=0): |
paulb@68 | 891 | self.merge_items(namespace.names.items()) |
paulb@73 | 892 | combine(self.returns, namespace.returns) |
paulb@132 | 893 | if not no_return_locals: |
paulb@132 | 894 | combine(self.return_locals, namespace.return_locals) |
paulb@115 | 895 | combine(self.raises, namespace.raises) |
paulb@121 | 896 | self.temp = {} |
paulb@121 | 897 | self.temp.update(namespace.temp) |
paulb@68 | 898 | |
paulb@68 | 899 | def merge_items(self, items): |
paulb@68 | 900 | for name, types in items: |
paulb@68 | 901 | self.merge(name, types) |
paulb@68 | 902 | |
paulb@48 | 903 | def merge(self, name, types): |
paulb@48 | 904 | if not self.names.has_key(name): |
paulb@48 | 905 | self.names[name] = types[:] |
paulb@48 | 906 | else: |
paulb@48 | 907 | existing = self.names[name] |
paulb@73 | 908 | combine(existing, types) |
paulb@48 | 909 | |
paulb@49 | 910 | def snapshot(self): |
paulb@49 | 911 | |
paulb@49 | 912 | "Make a snapshot of the locals and remember them." |
paulb@49 | 913 | |
paulb@49 | 914 | namespace = Namespace() |
paulb@49 | 915 | namespace.merge_namespace(self) |
paulb@49 | 916 | self.return_locals.append(namespace) |
paulb@49 | 917 | |
paulb@132 | 918 | def reset(self): |
paulb@132 | 919 | |
paulb@132 | 920 | "Reset a namespace in preparation for merging with returned locals." |
paulb@132 | 921 | |
paulb@132 | 922 | self.names = {} |
paulb@132 | 923 | |
paulb@48 | 924 | def __repr__(self): |
paulb@48 | 925 | return repr(self.names) |
paulb@48 | 926 | |
paulb@48 | 927 | class Attribute: |
paulb@48 | 928 | |
paulb@48 | 929 | """ |
paulb@48 | 930 | An attribute abstraction, indicating the type of the attribute along with |
paulb@48 | 931 | its context or origin. |
paulb@48 | 932 | """ |
paulb@48 | 933 | |
paulb@48 | 934 | def __init__(self, context, type): |
paulb@48 | 935 | self.context = context |
paulb@48 | 936 | self.type = type |
paulb@48 | 937 | |
paulb@48 | 938 | def __eq__(self, other): |
paulb@48 | 939 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@48 | 940 | |
paulb@48 | 941 | def __repr__(self): |
paulb@54 | 942 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@48 | 943 | |
paulb@53 | 944 | class Self: |
paulb@91 | 945 | |
paulb@116 | 946 | """ |
paulb@116 | 947 | A program node encapsulating object/context information in an argument list. |
paulb@116 | 948 | This is not particularly like Attribute, Class, Instance or other such |
paulb@116 | 949 | things, since it actually appears in the program representation. |
paulb@116 | 950 | """ |
paulb@91 | 951 | |
paulb@53 | 952 | def __init__(self, attribute): |
paulb@53 | 953 | self.types = [attribute] |
paulb@53 | 954 | |
paulb@73 | 955 | def combine(target, additions): |
paulb@91 | 956 | |
paulb@91 | 957 | """ |
paulb@91 | 958 | Merge into the 'target' sequence the given 'additions', preventing duplicate |
paulb@91 | 959 | items. |
paulb@91 | 960 | """ |
paulb@91 | 961 | |
paulb@73 | 962 | for addition in additions: |
paulb@73 | 963 | if addition not in target: |
paulb@73 | 964 | target.append(addition) |
paulb@73 | 965 | |
paulb@48 | 966 | def find_attributes(structure, name): |
paulb@48 | 967 | |
paulb@48 | 968 | """ |
paulb@48 | 969 | Find for the given 'structure' all attributes for the given 'name', visiting |
paulb@48 | 970 | base classes where appropriate and returning the attributes in order of |
paulb@48 | 971 | descending precedence for all possible base classes. |
paulb@48 | 972 | |
paulb@48 | 973 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 974 | the structure involved in accessing the attribute. |
paulb@48 | 975 | """ |
paulb@48 | 976 | |
paulb@48 | 977 | # First attempt to search the instance/class namespace. |
paulb@48 | 978 | |
paulb@48 | 979 | try: |
paulb@48 | 980 | l = structure.namespace.load(name) |
paulb@48 | 981 | attributes = [] |
paulb@48 | 982 | for attribute in l: |
paulb@48 | 983 | attributes.append((attribute, structure)) |
paulb@48 | 984 | |
paulb@48 | 985 | # If that does not work, attempt to investigate any class or base classes. |
paulb@48 | 986 | |
paulb@48 | 987 | except KeyError: |
paulb@48 | 988 | attributes = [] |
paulb@48 | 989 | |
paulb@48 | 990 | # Investigate any instance's implementing class. |
paulb@48 | 991 | |
paulb@48 | 992 | if isinstance(structure, Instance): |
paulb@73 | 993 | for attr in structure.namespace.load("__class__"): |
paulb@73 | 994 | cls = attr.type |
paulb@50 | 995 | l = get_attributes(cls, name) |
paulb@73 | 996 | combine(attributes, l) |
paulb@48 | 997 | |
paulb@48 | 998 | # Investigate any class's base classes. |
paulb@48 | 999 | |
paulb@48 | 1000 | elif isinstance(structure, Class): |
paulb@48 | 1001 | |
paulb@48 | 1002 | # If no base classes exist, return an indicator that no attribute |
paulb@48 | 1003 | # exists. |
paulb@48 | 1004 | |
paulb@48 | 1005 | if not structure.base_refs: |
paulb@48 | 1006 | return [(None, structure)] |
paulb@48 | 1007 | |
paulb@48 | 1008 | # Otherwise, find all possible base classes. |
paulb@48 | 1009 | |
paulb@48 | 1010 | for base_refs in structure.base_refs: |
paulb@48 | 1011 | base_attributes = [] |
paulb@48 | 1012 | |
paulb@48 | 1013 | # For each base class, find attributes either in the base |
paulb@48 | 1014 | # class or its own base classes. |
paulb@48 | 1015 | |
paulb@48 | 1016 | for base_ref in base_refs: |
paulb@50 | 1017 | l = get_attributes(base_ref, name) |
paulb@73 | 1018 | combine(base_attributes, l) |
paulb@48 | 1019 | |
paulb@73 | 1020 | combine(attributes, base_attributes) |
paulb@48 | 1021 | |
paulb@49 | 1022 | return attributes |
paulb@48 | 1023 | |
paulb@48 | 1024 | def get_attributes(structure, name): |
paulb@48 | 1025 | |
paulb@48 | 1026 | """ |
paulb@48 | 1027 | Return all possible attributes for the given 'structure' having the given |
paulb@48 | 1028 | 'name', wrapping each attribute in an Attribute object which includes |
paulb@48 | 1029 | context information for the attribute access. |
paulb@48 | 1030 | |
paulb@48 | 1031 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 1032 | the structure involved in accessing the attribute. |
paulb@48 | 1033 | """ |
paulb@48 | 1034 | |
paulb@48 | 1035 | if isinstance(structure, Attribute): |
paulb@48 | 1036 | structure = structure.type |
paulb@53 | 1037 | results = [] |
paulb@53 | 1038 | for attribute, accessor in find_attributes(structure, name): |
paulb@53 | 1039 | if attribute is not None and isinstance(structure, Structure): |
paulb@53 | 1040 | results.append((Attribute(structure, attribute.type), accessor)) |
paulb@53 | 1041 | else: |
paulb@53 | 1042 | results.append((attribute, accessor)) |
paulb@53 | 1043 | return results |
paulb@48 | 1044 | |
paulb@57 | 1045 | # Convenience functions. |
paulb@57 | 1046 | |
paulb@57 | 1047 | def annotate(module, builtins=None): |
paulb@57 | 1048 | |
paulb@57 | 1049 | """ |
paulb@57 | 1050 | Annotate the given 'module', also employing the optional 'builtins' module, |
paulb@57 | 1051 | if specified. |
paulb@57 | 1052 | """ |
paulb@57 | 1053 | |
paulb@57 | 1054 | annotator = Annotator() |
paulb@57 | 1055 | if builtins is not None: |
paulb@57 | 1056 | annotator.process(module, builtins) |
paulb@57 | 1057 | else: |
paulb@57 | 1058 | annotator.process(module) |
paulb@57 | 1059 | |
paulb@57 | 1060 | def annotate_all(modules, builtins): |
paulb@57 | 1061 | annotate(builtins) |
paulb@57 | 1062 | for module in modules: |
paulb@57 | 1063 | annotate(module, builtins) |
paulb@57 | 1064 | |
paulb@19 | 1065 | # vim: tabstop=4 expandtab shiftwidth=4 |