paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Simplified AST nodes for easier type propagation and analysis. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This software is free software; you can redistribute it and/or |
paul@0 | 9 | modify it under the terms of the GNU General Public License as |
paul@0 | 10 | published by the Free Software Foundation; either version 2 of |
paul@0 | 11 | the License, or (at your option) any later version. |
paul@0 | 12 | |
paul@0 | 13 | This software is distributed in the hope that it will be useful, |
paul@0 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 16 | GNU General Public License for more details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public |
paul@0 | 19 | License along with this library; see the file LICENCE.txt |
paul@0 | 20 | If not, write to the Free Software Foundation, Inc., |
paul@0 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paul@0 | 22 | """ |
paul@0 | 23 | |
paulb@19 | 24 | from compiler.visitor import ASTVisitor |
paulb@60 | 25 | import sys |
paulb@19 | 26 | |
paulb@83 | 27 | # Exceptions. |
paulb@83 | 28 | |
paulb@83 | 29 | class SimplifiedError(Exception): |
paulb@83 | 30 | |
paulb@83 | 31 | "An error in the annotation process." |
paulb@83 | 32 | |
paulb@83 | 33 | def __init__(self, exc, node, *args): |
paulb@83 | 34 | |
paulb@83 | 35 | """ |
paulb@83 | 36 | Initialise the error with an existing exception 'exc', the 'node' at |
paulb@83 | 37 | which this error occurs, along with additional optional arguments. |
paulb@83 | 38 | """ |
paulb@83 | 39 | |
paulb@83 | 40 | Exception.__init__(self, *args) |
paulb@83 | 41 | self.nodes = [node] |
paulb@83 | 42 | self.exc = exc |
paulb@83 | 43 | |
paulb@83 | 44 | def add(self, node): |
paulb@83 | 45 | |
paulb@83 | 46 | "Add the given 'node' to the path of nodes leading from the exception." |
paulb@83 | 47 | |
paulb@83 | 48 | self.nodes.append(node) |
paulb@83 | 49 | |
paulb@83 | 50 | def __str__(self): |
paulb@83 | 51 | |
paulb@83 | 52 | "Return a string showing the principal exception details." |
paulb@83 | 53 | |
paulb@83 | 54 | return "%s, %s" % (self.exc, self.nodes) |
paulb@83 | 55 | |
paulb@65 | 56 | # Unique name registration. |
paulb@65 | 57 | |
paulb@65 | 58 | class Naming: |
paulb@67 | 59 | |
paulb@67 | 60 | "Maintain records of unique names for each simple name." |
paulb@67 | 61 | |
paulb@78 | 62 | index_separator = "-" |
paulb@78 | 63 | |
paulb@65 | 64 | def __init__(self): |
paulb@65 | 65 | self.obj_to_name = {} |
paulb@65 | 66 | self.names = {} |
paulb@67 | 67 | |
paulb@65 | 68 | def get(self, obj): |
paulb@65 | 69 | return self.obj_to_name[obj] |
paulb@67 | 70 | |
paulb@65 | 71 | def set(self, obj, name): |
paulb@65 | 72 | if self.obj_to_name.has_key(obj): |
paulb@65 | 73 | return |
paulb@65 | 74 | if not self.names.has_key(name): |
paulb@65 | 75 | self.names[name] = 0 |
paulb@65 | 76 | n = self.names[name] + 1 |
paulb@65 | 77 | self.names[name] = n |
paulb@78 | 78 | self.obj_to_name[obj] = "%s%s%d" % (name, self.index_separator, n) |
paulb@65 | 79 | |
paulb@65 | 80 | naming = Naming() |
paulb@65 | 81 | |
paulb@65 | 82 | def name(obj, name): |
paulb@81 | 83 | |
paulb@81 | 84 | "Return a unique name for the given 'obj', indicating the base 'name'." |
paulb@81 | 85 | |
paulb@65 | 86 | naming.set(obj, name) |
paulb@65 | 87 | return naming.get(obj) |
paulb@65 | 88 | |
paulb@24 | 89 | # Elementary visitor support. |
paulb@24 | 90 | |
paulb@24 | 91 | class Visitor(ASTVisitor): |
paulb@24 | 92 | |
paulb@24 | 93 | "A visitor base class." |
paulb@24 | 94 | |
paulb@24 | 95 | def __init__(self): |
paulb@24 | 96 | ASTVisitor.__init__(self) |
paulb@24 | 97 | |
paulb@24 | 98 | def default(self, node, *args): |
paulb@24 | 99 | raise ValueError, node.__class__ |
paulb@24 | 100 | |
paulb@24 | 101 | def dispatch(self, node, *args): |
paulb@24 | 102 | return ASTVisitor.dispatch(self, node, *args) |
paulb@24 | 103 | |
paulb@24 | 104 | def dispatches(self, nodes, *args): |
paulb@24 | 105 | results = [] |
paulb@24 | 106 | for node in nodes: |
paulb@24 | 107 | results.append(self.dispatch(node, *args)) |
paulb@24 | 108 | return results |
paulb@24 | 109 | |
paulb@99 | 110 | def dispatch_dict(self, d, *args): |
paulb@99 | 111 | results = {} |
paulb@99 | 112 | for name, node in d.items(): |
paulb@99 | 113 | results[name] = self.dispatch(node, *args) |
paulb@99 | 114 | return results |
paulb@99 | 115 | |
paulb@24 | 116 | # Simplified program nodes. |
paulb@24 | 117 | |
paul@0 | 118 | class Node: |
paul@0 | 119 | |
paul@0 | 120 | """ |
paul@0 | 121 | A result node with common attributes: |
paul@0 | 122 | |
paul@0 | 123 | original The original node from which this node was created. |
paulb@81 | 124 | defining Whether the node defines something in the original program. |
paulb@16 | 125 | name Any name involved (variable or attribute). |
paulb@17 | 126 | index Any index involved (temporary variable name). |
paulb@17 | 127 | value Any constant value. |
paulb@17 | 128 | ref Any reference to (for example) subprograms. |
paulb@47 | 129 | nstype Any indication of the namespace type involved in a name access. |
paulb@17 | 130 | |
paulb@17 | 131 | Expression-related attributes: |
paulb@17 | 132 | |
paul@0 | 133 | expr Any contributing expression. |
paulb@16 | 134 | lvalue Any target expression. |
paulb@16 | 135 | test Any test expression in a conditional instruction. |
paulb@16 | 136 | |
paulb@16 | 137 | Invocation and subprogram attributes: |
paulb@16 | 138 | |
paulb@16 | 139 | args Any collection of argument nodes. |
paulb@16 | 140 | params Any collection of parameter nodes and defaults. |
paulb@16 | 141 | |
paulb@16 | 142 | Statement-grouping attributes: |
paulb@16 | 143 | |
paulb@16 | 144 | body Any conditional code depending on the success of a test. |
paulb@16 | 145 | else_ Any conditional code depending on the failure of a test. |
paulb@35 | 146 | handler Any exception handler code. |
paulb@16 | 147 | finally_ Any code which will be executed regardless. |
paulb@16 | 148 | code Any unconditional code. |
paulb@32 | 149 | choices Any choices which may be included in the final program. |
paul@0 | 150 | """ |
paul@0 | 151 | |
paulb@79 | 152 | def __init__(self, original=None, defining=0, **kw): |
paulb@47 | 153 | |
paulb@47 | 154 | """ |
paulb@79 | 155 | Initialise a program node with a link to an optional 'original' AST |
paulb@79 | 156 | node. An optional 'defining' parameter (if set to a true value), sets |
paulb@79 | 157 | this node as the defining node in the original. |
paulb@47 | 158 | """ |
paulb@47 | 159 | |
paul@0 | 160 | self.original = original |
paulb@79 | 161 | self.defining = defining |
paulb@79 | 162 | |
paulb@79 | 163 | if self.original is not None and defining: |
paulb@8 | 164 | self.original._node = self |
paul@0 | 165 | for name, value in kw.items(): |
paul@0 | 166 | setattr(self, name, value) |
paul@0 | 167 | |
paul@0 | 168 | def __repr__(self): |
paulb@81 | 169 | |
paulb@81 | 170 | "Return a readable representation." |
paulb@81 | 171 | |
paulb@65 | 172 | if hasattr(self, "full_name"): |
paulb@79 | 173 | s = "%s '%s'" % (self.__class__.__name__, self.full_name()) |
paulb@65 | 174 | elif hasattr(self, "name"): |
paulb@69 | 175 | s = "%s '%s'" % (self.__class__.__name__, self.name) |
paulb@65 | 176 | elif hasattr(self, "index"): |
paulb@69 | 177 | s = "%s (%s)" % (self.__class__.__name__, self.index) |
paul@0 | 178 | elif hasattr(self, "value"): |
paulb@69 | 179 | s = "%s %s" % (self.__class__.__name__, repr(self.value)) |
paul@0 | 180 | elif hasattr(self, "ref"): |
paulb@69 | 181 | s = "%s '%s'" % (self.__class__.__name__, name(self.ref, self.ref.name)) |
paul@0 | 182 | else: |
paulb@69 | 183 | s = "%s" % (self.__class__.__name__,) |
paulb@69 | 184 | |
paulb@69 | 185 | # Annotations. |
paulb@69 | 186 | |
paulb@69 | 187 | if hasattr(self, "types"): |
paulb@69 | 188 | return "%s -> %s" % (s, self.types) |
paulb@69 | 189 | else: |
paulb@69 | 190 | return s |
paul@0 | 191 | |
paulb@60 | 192 | def _pprint(self, indent, continuation, s, stream=None): |
paulb@81 | 193 | |
paulb@81 | 194 | """ |
paulb@81 | 195 | Print, at the given 'indent' level, with the given 'continuation' text, |
paulb@81 | 196 | the string 's', either to the given, optional 'stream' or to standard |
paulb@81 | 197 | output, this node's "pretty" representation. |
paulb@81 | 198 | """ |
paulb@81 | 199 | |
paulb@60 | 200 | stream = stream or sys.stdout |
paul@0 | 201 | if continuation: |
paulb@60 | 202 | print >>stream, (" " * max(0, indent - len(continuation))) + continuation + s |
paul@0 | 203 | else: |
paulb@60 | 204 | print >>stream, (" " * indent) + s |
paul@0 | 205 | |
paulb@60 | 206 | def pprint(self, indent=0, continuation=None, stream=None): |
paulb@81 | 207 | |
paulb@81 | 208 | """ |
paulb@81 | 209 | Print, at the given, optional 'indent', with the given optional |
paulb@81 | 210 | 'continuation' text, either to the given, optional 'stream' or to |
paulb@81 | 211 | standard output, this node's "pretty" representation along with its |
paulb@81 | 212 | children and their "pretty" representation (and so on). |
paulb@81 | 213 | """ |
paulb@81 | 214 | |
paulb@60 | 215 | stream = stream or sys.stdout |
paulb@60 | 216 | self._pprint(indent, continuation, repr(self), stream) |
paul@0 | 217 | |
paulb@47 | 218 | # Subprogram-related details. |
paul@0 | 219 | |
paul@0 | 220 | if hasattr(self, "params"): |
paul@0 | 221 | for name, default in self.params: |
paulb@75 | 222 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@24 | 223 | if hasattr(self, "star") and self.star: |
paulb@24 | 224 | name, default = self.star |
paulb@75 | 225 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@24 | 226 | if hasattr(self, "dstar") and self.dstar: |
paulb@24 | 227 | name, default = self.dstar |
paulb@75 | 228 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paul@0 | 229 | if getattr(self, "acquire_locals", 0): |
paulb@60 | 230 | self._pprint(indent + 2, "( ", "acquiring locals", stream=stream) |
paulb@18 | 231 | if getattr(self, "structure", 0): |
paulb@60 | 232 | self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name, stream=stream) |
paulb@47 | 233 | |
paulb@47 | 234 | # Statement-related details. |
paulb@47 | 235 | |
paul@0 | 236 | if hasattr(self, "test"): |
paulb@60 | 237 | self.test.pprint(indent + 2, "? ", stream=stream) |
paulb@35 | 238 | for attr in "code", "body", "else_", "handler", "finally_", "choices": |
paul@0 | 239 | if hasattr(self, attr) and getattr(self, attr): |
paulb@60 | 240 | self._pprint(indent, "", "%s {" % attr, stream=stream) |
paul@0 | 241 | for node in getattr(self, attr): |
paulb@60 | 242 | node.pprint(indent + 2, stream=stream) |
paulb@60 | 243 | self._pprint(indent, "", "}", stream=stream) |
paulb@47 | 244 | |
paulb@47 | 245 | # Expression-related details. |
paulb@47 | 246 | |
paul@0 | 247 | if hasattr(self, "expr"): |
paulb@60 | 248 | self.expr.pprint(indent + 2, "- ", stream=stream) |
paul@1 | 249 | if hasattr(self, "nodes"): |
paul@1 | 250 | for node in self.nodes: |
paulb@60 | 251 | node.pprint(indent + 2, "- ", stream=stream) |
paul@0 | 252 | if hasattr(self, "lvalue"): |
paulb@60 | 253 | self.lvalue.pprint(indent + 2, "->", stream=stream) |
paulb@47 | 254 | if hasattr(self, "nstype"): |
paulb@60 | 255 | self._pprint(indent + 2, "", self.nstype, stream=stream) |
paul@0 | 256 | if hasattr(self, "args"): |
paulb@99 | 257 | for arg in self.pos_args: |
paulb@60 | 258 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@99 | 259 | for name, arg in self.kw_args.items(): |
paulb@99 | 260 | arg.pprint(indent + 2, "( %s=" % name, stream=stream) |
paulb@24 | 261 | if hasattr(self, "star") and self.star: |
paulb@60 | 262 | self.star.pprint(indent + 2, "( ", stream=stream) |
paulb@24 | 263 | if hasattr(self, "dstar") and self.dstar: |
paulb@60 | 264 | self.dstar.pprint(indent + 2, "( ", stream=stream) |
paul@0 | 265 | |
paulb@39 | 266 | # Annotations. |
paulb@39 | 267 | |
paulb@39 | 268 | if hasattr(self, "accesses"): |
paulb@60 | 269 | self._pprint(indent, "", "--------", stream=stream) |
paulb@39 | 270 | for ref, attributes in self.accesses.items(): |
paulb@60 | 271 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes])), stream=stream) |
paulb@60 | 272 | self._pprint(indent, "", "--------", stream=stream) |
paulb@57 | 273 | if hasattr(self, "writes"): |
paulb@60 | 274 | self._pprint(indent, "", "--------", stream=stream) |
paulb@57 | 275 | for ref, attribute in self.writes.items(): |
paulb@60 | 276 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream) |
paulb@60 | 277 | self._pprint(indent, "", "--------", stream=stream) |
paulb@39 | 278 | |
paul@0 | 279 | class Pass(Node): "A placeholder node corresponding to pass." |
paul@0 | 280 | class Return(Node): "Return an evaluated expression." |
paul@0 | 281 | class Assign(Node): "A grouping node for assignment-related operations." |
paul@0 | 282 | class Keyword(Node): "A grouping node for keyword arguments." |
paul@1 | 283 | class Global(Node): "A global name designator." |
paul@3 | 284 | class Import(Node): "A module import operation." |
paul@0 | 285 | class LoadTemp(Node): "Load a previously-stored temporary value." |
paul@0 | 286 | class LoadName(Node): "Load a named object." |
paul@0 | 287 | class LoadAttr(Node): "Load an object attribute." |
paulb@34 | 288 | class LoadRef(Node): "Load a reference, typically a subprogram or a constant." |
paul@0 | 289 | class LoadExc(Node): "Load a handled exception." |
paul@0 | 290 | class StoreTemp(Node): "Store a temporary value." |
paul@0 | 291 | class StoreName(Node): "Associate a name with an object." |
paul@0 | 292 | class StoreAttr(Node): "Associate an object's attribute with a value." |
paul@0 | 293 | class ReleaseTemp(Node): "Release a temporary value." |
paul@0 | 294 | class Conditional(Node): "A conditional node consisting of a test and outcomes." |
paul@0 | 295 | class Try(Node): "A try...except...else...finally grouping node." |
paul@5 | 296 | class Raise(Node): "An exception raising node." |
paul@1 | 297 | class Not(Node): "A negation of an expression." |
paulb@32 | 298 | class Choice(Node): "A special node which indicates a choice of expressions." |
paulb@28 | 299 | |
paulb@67 | 300 | # Invocations involve some more work to process calculated attributes. |
paulb@67 | 301 | |
paulb@67 | 302 | class Invoke(Node): "An invocation." |
paulb@67 | 303 | |
paulb@67 | 304 | class InvokeFunction(Invoke): |
paulb@67 | 305 | |
paulb@67 | 306 | "A function or method invocation." |
paulb@67 | 307 | |
paulb@67 | 308 | def __init__(self, *args, **kw): |
paulb@67 | 309 | Node.__init__(self, *args, **kw) |
paulb@67 | 310 | if hasattr(self, "args"): |
paulb@67 | 311 | self.set_args(self.args) |
paulb@67 | 312 | |
paulb@67 | 313 | def set_args(self, args): |
paulb@67 | 314 | |
paulb@67 | 315 | "Sort the 'args' into positional and keyword arguments." |
paulb@67 | 316 | |
paulb@67 | 317 | self.pos_args = [] |
paulb@99 | 318 | self.kw_args = {} |
paulb@67 | 319 | add_kw = 0 |
paulb@67 | 320 | for arg in args: |
paulb@67 | 321 | if not add_kw: |
paulb@67 | 322 | if not isinstance(arg, Keyword): |
paulb@67 | 323 | self.pos_args.append(arg) |
paulb@67 | 324 | else: |
paulb@67 | 325 | add_kw = 1 |
paulb@67 | 326 | if add_kw: |
paulb@67 | 327 | if isinstance(arg, Keyword): |
paulb@99 | 328 | self.kw_args[arg.name] = arg |
paulb@67 | 329 | else: |
paulb@67 | 330 | raise TypeError, "Positional argument appears after keyword arguments in '%s'." % self |
paulb@67 | 331 | |
paulb@67 | 332 | class InvokeBlock(Invoke): "A block or loop invocation." |
paulb@67 | 333 | |
paulb@67 | 334 | # Named nodes are those which can be referenced in some way. |
paulb@67 | 335 | |
paulb@67 | 336 | class WithName: |
paulb@67 | 337 | |
paulb@67 | 338 | "Node naming." |
paulb@67 | 339 | |
paulb@67 | 340 | def __init__(self): |
paulb@79 | 341 | self._full_name = name(self, self.name or "$untitled") |
paulb@79 | 342 | |
paulb@79 | 343 | def full_name(self): |
paulb@79 | 344 | return self._full_name |
paulb@67 | 345 | |
paulb@91 | 346 | class Module(Node): |
paulb@67 | 347 | |
paulb@67 | 348 | "A Python module." |
paulb@67 | 349 | |
paulb@91 | 350 | def full_name(self): |
paulb@99 | 351 | return "module %s" % self.name |
paulb@67 | 352 | |
paulb@67 | 353 | class Subprogram(Node, WithName): |
paulb@67 | 354 | |
paulb@67 | 355 | "A subprogram: functions, methods and loops." |
paulb@67 | 356 | |
paulb@67 | 357 | def __init__(self, *args, **kw): |
paulb@67 | 358 | Node.__init__(self, *args, **kw) |
paulb@67 | 359 | WithName.__init__(self) |
paulb@67 | 360 | |
paulb@28 | 361 | # Special non-program nodes. |
paulb@28 | 362 | |
paulb@67 | 363 | class Structure(Node): "A non-program node containing some kind of namespace." |
paulb@28 | 364 | |
paulb@67 | 365 | class Class(Structure, WithName): |
paulb@34 | 366 | |
paulb@34 | 367 | "A Python class." |
paulb@34 | 368 | |
paulb@60 | 369 | def __init__(self, *args, **kw): |
paulb@60 | 370 | Structure.__init__(self, *args, **kw) |
paulb@67 | 371 | WithName.__init__(self) |
paulb@91 | 372 | self.instances = [] |
paulb@67 | 373 | |
paulb@99 | 374 | def full_name(self): |
paulb@99 | 375 | return "class %s" % self._full_name |
paulb@99 | 376 | |
paulb@72 | 377 | class Instance(Structure): |
paulb@72 | 378 | |
paulb@72 | 379 | "An instance." |
paulb@72 | 380 | |
paulb@79 | 381 | def full_name(self): |
paulb@91 | 382 | # NOTE: Wrap the result in a call to name(self, ...) where multiple |
paulb@91 | 383 | # NOTE: instances per class can occur. |
paulb@99 | 384 | return self.namespace.load("__class__")[0].type._full_name |
paulb@79 | 385 | |
paulb@72 | 386 | def __repr__(self): |
paulb@79 | 387 | return "Instance of type '%s'" % self.full_name() |
paulb@34 | 388 | |
paulb@91 | 389 | def __eq__(self, other): |
paulb@91 | 390 | # NOTE: Assuming that multiple instances of the same class are equal. |
paulb@91 | 391 | return self.full_name() == other.full_name() |
paulb@91 | 392 | |
paulb@91 | 393 | def __hash__(self): |
paulb@91 | 394 | return id(self) |
paulb@91 | 395 | |
paulb@34 | 396 | class Constant(Instance): |
paulb@34 | 397 | |
paulb@34 | 398 | "A constant initialised with a type name for future processing." |
paulb@34 | 399 | |
paulb@60 | 400 | def __init__(self, *args, **kw): |
paulb@60 | 401 | Instance.__init__(self, *args, **kw) |
paulb@34 | 402 | self.typename = self.value.__class__.__name__ |
paulb@19 | 403 | |
paul@0 | 404 | # vim: tabstop=4 expandtab shiftwidth=4 |