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@437 | 263 | def get_object_types_for_usage(usage, objtable, name, unit_name): |
paul@437 | 264 | |
paul@437 | 265 | """ |
paul@437 | 266 | Return for the given attribute 'usage', using the 'objtable', the object |
paul@437 | 267 | types which satisfy such usage, reporting any problems for the given 'name' |
paul@437 | 268 | and 'unit_name'. |
paul@437 | 269 | """ |
paul@437 | 270 | |
paul@437 | 271 | all_objtypes = set() |
paul@437 | 272 | |
paul@437 | 273 | for attrnames in usage: |
paul@437 | 274 | objtypes = objtable.all_possible_objects_plus_status(attrnames) |
paul@437 | 275 | if not objtypes: |
paul@437 | 276 | print "Warning: usage in %r for %r finds no object supporting all attributes %r" % ( |
paul@437 | 277 | unit_name, name, attrnames) |
paul@437 | 278 | objtypes = objtable.any_possible_objects_plus_status(attrnames) |
paul@437 | 279 | if not objtypes: |
paul@437 | 280 | print "Warning: usage in %r for %r finds no object supporting any attributes %r" % ( |
paul@437 | 281 | unit_name, name, attrnames) |
paul@437 | 282 | |
paul@437 | 283 | all_objtypes.update(objtypes) |
paul@437 | 284 | |
paul@437 | 285 | return all_objtypes |
paul@437 | 286 | |
paul@83 | 287 | # Errors. |
paul@83 | 288 | |
paul@39 | 289 | class ProcessingError(Exception): |
paul@39 | 290 | |
paul@39 | 291 | "A processing error." |
paul@39 | 292 | |
paul@63 | 293 | pass |
paul@63 | 294 | |
paul@84 | 295 | class TableError(ProcessingError): |
paul@84 | 296 | |
paul@84 | 297 | "An error occurring during access to a lookup table." |
paul@84 | 298 | |
paul@84 | 299 | pass |
paul@84 | 300 | |
paul@243 | 301 | class TableGenerationError(ProcessingError): |
paul@243 | 302 | |
paul@243 | 303 | "An error occurring when generating a lookup table." |
paul@243 | 304 | |
paul@243 | 305 | pass |
paul@243 | 306 | |
paul@63 | 307 | class NodeProcessingError(ProcessingError): |
paul@63 | 308 | |
paul@63 | 309 | "A processing error associated with a particular program node." |
paul@63 | 310 | |
paul@339 | 311 | def __init__(self, message): |
paul@39 | 312 | self.message = message |
paul@339 | 313 | self.unit_name = None |
paul@339 | 314 | self.astnode = None |
paul@339 | 315 | |
paul@339 | 316 | def get_lineno(self, node): |
paul@339 | 317 | lineno = node.lineno |
paul@339 | 318 | if lineno is not None: |
paul@339 | 319 | return lineno |
paul@339 | 320 | else: |
paul@339 | 321 | for child in node.getChildNodes(): |
paul@339 | 322 | lineno = self.get_lineno(child) |
paul@339 | 323 | if lineno is not None: |
paul@339 | 324 | return lineno |
paul@339 | 325 | return None |
paul@39 | 326 | |
paul@39 | 327 | def __repr__(self): |
paul@339 | 328 | return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) |
paul@39 | 329 | |
paul@39 | 330 | def __str__(self): |
paul@39 | 331 | return repr(self) |
paul@39 | 332 | |
paul@296 | 333 | class InspectError(NodeProcessingError): |
paul@296 | 334 | |
paul@296 | 335 | "An error during the module inspection process." |
paul@296 | 336 | |
paul@296 | 337 | pass |
paul@296 | 338 | |
paul@83 | 339 | class TranslateError(NodeProcessingError): |
paul@83 | 340 | |
paul@83 | 341 | "An error during the module translation process." |
paul@83 | 342 | |
paul@83 | 343 | pass |
paul@83 | 344 | |
paul@100 | 345 | class TranslationNotImplementedError(TranslateError): |
paul@100 | 346 | |
paul@100 | 347 | "An error caused by a node not being supported in translation." |
paul@100 | 348 | |
paul@100 | 349 | pass |
paul@100 | 350 | |
paul@343 | 351 | # Special representations. |
paul@343 | 352 | |
paul@343 | 353 | class AtLeast: |
paul@343 | 354 | |
paul@343 | 355 | "A special representation for numbers of a given value or greater." |
paul@343 | 356 | |
paul@343 | 357 | def __init__(self, count): |
paul@343 | 358 | self.count = count |
paul@343 | 359 | |
paul@343 | 360 | def __eq__(self, other): |
paul@343 | 361 | return 0 |
paul@343 | 362 | |
paul@343 | 363 | __lt__ = __le__ = __eq__ |
paul@343 | 364 | |
paul@343 | 365 | def __ne__(self, other): |
paul@343 | 366 | return 1 |
paul@343 | 367 | |
paul@343 | 368 | def __gt__(self, other): |
paul@343 | 369 | if isinstance(other, AtLeast): |
paul@343 | 370 | return 0 |
paul@343 | 371 | else: |
paul@343 | 372 | return self.count > other |
paul@343 | 373 | |
paul@343 | 374 | def __ge__(self, other): |
paul@343 | 375 | if isinstance(other, AtLeast): |
paul@343 | 376 | return 0 |
paul@343 | 377 | else: |
paul@343 | 378 | return self.count >= other |
paul@343 | 379 | |
paul@343 | 380 | def __iadd__(self, other): |
paul@343 | 381 | if isinstance(other, AtLeast): |
paul@343 | 382 | self.count += other.count |
paul@343 | 383 | else: |
paul@343 | 384 | self.count += other |
paul@343 | 385 | return self |
paul@343 | 386 | |
paul@343 | 387 | def __radd__(self, other): |
paul@343 | 388 | if isinstance(other, AtLeast): |
paul@343 | 389 | return AtLeast(self.count + other.count) |
paul@343 | 390 | else: |
paul@343 | 391 | return AtLeast(self.count + other) |
paul@343 | 392 | |
paul@343 | 393 | def __repr__(self): |
paul@343 | 394 | return "AtLeast(%r)" % self.count |
paul@343 | 395 | |
paul@172 | 396 | # Useful data. |
paul@172 | 397 | |
paul@354 | 398 | operator_functions = { |
paul@354 | 399 | |
paul@354 | 400 | # Binary operations. |
paul@354 | 401 | |
paul@354 | 402 | "Add" : "add", |
paul@354 | 403 | "Bitand" : "and_", |
paul@354 | 404 | "Bitor" : "or_", |
paul@354 | 405 | "Bitxor" : "xor", |
paul@354 | 406 | "Div" : "div", |
paul@354 | 407 | "FloorDiv" : "floordiv", |
paul@354 | 408 | "LeftShift" : "lshift", |
paul@354 | 409 | "Mod" : "mod", |
paul@354 | 410 | "Mul" : "mul", |
paul@354 | 411 | "Power" : "pow", |
paul@354 | 412 | "RightShift" : "rshift", |
paul@354 | 413 | "Sub" : "sub", |
paul@354 | 414 | |
paul@354 | 415 | # Unary operations. |
paul@354 | 416 | |
paul@354 | 417 | "Invert" : "invert", |
paul@354 | 418 | "UnaryAdd" : "pos", |
paul@354 | 419 | "UnarySub" : "neg", |
paul@354 | 420 | |
paul@354 | 421 | # Augmented assignment. |
paul@354 | 422 | |
paul@354 | 423 | "+=" : "iadd", |
paul@354 | 424 | "-=" : "isub", |
paul@354 | 425 | "*=" : "imul", |
paul@354 | 426 | "/=" : "idiv", |
paul@354 | 427 | "//=" : "ifloordiv", |
paul@354 | 428 | "%=" : "imod", |
paul@354 | 429 | "**=" : "ipow", |
paul@354 | 430 | "<<=" : "ilshift", |
paul@354 | 431 | ">>=" : "irshift", |
paul@354 | 432 | "&=" : "iand", |
paul@354 | 433 | "^=" : "ixor", |
paul@354 | 434 | "|=" : "ior", |
paul@354 | 435 | |
paul@354 | 436 | # Comparisons. |
paul@354 | 437 | |
paul@354 | 438 | "==" : "eq", |
paul@354 | 439 | "!=" : "ne", |
paul@354 | 440 | "<" : "lt", |
paul@354 | 441 | "<=" : "le", |
paul@354 | 442 | ">=" : "ge", |
paul@374 | 443 | ">" : "gt", |
paul@374 | 444 | |
paul@374 | 445 | # Access and slicing. |
paul@374 | 446 | |
paul@374 | 447 | "AssSlice" : "setslice", |
paul@374 | 448 | "Slice" : "getslice", |
paul@374 | 449 | "AssSubscript" : "setitem", |
paul@374 | 450 | "Subscript" : "getitem", |
paul@354 | 451 | } |
paul@354 | 452 | |
paul@39 | 453 | # vim: tabstop=4 expandtab shiftwidth=4 |