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