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 Class, Function, Module, AssAttr, Getattr, Name 23 from micropython.data import Attr 24 from micropython.errors import * 25 26 try: 27 set 28 except NameError: 29 from sets import Set as set 30 31 # Visitors and activities related to node annotations. 32 33 class ASTVisitor: 34 35 "A base class for visitors." 36 37 def default(self, node, *args): 38 for n in node.getChildNodes(): 39 self.dispatch(n) 40 41 def dispatch(self, node, *args): 42 43 "Dispatch using 'node', annotating any raised exceptions." 44 45 # Dispatch via a generic visit method. 46 47 try: 48 return node.visit(self, *args) 49 50 # Annotate the exception in case of failure. 51 52 except NodeProcessingError, exc: 53 if exc.astnode is None: 54 exc.astnode = node 55 56 # NOTE: Should perhaps specialise the subclasses appropriately. 57 58 if isinstance(self, (Class, Function, Module)): 59 exc.unit_name = self.unit.full_name() 60 else: 61 exc.unit_name = self.full_name() 62 raise 63 64 def possible_accessor_types(self, node, defining_users=1): 65 66 """ 67 Given annotations made during the inspection process, return all possible 68 type names and indications of static usage for a 'node' involved in 69 attribute access. 70 71 If 'defining_users' is set to a false value, attempt to get the type 72 names specifically applicable to the node, rather than retrieving more 73 general definition-based type observations. 74 """ 75 76 target_names = set() 77 78 # Where an attribute could already be detected and where its nature is 79 # not that of a general instance or an unresolved name, attempt to 80 # identify it. 81 82 if isinstance(node, (AssAttr, Getattr, Name)): 83 84 if isinstance(node._attr, Attr): 85 attr = node._attr 86 target_names.add((attr.parent.full_name(), attr.is_static_attribute())) 87 88 # Otherwise, attempt to employ the attribute usage observations. 89 90 elif node._attrusers: 91 92 # Visit each attribute user. 93 94 for user in node._attrusers: 95 96 # Since users such as branches may not provide type information, 97 # attempt to find defining users. 98 99 if defining_users: 100 for def_user in user._attrdefs or [user]: 101 for target_name, is_static in def_user._attrtypes.get(node._username, []): 102 target_names.add((target_name, is_static)) 103 else: 104 for target_name, is_static in user._attrspecifictypes.get(node._username, []): 105 target_names.add((target_name, is_static)) 106 107 return target_names 108 109 def used_by_unit(node): 110 111 """ 112 Return whether the definition made by a 'node' is actually employed by the 113 program unit within which it is found. 114 """ 115 116 return node.unit and node.unit.parent.has_key(node.unit.name) 117 118 # Useful data. 119 120 operator_functions = { 121 122 # Binary operations. 123 124 "Add" : "add", 125 "Bitand" : "and_", 126 "Bitor" : "or_", 127 "Bitxor" : "xor", 128 "Div" : "div", 129 "FloorDiv" : "floordiv", 130 "LeftShift" : "lshift", 131 "Mod" : "mod", 132 "Mul" : "mul", 133 "Power" : "pow", 134 "RightShift" : "rshift", 135 "Sub" : "sub", 136 137 # Unary operations. 138 139 "Invert" : "invert", 140 "UnaryAdd" : "pos", 141 "UnarySub" : "neg", 142 143 # Augmented assignment. 144 145 "+=" : "iadd", 146 "-=" : "isub", 147 "*=" : "imul", 148 "/=" : "idiv", 149 "//=" : "ifloordiv", 150 "%=" : "imod", 151 "**=" : "ipow", 152 "<<=" : "ilshift", 153 ">>=" : "irshift", 154 "&=" : "iand", 155 "^=" : "ixor", 156 "|=" : "ior", 157 158 # Comparisons. 159 160 "==" : "eq", 161 "!=" : "ne", 162 "<" : "lt", 163 "<=" : "le", 164 ">=" : "ge", 165 ">" : "gt", 166 167 # Access and slicing. 168 169 "AssSlice" : "setslice", 170 "Slice" : "getslice", 171 "AssSubscript" : "setitem", 172 "Subscript" : "getitem", 173 } 174 175 # vim: tabstop=4 expandtab shiftwidth=4