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