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