paul@646 | 1 | #!/usr/bin/env python |
paul@646 | 2 | |
paul@646 | 3 | """ |
paul@646 | 4 | Perform deductions on an inspected program. |
paul@646 | 5 | |
paul@646 | 6 | Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013 Paul Boddie <paul@boddie.org.uk> |
paul@646 | 7 | |
paul@646 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@646 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@646 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@646 | 11 | version. |
paul@646 | 12 | |
paul@646 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@646 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@646 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@646 | 16 | details. |
paul@646 | 17 | |
paul@646 | 18 | You should have received a copy of the GNU General Public License along with |
paul@646 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@646 | 20 | """ |
paul@646 | 21 | |
paul@646 | 22 | from micropython.common import * |
paul@646 | 23 | from micropython.data import * |
paul@646 | 24 | from micropython.errors import * |
paul@646 | 25 | import compiler.ast |
paul@646 | 26 | |
paul@646 | 27 | # Source code classes. |
paul@646 | 28 | |
paul@646 | 29 | class DeducedSource(ASTVisitor): |
paul@646 | 30 | |
paul@646 | 31 | "A module upon which deductions of code behaviour are made." |
paul@646 | 32 | |
paul@646 | 33 | def __init__(self, module, program): |
paul@646 | 34 | self.visitor = self |
paul@646 | 35 | self.module = module |
paul@646 | 36 | self.program = program |
paul@646 | 37 | self.objtable = program.get_object_table() |
paul@646 | 38 | self.units = [] |
paul@646 | 39 | |
paul@646 | 40 | def get_unit(self): |
paul@646 | 41 | return self.units[-1] |
paul@646 | 42 | |
paul@646 | 43 | def get_module(self): |
paul@646 | 44 | return self.units[0] |
paul@646 | 45 | |
paul@646 | 46 | def deduce(self): |
paul@646 | 47 | |
paul@646 | 48 | "Process the module, making deductions." |
paul@646 | 49 | |
paul@646 | 50 | self.dispatch(self.module.astnode) |
paul@646 | 51 | |
paul@646 | 52 | def dispatch(self, node, *args): |
paul@646 | 53 | |
paul@646 | 54 | "NOTE: From compiler.visitor.ASTVisitor." |
paul@646 | 55 | |
paul@646 | 56 | try: |
paul@646 | 57 | return node.visit(self.visitor, *args) |
paul@646 | 58 | except AttributeError: |
paul@646 | 59 | # NOTE: Obligatory hack to find real attribute errors. |
paul@646 | 60 | if isinstance(node, (Getattr, AssAttr)): |
paul@646 | 61 | raise |
paul@646 | 62 | return self.visitor.default(node, *args) |
paul@646 | 63 | |
paul@646 | 64 | def _visitUnit(self, node): |
paul@646 | 65 | |
paul@646 | 66 | """ |
paul@646 | 67 | Track entry into program units in order to support various attribute |
paul@646 | 68 | access operations. |
paul@646 | 69 | """ |
paul@646 | 70 | |
paul@646 | 71 | self.units.append(node.unit) |
paul@646 | 72 | self.dispatch(node.node) |
paul@646 | 73 | self.units.pop() |
paul@646 | 74 | |
paul@649 | 75 | def _visitOptionalUnit(self, node): |
paul@649 | 76 | |
paul@649 | 77 | "Optionally visit a unit, depending on whether it is used." |
paul@649 | 78 | |
paul@649 | 79 | if not used_by_unit(node): |
paul@649 | 80 | return |
paul@649 | 81 | self._visitUnit(node) |
paul@649 | 82 | |
paul@649 | 83 | visitModule = _visitUnit |
paul@649 | 84 | visitClass = visitFunction = _visitOptionalUnit |
paul@646 | 85 | |
paul@646 | 86 | def _visitAttr(self, node): |
paul@646 | 87 | |
paul@646 | 88 | """ |
paul@646 | 89 | Perform deductions on attribute accesses, adding annotations to the node |
paul@646 | 90 | that can be used by subsequent activities. |
paul@646 | 91 | """ |
paul@646 | 92 | |
paul@646 | 93 | unit = self.get_unit() |
paul@646 | 94 | |
paul@646 | 95 | # The target, on which the access is performed, may influence the effect |
paul@646 | 96 | # on the context. We can only reliably assume that a literal constant is |
paul@646 | 97 | # an instance: all other "instances" may actually be classes in certain |
paul@646 | 98 | # cases. |
paul@646 | 99 | |
paul@646 | 100 | target = node._expr |
paul@646 | 101 | instance_target = isinstance(target, Const) |
paul@646 | 102 | |
paul@646 | 103 | # Attempt to deduce attributes from explicit annotations. |
paul@646 | 104 | |
paul@646 | 105 | node._attrs_deduced = attrs = self.possible_attributes_from_annotation(node) |
paul@646 | 106 | |
paul@646 | 107 | if len(attrs) == 1: |
paul@646 | 108 | for attr, value in attrs: |
paul@646 | 109 | |
paul@646 | 110 | # Constant values can be obtained directly. |
paul@646 | 111 | |
paul@646 | 112 | if self.provides_constant_result(value): |
paul@646 | 113 | node._access_type = "constant" |
paul@646 | 114 | node._value_deduced = value |
paul@646 | 115 | return |
paul@646 | 116 | |
paul@646 | 117 | # Static attributes can be obtained via their parent. |
paul@646 | 118 | |
paul@646 | 119 | if attr.is_static_attribute(): |
paul@646 | 120 | node._access_type = "static" |
paul@646 | 121 | node._attr_deduced = attr |
paul@646 | 122 | node._set_context = instance_target and "set" or None |
paul@646 | 123 | return |
paul@646 | 124 | |
paul@646 | 125 | # Attributes of self, which is by definition an instance. |
paul@646 | 126 | |
paul@646 | 127 | if self.provides_self_access(node, unit): |
paul@646 | 128 | |
paul@646 | 129 | # Find instance attributes. |
paul@646 | 130 | |
paul@646 | 131 | attr = unit.parent.instance_attributes().get(node.attrname) |
paul@646 | 132 | |
paul@646 | 133 | if attr: |
paul@646 | 134 | node._access_type = "instance" |
paul@646 | 135 | node._attr_deduced = attr |
paul@646 | 136 | return |
paul@646 | 137 | |
paul@646 | 138 | # Find class attributes. |
paul@646 | 139 | # The context will be overridden for compatible class attributes |
paul@646 | 140 | # only. |
paul@646 | 141 | |
paul@646 | 142 | attr = unit.parent.get(node.attrname) |
paul@646 | 143 | |
paul@646 | 144 | if attr: |
paul@646 | 145 | |
paul@646 | 146 | # Constant attributes. |
paul@646 | 147 | |
paul@646 | 148 | if attr.is_strict_constant(): |
paul@646 | 149 | if self.provides_constant_result(attr.get_value()): |
paul@646 | 150 | node._access_type = "constant" |
paul@646 | 151 | node._value_deduced = attr.get_value() |
paul@646 | 152 | return |
paul@646 | 153 | |
paul@646 | 154 | # Compatible class attributes. |
paul@646 | 155 | |
paul@646 | 156 | if attr.defined_within_hierarchy(): |
paul@646 | 157 | node._access_type = "static" |
paul@646 | 158 | node._attr_deduced = attr |
paul@646 | 159 | node._set_context = "set" |
paul@646 | 160 | return |
paul@646 | 161 | |
paul@646 | 162 | # Incompatible class attributes. |
paul@646 | 163 | |
paul@646 | 164 | elif attr.defined_outside_hierarchy(): |
paul@646 | 165 | node._access_type = "static" |
paul@646 | 166 | node._attr_deduced = attr |
paul@646 | 167 | return |
paul@646 | 168 | |
paul@646 | 169 | # Unknown or mixed compatibility. |
paul@646 | 170 | |
paul@646 | 171 | node._access_type = "static" |
paul@646 | 172 | node._attr_deduced = attr |
paul@646 | 173 | node._set_context = "cond" |
paul@646 | 174 | return |
paul@646 | 175 | |
paul@646 | 176 | # Usage observations, both specific to this node's region of the program |
paul@646 | 177 | # and also applicable to the lifespan of the affected name. |
paul@646 | 178 | |
paul@646 | 179 | specific_targets = self.possible_accessors_from_usage(node, defining_users=0) |
paul@646 | 180 | targets = self.possible_accessors_from_usage(node, defining_users=1) |
paul@646 | 181 | |
paul@646 | 182 | # Record whether types were already deduced. If not, get types using |
paul@646 | 183 | # only this attribute. |
paul@646 | 184 | |
paul@646 | 185 | if not specific_targets or not targets: |
paul@646 | 186 | attribute_targets = self.possible_accessors_for_attribute(node.attrname) |
paul@646 | 187 | if not specific_targets: |
paul@646 | 188 | specific_targets = attribute_targets |
paul@646 | 189 | if not targets: |
paul@646 | 190 | targets = attribute_targets |
paul@646 | 191 | |
paul@646 | 192 | node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, node.attrname) |
paul@646 | 193 | node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, node.attrname) |
paul@646 | 194 | |
paul@646 | 195 | # Generate optimisations where only a single attribute applies. |
paul@646 | 196 | |
paul@646 | 197 | if attrs and len(attrs) == 1: |
paul@646 | 198 | for attr in attrs: |
paul@646 | 199 | |
paul@646 | 200 | # Static attributes, but potentially non-static targets. |
paul@646 | 201 | |
paul@646 | 202 | if attr.is_static_attribute(): |
paul@646 | 203 | |
paul@646 | 204 | # Static attributes may be accompanied by a different context |
paul@646 | 205 | # depending on the accessor. |
paul@646 | 206 | # NOTE: Should determine whether the context is always replaced. |
paul@646 | 207 | |
paul@646 | 208 | node._access_type = "static" |
paul@646 | 209 | node._attr_deduced = attr |
paul@646 | 210 | node._set_context = instance_target and "set" or "cond" |
paul@646 | 211 | return |
paul@646 | 212 | |
paul@646 | 213 | # Non-static attributes. |
paul@646 | 214 | |
paul@646 | 215 | node._access_type = "instance" |
paul@646 | 216 | node._attr_deduced = attr |
paul@646 | 217 | return |
paul@646 | 218 | |
paul@646 | 219 | # Test for compatible attribute positioning. |
paul@646 | 220 | |
paul@646 | 221 | elif attrs: |
paul@646 | 222 | positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) |
paul@646 | 223 | |
paul@646 | 224 | # Permit a position-based access only on non-static attributes since |
paul@646 | 225 | # access to static attributes may happen via instances and thus not |
paul@646 | 226 | # be relative to the accessor but to its parent. |
paul@646 | 227 | |
paul@646 | 228 | if len(positions) == 1: |
paul@646 | 229 | for position in positions: |
paul@646 | 230 | if not position[0]: |
paul@646 | 231 | node._access_type = "positioned" |
paul@646 | 232 | node._position_deduced = position[0] |
paul@646 | 233 | return |
paul@646 | 234 | |
paul@646 | 235 | # With no usable deductions, generate a table-based access. |
paul@646 | 236 | |
paul@646 | 237 | node._access_type = "unknown" |
paul@646 | 238 | node._set_context = "cond" |
paul@646 | 239 | |
paul@646 | 240 | visitAssAttr = visitGetattr = _visitAttr |
paul@646 | 241 | |
paul@646 | 242 | # Convenience functions. |
paul@646 | 243 | |
paul@646 | 244 | def deduce(program): |
paul@646 | 245 | for module in program.get_importer().get_modules(): |
paul@646 | 246 | DeducedSource(module, program).deduce() |
paul@646 | 247 | |
paul@646 | 248 | # vim: tabstop=4 expandtab shiftwidth=4 |