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