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