paulb@213 | 1 | #!/usr/bin/env python |
paulb@213 | 2 | |
paulb@213 | 3 | """ |
paulb@213 | 4 | Simplified program nodes for easier type propagation and analysis. This module |
paulb@213 | 5 | contains nodes representing program instructions or operations, program |
paulb@213 | 6 | structure or organisation, and abstract program data. |
paulb@213 | 7 | |
paulb@213 | 8 | Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@213 | 9 | |
paulb@260 | 10 | This program is free software; you can redistribute it and/or modify it under |
paulb@260 | 11 | the terms of the GNU General Public License as published by the Free Software |
paulb@260 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paulb@260 | 13 | version. |
paulb@213 | 14 | |
paulb@260 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@260 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@260 | 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paulb@260 | 18 | details. |
paulb@213 | 19 | |
paulb@260 | 20 | You should have received a copy of the GNU General Public License along with |
paulb@260 | 21 | this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@213 | 22 | """ |
paulb@213 | 23 | |
paulb@244 | 24 | from simplify.simplified.utils import Structure, WithName, name, Namespace |
paulb@213 | 25 | import sys |
paulb@225 | 26 | import operator |
paulb@213 | 27 | |
paulb@213 | 28 | # Simplified program nodes. |
paulb@213 | 29 | |
paulb@213 | 30 | class Node: |
paulb@213 | 31 | |
paulb@213 | 32 | """ |
paulb@213 | 33 | A result node with common attributes: |
paulb@213 | 34 | |
paulb@213 | 35 | original The original node from which this node was created. |
paulb@213 | 36 | defining Whether the node defines something in the original program. |
paulb@213 | 37 | name Any name involved (variable or attribute). |
paulb@213 | 38 | index Any index involved (temporary variable name). |
paulb@213 | 39 | value Any constant value. |
paulb@213 | 40 | ref Any reference to (for example) subprograms. |
paulb@213 | 41 | nstype Any indication of the namespace type involved in a name access. |
paulb@213 | 42 | |
paulb@213 | 43 | Expression-related attributes: |
paulb@213 | 44 | |
paulb@213 | 45 | expr Any contributing expression. |
paulb@213 | 46 | lvalue Any target expression. |
paulb@213 | 47 | test Any test expression in a conditional instruction. |
paulb@213 | 48 | |
paulb@213 | 49 | Invocation and subprogram attributes: |
paulb@213 | 50 | |
paulb@213 | 51 | args Any collection of argument nodes. |
paulb@213 | 52 | params Any collection of parameter nodes and defaults. |
paulb@213 | 53 | |
paulb@219 | 54 | Tuple construction attributes: |
paulb@219 | 55 | |
paulb@219 | 56 | nodes Any expressions used to initialise a tuple |
paulb@219 | 57 | |
paulb@213 | 58 | Statement-grouping attributes: |
paulb@213 | 59 | |
paulb@213 | 60 | body Any conditional code depending on the success of a test. |
paulb@213 | 61 | else_ Any conditional code depending on the failure of a test. |
paulb@213 | 62 | handler Any exception handler code. |
paulb@213 | 63 | finally_ Any code which will be executed regardless. |
paulb@213 | 64 | code Any unconditional code. |
paulb@213 | 65 | choices Any choices which may be included in the final program. |
paulb@213 | 66 | """ |
paulb@213 | 67 | |
paul@281 | 68 | common_attributes = ("name", "index", "value", "nstype", "internal", |
paul@281 | 69 | "returns_value", "is_method", "ref", "module", "structures", "original") |
paul@281 | 70 | |
paulb@213 | 71 | expression_attributes = "expr", "lvalue", "test" |
paul@281 | 72 | |
paulb@213 | 73 | argument_attributes = "star", "dstar" |
paul@281 | 74 | |
paulb@213 | 75 | invocation_attributes = "params", # not "args" - see "pos_args", "kw_args" |
paul@281 | 76 | |
paul@281 | 77 | grouping_attributes = ("code", "body", "else_", "handler", "finally_", |
paul@281 | 78 | "choices", "nodes") |
paulb@213 | 79 | |
paulb@213 | 80 | def __init__(self, original=None, defining=0, **kw): |
paulb@213 | 81 | |
paulb@213 | 82 | """ |
paulb@213 | 83 | Initialise a program node with a link to an optional 'original' AST |
paulb@213 | 84 | node. An optional 'defining' parameter (if set to a true value), sets |
paulb@213 | 85 | this node as the defining node in the original. |
paulb@213 | 86 | """ |
paulb@213 | 87 | |
paulb@213 | 88 | self.original = original |
paulb@213 | 89 | self.defining = defining |
paulb@213 | 90 | self.copies = {} |
paulb@213 | 91 | |
paulb@213 | 92 | if self.original is not None and defining: |
paulb@213 | 93 | self.original._node = self |
paulb@213 | 94 | for name, value in kw.items(): |
paulb@213 | 95 | setattr(self, name, value) |
paulb@213 | 96 | |
paulb@213 | 97 | # Annotations. |
paulb@213 | 98 | |
paulb@213 | 99 | self.types = set() |
paulb@225 | 100 | self.annotated = 0 |
paulb@213 | 101 | |
paulb@233 | 102 | def __hash__(self): |
paulb@233 | 103 | return id(self) |
paulb@233 | 104 | |
paulb@213 | 105 | def __repr__(self): |
paulb@213 | 106 | |
paulb@213 | 107 | "Return a readable representation." |
paulb@213 | 108 | |
paulb@213 | 109 | if hasattr(self, "full_name"): |
paulb@213 | 110 | s = "%s '%s'" % (self.__class__.__name__, self.full_name()) |
paulb@213 | 111 | elif hasattr(self, "name"): |
paulb@213 | 112 | s = "%s '%s'" % (self.__class__.__name__, self.name) |
paulb@213 | 113 | elif hasattr(self, "index"): |
paulb@213 | 114 | s = "%s (%s)" % (self.__class__.__name__, self.index) |
paulb@213 | 115 | elif hasattr(self, "value"): |
paulb@213 | 116 | s = "%s %s" % (self.__class__.__name__, repr(self.value)) |
paulb@213 | 117 | elif hasattr(self, "ref"): |
paulb@213 | 118 | s = "%s '%s'" % (self.__class__.__name__, name(self.ref, self.ref.name)) |
paulb@213 | 119 | else: |
paulb@213 | 120 | s = "%s" % (self.__class__.__name__,) |
paulb@213 | 121 | |
paulb@213 | 122 | # Annotations. |
paulb@213 | 123 | |
paulb@213 | 124 | if self.types: |
paulb@213 | 125 | return "%s -> %s" % (s, self.types) |
paulb@213 | 126 | else: |
paulb@213 | 127 | return s |
paulb@213 | 128 | |
paulb@213 | 129 | def _pprint(self, indent, continuation, s, stream=None): |
paulb@213 | 130 | |
paulb@213 | 131 | """ |
paulb@213 | 132 | Print, at the given 'indent' level, with the given 'continuation' text, |
paulb@213 | 133 | the string 's', either to the given, optional 'stream' or to standard |
paulb@213 | 134 | output, this node's "pretty" representation. |
paulb@213 | 135 | """ |
paulb@213 | 136 | |
paulb@213 | 137 | stream = stream or sys.stdout |
paulb@213 | 138 | if continuation: |
paulb@213 | 139 | print >>stream, (" " * max(0, indent - len(continuation))) + continuation + s |
paulb@213 | 140 | else: |
paulb@213 | 141 | print >>stream, (" " * indent) + s |
paulb@213 | 142 | |
paulb@213 | 143 | def pprint(self, indent=0, continuation=None, stream=None): |
paulb@213 | 144 | |
paulb@213 | 145 | """ |
paulb@213 | 146 | Print, at the given, optional 'indent', with the given optional |
paulb@213 | 147 | 'continuation' text, either to the given, optional 'stream' or to |
paulb@213 | 148 | standard output, this node's "pretty" representation along with its |
paulb@213 | 149 | children and their "pretty" representation (and so on). |
paulb@213 | 150 | """ |
paulb@213 | 151 | |
paulb@213 | 152 | stream = stream or sys.stdout |
paulb@213 | 153 | self._pprint(indent, continuation, repr(self), stream) |
paulb@213 | 154 | |
paulb@213 | 155 | # Subprogram-related details. |
paulb@213 | 156 | |
paulb@213 | 157 | if hasattr(self, "params"): |
paulb@213 | 158 | for name, default in self.params: |
paulb@213 | 159 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@213 | 160 | if hasattr(self, "star") and self.star: |
paulb@213 | 161 | name, default = self.star |
paulb@213 | 162 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@213 | 163 | if hasattr(self, "dstar") and self.dstar: |
paulb@213 | 164 | name, default = self.dstar |
paulb@213 | 165 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@213 | 166 | if getattr(self, "internal", 0): |
paulb@213 | 167 | self._pprint(indent + 2, "( ", "internal", stream=stream) |
paulb@213 | 168 | if getattr(self, "structure", 0): |
paulb@213 | 169 | self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name, stream=stream) |
paulb@213 | 170 | |
paulb@213 | 171 | # Expression-related details. |
paulb@213 | 172 | |
paulb@213 | 173 | if hasattr(self, "expr"): |
paulb@213 | 174 | self.expr.pprint(indent + 2, "- ", stream=stream) |
paulb@213 | 175 | if hasattr(self, "nodes"): |
paulb@213 | 176 | for node in self.nodes: |
paulb@213 | 177 | node.pprint(indent + 2, "- ", stream=stream) |
paulb@213 | 178 | if hasattr(self, "lvalue"): |
paulb@213 | 179 | self.lvalue.pprint(indent + 2, "->", stream=stream) |
paulb@213 | 180 | if hasattr(self, "nstype"): |
paulb@213 | 181 | self._pprint(indent + 2, "", self.nstype, stream=stream) |
paulb@213 | 182 | if hasattr(self, "args"): |
paulb@213 | 183 | for arg in self.pos_args: |
paulb@213 | 184 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@213 | 185 | for name, arg in self.kw_args.items(): |
paulb@213 | 186 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@213 | 187 | if hasattr(self, "star") and self.star: |
paulb@213 | 188 | self.star.pprint(indent + 2, "( ", stream=stream) |
paulb@213 | 189 | if hasattr(self, "dstar") and self.dstar: |
paulb@213 | 190 | self.dstar.pprint(indent + 2, "( ", stream=stream) |
paulb@213 | 191 | |
paulb@213 | 192 | # Statement-related details. |
paulb@213 | 193 | |
paulb@213 | 194 | if hasattr(self, "test"): |
paulb@213 | 195 | self.test.pprint(indent + 2, "? ", stream=stream) |
paulb@213 | 196 | for attr in self.grouping_attributes: |
paulb@213 | 197 | if hasattr(self, attr) and getattr(self, attr): |
paulb@213 | 198 | self._pprint(indent, "", "%s {" % attr, stream=stream) |
paulb@213 | 199 | for node in getattr(self, attr): |
paulb@213 | 200 | node.pprint(indent + 2, stream=stream) |
paulb@213 | 201 | self._pprint(indent, "", "}", stream=stream) |
paulb@213 | 202 | |
paulb@213 | 203 | # Annotations. |
paulb@213 | 204 | |
paulb@213 | 205 | if hasattr(self, "accesses"): |
paulb@213 | 206 | self._pprint(indent, "", "--------", stream=stream) |
paulb@213 | 207 | for ref, attributes in self.accesses.items(): |
paul@281 | 208 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, |
paul@281 | 209 | ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes]) |
paul@281 | 210 | ), stream=stream) |
paulb@213 | 211 | self._pprint(indent, "", "--------", stream=stream) |
paulb@213 | 212 | if hasattr(self, "writes"): |
paulb@213 | 213 | self._pprint(indent, "", "--------", stream=stream) |
paulb@213 | 214 | for ref, attribute in self.writes.items(): |
paulb@213 | 215 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream) |
paulb@213 | 216 | self._pprint(indent, "", "--------", stream=stream) |
paulb@213 | 217 | |
paulb@213 | 218 | # Node discovery functions. |
paulb@213 | 219 | |
paulb@213 | 220 | def active(self): |
paulb@213 | 221 | |
paulb@213 | 222 | "Return the active copies of this node or a list containing this node." |
paulb@213 | 223 | |
paulb@213 | 224 | return self.copies.values() or [self] |
paulb@213 | 225 | |
paulb@225 | 226 | def is_annotated(self): |
paulb@225 | 227 | |
paulb@225 | 228 | """ |
paulb@225 | 229 | Return whether active copies of this node (or this node itself) is |
paulb@225 | 230 | annotated. |
paulb@225 | 231 | """ |
paulb@225 | 232 | |
paulb@225 | 233 | return reduce(operator.or_, [n.annotated for n in self.active()]) |
paulb@225 | 234 | |
paulb@213 | 235 | # Node manipulation functions. |
paulb@213 | 236 | |
paulb@213 | 237 | def copy(self, instance=None, new_name=None): |
paulb@213 | 238 | |
paulb@213 | 239 | """ |
paulb@213 | 240 | Perform a deep copy of the node, optionally specifying the 'instance' |
paulb@213 | 241 | for whom the copy has been requested and a 'new_name' for the copied |
paulb@213 | 242 | node. Return new unannotated copies of the node and its descendants. |
paulb@213 | 243 | """ |
paulb@213 | 244 | |
paulb@213 | 245 | # Copy the common attributes of this node. |
paulb@213 | 246 | |
paulb@213 | 247 | common = {} |
paulb@213 | 248 | for attr in self.common_attributes: |
paulb@213 | 249 | if hasattr(self, attr): |
paulb@213 | 250 | common[attr] = getattr(self, attr) |
paulb@213 | 251 | |
paulb@213 | 252 | # Add new attributes specially for copies. |
paulb@213 | 253 | |
paulb@213 | 254 | common["instance"] = instance |
paulb@213 | 255 | |
paulb@213 | 256 | if new_name is not None: |
paulb@213 | 257 | common["copy_of"] = self |
paulb@213 | 258 | common["name"] = new_name |
paulb@213 | 259 | |
paulb@213 | 260 | # Instantiate the copy, avoiding side-effects with original and defining. |
paulb@213 | 261 | |
paulb@213 | 262 | node = self.__class__(**common) |
paulb@213 | 263 | node.defining = self.defining |
paulb@213 | 264 | |
paulb@213 | 265 | # Add links to copies from originals. |
paulb@213 | 266 | |
paulb@213 | 267 | self.copies[instance] = node |
paulb@213 | 268 | |
paulb@213 | 269 | # Copy attributes of different types. |
paulb@213 | 270 | |
paulb@213 | 271 | for attr in self.expression_attributes: |
paulb@213 | 272 | if hasattr(self, attr): |
paulb@213 | 273 | n = getattr(self, attr) |
paulb@213 | 274 | if n is None: |
paulb@213 | 275 | n2 = n |
paulb@213 | 276 | else: |
paulb@213 | 277 | n2 = n.copy(instance) |
paulb@213 | 278 | setattr(node, attr, n2) |
paulb@213 | 279 | |
paulb@213 | 280 | for attr in self.argument_attributes: |
paulb@213 | 281 | if hasattr(self, attr): |
paulb@213 | 282 | t = getattr(self, attr) |
paulb@213 | 283 | if t is None: |
paulb@213 | 284 | t2 = t |
paulb@213 | 285 | else: |
paulb@213 | 286 | name, n = t |
paulb@213 | 287 | n2 = n.copy(instance) |
paulb@213 | 288 | t2 = name, n2 |
paulb@213 | 289 | setattr(node, attr, t2) |
paulb@213 | 290 | |
paulb@213 | 291 | for attr in self.invocation_attributes: |
paulb@213 | 292 | if hasattr(self, attr): |
paulb@213 | 293 | l = getattr(self, attr) |
paulb@213 | 294 | l2 = [] |
paulb@213 | 295 | for name, n in l: |
paulb@213 | 296 | if n is None: |
paulb@213 | 297 | l2.append((name, n)) |
paulb@213 | 298 | else: |
paulb@213 | 299 | l2.append((name, n.copy(instance))) |
paulb@213 | 300 | setattr(node, attr, l2) |
paulb@213 | 301 | |
paulb@213 | 302 | for attr in self.grouping_attributes: |
paulb@213 | 303 | if hasattr(self, attr): |
paulb@213 | 304 | l = getattr(self, attr) |
paulb@213 | 305 | setattr(node, attr, [n.copy(instance) for n in l]) |
paulb@213 | 306 | |
paulb@213 | 307 | # Arguments are usually processed further - "args" is useless. |
paulb@213 | 308 | |
paulb@213 | 309 | if hasattr(self, "pos_args"): |
paulb@213 | 310 | node.pos_args = [n.copy(instance) for n in self.pos_args] |
paulb@213 | 311 | |
paulb@213 | 312 | if hasattr(self, "kw_args"): |
paulb@213 | 313 | node.kw_args = {} |
paulb@213 | 314 | for name, n in self.kw_args.items(): |
paulb@213 | 315 | node.kw_args[name] = n.copy(instance) |
paulb@213 | 316 | |
paulb@213 | 317 | return node |
paulb@213 | 318 | |
paulb@213 | 319 | # These are the supported "operations" described by simplified program nodes. |
paulb@213 | 320 | |
paulb@213 | 321 | class Pass(Node): "A placeholder node corresponding to pass." |
paulb@213 | 322 | class Assign(Node): "A grouping node for assignment-related operations." |
paulb@213 | 323 | class Keyword(Node): "A grouping node for keyword arguments." |
paulb@213 | 324 | class Global(Node): "A global name designator." |
paulb@213 | 325 | class Import(Node): "A module import operation." |
paulb@213 | 326 | class LoadTemp(Node): "Load a previously-stored temporary value." |
paulb@213 | 327 | class LoadName(Node): "Load a named object." |
paulb@213 | 328 | class LoadAttr(Node): "Load an object attribute." |
paulb@213 | 329 | class LoadRef(Node): "Load a reference, typically a subprogram or a constant." |
paulb@213 | 330 | class LoadExc(Node): "Load a handled exception." |
paulb@213 | 331 | class ResetExc(Node): "Reset the exception state." |
paulb@213 | 332 | class StoreTemp(Node): "Store a temporary value." |
paulb@213 | 333 | class StoreName(Node): "Associate a name with an object." |
paulb@213 | 334 | class StoreAttr(Node): "Associate an object's attribute with a value." |
paulb@213 | 335 | class ReleaseTemp(Node): "Release a temporary value." |
paulb@213 | 336 | class Try(Node): "A try...except...else...finally grouping node." |
paulb@213 | 337 | class Not(Node): "A negation of an expression." |
paulb@213 | 338 | class CheckType(Node): "Check a value's type from a list of choices." |
paulb@213 | 339 | class Return(Node): "Return an evaluated expression." |
paulb@213 | 340 | class Invoke(Node): "An invocation." |
paulb@219 | 341 | class MakeTuple(Node): "Make a tuple object." |
paulb@213 | 342 | |
paulb@274 | 343 | class Constant(Node): |
paulb@274 | 344 | |
paulb@274 | 345 | "A constant initialised with a type name for future processing." |
paulb@274 | 346 | |
paulb@274 | 347 | def __init__(self, original=None, defining=0, name=None, value=None, *args, **kw): |
paulb@274 | 348 | Node.__init__(self, original, defining, *args, **kw) |
paulb@274 | 349 | self.name = name |
paulb@274 | 350 | self.value = value |
paulb@274 | 351 | self.typename = self.value.__class__.__name__ |
paulb@274 | 352 | |
paulb@213 | 353 | # There are two types of return node: return from function and return from |
paulb@213 | 354 | # block. |
paulb@213 | 355 | |
paulb@213 | 356 | class ReturnFromFunction(Return): |
paulb@213 | 357 | pass |
paulb@213 | 358 | |
paulb@213 | 359 | class ReturnFromBlock(Return): |
paulb@213 | 360 | pass |
paulb@213 | 361 | |
paulb@213 | 362 | # NOTE: Not actually supported. |
paulb@213 | 363 | # Additionally, yield statements act like return statements for the purposes |
paulb@213 | 364 | # of this system. |
paulb@213 | 365 | |
paulb@213 | 366 | class Yield(ReturnFromFunction): |
paulb@213 | 367 | pass |
paulb@213 | 368 | |
paulb@213 | 369 | # Some behaviour is set as the default in conditional nodes but may be |
paulb@213 | 370 | # overridden. |
paulb@213 | 371 | |
paulb@213 | 372 | class Conditional(Node): |
paulb@213 | 373 | |
paulb@213 | 374 | "A conditional node consisting of a test and outcomes." |
paulb@213 | 375 | |
paulb@213 | 376 | def __init__(self, *args, **kw): |
paulb@213 | 377 | self.isolate_test = 0 |
paulb@213 | 378 | Node.__init__(self, *args, **kw) |
paulb@213 | 379 | |
paulb@213 | 380 | # Invocations involve some more work to process calculated attributes. |
paulb@213 | 381 | |
paulb@268 | 382 | class Raise(Invoke): |
paulb@268 | 383 | |
paulb@268 | 384 | "An exception raising node which may behave like an invocation." |
paulb@268 | 385 | |
paulb@268 | 386 | def __init__(self, original=None, defining=0, expr=None, traceback=None, **kw): |
paulb@268 | 387 | |
paulb@268 | 388 | """ |
paulb@268 | 389 | Initialise the invocation with the following optional parameters: |
paulb@268 | 390 | |
paulb@268 | 391 | * The 'original' AST node represented by this invocation. |
paulb@268 | 392 | * Whether this invocation is 'defining' (false by default). |
paulb@268 | 393 | * The 'expr' or expression indicating the invoked subprogram. |
paulb@268 | 394 | * The 'traceback' associated with the raised exception. |
paulb@268 | 395 | """ |
paulb@268 | 396 | |
paulb@268 | 397 | Invoke.__init__(self, original, defining, expr=expr, traceback=traceback, **kw) |
paulb@268 | 398 | self.share_locals = 0 |
paulb@274 | 399 | self.consumed_args = {} |
paulb@268 | 400 | self.raises = set() |
paulb@268 | 401 | |
paulb@213 | 402 | class InvokeFunction(Invoke): |
paulb@213 | 403 | |
paulb@213 | 404 | "A function or method invocation." |
paulb@213 | 405 | |
paulb@267 | 406 | def __init__(self, original=None, defining=0, expr=None, args=None, star=None, dstar=None, **kw): |
paulb@267 | 407 | |
paulb@267 | 408 | """ |
paulb@267 | 409 | Initialise the invocation with the following optional parameters: |
paulb@267 | 410 | |
paulb@267 | 411 | * The 'original' AST node represented by this invocation. |
paulb@267 | 412 | * Whether this invocation is 'defining' (false by default). |
paulb@267 | 413 | * The 'expr' or expression indicating the invoked subprogram. |
paulb@267 | 414 | * The 'args' or arguments to be supplied, yielding the 'pos_args' and |
paulb@267 | 415 | 'kw_args' attributes on this object. |
paulb@267 | 416 | * The 'star' argument containing additional unlabelled arguments. |
paulb@267 | 417 | * The 'dstar' argument containing keyword arguments. |
paulb@267 | 418 | """ |
paulb@267 | 419 | |
paulb@267 | 420 | Invoke.__init__(self, original, defining, expr=expr, args=(args or []), star=star, dstar=dstar, **kw) |
paulb@213 | 421 | self.set_args(self.args) |
paulb@213 | 422 | self.share_locals = 0 |
paulb@272 | 423 | self.consumed_args = {} |
paulb@268 | 424 | self.raises = set() |
paulb@213 | 425 | |
paulb@213 | 426 | def set_args(self, args): |
paulb@213 | 427 | |
paulb@213 | 428 | "Sort the 'args' into positional and keyword arguments." |
paulb@213 | 429 | |
paulb@213 | 430 | self.pos_args = [] |
paulb@213 | 431 | self.kw_args = {} |
paulb@213 | 432 | add_kw = 0 |
paulb@213 | 433 | for arg in args: |
paulb@213 | 434 | if not add_kw: |
paulb@213 | 435 | if not isinstance(arg, Keyword): |
paulb@213 | 436 | self.pos_args.append(arg) |
paulb@213 | 437 | else: |
paulb@213 | 438 | add_kw = 1 |
paulb@213 | 439 | if add_kw: |
paulb@213 | 440 | if isinstance(arg, Keyword): |
paulb@213 | 441 | self.kw_args[arg.name] = arg.expr |
paulb@213 | 442 | else: |
paulb@213 | 443 | raise TypeError, "Positional argument appears after keyword arguments in '%s'." % self |
paulb@213 | 444 | |
paulb@213 | 445 | class InvokeRef(Invoke): |
paulb@213 | 446 | |
paulb@213 | 447 | "A block or loop invocation." |
paulb@213 | 448 | |
paulb@267 | 449 | def __init__(self, original=None, defining=0, ref=None, produces_result=1, share_locals=1, **kw): |
paulb@267 | 450 | |
paulb@267 | 451 | """ |
paulb@267 | 452 | Initialise the invocation with the following optional parameters: |
paulb@267 | 453 | |
paulb@267 | 454 | * The 'original' AST node represented by this invocation. |
paulb@267 | 455 | * Whether this invocation is 'defining' (false by default). |
paulb@267 | 456 | * The 'ref' indicating the subprogram to be invoked. |
paulb@267 | 457 | * Whether a result is produced as indicated by 'produces_result' (true |
paulb@267 | 458 | by default). |
paulb@267 | 459 | * Whether the subprogram shares the locals of the caller as indicated |
paulb@267 | 460 | by 'share_locals' (true by default). |
paulb@267 | 461 | """ |
paulb@267 | 462 | |
paulb@267 | 463 | Invoke.__init__(self, original, defining, ref=ref, produces_result=produces_result, share_locals=share_locals, **kw) |
paulb@268 | 464 | self.raises = set() |
paulb@213 | 465 | |
paulb@213 | 466 | # Program structure nodes. |
paulb@213 | 467 | |
paulb@213 | 468 | class Module(Node, Structure): |
paulb@213 | 469 | |
paulb@213 | 470 | "A Python module." |
paulb@213 | 471 | |
paulb@268 | 472 | def __init__(self, *args, **kw): |
paulb@268 | 473 | Node.__init__(self, *args, **kw) |
paulb@268 | 474 | self.structure = None |
paulb@269 | 475 | self.params = [] |
paulb@269 | 476 | self.star = None |
paulb@269 | 477 | self.dstar = None |
paulb@268 | 478 | |
paulb@213 | 479 | def full_name(self): |
paulb@213 | 480 | return "module %s" % self.name |
paulb@213 | 481 | |
paulb@244 | 482 | class Subprogram(Node, WithName, Structure): |
paulb@213 | 483 | |
paulb@213 | 484 | "A subprogram: functions, methods and loops." |
paulb@213 | 485 | |
paulb@267 | 486 | def __init__(self, original=None, defining=0, name=None, module=None, structure=None, |
paulb@268 | 487 | structures=None, internal=0, returns_value=1, is_method=0, params=None, star=None, |
paulb@267 | 488 | dstar=None, **kw): |
paulb@267 | 489 | |
paulb@267 | 490 | """ |
paulb@267 | 491 | Initialise the subprogram with the following optional parameters: |
paulb@267 | 492 | |
paulb@267 | 493 | * The 'original' AST node represented by this subprogram. |
paulb@267 | 494 | * Whether this subprogram is 'defining' or not (false by default). |
paulb@267 | 495 | * The 'name' of this subprogram which may be None. |
paulb@267 | 496 | * The 'module' in which this subprogram is found. |
paulb@267 | 497 | * The 'structure' initialised by this subprogram. |
paulb@267 | 498 | * The 'structures' within which this subprogram resides. |
paulb@267 | 499 | * The 'internal' status of this subprogram (false by default), which |
paulb@267 | 500 | if true typically means that a loop or operation is being |
paulb@267 | 501 | represented. |
paulb@267 | 502 | * Whether a value is returned, as specified by 'returns_value' (true |
paulb@267 | 503 | by default). |
paulb@268 | 504 | * Whether the subprogram is a method, as specified by 'is_method' |
paulb@268 | 505 | (false by default). |
paulb@267 | 506 | * The 'params' (a parameter list which is empty by default). |
paulb@267 | 507 | * The 'star' parameter which collects excess positional arguments. |
paulb@267 | 508 | * The 'dstar' parameter which collects unmatched keyword arguments. |
paulb@267 | 509 | """ |
paulb@267 | 510 | |
paulb@267 | 511 | Node.__init__(self, original, defining, name=name, module=module, structure=structure, |
paulb@267 | 512 | structures=structures, internal=internal, returns_value=returns_value, |
paulb@268 | 513 | is_method=is_method, params=(params or []), star=star, dstar=dstar, **kw) |
paulb@267 | 514 | |
paulb@213 | 515 | WithName.__init__(self) |
paulb@213 | 516 | self.raises = set() |
paulb@213 | 517 | self.returns = set() |
paulb@213 | 518 | self.return_locals = set() |
paulb@268 | 519 | self.paramtypes = {} |
paulb@244 | 520 | self.namespace = Namespace() # NOTE: Temporary. |
paulb@213 | 521 | |
paulb@270 | 522 | def specialisations(self): |
paulb@270 | 523 | |
paulb@270 | 524 | "Return the active specialisations using only distinct instances." |
paulb@270 | 525 | |
paulb@270 | 526 | distinct_instances = {} |
paulb@270 | 527 | |
paulb@270 | 528 | subprograms = set() |
paulb@270 | 529 | for instance, subprogram in self.copies.items() or [(None, self)]: |
paulb@270 | 530 | if instance is None: |
paulb@270 | 531 | subprograms.add(subprogram) |
paulb@270 | 532 | continue |
paulb@270 | 533 | |
paulb@270 | 534 | cls = instance.get_class() |
paulb@270 | 535 | if not distinct_instances.has_key(cls): |
paulb@270 | 536 | distinct_instances[cls] = cls.get_distinct_instances() |
paulb@270 | 537 | distinct_instance = distinct_instances[cls][instance] |
paulb@270 | 538 | if instance is distinct_instance: |
paulb@270 | 539 | subprograms.add(subprogram) |
paulb@270 | 540 | |
paulb@270 | 541 | return subprograms |
paulb@270 | 542 | |
paulb@213 | 543 | # vim: tabstop=4 expandtab shiftwidth=4 |