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