paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@39 | 3 | """ |
paul@39 | 4 | Common classes. |
paul@39 | 5 | |
paul@487 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 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@602 | 24 | from micropython.data import Attr, Class, Module |
paul@556 | 25 | from micropython.errors import * |
paul@339 | 26 | |
paul@582 | 27 | try: |
paul@582 | 28 | set |
paul@582 | 29 | except NameError: |
paul@582 | 30 | from sets import Set as set |
paul@582 | 31 | |
paul@357 | 32 | # Visitors and activities related to node annotations. |
paul@339 | 33 | |
paul@496 | 34 | class ASTVisitor: |
paul@339 | 35 | |
paul@339 | 36 | "A base class for visitors." |
paul@339 | 37 | |
paul@496 | 38 | def default(self, node, *args): |
paul@496 | 39 | for n in node.getChildNodes(): |
paul@496 | 40 | self.dispatch(n) |
paul@496 | 41 | |
paul@339 | 42 | def dispatch(self, node, *args): |
paul@357 | 43 | |
paul@357 | 44 | "Dispatch using 'node', annotating any raised exceptions." |
paul@357 | 45 | |
paul@496 | 46 | # Dispatch via a generic visit method. |
paul@496 | 47 | |
paul@339 | 48 | try: |
paul@496 | 49 | return node.visit(self, *args) |
paul@496 | 50 | |
paul@496 | 51 | # Annotate the exception in case of failure. |
paul@496 | 52 | |
paul@339 | 53 | except NodeProcessingError, exc: |
paul@340 | 54 | if exc.astnode is None: |
paul@340 | 55 | exc.astnode = node |
paul@623 | 56 | exc.unit_name = self.get_unit().full_name() |
paul@339 | 57 | raise |
paul@339 | 58 | |
paul@488 | 59 | def possible_accessor_types(self, node, defining_users=1): |
paul@357 | 60 | |
paul@357 | 61 | """ |
paul@357 | 62 | Given annotations made during the inspection process, return all possible |
paul@401 | 63 | type names and indications of static usage for a 'node' involved in |
paul@401 | 64 | attribute access. |
paul@488 | 65 | |
paul@488 | 66 | If 'defining_users' is set to a false value, attempt to get the type |
paul@488 | 67 | names specifically applicable to the node, rather than retrieving more |
paul@488 | 68 | general definition-based type observations. |
paul@357 | 69 | """ |
paul@357 | 70 | |
paul@610 | 71 | all_target_names = [] |
paul@401 | 72 | |
paul@596 | 73 | # Where an attribute could already be detected and where its nature is |
paul@596 | 74 | # not that of a general instance or an unresolved name, attempt to |
paul@596 | 75 | # identify it. |
paul@596 | 76 | |
paul@600 | 77 | if isinstance(node, (AssAttr, Getattr, Name)): |
paul@600 | 78 | |
paul@602 | 79 | # Use any explicit attribute annotation. |
paul@602 | 80 | |
paul@600 | 81 | if isinstance(node._attr, Attr): |
paul@600 | 82 | attr = node._attr |
paul@610 | 83 | all_target_names.append(set([(attr.parent.full_name(), attr.is_static_attribute())])) |
paul@602 | 84 | |
paul@602 | 85 | # Otherwise, try and use an expression annotation. |
paul@602 | 86 | |
paul@602 | 87 | if isinstance(node, (AssAttr, Getattr)): |
paul@602 | 88 | expr = node._expr |
paul@602 | 89 | |
paul@610 | 90 | # Permitting multiple expression types if they provide the |
paul@610 | 91 | # attribute. |
paul@610 | 92 | |
paul@610 | 93 | if isinstance(expr, Attr): |
paul@610 | 94 | exprs = expr.get_values() |
paul@610 | 95 | elif expr: |
paul@610 | 96 | exprs = [expr] |
paul@610 | 97 | else: |
paul@610 | 98 | exprs = None |
paul@602 | 99 | |
paul@610 | 100 | if exprs: |
paul@610 | 101 | target_names = set() |
paul@610 | 102 | |
paul@612 | 103 | # For each expression value try and get a concrete |
paul@612 | 104 | # attribute. |
paul@612 | 105 | |
paul@610 | 106 | for expr in exprs: |
paul@612 | 107 | attr = expr.all_attributes().get(node.attrname) |
paul@612 | 108 | |
paul@612 | 109 | # Where an attribute can be obtained, record its |
paul@612 | 110 | # details. |
paul@602 | 111 | |
paul@610 | 112 | if attr: |
paul@610 | 113 | target_names.add((attr.parent.full_name(), attr.is_static_attribute())) |
paul@610 | 114 | |
paul@610 | 115 | if target_names: |
paul@610 | 116 | all_target_names.append(target_names) |
paul@556 | 117 | |
paul@600 | 118 | # Otherwise, attempt to employ the attribute usage observations. |
paul@596 | 119 | |
paul@602 | 120 | if node._attrusers: |
paul@610 | 121 | target_names = set() |
paul@357 | 122 | |
paul@600 | 123 | # Visit each attribute user. |
paul@381 | 124 | |
paul@600 | 125 | for user in node._attrusers: |
paul@381 | 126 | |
paul@600 | 127 | # Since users such as branches may not provide type information, |
paul@600 | 128 | # attempt to find defining users. |
paul@381 | 129 | |
paul@600 | 130 | if defining_users: |
paul@600 | 131 | for def_user in user._attrdefs or [user]: |
paul@600 | 132 | for target_name, is_static in def_user._attrtypes.get(node._username, []): |
paul@600 | 133 | target_names.add((target_name, is_static)) |
paul@600 | 134 | else: |
paul@600 | 135 | for target_name, is_static in user._attrspecifictypes.get(node._username, []): |
paul@488 | 136 | target_names.add((target_name, is_static)) |
paul@357 | 137 | |
paul@612 | 138 | if target_names: |
paul@612 | 139 | all_target_names.append(target_names) |
paul@610 | 140 | |
paul@610 | 141 | # Return the smallest set of target names. |
paul@610 | 142 | |
paul@610 | 143 | all_target_names.sort(key=lambda x: len(x)) |
paul@612 | 144 | |
paul@610 | 145 | return all_target_names and all_target_names[0] |
paul@357 | 146 | |
paul@353 | 147 | def used_by_unit(node): |
paul@353 | 148 | |
paul@353 | 149 | """ |
paul@353 | 150 | Return whether the definition made by a 'node' is actually employed by the |
paul@353 | 151 | program unit within which it is found. |
paul@353 | 152 | """ |
paul@353 | 153 | |
paul@599 | 154 | return node.unit and node.unit.parent.has_key(node.unit.name) |
paul@353 | 155 | |
paul@172 | 156 | # Useful data. |
paul@172 | 157 | |
paul@354 | 158 | operator_functions = { |
paul@354 | 159 | |
paul@354 | 160 | # Binary operations. |
paul@354 | 161 | |
paul@354 | 162 | "Add" : "add", |
paul@354 | 163 | "Bitand" : "and_", |
paul@354 | 164 | "Bitor" : "or_", |
paul@354 | 165 | "Bitxor" : "xor", |
paul@354 | 166 | "Div" : "div", |
paul@354 | 167 | "FloorDiv" : "floordiv", |
paul@354 | 168 | "LeftShift" : "lshift", |
paul@354 | 169 | "Mod" : "mod", |
paul@354 | 170 | "Mul" : "mul", |
paul@354 | 171 | "Power" : "pow", |
paul@354 | 172 | "RightShift" : "rshift", |
paul@354 | 173 | "Sub" : "sub", |
paul@354 | 174 | |
paul@354 | 175 | # Unary operations. |
paul@354 | 176 | |
paul@354 | 177 | "Invert" : "invert", |
paul@354 | 178 | "UnaryAdd" : "pos", |
paul@354 | 179 | "UnarySub" : "neg", |
paul@354 | 180 | |
paul@354 | 181 | # Augmented assignment. |
paul@354 | 182 | |
paul@354 | 183 | "+=" : "iadd", |
paul@354 | 184 | "-=" : "isub", |
paul@354 | 185 | "*=" : "imul", |
paul@354 | 186 | "/=" : "idiv", |
paul@354 | 187 | "//=" : "ifloordiv", |
paul@354 | 188 | "%=" : "imod", |
paul@354 | 189 | "**=" : "ipow", |
paul@354 | 190 | "<<=" : "ilshift", |
paul@354 | 191 | ">>=" : "irshift", |
paul@354 | 192 | "&=" : "iand", |
paul@354 | 193 | "^=" : "ixor", |
paul@354 | 194 | "|=" : "ior", |
paul@354 | 195 | |
paul@354 | 196 | # Comparisons. |
paul@354 | 197 | |
paul@354 | 198 | "==" : "eq", |
paul@354 | 199 | "!=" : "ne", |
paul@354 | 200 | "<" : "lt", |
paul@354 | 201 | "<=" : "le", |
paul@354 | 202 | ">=" : "ge", |
paul@374 | 203 | ">" : "gt", |
paul@374 | 204 | |
paul@374 | 205 | # Access and slicing. |
paul@374 | 206 | |
paul@623 | 207 | "AssSlice" : "setslice", # Python 2.7 |
paul@374 | 208 | "Slice" : "getslice", |
paul@623 | 209 | "AssSubscript" : "setitem", # Python 2.7 |
paul@374 | 210 | "Subscript" : "getitem", |
paul@354 | 211 | } |
paul@354 | 212 | |
paul@39 | 213 | # vim: tabstop=4 expandtab shiftwidth=4 |