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