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@645 | 79 | def provides_self_access(self, node, unit): |
paul@645 | 80 | |
paul@645 | 81 | """ |
paul@645 | 82 | Return whether the 'node' in the given 'unit' provides a self-based |
paul@645 | 83 | attribute access. |
paul@645 | 84 | """ |
paul@645 | 85 | |
paul@645 | 86 | attr_value = self.get_attribute_and_value(node._expr) |
paul@645 | 87 | |
paul@645 | 88 | if attr_value: |
paul@645 | 89 | target, value = attr_value |
paul@645 | 90 | |
paul@645 | 91 | return target and target.name == "self" and target.parent is unit and \ |
paul@645 | 92 | unit.is_method() |
paul@645 | 93 | |
paul@645 | 94 | return False |
paul@645 | 95 | |
paul@488 | 96 | def possible_accessor_types(self, node, defining_users=1): |
paul@357 | 97 | |
paul@357 | 98 | """ |
paul@357 | 99 | Given annotations made during the inspection process, return all possible |
paul@401 | 100 | type names and indications of static usage for a 'node' involved in |
paul@401 | 101 | attribute access. |
paul@488 | 102 | |
paul@488 | 103 | If 'defining_users' is set to a false value, attempt to get the type |
paul@488 | 104 | names specifically applicable to the node, rather than retrieving more |
paul@488 | 105 | general definition-based type observations. |
paul@357 | 106 | """ |
paul@357 | 107 | |
paul@610 | 108 | all_target_names = [] |
paul@401 | 109 | |
paul@596 | 110 | # Where an attribute could already be detected and where its nature is |
paul@596 | 111 | # not that of a general instance or an unresolved name, attempt to |
paul@596 | 112 | # identify it. |
paul@596 | 113 | |
paul@645 | 114 | # Use explicit annotations on the node. |
paul@612 | 115 | |
paul@645 | 116 | attrs = self.possible_attributes_from_annotation(node) |
paul@645 | 117 | if attrs: |
paul@645 | 118 | target_names = set() |
paul@645 | 119 | for (attr, value) in attrs: |
paul@645 | 120 | # NOTE: Ignoring constant objects. |
paul@645 | 121 | if attr: |
paul@645 | 122 | target_names.add((attr.parent.full_name(), attr.is_static_attribute())) |
paul@645 | 123 | all_target_names.append(target_names) |
paul@381 | 124 | |
paul@645 | 125 | # Use attribute usage observations. |
paul@381 | 126 | |
paul@645 | 127 | target_names = self.possible_accessor_types_from_usage(node, defining_users) |
paul@645 | 128 | if target_names: |
paul@645 | 129 | all_target_names.append(target_names) |
paul@610 | 130 | |
paul@610 | 131 | # Return the smallest set of target names. |
paul@610 | 132 | |
paul@610 | 133 | all_target_names.sort(key=lambda x: len(x)) |
paul@612 | 134 | |
paul@610 | 135 | return all_target_names and all_target_names[0] |
paul@357 | 136 | |
paul@645 | 137 | def get_attribute_and_value(self, obj): |
paul@645 | 138 | |
paul@645 | 139 | """ |
paul@645 | 140 | Return (attribute, value) details for the given 'obj', where an |
paul@645 | 141 | attribute of None can be returned for constant objects, and where None |
paul@645 | 142 | can be returned as the result where no concrete details can be provided. |
paul@645 | 143 | """ |
paul@645 | 144 | |
paul@645 | 145 | if isinstance(obj, Constant): |
paul@645 | 146 | return None, obj |
paul@645 | 147 | |
paul@645 | 148 | if isinstance(obj, Attr): |
paul@645 | 149 | return obj, obj.get_value() |
paul@645 | 150 | |
paul@645 | 151 | return None |
paul@645 | 152 | |
paul@645 | 153 | def possible_attributes_from_annotation(self, node): |
paul@645 | 154 | |
paul@645 | 155 | """ |
paul@645 | 156 | Return (attribute, value) details provided by any _expr or _attr |
paul@645 | 157 | annotations on 'node'. |
paul@645 | 158 | """ |
paul@645 | 159 | |
paul@645 | 160 | attr_value = self.get_attribute_and_value(node._attr) |
paul@645 | 161 | |
paul@645 | 162 | if attr_value: |
paul@645 | 163 | return [attr_value] |
paul@645 | 164 | |
paul@645 | 165 | attrs = set() |
paul@645 | 166 | expr = node._expr |
paul@645 | 167 | |
paul@645 | 168 | if expr: |
paul@645 | 169 | |
paul@645 | 170 | # Permitting multiple expression types if they provide the |
paul@645 | 171 | # attribute. |
paul@645 | 172 | |
paul@645 | 173 | if isinstance(expr, Attr): |
paul@645 | 174 | exprs = expr.get_values() |
paul@645 | 175 | else: |
paul@645 | 176 | exprs = [expr] |
paul@645 | 177 | |
paul@645 | 178 | # For each expression value try and get a concrete |
paul@645 | 179 | # attribute. |
paul@645 | 180 | |
paul@645 | 181 | for expr in exprs: |
paul@645 | 182 | attr = expr.all_attributes().get(node.attrname) |
paul@645 | 183 | |
paul@645 | 184 | # Where an attribute can be obtained, record its |
paul@645 | 185 | # details. |
paul@645 | 186 | |
paul@645 | 187 | if attr: |
paul@645 | 188 | attrs.add((attr, attr.get_value())) |
paul@645 | 189 | |
paul@645 | 190 | return attrs |
paul@645 | 191 | |
paul@645 | 192 | def possible_accessor_types_from_usage(self, node, defining_users=1): |
paul@645 | 193 | |
paul@645 | 194 | """ |
paul@645 | 195 | Return a set of (target name, static) tuples from an investigation of |
paul@645 | 196 | attribute usage observations stored on the given 'node'. |
paul@645 | 197 | |
paul@645 | 198 | If 'defining_users' is set to a false value, attempt to get the type |
paul@645 | 199 | names specifically applicable to the node, rather than retrieving more |
paul@645 | 200 | general definition-based type observations. |
paul@645 | 201 | """ |
paul@645 | 202 | |
paul@645 | 203 | target_names = set() |
paul@645 | 204 | |
paul@645 | 205 | if node._attrusers: |
paul@645 | 206 | |
paul@645 | 207 | # Visit each attribute user. |
paul@645 | 208 | |
paul@645 | 209 | for user in node._attrusers: |
paul@645 | 210 | |
paul@645 | 211 | # Since users such as branches may not provide type information, |
paul@645 | 212 | # attempt to find defining users. |
paul@645 | 213 | |
paul@645 | 214 | if defining_users: |
paul@645 | 215 | for def_user in user._attrdefs or [user]: |
paul@645 | 216 | for target_name, is_static in def_user._attrtypes.get(node._username, []): |
paul@645 | 217 | target_names.add((target_name, is_static)) |
paul@645 | 218 | else: |
paul@645 | 219 | for target_name, is_static in user._attrspecifictypes.get(node._username, []): |
paul@645 | 220 | target_names.add((target_name, is_static)) |
paul@645 | 221 | |
paul@645 | 222 | return target_names |
paul@645 | 223 | |
paul@645 | 224 | def possible_accessors_from_usage(self, node, defining_users=1): |
paul@645 | 225 | |
paul@645 | 226 | """ |
paul@645 | 227 | Return possible accessors from the usage recorded on the given 'node'. |
paul@645 | 228 | |
paul@645 | 229 | If 'defining_users' is set to a false value, attempt to get the type |
paul@645 | 230 | names specifically applicable to the node, rather than retrieving more |
paul@645 | 231 | general definition-based type observations. |
paul@645 | 232 | """ |
paul@645 | 233 | |
paul@645 | 234 | targets = set() |
paul@645 | 235 | target_names = self.possible_accessor_types_from_usage(node, defining_users) |
paul@645 | 236 | |
paul@645 | 237 | if target_names: |
paul@645 | 238 | for target_name, is_static in target_names: |
paul@645 | 239 | targets.add(self.objtable.get_object(target_name)) |
paul@645 | 240 | |
paul@645 | 241 | return targets |
paul@645 | 242 | |
paul@353 | 243 | def used_by_unit(node): |
paul@353 | 244 | |
paul@353 | 245 | """ |
paul@353 | 246 | Return whether the definition made by a 'node' is actually employed by the |
paul@353 | 247 | program unit within which it is found. |
paul@353 | 248 | """ |
paul@353 | 249 | |
paul@599 | 250 | return node.unit and node.unit.parent.has_key(node.unit.name) |
paul@353 | 251 | |
paul@172 | 252 | # Useful data. |
paul@172 | 253 | |
paul@354 | 254 | operator_functions = { |
paul@354 | 255 | |
paul@354 | 256 | # Binary operations. |
paul@354 | 257 | |
paul@354 | 258 | "Add" : "add", |
paul@354 | 259 | "Bitand" : "and_", |
paul@354 | 260 | "Bitor" : "or_", |
paul@354 | 261 | "Bitxor" : "xor", |
paul@354 | 262 | "Div" : "div", |
paul@354 | 263 | "FloorDiv" : "floordiv", |
paul@354 | 264 | "LeftShift" : "lshift", |
paul@354 | 265 | "Mod" : "mod", |
paul@354 | 266 | "Mul" : "mul", |
paul@354 | 267 | "Power" : "pow", |
paul@354 | 268 | "RightShift" : "rshift", |
paul@354 | 269 | "Sub" : "sub", |
paul@354 | 270 | |
paul@354 | 271 | # Unary operations. |
paul@354 | 272 | |
paul@354 | 273 | "Invert" : "invert", |
paul@354 | 274 | "UnaryAdd" : "pos", |
paul@354 | 275 | "UnarySub" : "neg", |
paul@354 | 276 | |
paul@354 | 277 | # Augmented assignment. |
paul@354 | 278 | |
paul@354 | 279 | "+=" : "iadd", |
paul@354 | 280 | "-=" : "isub", |
paul@354 | 281 | "*=" : "imul", |
paul@354 | 282 | "/=" : "idiv", |
paul@354 | 283 | "//=" : "ifloordiv", |
paul@354 | 284 | "%=" : "imod", |
paul@354 | 285 | "**=" : "ipow", |
paul@354 | 286 | "<<=" : "ilshift", |
paul@354 | 287 | ">>=" : "irshift", |
paul@354 | 288 | "&=" : "iand", |
paul@354 | 289 | "^=" : "ixor", |
paul@354 | 290 | "|=" : "ior", |
paul@354 | 291 | |
paul@354 | 292 | # Comparisons. |
paul@354 | 293 | |
paul@354 | 294 | "==" : "eq", |
paul@354 | 295 | "!=" : "ne", |
paul@354 | 296 | "<" : "lt", |
paul@354 | 297 | "<=" : "le", |
paul@354 | 298 | ">=" : "ge", |
paul@374 | 299 | ">" : "gt", |
paul@374 | 300 | |
paul@374 | 301 | # Access and slicing. |
paul@374 | 302 | |
paul@623 | 303 | "AssSlice" : "setslice", # Python 2.7 |
paul@374 | 304 | "Slice" : "getslice", |
paul@623 | 305 | "AssSubscript" : "setitem", # Python 2.7 |
paul@374 | 306 | "Subscript" : "getitem", |
paul@354 | 307 | } |
paul@354 | 308 | |
paul@39 | 309 | # vim: tabstop=4 expandtab shiftwidth=4 |