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 | |
paul@646 | 26 | # Source code classes. |
paul@646 | 27 | |
paul@646 | 28 | class DeducedSource(ASTVisitor): |
paul@646 | 29 | |
paul@646 | 30 | "A module upon which deductions of code behaviour are made." |
paul@646 | 31 | |
paul@646 | 32 | def __init__(self, module, program): |
paul@646 | 33 | self.visitor = self |
paul@646 | 34 | self.module = module |
paul@646 | 35 | self.program = program |
paul@646 | 36 | self.objtable = program.get_object_table() |
paul@646 | 37 | self.units = [] |
paul@710 | 38 | self.expr = None |
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@704 | 93 | # Remember to permit deductions on the expression node. Here, we may |
paul@704 | 94 | # also obtain a concrete type associated with an instantiation. |
paul@650 | 95 | |
paul@704 | 96 | expr_type = self.dispatch(node.expr) |
paul@704 | 97 | if not node._expr or isinstance(node._expr, Instance): |
paul@704 | 98 | node._expr = expr_type |
paul@650 | 99 | |
paul@646 | 100 | # The target, on which the access is performed, may influence the effect |
paul@646 | 101 | # on the context. We can only reliably assume that a literal constant is |
paul@646 | 102 | # an instance: all other "instances" may actually be classes in certain |
paul@646 | 103 | # cases. |
paul@646 | 104 | |
paul@764 | 105 | self._annotateAttr(node, node._expr, node.attrname) |
paul@764 | 106 | |
paul@764 | 107 | def _annotateAttr(self, node, target, attrname): |
paul@764 | 108 | |
paul@764 | 109 | """ |
paul@764 | 110 | Annotate the access on the given 'node' using the 'target' and |
paul@764 | 111 | 'attrname' information. |
paul@764 | 112 | """ |
paul@764 | 113 | |
paul@764 | 114 | unit = self.get_unit() |
paul@764 | 115 | |
paul@710 | 116 | instance_target = isinstance(target, TypedInstance) |
paul@717 | 117 | typed_instance_attr = isinstance(target, BaseAttr) and isinstance(target.get_value(), TypedInstance) |
paul@764 | 118 | self_access = self.provides_self_access(target, unit) |
paul@646 | 119 | |
paul@646 | 120 | # Attempt to deduce attributes from explicit annotations. |
paul@646 | 121 | |
paul@764 | 122 | node._attrs_deduced = attrs = self.possible_attributes_from_annotation(target, node._attr, attrname) |
paul@646 | 123 | |
paul@646 | 124 | if len(attrs) == 1: |
paul@646 | 125 | for attr, value in attrs: |
paul@646 | 126 | |
paul@646 | 127 | # Constant values can be obtained directly. |
paul@646 | 128 | |
paul@646 | 129 | if self.provides_constant_result(value): |
paul@646 | 130 | node._access_type = "constant" |
paul@646 | 131 | node._value_deduced = value |
paul@646 | 132 | return |
paul@646 | 133 | |
paul@646 | 134 | # Static attributes can be obtained via their parent. |
paul@646 | 135 | |
paul@646 | 136 | if attr.is_static_attribute(): |
paul@646 | 137 | node._access_type = "static" |
paul@646 | 138 | node._attr_deduced = attr |
paul@646 | 139 | node._set_context = instance_target and "set" or None |
paul@646 | 140 | return |
paul@646 | 141 | |
paul@698 | 142 | # If a reliable target was originally specified, any usable attributes |
paul@698 | 143 | # should have been detected above, and any attributes deduced by other |
paul@698 | 144 | # means will not be compatible with the target. Thus, the nature of the |
paul@698 | 145 | # target is investigated: it must be an inspectable namespace or be an |
paul@698 | 146 | # attribute only providing such namespaces; otherwise, it is possible |
paul@698 | 147 | # that deduced attributes might be appropriate. |
paul@698 | 148 | |
paul@698 | 149 | if target and ( |
paul@698 | 150 | isinstance(target, (Class, Module)) or |
paul@717 | 151 | isinstance(target, BaseAttr) and not [v for v in target.get_values() if not isinstance(v, (Class, Module))] |
paul@698 | 152 | ): |
paul@698 | 153 | node._access_type = "impossible" |
paul@698 | 154 | return |
paul@698 | 155 | |
paul@710 | 156 | # Attributes of self, which is by definition an instance, or typed |
paul@710 | 157 | # instances, which act somewhat like self in that their properties |
paul@710 | 158 | # should be known. |
paul@710 | 159 | |
paul@710 | 160 | if instance_target or typed_instance_attr or self_access: |
paul@646 | 161 | |
paul@710 | 162 | if instance_target: |
paul@710 | 163 | value = target |
paul@710 | 164 | elif typed_instance_attr: |
paul@710 | 165 | value = target.get_value() |
paul@710 | 166 | |
paul@710 | 167 | # Find the class of the instance. |
paul@710 | 168 | |
paul@710 | 169 | if instance_target or typed_instance_attr: |
paul@710 | 170 | if isinstance(value, Const): |
paul@710 | 171 | cls = get_constant_class(value.get_class_name()) |
paul@710 | 172 | else: |
paul@710 | 173 | cls = value.cls |
paul@710 | 174 | else: |
paul@710 | 175 | cls = unit.parent |
paul@646 | 176 | |
paul@646 | 177 | # Find instance attributes. |
paul@646 | 178 | |
paul@764 | 179 | attr = cls.instance_attributes().get(attrname) |
paul@710 | 180 | |
paul@710 | 181 | # Where self is involved, descendants can also provide attributes. |
paul@710 | 182 | |
paul@764 | 183 | attrs = self_access and filter(None, [desc.instance_attributes().get(attrname) for desc in cls.descendants]) or [] |
paul@646 | 184 | |
paul@693 | 185 | # A "leaf" class whose instances provide an attribute. |
paul@693 | 186 | |
paul@693 | 187 | if attr and not attrs: |
paul@646 | 188 | node._access_type = "instance" |
paul@646 | 189 | node._attr_deduced = attr |
paul@646 | 190 | return |
paul@646 | 191 | |
paul@693 | 192 | # A class where instances of subclasses may provide an attribute. |
paul@693 | 193 | |
paul@722 | 194 | elif attrs and self_access: |
paul@693 | 195 | if attr: |
paul@693 | 196 | attrs.append(attr) |
paul@693 | 197 | |
paul@693 | 198 | node._attrs_deduced = [(a, a.get_value()) for a in attrs] |
paul@693 | 199 | |
paul@693 | 200 | # The instances may provide the attribute at the same position. |
paul@693 | 201 | |
paul@693 | 202 | positions = set([a.position for a in attrs]) |
paul@693 | 203 | if len(positions) == 1: |
paul@693 | 204 | for position in positions: |
paul@693 | 205 | node._access_type = "positioned" |
paul@693 | 206 | node._position_deduced = position |
paul@693 | 207 | return |
paul@693 | 208 | |
paul@693 | 209 | # Otherwise, accessing the attributes is more work. |
paul@693 | 210 | |
paul@693 | 211 | node._access_type = "instance" |
paul@693 | 212 | return |
paul@693 | 213 | |
paul@646 | 214 | # Find class attributes. |
paul@646 | 215 | # The context will be overridden for compatible class attributes |
paul@646 | 216 | # only. |
paul@646 | 217 | |
paul@764 | 218 | attr = cls.all_class_attributes().get(attrname) |
paul@646 | 219 | |
paul@646 | 220 | if attr: |
paul@646 | 221 | |
paul@646 | 222 | # Constant attributes. |
paul@646 | 223 | |
paul@646 | 224 | if attr.is_strict_constant(): |
paul@646 | 225 | if self.provides_constant_result(attr.get_value()): |
paul@646 | 226 | node._access_type = "constant" |
paul@646 | 227 | node._value_deduced = attr.get_value() |
paul@710 | 228 | node._set_context = "set" |
paul@646 | 229 | return |
paul@646 | 230 | |
paul@646 | 231 | # Compatible class attributes. |
paul@646 | 232 | |
paul@646 | 233 | if attr.defined_within_hierarchy(): |
paul@646 | 234 | node._access_type = "static" |
paul@646 | 235 | node._attr_deduced = attr |
paul@646 | 236 | node._set_context = "set" |
paul@646 | 237 | return |
paul@646 | 238 | |
paul@646 | 239 | # Incompatible class attributes. |
paul@646 | 240 | |
paul@646 | 241 | elif attr.defined_outside_hierarchy(): |
paul@646 | 242 | node._access_type = "static" |
paul@646 | 243 | node._attr_deduced = attr |
paul@646 | 244 | return |
paul@646 | 245 | |
paul@646 | 246 | # Unknown or mixed compatibility. |
paul@646 | 247 | |
paul@646 | 248 | node._access_type = "static" |
paul@646 | 249 | node._attr_deduced = attr |
paul@646 | 250 | node._set_context = "cond" |
paul@646 | 251 | return |
paul@646 | 252 | |
paul@646 | 253 | # Usage observations, both specific to this node's region of the program |
paul@646 | 254 | # and also applicable to the lifespan of the affected name. |
paul@646 | 255 | |
paul@646 | 256 | specific_targets = self.possible_accessors_from_usage(node, defining_users=0) |
paul@646 | 257 | targets = self.possible_accessors_from_usage(node, defining_users=1) |
paul@646 | 258 | |
paul@646 | 259 | # Record whether types were already deduced. If not, get types using |
paul@646 | 260 | # only this attribute. |
paul@646 | 261 | |
paul@646 | 262 | if not specific_targets or not targets: |
paul@764 | 263 | attribute_targets = self.possible_accessors_for_attribute(attrname) |
paul@646 | 264 | if not specific_targets: |
paul@646 | 265 | specific_targets = attribute_targets |
paul@646 | 266 | if not targets: |
paul@646 | 267 | targets = attribute_targets |
paul@646 | 268 | |
paul@698 | 269 | # Get the attributes from the deduced targets. |
paul@698 | 270 | |
paul@764 | 271 | node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, attrname) |
paul@764 | 272 | node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, attrname) |
paul@646 | 273 | |
paul@646 | 274 | # Generate optimisations where only a single attribute applies. |
paul@646 | 275 | |
paul@646 | 276 | if attrs and len(attrs) == 1: |
paul@646 | 277 | for attr in attrs: |
paul@646 | 278 | |
paul@646 | 279 | # Static attributes, but potentially non-static targets. |
paul@646 | 280 | |
paul@646 | 281 | if attr.is_static_attribute(): |
paul@646 | 282 | |
paul@646 | 283 | # Static attributes may be accompanied by a different context |
paul@646 | 284 | # depending on the accessor. |
paul@646 | 285 | # NOTE: Should determine whether the context is always replaced. |
paul@646 | 286 | |
paul@646 | 287 | node._access_type = "static" |
paul@646 | 288 | node._attr_deduced = attr |
paul@646 | 289 | node._set_context = instance_target and "set" or "cond" |
paul@646 | 290 | return |
paul@646 | 291 | |
paul@646 | 292 | # Non-static attributes. |
paul@646 | 293 | |
paul@646 | 294 | node._access_type = "instance" |
paul@646 | 295 | node._attr_deduced = attr |
paul@646 | 296 | return |
paul@646 | 297 | |
paul@646 | 298 | # Test for compatible attribute positioning. |
paul@646 | 299 | |
paul@646 | 300 | elif attrs: |
paul@646 | 301 | positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) |
paul@646 | 302 | |
paul@646 | 303 | # Permit a position-based access only on non-static attributes since |
paul@646 | 304 | # access to static attributes may happen via instances and thus not |
paul@646 | 305 | # be relative to the accessor but to its parent. |
paul@646 | 306 | |
paul@646 | 307 | if len(positions) == 1: |
paul@646 | 308 | for position in positions: |
paul@646 | 309 | if not position[0]: |
paul@646 | 310 | node._access_type = "positioned" |
paul@646 | 311 | node._position_deduced = position[0] |
paul@646 | 312 | return |
paul@646 | 313 | |
paul@646 | 314 | # With no usable deductions, generate a table-based access. |
paul@646 | 315 | |
paul@646 | 316 | node._access_type = "unknown" |
paul@646 | 317 | node._set_context = "cond" |
paul@646 | 318 | |
paul@646 | 319 | visitAssAttr = visitGetattr = _visitAttr |
paul@646 | 320 | |
paul@710 | 321 | def visitAssign(self, node): |
paul@710 | 322 | self.expr = self.dispatch(node.expr) |
paul@710 | 323 | for n in node.nodes: |
paul@710 | 324 | self.dispatch(n) |
paul@710 | 325 | |
paul@714 | 326 | def visitAssList(self, node): |
paul@714 | 327 | expr = self.expr |
paul@714 | 328 | self.expr = make_instance() |
paul@714 | 329 | for n in node.nodes: |
paul@714 | 330 | self.dispatch(n) |
paul@714 | 331 | self.expr = expr |
paul@714 | 332 | |
paul@714 | 333 | visitAssTuple = visitAssList |
paul@714 | 334 | |
paul@710 | 335 | def visitAssName(self, node): |
paul@710 | 336 | if self.expr: |
paul@717 | 337 | if isinstance(self.expr, BaseAttr): |
paul@710 | 338 | expr = self.expr.get_value() |
paul@710 | 339 | elif isinstance(self.expr, TypedInstance): |
paul@710 | 340 | expr = self.expr |
paul@710 | 341 | else: |
paul@710 | 342 | return |
paul@710 | 343 | else: |
paul@710 | 344 | return |
paul@710 | 345 | |
paul@717 | 346 | attr = node._values and node._values.get(node.name) or None |
paul@710 | 347 | |
paul@712 | 348 | # Need to replace any uncertain value with a concrete value. |
paul@710 | 349 | |
paul@717 | 350 | if attr: |
paul@717 | 351 | if isinstance(attr, BaseAttr): |
paul@717 | 352 | value = attr.get_value() |
paul@717 | 353 | else: |
paul@717 | 354 | value = attr |
paul@717 | 355 | |
paul@717 | 356 | if value and isinstance(value, Instance) and not isinstance(value, TypedInstance): |
paul@717 | 357 | node._values[node.name] = self.expr |
paul@710 | 358 | |
paul@704 | 359 | def visitCallFunc(self, node): |
paul@704 | 360 | |
paul@704 | 361 | "Identify any concrete types involved with instantiation." |
paul@704 | 362 | |
paul@704 | 363 | for n in node.getChildNodes(): |
paul@704 | 364 | self.dispatch(n) |
paul@704 | 365 | |
paul@704 | 366 | # Determine whether the target of the invocation refers to a class. |
paul@704 | 367 | |
paul@704 | 368 | attr = node.node._attr |
paul@704 | 369 | |
paul@704 | 370 | if attr: |
paul@704 | 371 | value = attr.get_value() |
paul@704 | 372 | if value and isinstance(value, Class): |
paul@710 | 373 | return TypedInstance(value) |
paul@704 | 374 | |
paul@764 | 375 | def _visitOperator(self, node): |
paul@764 | 376 | |
paul@764 | 377 | "Annotate operators with function information." |
paul@764 | 378 | |
paul@764 | 379 | self._annotateAttr(node, node._module, node._attr.name) |
paul@764 | 380 | |
paul@764 | 381 | visitAdd = \ |
paul@764 | 382 | visitBitand = \ |
paul@764 | 383 | visitBitor = \ |
paul@764 | 384 | visitBitxor = \ |
paul@764 | 385 | visitDiv = \ |
paul@764 | 386 | visitFloorDiv = \ |
paul@764 | 387 | visitInvert = \ |
paul@764 | 388 | visitLeftShift = \ |
paul@764 | 389 | visitMod = \ |
paul@764 | 390 | visitMul = \ |
paul@764 | 391 | visitPower = \ |
paul@764 | 392 | visitRightShift = \ |
paul@764 | 393 | visitSub = \ |
paul@764 | 394 | visitUnaryAdd = \ |
paul@764 | 395 | visitUnarySub = _visitOperator |
paul@764 | 396 | |
paul@646 | 397 | # Convenience functions. |
paul@646 | 398 | |
paul@646 | 399 | def deduce(program): |
paul@646 | 400 | for module in program.get_importer().get_modules(): |
paul@646 | 401 | DeducedSource(module, program).deduce() |
paul@646 | 402 | |
paul@646 | 403 | # vim: tabstop=4 expandtab shiftwidth=4 |