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