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@178 | 166 | expression_attributes = "expr", "lvalue", "test", "star", "dstar" |
paulb@178 | 167 | invocation_attributes = "params", # not "args" - see "pos_args", "kw_args" |
paulb@178 | 168 | grouping_attributes = "code", "body", "else_", "handler", "finally_", "choices" |
paulb@178 | 169 | |
paulb@79 | 170 | def __init__(self, original=None, defining=0, **kw): |
paulb@47 | 171 | |
paulb@47 | 172 | """ |
paulb@79 | 173 | Initialise a program node with a link to an optional 'original' AST |
paulb@79 | 174 | node. An optional 'defining' parameter (if set to a true value), sets |
paulb@79 | 175 | this node as the defining node in the original. |
paulb@47 | 176 | """ |
paulb@47 | 177 | |
paul@0 | 178 | self.original = original |
paulb@79 | 179 | self.defining = defining |
paulb@196 | 180 | self.copies = [] |
paulb@79 | 181 | |
paulb@79 | 182 | if self.original is not None and defining: |
paulb@8 | 183 | self.original._node = self |
paul@0 | 184 | for name, value in kw.items(): |
paul@0 | 185 | setattr(self, name, value) |
paul@0 | 186 | |
paul@0 | 187 | def __repr__(self): |
paulb@81 | 188 | |
paulb@81 | 189 | "Return a readable representation." |
paulb@81 | 190 | |
paulb@65 | 191 | if hasattr(self, "full_name"): |
paulb@79 | 192 | s = "%s '%s'" % (self.__class__.__name__, self.full_name()) |
paulb@65 | 193 | elif hasattr(self, "name"): |
paulb@69 | 194 | s = "%s '%s'" % (self.__class__.__name__, self.name) |
paulb@65 | 195 | elif hasattr(self, "index"): |
paulb@69 | 196 | s = "%s (%s)" % (self.__class__.__name__, self.index) |
paul@0 | 197 | elif hasattr(self, "value"): |
paulb@69 | 198 | s = "%s %s" % (self.__class__.__name__, repr(self.value)) |
paul@0 | 199 | elif hasattr(self, "ref"): |
paulb@69 | 200 | s = "%s '%s'" % (self.__class__.__name__, name(self.ref, self.ref.name)) |
paul@0 | 201 | else: |
paulb@69 | 202 | s = "%s" % (self.__class__.__name__,) |
paulb@69 | 203 | |
paulb@69 | 204 | # Annotations. |
paulb@69 | 205 | |
paulb@69 | 206 | if hasattr(self, "types"): |
paulb@69 | 207 | return "%s -> %s" % (s, self.types) |
paulb@69 | 208 | else: |
paulb@69 | 209 | return s |
paul@0 | 210 | |
paulb@60 | 211 | def _pprint(self, indent, continuation, s, stream=None): |
paulb@81 | 212 | |
paulb@81 | 213 | """ |
paulb@81 | 214 | Print, at the given 'indent' level, with the given 'continuation' text, |
paulb@81 | 215 | the string 's', either to the given, optional 'stream' or to standard |
paulb@81 | 216 | output, this node's "pretty" representation. |
paulb@81 | 217 | """ |
paulb@81 | 218 | |
paulb@60 | 219 | stream = stream or sys.stdout |
paul@0 | 220 | if continuation: |
paulb@60 | 221 | print >>stream, (" " * max(0, indent - len(continuation))) + continuation + s |
paul@0 | 222 | else: |
paulb@60 | 223 | print >>stream, (" " * indent) + s |
paul@0 | 224 | |
paulb@60 | 225 | def pprint(self, indent=0, continuation=None, stream=None): |
paulb@81 | 226 | |
paulb@81 | 227 | """ |
paulb@81 | 228 | Print, at the given, optional 'indent', with the given optional |
paulb@81 | 229 | 'continuation' text, either to the given, optional 'stream' or to |
paulb@81 | 230 | standard output, this node's "pretty" representation along with its |
paulb@81 | 231 | children and their "pretty" representation (and so on). |
paulb@81 | 232 | """ |
paulb@81 | 233 | |
paulb@60 | 234 | stream = stream or sys.stdout |
paulb@60 | 235 | self._pprint(indent, continuation, repr(self), stream) |
paul@0 | 236 | |
paulb@47 | 237 | # Subprogram-related details. |
paul@0 | 238 | |
paul@0 | 239 | if hasattr(self, "params"): |
paul@0 | 240 | for name, default in self.params: |
paulb@75 | 241 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@24 | 242 | if hasattr(self, "star") and self.star: |
paulb@24 | 243 | name, default = self.star |
paulb@75 | 244 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@24 | 245 | if hasattr(self, "dstar") and self.dstar: |
paulb@24 | 246 | name, default = self.dstar |
paulb@75 | 247 | self._pprint(indent + 2, "( ", "%s default %s" % (name, default), stream=stream) |
paulb@112 | 248 | if getattr(self, "internal", 0): |
paulb@112 | 249 | self._pprint(indent + 2, "( ", "internal", stream=stream) |
paulb@18 | 250 | if getattr(self, "structure", 0): |
paulb@60 | 251 | self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name, stream=stream) |
paulb@47 | 252 | |
paulb@47 | 253 | # Expression-related details. |
paulb@47 | 254 | |
paul@0 | 255 | if hasattr(self, "expr"): |
paulb@60 | 256 | self.expr.pprint(indent + 2, "- ", stream=stream) |
paul@1 | 257 | if hasattr(self, "nodes"): |
paul@1 | 258 | for node in self.nodes: |
paulb@60 | 259 | node.pprint(indent + 2, "- ", stream=stream) |
paul@0 | 260 | if hasattr(self, "lvalue"): |
paulb@60 | 261 | self.lvalue.pprint(indent + 2, "->", stream=stream) |
paulb@47 | 262 | if hasattr(self, "nstype"): |
paulb@60 | 263 | self._pprint(indent + 2, "", self.nstype, stream=stream) |
paul@0 | 264 | if hasattr(self, "args"): |
paulb@99 | 265 | for arg in self.pos_args: |
paulb@60 | 266 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@99 | 267 | for name, arg in self.kw_args.items(): |
paulb@153 | 268 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@24 | 269 | if hasattr(self, "star") and self.star: |
paulb@60 | 270 | self.star.pprint(indent + 2, "( ", stream=stream) |
paulb@24 | 271 | if hasattr(self, "dstar") and self.dstar: |
paulb@60 | 272 | self.dstar.pprint(indent + 2, "( ", stream=stream) |
paul@0 | 273 | |
paulb@133 | 274 | # Statement-related details. |
paulb@133 | 275 | |
paulb@133 | 276 | if hasattr(self, "test"): |
paulb@133 | 277 | self.test.pprint(indent + 2, "? ", stream=stream) |
paulb@178 | 278 | for attr in self.grouping_attributes: |
paulb@133 | 279 | if hasattr(self, attr) and getattr(self, attr): |
paulb@133 | 280 | self._pprint(indent, "", "%s {" % attr, stream=stream) |
paulb@133 | 281 | for node in getattr(self, attr): |
paulb@133 | 282 | node.pprint(indent + 2, stream=stream) |
paulb@133 | 283 | self._pprint(indent, "", "}", stream=stream) |
paulb@133 | 284 | |
paulb@39 | 285 | # Annotations. |
paulb@39 | 286 | |
paulb@39 | 287 | if hasattr(self, "accesses"): |
paulb@60 | 288 | self._pprint(indent, "", "--------", stream=stream) |
paulb@39 | 289 | for ref, attributes in self.accesses.items(): |
paulb@60 | 290 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes])), stream=stream) |
paulb@60 | 291 | self._pprint(indent, "", "--------", stream=stream) |
paulb@57 | 292 | if hasattr(self, "writes"): |
paulb@60 | 293 | self._pprint(indent, "", "--------", stream=stream) |
paulb@57 | 294 | for ref, attribute in self.writes.items(): |
paulb@60 | 295 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream) |
paulb@60 | 296 | self._pprint(indent, "", "--------", stream=stream) |
paulb@39 | 297 | |
paulb@196 | 298 | # Node discovery functions. |
paulb@196 | 299 | |
paulb@196 | 300 | def active(self): |
paulb@196 | 301 | |
paulb@196 | 302 | "Return the active copies of this node or a list containing this node." |
paulb@196 | 303 | |
paulb@196 | 304 | return self.copies or [self] |
paulb@196 | 305 | |
paulb@178 | 306 | # Node manipulation functions. |
paulb@178 | 307 | |
paulb@185 | 308 | def copy(self, new_name=None): |
paulb@178 | 309 | |
paulb@178 | 310 | """ |
paulb@178 | 311 | Perform a deep copy of the node, optionally specifying a 'new_name', |
paulb@178 | 312 | returning a new unannotated copy. |
paulb@178 | 313 | """ |
paulb@178 | 314 | |
paulb@178 | 315 | # Copy the common attributes of this node. |
paulb@178 | 316 | |
paulb@178 | 317 | common = {} |
paulb@178 | 318 | for attr in self.common_attributes: |
paulb@178 | 319 | if hasattr(self, attr): |
paulb@178 | 320 | common[attr] = getattr(self, attr) |
paulb@178 | 321 | |
paulb@178 | 322 | if new_name is not None: |
paulb@191 | 323 | common["copy_of"] = self |
paulb@178 | 324 | common["name"] = new_name |
paulb@178 | 325 | |
paulb@178 | 326 | # Instantiate the copy, avoiding side-effects with original and defining. |
paulb@178 | 327 | |
paulb@178 | 328 | node = self.__class__(**common) |
paulb@178 | 329 | node.defining = self.defining |
paulb@178 | 330 | |
paulb@196 | 331 | # Add links to copies from originals. |
paulb@178 | 332 | |
paulb@196 | 333 | self.copies.append(node) |
paulb@178 | 334 | |
paulb@178 | 335 | # Copy attributes of different types. |
paulb@178 | 336 | |
paulb@178 | 337 | for attr in self.expression_attributes: |
paulb@178 | 338 | if hasattr(self, attr): |
paulb@178 | 339 | n = getattr(self, attr) |
paulb@178 | 340 | if n is None: |
paulb@178 | 341 | n2 = n |
paulb@178 | 342 | else: |
paulb@185 | 343 | n2 = n.copy() |
paulb@178 | 344 | setattr(node, attr, n2) |
paulb@178 | 345 | |
paulb@178 | 346 | for attr in self.invocation_attributes: |
paulb@178 | 347 | if hasattr(self, attr): |
paulb@178 | 348 | l = getattr(self, attr) |
paulb@178 | 349 | l2 = [] |
paulb@178 | 350 | for name, n in l: |
paulb@178 | 351 | if n is None: |
paulb@178 | 352 | l2.append((name, n)) |
paulb@178 | 353 | else: |
paulb@185 | 354 | l2.append((name, n.copy())) |
paulb@178 | 355 | setattr(node, attr, l2) |
paulb@178 | 356 | |
paulb@178 | 357 | for attr in self.grouping_attributes: |
paulb@178 | 358 | if hasattr(self, attr): |
paulb@178 | 359 | l = getattr(self, attr) |
paulb@185 | 360 | setattr(node, attr, [n.copy() for n in l]) |
paulb@178 | 361 | |
paulb@178 | 362 | # Arguments are usually processed further - "args" is useless. |
paulb@178 | 363 | |
paulb@178 | 364 | if hasattr(self, "pos_args"): |
paulb@185 | 365 | node.pos_args = [n.copy() for n in self.pos_args] |
paulb@178 | 366 | |
paulb@178 | 367 | if hasattr(self, "kw_args"): |
paulb@178 | 368 | node.kw_args = {} |
paulb@178 | 369 | for name, n in self.kw_args.items(): |
paulb@185 | 370 | node.kw_args[name] = n.copy() |
paulb@178 | 371 | |
paulb@178 | 372 | return node |
paulb@178 | 373 | |
paulb@198 | 374 | # Comparable nodes based on naming. |
paulb@198 | 375 | |
paulb@198 | 376 | class Comparable(Node): |
paulb@198 | 377 | |
paulb@198 | 378 | "Comparable nodes implementing the 'full_name' method." |
paulb@198 | 379 | |
paulb@198 | 380 | def __eq__(self, other): |
paulb@198 | 381 | # NOTE: Single instance: all instances are the same |
paulb@198 | 382 | # NOTE: Multiple instances: all instances are different |
paulb@198 | 383 | if hasattr(other, "full_name"): |
paulb@198 | 384 | return self.full_name() == other.full_name() |
paulb@198 | 385 | else: |
paulb@198 | 386 | return NotImplemented |
paulb@198 | 387 | |
paulb@198 | 388 | def __hash__(self): |
paulb@198 | 389 | return id(self) |
paulb@198 | 390 | |
paulb@109 | 391 | # These are the supported "operations" described by simplified program nodes. |
paulb@109 | 392 | |
paul@0 | 393 | class Pass(Node): "A placeholder node corresponding to pass." |
paul@0 | 394 | class Assign(Node): "A grouping node for assignment-related operations." |
paul@0 | 395 | class Keyword(Node): "A grouping node for keyword arguments." |
paul@1 | 396 | class Global(Node): "A global name designator." |
paul@3 | 397 | class Import(Node): "A module import operation." |
paul@0 | 398 | class LoadTemp(Node): "Load a previously-stored temporary value." |
paul@0 | 399 | class LoadName(Node): "Load a named object." |
paul@0 | 400 | class LoadAttr(Node): "Load an object attribute." |
paulb@34 | 401 | class LoadRef(Node): "Load a reference, typically a subprogram or a constant." |
paul@0 | 402 | class LoadExc(Node): "Load a handled exception." |
paulb@190 | 403 | class ResetExc(Node): "Reset the exception state." |
paul@0 | 404 | class StoreTemp(Node): "Store a temporary value." |
paul@0 | 405 | class StoreName(Node): "Associate a name with an object." |
paul@0 | 406 | class StoreAttr(Node): "Associate an object's attribute with a value." |
paul@0 | 407 | class ReleaseTemp(Node): "Release a temporary value." |
paul@0 | 408 | class Try(Node): "A try...except...else...finally grouping node." |
paul@5 | 409 | class Raise(Node): "An exception raising node." |
paul@1 | 410 | class Not(Node): "A negation of an expression." |
paulb@193 | 411 | class CheckType(Node): "Check a value's type from a list of choices." |
paulb@28 | 412 | |
paulb@135 | 413 | # There are two types of return node: return from function and return from |
paulb@135 | 414 | # block. |
paulb@135 | 415 | |
paulb@135 | 416 | class Return(Node): |
paulb@135 | 417 | |
paulb@135 | 418 | "Return an evaluated expression." |
paulb@135 | 419 | |
paulb@135 | 420 | pass |
paulb@135 | 421 | |
paulb@135 | 422 | class ReturnFromFunction(Return): |
paulb@135 | 423 | pass |
paulb@135 | 424 | |
paulb@135 | 425 | class ReturnFromBlock(Return): |
paulb@135 | 426 | pass |
paulb@135 | 427 | |
paulb@203 | 428 | # NOTE: Not actually supported. |
paulb@203 | 429 | # Additionally, yield statements act like return statements for the purposes |
paulb@203 | 430 | # of this system. |
paulb@203 | 431 | |
paulb@203 | 432 | class Yield(ReturnFromFunction): |
paulb@203 | 433 | pass |
paulb@203 | 434 | |
paulb@133 | 435 | # Some behaviour is set as the default in conditional nodes but may be |
paulb@133 | 436 | # overridden. |
paulb@133 | 437 | |
paulb@133 | 438 | class Conditional(Node): |
paulb@133 | 439 | |
paulb@133 | 440 | "A conditional node consisting of a test and outcomes." |
paulb@133 | 441 | |
paulb@133 | 442 | def __init__(self, *args, **kw): |
paulb@133 | 443 | self.isolate_test = 0 |
paulb@133 | 444 | Node.__init__(self, *args, **kw) |
paulb@133 | 445 | |
paulb@67 | 446 | # Invocations involve some more work to process calculated attributes. |
paulb@67 | 447 | |
paulb@147 | 448 | class Invoke(Node): |
paulb@147 | 449 | |
paulb@147 | 450 | "An invocation." |
paulb@147 | 451 | |
paulb@147 | 452 | pass |
paulb@147 | 453 | |
paulb@67 | 454 | class InvokeFunction(Invoke): |
paulb@67 | 455 | |
paulb@67 | 456 | "A function or method invocation." |
paulb@67 | 457 | |
paulb@67 | 458 | def __init__(self, *args, **kw): |
paulb@148 | 459 | self.args = [] |
paulb@148 | 460 | self.star = None |
paulb@148 | 461 | self.dstar = None |
paulb@112 | 462 | Invoke.__init__(self, *args, **kw) |
paulb@148 | 463 | self.set_args(self.args) |
paulb@112 | 464 | self.share_locals = 0 |
paulb@67 | 465 | |
paulb@67 | 466 | def set_args(self, args): |
paulb@67 | 467 | |
paulb@67 | 468 | "Sort the 'args' into positional and keyword arguments." |
paulb@67 | 469 | |
paulb@67 | 470 | self.pos_args = [] |
paulb@99 | 471 | self.kw_args = {} |
paulb@67 | 472 | add_kw = 0 |
paulb@67 | 473 | for arg in args: |
paulb@67 | 474 | if not add_kw: |
paulb@67 | 475 | if not isinstance(arg, Keyword): |
paulb@67 | 476 | self.pos_args.append(arg) |
paulb@67 | 477 | else: |
paulb@67 | 478 | add_kw = 1 |
paulb@67 | 479 | if add_kw: |
paulb@67 | 480 | if isinstance(arg, Keyword): |
paulb@154 | 481 | self.kw_args[arg.name] = arg.expr |
paulb@67 | 482 | else: |
paulb@67 | 483 | raise TypeError, "Positional argument appears after keyword arguments in '%s'." % self |
paulb@67 | 484 | |
paulb@112 | 485 | class InvokeBlock(Invoke): |
paulb@112 | 486 | |
paulb@112 | 487 | "A block or loop invocation." |
paulb@112 | 488 | |
paulb@112 | 489 | def __init__(self, *args, **kw): |
paulb@112 | 490 | self.share_locals = 1 |
paulb@112 | 491 | Invoke.__init__(self, *args, **kw) |
paulb@67 | 492 | |
paulb@109 | 493 | # Program structure nodes. |
paulb@109 | 494 | |
paulb@67 | 495 | class Subprogram(Node, WithName): |
paulb@67 | 496 | |
paulb@67 | 497 | "A subprogram: functions, methods and loops." |
paulb@67 | 498 | |
paulb@67 | 499 | def __init__(self, *args, **kw): |
paulb@67 | 500 | Node.__init__(self, *args, **kw) |
paulb@67 | 501 | WithName.__init__(self) |
paulb@67 | 502 | |
paulb@181 | 503 | class Module(Comparable): |
paulb@181 | 504 | |
paulb@181 | 505 | "A Python module." |
paulb@181 | 506 | |
paulb@181 | 507 | def full_name(self): |
paulb@181 | 508 | return "module %s" % self.name |
paulb@181 | 509 | |
paulb@28 | 510 | # Special non-program nodes. |
paulb@28 | 511 | |
paulb@181 | 512 | class Structure(Comparable): "A non-program node containing some kind of namespace." |
paulb@28 | 513 | |
paulb@176 | 514 | class _Class(Structure, WithName): |
paulb@34 | 515 | |
paulb@34 | 516 | "A Python class." |
paulb@34 | 517 | |
paulb@60 | 518 | def __init__(self, *args, **kw): |
paulb@60 | 519 | Structure.__init__(self, *args, **kw) |
paulb@67 | 520 | WithName.__init__(self) |
paulb@67 | 521 | |
paulb@99 | 522 | def full_name(self): |
paulb@99 | 523 | return "class %s" % self._full_name |
paulb@99 | 524 | |
paulb@176 | 525 | class SingleInstanceClass(_Class): |
paulb@176 | 526 | |
paulb@176 | 527 | "A Python class." |
paulb@176 | 528 | |
paulb@176 | 529 | def __init__(self, *args, **kw): |
paulb@176 | 530 | _Class.__init__(self, *args, **kw) |
paulb@176 | 531 | self.instance = None |
paulb@176 | 532 | |
paulb@176 | 533 | def has_instance(self, node): |
paulb@176 | 534 | return self.instance is not None |
paulb@176 | 535 | |
paulb@176 | 536 | def add_instance(self, node, instance): |
paulb@176 | 537 | self.instance = instance |
paulb@176 | 538 | |
paulb@176 | 539 | def get_instance(self, node): |
paulb@176 | 540 | return self.instance |
paulb@176 | 541 | |
paulb@176 | 542 | def get_instance_name(self, instance): |
paulb@176 | 543 | return self._full_name |
paulb@176 | 544 | |
paulb@178 | 545 | # Attribute propagation. |
paulb@178 | 546 | |
paulb@178 | 547 | def get_attribute_for_instance(self, attribute, instance): |
paulb@178 | 548 | return attribute |
paulb@178 | 549 | |
paulb@176 | 550 | class MultipleInstanceClass(_Class): |
paulb@176 | 551 | |
paulb@176 | 552 | "A Python class." |
paulb@176 | 553 | |
paulb@176 | 554 | def __init__(self, *args, **kw): |
paulb@176 | 555 | _Class.__init__(self, *args, **kw) |
paulb@176 | 556 | self.instances = {} |
paulb@178 | 557 | self.attributes_for_instances = {} |
paulb@178 | 558 | |
paulb@178 | 559 | def _get_key(self, node): |
paulb@185 | 560 | return id(getattr(node, "original", None)) # self.module.original |
paulb@176 | 561 | |
paulb@176 | 562 | def has_instance(self, node): |
paulb@178 | 563 | return self.instances.has_key(self._get_key(node)) |
paulb@176 | 564 | |
paulb@176 | 565 | def add_instance(self, node, instance): |
paulb@178 | 566 | self.instances[self._get_key(node)] = instance |
paulb@176 | 567 | |
paulb@176 | 568 | def get_instance(self, node): |
paulb@178 | 569 | return self.instances[self._get_key(node)] |
paulb@176 | 570 | |
paulb@176 | 571 | def get_instance_name(self, instance): |
paulb@176 | 572 | return name(instance, self._full_name) |
paulb@176 | 573 | |
paulb@178 | 574 | # Attribute propagation. |
paulb@178 | 575 | |
paulb@178 | 576 | def get_attribute_for_instance(self, attribute, instance): |
paulb@178 | 577 | if isinstance(attribute.type, Subprogram): |
paulb@178 | 578 | subprogram = attribute.type |
paulb@178 | 579 | key = (subprogram, instance) |
paulb@178 | 580 | if not self.attributes_for_instances.has_key(key): |
paulb@178 | 581 | self.attributes_for_instances[key] = Attribute(attribute.context, subprogram.copy(subprogram.full_name())) |
paulb@178 | 582 | return self.attributes_for_instances[key] |
paulb@178 | 583 | else: |
paulb@178 | 584 | return attribute |
paulb@178 | 585 | |
paulb@198 | 586 | class SelectiveMultipleInstanceClass(MultipleInstanceClass): |
paulb@198 | 587 | |
paulb@198 | 588 | "A Python class which provides multiple instances depending on the class." |
paulb@198 | 589 | |
paulb@198 | 590 | def _get_key(self, node): |
paulb@198 | 591 | if self.namespace.has_key("__atomic__"): |
paulb@198 | 592 | return id(self) |
paulb@198 | 593 | else: |
paulb@198 | 594 | return MultipleInstanceClass._get_key(self, node) |
paulb@198 | 595 | |
paulb@72 | 596 | class Instance(Structure): |
paulb@72 | 597 | |
paulb@72 | 598 | "An instance." |
paulb@72 | 599 | |
paulb@79 | 600 | def full_name(self): |
paulb@176 | 601 | return self.get_class().get_instance_name(self) |
paulb@133 | 602 | |
paulb@133 | 603 | def get_class(self): |
paulb@133 | 604 | return self.namespace.load("__class__")[0].type |
paulb@79 | 605 | |
paulb@72 | 606 | def __repr__(self): |
paulb@79 | 607 | return "Instance of type '%s'" % self.full_name() |
paulb@34 | 608 | |
paulb@91 | 609 | def __eq__(self, other): |
paulb@176 | 610 | # NOTE: Single instance: all instances are the same |
paulb@176 | 611 | # NOTE: Multiple instances: all instances are different |
paulb@91 | 612 | return self.full_name() == other.full_name() |
paulb@91 | 613 | |
paulb@91 | 614 | def __hash__(self): |
paulb@91 | 615 | return id(self) |
paulb@91 | 616 | |
paulb@34 | 617 | class Constant(Instance): |
paulb@34 | 618 | |
paulb@34 | 619 | "A constant initialised with a type name for future processing." |
paulb@34 | 620 | |
paulb@60 | 621 | def __init__(self, *args, **kw): |
paulb@60 | 622 | Instance.__init__(self, *args, **kw) |
paulb@34 | 623 | self.typename = self.value.__class__.__name__ |
paulb@19 | 624 | |
paulb@181 | 625 | # NOTE: Hacked full_name avoiding instantiation ordering issues: |
paulb@181 | 626 | # NOTE: initialise built-in types, initialise built-in constants. |
paulb@181 | 627 | |
paulb@181 | 628 | #def full_name(self): |
paulb@181 | 629 | # return self.typename + "-c" |
paulb@181 | 630 | |
paulb@178 | 631 | class Attribute: |
paulb@178 | 632 | |
paulb@178 | 633 | """ |
paulb@178 | 634 | An attribute abstraction, indicating the type of the attribute along with |
paulb@178 | 635 | its context or origin. |
paulb@178 | 636 | """ |
paulb@178 | 637 | |
paulb@178 | 638 | def __init__(self, context, type): |
paulb@178 | 639 | self.context = context |
paulb@178 | 640 | self.type = type |
paulb@178 | 641 | |
paulb@178 | 642 | def __eq__(self, other): |
paulb@178 | 643 | return hasattr(other, "type") and other.type == self.type or other == self.type |
paulb@178 | 644 | |
paulb@178 | 645 | def __repr__(self): |
paulb@178 | 646 | return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) |
paulb@178 | 647 | |
paulb@179 | 648 | def __hash__(self): |
paulb@179 | 649 | return id(self) |
paulb@179 | 650 | |
paulb@198 | 651 | # Additional program and AST nodes. |
paulb@198 | 652 | |
paulb@178 | 653 | class Self: |
paulb@178 | 654 | |
paulb@178 | 655 | """ |
paulb@178 | 656 | A program node encapsulating object/context information in an argument list. |
paulb@178 | 657 | This is not particularly like Attribute, Class, Instance or other such |
paulb@178 | 658 | things, since it actually appears in the program representation. |
paulb@178 | 659 | """ |
paulb@178 | 660 | |
paulb@178 | 661 | def __init__(self, attribute): |
paulb@178 | 662 | self.types = [attribute] |
paulb@178 | 663 | |
paulb@192 | 664 | class Op: |
paulb@192 | 665 | |
paulb@192 | 666 | "A replacement AST node representing an operation in a Compare construct." |
paulb@192 | 667 | |
paulb@192 | 668 | def __init__(self, name, expr): |
paulb@192 | 669 | self.name = name |
paulb@192 | 670 | self.expr = expr |
paulb@192 | 671 | |
paulb@176 | 672 | # Configuration setting. |
paulb@176 | 673 | |
paulb@176 | 674 | Class = SingleInstanceClass |
paulb@176 | 675 | #Class = MultipleInstanceClass |
paulb@176 | 676 | |
paulb@176 | 677 | def set_single_instance_mode(): |
paulb@176 | 678 | global Class |
paulb@176 | 679 | Class = SingleInstanceClass |
paulb@176 | 680 | |
paulb@176 | 681 | def set_multiple_instance_mode(): |
paulb@176 | 682 | global Class |
paulb@176 | 683 | Class = MultipleInstanceClass |
paulb@176 | 684 | |
paulb@198 | 685 | def set_selective_multiple_instance_mode(): |
paulb@198 | 686 | global Class |
paulb@198 | 687 | Class = SelectiveMultipleInstanceClass |
paulb@198 | 688 | |
paul@0 | 689 | # vim: tabstop=4 expandtab shiftwidth=4 |