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@710 | 39 | self.expr = None |
paul@646 | 40 | |
paul@646 | 41 | def get_unit(self): |
paul@646 | 42 | return self.units[-1] |
paul@646 | 43 | |
paul@646 | 44 | def get_module(self): |
paul@646 | 45 | return self.units[0] |
paul@646 | 46 | |
paul@646 | 47 | def deduce(self): |
paul@646 | 48 | |
paul@646 | 49 | "Process the module, making deductions." |
paul@646 | 50 | |
paul@646 | 51 | self.dispatch(self.module.astnode) |
paul@646 | 52 | |
paul@646 | 53 | def dispatch(self, node, *args): |
paul@646 | 54 | |
paul@646 | 55 | "NOTE: From compiler.visitor.ASTVisitor." |
paul@646 | 56 | |
paul@646 | 57 | try: |
paul@646 | 58 | return node.visit(self.visitor, *args) |
paul@646 | 59 | except AttributeError: |
paul@646 | 60 | # NOTE: Obligatory hack to find real attribute errors. |
paul@646 | 61 | if isinstance(node, (Getattr, AssAttr)): |
paul@646 | 62 | raise |
paul@646 | 63 | return self.visitor.default(node, *args) |
paul@646 | 64 | |
paul@646 | 65 | def _visitUnit(self, node): |
paul@646 | 66 | |
paul@646 | 67 | """ |
paul@646 | 68 | Track entry into program units in order to support various attribute |
paul@646 | 69 | access operations. |
paul@646 | 70 | """ |
paul@646 | 71 | |
paul@646 | 72 | self.units.append(node.unit) |
paul@646 | 73 | self.dispatch(node.node) |
paul@646 | 74 | self.units.pop() |
paul@646 | 75 | |
paul@649 | 76 | def _visitOptionalUnit(self, node): |
paul@649 | 77 | |
paul@649 | 78 | "Optionally visit a unit, depending on whether it is used." |
paul@649 | 79 | |
paul@649 | 80 | if not used_by_unit(node): |
paul@649 | 81 | return |
paul@649 | 82 | self._visitUnit(node) |
paul@649 | 83 | |
paul@649 | 84 | visitModule = _visitUnit |
paul@649 | 85 | visitClass = visitFunction = _visitOptionalUnit |
paul@646 | 86 | |
paul@646 | 87 | def _visitAttr(self, node): |
paul@646 | 88 | |
paul@646 | 89 | """ |
paul@646 | 90 | Perform deductions on attribute accesses, adding annotations to the node |
paul@646 | 91 | that can be used by subsequent activities. |
paul@646 | 92 | """ |
paul@646 | 93 | |
paul@646 | 94 | unit = self.get_unit() |
paul@646 | 95 | |
paul@704 | 96 | # Remember to permit deductions on the expression node. Here, we may |
paul@704 | 97 | # also obtain a concrete type associated with an instantiation. |
paul@650 | 98 | |
paul@704 | 99 | expr_type = self.dispatch(node.expr) |
paul@704 | 100 | if not node._expr or isinstance(node._expr, Instance): |
paul@704 | 101 | node._expr = expr_type |
paul@650 | 102 | |
paul@646 | 103 | # The target, on which the access is performed, may influence the effect |
paul@646 | 104 | # on the context. We can only reliably assume that a literal constant is |
paul@646 | 105 | # an instance: all other "instances" may actually be classes in certain |
paul@646 | 106 | # cases. |
paul@646 | 107 | |
paul@646 | 108 | target = node._expr |
paul@710 | 109 | instance_target = isinstance(target, TypedInstance) |
paul@710 | 110 | typed_instance_attr = isinstance(target, Attr) and isinstance(target.get_value(), TypedInstance) |
paul@710 | 111 | self_access = self.provides_self_access(node, unit) |
paul@646 | 112 | |
paul@646 | 113 | # Attempt to deduce attributes from explicit annotations. |
paul@646 | 114 | |
paul@646 | 115 | node._attrs_deduced = attrs = self.possible_attributes_from_annotation(node) |
paul@646 | 116 | |
paul@646 | 117 | if len(attrs) == 1: |
paul@646 | 118 | for attr, value in attrs: |
paul@646 | 119 | |
paul@646 | 120 | # Constant values can be obtained directly. |
paul@646 | 121 | |
paul@646 | 122 | if self.provides_constant_result(value): |
paul@646 | 123 | node._access_type = "constant" |
paul@646 | 124 | node._value_deduced = value |
paul@646 | 125 | return |
paul@646 | 126 | |
paul@646 | 127 | # Static attributes can be obtained via their parent. |
paul@646 | 128 | |
paul@646 | 129 | if attr.is_static_attribute(): |
paul@646 | 130 | node._access_type = "static" |
paul@646 | 131 | node._attr_deduced = attr |
paul@646 | 132 | node._set_context = instance_target and "set" or None |
paul@646 | 133 | return |
paul@646 | 134 | |
paul@698 | 135 | # If a reliable target was originally specified, any usable attributes |
paul@698 | 136 | # should have been detected above, and any attributes deduced by other |
paul@698 | 137 | # means will not be compatible with the target. Thus, the nature of the |
paul@698 | 138 | # target is investigated: it must be an inspectable namespace or be an |
paul@698 | 139 | # attribute only providing such namespaces; otherwise, it is possible |
paul@698 | 140 | # that deduced attributes might be appropriate. |
paul@698 | 141 | |
paul@698 | 142 | if target and ( |
paul@698 | 143 | isinstance(target, (Class, Module)) or |
paul@698 | 144 | isinstance(target, Attr) and not [v for v in target.get_values() if not isinstance(v, (Class, Module))] |
paul@698 | 145 | ): |
paul@698 | 146 | node._access_type = "impossible" |
paul@698 | 147 | return |
paul@698 | 148 | |
paul@710 | 149 | # Attributes of self, which is by definition an instance, or typed |
paul@710 | 150 | # instances, which act somewhat like self in that their properties |
paul@710 | 151 | # should be known. |
paul@710 | 152 | |
paul@710 | 153 | if instance_target or typed_instance_attr or self_access: |
paul@646 | 154 | |
paul@710 | 155 | if instance_target: |
paul@710 | 156 | value = target |
paul@710 | 157 | elif typed_instance_attr: |
paul@710 | 158 | value = target.get_value() |
paul@710 | 159 | |
paul@710 | 160 | # Find the class of the instance. |
paul@710 | 161 | |
paul@710 | 162 | if instance_target or typed_instance_attr: |
paul@710 | 163 | if isinstance(value, Const): |
paul@710 | 164 | cls = get_constant_class(value.get_class_name()) |
paul@710 | 165 | else: |
paul@710 | 166 | cls = value.cls |
paul@710 | 167 | else: |
paul@710 | 168 | cls = unit.parent |
paul@646 | 169 | |
paul@646 | 170 | # Find instance attributes. |
paul@646 | 171 | |
paul@710 | 172 | attr = cls.instance_attributes().get(node.attrname) |
paul@710 | 173 | |
paul@710 | 174 | # Where self is involved, descendants can also provide attributes. |
paul@710 | 175 | |
paul@710 | 176 | attrs = self_access and filter(None, [desc.instance_attributes().get(node.attrname) for desc in cls.descendants]) or [] |
paul@646 | 177 | |
paul@693 | 178 | # A "leaf" class whose instances provide an attribute. |
paul@693 | 179 | |
paul@693 | 180 | if attr and not attrs: |
paul@646 | 181 | node._access_type = "instance" |
paul@646 | 182 | node._attr_deduced = attr |
paul@646 | 183 | return |
paul@646 | 184 | |
paul@693 | 185 | # A class where instances of subclasses may provide an attribute. |
paul@693 | 186 | |
paul@693 | 187 | elif attrs: |
paul@693 | 188 | if attr: |
paul@693 | 189 | attrs.append(attr) |
paul@693 | 190 | |
paul@693 | 191 | node._attrs_deduced = [(a, a.get_value()) for a in attrs] |
paul@693 | 192 | |
paul@693 | 193 | # The instances may provide the attribute at the same position. |
paul@693 | 194 | |
paul@693 | 195 | positions = set([a.position for a in attrs]) |
paul@693 | 196 | if len(positions) == 1: |
paul@693 | 197 | for position in positions: |
paul@693 | 198 | node._access_type = "positioned" |
paul@693 | 199 | node._position_deduced = position |
paul@693 | 200 | return |
paul@693 | 201 | |
paul@693 | 202 | # Otherwise, accessing the attributes is more work. |
paul@693 | 203 | |
paul@693 | 204 | node._access_type = "instance" |
paul@693 | 205 | return |
paul@693 | 206 | |
paul@646 | 207 | # Find class attributes. |
paul@646 | 208 | # The context will be overridden for compatible class attributes |
paul@646 | 209 | # only. |
paul@646 | 210 | |
paul@710 | 211 | attr = cls.all_class_attributes().get(node.attrname) |
paul@646 | 212 | |
paul@646 | 213 | if attr: |
paul@646 | 214 | |
paul@646 | 215 | # Constant attributes. |
paul@646 | 216 | |
paul@646 | 217 | if attr.is_strict_constant(): |
paul@646 | 218 | if self.provides_constant_result(attr.get_value()): |
paul@646 | 219 | node._access_type = "constant" |
paul@646 | 220 | node._value_deduced = attr.get_value() |
paul@710 | 221 | node._set_context = "set" |
paul@646 | 222 | return |
paul@646 | 223 | |
paul@646 | 224 | # Compatible class attributes. |
paul@646 | 225 | |
paul@646 | 226 | if attr.defined_within_hierarchy(): |
paul@646 | 227 | node._access_type = "static" |
paul@646 | 228 | node._attr_deduced = attr |
paul@646 | 229 | node._set_context = "set" |
paul@646 | 230 | return |
paul@646 | 231 | |
paul@646 | 232 | # Incompatible class attributes. |
paul@646 | 233 | |
paul@646 | 234 | elif attr.defined_outside_hierarchy(): |
paul@646 | 235 | node._access_type = "static" |
paul@646 | 236 | node._attr_deduced = attr |
paul@646 | 237 | return |
paul@646 | 238 | |
paul@646 | 239 | # Unknown or mixed compatibility. |
paul@646 | 240 | |
paul@646 | 241 | node._access_type = "static" |
paul@646 | 242 | node._attr_deduced = attr |
paul@646 | 243 | node._set_context = "cond" |
paul@646 | 244 | return |
paul@646 | 245 | |
paul@646 | 246 | # Usage observations, both specific to this node's region of the program |
paul@646 | 247 | # and also applicable to the lifespan of the affected name. |
paul@646 | 248 | |
paul@646 | 249 | specific_targets = self.possible_accessors_from_usage(node, defining_users=0) |
paul@646 | 250 | targets = self.possible_accessors_from_usage(node, defining_users=1) |
paul@646 | 251 | |
paul@646 | 252 | # Record whether types were already deduced. If not, get types using |
paul@646 | 253 | # only this attribute. |
paul@646 | 254 | |
paul@646 | 255 | if not specific_targets or not targets: |
paul@646 | 256 | attribute_targets = self.possible_accessors_for_attribute(node.attrname) |
paul@646 | 257 | if not specific_targets: |
paul@646 | 258 | specific_targets = attribute_targets |
paul@646 | 259 | if not targets: |
paul@646 | 260 | targets = attribute_targets |
paul@646 | 261 | |
paul@698 | 262 | # Get the attributes from the deduced targets. |
paul@698 | 263 | |
paul@646 | 264 | node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, node.attrname) |
paul@646 | 265 | node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, node.attrname) |
paul@646 | 266 | |
paul@646 | 267 | # Generate optimisations where only a single attribute applies. |
paul@646 | 268 | |
paul@646 | 269 | if attrs and len(attrs) == 1: |
paul@646 | 270 | for attr in attrs: |
paul@646 | 271 | |
paul@646 | 272 | # Static attributes, but potentially non-static targets. |
paul@646 | 273 | |
paul@646 | 274 | if attr.is_static_attribute(): |
paul@646 | 275 | |
paul@646 | 276 | # Static attributes may be accompanied by a different context |
paul@646 | 277 | # depending on the accessor. |
paul@646 | 278 | # NOTE: Should determine whether the context is always replaced. |
paul@646 | 279 | |
paul@646 | 280 | node._access_type = "static" |
paul@646 | 281 | node._attr_deduced = attr |
paul@646 | 282 | node._set_context = instance_target and "set" or "cond" |
paul@646 | 283 | return |
paul@646 | 284 | |
paul@646 | 285 | # Non-static attributes. |
paul@646 | 286 | |
paul@646 | 287 | node._access_type = "instance" |
paul@646 | 288 | node._attr_deduced = attr |
paul@646 | 289 | return |
paul@646 | 290 | |
paul@646 | 291 | # Test for compatible attribute positioning. |
paul@646 | 292 | |
paul@646 | 293 | elif attrs: |
paul@646 | 294 | positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) |
paul@646 | 295 | |
paul@646 | 296 | # Permit a position-based access only on non-static attributes since |
paul@646 | 297 | # access to static attributes may happen via instances and thus not |
paul@646 | 298 | # be relative to the accessor but to its parent. |
paul@646 | 299 | |
paul@646 | 300 | if len(positions) == 1: |
paul@646 | 301 | for position in positions: |
paul@646 | 302 | if not position[0]: |
paul@646 | 303 | node._access_type = "positioned" |
paul@646 | 304 | node._position_deduced = position[0] |
paul@646 | 305 | return |
paul@646 | 306 | |
paul@646 | 307 | # With no usable deductions, generate a table-based access. |
paul@646 | 308 | |
paul@646 | 309 | node._access_type = "unknown" |
paul@646 | 310 | node._set_context = "cond" |
paul@646 | 311 | |
paul@646 | 312 | visitAssAttr = visitGetattr = _visitAttr |
paul@646 | 313 | |
paul@710 | 314 | def visitAssign(self, node): |
paul@710 | 315 | self.expr = self.dispatch(node.expr) |
paul@710 | 316 | for n in node.nodes: |
paul@710 | 317 | self.dispatch(n) |
paul@710 | 318 | |
paul@710 | 319 | def visitAssName(self, node): |
paul@710 | 320 | if self.expr: |
paul@710 | 321 | if isinstance(self.expr, Attr): |
paul@710 | 322 | expr = self.expr.get_value() |
paul@710 | 323 | elif isinstance(self.expr, TypedInstance): |
paul@710 | 324 | expr = self.expr |
paul@710 | 325 | else: |
paul@710 | 326 | return |
paul@710 | 327 | else: |
paul@710 | 328 | return |
paul@710 | 329 | |
paul@712 | 330 | value = node._values and node._values.get(node.name) or None |
paul@710 | 331 | |
paul@712 | 332 | # Need to replace any uncertain value with a concrete value. |
paul@710 | 333 | |
paul@712 | 334 | if value and isinstance(value, Instance) and not isinstance(value, TypedInstance): |
paul@712 | 335 | node._values[node.name] = expr |
paul@710 | 336 | |
paul@704 | 337 | def visitCallFunc(self, node): |
paul@704 | 338 | |
paul@704 | 339 | "Identify any concrete types involved with instantiation." |
paul@704 | 340 | |
paul@704 | 341 | for n in node.getChildNodes(): |
paul@704 | 342 | self.dispatch(n) |
paul@704 | 343 | |
paul@704 | 344 | # Determine whether the target of the invocation refers to a class. |
paul@704 | 345 | |
paul@704 | 346 | attr = node.node._attr |
paul@704 | 347 | |
paul@704 | 348 | if attr: |
paul@704 | 349 | value = attr.get_value() |
paul@704 | 350 | if value and isinstance(value, Class): |
paul@710 | 351 | return TypedInstance(value) |
paul@704 | 352 | |
paul@646 | 353 | # Convenience functions. |
paul@646 | 354 | |
paul@646 | 355 | def deduce(program): |
paul@646 | 356 | for module in program.get_importer().get_modules(): |
paul@646 | 357 | DeducedSource(module, program).deduce() |
paul@646 | 358 | |
paul@646 | 359 | # vim: tabstop=4 expandtab shiftwidth=4 |