paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@39 | 3 | """ |
paul@39 | 4 | Common classes. |
paul@39 | 5 | |
paul@487 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie <paul@boddie.org.uk> |
paul@39 | 7 | |
paul@39 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@39 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@39 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@39 | 11 | version. |
paul@39 | 12 | |
paul@39 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@39 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@39 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@39 | 16 | details. |
paul@39 | 17 | |
paul@39 | 18 | You should have received a copy of the GNU General Public License along with |
paul@39 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@39 | 20 | """ |
paul@39 | 21 | |
paul@567 | 22 | from micropython.data import Attr, Instance, UnresolvedName |
paul@556 | 23 | from micropython.errors import * |
paul@507 | 24 | import sys |
paul@339 | 25 | |
paul@582 | 26 | try: |
paul@582 | 27 | set |
paul@582 | 28 | except NameError: |
paul@582 | 29 | from sets import Set as set |
paul@582 | 30 | |
paul@357 | 31 | # Visitors and activities related to node annotations. |
paul@339 | 32 | |
paul@496 | 33 | class ASTVisitor: |
paul@339 | 34 | |
paul@339 | 35 | "A base class for visitors." |
paul@339 | 36 | |
paul@496 | 37 | def default(self, node, *args): |
paul@496 | 38 | for n in node.getChildNodes(): |
paul@496 | 39 | self.dispatch(n) |
paul@496 | 40 | |
paul@339 | 41 | def dispatch(self, node, *args): |
paul@357 | 42 | |
paul@357 | 43 | "Dispatch using 'node', annotating any raised exceptions." |
paul@357 | 44 | |
paul@496 | 45 | # Dispatch via a generic visit method. |
paul@496 | 46 | |
paul@339 | 47 | try: |
paul@496 | 48 | return node.visit(self, *args) |
paul@496 | 49 | |
paul@496 | 50 | # Annotate the exception in case of failure. |
paul@496 | 51 | |
paul@339 | 52 | except NodeProcessingError, exc: |
paul@340 | 53 | if exc.astnode is None: |
paul@340 | 54 | exc.astnode = node |
paul@342 | 55 | |
paul@342 | 56 | # NOTE: Should perhaps specialise the subclasses appropriately. |
paul@342 | 57 | |
paul@342 | 58 | if hasattr(self, "unit"): |
paul@342 | 59 | exc.unit_name = self.unit.full_name() |
paul@342 | 60 | else: |
paul@342 | 61 | exc.unit_name = self.full_name() |
paul@339 | 62 | raise |
paul@339 | 63 | |
paul@488 | 64 | def possible_accessor_types(self, node, defining_users=1): |
paul@357 | 65 | |
paul@357 | 66 | """ |
paul@357 | 67 | Given annotations made during the inspection process, return all possible |
paul@401 | 68 | type names and indications of static usage for a 'node' involved in |
paul@401 | 69 | attribute access. |
paul@488 | 70 | |
paul@488 | 71 | If 'defining_users' is set to a false value, attempt to get the type |
paul@488 | 72 | names specifically applicable to the node, rather than retrieving more |
paul@488 | 73 | general definition-based type observations. |
paul@357 | 74 | """ |
paul@357 | 75 | |
paul@401 | 76 | target_names = set() |
paul@401 | 77 | |
paul@567 | 78 | if hasattr(node, "_attr") and not isinstance(node._attr, (Instance, UnresolvedName)): |
paul@556 | 79 | attr = node._attr |
paul@556 | 80 | if isinstance(attr, Attr): |
paul@556 | 81 | target_names.add((attr.parent.full_name(), attr.is_static_attribute())) |
paul@556 | 82 | else: |
paul@556 | 83 | target_names.add((attr.full_name(), attr.is_static_attribute())) |
paul@556 | 84 | |
paul@556 | 85 | elif hasattr(node, "_attrusers"): |
paul@357 | 86 | |
paul@381 | 87 | # Visit each attribute user. |
paul@381 | 88 | |
paul@357 | 89 | for user in node._attrusers: |
paul@381 | 90 | |
paul@381 | 91 | # Since users such as branches may not provide type information, |
paul@381 | 92 | # attempt to find defining users. |
paul@381 | 93 | |
paul@488 | 94 | if defining_users: |
paul@488 | 95 | for def_user in user._attrdefs or [user]: |
paul@488 | 96 | for target_name, is_static in def_user._attrtypes.get(node._username, []): |
paul@488 | 97 | target_names.add((target_name, is_static)) |
paul@488 | 98 | else: |
paul@490 | 99 | for target_name, is_static in user._attrspecifictypes.get(node._username, []): |
paul@401 | 100 | target_names.add((target_name, is_static)) |
paul@357 | 101 | |
paul@401 | 102 | return target_names |
paul@357 | 103 | |
paul@353 | 104 | def used_by_unit(node): |
paul@353 | 105 | |
paul@353 | 106 | """ |
paul@353 | 107 | Return whether the definition made by a 'node' is actually employed by the |
paul@353 | 108 | program unit within which it is found. |
paul@353 | 109 | """ |
paul@353 | 110 | |
paul@370 | 111 | return hasattr(node, "unit") and node.unit.parent.has_key(node.unit.name) |
paul@353 | 112 | |
paul@172 | 113 | # Useful data. |
paul@172 | 114 | |
paul@354 | 115 | operator_functions = { |
paul@354 | 116 | |
paul@354 | 117 | # Binary operations. |
paul@354 | 118 | |
paul@354 | 119 | "Add" : "add", |
paul@354 | 120 | "Bitand" : "and_", |
paul@354 | 121 | "Bitor" : "or_", |
paul@354 | 122 | "Bitxor" : "xor", |
paul@354 | 123 | "Div" : "div", |
paul@354 | 124 | "FloorDiv" : "floordiv", |
paul@354 | 125 | "LeftShift" : "lshift", |
paul@354 | 126 | "Mod" : "mod", |
paul@354 | 127 | "Mul" : "mul", |
paul@354 | 128 | "Power" : "pow", |
paul@354 | 129 | "RightShift" : "rshift", |
paul@354 | 130 | "Sub" : "sub", |
paul@354 | 131 | |
paul@354 | 132 | # Unary operations. |
paul@354 | 133 | |
paul@354 | 134 | "Invert" : "invert", |
paul@354 | 135 | "UnaryAdd" : "pos", |
paul@354 | 136 | "UnarySub" : "neg", |
paul@354 | 137 | |
paul@354 | 138 | # Augmented assignment. |
paul@354 | 139 | |
paul@354 | 140 | "+=" : "iadd", |
paul@354 | 141 | "-=" : "isub", |
paul@354 | 142 | "*=" : "imul", |
paul@354 | 143 | "/=" : "idiv", |
paul@354 | 144 | "//=" : "ifloordiv", |
paul@354 | 145 | "%=" : "imod", |
paul@354 | 146 | "**=" : "ipow", |
paul@354 | 147 | "<<=" : "ilshift", |
paul@354 | 148 | ">>=" : "irshift", |
paul@354 | 149 | "&=" : "iand", |
paul@354 | 150 | "^=" : "ixor", |
paul@354 | 151 | "|=" : "ior", |
paul@354 | 152 | |
paul@354 | 153 | # Comparisons. |
paul@354 | 154 | |
paul@354 | 155 | "==" : "eq", |
paul@354 | 156 | "!=" : "ne", |
paul@354 | 157 | "<" : "lt", |
paul@354 | 158 | "<=" : "le", |
paul@354 | 159 | ">=" : "ge", |
paul@374 | 160 | ">" : "gt", |
paul@374 | 161 | |
paul@374 | 162 | # Access and slicing. |
paul@374 | 163 | |
paul@374 | 164 | "AssSlice" : "setslice", |
paul@374 | 165 | "Slice" : "getslice", |
paul@374 | 166 | "AssSubscript" : "setitem", |
paul@374 | 167 | "Subscript" : "getitem", |
paul@354 | 168 | } |
paul@354 | 169 | |
paul@39 | 170 | # vim: tabstop=4 expandtab shiftwidth=4 |