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