paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@39 | 3 | """ |
paul@39 | 4 | Common classes. |
paul@39 | 5 | |
paul@645 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 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@602 | 22 | from compiler.ast import AssAttr, Getattr, Name |
paul@602 | 23 | import compiler.ast |
paul@645 | 24 | from micropython.basicdata import Const, Constant |
paul@602 | 25 | from micropython.data import Attr, Class, Module |
paul@556 | 26 | from micropython.errors import * |
paul@339 | 27 | |
paul@582 | 28 | try: |
paul@582 | 29 | set |
paul@582 | 30 | except NameError: |
paul@582 | 31 | from sets import Set as set |
paul@582 | 32 | |
paul@357 | 33 | # Visitors and activities related to node annotations. |
paul@339 | 34 | |
paul@496 | 35 | class ASTVisitor: |
paul@339 | 36 | |
paul@339 | 37 | "A base class for visitors." |
paul@339 | 38 | |
paul@645 | 39 | def process_definitions(self, node): |
paul@645 | 40 | |
paul@645 | 41 | """ |
paul@645 | 42 | Process and return all definitions beneath 'node', but not definitions |
paul@645 | 43 | within definitions. |
paul@645 | 44 | """ |
paul@645 | 45 | |
paul@645 | 46 | definitions = [] |
paul@645 | 47 | for n in node.getChildNodes(): |
paul@645 | 48 | if isinstance(n, (compiler.ast.Class, compiler.ast.Function)): |
paul@645 | 49 | definitions.append(self.dispatch(n)) |
paul@645 | 50 | else: |
paul@645 | 51 | definitions += self.process_definitions(n) |
paul@645 | 52 | return definitions |
paul@645 | 53 | |
paul@645 | 54 | # Visitor support methods. |
paul@645 | 55 | |
paul@496 | 56 | def default(self, node, *args): |
paul@496 | 57 | for n in node.getChildNodes(): |
paul@496 | 58 | self.dispatch(n) |
paul@496 | 59 | |
paul@339 | 60 | def dispatch(self, node, *args): |
paul@357 | 61 | |
paul@357 | 62 | "Dispatch using 'node', annotating any raised exceptions." |
paul@357 | 63 | |
paul@496 | 64 | # Dispatch via a generic visit method. |
paul@496 | 65 | |
paul@339 | 66 | try: |
paul@496 | 67 | return node.visit(self, *args) |
paul@496 | 68 | |
paul@496 | 69 | # Annotate the exception in case of failure. |
paul@496 | 70 | |
paul@339 | 71 | except NodeProcessingError, exc: |
paul@340 | 72 | if exc.astnode is None: |
paul@340 | 73 | exc.astnode = node |
paul@623 | 74 | exc.unit_name = self.get_unit().full_name() |
paul@339 | 75 | raise |
paul@339 | 76 | |
paul@645 | 77 | # Deduction-related methods. |
paul@645 | 78 | |
paul@646 | 79 | def get_attributes(self, targets, attrname): |
paul@646 | 80 | |
paul@646 | 81 | "Return a list of attributes for 'targets' supporting 'attrname'." |
paul@646 | 82 | |
paul@646 | 83 | attributes = set() |
paul@646 | 84 | |
paul@646 | 85 | for target in targets: |
paul@646 | 86 | try: |
paul@646 | 87 | attributes.add(self.objtable.access(target.full_name(), attrname)) |
paul@646 | 88 | except TableError: |
paul@648 | 89 | pass |
paul@646 | 90 | |
paul@646 | 91 | return attributes |
paul@646 | 92 | |
paul@646 | 93 | def get_attribute_and_value(self, obj): |
paul@646 | 94 | |
paul@646 | 95 | """ |
paul@646 | 96 | Return (attribute, value) details for the given 'obj', where an |
paul@646 | 97 | attribute of None can be returned for constant objects, and where None |
paul@646 | 98 | can be returned as the result where no concrete details can be provided. |
paul@646 | 99 | """ |
paul@646 | 100 | |
paul@646 | 101 | if isinstance(obj, Constant): |
paul@646 | 102 | return None, obj |
paul@646 | 103 | |
paul@646 | 104 | if isinstance(obj, Attr): |
paul@646 | 105 | return obj, obj.get_value() |
paul@646 | 106 | |
paul@646 | 107 | return None |
paul@646 | 108 | |
paul@646 | 109 | def provides_constant_result(self, value): |
paul@646 | 110 | |
paul@646 | 111 | "Return whether 'value' provides a constant result." |
paul@646 | 112 | |
paul@646 | 113 | return isinstance(value, (Const, Constant)) |
paul@646 | 114 | |
paul@645 | 115 | def provides_self_access(self, node, unit): |
paul@645 | 116 | |
paul@645 | 117 | """ |
paul@645 | 118 | Return whether the 'node' in the given 'unit' provides a self-based |
paul@645 | 119 | attribute access. |
paul@645 | 120 | """ |
paul@645 | 121 | |
paul@645 | 122 | attr_value = self.get_attribute_and_value(node._expr) |
paul@645 | 123 | |
paul@645 | 124 | if attr_value: |
paul@645 | 125 | target, value = attr_value |
paul@645 | 126 | |
paul@645 | 127 | return target and target.name == "self" and target.parent is unit and \ |
paul@645 | 128 | unit.is_method() |
paul@645 | 129 | |
paul@645 | 130 | return False |
paul@645 | 131 | |
paul@645 | 132 | def possible_attributes_from_annotation(self, node): |
paul@645 | 133 | |
paul@645 | 134 | """ |
paul@645 | 135 | Return (attribute, value) details provided by any _expr or _attr |
paul@645 | 136 | annotations on 'node'. |
paul@645 | 137 | """ |
paul@645 | 138 | |
paul@645 | 139 | attr_value = self.get_attribute_and_value(node._attr) |
paul@645 | 140 | |
paul@645 | 141 | if attr_value: |
paul@645 | 142 | return [attr_value] |
paul@645 | 143 | |
paul@645 | 144 | attrs = set() |
paul@645 | 145 | expr = node._expr |
paul@645 | 146 | |
paul@645 | 147 | if expr: |
paul@645 | 148 | |
paul@645 | 149 | # Permitting multiple expression types if they provide the |
paul@645 | 150 | # attribute. |
paul@645 | 151 | |
paul@645 | 152 | if isinstance(expr, Attr): |
paul@645 | 153 | exprs = expr.get_values() |
paul@645 | 154 | else: |
paul@645 | 155 | exprs = [expr] |
paul@645 | 156 | |
paul@645 | 157 | # For each expression value try and get a concrete |
paul@645 | 158 | # attribute. |
paul@645 | 159 | |
paul@645 | 160 | for expr in exprs: |
paul@645 | 161 | attr = expr.all_attributes().get(node.attrname) |
paul@645 | 162 | |
paul@645 | 163 | # Where an attribute can be obtained, record its |
paul@645 | 164 | # details. |
paul@645 | 165 | |
paul@645 | 166 | if attr: |
paul@645 | 167 | attrs.add((attr, attr.get_value())) |
paul@645 | 168 | |
paul@645 | 169 | return attrs |
paul@645 | 170 | |
paul@645 | 171 | def possible_accessor_types_from_usage(self, node, defining_users=1): |
paul@645 | 172 | |
paul@645 | 173 | """ |
paul@645 | 174 | Return a set of (target name, static) tuples from an investigation of |
paul@645 | 175 | attribute usage observations stored on the given 'node'. |
paul@645 | 176 | |
paul@645 | 177 | If 'defining_users' is set to a false value, attempt to get the type |
paul@645 | 178 | names specifically applicable to the node, rather than retrieving more |
paul@645 | 179 | general definition-based type observations. |
paul@645 | 180 | """ |
paul@645 | 181 | |
paul@645 | 182 | target_names = set() |
paul@645 | 183 | |
paul@645 | 184 | if node._attrusers: |
paul@645 | 185 | |
paul@645 | 186 | # Visit each attribute user. |
paul@645 | 187 | |
paul@645 | 188 | for user in node._attrusers: |
paul@645 | 189 | |
paul@645 | 190 | # Since users such as branches may not provide type information, |
paul@645 | 191 | # attempt to find defining users. |
paul@645 | 192 | |
paul@645 | 193 | if defining_users: |
paul@645 | 194 | for def_user in user._attrdefs or [user]: |
paul@645 | 195 | for target_name, is_static in def_user._attrtypes.get(node._username, []): |
paul@645 | 196 | target_names.add((target_name, is_static)) |
paul@645 | 197 | else: |
paul@645 | 198 | for target_name, is_static in user._attrspecifictypes.get(node._username, []): |
paul@645 | 199 | target_names.add((target_name, is_static)) |
paul@645 | 200 | |
paul@645 | 201 | return target_names |
paul@645 | 202 | |
paul@645 | 203 | def possible_accessors_from_usage(self, node, defining_users=1): |
paul@645 | 204 | |
paul@645 | 205 | """ |
paul@645 | 206 | Return possible accessors from the usage recorded on the given 'node'. |
paul@645 | 207 | |
paul@645 | 208 | If 'defining_users' is set to a false value, attempt to get the type |
paul@645 | 209 | names specifically applicable to the node, rather than retrieving more |
paul@645 | 210 | general definition-based type observations. |
paul@645 | 211 | """ |
paul@645 | 212 | |
paul@645 | 213 | targets = set() |
paul@645 | 214 | target_names = self.possible_accessor_types_from_usage(node, defining_users) |
paul@645 | 215 | |
paul@645 | 216 | if target_names: |
paul@645 | 217 | for target_name, is_static in target_names: |
paul@645 | 218 | targets.add(self.objtable.get_object(target_name)) |
paul@645 | 219 | |
paul@645 | 220 | return targets |
paul@645 | 221 | |
paul@646 | 222 | def possible_accessors_for_attribute(self, attrname): |
paul@646 | 223 | |
paul@646 | 224 | "Return possible accessors given the single 'attrname'." |
paul@646 | 225 | |
paul@646 | 226 | targets = set() |
paul@646 | 227 | |
paul@646 | 228 | for target_name in self.objtable.any_possible_objects([attrname]): |
paul@646 | 229 | targets.add(self.objtable.get_object(target_name)) |
paul@646 | 230 | |
paul@646 | 231 | return targets |
paul@646 | 232 | |
paul@353 | 233 | def used_by_unit(node): |
paul@353 | 234 | |
paul@353 | 235 | """ |
paul@353 | 236 | Return whether the definition made by a 'node' is actually employed by the |
paul@353 | 237 | program unit within which it is found. |
paul@353 | 238 | """ |
paul@353 | 239 | |
paul@599 | 240 | return node.unit and node.unit.parent.has_key(node.unit.name) |
paul@353 | 241 | |
paul@172 | 242 | # Useful data. |
paul@172 | 243 | |
paul@354 | 244 | operator_functions = { |
paul@354 | 245 | |
paul@354 | 246 | # Binary operations. |
paul@354 | 247 | |
paul@354 | 248 | "Add" : "add", |
paul@354 | 249 | "Bitand" : "and_", |
paul@354 | 250 | "Bitor" : "or_", |
paul@354 | 251 | "Bitxor" : "xor", |
paul@354 | 252 | "Div" : "div", |
paul@354 | 253 | "FloorDiv" : "floordiv", |
paul@354 | 254 | "LeftShift" : "lshift", |
paul@354 | 255 | "Mod" : "mod", |
paul@354 | 256 | "Mul" : "mul", |
paul@354 | 257 | "Power" : "pow", |
paul@354 | 258 | "RightShift" : "rshift", |
paul@354 | 259 | "Sub" : "sub", |
paul@354 | 260 | |
paul@354 | 261 | # Unary operations. |
paul@354 | 262 | |
paul@354 | 263 | "Invert" : "invert", |
paul@354 | 264 | "UnaryAdd" : "pos", |
paul@354 | 265 | "UnarySub" : "neg", |
paul@354 | 266 | |
paul@354 | 267 | # Augmented assignment. |
paul@354 | 268 | |
paul@354 | 269 | "+=" : "iadd", |
paul@354 | 270 | "-=" : "isub", |
paul@354 | 271 | "*=" : "imul", |
paul@354 | 272 | "/=" : "idiv", |
paul@354 | 273 | "//=" : "ifloordiv", |
paul@354 | 274 | "%=" : "imod", |
paul@354 | 275 | "**=" : "ipow", |
paul@354 | 276 | "<<=" : "ilshift", |
paul@354 | 277 | ">>=" : "irshift", |
paul@354 | 278 | "&=" : "iand", |
paul@354 | 279 | "^=" : "ixor", |
paul@354 | 280 | "|=" : "ior", |
paul@354 | 281 | |
paul@354 | 282 | # Comparisons. |
paul@354 | 283 | |
paul@354 | 284 | "==" : "eq", |
paul@354 | 285 | "!=" : "ne", |
paul@354 | 286 | "<" : "lt", |
paul@354 | 287 | "<=" : "le", |
paul@354 | 288 | ">=" : "ge", |
paul@374 | 289 | ">" : "gt", |
paul@374 | 290 | |
paul@374 | 291 | # Access and slicing. |
paul@374 | 292 | |
paul@623 | 293 | "AssSlice" : "setslice", # Python 2.7 |
paul@374 | 294 | "Slice" : "getslice", |
paul@623 | 295 | "AssSubscript" : "setitem", # Python 2.7 |
paul@374 | 296 | "Subscript" : "getitem", |
paul@354 | 297 | } |
paul@354 | 298 | |
paul@39 | 299 | # vim: tabstop=4 expandtab shiftwidth=4 |