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 57 # NOTE: Should perhaps specialise the subclasses appropriately. 58 59 if isinstance(self, (compiler.ast.Class, compiler.ast.Function, compiler.ast.Module)): 60 exc.unit_name = self.unit.full_name() 61 else: 62 exc.unit_name = self.full_name() 63 raise 64 65 def possible_accessor_types(self, node, defining_users=1): 66 67 """ 68 Given annotations made during the inspection process, return all possible 69 type names and indications of static usage for a 'node' involved in 70 attribute access. 71 72 If 'defining_users' is set to a false value, attempt to get the type 73 names specifically applicable to the node, rather than retrieving more 74 general definition-based type observations. 75 """ 76 77 all_target_names = [] 78 79 # Where an attribute could already be detected and where its nature is 80 # not that of a general instance or an unresolved name, attempt to 81 # identify it. 82 83 if isinstance(node, (AssAttr, Getattr, Name)): 84 85 # Use any explicit attribute annotation. 86 87 if isinstance(node._attr, Attr): 88 attr = node._attr 89 all_target_names.append(set([(attr.parent.full_name(), attr.is_static_attribute())])) 90 91 # Otherwise, try and use an expression annotation. 92 93 if isinstance(node, (AssAttr, Getattr)): 94 expr = node._expr 95 96 # Permitting multiple expression types if they provide the 97 # attribute. 98 99 if isinstance(expr, Attr): 100 exprs = expr.get_values() 101 elif expr: 102 exprs = [expr] 103 else: 104 exprs = None 105 106 if exprs: 107 target_names = set() 108 109 for expr in exprs: 110 attr = expr.get_static_attribute(node.attrname) 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 all_target_names.append(target_names) 139 140 # Return the smallest set of target names. 141 142 all_target_names.sort(key=lambda x: len(x)) 143 return all_target_names and all_target_names[0] 144 145 def used_by_unit(node): 146 147 """ 148 Return whether the definition made by a 'node' is actually employed by the 149 program unit within which it is found. 150 """ 151 152 return node.unit and node.unit.parent.has_key(node.unit.name) 153 154 # Useful data. 155 156 operator_functions = { 157 158 # Binary operations. 159 160 "Add" : "add", 161 "Bitand" : "and_", 162 "Bitor" : "or_", 163 "Bitxor" : "xor", 164 "Div" : "div", 165 "FloorDiv" : "floordiv", 166 "LeftShift" : "lshift", 167 "Mod" : "mod", 168 "Mul" : "mul", 169 "Power" : "pow", 170 "RightShift" : "rshift", 171 "Sub" : "sub", 172 173 # Unary operations. 174 175 "Invert" : "invert", 176 "UnaryAdd" : "pos", 177 "UnarySub" : "neg", 178 179 # Augmented assignment. 180 181 "+=" : "iadd", 182 "-=" : "isub", 183 "*=" : "imul", 184 "/=" : "idiv", 185 "//=" : "ifloordiv", 186 "%=" : "imod", 187 "**=" : "ipow", 188 "<<=" : "ilshift", 189 ">>=" : "irshift", 190 "&=" : "iand", 191 "^=" : "ixor", 192 "|=" : "ior", 193 194 # Comparisons. 195 196 "==" : "eq", 197 "!=" : "ne", 198 "<" : "lt", 199 "<=" : "le", 200 ">=" : "ge", 201 ">" : "gt", 202 203 # Access and slicing. 204 205 "AssSlice" : "setslice", 206 "Slice" : "getslice", 207 "AssSubscript" : "setitem", 208 "Subscript" : "getitem", 209 } 210 211 # vim: tabstop=4 expandtab shiftwidth=4