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@342 | 56 | |
paul@342 | 57 | # NOTE: Should perhaps specialise the subclasses appropriately. |
paul@342 | 58 | |
paul@602 | 59 | if isinstance(self, (compiler.ast.Class, compiler.ast.Function, compiler.ast.Module)): |
paul@342 | 60 | exc.unit_name = self.unit.full_name() |
paul@342 | 61 | else: |
paul@342 | 62 | exc.unit_name = self.full_name() |
paul@339 | 63 | raise |
paul@339 | 64 | |
paul@488 | 65 | def possible_accessor_types(self, node, defining_users=1): |
paul@357 | 66 | |
paul@357 | 67 | """ |
paul@357 | 68 | Given annotations made during the inspection process, return all possible |
paul@401 | 69 | type names and indications of static usage for a 'node' involved in |
paul@401 | 70 | attribute access. |
paul@488 | 71 | |
paul@488 | 72 | If 'defining_users' is set to a false value, attempt to get the type |
paul@488 | 73 | names specifically applicable to the node, rather than retrieving more |
paul@488 | 74 | general definition-based type observations. |
paul@357 | 75 | """ |
paul@357 | 76 | |
paul@401 | 77 | target_names = set() |
paul@401 | 78 | |
paul@596 | 79 | # Where an attribute could already be detected and where its nature is |
paul@596 | 80 | # not that of a general instance or an unresolved name, attempt to |
paul@596 | 81 | # identify it. |
paul@596 | 82 | |
paul@600 | 83 | if isinstance(node, (AssAttr, Getattr, Name)): |
paul@600 | 84 | |
paul@602 | 85 | # Use any explicit attribute annotation. |
paul@602 | 86 | |
paul@600 | 87 | if isinstance(node._attr, Attr): |
paul@600 | 88 | attr = node._attr |
paul@600 | 89 | target_names.add((attr.parent.full_name(), attr.is_static_attribute())) |
paul@602 | 90 | return target_names |
paul@602 | 91 | |
paul@602 | 92 | # Otherwise, try and use an expression annotation. |
paul@602 | 93 | |
paul@602 | 94 | if isinstance(node, (AssAttr, Getattr)): |
paul@602 | 95 | expr = node._expr |
paul@602 | 96 | |
paul@602 | 97 | if isinstance(expr, Attr) and expr.get_value(): |
paul@602 | 98 | expr = expr.get_value() |
paul@602 | 99 | |
paul@602 | 100 | if isinstance(expr, Class): |
paul@602 | 101 | attr = expr.all_class_attributes().get(node.attrname) |
paul@602 | 102 | elif isinstance(expr, Module): |
paul@602 | 103 | attr = expr.get(node.attrname) |
paul@602 | 104 | else: |
paul@602 | 105 | attr = None |
paul@602 | 106 | |
paul@602 | 107 | if attr: |
paul@602 | 108 | target_names.add((attr.parent.full_name(), attr.is_static_attribute())) |
paul@602 | 109 | return target_names |
paul@556 | 110 | |
paul@600 | 111 | # Otherwise, attempt to employ the attribute usage observations. |
paul@596 | 112 | |
paul@602 | 113 | if node._attrusers: |
paul@357 | 114 | |
paul@600 | 115 | # Visit each attribute user. |
paul@381 | 116 | |
paul@600 | 117 | for user in node._attrusers: |
paul@381 | 118 | |
paul@600 | 119 | # Since users such as branches may not provide type information, |
paul@600 | 120 | # attempt to find defining users. |
paul@381 | 121 | |
paul@600 | 122 | if defining_users: |
paul@600 | 123 | for def_user in user._attrdefs or [user]: |
paul@600 | 124 | for target_name, is_static in def_user._attrtypes.get(node._username, []): |
paul@600 | 125 | target_names.add((target_name, is_static)) |
paul@600 | 126 | else: |
paul@600 | 127 | for target_name, is_static in user._attrspecifictypes.get(node._username, []): |
paul@488 | 128 | target_names.add((target_name, is_static)) |
paul@357 | 129 | |
paul@401 | 130 | return target_names |
paul@357 | 131 | |
paul@353 | 132 | def used_by_unit(node): |
paul@353 | 133 | |
paul@353 | 134 | """ |
paul@353 | 135 | Return whether the definition made by a 'node' is actually employed by the |
paul@353 | 136 | program unit within which it is found. |
paul@353 | 137 | """ |
paul@353 | 138 | |
paul@599 | 139 | return node.unit and node.unit.parent.has_key(node.unit.name) |
paul@353 | 140 | |
paul@172 | 141 | # Useful data. |
paul@172 | 142 | |
paul@354 | 143 | operator_functions = { |
paul@354 | 144 | |
paul@354 | 145 | # Binary operations. |
paul@354 | 146 | |
paul@354 | 147 | "Add" : "add", |
paul@354 | 148 | "Bitand" : "and_", |
paul@354 | 149 | "Bitor" : "or_", |
paul@354 | 150 | "Bitxor" : "xor", |
paul@354 | 151 | "Div" : "div", |
paul@354 | 152 | "FloorDiv" : "floordiv", |
paul@354 | 153 | "LeftShift" : "lshift", |
paul@354 | 154 | "Mod" : "mod", |
paul@354 | 155 | "Mul" : "mul", |
paul@354 | 156 | "Power" : "pow", |
paul@354 | 157 | "RightShift" : "rshift", |
paul@354 | 158 | "Sub" : "sub", |
paul@354 | 159 | |
paul@354 | 160 | # Unary operations. |
paul@354 | 161 | |
paul@354 | 162 | "Invert" : "invert", |
paul@354 | 163 | "UnaryAdd" : "pos", |
paul@354 | 164 | "UnarySub" : "neg", |
paul@354 | 165 | |
paul@354 | 166 | # Augmented assignment. |
paul@354 | 167 | |
paul@354 | 168 | "+=" : "iadd", |
paul@354 | 169 | "-=" : "isub", |
paul@354 | 170 | "*=" : "imul", |
paul@354 | 171 | "/=" : "idiv", |
paul@354 | 172 | "//=" : "ifloordiv", |
paul@354 | 173 | "%=" : "imod", |
paul@354 | 174 | "**=" : "ipow", |
paul@354 | 175 | "<<=" : "ilshift", |
paul@354 | 176 | ">>=" : "irshift", |
paul@354 | 177 | "&=" : "iand", |
paul@354 | 178 | "^=" : "ixor", |
paul@354 | 179 | "|=" : "ior", |
paul@354 | 180 | |
paul@354 | 181 | # Comparisons. |
paul@354 | 182 | |
paul@354 | 183 | "==" : "eq", |
paul@354 | 184 | "!=" : "ne", |
paul@354 | 185 | "<" : "lt", |
paul@354 | 186 | "<=" : "le", |
paul@354 | 187 | ">=" : "ge", |
paul@374 | 188 | ">" : "gt", |
paul@374 | 189 | |
paul@374 | 190 | # Access and slicing. |
paul@374 | 191 | |
paul@374 | 192 | "AssSlice" : "setslice", |
paul@374 | 193 | "Slice" : "getslice", |
paul@374 | 194 | "AssSubscript" : "setitem", |
paul@374 | 195 | "Subscript" : "getitem", |
paul@354 | 196 | } |
paul@354 | 197 | |
paul@39 | 198 | # vim: tabstop=4 expandtab shiftwidth=4 |