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