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 micropython.data import Attr, Instance, UnresolvedName 23 from micropython.errors import * 24 import sys 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 hasattr(self, "unit"): 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 if hasattr(node, "_attr") and not isinstance(node._attr, (Instance, UnresolvedName)): 79 attr = node._attr 80 if isinstance(attr, Attr): 81 target_names.add((attr.parent.full_name(), attr.is_static_attribute())) 82 else: 83 target_names.add((attr.full_name(), attr.is_static_attribute())) 84 85 elif hasattr(node, "_attrusers"): 86 87 # Visit each attribute user. 88 89 for user in node._attrusers: 90 91 # Since users such as branches may not provide type information, 92 # attempt to find defining users. 93 94 if defining_users: 95 for def_user in user._attrdefs or [user]: 96 for target_name, is_static in def_user._attrtypes.get(node._username, []): 97 target_names.add((target_name, is_static)) 98 else: 99 for target_name, is_static in user._attrspecifictypes.get(node._username, []): 100 target_names.add((target_name, is_static)) 101 102 return target_names 103 104 def used_by_unit(node): 105 106 """ 107 Return whether the definition made by a 'node' is actually employed by the 108 program unit within which it is found. 109 """ 110 111 return hasattr(node, "unit") and node.unit.parent.has_key(node.unit.name) 112 113 # Useful data. 114 115 operator_functions = { 116 117 # Binary operations. 118 119 "Add" : "add", 120 "Bitand" : "and_", 121 "Bitor" : "or_", 122 "Bitxor" : "xor", 123 "Div" : "div", 124 "FloorDiv" : "floordiv", 125 "LeftShift" : "lshift", 126 "Mod" : "mod", 127 "Mul" : "mul", 128 "Power" : "pow", 129 "RightShift" : "rshift", 130 "Sub" : "sub", 131 132 # Unary operations. 133 134 "Invert" : "invert", 135 "UnaryAdd" : "pos", 136 "UnarySub" : "neg", 137 138 # Augmented assignment. 139 140 "+=" : "iadd", 141 "-=" : "isub", 142 "*=" : "imul", 143 "/=" : "idiv", 144 "//=" : "ifloordiv", 145 "%=" : "imod", 146 "**=" : "ipow", 147 "<<=" : "ilshift", 148 ">>=" : "irshift", 149 "&=" : "iand", 150 "^=" : "ixor", 151 "|=" : "ior", 152 153 # Comparisons. 154 155 "==" : "eq", 156 "!=" : "ne", 157 "<" : "lt", 158 "<=" : "le", 159 ">=" : "ge", 160 ">" : "gt", 161 162 # Access and slicing. 163 164 "AssSlice" : "setslice", 165 "Slice" : "getslice", 166 "AssSubscript" : "setitem", 167 "Subscript" : "getitem", 168 } 169 170 # vim: tabstop=4 expandtab shiftwidth=4