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@213 | 10 | This software is free software; you can redistribute it and/or |
paulb@213 | 11 | modify it under the terms of the GNU General Public License as |
paulb@213 | 12 | published by the Free Software Foundation; either version 2 of |
paulb@213 | 13 | the License, or (at your option) any later version. |
paulb@213 | 14 | |
paulb@213 | 15 | This software is distributed in the hope that it will be useful, |
paulb@213 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@213 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@213 | 18 | GNU General Public License for more details. |
paulb@213 | 19 | |
paulb@213 | 20 | You should have received a copy of the GNU General Public |
paulb@213 | 21 | License along with this library; see the file LICENCE.txt |
paulb@213 | 22 | If not, write to the Free Software Foundation, Inc., |
paulb@213 | 23 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@213 | 24 | """ |
paulb@213 | 25 | |
paulb@213 | 26 | from simplified.utils import Structure, WithName, name |
paulb@213 | 27 | import sys |
paulb@213 | 28 | |
paulb@213 | 29 | # Simplified program nodes. |
paulb@213 | 30 | |
paulb@213 | 31 | class Node: |
paulb@213 | 32 | |
paulb@213 | 33 | """ |
paulb@213 | 34 | A result node with common attributes: |
paulb@213 | 35 | |
paulb@213 | 36 | original The original node from which this node was created. |
paulb@213 | 37 | defining Whether the node defines something in the original program. |
paulb@213 | 38 | name Any name involved (variable or attribute). |
paulb@213 | 39 | index Any index involved (temporary variable name). |
paulb@213 | 40 | value Any constant value. |
paulb@213 | 41 | ref Any reference to (for example) subprograms. |
paulb@213 | 42 | nstype Any indication of the namespace type involved in a name access. |
paulb@213 | 43 | |
paulb@213 | 44 | Expression-related attributes: |
paulb@213 | 45 | |
paulb@213 | 46 | expr Any contributing expression. |
paulb@213 | 47 | lvalue Any target expression. |
paulb@213 | 48 | test Any test expression in a conditional instruction. |
paulb@213 | 49 | |
paulb@213 | 50 | Invocation and subprogram attributes: |
paulb@213 | 51 | |
paulb@213 | 52 | args Any collection of argument nodes. |
paulb@213 | 53 | params Any collection of parameter nodes and defaults. |
paulb@213 | 54 | |
paulb@213 | 55 | Statement-grouping attributes: |
paulb@213 | 56 | |
paulb@213 | 57 | body Any conditional code depending on the success of a test. |
paulb@213 | 58 | else_ Any conditional code depending on the failure of a test. |
paulb@213 | 59 | handler Any exception handler code. |
paulb@213 | 60 | finally_ Any code which will be executed regardless. |
paulb@213 | 61 | code Any unconditional code. |
paulb@213 | 62 | choices Any choices which may be included in the final program. |
paulb@213 | 63 | """ |
paulb@213 | 64 | |
paulb@213 | 65 | common_attributes = "name", "index", "value", "nstype", "internal", "returns_value", "is_method", "ref", "module", "structures", "original" |
paulb@213 | 66 | expression_attributes = "expr", "lvalue", "test" |
paulb@213 | 67 | argument_attributes = "star", "dstar" |
paulb@213 | 68 | invocation_attributes = "params", # not "args" - see "pos_args", "kw_args" |
paulb@213 | 69 | grouping_attributes = "code", "body", "else_", "handler", "finally_", "choices" |
paulb@213 | 70 | |
paulb@213 | 71 | def __init__(self, original=None, defining=0, **kw): |
paulb@213 | 72 | |
paulb@213 | 73 | """ |
paulb@213 | 74 | Initialise a program node with a link to an optional 'original' AST |
paulb@213 | 75 | node. An optional 'defining' parameter (if set to a true value), sets |
paulb@213 | 76 | this node as the defining node in the original. |
paulb@213 | 77 | """ |
paulb@213 | 78 | |
paulb@213 | 79 | self.original = original |
paulb@213 | 80 | self.defining = defining |
paulb@213 | 81 | self.copies = {} |
paulb@213 | 82 | |
paulb@213 | 83 | if self.original is not None and defining: |
paulb@213 | 84 | self.original._node = self |
paulb@213 | 85 | for name, value in kw.items(): |
paulb@213 | 86 | setattr(self, name, value) |
paulb@213 | 87 | |
paulb@213 | 88 | # Annotations. |
paulb@213 | 89 | |
paulb@213 | 90 | self.types = set() |
paulb@213 | 91 | |
paulb@213 | 92 | def __repr__(self): |
paulb@213 | 93 | |
paulb@213 | 94 | "Return a readable representation." |
paulb@213 | 95 | |
paulb@213 | 96 | if hasattr(self, "full_name"): |
paulb@213 | 97 | s = "%s '%s'" % (self.__class__.__name__, self.full_name()) |
paulb@213 | 98 | elif hasattr(self, "name"): |
paulb@213 | 99 | s = "%s '%s'" % (self.__class__.__name__, self.name) |
paulb@213 | 100 | elif hasattr(self, "index"): |
paulb@213 | 101 | s = "%s (%s)" % (self.__class__.__name__, self.index) |
paulb@213 | 102 | elif hasattr(self, "value"): |
paulb@213 | 103 | s = "%s %s" % (self.__class__.__name__, repr(self.value)) |
paulb@213 | 104 | elif hasattr(self, "ref"): |
paulb@213 | 105 | s = "%s '%s'" % (self.__class__.__name__, name(self.ref, self.ref.name)) |
paulb@213 | 106 | else: |
paulb@213 | 107 | s = "%s" % (self.__class__.__name__,) |
paulb@213 | 108 | |
paulb@213 | 109 | # Annotations. |
paulb@213 | 110 | |
paulb@213 | 111 | if self.types: |
paulb@213 | 112 | return "%s -> %s" % (s, self.types) |
paulb@213 | 113 | else: |
paulb@213 | 114 | return s |
paulb@213 | 115 | |
paulb@213 | 116 | def _pprint(self, indent, continuation, s, stream=None): |
paulb@213 | 117 | |
paulb@213 | 118 | """ |
paulb@213 | 119 | Print, at the given 'indent' level, with the given 'continuation' text, |
paulb@213 | 120 | the string 's', either to the given, optional 'stream' or to standard |
paulb@213 | 121 | output, this node's "pretty" representation. |
paulb@213 | 122 | """ |
paulb@213 | 123 | |
paulb@213 | 124 | stream = stream or sys.stdout |
paulb@213 | 125 | if continuation: |
paulb@213 | 126 | print >>stream, (" " * max(0, indent - len(continuation))) + continuation + s |
paulb@213 | 127 | else: |
paulb@213 | 128 | print >>stream, (" " * indent) + s |
paulb@213 | 129 | |
paulb@213 | 130 | def pprint(self, indent=0, continuation=None, stream=None): |
paulb@213 | 131 | |
paulb@213 | 132 | """ |
paulb@213 | 133 | Print, at the given, optional 'indent', with the given optional |
paulb@213 | 134 | 'continuation' text, either to the given, optional 'stream' or to |
paulb@213 | 135 | standard output, this node's "pretty" representation along with its |
paulb@213 | 136 | children and their "pretty" representation (and so on). |
paulb@213 | 137 | """ |
paulb@213 | 138 | |
paulb@213 | 139 | stream = stream or sys.stdout |
paulb@213 | 140 | self._pprint(indent, continuation, repr(self), stream) |
paulb@213 | 141 | |
paulb@213 | 142 | # Subprogram-related details. |
paulb@213 | 143 | |
paulb@213 | 144 | if hasattr(self, "params"): |
paulb@213 | 145 | for name, default in self.params: |
paulb@213 | 146 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@213 | 147 | if hasattr(self, "star") and self.star: |
paulb@213 | 148 | name, default = self.star |
paulb@213 | 149 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@213 | 150 | if hasattr(self, "dstar") and self.dstar: |
paulb@213 | 151 | name, default = self.dstar |
paulb@213 | 152 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@213 | 153 | if getattr(self, "internal", 0): |
paulb@213 | 154 | self._pprint(indent + 2, "( ", "internal", stream=stream) |
paulb@213 | 155 | if getattr(self, "structure", 0): |
paulb@213 | 156 | self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name, stream=stream) |
paulb@213 | 157 | |
paulb@213 | 158 | # Expression-related details. |
paulb@213 | 159 | |
paulb@213 | 160 | if hasattr(self, "expr"): |
paulb@213 | 161 | self.expr.pprint(indent + 2, "- ", stream=stream) |
paulb@213 | 162 | if hasattr(self, "nodes"): |
paulb@213 | 163 | for node in self.nodes: |
paulb@213 | 164 | node.pprint(indent + 2, "- ", stream=stream) |
paulb@213 | 165 | if hasattr(self, "lvalue"): |
paulb@213 | 166 | self.lvalue.pprint(indent + 2, "->", stream=stream) |
paulb@213 | 167 | if hasattr(self, "nstype"): |
paulb@213 | 168 | self._pprint(indent + 2, "", self.nstype, stream=stream) |
paulb@213 | 169 | if hasattr(self, "args"): |
paulb@213 | 170 | for arg in self.pos_args: |
paulb@213 | 171 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@213 | 172 | for name, arg in self.kw_args.items(): |
paulb@213 | 173 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@213 | 174 | if hasattr(self, "star") and self.star: |
paulb@213 | 175 | self.star.pprint(indent + 2, "( ", stream=stream) |
paulb@213 | 176 | if hasattr(self, "dstar") and self.dstar: |
paulb@213 | 177 | self.dstar.pprint(indent + 2, "( ", stream=stream) |
paulb@213 | 178 | |
paulb@213 | 179 | # Statement-related details. |
paulb@213 | 180 | |
paulb@213 | 181 | if hasattr(self, "test"): |
paulb@213 | 182 | self.test.pprint(indent + 2, "? ", stream=stream) |
paulb@213 | 183 | for attr in self.grouping_attributes: |
paulb@213 | 184 | if hasattr(self, attr) and getattr(self, attr): |
paulb@213 | 185 | self._pprint(indent, "", "%s {" % attr, stream=stream) |
paulb@213 | 186 | for node in getattr(self, attr): |
paulb@213 | 187 | node.pprint(indent + 2, stream=stream) |
paulb@213 | 188 | self._pprint(indent, "", "}", stream=stream) |
paulb@213 | 189 | |
paulb@213 | 190 | # Annotations. |
paulb@213 | 191 | |
paulb@213 | 192 | if hasattr(self, "accesses"): |
paulb@213 | 193 | self._pprint(indent, "", "--------", stream=stream) |
paulb@213 | 194 | for ref, attributes in self.accesses.items(): |
paulb@213 | 195 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes])), stream=stream) |
paulb@213 | 196 | self._pprint(indent, "", "--------", stream=stream) |
paulb@213 | 197 | if hasattr(self, "writes"): |
paulb@213 | 198 | self._pprint(indent, "", "--------", stream=stream) |
paulb@213 | 199 | for ref, attribute in self.writes.items(): |
paulb@213 | 200 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream) |
paulb@213 | 201 | self._pprint(indent, "", "--------", stream=stream) |
paulb@213 | 202 | |
paulb@213 | 203 | # Node discovery functions. |
paulb@213 | 204 | |
paulb@213 | 205 | def active(self): |
paulb@213 | 206 | |
paulb@213 | 207 | "Return the active copies of this node or a list containing this node." |
paulb@213 | 208 | |
paulb@213 | 209 | return self.copies.values() or [self] |
paulb@213 | 210 | |
paulb@213 | 211 | # Node manipulation functions. |
paulb@213 | 212 | |
paulb@213 | 213 | def copy(self, instance=None, new_name=None): |
paulb@213 | 214 | |
paulb@213 | 215 | """ |
paulb@213 | 216 | Perform a deep copy of the node, optionally specifying the 'instance' |
paulb@213 | 217 | for whom the copy has been requested and a 'new_name' for the copied |
paulb@213 | 218 | node. Return new unannotated copies of the node and its descendants. |
paulb@213 | 219 | """ |
paulb@213 | 220 | |
paulb@213 | 221 | # Copy the common attributes of this node. |
paulb@213 | 222 | |
paulb@213 | 223 | common = {} |
paulb@213 | 224 | for attr in self.common_attributes: |
paulb@213 | 225 | if hasattr(self, attr): |
paulb@213 | 226 | common[attr] = getattr(self, attr) |
paulb@213 | 227 | |
paulb@213 | 228 | # Add new attributes specially for copies. |
paulb@213 | 229 | |
paulb@213 | 230 | common["instance"] = instance |
paulb@213 | 231 | |
paulb@213 | 232 | if new_name is not None: |
paulb@213 | 233 | common["copy_of"] = self |
paulb@213 | 234 | common["name"] = new_name |
paulb@213 | 235 | |
paulb@213 | 236 | # Instantiate the copy, avoiding side-effects with original and defining. |
paulb@213 | 237 | |
paulb@213 | 238 | node = self.__class__(**common) |
paulb@213 | 239 | node.defining = self.defining |
paulb@213 | 240 | |
paulb@213 | 241 | # Add links to copies from originals. |
paulb@213 | 242 | |
paulb@213 | 243 | self.copies[instance] = node |
paulb@213 | 244 | |
paulb@213 | 245 | # Copy attributes of different types. |
paulb@213 | 246 | |
paulb@213 | 247 | for attr in self.expression_attributes: |
paulb@213 | 248 | if hasattr(self, attr): |
paulb@213 | 249 | n = getattr(self, attr) |
paulb@213 | 250 | if n is None: |
paulb@213 | 251 | n2 = n |
paulb@213 | 252 | else: |
paulb@213 | 253 | n2 = n.copy(instance) |
paulb@213 | 254 | setattr(node, attr, n2) |
paulb@213 | 255 | |
paulb@213 | 256 | for attr in self.argument_attributes: |
paulb@213 | 257 | if hasattr(self, attr): |
paulb@213 | 258 | t = getattr(self, attr) |
paulb@213 | 259 | if t is None: |
paulb@213 | 260 | t2 = t |
paulb@213 | 261 | else: |
paulb@213 | 262 | name, n = t |
paulb@213 | 263 | n2 = n.copy(instance) |
paulb@213 | 264 | t2 = name, n2 |
paulb@213 | 265 | setattr(node, attr, t2) |
paulb@213 | 266 | |
paulb@213 | 267 | for attr in self.invocation_attributes: |
paulb@213 | 268 | if hasattr(self, attr): |
paulb@213 | 269 | l = getattr(self, attr) |
paulb@213 | 270 | l2 = [] |
paulb@213 | 271 | for name, n in l: |
paulb@213 | 272 | if n is None: |
paulb@213 | 273 | l2.append((name, n)) |
paulb@213 | 274 | else: |
paulb@213 | 275 | l2.append((name, n.copy(instance))) |
paulb@213 | 276 | setattr(node, attr, l2) |
paulb@213 | 277 | |
paulb@213 | 278 | for attr in self.grouping_attributes: |
paulb@213 | 279 | if hasattr(self, attr): |
paulb@213 | 280 | l = getattr(self, attr) |
paulb@213 | 281 | setattr(node, attr, [n.copy(instance) for n in l]) |
paulb@213 | 282 | |
paulb@213 | 283 | # Arguments are usually processed further - "args" is useless. |
paulb@213 | 284 | |
paulb@213 | 285 | if hasattr(self, "pos_args"): |
paulb@213 | 286 | node.pos_args = [n.copy(instance) for n in self.pos_args] |
paulb@213 | 287 | |
paulb@213 | 288 | if hasattr(self, "kw_args"): |
paulb@213 | 289 | node.kw_args = {} |
paulb@213 | 290 | for name, n in self.kw_args.items(): |
paulb@213 | 291 | node.kw_args[name] = n.copy(instance) |
paulb@213 | 292 | |
paulb@213 | 293 | return node |
paulb@213 | 294 | |
paulb@213 | 295 | # These are the supported "operations" described by simplified program nodes. |
paulb@213 | 296 | |
paulb@213 | 297 | class Pass(Node): "A placeholder node corresponding to pass." |
paulb@213 | 298 | class Assign(Node): "A grouping node for assignment-related operations." |
paulb@213 | 299 | class Keyword(Node): "A grouping node for keyword arguments." |
paulb@213 | 300 | class Global(Node): "A global name designator." |
paulb@213 | 301 | class Import(Node): "A module import operation." |
paulb@213 | 302 | class LoadTemp(Node): "Load a previously-stored temporary value." |
paulb@213 | 303 | class LoadName(Node): "Load a named object." |
paulb@213 | 304 | class LoadAttr(Node): "Load an object attribute." |
paulb@213 | 305 | class LoadRef(Node): "Load a reference, typically a subprogram or a constant." |
paulb@213 | 306 | class LoadExc(Node): "Load a handled exception." |
paulb@213 | 307 | class ResetExc(Node): "Reset the exception state." |
paulb@213 | 308 | class StoreTemp(Node): "Store a temporary value." |
paulb@213 | 309 | class StoreName(Node): "Associate a name with an object." |
paulb@213 | 310 | class StoreAttr(Node): "Associate an object's attribute with a value." |
paulb@213 | 311 | class ReleaseTemp(Node): "Release a temporary value." |
paulb@213 | 312 | class Try(Node): "A try...except...else...finally grouping node." |
paulb@213 | 313 | class Raise(Node): "An exception raising node." |
paulb@213 | 314 | class Not(Node): "A negation of an expression." |
paulb@213 | 315 | class CheckType(Node): "Check a value's type from a list of choices." |
paulb@213 | 316 | class Return(Node): "Return an evaluated expression." |
paulb@213 | 317 | class Invoke(Node): "An invocation." |
paulb@213 | 318 | |
paulb@213 | 319 | # There are two types of return node: return from function and return from |
paulb@213 | 320 | # block. |
paulb@213 | 321 | |
paulb@213 | 322 | class ReturnFromFunction(Return): |
paulb@213 | 323 | pass |
paulb@213 | 324 | |
paulb@213 | 325 | class ReturnFromBlock(Return): |
paulb@213 | 326 | pass |
paulb@213 | 327 | |
paulb@213 | 328 | # NOTE: Not actually supported. |
paulb@213 | 329 | # Additionally, yield statements act like return statements for the purposes |
paulb@213 | 330 | # of this system. |
paulb@213 | 331 | |
paulb@213 | 332 | class Yield(ReturnFromFunction): |
paulb@213 | 333 | pass |
paulb@213 | 334 | |
paulb@213 | 335 | # Some behaviour is set as the default in conditional nodes but may be |
paulb@213 | 336 | # overridden. |
paulb@213 | 337 | |
paulb@213 | 338 | class Conditional(Node): |
paulb@213 | 339 | |
paulb@213 | 340 | "A conditional node consisting of a test and outcomes." |
paulb@213 | 341 | |
paulb@213 | 342 | def __init__(self, *args, **kw): |
paulb@213 | 343 | self.isolate_test = 0 |
paulb@213 | 344 | Node.__init__(self, *args, **kw) |
paulb@213 | 345 | |
paulb@213 | 346 | # Invocations involve some more work to process calculated attributes. |
paulb@213 | 347 | |
paulb@213 | 348 | class InvokeFunction(Invoke): |
paulb@213 | 349 | |
paulb@213 | 350 | "A function or method invocation." |
paulb@213 | 351 | |
paulb@213 | 352 | def __init__(self, *args, **kw): |
paulb@213 | 353 | self.args = [] |
paulb@213 | 354 | self.star = None |
paulb@213 | 355 | self.dstar = None |
paulb@213 | 356 | Invoke.__init__(self, *args, **kw) |
paulb@213 | 357 | self.set_args(self.args) |
paulb@213 | 358 | self.share_locals = 0 |
paulb@213 | 359 | |
paulb@213 | 360 | def set_args(self, args): |
paulb@213 | 361 | |
paulb@213 | 362 | "Sort the 'args' into positional and keyword arguments." |
paulb@213 | 363 | |
paulb@213 | 364 | self.pos_args = [] |
paulb@213 | 365 | self.kw_args = {} |
paulb@213 | 366 | add_kw = 0 |
paulb@213 | 367 | for arg in args: |
paulb@213 | 368 | if not add_kw: |
paulb@213 | 369 | if not isinstance(arg, Keyword): |
paulb@213 | 370 | self.pos_args.append(arg) |
paulb@213 | 371 | else: |
paulb@213 | 372 | add_kw = 1 |
paulb@213 | 373 | if add_kw: |
paulb@213 | 374 | if isinstance(arg, Keyword): |
paulb@213 | 375 | self.kw_args[arg.name] = arg.expr |
paulb@213 | 376 | else: |
paulb@213 | 377 | raise TypeError, "Positional argument appears after keyword arguments in '%s'." % self |
paulb@213 | 378 | |
paulb@213 | 379 | class InvokeRef(Invoke): |
paulb@213 | 380 | |
paulb@213 | 381 | "A block or loop invocation." |
paulb@213 | 382 | |
paulb@213 | 383 | def __init__(self, *args, **kw): |
paulb@213 | 384 | self.share_locals = 1 |
paulb@213 | 385 | Invoke.__init__(self, *args, **kw) |
paulb@213 | 386 | |
paulb@213 | 387 | # Program structure nodes. |
paulb@213 | 388 | |
paulb@213 | 389 | class Module(Node, Structure): |
paulb@213 | 390 | |
paulb@213 | 391 | "A Python module." |
paulb@213 | 392 | |
paulb@213 | 393 | def full_name(self): |
paulb@213 | 394 | return "module %s" % self.name |
paulb@213 | 395 | |
paulb@213 | 396 | class Subprogram(Node, WithName): |
paulb@213 | 397 | |
paulb@213 | 398 | "A subprogram: functions, methods and loops." |
paulb@213 | 399 | |
paulb@213 | 400 | def __init__(self, *args, **kw): |
paulb@213 | 401 | Node.__init__(self, *args, **kw) |
paulb@213 | 402 | WithName.__init__(self) |
paulb@213 | 403 | self.raises = set() |
paulb@213 | 404 | self.returns = set() |
paulb@213 | 405 | self.return_locals = set() |
paulb@213 | 406 | |
paulb@213 | 407 | # vim: tabstop=4 expandtab shiftwidth=4 |