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