paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paulb@109 | 4 | Simplified program nodes for easier type propagation and analysis. This module |
paulb@109 | 5 | contains nodes representing program instructions or operations, program |
paulb@109 | 6 | structure or organisation, and abstract program data. |
paul@0 | 7 | |
paulb@178 | 8 | Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | |
paul@0 | 10 | This software is free software; you can redistribute it and/or |
paul@0 | 11 | modify it under the terms of the GNU General Public License as |
paul@0 | 12 | published by the Free Software Foundation; either version 2 of |
paul@0 | 13 | the License, or (at your option) any later version. |
paul@0 | 14 | |
paul@0 | 15 | This software is distributed in the hope that it will be useful, |
paul@0 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 18 | GNU General Public License for more details. |
paul@0 | 19 | |
paul@0 | 20 | You should have received a copy of the GNU General Public |
paul@0 | 21 | License along with this library; see the file LICENCE.txt |
paul@0 | 22 | If not, write to the Free Software Foundation, Inc., |
paul@0 | 23 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paul@0 | 24 | """ |
paul@0 | 25 | |
paulb@19 | 26 | from compiler.visitor import ASTVisitor |
paulb@60 | 27 | import sys |
paulb@19 | 28 | |
paulb@83 | 29 | # Exceptions. |
paulb@83 | 30 | |
paulb@83 | 31 | class SimplifiedError(Exception): |
paulb@83 | 32 | |
paulb@83 | 33 | "An error in the annotation process." |
paulb@83 | 34 | |
paulb@83 | 35 | def __init__(self, exc, node, *args): |
paulb@83 | 36 | |
paulb@83 | 37 | """ |
paulb@83 | 38 | Initialise the error with an existing exception 'exc', the 'node' at |
paulb@83 | 39 | which this error occurs, along with additional optional arguments. |
paulb@83 | 40 | """ |
paulb@83 | 41 | |
paulb@83 | 42 | Exception.__init__(self, *args) |
paulb@83 | 43 | self.nodes = [node] |
paulb@83 | 44 | self.exc = exc |
paulb@83 | 45 | |
paulb@83 | 46 | def add(self, node): |
paulb@83 | 47 | |
paulb@83 | 48 | "Add the given 'node' to the path of nodes leading from the exception." |
paulb@83 | 49 | |
paulb@83 | 50 | self.nodes.append(node) |
paulb@83 | 51 | |
paulb@83 | 52 | def __str__(self): |
paulb@83 | 53 | |
paulb@83 | 54 | "Return a string showing the principal exception details." |
paulb@83 | 55 | |
paulb@83 | 56 | return "%s, %s" % (self.exc, self.nodes) |
paulb@83 | 57 | |
paulb@65 | 58 | # Unique name registration. |
paulb@65 | 59 | |
paulb@65 | 60 | class Naming: |
paulb@67 | 61 | |
paulb@67 | 62 | "Maintain records of unique names for each simple name." |
paulb@67 | 63 | |
paulb@78 | 64 | index_separator = "-" |
paulb@78 | 65 | |
paulb@65 | 66 | def __init__(self): |
paulb@65 | 67 | self.names = {} |
paulb@67 | 68 | |
paulb@65 | 69 | def get(self, obj): |
paulb@194 | 70 | return obj._unique_name |
paulb@67 | 71 | |
paulb@65 | 72 | def set(self, obj, name): |
paulb@194 | 73 | if hasattr(obj, "_unique_name"): |
paulb@65 | 74 | return |
paulb@65 | 75 | if not self.names.has_key(name): |
paulb@65 | 76 | self.names[name] = 0 |
paulb@65 | 77 | n = self.names[name] + 1 |
paulb@65 | 78 | self.names[name] = n |
paulb@194 | 79 | obj._unique_name = "%s%s%d" % (name, self.index_separator, n) |
paulb@65 | 80 | |
paulb@65 | 81 | naming = Naming() |
paulb@65 | 82 | |
paulb@65 | 83 | def name(obj, name): |
paulb@81 | 84 | |
paulb@81 | 85 | "Return a unique name for the given 'obj', indicating the base 'name'." |
paulb@81 | 86 | |
paulb@65 | 87 | naming.set(obj, name) |
paulb@65 | 88 | return naming.get(obj) |
paulb@65 | 89 | |
paulb@198 | 90 | # Named nodes are those which can be referenced in some way. |
paulb@198 | 91 | |
paulb@198 | 92 | class WithName: |
paulb@198 | 93 | |
paulb@198 | 94 | "Node naming." |
paulb@198 | 95 | |
paulb@198 | 96 | def __init__(self): |
paulb@198 | 97 | self._full_name = name(self, self.name or "$untitled") |
paulb@198 | 98 | |
paulb@198 | 99 | def full_name(self): |
paulb@198 | 100 | return self._full_name |
paulb@198 | 101 | |
paulb@24 | 102 | # Elementary visitor support. |
paulb@24 | 103 | |
paulb@24 | 104 | class Visitor(ASTVisitor): |
paulb@24 | 105 | |
paulb@24 | 106 | "A visitor base class." |
paulb@24 | 107 | |
paulb@24 | 108 | def __init__(self): |
paulb@24 | 109 | ASTVisitor.__init__(self) |
paulb@24 | 110 | |
paulb@24 | 111 | def default(self, node, *args): |
paulb@202 | 112 | raise SimplifiedError, (None, node) |
paulb@24 | 113 | |
paulb@24 | 114 | def dispatch(self, node, *args): |
paulb@24 | 115 | return ASTVisitor.dispatch(self, node, *args) |
paulb@24 | 116 | |
paulb@24 | 117 | def dispatches(self, nodes, *args): |
paulb@24 | 118 | results = [] |
paulb@24 | 119 | for node in nodes: |
paulb@24 | 120 | results.append(self.dispatch(node, *args)) |
paulb@24 | 121 | return results |
paulb@24 | 122 | |
paulb@99 | 123 | def dispatch_dict(self, d, *args): |
paulb@99 | 124 | results = {} |
paulb@99 | 125 | for name, node in d.items(): |
paulb@99 | 126 | results[name] = self.dispatch(node, *args) |
paulb@99 | 127 | return results |
paulb@99 | 128 | |
paulb@24 | 129 | # Simplified program nodes. |
paulb@24 | 130 | |
paul@0 | 131 | class Node: |
paul@0 | 132 | |
paul@0 | 133 | """ |
paul@0 | 134 | A result node with common attributes: |
paul@0 | 135 | |
paul@0 | 136 | original The original node from which this node was created. |
paulb@81 | 137 | defining Whether the node defines something in the original program. |
paulb@16 | 138 | name Any name involved (variable or attribute). |
paulb@17 | 139 | index Any index involved (temporary variable name). |
paulb@17 | 140 | value Any constant value. |
paulb@17 | 141 | ref Any reference to (for example) subprograms. |
paulb@47 | 142 | nstype Any indication of the namespace type involved in a name access. |
paulb@17 | 143 | |
paulb@17 | 144 | Expression-related attributes: |
paulb@17 | 145 | |
paul@0 | 146 | expr Any contributing expression. |
paulb@16 | 147 | lvalue Any target expression. |
paulb@16 | 148 | test Any test expression in a conditional instruction. |
paulb@16 | 149 | |
paulb@16 | 150 | Invocation and subprogram attributes: |
paulb@16 | 151 | |
paulb@16 | 152 | args Any collection of argument nodes. |
paulb@16 | 153 | params Any collection of parameter nodes and defaults. |
paulb@16 | 154 | |
paulb@16 | 155 | Statement-grouping attributes: |
paulb@16 | 156 | |
paulb@16 | 157 | body Any conditional code depending on the success of a test. |
paulb@16 | 158 | else_ Any conditional code depending on the failure of a test. |
paulb@35 | 159 | handler Any exception handler code. |
paulb@16 | 160 | finally_ Any code which will be executed regardless. |
paulb@16 | 161 | code Any unconditional code. |
paulb@32 | 162 | choices Any choices which may be included in the final program. |
paul@0 | 163 | """ |
paul@0 | 164 | |
paulb@185 | 165 | common_attributes = "name", "index", "value", "nstype", "internal", "returns_value", "is_method", "ref", "module", "structures", "original" |
paulb@204 | 166 | expression_attributes = "expr", "lvalue", "test" |
paulb@204 | 167 | argument_attributes = "star", "dstar" |
paulb@178 | 168 | invocation_attributes = "params", # not "args" - see "pos_args", "kw_args" |
paulb@178 | 169 | grouping_attributes = "code", "body", "else_", "handler", "finally_", "choices" |
paulb@178 | 170 | |
paulb@79 | 171 | def __init__(self, original=None, defining=0, **kw): |
paulb@47 | 172 | |
paulb@47 | 173 | """ |
paulb@79 | 174 | Initialise a program node with a link to an optional 'original' AST |
paulb@79 | 175 | node. An optional 'defining' parameter (if set to a true value), sets |
paulb@79 | 176 | this node as the defining node in the original. |
paulb@47 | 177 | """ |
paulb@47 | 178 | |
paul@0 | 179 | self.original = original |
paulb@79 | 180 | self.defining = defining |
paulb@204 | 181 | self.copies = {} |
paulb@79 | 182 | |
paulb@79 | 183 | if self.original is not None and defining: |
paulb@8 | 184 | self.original._node = self |
paul@0 | 185 | for name, value in kw.items(): |
paul@0 | 186 | setattr(self, name, value) |
paul@0 | 187 | |
paul@0 | 188 | def __repr__(self): |
paulb@81 | 189 | |
paulb@81 | 190 | "Return a readable representation." |
paulb@81 | 191 | |
paulb@65 | 192 | if hasattr(self, "full_name"): |
paulb@79 | 193 | s = "%s '%s'" % (self.__class__.__name__, self.full_name()) |
paulb@65 | 194 | elif hasattr(self, "name"): |
paulb@69 | 195 | s = "%s '%s'" % (self.__class__.__name__, self.name) |
paulb@65 | 196 | elif hasattr(self, "index"): |
paulb@69 | 197 | s = "%s (%s)" % (self.__class__.__name__, self.index) |
paul@0 | 198 | elif hasattr(self, "value"): |
paulb@69 | 199 | s = "%s %s" % (self.__class__.__name__, repr(self.value)) |
paul@0 | 200 | elif hasattr(self, "ref"): |
paulb@69 | 201 | s = "%s '%s'" % (self.__class__.__name__, name(self.ref, self.ref.name)) |
paul@0 | 202 | else: |
paulb@69 | 203 | s = "%s" % (self.__class__.__name__,) |
paulb@69 | 204 | |
paulb@69 | 205 | # Annotations. |
paulb@69 | 206 | |
paulb@69 | 207 | if hasattr(self, "types"): |
paulb@69 | 208 | return "%s -> %s" % (s, self.types) |
paulb@69 | 209 | else: |
paulb@69 | 210 | return s |
paul@0 | 211 | |
paulb@60 | 212 | def _pprint(self, indent, continuation, s, stream=None): |
paulb@81 | 213 | |
paulb@81 | 214 | """ |
paulb@81 | 215 | Print, at the given 'indent' level, with the given 'continuation' text, |
paulb@81 | 216 | the string 's', either to the given, optional 'stream' or to standard |
paulb@81 | 217 | output, this node's "pretty" representation. |
paulb@81 | 218 | """ |
paulb@81 | 219 | |
paulb@60 | 220 | stream = stream or sys.stdout |
paul@0 | 221 | if continuation: |
paulb@60 | 222 | print >>stream, (" " * max(0, indent - len(continuation))) + continuation + s |
paul@0 | 223 | else: |
paulb@60 | 224 | print >>stream, (" " * indent) + s |
paul@0 | 225 | |
paulb@60 | 226 | def pprint(self, indent=0, continuation=None, stream=None): |
paulb@81 | 227 | |
paulb@81 | 228 | """ |
paulb@81 | 229 | Print, at the given, optional 'indent', with the given optional |
paulb@81 | 230 | 'continuation' text, either to the given, optional 'stream' or to |
paulb@81 | 231 | standard output, this node's "pretty" representation along with its |
paulb@81 | 232 | children and their "pretty" representation (and so on). |
paulb@81 | 233 | """ |
paulb@81 | 234 | |
paulb@60 | 235 | stream = stream or sys.stdout |
paulb@60 | 236 | self._pprint(indent, continuation, repr(self), stream) |
paul@0 | 237 | |
paulb@47 | 238 | # Subprogram-related details. |
paul@0 | 239 | |
paul@0 | 240 | if hasattr(self, "params"): |
paul@0 | 241 | for name, default in self.params: |
paulb@75 | 242 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@24 | 243 | if hasattr(self, "star") and self.star: |
paulb@24 | 244 | name, default = self.star |
paulb@75 | 245 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@24 | 246 | if hasattr(self, "dstar") and self.dstar: |
paulb@24 | 247 | name, default = self.dstar |
paulb@75 | 248 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@112 | 249 | if getattr(self, "internal", 0): |
paulb@112 | 250 | self._pprint(indent + 2, "( ", "internal", stream=stream) |
paulb@18 | 251 | if getattr(self, "structure", 0): |
paulb@60 | 252 | self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name, stream=stream) |
paulb@47 | 253 | |
paulb@47 | 254 | # Expression-related details. |
paulb@47 | 255 | |
paul@0 | 256 | if hasattr(self, "expr"): |
paulb@60 | 257 | self.expr.pprint(indent + 2, "- ", stream=stream) |
paul@1 | 258 | if hasattr(self, "nodes"): |
paul@1 | 259 | for node in self.nodes: |
paulb@60 | 260 | node.pprint(indent + 2, "- ", stream=stream) |
paul@0 | 261 | if hasattr(self, "lvalue"): |
paulb@60 | 262 | self.lvalue.pprint(indent + 2, "->", stream=stream) |
paulb@47 | 263 | if hasattr(self, "nstype"): |
paulb@60 | 264 | self._pprint(indent + 2, "", self.nstype, stream=stream) |
paul@0 | 265 | if hasattr(self, "args"): |
paulb@99 | 266 | for arg in self.pos_args: |
paulb@60 | 267 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@99 | 268 | for name, arg in self.kw_args.items(): |
paulb@153 | 269 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@24 | 270 | if hasattr(self, "star") and self.star: |
paulb@60 | 271 | self.star.pprint(indent + 2, "( ", stream=stream) |
paulb@24 | 272 | if hasattr(self, "dstar") and self.dstar: |
paulb@60 | 273 | self.dstar.pprint(indent + 2, "( ", stream=stream) |
paul@0 | 274 | |
paulb@133 | 275 | # Statement-related details. |
paulb@133 | 276 | |
paulb@133 | 277 | if hasattr(self, "test"): |
paulb@133 | 278 | self.test.pprint(indent + 2, "? ", stream=stream) |
paulb@178 | 279 | for attr in self.grouping_attributes: |
paulb@133 | 280 | if hasattr(self, attr) and getattr(self, attr): |
paulb@133 | 281 | self._pprint(indent, "", "%s {" % attr, stream=stream) |
paulb@133 | 282 | for node in getattr(self, attr): |
paulb@133 | 283 | node.pprint(indent + 2, stream=stream) |
paulb@133 | 284 | self._pprint(indent, "", "}", stream=stream) |
paulb@133 | 285 | |
paulb@39 | 286 | # Annotations. |
paulb@39 | 287 | |
paulb@39 | 288 | if hasattr(self, "accesses"): |
paulb@60 | 289 | self._pprint(indent, "", "--------", stream=stream) |
paulb@39 | 290 | for ref, attributes in self.accesses.items(): |
paulb@60 | 291 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes])), stream=stream) |
paulb@60 | 292 | self._pprint(indent, "", "--------", stream=stream) |
paulb@57 | 293 | if hasattr(self, "writes"): |
paulb@60 | 294 | self._pprint(indent, "", "--------", stream=stream) |
paulb@57 | 295 | for ref, attribute in self.writes.items(): |
paulb@60 | 296 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream) |
paulb@60 | 297 | self._pprint(indent, "", "--------", stream=stream) |
paulb@39 | 298 | |
paulb@196 | 299 | # Node discovery functions. |
paulb@196 | 300 | |
paulb@196 | 301 | def active(self): |
paulb@196 | 302 | |
paulb@196 | 303 | "Return the active copies of this node or a list containing this node." |
paulb@196 | 304 | |
paulb@204 | 305 | return self.copies.values() or [self] |
paulb@196 | 306 | |
paulb@178 | 307 | # Node manipulation functions. |
paulb@178 | 308 | |
paulb@204 | 309 | def copy(self, instance=None, new_name=None): |
paulb@178 | 310 | |
paulb@178 | 311 | """ |
paulb@204 | 312 | Perform a deep copy of the node, optionally specifying the 'instance' |
paulb@204 | 313 | for whom the copy has been requested and a 'new_name' for the copied |
paulb@204 | 314 | node. Return new unannotated copies of the node and its descendants. |
paulb@178 | 315 | """ |
paulb@178 | 316 | |
paulb@178 | 317 | # Copy the common attributes of this node. |
paulb@178 | 318 | |
paulb@178 | 319 | common = {} |
paulb@178 | 320 | for attr in self.common_attributes: |
paulb@178 | 321 | if hasattr(self, attr): |
paulb@178 | 322 | common[attr] = getattr(self, attr) |
paulb@178 | 323 | |
paulb@204 | 324 | # Add new attributes specially for copies. |
paulb@204 | 325 | |
paulb@204 | 326 | common["instance"] = instance |
paulb@204 | 327 | |
paulb@178 | 328 | if new_name is not None: |
paulb@191 | 329 | common["copy_of"] = self |
paulb@178 | 330 | common["name"] = new_name |
paulb@178 | 331 | |
paulb@178 | 332 | # Instantiate the copy, avoiding side-effects with original and defining. |
paulb@178 | 333 | |
paulb@178 | 334 | node = self.__class__(**common) |
paulb@178 | 335 | node.defining = self.defining |
paulb@178 | 336 | |
paulb@196 | 337 | # Add links to copies from originals. |
paulb@178 | 338 | |
paulb@204 | 339 | self.copies[instance] = node |
paulb@178 | 340 | |
paulb@178 | 341 | # Copy attributes of different types. |
paulb@178 | 342 | |
paulb@178 | 343 | for attr in self.expression_attributes: |
paulb@178 | 344 | if hasattr(self, attr): |
paulb@178 | 345 | n = getattr(self, attr) |
paulb@178 | 346 | if n is None: |
paulb@178 | 347 | n2 = n |
paulb@178 | 348 | else: |
paulb@204 | 349 | n2 = n.copy(instance) |
paulb@178 | 350 | setattr(node, attr, n2) |
paulb@178 | 351 | |
paulb@204 | 352 | for attr in self.argument_attributes: |
paulb@204 | 353 | if hasattr(self, attr): |
paulb@204 | 354 | t = getattr(self, attr) |
paulb@204 | 355 | if t is None: |
paulb@204 | 356 | t2 = t |
paulb@204 | 357 | else: |
paulb@204 | 358 | name, n = t |
paulb@204 | 359 | n2 = n.copy(instance) |
paulb@204 | 360 | t2 = name, n2 |
paulb@204 | 361 | setattr(node, attr, t2) |
paulb@204 | 362 | |
paulb@178 | 363 | for attr in self.invocation_attributes: |
paulb@178 | 364 | if hasattr(self, attr): |
paulb@178 | 365 | l = getattr(self, attr) |
paulb@178 | 366 | l2 = [] |
paulb@178 | 367 | for name, n in l: |
paulb@178 | 368 | if n is None: |
paulb@178 | 369 | l2.append((name, n)) |
paulb@178 | 370 | else: |
paulb@204 | 371 | l2.append((name, n.copy(instance))) |
paulb@178 | 372 | setattr(node, attr, l2) |
paulb@178 | 373 | |
paulb@178 | 374 | for attr in self.grouping_attributes: |
paulb@178 | 375 | if hasattr(self, attr): |
paulb@178 | 376 | l = getattr(self, attr) |
paulb@204 | 377 | setattr(node, attr, [n.copy(instance) for n in l]) |
paulb@178 | 378 | |
paulb@178 | 379 | # Arguments are usually processed further - "args" is useless. |
paulb@178 | 380 | |
paulb@178 | 381 | if hasattr(self, "pos_args"): |
paulb@204 | 382 | node.pos_args = [n.copy(instance) for n in self.pos_args] |
paulb@178 | 383 | |
paulb@178 | 384 | if hasattr(self, "kw_args"): |
paulb@178 | 385 | node.kw_args = {} |
paulb@178 | 386 | for name, n in self.kw_args.items(): |
paulb@204 | 387 | node.kw_args[name] = n.copy(instance) |
paulb@178 | 388 | |
paulb@178 | 389 | return node |
paulb@178 | 390 | |
paulb@198 | 391 | # Comparable nodes based on naming. |
paulb@198 | 392 | |
paulb@198 | 393 | class Comparable(Node): |
paulb@198 | 394 | |
paulb@198 | 395 | "Comparable nodes implementing the 'full_name' method." |
paulb@198 | 396 | |
paulb@198 | 397 | def __eq__(self, other): |
paulb@198 | 398 | # NOTE: Single instance: all instances are the same |
paulb@198 | 399 | # NOTE: Multiple instances: all instances are different |
paulb@198 | 400 | if hasattr(other, "full_name"): |
paulb@198 | 401 | return self.full_name() == other.full_name() |
paulb@198 | 402 | else: |
paulb@198 | 403 | return NotImplemented |
paulb@198 | 404 | |
paulb@198 | 405 | def __hash__(self): |
paulb@198 | 406 | return id(self) |
paulb@198 | 407 | |
paulb@109 | 408 | # These are the supported "operations" described by simplified program nodes. |
paulb@109 | 409 | |
paul@0 | 410 | class Pass(Node): "A placeholder node corresponding to pass." |
paul@0 | 411 | class Assign(Node): "A grouping node for assignment-related operations." |
paul@0 | 412 | class Keyword(Node): "A grouping node for keyword arguments." |
paul@1 | 413 | class Global(Node): "A global name designator." |
paul@3 | 414 | class Import(Node): "A module import operation." |
paul@0 | 415 | class LoadTemp(Node): "Load a previously-stored temporary value." |
paul@0 | 416 | class LoadName(Node): "Load a named object." |
paul@0 | 417 | class LoadAttr(Node): "Load an object attribute." |
paulb@34 | 418 | class LoadRef(Node): "Load a reference, typically a subprogram or a constant." |
paul@0 | 419 | class LoadExc(Node): "Load a handled exception." |
paulb@190 | 420 | class ResetExc(Node): "Reset the exception state." |
paul@0 | 421 | class StoreTemp(Node): "Store a temporary value." |
paul@0 | 422 | class StoreName(Node): "Associate a name with an object." |
paul@0 | 423 | class StoreAttr(Node): "Associate an object's attribute with a value." |
paul@0 | 424 | class ReleaseTemp(Node): "Release a temporary value." |
paul@0 | 425 | class Try(Node): "A try...except...else...finally grouping node." |
paul@5 | 426 | class Raise(Node): "An exception raising node." |
paul@1 | 427 | class Not(Node): "A negation of an expression." |
paulb@193 | 428 | class CheckType(Node): "Check a value's type from a list of choices." |
paulb@28 | 429 | |
paulb@135 | 430 | # There are two types of return node: return from function and return from |
paulb@135 | 431 | # block. |
paulb@135 | 432 | |
paulb@135 | 433 | class Return(Node): |
paulb@135 | 434 | |
paulb@135 | 435 | "Return an evaluated expression." |
paulb@135 | 436 | |
paulb@135 | 437 | pass |
paulb@135 | 438 | |
paulb@135 | 439 | class ReturnFromFunction(Return): |
paulb@135 | 440 | pass |
paulb@135 | 441 | |
paulb@135 | 442 | class ReturnFromBlock(Return): |
paulb@135 | 443 | pass |
paulb@135 | 444 | |
paulb@203 | 445 | # NOTE: Not actually supported. |
paulb@203 | 446 | # Additionally, yield statements act like return statements for the purposes |
paulb@203 | 447 | # of this system. |
paulb@203 | 448 | |
paulb@203 | 449 | class Yield(ReturnFromFunction): |
paulb@203 | 450 | pass |
paulb@203 | 451 | |
paulb@133 | 452 | # Some behaviour is set as the default in conditional nodes but may be |
paulb@133 | 453 | # overridden. |
paulb@133 | 454 | |
paulb@133 | 455 | class Conditional(Node): |
paulb@133 | 456 | |
paulb@133 | 457 | "A conditional node consisting of a test and outcomes." |
paulb@133 | 458 | |
paulb@133 | 459 | def __init__(self, *args, **kw): |
paulb@133 | 460 | self.isolate_test = 0 |
paulb@133 | 461 | Node.__init__(self, *args, **kw) |
paulb@133 | 462 | |
paulb@67 | 463 | # Invocations involve some more work to process calculated attributes. |
paulb@67 | 464 | |
paulb@147 | 465 | class Invoke(Node): |
paulb@147 | 466 | |
paulb@147 | 467 | "An invocation." |
paulb@147 | 468 | |
paulb@147 | 469 | pass |
paulb@147 | 470 | |
paulb@67 | 471 | class InvokeFunction(Invoke): |
paulb@67 | 472 | |
paulb@67 | 473 | "A function or method invocation." |
paulb@67 | 474 | |
paulb@67 | 475 | def __init__(self, *args, **kw): |
paulb@148 | 476 | self.args = [] |
paulb@148 | 477 | self.star = None |
paulb@148 | 478 | self.dstar = None |
paulb@112 | 479 | Invoke.__init__(self, *args, **kw) |
paulb@148 | 480 | self.set_args(self.args) |
paulb@112 | 481 | self.share_locals = 0 |
paulb@67 | 482 | |
paulb@67 | 483 | def set_args(self, args): |
paulb@67 | 484 | |
paulb@67 | 485 | "Sort the 'args' into positional and keyword arguments." |
paulb@67 | 486 | |
paulb@67 | 487 | self.pos_args = [] |
paulb@99 | 488 | self.kw_args = {} |
paulb@67 | 489 | add_kw = 0 |
paulb@67 | 490 | for arg in args: |
paulb@67 | 491 | if not add_kw: |
paulb@67 | 492 | if not isinstance(arg, Keyword): |
paulb@67 | 493 | self.pos_args.append(arg) |
paulb@67 | 494 | else: |
paulb@67 | 495 | add_kw = 1 |
paulb@67 | 496 | if add_kw: |
paulb@67 | 497 | if isinstance(arg, Keyword): |
paulb@154 | 498 | self.kw_args[arg.name] = arg.expr |
paulb@67 | 499 | else: |
paulb@67 | 500 | raise TypeError, "Positional argument appears after keyword arguments in '%s'." % self |
paulb@67 | 501 | |
paulb@204 | 502 | class InvokeRef(Invoke): |
paulb@112 | 503 | |
paulb@112 | 504 | "A block or loop invocation." |
paulb@112 | 505 | |
paulb@112 | 506 | def __init__(self, *args, **kw): |
paulb@112 | 507 | self.share_locals = 1 |
paulb@112 | 508 | Invoke.__init__(self, *args, **kw) |
paulb@67 | 509 | |
paulb@109 | 510 | # Program structure nodes. |
paulb@109 | 511 | |
paulb@67 | 512 | class Subprogram(Node, WithName): |
paulb@67 | 513 | |
paulb@67 | 514 | "A subprogram: functions, methods and loops." |
paulb@67 | 515 | |
paulb@67 | 516 | def __init__(self, *args, **kw): |
paulb@67 | 517 | Node.__init__(self, *args, **kw) |
paulb@67 | 518 | WithName.__init__(self) |
paulb@67 | 519 | |
paulb@181 | 520 | class Module(Comparable): |
paulb@181 | 521 | |
paulb@181 | 522 | "A Python module." |
paulb@181 | 523 | |
paulb@181 | 524 | def full_name(self): |
paulb@181 | 525 | return "module %s" % self.name |
paulb@181 | 526 | |
paulb@28 | 527 | # Special non-program nodes. |
paulb@28 | 528 | |
paulb@181 | 529 | class Structure(Comparable): "A non-program node containing some kind of namespace." |
paulb@28 | 530 | |
paulb@176 | 531 | class _Class(Structure, WithName): |
paulb@34 | 532 | |
paulb@34 | 533 | "A Python class." |
paulb@34 | 534 | |
paulb@60 | 535 | def __init__(self, *args, **kw): |
paulb@60 | 536 | Structure.__init__(self, *args, **kw) |
paulb@67 | 537 | WithName.__init__(self) |
paulb@67 | 538 | |
paulb@99 | 539 | def full_name(self): |
paulb@99 | 540 | return "class %s" % self._full_name |
paulb@99 | 541 | |
paulb@176 | 542 | class SingleInstanceClass(_Class): |
paulb@176 | 543 | |
paulb@176 | 544 | "A Python class." |
paulb@176 | 545 | |
paulb@176 | 546 | def __init__(self, *args, **kw): |
paulb@176 | 547 | _Class.__init__(self, *args, **kw) |
paulb@176 | 548 | self.instance = None |
paulb@176 | 549 | |
paulb@176 | 550 | def has_instance(self, node): |
paulb@176 | 551 | return self.instance is not None |
paulb@176 | 552 | |
paulb@176 | 553 | def add_instance(self, node, instance): |
paulb@176 | 554 | self.instance = instance |
paulb@176 | 555 | |
paulb@176 | 556 | def get_instance(self, node): |
paulb@176 | 557 | return self.instance |
paulb@176 | 558 | |
paulb@176 | 559 | def get_instance_name(self, instance): |
paulb@176 | 560 | return self._full_name |
paulb@176 | 561 | |
paulb@178 | 562 | # Attribute propagation. |
paulb@178 | 563 | |
paulb@178 | 564 | def get_attribute_for_instance(self, attribute, instance): |
paulb@178 | 565 | return attribute |
paulb@178 | 566 | |
paulb@176 | 567 | class MultipleInstanceClass(_Class): |
paulb@176 | 568 | |
paulb@176 | 569 | "A Python class." |
paulb@176 | 570 | |
paulb@176 | 571 | def __init__(self, *args, **kw): |
paulb@176 | 572 | _Class.__init__(self, *args, **kw) |
paulb@176 | 573 | self.instances = {} |
paulb@178 | 574 | self.attributes_for_instances = {} |
paulb@178 | 575 | |
paulb@178 | 576 | def _get_key(self, node): |
paulb@185 | 577 | return id(getattr(node, "original", None)) # self.module.original |
paulb@176 | 578 | |
paulb@176 | 579 | def has_instance(self, node): |
paulb@178 | 580 | return self.instances.has_key(self._get_key(node)) |
paulb@176 | 581 | |
paulb@176 | 582 | def add_instance(self, node, instance): |
paulb@178 | 583 | self.instances[self._get_key(node)] = instance |
paulb@176 | 584 | |
paulb@176 | 585 | def get_instance(self, node): |
paulb@178 | 586 | return self.instances[self._get_key(node)] |
paulb@176 | 587 | |
paulb@176 | 588 | def get_instance_name(self, instance): |
paulb@176 | 589 | return name(instance, self._full_name) |
paulb@176 | 590 | |
paulb@178 | 591 | # Attribute propagation. |
paulb@178 | 592 | |
paulb@178 | 593 | def get_attribute_for_instance(self, attribute, instance): |
paulb@204 | 594 | |
paulb@204 | 595 | # Create specialised methods. |
paulb@204 | 596 | |
paulb@178 | 597 | if isinstance(attribute.type, Subprogram): |
paulb@178 | 598 | subprogram = attribute.type |
paulb@204 | 599 | |
paulb@204 | 600 | # Each instance may have its own version of the subprogram. |
paulb@204 | 601 | |
paulb@178 | 602 | key = (subprogram, instance) |
paulb@178 | 603 | if not self.attributes_for_instances.has_key(key): |
paulb@204 | 604 | self.attributes_for_instances[key] = Attribute(attribute.context, subprogram.copy(instance, subprogram.full_name())) |
paulb@204 | 605 | print "New subprogram", self.attributes_for_instances[key].type, "for", key |
paulb@204 | 606 | |
paulb@178 | 607 | return self.attributes_for_instances[key] |
paulb@204 | 608 | |
paulb@204 | 609 | # The original nodes are returned for other attributes. |
paulb@204 | 610 | |
paulb@178 | 611 | else: |
paulb@178 | 612 | return attribute |
paulb@178 | 613 | |
paulb@198 | 614 | class SelectiveMultipleInstanceClass(MultipleInstanceClass): |
paulb@198 | 615 | |
paulb@198 | 616 | "A Python class which provides multiple instances depending on the class." |
paulb@198 | 617 | |
paulb@198 | 618 | def _get_key(self, node): |
paulb@198 | 619 | if self.namespace.has_key("__atomic__"): |
paulb@198 | 620 | return id(self) |
paulb@198 | 621 | else: |
paulb@198 | 622 | return MultipleInstanceClass._get_key(self, node) |
paulb@198 | 623 | |
paulb@204 | 624 | class ProlificMultipleInstanceClass(MultipleInstanceClass): |
paulb@204 | 625 | |
paulb@204 | 626 | """ |
paulb@204 | 627 | A Python class which provides multiple instances for different versions of |
paulb@204 | 628 | methods. In order to avoid unbounded instance production (since new |
paulb@204 | 629 | instances cause new copies of methods which in turn would cause new |
paulb@204 | 630 | instances), |
paulb@204 | 631 | """ |
paulb@204 | 632 | |
paulb@204 | 633 | def __init__(self, *args, **kw): |
paulb@204 | 634 | MultipleInstanceClass.__init__(self, *args, **kw) |
paulb@204 | 635 | self.instance_relations = {} |
paulb@204 | 636 | |
paulb@204 | 637 | def _get_key(self, node): |
paulb@204 | 638 | if self.namespace.has_key("__atomic__"): |
paulb@204 | 639 | return id(self) |
paulb@204 | 640 | else: |
paulb@204 | 641 | return id(node) |
paulb@204 | 642 | |
paulb@204 | 643 | def has_instance(self, node): |
paulb@204 | 644 | requesting_instance = getattr(node, "instance", None) |
paulb@204 | 645 | return requesting_instance is not None and requesting_instance.get_class() is self or \ |
paulb@204 | 646 | self.instance_relations.has_key(requesting_instance) or self.instances.has_key(self._get_key(node)) |
paulb@204 | 647 | |
paulb@204 | 648 | def add_instance(self, node, instance): |
paulb@204 | 649 | requesting_instance = getattr(node, "instance", None) |
paulb@204 | 650 | print "New instance", instance, "for", id(node), requesting_instance |
paulb@204 | 651 | self.instances[self._get_key(node)] = instance |
paulb@204 | 652 | if requesting_instance is not None: |
paulb@204 | 653 | self.instance_relations[requesting_instance] = instance |
paulb@204 | 654 | requesting_instance.get_class().instance_relations[instance] = requesting_instance |
paulb@204 | 655 | |
paulb@204 | 656 | def get_instance(self, node): |
paulb@204 | 657 | requesting_instance = getattr(node, "instance", None) |
paulb@204 | 658 | if requesting_instance is not None and requesting_instance.get_class() is self: |
paulb@204 | 659 | return requesting_instance |
paulb@204 | 660 | return self.instance_relations.get(requesting_instance) or self.instances[self._get_key(node)] |
paulb@204 | 661 | |
paulb@72 | 662 | class Instance(Structure): |
paulb@72 | 663 | |
paulb@72 | 664 | "An instance." |
paulb@72 | 665 | |
paulb@79 | 666 | def full_name(self): |
paulb@176 | 667 | return self.get_class().get_instance_name(self) |
paulb@133 | 668 | |
paulb@133 | 669 | def get_class(self): |
paulb@133 | 670 | return self.namespace.load("__class__")[0].type |
paulb@79 | 671 | |
paulb@72 | 672 | def __repr__(self): |
paulb@79 | 673 | return "Instance of type '%s'" % self.full_name() |
paulb@34 | 674 | |
paulb@91 | 675 | def __eq__(self, other): |
paulb@176 | 676 | # NOTE: Single instance: all instances are the same |
paulb@176 | 677 | # NOTE: Multiple instances: all instances are different |
paulb@91 | 678 | return self.full_name() == other.full_name() |
paulb@91 | 679 | |
paulb@91 | 680 | def __hash__(self): |
paulb@91 | 681 | return id(self) |
paulb@91 | 682 | |
paulb@34 | 683 | class Constant(Instance): |
paulb@34 | 684 | |
paulb@34 | 685 | "A constant initialised with a type name for future processing." |
paulb@34 | 686 | |
paulb@60 | 687 | def __init__(self, *args, **kw): |
paulb@60 | 688 | Instance.__init__(self, *args, **kw) |
paulb@34 | 689 | self.typename = self.value.__class__.__name__ |
paulb@19 | 690 | |
paulb@181 | 691 | # NOTE: Hacked full_name avoiding instantiation ordering issues: |
paulb@181 | 692 | # NOTE: initialise built-in types, initialise built-in constants. |
paulb@181 | 693 | |
paulb@181 | 694 | #def full_name(self): |
paulb@181 | 695 | # return self.typename + "-c" |
paulb@181 | 696 | |
paulb@178 | 697 | class Attribute: |
paulb@178 | 698 | |
paulb@178 | 699 | """ |
paulb@178 | 700 | An attribute abstraction, indicating the type of the attribute along with |
paulb@178 | 701 | its context or origin. |
paulb@178 | 702 | """ |
paulb@178 | 703 | |
paulb@178 | 704 | def __init__(self, context, type): |
paulb@178 | 705 | self.context = context |
paulb@178 | 706 | self.type = type |
paulb@178 | 707 | |
paulb@178 | 708 | def __eq__(self, other): |
paulb@178 | 709 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@178 | 710 | |
paulb@178 | 711 | def __repr__(self): |
paulb@178 | 712 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@178 | 713 | |
paulb@179 | 714 | def __hash__(self): |
paulb@179 | 715 | return id(self) |
paulb@179 | 716 | |
paulb@198 | 717 | # Additional program and AST nodes. |
paulb@198 | 718 | |
paulb@178 | 719 | class Self: |
paulb@178 | 720 | |
paulb@178 | 721 | """ |
paulb@178 | 722 | A program node encapsulating object/context information in an argument list. |
paulb@178 | 723 | This is not particularly like Attribute, Class, Instance or other such |
paulb@178 | 724 | things, since it actually appears in the program representation. |
paulb@178 | 725 | """ |
paulb@178 | 726 | |
paulb@178 | 727 | def __init__(self, attribute): |
paulb@178 | 728 | self.types = [attribute] |
paulb@178 | 729 | |
paulb@192 | 730 | class Op: |
paulb@192 | 731 | |
paulb@192 | 732 | "A replacement AST node representing an operation in a Compare construct." |
paulb@192 | 733 | |
paulb@192 | 734 | def __init__(self, name, expr): |
paulb@192 | 735 | self.name = name |
paulb@192 | 736 | self.expr = expr |
paulb@192 | 737 | |
paulb@176 | 738 | # Configuration setting. |
paulb@176 | 739 | |
paulb@176 | 740 | Class = SingleInstanceClass |
paulb@176 | 741 | #Class = MultipleInstanceClass |
paulb@176 | 742 | |
paulb@176 | 743 | def set_single_instance_mode(): |
paulb@176 | 744 | global Class |
paulb@176 | 745 | Class = SingleInstanceClass |
paulb@176 | 746 | |
paulb@176 | 747 | def set_multiple_instance_mode(): |
paulb@176 | 748 | global Class |
paulb@176 | 749 | Class = MultipleInstanceClass |
paulb@176 | 750 | |
paulb@198 | 751 | def set_selective_multiple_instance_mode(): |
paulb@198 | 752 | global Class |
paulb@198 | 753 | Class = SelectiveMultipleInstanceClass |
paulb@198 | 754 | |
paulb@204 | 755 | def set_prolific_multiple_instance_mode(): |
paulb@204 | 756 | global Class |
paulb@204 | 757 | Class = ProlificMultipleInstanceClass |
paulb@204 | 758 | |
paul@0 | 759 | # vim: tabstop=4 expandtab shiftwidth=4 |