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