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