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 target_names = set() 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 target_names.add((attr.parent.full_name(), attr.is_static_attribute())) 90 return target_names 91 92 # Otherwise, try and use an expression annotation. 93 94 if isinstance(node, (AssAttr, Getattr)): 95 expr = node._expr 96 97 if isinstance(expr, Attr) and expr.get_value(): 98 expr = expr.get_value() 99 100 if isinstance(expr, Class): 101 attr = expr.all_class_attributes().get(node.attrname) 102 elif isinstance(expr, Module): 103 attr = expr.get(node.attrname) 104 else: 105 attr = None 106 107 if attr: 108 target_names.add((attr.parent.full_name(), attr.is_static_attribute())) 109 return target_names 110 111 # Otherwise, attempt to employ the attribute usage observations. 112 113 if node._attrusers: 114 115 # Visit each attribute user. 116 117 for user in node._attrusers: 118 119 # Since users such as branches may not provide type information, 120 # attempt to find defining users. 121 122 if defining_users: 123 for def_user in user._attrdefs or [user]: 124 for target_name, is_static in def_user._attrtypes.get(node._username, []): 125 target_names.add((target_name, is_static)) 126 else: 127 for target_name, is_static in user._attrspecifictypes.get(node._username, []): 128 target_names.add((target_name, is_static)) 129 130 return target_names 131 132 def used_by_unit(node): 133 134 """ 135 Return whether the definition made by a 'node' is actually employed by the 136 program unit within which it is found. 137 """ 138 139 return node.unit and node.unit.parent.has_key(node.unit.name) 140 141 # Useful data. 142 143 operator_functions = { 144 145 # Binary operations. 146 147 "Add" : "add", 148 "Bitand" : "and_", 149 "Bitor" : "or_", 150 "Bitxor" : "xor", 151 "Div" : "div", 152 "FloorDiv" : "floordiv", 153 "LeftShift" : "lshift", 154 "Mod" : "mod", 155 "Mul" : "mul", 156 "Power" : "pow", 157 "RightShift" : "rshift", 158 "Sub" : "sub", 159 160 # Unary operations. 161 162 "Invert" : "invert", 163 "UnaryAdd" : "pos", 164 "UnarySub" : "neg", 165 166 # Augmented assignment. 167 168 "+=" : "iadd", 169 "-=" : "isub", 170 "*=" : "imul", 171 "/=" : "idiv", 172 "//=" : "ifloordiv", 173 "%=" : "imod", 174 "**=" : "ipow", 175 "<<=" : "ilshift", 176 ">>=" : "irshift", 177 "&=" : "iand", 178 "^=" : "ixor", 179 "|=" : "ior", 180 181 # Comparisons. 182 183 "==" : "eq", 184 "!=" : "ne", 185 "<" : "lt", 186 "<=" : "le", 187 ">=" : "ge", 188 ">" : "gt", 189 190 # Access and slicing. 191 192 "AssSlice" : "setslice", 193 "Slice" : "getslice", 194 "AssSubscript" : "setitem", 195 "Subscript" : "getitem", 196 } 197 198 # vim: tabstop=4 expandtab shiftwidth=4