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