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@50 | 27 | To annotate a module, first obtain an Annotator: |
paulb@50 | 28 | |
paulb@50 | 29 | annotator = Annotator() |
paulb@50 | 30 | |
paulb@50 | 31 | Then, process the Simplifier object which holds the details of the module: |
paulb@50 | 32 | |
paulb@50 | 33 | annotator.process(simplifier) |
paulb@50 | 34 | |
paulb@50 | 35 | If a module containing built-in classes and functions has already been |
paulb@50 | 36 | annotated, such a module should be passed in as an additional argument: |
paulb@50 | 37 | |
paulb@50 | 38 | annotator.process(simplifier, builtins_simplifier) |
paulb@19 | 39 | """ |
paulb@19 | 40 | |
paulb@19 | 41 | from simplified import * |
paulb@19 | 42 | import compiler |
paulb@19 | 43 | |
paulb@24 | 44 | class System: |
paulb@24 | 45 | |
paulb@50 | 46 | """ |
paulb@50 | 47 | A class maintaining the state of the annotation system. When the system |
paulb@50 | 48 | counter can no longer be incremented by any annotation operation, the |
paulb@50 | 49 | system may be considered stable and fully annotated. |
paulb@50 | 50 | """ |
paulb@24 | 51 | |
paulb@24 | 52 | def __init__(self): |
paulb@24 | 53 | self.count = 0 |
paulb@24 | 54 | def init(self, node): |
paulb@24 | 55 | if not hasattr(node, "types"): |
paulb@24 | 56 | node.types = [] |
paulb@24 | 57 | def annotate(self, node, types): |
paulb@24 | 58 | self.init(node) |
paulb@24 | 59 | for type in types: |
paulb@24 | 60 | if type not in node.types: |
paulb@24 | 61 | node.types.append(type) |
paulb@24 | 62 | self.count += 1 |
paulb@24 | 63 | |
paulb@24 | 64 | system = System() |
paulb@24 | 65 | |
paulb@24 | 66 | # Annotation. |
paulb@19 | 67 | |
paulb@20 | 68 | class Annotator(Visitor): |
paulb@24 | 69 | |
paulb@24 | 70 | """ |
paulb@24 | 71 | The type annotator which traverses the program nodes, typically depth-first, |
paulb@24 | 72 | and maintains a record of the current set of types applying to the currently |
paulb@24 | 73 | considered operation. Such types are also recorded on the nodes, and a |
paulb@24 | 74 | special "system" record is maintained to monitor the level of annotation |
paulb@24 | 75 | activity with a view to recognising when no more annotations are possible. |
paulb@50 | 76 | |
paulb@50 | 77 | Throughout the annotation activity, type information consists of lists of |
paulb@50 | 78 | Attribute objects where such objects retain information about the context of |
paulb@50 | 79 | the type (since a value in the program may be associated with an object or |
paulb@50 | 80 | class) and the actual type of the value being manipulated. Upon accessing |
paulb@50 | 81 | attribute information on namespaces, additional accessor information is also |
paulb@50 | 82 | exchanged - this provides a means of distinguishing between the different |
paulb@50 | 83 | types possible when the means of constructing the namespace may depend on |
paulb@50 | 84 | run-time behaviour. |
paulb@24 | 85 | """ |
paulb@24 | 86 | |
paulb@19 | 87 | def __init__(self): |
paulb@50 | 88 | |
paulb@50 | 89 | "Initialise the visitor." |
paulb@50 | 90 | |
paulb@19 | 91 | Visitor.__init__(self) |
paulb@24 | 92 | self.system = system |
paulb@19 | 93 | |
paulb@30 | 94 | # Satisfy visitor issues. |
paulb@30 | 95 | |
paulb@30 | 96 | self.visitor = self |
paulb@30 | 97 | |
paulb@45 | 98 | def process(self, visitor, builtins_visitor=None): |
paulb@45 | 99 | |
paulb@45 | 100 | """ |
paulb@45 | 101 | Process the resources of the given 'visitor', using the optional |
paulb@45 | 102 | 'builtins_visitor' to access built-in classes and functions. |
paulb@45 | 103 | """ |
paulb@45 | 104 | |
paulb@45 | 105 | self.subprograms = [] |
paulb@45 | 106 | self.current_subprograms = [] |
paulb@45 | 107 | self.current_namespaces = [] |
paulb@34 | 108 | |
paulb@34 | 109 | # Give constants their own namespace. |
paulb@34 | 110 | |
paulb@34 | 111 | for value, constant in visitor.constants.items(): |
paulb@34 | 112 | constant.namespace = Namespace() |
paulb@34 | 113 | |
paulb@34 | 114 | # Process the module, supplying builtins if possible. |
paulb@34 | 115 | |
paulb@45 | 116 | self.global_namespace = Namespace() |
paulb@45 | 117 | |
paulb@34 | 118 | if builtins_visitor is not None: |
paulb@45 | 119 | self.builtins_namespace = builtins_visitor.result.namespace |
paulb@34 | 120 | else: |
paulb@45 | 121 | self.builtins_namespace = self.global_namespace |
paulb@34 | 122 | |
paulb@45 | 123 | return self.process_node(visitor.result) |
paulb@45 | 124 | |
paulb@45 | 125 | def process_node(self, node, locals=None): |
paulb@24 | 126 | |
paulb@24 | 127 | """ |
paulb@34 | 128 | Process a subprogram or module 'node', indicating any initial 'locals'. |
paulb@34 | 129 | Return an annotated subprogram or module. Note that this method may |
paulb@34 | 130 | mutate nodes in the original program. |
paulb@24 | 131 | """ |
paulb@24 | 132 | |
paulb@45 | 133 | if locals: |
paulb@45 | 134 | self.namespace = locals |
paulb@45 | 135 | else: |
paulb@45 | 136 | self.namespace = self.global_namespace |
paulb@45 | 137 | |
paulb@45 | 138 | # Record the current subprogram and namespace. |
paulb@20 | 139 | |
paulb@45 | 140 | self.current_subprograms.append(node) |
paulb@45 | 141 | self.current_namespaces.append(self.namespace) |
paulb@30 | 142 | |
paulb@25 | 143 | # Add namespace details to any structure involved. |
paulb@25 | 144 | |
paulb@34 | 145 | if getattr(node, "structure", None) is not None: |
paulb@34 | 146 | node.structure.namespace = Namespace() |
paulb@25 | 147 | |
paulb@30 | 148 | # Initialise bases where appropriate. |
paulb@19 | 149 | |
paulb@30 | 150 | if hasattr(node.structure, "bases"): |
paulb@30 | 151 | base_refs = [] |
paulb@30 | 152 | for base in node.structure.bases: |
paulb@30 | 153 | self.dispatch(base) |
paulb@49 | 154 | base_refs.append(self.namespace.types) |
paulb@30 | 155 | node.structure.base_refs = base_refs |
paulb@30 | 156 | |
paulb@30 | 157 | # Dispatch to the code itself. |
paulb@30 | 158 | |
paulb@49 | 159 | node.namespace = self.namespace |
paulb@20 | 160 | result = self.dispatch(node) |
paulb@49 | 161 | result.namespace = self.namespace |
paulb@49 | 162 | |
paulb@49 | 163 | # Obtain the return values. |
paulb@49 | 164 | |
paulb@49 | 165 | self.last_returns = self.namespace.returns |
paulb@49 | 166 | self.returned_locals = self.namespace.return_locals |
paulb@45 | 167 | |
paulb@45 | 168 | # Restore the previous subprogram and namespace. |
paulb@45 | 169 | |
paulb@45 | 170 | self.current_namespaces.pop() |
paulb@45 | 171 | if self.current_namespaces: |
paulb@45 | 172 | self.namespace = self.current_namespaces[-1] |
paulb@45 | 173 | |
paulb@45 | 174 | self.current_subprograms.pop() |
paulb@45 | 175 | |
paulb@20 | 176 | return result |
paulb@20 | 177 | |
paulb@24 | 178 | def annotate(self, node): |
paulb@24 | 179 | |
paulb@24 | 180 | "Annotate the given 'node' in the system." |
paulb@24 | 181 | |
paulb@49 | 182 | self.system.annotate(node, self.namespace.types) |
paulb@34 | 183 | |
paulb@25 | 184 | # Visitor methods. |
paulb@25 | 185 | |
paulb@19 | 186 | def default(self, node): |
paulb@24 | 187 | |
paulb@24 | 188 | """ |
paulb@24 | 189 | Process the given 'node', given that it does not have a specific |
paulb@24 | 190 | handler. |
paulb@24 | 191 | """ |
paulb@24 | 192 | |
paulb@25 | 193 | for attr in ("expr", "lvalue", "test", "handler"): |
paulb@19 | 194 | value = getattr(node, attr, None) |
paulb@19 | 195 | if value is not None: |
paulb@20 | 196 | setattr(node, attr, self.dispatch(value)) |
paulb@19 | 197 | for attr in ("body", "else_", "finally_", "code"): |
paulb@19 | 198 | value = getattr(node, attr, None) |
paulb@19 | 199 | if value is not None: |
paulb@20 | 200 | setattr(node, attr, self.dispatches(value)) |
paulb@20 | 201 | return node |
paulb@20 | 202 | |
paulb@20 | 203 | def dispatch(self, node, *args): |
paulb@34 | 204 | try: |
paulb@34 | 205 | return Visitor.dispatch(self, node, *args) |
paulb@34 | 206 | except: |
paulb@34 | 207 | print "Failed using node", node |
paulb@34 | 208 | raise |
paulb@19 | 209 | |
paulb@19 | 210 | def visitLoadRef(self, loadref): |
paulb@49 | 211 | self.namespace.set_types([Attribute(None, loadref.ref)]) |
paulb@24 | 212 | self.annotate(loadref) |
paulb@20 | 213 | return loadref |
paulb@19 | 214 | |
paulb@19 | 215 | def visitLoadName(self, loadname): |
paulb@49 | 216 | self.namespace.set_types(self.namespace.load(loadname.name)) |
paulb@31 | 217 | result = loadname |
paulb@31 | 218 | self.annotate(result) |
paulb@24 | 219 | return result |
paulb@19 | 220 | |
paulb@19 | 221 | def visitStoreName(self, storename): |
paulb@31 | 222 | storename.expr = self.dispatch(storename.expr) |
paulb@49 | 223 | self.namespace.store(storename.name, self.namespace.types) |
paulb@31 | 224 | return storename |
paulb@19 | 225 | |
paulb@19 | 226 | def visitLoadTemp(self, loadtemp): |
paulb@19 | 227 | index = getattr(loadtemp, "index", None) |
paulb@49 | 228 | self.namespace.set_types(self.namespace.temp[index][-1]) |
paulb@24 | 229 | self.annotate(loadtemp) |
paulb@20 | 230 | return loadtemp |
paulb@19 | 231 | |
paulb@19 | 232 | def visitStoreTemp(self, storetemp): |
paulb@21 | 233 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@19 | 234 | index = getattr(storetemp, "index", None) |
paulb@49 | 235 | if not self.namespace.temp.has_key(index): |
paulb@49 | 236 | self.namespace.temp[index] = [] |
paulb@49 | 237 | self.namespace.temp[index].append(self.namespace.types) |
paulb@20 | 238 | return storetemp |
paulb@19 | 239 | |
paulb@19 | 240 | def visitReleaseTemp(self, releasetemp): |
paulb@19 | 241 | index = getattr(releasetemp, "index", None) |
paulb@49 | 242 | self.namespace.temp[index].pop() |
paulb@20 | 243 | return releasetemp |
paulb@20 | 244 | |
paulb@20 | 245 | def visitLoadAttr(self, loadattr): |
paulb@21 | 246 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@20 | 247 | types = [] |
paulb@39 | 248 | accesses = {} |
paulb@49 | 249 | for attr in self.namespace.types: |
paulb@49 | 250 | if not accesses.has_key(attr.type): |
paulb@49 | 251 | accesses[attr.type] = [] |
paulb@49 | 252 | for attribute, accessor in get_attributes(attr.type, loadattr.name): |
paulb@49 | 253 | if attribute is not None: |
paulb@49 | 254 | types.append(attribute) |
paulb@49 | 255 | else: |
paulb@49 | 256 | print "Empty attribute via accessor", accessor |
paulb@49 | 257 | accesses[attr.type].append((attribute, accessor)) |
paulb@49 | 258 | self.namespace.set_types(types) |
paulb@39 | 259 | loadattr.accesses = accesses |
paulb@24 | 260 | self.annotate(loadattr) |
paulb@20 | 261 | return loadattr |
paulb@20 | 262 | |
paulb@20 | 263 | def visitStoreAttr(self, storeattr): |
paulb@21 | 264 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@49 | 265 | expr = self.namespace.types |
paulb@21 | 266 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@39 | 267 | accesses = {} |
paulb@49 | 268 | for attr in self.namespace.types: |
paulb@49 | 269 | if attr is None: |
paulb@49 | 270 | print "Empty attribute storage attempt" |
paulb@49 | 271 | continue |
paulb@49 | 272 | attr.type.namespace.store(storeattr.name, expr) |
paulb@49 | 273 | accesses[attr.type] = attr.type.namespace.load(storeattr.name) |
paulb@39 | 274 | storeattr.accesses = accesses |
paulb@20 | 275 | return storeattr |
paulb@19 | 276 | |
paulb@34 | 277 | def visitConditional(self, conditional): |
paulb@34 | 278 | |
paulb@34 | 279 | # Conditionals keep local namespace changes isolated. |
paulb@34 | 280 | # With Return nodes inside the body/else sections, the changes are |
paulb@34 | 281 | # communicated to the caller. |
paulb@34 | 282 | |
paulb@34 | 283 | conditional.test = self.dispatch(conditional.test) |
paulb@34 | 284 | saved_namespace = self.namespace |
paulb@34 | 285 | |
paulb@34 | 286 | self.namespace = Namespace() |
paulb@34 | 287 | self.namespace.merge_namespace(saved_namespace) |
paulb@34 | 288 | conditional.body = self.dispatches(conditional.body) |
paulb@38 | 289 | body_namespace = self.namespace |
paulb@34 | 290 | |
paulb@34 | 291 | self.namespace = Namespace() |
paulb@34 | 292 | self.namespace.merge_namespace(saved_namespace) |
paulb@34 | 293 | conditional.else_ = self.dispatches(conditional.else_) |
paulb@38 | 294 | else_namespace = self.namespace |
paulb@34 | 295 | |
paulb@38 | 296 | self.namespace = Namespace() |
paulb@38 | 297 | self.namespace.merge_namespace(body_namespace) |
paulb@38 | 298 | self.namespace.merge_namespace(else_namespace) |
paulb@34 | 299 | return conditional |
paulb@34 | 300 | |
paulb@30 | 301 | def visitReturn(self, return_): |
paulb@30 | 302 | if hasattr(return_, "expr"): |
paulb@30 | 303 | return_.expr = self.dispatch(return_.expr) |
paulb@49 | 304 | self.namespace.returns += self.namespace.types |
paulb@49 | 305 | self.namespace.snapshot() |
paulb@30 | 306 | return return_ |
paulb@30 | 307 | |
paulb@24 | 308 | def visitInvoke(self, invoke): |
paulb@24 | 309 | invoke.expr = self.dispatch(invoke.expr) |
paulb@49 | 310 | invocation_types = self.namespace.types |
paulb@25 | 311 | |
paulb@25 | 312 | # NOTE: Consider initialiser invocation for classes. |
paulb@25 | 313 | |
paulb@24 | 314 | types = [] |
paulb@24 | 315 | args = [] |
paulb@24 | 316 | |
paulb@24 | 317 | # Get type information for regular arguments. |
paulb@24 | 318 | |
paulb@24 | 319 | for arg in invoke.args: |
paulb@24 | 320 | args.append(self.dispatch(arg)) |
paulb@49 | 321 | types.append(self.namespace.types) |
paulb@24 | 322 | |
paulb@24 | 323 | # Get type information for star and dstar arguments. |
paulb@24 | 324 | |
paulb@24 | 325 | if invoke.star is not None: |
paulb@24 | 326 | param, default = invoke.star |
paulb@24 | 327 | default = self.dispatch(default) |
paulb@24 | 328 | invoke.star = param, default |
paulb@24 | 329 | types.append(default.types) |
paulb@24 | 330 | |
paulb@24 | 331 | if invoke.dstar is not None: |
paulb@24 | 332 | param, default = invoke.dstar |
paulb@24 | 333 | default = self.dispatch(default) |
paulb@24 | 334 | invoke.dstar = param, default |
paulb@24 | 335 | types.append(default.types) |
paulb@24 | 336 | |
paulb@24 | 337 | invoke.args = args |
paulb@24 | 338 | |
paulb@34 | 339 | # Now locate and invoke the subprogram. This can be complicated because |
paulb@34 | 340 | # the target may be a class or object, and there may be many different |
paulb@34 | 341 | # related subprograms. |
paulb@24 | 342 | |
paulb@38 | 343 | invocations = {} |
paulb@34 | 344 | |
paulb@39 | 345 | # Visit each callable in turn, finding subprograms. |
paulb@34 | 346 | |
paulb@49 | 347 | for attr in invocation_types: |
paulb@30 | 348 | |
paulb@34 | 349 | # Deal with class invocations by providing instance objects. |
paulb@34 | 350 | # Here, each class is queried for the __init__ method, which may |
paulb@34 | 351 | # exist for some combinations of classes in a hierarchy but not for |
paulb@34 | 352 | # others. |
paulb@34 | 353 | |
paulb@49 | 354 | if isinstance(attr.type, Class): |
paulb@49 | 355 | attributes = get_attributes(attr.type, "__init__") |
paulb@30 | 356 | |
paulb@34 | 357 | # Deal with object invocations by using __call__ methods. |
paulb@34 | 358 | |
paulb@49 | 359 | elif isinstance(attr.type, Instance): |
paulb@49 | 360 | attributes = get_attributes(attr.type, "__call__") |
paulb@34 | 361 | |
paulb@34 | 362 | # Normal functions or methods are more straightforward. |
paulb@39 | 363 | # Here, we model them using an attribute with no context and with |
paulb@39 | 364 | # no associated accessor. |
paulb@34 | 365 | |
paulb@27 | 366 | else: |
paulb@49 | 367 | attributes = [(attr, None)] |
paulb@39 | 368 | |
paulb@39 | 369 | # Inspect each attribute and extract the subprogram. |
paulb@25 | 370 | |
paulb@39 | 371 | for attribute, accessor in attributes: |
paulb@38 | 372 | |
paulb@39 | 373 | # If a class is involved, presume that it must create a new |
paulb@39 | 374 | # object. |
paulb@39 | 375 | |
paulb@49 | 376 | if isinstance(attr.type, Class): |
paulb@53 | 377 | |
paulb@53 | 378 | # Instantiate the class. |
paulb@53 | 379 | # NOTE: Should probably only allocate a single instance. |
paulb@53 | 380 | |
paulb@53 | 381 | instance = Instance() |
paulb@53 | 382 | instance.namespace = Namespace() |
paulb@53 | 383 | instance.namespace.store("__class__", [attr.type]) |
paulb@53 | 384 | instance = Attribute(None, instance) |
paulb@53 | 385 | |
paulb@53 | 386 | # For instantiations, switch the context. |
paulb@53 | 387 | |
paulb@53 | 388 | if attribute is not None: |
paulb@53 | 389 | attribute = Attribute(instance, attribute.type) |
paulb@53 | 390 | |
paulb@53 | 391 | # Skip cases where no callable is found. |
paulb@53 | 392 | |
paulb@53 | 393 | if attribute is not None: |
paulb@53 | 394 | |
paulb@53 | 395 | # If a subprogram is defined, invoke it. |
paulb@53 | 396 | |
paulb@53 | 397 | self.invoke_subprogram(invoke, attribute) |
paulb@53 | 398 | invocations[callable] = attribute.type |
paulb@53 | 399 | |
paulb@53 | 400 | else: |
paulb@53 | 401 | print "Invocation type is None" |
paulb@53 | 402 | |
paulb@53 | 403 | if isinstance(attr.type, Class): |
paulb@53 | 404 | |
paulb@53 | 405 | # Associate the instance with the result of this invocation. |
paulb@53 | 406 | |
paulb@53 | 407 | self.namespace.set_types([instance]) |
paulb@39 | 408 | self.annotate(invoke) |
paulb@39 | 409 | |
paulb@38 | 410 | invoke.invocations = invocations |
paulb@24 | 411 | |
paulb@24 | 412 | return invoke |
paulb@24 | 413 | |
paulb@25 | 414 | # Utility methods. |
paulb@25 | 415 | |
paulb@34 | 416 | def invoke_subprogram(self, invoke, subprogram): |
paulb@34 | 417 | |
paulb@34 | 418 | """ |
paulb@34 | 419 | Invoke using the given 'invoke' node the given 'subprogram'. |
paulb@34 | 420 | """ |
paulb@34 | 421 | |
paulb@42 | 422 | # Test to see if anything has changed. |
paulb@42 | 423 | |
paulb@42 | 424 | if hasattr(invoke, "syscount") and invoke.syscount == self.system.count: |
paulb@42 | 425 | return |
paulb@42 | 426 | |
paulb@34 | 427 | # Test for context information. |
paulb@34 | 428 | |
paulb@34 | 429 | if hasattr(subprogram, "context"): |
paulb@34 | 430 | context = subprogram.context |
paulb@34 | 431 | target = subprogram.type |
paulb@34 | 432 | else: |
paulb@34 | 433 | context = None |
paulb@34 | 434 | target = subprogram |
paulb@34 | 435 | |
paulb@53 | 436 | print subprogram, "->", context, target |
paulb@50 | 437 | |
paulb@34 | 438 | # Provide the correct namespace for the invocation. |
paulb@34 | 439 | |
paulb@34 | 440 | if getattr(invoke, "same_frame", 0): |
paulb@34 | 441 | namespace = Namespace() |
paulb@34 | 442 | namespace.merge_namespace(self.namespace) |
paulb@34 | 443 | else: |
paulb@34 | 444 | items = self.make_items(invoke, target, context) |
paulb@34 | 445 | namespace = self.make_namespace(items) |
paulb@34 | 446 | |
paulb@34 | 447 | # Process the subprogram. |
paulb@34 | 448 | |
paulb@53 | 449 | self.process_node(target, namespace) |
paulb@34 | 450 | |
paulb@45 | 451 | # NOTE: Improve and verify this. |
paulb@34 | 452 | |
paulb@53 | 453 | if getattr(target, "returns_value", 0): |
paulb@49 | 454 | self.namespace.set_types(self.last_returns) |
paulb@34 | 455 | self.annotate(invoke) |
paulb@34 | 456 | |
paulb@34 | 457 | if getattr(invoke, "same_frame", 0): |
paulb@45 | 458 | for locals in self.returned_locals: |
paulb@34 | 459 | self.namespace.merge_namespace(locals) |
paulb@34 | 460 | |
paulb@42 | 461 | # Remember the state of the system. |
paulb@42 | 462 | |
paulb@42 | 463 | invoke.syscount = self.system.count |
paulb@42 | 464 | |
paulb@34 | 465 | def make_items(self, invocation, subprogram, context): |
paulb@34 | 466 | |
paulb@34 | 467 | """ |
paulb@34 | 468 | Make an items mapping for the 'invocation' of the 'subprogram' using the |
paulb@34 | 469 | given 'context' (which may be None). |
paulb@34 | 470 | """ |
paulb@34 | 471 | |
paulb@34 | 472 | if context is not None: |
paulb@53 | 473 | args = [Self(context)] + invocation.args |
paulb@34 | 474 | else: |
paulb@34 | 475 | args = invocation.args |
paulb@34 | 476 | |
paulb@24 | 477 | params = subprogram.params |
paulb@24 | 478 | items = [] |
paulb@24 | 479 | keywords = {} |
paulb@24 | 480 | |
paulb@24 | 481 | # Process the specified arguments. |
paulb@24 | 482 | |
paulb@24 | 483 | for arg in args: |
paulb@24 | 484 | if isinstance(arg, Keyword): |
paulb@24 | 485 | keywords[arg.name] = arg.expr |
paulb@24 | 486 | continue |
paulb@24 | 487 | elif params: |
paulb@24 | 488 | param, default = params[0] |
paulb@24 | 489 | if arg is None: |
paulb@24 | 490 | arg = self.dispatch(default) |
paulb@24 | 491 | else: |
paulb@24 | 492 | raise TypeError, "Invocation has too many arguments." |
paulb@24 | 493 | items.append((param, arg.types)) |
paulb@24 | 494 | params = params[1:] |
paulb@24 | 495 | |
paulb@24 | 496 | # Collect the remaining defaults. |
paulb@24 | 497 | |
paulb@24 | 498 | while params: |
paulb@24 | 499 | param, default = params[0] |
paulb@24 | 500 | if keywords.has_key(param): |
paulb@24 | 501 | arg = keywords[param] |
paulb@24 | 502 | else: |
paulb@24 | 503 | arg = self.dispatch(default) |
paulb@24 | 504 | items.append((param, arg.types)) |
paulb@24 | 505 | params = params[1:] |
paulb@24 | 506 | |
paulb@24 | 507 | # Add star and dstar. |
paulb@24 | 508 | |
paulb@24 | 509 | if invocation.star is not None: |
paulb@24 | 510 | if subprogram.star is not None: |
paulb@24 | 511 | param, default = subprogram.star |
paulb@24 | 512 | items.append((param, invocation.star.types)) |
paulb@24 | 513 | else: |
paulb@24 | 514 | raise TypeError, "Invocation provides unwanted *args." |
paulb@24 | 515 | elif subprogram.star is not None: |
paulb@24 | 516 | param, default = subprogram.star |
paulb@24 | 517 | items.append((param, self.dispatch(default))) |
paulb@24 | 518 | |
paulb@24 | 519 | if invocation.dstar is not None: |
paulb@24 | 520 | if subprogram.dstar is not None: |
paulb@24 | 521 | param, default = subprogram.dstar |
paulb@24 | 522 | items.append((param, invocation.dstar.types)) |
paulb@24 | 523 | else: |
paulb@24 | 524 | raise TypeError, "Invocation provides unwanted **args." |
paulb@24 | 525 | elif subprogram.dstar is not None: |
paulb@24 | 526 | param, default = subprogram.dstar |
paulb@24 | 527 | items.append((param, self.dispatch(default))) |
paulb@24 | 528 | |
paulb@24 | 529 | return items |
paulb@24 | 530 | |
paulb@24 | 531 | def make_namespace(self, items): |
paulb@24 | 532 | namespace = Namespace() |
paulb@24 | 533 | namespace.merge_items(items) |
paulb@24 | 534 | return namespace |
paulb@24 | 535 | |
paulb@48 | 536 | # Namespace-related abstractions. |
paulb@48 | 537 | |
paulb@48 | 538 | class Namespace: |
paulb@48 | 539 | |
paulb@48 | 540 | """ |
paulb@48 | 541 | A local namespace which may either relate to a genuine set of function |
paulb@50 | 542 | locals or the initialisation of a structure or module. |
paulb@48 | 543 | """ |
paulb@48 | 544 | |
paulb@48 | 545 | def __init__(self): |
paulb@50 | 546 | |
paulb@50 | 547 | """ |
paulb@50 | 548 | Initialise the namespace with a mapping of local names to possible |
paulb@50 | 549 | types, a list of return values and of possible returned local |
paulb@50 | 550 | namespaces. The namespace also tracks the "current" types and a mapping |
paulb@50 | 551 | of temporary value names to types. |
paulb@50 | 552 | """ |
paulb@50 | 553 | |
paulb@48 | 554 | self.names = {} |
paulb@49 | 555 | self.returns = [] |
paulb@49 | 556 | self.return_locals = [] |
paulb@49 | 557 | self.temp = {} |
paulb@49 | 558 | self.types = [] |
paulb@49 | 559 | |
paulb@49 | 560 | def set_types(self, types): |
paulb@49 | 561 | self.types = types |
paulb@48 | 562 | |
paulb@48 | 563 | def store(self, name, types): |
paulb@48 | 564 | self.names[name] = types |
paulb@48 | 565 | |
paulb@48 | 566 | def load(self, name): |
paulb@48 | 567 | return self.names[name] |
paulb@48 | 568 | |
paulb@48 | 569 | def merge(self, name, types): |
paulb@48 | 570 | if not self.names.has_key(name): |
paulb@48 | 571 | self.names[name] = types[:] |
paulb@48 | 572 | else: |
paulb@48 | 573 | existing = self.names[name] |
paulb@48 | 574 | for type in types: |
paulb@48 | 575 | if type not in existing: |
paulb@48 | 576 | existing.append(type) |
paulb@48 | 577 | |
paulb@48 | 578 | def merge_namespace(self, namespace): |
paulb@48 | 579 | self.merge_items(namespace.names.items()) |
paulb@48 | 580 | |
paulb@48 | 581 | def merge_items(self, items): |
paulb@48 | 582 | for name, types in items: |
paulb@48 | 583 | self.merge(name, types) |
paulb@48 | 584 | |
paulb@49 | 585 | def snapshot(self): |
paulb@49 | 586 | |
paulb@49 | 587 | "Make a snapshot of the locals and remember them." |
paulb@49 | 588 | |
paulb@49 | 589 | namespace = Namespace() |
paulb@49 | 590 | namespace.merge_namespace(self) |
paulb@49 | 591 | self.return_locals.append(namespace) |
paulb@49 | 592 | |
paulb@48 | 593 | def __repr__(self): |
paulb@48 | 594 | return repr(self.names) |
paulb@48 | 595 | |
paulb@48 | 596 | class Attribute: |
paulb@48 | 597 | |
paulb@48 | 598 | """ |
paulb@48 | 599 | An attribute abstraction, indicating the type of the attribute along with |
paulb@48 | 600 | its context or origin. |
paulb@48 | 601 | """ |
paulb@48 | 602 | |
paulb@48 | 603 | def __init__(self, context, type): |
paulb@48 | 604 | self.context = context |
paulb@48 | 605 | self.type = type |
paulb@48 | 606 | |
paulb@48 | 607 | def __eq__(self, other): |
paulb@48 | 608 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@48 | 609 | |
paulb@48 | 610 | def __repr__(self): |
paulb@48 | 611 | return "Attribute of type %s (context %s)" % (self.type, self.context) |
paulb@48 | 612 | |
paulb@53 | 613 | class Self: |
paulb@53 | 614 | def __init__(self, attribute): |
paulb@53 | 615 | self.types = [attribute] |
paulb@53 | 616 | |
paulb@48 | 617 | def find_attributes(structure, name): |
paulb@48 | 618 | |
paulb@48 | 619 | """ |
paulb@48 | 620 | Find for the given 'structure' all attributes for the given 'name', visiting |
paulb@48 | 621 | base classes where appropriate and returning the attributes in order of |
paulb@48 | 622 | descending precedence for all possible base classes. |
paulb@48 | 623 | |
paulb@48 | 624 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 625 | the structure involved in accessing the attribute. |
paulb@48 | 626 | """ |
paulb@48 | 627 | |
paulb@48 | 628 | # First attempt to search the instance/class namespace. |
paulb@48 | 629 | |
paulb@48 | 630 | try: |
paulb@48 | 631 | l = structure.namespace.load(name) |
paulb@48 | 632 | attributes = [] |
paulb@48 | 633 | for attribute in l: |
paulb@48 | 634 | attributes.append((attribute, structure)) |
paulb@48 | 635 | |
paulb@48 | 636 | # If that does not work, attempt to investigate any class or base classes. |
paulb@48 | 637 | |
paulb@48 | 638 | except KeyError: |
paulb@48 | 639 | attributes = [] |
paulb@48 | 640 | |
paulb@48 | 641 | # Investigate any instance's implementing class. |
paulb@48 | 642 | |
paulb@48 | 643 | if isinstance(structure, Instance): |
paulb@48 | 644 | for cls in structure.namespace.load("__class__"): |
paulb@50 | 645 | l = get_attributes(cls, name) |
paulb@48 | 646 | for attribute in l: |
paulb@48 | 647 | if attribute not in attributes: |
paulb@48 | 648 | attributes.append(attribute) |
paulb@48 | 649 | |
paulb@48 | 650 | # Investigate any class's base classes. |
paulb@48 | 651 | |
paulb@48 | 652 | elif isinstance(structure, Class): |
paulb@48 | 653 | |
paulb@48 | 654 | # If no base classes exist, return an indicator that no attribute |
paulb@48 | 655 | # exists. |
paulb@48 | 656 | |
paulb@48 | 657 | if not structure.base_refs: |
paulb@48 | 658 | return [(None, structure)] |
paulb@48 | 659 | |
paulb@48 | 660 | # Otherwise, find all possible base classes. |
paulb@48 | 661 | |
paulb@48 | 662 | for base_refs in structure.base_refs: |
paulb@48 | 663 | base_attributes = [] |
paulb@48 | 664 | |
paulb@48 | 665 | # For each base class, find attributes either in the base |
paulb@48 | 666 | # class or its own base classes. |
paulb@48 | 667 | |
paulb@48 | 668 | for base_ref in base_refs: |
paulb@50 | 669 | l = get_attributes(base_ref, name) |
paulb@48 | 670 | for attribute in l: |
paulb@48 | 671 | if attribute not in base_attributes: |
paulb@48 | 672 | base_attributes.append(attribute) |
paulb@48 | 673 | |
paulb@48 | 674 | attributes += base_attributes |
paulb@48 | 675 | |
paulb@49 | 676 | return attributes |
paulb@48 | 677 | |
paulb@48 | 678 | def get_attributes(structure, name): |
paulb@48 | 679 | |
paulb@48 | 680 | """ |
paulb@48 | 681 | Return all possible attributes for the given 'structure' having the given |
paulb@48 | 682 | 'name', wrapping each attribute in an Attribute object which includes |
paulb@48 | 683 | context information for the attribute access. |
paulb@48 | 684 | |
paulb@48 | 685 | The elements in the result list are 2-tuples which contain the attribute and |
paulb@48 | 686 | the structure involved in accessing the attribute. |
paulb@48 | 687 | """ |
paulb@48 | 688 | |
paulb@48 | 689 | if isinstance(structure, Attribute): |
paulb@48 | 690 | structure = structure.type |
paulb@53 | 691 | results = [] |
paulb@53 | 692 | for attribute, accessor in find_attributes(structure, name): |
paulb@53 | 693 | if attribute is not None and isinstance(structure, Structure): |
paulb@53 | 694 | results.append((Attribute(structure, attribute.type), accessor)) |
paulb@53 | 695 | else: |
paulb@53 | 696 | results.append((attribute, accessor)) |
paulb@53 | 697 | return results |
paulb@48 | 698 | |
paulb@19 | 699 | # vim: tabstop=4 expandtab shiftwidth=4 |