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