paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@39 | 3 | """ |
paul@39 | 4 | Common classes. |
paul@39 | 5 | |
paul@296 | 6 | Copyright (C) 2007, 2008, 2009, 2010 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@387 | 29 | def merge_set_dicts(dicts): |
paul@387 | 30 | |
paul@387 | 31 | "Merge the given 'dicts' mapping keys to sets of objects." |
paul@387 | 32 | |
paul@387 | 33 | new_dict = {} |
paul@388 | 34 | update_set_dict(new_dict, dicts) |
paul@388 | 35 | return new_dict |
paul@388 | 36 | |
paul@388 | 37 | def update_set_dict(new_dict, dicts): |
paul@388 | 38 | |
paul@388 | 39 | "Update 'new_dict' with the contents of the set dictionary 'dicts'." |
paul@387 | 40 | |
paul@387 | 41 | for old_dict in dicts: |
paul@387 | 42 | for key, values in old_dict.items(): |
paul@387 | 43 | if not new_dict.has_key(key): |
paul@387 | 44 | new_dict[key] = set(values) |
paul@387 | 45 | else: |
paul@387 | 46 | new_dict[key].update(values) |
paul@387 | 47 | |
paul@388 | 48 | def deepen_set_dict(d): |
paul@388 | 49 | |
paul@388 | 50 | """ |
paul@388 | 51 | Return a new dictionary mapping keys to sets of tuples of values, using data |
paul@388 | 52 | from 'd' which is a dictionary mapping keys to sets of values. |
paul@388 | 53 | """ |
paul@388 | 54 | |
paul@388 | 55 | d2 = {} |
paul@388 | 56 | for key, values in d.items(): |
paul@388 | 57 | d2[key] = set([tuple(values)]) |
paul@388 | 58 | return d2 |
paul@388 | 59 | |
paul@388 | 60 | def combine_set_dicts(d1, d2): |
paul@388 | 61 | |
paul@388 | 62 | """ |
paul@388 | 63 | Combine dictionaries 'd1' and 'd2' in a resulting dictionary, with the set |
paul@388 | 64 | values of the contributing dictionaries being merged such that a "product" |
paul@388 | 65 | of the values for a given key are stored in the combined dictionary. |
paul@388 | 66 | """ |
paul@388 | 67 | |
paul@388 | 68 | combined = {} |
paul@388 | 69 | d2_keys = d2.keys() |
paul@388 | 70 | |
paul@388 | 71 | for key in d2_keys: |
paul@388 | 72 | |
paul@388 | 73 | d1_values = d1.get(key) |
paul@388 | 74 | |
paul@388 | 75 | if d1_values is None: |
paul@388 | 76 | combined[key] = d2[key] |
paul@388 | 77 | else: |
paul@388 | 78 | combined[key] = set() |
paul@388 | 79 | for d2_value in d2[key]: |
paul@388 | 80 | for d1_value in d1_values: |
paul@388 | 81 | combined[key].add(tuple(set(d1_value).union(d2_value))) |
paul@388 | 82 | |
paul@388 | 83 | for key in d1.keys(): |
paul@388 | 84 | if key not in d2_keys: |
paul@388 | 85 | combined[key] = d1[key] |
paul@388 | 86 | |
paul@388 | 87 | return combined |
paul@387 | 88 | |
paul@357 | 89 | # Visitors and activities related to node annotations. |
paul@339 | 90 | |
paul@339 | 91 | class ASTVisitor(compiler.visitor.ASTVisitor): |
paul@339 | 92 | |
paul@339 | 93 | "A base class for visitors." |
paul@339 | 94 | |
paul@339 | 95 | def dispatch(self, node, *args): |
paul@357 | 96 | |
paul@357 | 97 | "Dispatch using 'node', annotating any raised exceptions." |
paul@357 | 98 | |
paul@339 | 99 | try: |
paul@339 | 100 | return compiler.visitor.ASTVisitor.dispatch(self, node, *args) |
paul@339 | 101 | except NodeProcessingError, exc: |
paul@340 | 102 | if exc.astnode is None: |
paul@340 | 103 | exc.astnode = node |
paul@342 | 104 | |
paul@342 | 105 | # NOTE: Should perhaps specialise the subclasses appropriately. |
paul@342 | 106 | |
paul@342 | 107 | if hasattr(self, "unit"): |
paul@342 | 108 | exc.unit_name = self.unit.full_name() |
paul@342 | 109 | else: |
paul@342 | 110 | exc.unit_name = self.full_name() |
paul@339 | 111 | raise |
paul@339 | 112 | |
paul@357 | 113 | def possible_accessor_types(self, node): |
paul@357 | 114 | |
paul@357 | 115 | """ |
paul@357 | 116 | Given annotations made during the inspection process, return all possible |
paul@357 | 117 | types for a 'node' involved in attribute access, or return None if no |
paul@357 | 118 | annotations are available. |
paul@357 | 119 | """ |
paul@357 | 120 | |
paul@357 | 121 | if hasattr(node, "_attrusers"): |
paul@357 | 122 | target_names = set() |
paul@357 | 123 | |
paul@381 | 124 | # Visit each attribute user. |
paul@381 | 125 | |
paul@357 | 126 | for user in node._attrusers: |
paul@381 | 127 | |
paul@381 | 128 | # Since users such as branches may not provide type information, |
paul@381 | 129 | # attempt to find defining users. |
paul@381 | 130 | |
paul@381 | 131 | for def_user in user._attrdefs or [user]: |
paul@384 | 132 | for target_name, is_static in def_user._attrtypes.get(node._username, []): |
paul@381 | 133 | target_names.add(target_name) |
paul@357 | 134 | |
paul@357 | 135 | return target_names |
paul@357 | 136 | |
paul@357 | 137 | else: |
paul@357 | 138 | return None |
paul@357 | 139 | |
paul@353 | 140 | def used_by_unit(node): |
paul@353 | 141 | |
paul@353 | 142 | """ |
paul@353 | 143 | Return whether the definition made by a 'node' is actually employed by the |
paul@353 | 144 | program unit within which it is found. |
paul@353 | 145 | """ |
paul@353 | 146 | |
paul@370 | 147 | return hasattr(node, "unit") and node.unit.parent.has_key(node.unit.name) |
paul@353 | 148 | |
paul@83 | 149 | # Errors. |
paul@83 | 150 | |
paul@39 | 151 | class ProcessingError(Exception): |
paul@39 | 152 | |
paul@39 | 153 | "A processing error." |
paul@39 | 154 | |
paul@63 | 155 | pass |
paul@63 | 156 | |
paul@84 | 157 | class TableError(ProcessingError): |
paul@84 | 158 | |
paul@84 | 159 | "An error occurring during access to a lookup table." |
paul@84 | 160 | |
paul@84 | 161 | pass |
paul@84 | 162 | |
paul@243 | 163 | class TableGenerationError(ProcessingError): |
paul@243 | 164 | |
paul@243 | 165 | "An error occurring when generating a lookup table." |
paul@243 | 166 | |
paul@243 | 167 | pass |
paul@243 | 168 | |
paul@63 | 169 | class NodeProcessingError(ProcessingError): |
paul@63 | 170 | |
paul@63 | 171 | "A processing error associated with a particular program node." |
paul@63 | 172 | |
paul@339 | 173 | def __init__(self, message): |
paul@39 | 174 | self.message = message |
paul@339 | 175 | self.unit_name = None |
paul@339 | 176 | self.astnode = None |
paul@339 | 177 | |
paul@339 | 178 | def get_lineno(self, node): |
paul@339 | 179 | lineno = node.lineno |
paul@339 | 180 | if lineno is not None: |
paul@339 | 181 | return lineno |
paul@339 | 182 | else: |
paul@339 | 183 | for child in node.getChildNodes(): |
paul@339 | 184 | lineno = self.get_lineno(child) |
paul@339 | 185 | if lineno is not None: |
paul@339 | 186 | return lineno |
paul@339 | 187 | return None |
paul@39 | 188 | |
paul@39 | 189 | def __repr__(self): |
paul@339 | 190 | return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) |
paul@39 | 191 | |
paul@39 | 192 | def __str__(self): |
paul@39 | 193 | return repr(self) |
paul@39 | 194 | |
paul@296 | 195 | class InspectError(NodeProcessingError): |
paul@296 | 196 | |
paul@296 | 197 | "An error during the module inspection process." |
paul@296 | 198 | |
paul@296 | 199 | pass |
paul@296 | 200 | |
paul@83 | 201 | class TranslateError(NodeProcessingError): |
paul@83 | 202 | |
paul@83 | 203 | "An error during the module translation process." |
paul@83 | 204 | |
paul@83 | 205 | pass |
paul@83 | 206 | |
paul@100 | 207 | class TranslationNotImplementedError(TranslateError): |
paul@100 | 208 | |
paul@100 | 209 | "An error caused by a node not being supported in translation." |
paul@100 | 210 | |
paul@100 | 211 | pass |
paul@100 | 212 | |
paul@343 | 213 | # Special representations. |
paul@343 | 214 | |
paul@343 | 215 | class AtLeast: |
paul@343 | 216 | |
paul@343 | 217 | "A special representation for numbers of a given value or greater." |
paul@343 | 218 | |
paul@343 | 219 | def __init__(self, count): |
paul@343 | 220 | self.count = count |
paul@343 | 221 | |
paul@343 | 222 | def __eq__(self, other): |
paul@343 | 223 | return 0 |
paul@343 | 224 | |
paul@343 | 225 | __lt__ = __le__ = __eq__ |
paul@343 | 226 | |
paul@343 | 227 | def __ne__(self, other): |
paul@343 | 228 | return 1 |
paul@343 | 229 | |
paul@343 | 230 | def __gt__(self, other): |
paul@343 | 231 | if isinstance(other, AtLeast): |
paul@343 | 232 | return 0 |
paul@343 | 233 | else: |
paul@343 | 234 | return self.count > other |
paul@343 | 235 | |
paul@343 | 236 | def __ge__(self, other): |
paul@343 | 237 | if isinstance(other, AtLeast): |
paul@343 | 238 | return 0 |
paul@343 | 239 | else: |
paul@343 | 240 | return self.count >= other |
paul@343 | 241 | |
paul@343 | 242 | def __iadd__(self, other): |
paul@343 | 243 | if isinstance(other, AtLeast): |
paul@343 | 244 | self.count += other.count |
paul@343 | 245 | else: |
paul@343 | 246 | self.count += other |
paul@343 | 247 | return self |
paul@343 | 248 | |
paul@343 | 249 | def __radd__(self, other): |
paul@343 | 250 | if isinstance(other, AtLeast): |
paul@343 | 251 | return AtLeast(self.count + other.count) |
paul@343 | 252 | else: |
paul@343 | 253 | return AtLeast(self.count + other) |
paul@343 | 254 | |
paul@343 | 255 | def __repr__(self): |
paul@343 | 256 | return "AtLeast(%r)" % self.count |
paul@343 | 257 | |
paul@172 | 258 | # Useful data. |
paul@172 | 259 | |
paul@172 | 260 | comparison_methods = { |
paul@232 | 261 | "==" : ("__eq__", "__eq__"), |
paul@232 | 262 | "!=" : ("__ne__", "__ne__"), |
paul@172 | 263 | "<" : ("__lt__", "__gt__"), |
paul@172 | 264 | "<=" : ("__le__", "__ge__"), |
paul@172 | 265 | ">=" : ("__ge__", "__le__"), |
paul@172 | 266 | ">" : ("__gt__", "__lt__"), |
paul@172 | 267 | "is" : None, |
paul@172 | 268 | "is not" : None, |
paul@172 | 269 | "in" : None, |
paul@172 | 270 | "not in" : None |
paul@172 | 271 | } |
paul@172 | 272 | |
paul@172 | 273 | augassign_methods = { |
paul@172 | 274 | "+=" : ("__iadd__", ("__add__", "__radd__")), |
paul@172 | 275 | "-=" : ("__isub__", ("__sub__", "__rsub__")), |
paul@172 | 276 | "*=" : ("__imul__", ("__mul__", "__rmul__")), |
paul@172 | 277 | "/=" : ("__idiv__", ("__div__", "__rdiv__")), |
paul@324 | 278 | "//=" : ("__ifloordiv__", ("__floordiv__", "__rfloordiv__")), |
paul@172 | 279 | "%=" : ("__imod__", ("__mod__", "__rmod__")), |
paul@172 | 280 | "**=" : ("__ipow__", ("__pow__", "__rpow__")), |
paul@172 | 281 | "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), |
paul@172 | 282 | ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), |
paul@172 | 283 | "&=" : ("__iand__", ("__and__", "__rand__")), |
paul@172 | 284 | "^=" : ("__ixor__", ("__xor__", "__rxor__")), |
paul@172 | 285 | "|=" : ("__ior__", ("__or__", "__ror__")) |
paul@172 | 286 | } |
paul@172 | 287 | |
paul@172 | 288 | binary_methods = { |
paul@172 | 289 | "Add" : ("__add__", "__radd__"), |
paul@172 | 290 | "Bitand" : ("__and__", "__rand__"), |
paul@172 | 291 | "Bitor" : ("__or__", "__ror__"), |
paul@172 | 292 | "Bitxor" : ("__xor__", "__rxor__"), |
paul@172 | 293 | "Div" : ("__div__", "__rdiv__"), |
paul@172 | 294 | "FloorDiv" : ("__floordiv__", "__rfloordiv__"), |
paul@172 | 295 | "LeftShift" : ("__lshift__", "__rlshift__"), |
paul@172 | 296 | "Mod" : ("__mod__", "__rmod__"), |
paul@172 | 297 | "Mul" : ("__mul__", "__rmul__"), |
paul@172 | 298 | "Power" : ("__pow__", "__rpow__"), |
paul@172 | 299 | "RightShift" : ("__rshift__", "__rrshift__"), |
paul@172 | 300 | "Sub" : ("__sub__", "__rsub__") |
paul@172 | 301 | } |
paul@172 | 302 | |
paul@172 | 303 | unary_methods = { |
paul@172 | 304 | "Invert" : "__invert__", |
paul@172 | 305 | "UnaryAdd" : "__pos__", |
paul@172 | 306 | "UnarySub" : "__neg__" |
paul@172 | 307 | } |
paul@172 | 308 | |
paul@354 | 309 | operator_functions = { |
paul@354 | 310 | |
paul@354 | 311 | # Binary operations. |
paul@354 | 312 | |
paul@354 | 313 | "Add" : "add", |
paul@354 | 314 | "Bitand" : "and_", |
paul@354 | 315 | "Bitor" : "or_", |
paul@354 | 316 | "Bitxor" : "xor", |
paul@354 | 317 | "Div" : "div", |
paul@354 | 318 | "FloorDiv" : "floordiv", |
paul@354 | 319 | "LeftShift" : "lshift", |
paul@354 | 320 | "Mod" : "mod", |
paul@354 | 321 | "Mul" : "mul", |
paul@354 | 322 | "Power" : "pow", |
paul@354 | 323 | "RightShift" : "rshift", |
paul@354 | 324 | "Sub" : "sub", |
paul@354 | 325 | |
paul@354 | 326 | # Unary operations. |
paul@354 | 327 | |
paul@354 | 328 | "Invert" : "invert", |
paul@354 | 329 | "UnaryAdd" : "pos", |
paul@354 | 330 | "UnarySub" : "neg", |
paul@354 | 331 | |
paul@354 | 332 | # Augmented assignment. |
paul@354 | 333 | |
paul@354 | 334 | "+=" : "iadd", |
paul@354 | 335 | "-=" : "isub", |
paul@354 | 336 | "*=" : "imul", |
paul@354 | 337 | "/=" : "idiv", |
paul@354 | 338 | "//=" : "ifloordiv", |
paul@354 | 339 | "%=" : "imod", |
paul@354 | 340 | "**=" : "ipow", |
paul@354 | 341 | "<<=" : "ilshift", |
paul@354 | 342 | ">>=" : "irshift", |
paul@354 | 343 | "&=" : "iand", |
paul@354 | 344 | "^=" : "ixor", |
paul@354 | 345 | "|=" : "ior", |
paul@354 | 346 | |
paul@354 | 347 | # Comparisons. |
paul@354 | 348 | |
paul@354 | 349 | "==" : "eq", |
paul@354 | 350 | "!=" : "ne", |
paul@354 | 351 | "<" : "lt", |
paul@354 | 352 | "<=" : "le", |
paul@354 | 353 | ">=" : "ge", |
paul@374 | 354 | ">" : "gt", |
paul@374 | 355 | |
paul@374 | 356 | # Access and slicing. |
paul@374 | 357 | |
paul@374 | 358 | "AssSlice" : "setslice", |
paul@374 | 359 | "Slice" : "getslice", |
paul@374 | 360 | "AssSubscript" : "setitem", |
paul@374 | 361 | "Subscript" : "getitem", |
paul@354 | 362 | } |
paul@354 | 363 | |
paul@39 | 364 | # vim: tabstop=4 expandtab shiftwidth=4 |