paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@39 | 3 | """ |
paul@39 | 4 | Common classes. |
paul@39 | 5 | |
paul@404 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@39 | 7 | |
paul@39 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@39 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@39 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@39 | 11 | version. |
paul@39 | 12 | |
paul@39 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@39 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@39 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@39 | 16 | details. |
paul@39 | 17 | |
paul@39 | 18 | You should have received a copy of the GNU General Public License along with |
paul@39 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@39 | 20 | """ |
paul@39 | 21 | |
paul@339 | 22 | import compiler.visitor |
paul@339 | 23 | |
paul@83 | 24 | try: |
paul@83 | 25 | set |
paul@83 | 26 | except NameError: |
paul@83 | 27 | from sets import Set as set |
paul@83 | 28 | |
paul@404 | 29 | class ObjectSet: |
paul@404 | 30 | |
paul@404 | 31 | "A set of objects with optional associated data." |
paul@404 | 32 | |
paul@404 | 33 | def __init__(self, d=None): |
paul@404 | 34 | if d is None: |
paul@404 | 35 | self.objects = {} |
paul@404 | 36 | elif hasattr(d, "items"): |
paul@404 | 37 | self.objects = dict(d) |
paul@404 | 38 | else: |
paul@404 | 39 | self.objects = {} |
paul@404 | 40 | for key in d: |
paul@404 | 41 | self.add(key) |
paul@404 | 42 | |
paul@404 | 43 | def __repr__(self): |
paul@404 | 44 | out = ["{"] |
paul@404 | 45 | first = 1 |
paul@404 | 46 | for key, value in self.items(): |
paul@404 | 47 | if not first: |
paul@404 | 48 | out.append(", ") |
paul@404 | 49 | else: |
paul@404 | 50 | first = 0 |
paul@404 | 51 | out.append(repr(key)) |
paul@404 | 52 | if value: |
paul@404 | 53 | out.append(" : ") |
paul@404 | 54 | out.append(repr(value)) |
paul@404 | 55 | out.append("}") |
paul@404 | 56 | return "".join(out) |
paul@404 | 57 | |
paul@404 | 58 | def __iter__(self): |
paul@404 | 59 | return iter(self.keys()) |
paul@404 | 60 | |
paul@409 | 61 | def __nonzero__(self): |
paul@409 | 62 | return self.objects != {} |
paul@409 | 63 | |
paul@404 | 64 | # Set membership and comparisons. |
paul@404 | 65 | |
paul@404 | 66 | def __hash__(self): |
paul@404 | 67 | return hash(tuple(self.keys())) |
paul@404 | 68 | |
paul@409 | 69 | def __eq__(self, other): |
paul@409 | 70 | return self.objects == other.objects |
paul@404 | 71 | |
paul@404 | 72 | # Set methods. |
paul@404 | 73 | |
paul@404 | 74 | def add(self, obj): |
paul@404 | 75 | if not self.has_key(obj): |
paul@404 | 76 | self[obj] = [] |
paul@404 | 77 | |
paul@404 | 78 | # Dictionary and related methods. |
paul@404 | 79 | |
paul@404 | 80 | def __getitem__(self, key): |
paul@404 | 81 | return self.objects[key] |
paul@404 | 82 | |
paul@404 | 83 | def get(self, key, default=None): |
paul@404 | 84 | return self.objects.get(key, default) |
paul@404 | 85 | |
paul@404 | 86 | def __setitem__(self, key, value): |
paul@404 | 87 | self.objects[key] = value |
paul@404 | 88 | |
paul@404 | 89 | def has_key(self, key): |
paul@404 | 90 | return self.objects.has_key(key) |
paul@404 | 91 | |
paul@404 | 92 | def keys(self): |
paul@404 | 93 | return self.objects.keys() |
paul@404 | 94 | |
paul@404 | 95 | def values(self): |
paul@404 | 96 | return self.objects.values() |
paul@404 | 97 | |
paul@404 | 98 | def items(self): |
paul@404 | 99 | return self.objects.items() |
paul@404 | 100 | |
paul@404 | 101 | def update(self, other): |
paul@404 | 102 | |
paul@404 | 103 | # Combining dictionary-like objects involves combining values. |
paul@404 | 104 | |
paul@404 | 105 | if hasattr(other, "items"): |
paul@404 | 106 | for key, value in other.items(): |
paul@404 | 107 | self[key] = value + self.get(key, []) |
paul@404 | 108 | |
paul@404 | 109 | # Combining sequence-like objects involves just adding members. |
paul@404 | 110 | |
paul@404 | 111 | else: |
paul@404 | 112 | for key in other: |
paul@404 | 113 | self.add(key) |
paul@404 | 114 | |
paul@404 | 115 | def merge(self, other): |
paul@404 | 116 | |
paul@404 | 117 | """ |
paul@404 | 118 | Merge this object set with an 'other' set, combining the values where |
paul@404 | 119 | possible, and incorporating values present in only one of the sets. |
paul@404 | 120 | """ |
paul@404 | 121 | |
paul@404 | 122 | merged = ObjectSet() |
paul@404 | 123 | for key in other.keys(): |
paul@404 | 124 | if not self.has_key(key): |
paul@404 | 125 | merged[key] = other[key] |
paul@404 | 126 | else: |
paul@404 | 127 | for other_value in other[key]: |
paul@404 | 128 | for value in self[key]: |
paul@404 | 129 | merged[key] = value + other_value |
paul@404 | 130 | |
paul@404 | 131 | for key in self.keys(): |
paul@404 | 132 | if not merged.has_key(key): |
paul@404 | 133 | merged[key] = self[key] |
paul@404 | 134 | |
paul@404 | 135 | return merged |
paul@404 | 136 | |
paul@404 | 137 | def combine_object_set_lists(l1, l2): |
paul@404 | 138 | |
paul@404 | 139 | """ |
paul@404 | 140 | Combine lists of object sets 'l1' and 'l2' to make a product of their |
paul@404 | 141 | members. |
paul@404 | 142 | """ |
paul@404 | 143 | |
paul@409 | 144 | combined = set([]) |
paul@404 | 145 | for i1 in l1: |
paul@404 | 146 | for i2 in l2: |
paul@409 | 147 | combined.add(i1.merge(i2)) |
paul@404 | 148 | return combined |
paul@404 | 149 | |
paul@404 | 150 | def deepen_mapping_dict(d): |
paul@404 | 151 | |
paul@404 | 152 | "Convert the values of 'd' to be elements of a potentially larger set." |
paul@404 | 153 | |
paul@404 | 154 | new_dict = {} |
paul@404 | 155 | for key, value in d.items(): |
paul@404 | 156 | new_dict[key] = set([value]) |
paul@404 | 157 | return new_dict |
paul@404 | 158 | |
paul@404 | 159 | def merge_mapping_dicts(dicts): |
paul@387 | 160 | |
paul@387 | 161 | "Merge the given 'dicts' mapping keys to sets of objects." |
paul@387 | 162 | |
paul@387 | 163 | new_dict = {} |
paul@404 | 164 | update_mapping_dict(new_dict, dicts) |
paul@388 | 165 | return new_dict |
paul@388 | 166 | |
paul@404 | 167 | def update_mapping_dict(new_dict, dicts): |
paul@388 | 168 | |
paul@388 | 169 | "Update 'new_dict' with the contents of the set dictionary 'dicts'." |
paul@387 | 170 | |
paul@387 | 171 | for old_dict in dicts: |
paul@404 | 172 | for key, value in old_dict.items(): |
paul@387 | 173 | if not new_dict.has_key(key): |
paul@404 | 174 | new_dict[key] = ObjectSet(value) |
paul@387 | 175 | else: |
paul@404 | 176 | new_dict[key].update(value) |
paul@387 | 177 | |
paul@404 | 178 | def combine_mapping_dicts(d1, d2): |
paul@388 | 179 | |
paul@388 | 180 | """ |
paul@404 | 181 | Combine dictionaries 'd1' and 'd2' in a resulting dictionary, with the |
paul@404 | 182 | values of the contributing dictionaries being themselves combined such that |
paul@404 | 183 | a "product" of the values for a given key are stored in the combined |
paul@404 | 184 | dictionary. |
paul@388 | 185 | """ |
paul@388 | 186 | |
paul@388 | 187 | combined = {} |
paul@388 | 188 | d2_keys = d2.keys() |
paul@388 | 189 | |
paul@388 | 190 | for key in d2_keys: |
paul@388 | 191 | |
paul@388 | 192 | d1_values = d1.get(key) |
paul@388 | 193 | |
paul@388 | 194 | if d1_values is None: |
paul@388 | 195 | combined[key] = d2[key] |
paul@388 | 196 | else: |
paul@404 | 197 | combined[key] = combine_object_set_lists(d1_values, d2[key]) |
paul@388 | 198 | |
paul@388 | 199 | for key in d1.keys(): |
paul@388 | 200 | if key not in d2_keys: |
paul@388 | 201 | combined[key] = d1[key] |
paul@388 | 202 | |
paul@388 | 203 | return combined |
paul@387 | 204 | |
paul@357 | 205 | # Visitors and activities related to node annotations. |
paul@339 | 206 | |
paul@339 | 207 | class ASTVisitor(compiler.visitor.ASTVisitor): |
paul@339 | 208 | |
paul@339 | 209 | "A base class for visitors." |
paul@339 | 210 | |
paul@339 | 211 | def dispatch(self, node, *args): |
paul@357 | 212 | |
paul@357 | 213 | "Dispatch using 'node', annotating any raised exceptions." |
paul@357 | 214 | |
paul@339 | 215 | try: |
paul@339 | 216 | return compiler.visitor.ASTVisitor.dispatch(self, node, *args) |
paul@339 | 217 | except NodeProcessingError, exc: |
paul@340 | 218 | if exc.astnode is None: |
paul@340 | 219 | exc.astnode = node |
paul@342 | 220 | |
paul@342 | 221 | # NOTE: Should perhaps specialise the subclasses appropriately. |
paul@342 | 222 | |
paul@342 | 223 | if hasattr(self, "unit"): |
paul@342 | 224 | exc.unit_name = self.unit.full_name() |
paul@342 | 225 | else: |
paul@342 | 226 | exc.unit_name = self.full_name() |
paul@339 | 227 | raise |
paul@339 | 228 | |
paul@357 | 229 | def possible_accessor_types(self, node): |
paul@357 | 230 | |
paul@357 | 231 | """ |
paul@357 | 232 | Given annotations made during the inspection process, return all possible |
paul@401 | 233 | type names and indications of static usage for a 'node' involved in |
paul@401 | 234 | attribute access. |
paul@357 | 235 | """ |
paul@357 | 236 | |
paul@401 | 237 | target_names = set() |
paul@401 | 238 | |
paul@357 | 239 | if hasattr(node, "_attrusers"): |
paul@357 | 240 | |
paul@381 | 241 | # Visit each attribute user. |
paul@381 | 242 | |
paul@357 | 243 | for user in node._attrusers: |
paul@381 | 244 | |
paul@381 | 245 | # Since users such as branches may not provide type information, |
paul@381 | 246 | # attempt to find defining users. |
paul@381 | 247 | |
paul@381 | 248 | for def_user in user._attrdefs or [user]: |
paul@384 | 249 | for target_name, is_static in def_user._attrtypes.get(node._username, []): |
paul@401 | 250 | target_names.add((target_name, is_static)) |
paul@357 | 251 | |
paul@401 | 252 | return target_names |
paul@357 | 253 | |
paul@353 | 254 | def used_by_unit(node): |
paul@353 | 255 | |
paul@353 | 256 | """ |
paul@353 | 257 | Return whether the definition made by a 'node' is actually employed by the |
paul@353 | 258 | program unit within which it is found. |
paul@353 | 259 | """ |
paul@353 | 260 | |
paul@370 | 261 | return hasattr(node, "unit") and node.unit.parent.has_key(node.unit.name) |
paul@353 | 262 | |
paul@83 | 263 | # Errors. |
paul@83 | 264 | |
paul@39 | 265 | class ProcessingError(Exception): |
paul@39 | 266 | |
paul@39 | 267 | "A processing error." |
paul@39 | 268 | |
paul@63 | 269 | pass |
paul@63 | 270 | |
paul@84 | 271 | class TableError(ProcessingError): |
paul@84 | 272 | |
paul@84 | 273 | "An error occurring during access to a lookup table." |
paul@84 | 274 | |
paul@84 | 275 | pass |
paul@84 | 276 | |
paul@243 | 277 | class TableGenerationError(ProcessingError): |
paul@243 | 278 | |
paul@243 | 279 | "An error occurring when generating a lookup table." |
paul@243 | 280 | |
paul@243 | 281 | pass |
paul@243 | 282 | |
paul@63 | 283 | class NodeProcessingError(ProcessingError): |
paul@63 | 284 | |
paul@63 | 285 | "A processing error associated with a particular program node." |
paul@63 | 286 | |
paul@339 | 287 | def __init__(self, message): |
paul@39 | 288 | self.message = message |
paul@339 | 289 | self.unit_name = None |
paul@339 | 290 | self.astnode = None |
paul@339 | 291 | |
paul@339 | 292 | def get_lineno(self, node): |
paul@339 | 293 | lineno = node.lineno |
paul@339 | 294 | if lineno is not None: |
paul@339 | 295 | return lineno |
paul@339 | 296 | else: |
paul@339 | 297 | for child in node.getChildNodes(): |
paul@339 | 298 | lineno = self.get_lineno(child) |
paul@339 | 299 | if lineno is not None: |
paul@339 | 300 | return lineno |
paul@339 | 301 | return None |
paul@39 | 302 | |
paul@39 | 303 | def __repr__(self): |
paul@339 | 304 | return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) |
paul@39 | 305 | |
paul@39 | 306 | def __str__(self): |
paul@39 | 307 | return repr(self) |
paul@39 | 308 | |
paul@296 | 309 | class InspectError(NodeProcessingError): |
paul@296 | 310 | |
paul@296 | 311 | "An error during the module inspection process." |
paul@296 | 312 | |
paul@296 | 313 | pass |
paul@296 | 314 | |
paul@83 | 315 | class TranslateError(NodeProcessingError): |
paul@83 | 316 | |
paul@83 | 317 | "An error during the module translation process." |
paul@83 | 318 | |
paul@83 | 319 | pass |
paul@83 | 320 | |
paul@100 | 321 | class TranslationNotImplementedError(TranslateError): |
paul@100 | 322 | |
paul@100 | 323 | "An error caused by a node not being supported in translation." |
paul@100 | 324 | |
paul@100 | 325 | pass |
paul@100 | 326 | |
paul@343 | 327 | # Special representations. |
paul@343 | 328 | |
paul@343 | 329 | class AtLeast: |
paul@343 | 330 | |
paul@343 | 331 | "A special representation for numbers of a given value or greater." |
paul@343 | 332 | |
paul@343 | 333 | def __init__(self, count): |
paul@343 | 334 | self.count = count |
paul@343 | 335 | |
paul@343 | 336 | def __eq__(self, other): |
paul@343 | 337 | return 0 |
paul@343 | 338 | |
paul@343 | 339 | __lt__ = __le__ = __eq__ |
paul@343 | 340 | |
paul@343 | 341 | def __ne__(self, other): |
paul@343 | 342 | return 1 |
paul@343 | 343 | |
paul@343 | 344 | def __gt__(self, other): |
paul@343 | 345 | if isinstance(other, AtLeast): |
paul@343 | 346 | return 0 |
paul@343 | 347 | else: |
paul@343 | 348 | return self.count > other |
paul@343 | 349 | |
paul@343 | 350 | def __ge__(self, other): |
paul@343 | 351 | if isinstance(other, AtLeast): |
paul@343 | 352 | return 0 |
paul@343 | 353 | else: |
paul@343 | 354 | return self.count >= other |
paul@343 | 355 | |
paul@343 | 356 | def __iadd__(self, other): |
paul@343 | 357 | if isinstance(other, AtLeast): |
paul@343 | 358 | self.count += other.count |
paul@343 | 359 | else: |
paul@343 | 360 | self.count += other |
paul@343 | 361 | return self |
paul@343 | 362 | |
paul@343 | 363 | def __radd__(self, other): |
paul@343 | 364 | if isinstance(other, AtLeast): |
paul@343 | 365 | return AtLeast(self.count + other.count) |
paul@343 | 366 | else: |
paul@343 | 367 | return AtLeast(self.count + other) |
paul@343 | 368 | |
paul@343 | 369 | def __repr__(self): |
paul@343 | 370 | return "AtLeast(%r)" % self.count |
paul@343 | 371 | |
paul@172 | 372 | # Useful data. |
paul@172 | 373 | |
paul@172 | 374 | comparison_methods = { |
paul@232 | 375 | "==" : ("__eq__", "__eq__"), |
paul@232 | 376 | "!=" : ("__ne__", "__ne__"), |
paul@172 | 377 | "<" : ("__lt__", "__gt__"), |
paul@172 | 378 | "<=" : ("__le__", "__ge__"), |
paul@172 | 379 | ">=" : ("__ge__", "__le__"), |
paul@172 | 380 | ">" : ("__gt__", "__lt__"), |
paul@172 | 381 | "is" : None, |
paul@172 | 382 | "is not" : None, |
paul@172 | 383 | "in" : None, |
paul@172 | 384 | "not in" : None |
paul@172 | 385 | } |
paul@172 | 386 | |
paul@172 | 387 | augassign_methods = { |
paul@172 | 388 | "+=" : ("__iadd__", ("__add__", "__radd__")), |
paul@172 | 389 | "-=" : ("__isub__", ("__sub__", "__rsub__")), |
paul@172 | 390 | "*=" : ("__imul__", ("__mul__", "__rmul__")), |
paul@172 | 391 | "/=" : ("__idiv__", ("__div__", "__rdiv__")), |
paul@324 | 392 | "//=" : ("__ifloordiv__", ("__floordiv__", "__rfloordiv__")), |
paul@172 | 393 | "%=" : ("__imod__", ("__mod__", "__rmod__")), |
paul@172 | 394 | "**=" : ("__ipow__", ("__pow__", "__rpow__")), |
paul@172 | 395 | "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), |
paul@172 | 396 | ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), |
paul@172 | 397 | "&=" : ("__iand__", ("__and__", "__rand__")), |
paul@172 | 398 | "^=" : ("__ixor__", ("__xor__", "__rxor__")), |
paul@172 | 399 | "|=" : ("__ior__", ("__or__", "__ror__")) |
paul@172 | 400 | } |
paul@172 | 401 | |
paul@172 | 402 | binary_methods = { |
paul@172 | 403 | "Add" : ("__add__", "__radd__"), |
paul@172 | 404 | "Bitand" : ("__and__", "__rand__"), |
paul@172 | 405 | "Bitor" : ("__or__", "__ror__"), |
paul@172 | 406 | "Bitxor" : ("__xor__", "__rxor__"), |
paul@172 | 407 | "Div" : ("__div__", "__rdiv__"), |
paul@172 | 408 | "FloorDiv" : ("__floordiv__", "__rfloordiv__"), |
paul@172 | 409 | "LeftShift" : ("__lshift__", "__rlshift__"), |
paul@172 | 410 | "Mod" : ("__mod__", "__rmod__"), |
paul@172 | 411 | "Mul" : ("__mul__", "__rmul__"), |
paul@172 | 412 | "Power" : ("__pow__", "__rpow__"), |
paul@172 | 413 | "RightShift" : ("__rshift__", "__rrshift__"), |
paul@172 | 414 | "Sub" : ("__sub__", "__rsub__") |
paul@172 | 415 | } |
paul@172 | 416 | |
paul@172 | 417 | unary_methods = { |
paul@172 | 418 | "Invert" : "__invert__", |
paul@172 | 419 | "UnaryAdd" : "__pos__", |
paul@172 | 420 | "UnarySub" : "__neg__" |
paul@172 | 421 | } |
paul@172 | 422 | |
paul@354 | 423 | operator_functions = { |
paul@354 | 424 | |
paul@354 | 425 | # Binary operations. |
paul@354 | 426 | |
paul@354 | 427 | "Add" : "add", |
paul@354 | 428 | "Bitand" : "and_", |
paul@354 | 429 | "Bitor" : "or_", |
paul@354 | 430 | "Bitxor" : "xor", |
paul@354 | 431 | "Div" : "div", |
paul@354 | 432 | "FloorDiv" : "floordiv", |
paul@354 | 433 | "LeftShift" : "lshift", |
paul@354 | 434 | "Mod" : "mod", |
paul@354 | 435 | "Mul" : "mul", |
paul@354 | 436 | "Power" : "pow", |
paul@354 | 437 | "RightShift" : "rshift", |
paul@354 | 438 | "Sub" : "sub", |
paul@354 | 439 | |
paul@354 | 440 | # Unary operations. |
paul@354 | 441 | |
paul@354 | 442 | "Invert" : "invert", |
paul@354 | 443 | "UnaryAdd" : "pos", |
paul@354 | 444 | "UnarySub" : "neg", |
paul@354 | 445 | |
paul@354 | 446 | # Augmented assignment. |
paul@354 | 447 | |
paul@354 | 448 | "+=" : "iadd", |
paul@354 | 449 | "-=" : "isub", |
paul@354 | 450 | "*=" : "imul", |
paul@354 | 451 | "/=" : "idiv", |
paul@354 | 452 | "//=" : "ifloordiv", |
paul@354 | 453 | "%=" : "imod", |
paul@354 | 454 | "**=" : "ipow", |
paul@354 | 455 | "<<=" : "ilshift", |
paul@354 | 456 | ">>=" : "irshift", |
paul@354 | 457 | "&=" : "iand", |
paul@354 | 458 | "^=" : "ixor", |
paul@354 | 459 | "|=" : "ior", |
paul@354 | 460 | |
paul@354 | 461 | # Comparisons. |
paul@354 | 462 | |
paul@354 | 463 | "==" : "eq", |
paul@354 | 464 | "!=" : "ne", |
paul@354 | 465 | "<" : "lt", |
paul@354 | 466 | "<=" : "le", |
paul@354 | 467 | ">=" : "ge", |
paul@374 | 468 | ">" : "gt", |
paul@374 | 469 | |
paul@374 | 470 | # Access and slicing. |
paul@374 | 471 | |
paul@374 | 472 | "AssSlice" : "setslice", |
paul@374 | 473 | "Slice" : "getslice", |
paul@374 | 474 | "AssSubscript" : "setitem", |
paul@374 | 475 | "Subscript" : "getitem", |
paul@354 | 476 | } |
paul@354 | 477 | |
paul@39 | 478 | # vim: tabstop=4 expandtab shiftwidth=4 |