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