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@704 | 95 | # Remember to permit deductions on the expression node. Here, we may |
paul@704 | 96 | # also obtain a concrete type associated with an instantiation. |
paul@650 | 97 | |
paul@704 | 98 | expr_type = self.dispatch(node.expr) |
paul@704 | 99 | if not node._expr or isinstance(node._expr, Instance): |
paul@704 | 100 | node._expr = expr_type |
paul@650 | 101 | |
paul@646 | 102 | # The target, on which the access is performed, may influence the effect |
paul@646 | 103 | # on the context. We can only reliably assume that a literal constant is |
paul@646 | 104 | # an instance: all other "instances" may actually be classes in certain |
paul@646 | 105 | # cases. |
paul@646 | 106 | |
paul@646 | 107 | target = node._expr |
paul@646 | 108 | instance_target = isinstance(target, Const) |
paul@646 | 109 | |
paul@646 | 110 | # Attempt to deduce attributes from explicit annotations. |
paul@646 | 111 | |
paul@646 | 112 | node._attrs_deduced = attrs = self.possible_attributes_from_annotation(node) |
paul@646 | 113 | |
paul@646 | 114 | if len(attrs) == 1: |
paul@646 | 115 | for attr, value in attrs: |
paul@646 | 116 | |
paul@646 | 117 | # Constant values can be obtained directly. |
paul@646 | 118 | |
paul@646 | 119 | if self.provides_constant_result(value): |
paul@646 | 120 | node._access_type = "constant" |
paul@646 | 121 | node._value_deduced = value |
paul@646 | 122 | return |
paul@646 | 123 | |
paul@646 | 124 | # Static attributes can be obtained via their parent. |
paul@646 | 125 | |
paul@646 | 126 | if attr.is_static_attribute(): |
paul@646 | 127 | node._access_type = "static" |
paul@646 | 128 | node._attr_deduced = attr |
paul@646 | 129 | node._set_context = instance_target and "set" or None |
paul@646 | 130 | return |
paul@646 | 131 | |
paul@698 | 132 | # If a reliable target was originally specified, any usable attributes |
paul@698 | 133 | # should have been detected above, and any attributes deduced by other |
paul@698 | 134 | # means will not be compatible with the target. Thus, the nature of the |
paul@698 | 135 | # target is investigated: it must be an inspectable namespace or be an |
paul@698 | 136 | # attribute only providing such namespaces; otherwise, it is possible |
paul@698 | 137 | # that deduced attributes might be appropriate. |
paul@698 | 138 | |
paul@698 | 139 | if target and ( |
paul@698 | 140 | isinstance(target, (Class, Module)) or |
paul@698 | 141 | isinstance(target, Attr) and not [v for v in target.get_values() if not isinstance(v, (Class, Module))] |
paul@698 | 142 | ): |
paul@698 | 143 | node._access_type = "impossible" |
paul@698 | 144 | return |
paul@698 | 145 | |
paul@646 | 146 | # Attributes of self, which is by definition an instance. |
paul@646 | 147 | |
paul@646 | 148 | if self.provides_self_access(node, unit): |
paul@646 | 149 | |
paul@646 | 150 | # Find instance attributes. |
paul@646 | 151 | |
paul@646 | 152 | attr = unit.parent.instance_attributes().get(node.attrname) |
paul@693 | 153 | attrs = filter(None, [cls.instance_attributes().get(node.attrname) for cls in unit.parent.descendants]) |
paul@646 | 154 | |
paul@693 | 155 | # A "leaf" class whose instances provide an attribute. |
paul@693 | 156 | |
paul@693 | 157 | if attr and not attrs: |
paul@646 | 158 | node._access_type = "instance" |
paul@646 | 159 | node._attr_deduced = attr |
paul@646 | 160 | return |
paul@646 | 161 | |
paul@693 | 162 | # A class where instances of subclasses may provide an attribute. |
paul@693 | 163 | |
paul@693 | 164 | elif attrs: |
paul@693 | 165 | if attr: |
paul@693 | 166 | attrs.append(attr) |
paul@693 | 167 | |
paul@693 | 168 | node._attrs_deduced = [(a, a.get_value()) for a in attrs] |
paul@693 | 169 | |
paul@693 | 170 | # The instances may provide the attribute at the same position. |
paul@693 | 171 | |
paul@693 | 172 | positions = set([a.position for a in attrs]) |
paul@693 | 173 | if len(positions) == 1: |
paul@693 | 174 | for position in positions: |
paul@693 | 175 | node._access_type = "positioned" |
paul@693 | 176 | node._position_deduced = position |
paul@693 | 177 | return |
paul@693 | 178 | |
paul@693 | 179 | # Otherwise, accessing the attributes is more work. |
paul@693 | 180 | |
paul@693 | 181 | node._access_type = "instance" |
paul@693 | 182 | return |
paul@693 | 183 | |
paul@646 | 184 | # Find class attributes. |
paul@646 | 185 | # The context will be overridden for compatible class attributes |
paul@646 | 186 | # only. |
paul@646 | 187 | |
paul@646 | 188 | attr = unit.parent.get(node.attrname) |
paul@646 | 189 | |
paul@646 | 190 | if attr: |
paul@646 | 191 | |
paul@646 | 192 | # Constant attributes. |
paul@646 | 193 | |
paul@646 | 194 | if attr.is_strict_constant(): |
paul@646 | 195 | if self.provides_constant_result(attr.get_value()): |
paul@646 | 196 | node._access_type = "constant" |
paul@646 | 197 | node._value_deduced = attr.get_value() |
paul@646 | 198 | return |
paul@646 | 199 | |
paul@646 | 200 | # Compatible class attributes. |
paul@646 | 201 | |
paul@646 | 202 | if attr.defined_within_hierarchy(): |
paul@646 | 203 | node._access_type = "static" |
paul@646 | 204 | node._attr_deduced = attr |
paul@646 | 205 | node._set_context = "set" |
paul@646 | 206 | return |
paul@646 | 207 | |
paul@646 | 208 | # Incompatible class attributes. |
paul@646 | 209 | |
paul@646 | 210 | elif attr.defined_outside_hierarchy(): |
paul@646 | 211 | node._access_type = "static" |
paul@646 | 212 | node._attr_deduced = attr |
paul@646 | 213 | return |
paul@646 | 214 | |
paul@646 | 215 | # Unknown or mixed compatibility. |
paul@646 | 216 | |
paul@646 | 217 | node._access_type = "static" |
paul@646 | 218 | node._attr_deduced = attr |
paul@646 | 219 | node._set_context = "cond" |
paul@646 | 220 | return |
paul@646 | 221 | |
paul@646 | 222 | # Usage observations, both specific to this node's region of the program |
paul@646 | 223 | # and also applicable to the lifespan of the affected name. |
paul@646 | 224 | |
paul@646 | 225 | specific_targets = self.possible_accessors_from_usage(node, defining_users=0) |
paul@646 | 226 | targets = self.possible_accessors_from_usage(node, defining_users=1) |
paul@646 | 227 | |
paul@646 | 228 | # Record whether types were already deduced. If not, get types using |
paul@646 | 229 | # only this attribute. |
paul@646 | 230 | |
paul@646 | 231 | if not specific_targets or not targets: |
paul@646 | 232 | attribute_targets = self.possible_accessors_for_attribute(node.attrname) |
paul@646 | 233 | if not specific_targets: |
paul@646 | 234 | specific_targets = attribute_targets |
paul@646 | 235 | if not targets: |
paul@646 | 236 | targets = attribute_targets |
paul@646 | 237 | |
paul@698 | 238 | # Get the attributes from the deduced targets. |
paul@698 | 239 | |
paul@646 | 240 | node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, node.attrname) |
paul@646 | 241 | node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, node.attrname) |
paul@646 | 242 | |
paul@646 | 243 | # Generate optimisations where only a single attribute applies. |
paul@646 | 244 | |
paul@646 | 245 | if attrs and len(attrs) == 1: |
paul@646 | 246 | for attr in attrs: |
paul@646 | 247 | |
paul@646 | 248 | # Static attributes, but potentially non-static targets. |
paul@646 | 249 | |
paul@646 | 250 | if attr.is_static_attribute(): |
paul@646 | 251 | |
paul@646 | 252 | # Static attributes may be accompanied by a different context |
paul@646 | 253 | # depending on the accessor. |
paul@646 | 254 | # NOTE: Should determine whether the context is always replaced. |
paul@646 | 255 | |
paul@646 | 256 | node._access_type = "static" |
paul@646 | 257 | node._attr_deduced = attr |
paul@646 | 258 | node._set_context = instance_target and "set" or "cond" |
paul@646 | 259 | return |
paul@646 | 260 | |
paul@646 | 261 | # Non-static attributes. |
paul@646 | 262 | |
paul@646 | 263 | node._access_type = "instance" |
paul@646 | 264 | node._attr_deduced = attr |
paul@646 | 265 | return |
paul@646 | 266 | |
paul@646 | 267 | # Test for compatible attribute positioning. |
paul@646 | 268 | |
paul@646 | 269 | elif attrs: |
paul@646 | 270 | positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) |
paul@646 | 271 | |
paul@646 | 272 | # Permit a position-based access only on non-static attributes since |
paul@646 | 273 | # access to static attributes may happen via instances and thus not |
paul@646 | 274 | # be relative to the accessor but to its parent. |
paul@646 | 275 | |
paul@646 | 276 | if len(positions) == 1: |
paul@646 | 277 | for position in positions: |
paul@646 | 278 | if not position[0]: |
paul@646 | 279 | node._access_type = "positioned" |
paul@646 | 280 | node._position_deduced = position[0] |
paul@646 | 281 | return |
paul@646 | 282 | |
paul@646 | 283 | # With no usable deductions, generate a table-based access. |
paul@646 | 284 | |
paul@646 | 285 | node._access_type = "unknown" |
paul@646 | 286 | node._set_context = "cond" |
paul@646 | 287 | |
paul@646 | 288 | visitAssAttr = visitGetattr = _visitAttr |
paul@646 | 289 | |
paul@704 | 290 | def visitCallFunc(self, node): |
paul@704 | 291 | |
paul@704 | 292 | "Identify any concrete types involved with instantiation." |
paul@704 | 293 | |
paul@704 | 294 | for n in node.getChildNodes(): |
paul@704 | 295 | self.dispatch(n) |
paul@704 | 296 | |
paul@704 | 297 | # Determine whether the target of the invocation refers to a class. |
paul@704 | 298 | |
paul@704 | 299 | attr = node.node._attr |
paul@704 | 300 | |
paul@704 | 301 | if attr: |
paul@704 | 302 | value = attr.get_value() |
paul@704 | 303 | if value and isinstance(value, Class): |
paul@704 | 304 | return attr |
paul@704 | 305 | |
paul@646 | 306 | # Convenience functions. |
paul@646 | 307 | |
paul@646 | 308 | def deduce(program): |
paul@646 | 309 | for module in program.get_importer().get_modules(): |
paul@646 | 310 | DeducedSource(module, program).deduce() |
paul@646 | 311 | |
paul@646 | 312 | # vim: tabstop=4 expandtab shiftwidth=4 |