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@784 | 6 | Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013, 2014 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@765 | 22 | from micropython.stdcompiler import compiler |
paul@784 | 23 | from compiler.ast import * |
paul@765 | 24 | |
paul@765 | 25 | from micropython.basicdata import Const, Constant, TypedInstance |
paul@765 | 26 | from micropython.common import ASTVisitor, used_by_unit |
paul@646 | 27 | from micropython.data import * |
paul@646 | 28 | from micropython.errors import * |
paul@646 | 29 | |
paul@765 | 30 | try: |
paul@765 | 31 | set |
paul@765 | 32 | except NameError: |
paul@765 | 33 | from sets import Set as set |
paul@765 | 34 | |
paul@646 | 35 | # Source code classes. |
paul@646 | 36 | |
paul@646 | 37 | class DeducedSource(ASTVisitor): |
paul@646 | 38 | |
paul@646 | 39 | "A module upon which deductions of code behaviour are made." |
paul@646 | 40 | |
paul@646 | 41 | def __init__(self, module, program): |
paul@646 | 42 | self.visitor = self |
paul@646 | 43 | self.module = module |
paul@646 | 44 | self.program = program |
paul@646 | 45 | self.objtable = program.get_object_table() |
paul@646 | 46 | self.units = [] |
paul@710 | 47 | self.expr = None |
paul@646 | 48 | |
paul@646 | 49 | def get_unit(self): |
paul@646 | 50 | return self.units[-1] |
paul@646 | 51 | |
paul@646 | 52 | def get_module(self): |
paul@646 | 53 | return self.units[0] |
paul@646 | 54 | |
paul@646 | 55 | def deduce(self): |
paul@646 | 56 | |
paul@646 | 57 | "Process the module, making deductions." |
paul@646 | 58 | |
paul@646 | 59 | self.dispatch(self.module.astnode) |
paul@646 | 60 | |
paul@646 | 61 | def dispatch(self, node, *args): |
paul@646 | 62 | |
paul@646 | 63 | "NOTE: From compiler.visitor.ASTVisitor." |
paul@646 | 64 | |
paul@646 | 65 | try: |
paul@646 | 66 | return node.visit(self.visitor, *args) |
paul@646 | 67 | except AttributeError: |
paul@646 | 68 | # NOTE: Obligatory hack to find real attribute errors. |
paul@784 | 69 | if isinstance(node, self.implemented_nodes): |
paul@784 | 70 | raise |
paul@646 | 71 | return self.visitor.default(node, *args) |
paul@646 | 72 | |
paul@784 | 73 | implemented_nodes = ( |
paul@784 | 74 | AssAttr, Assign, AssName, AssList, AssTuple, CallFunc, Function, Getattr, |
paul@784 | 75 | Add, Bitand, Bitor, Bitxor, Div, FloorDiv, Invert, LeftShift, Mod, Mul, |
paul@784 | 76 | Power, RightShift, Sub, UnaryAdd, UnarySub |
paul@784 | 77 | ) |
paul@765 | 78 | |
paul@765 | 79 | # Deduction-related methods. |
paul@765 | 80 | |
paul@765 | 81 | def provides_constant_result(self, value): |
paul@765 | 82 | |
paul@765 | 83 | "Return whether 'value' provides a constant result." |
paul@765 | 84 | |
paul@765 | 85 | return isinstance(value, (Const, Constant)) |
paul@765 | 86 | |
paul@765 | 87 | def provides_self_access(self, expr, unit): |
paul@765 | 88 | |
paul@765 | 89 | """ |
paul@765 | 90 | Return whether the 'expr' in the given 'unit' provides a self-based |
paul@765 | 91 | attribute access. |
paul@765 | 92 | """ |
paul@765 | 93 | |
paul@765 | 94 | attr_value = self.get_attribute_and_value(expr) |
paul@765 | 95 | |
paul@765 | 96 | if attr_value: |
paul@765 | 97 | target, value = attr_value |
paul@765 | 98 | |
paul@765 | 99 | return target and target.name == "self" and target.parent is unit and \ |
paul@765 | 100 | unit.is_method() |
paul@765 | 101 | |
paul@765 | 102 | return False |
paul@765 | 103 | |
paul@765 | 104 | def possible_attributes_from_annotation(self, expr, attr, attrname): |
paul@765 | 105 | |
paul@765 | 106 | """ |
paul@765 | 107 | Return (attribute, value) details provided by the 'expr' or 'attr' |
paul@765 | 108 | annotations on a node for an access involving 'attrname'. |
paul@765 | 109 | """ |
paul@765 | 110 | |
paul@765 | 111 | attr_value = self.get_attribute_and_value(attr) |
paul@765 | 112 | |
paul@765 | 113 | if attr_value: |
paul@765 | 114 | return [attr_value] |
paul@765 | 115 | |
paul@765 | 116 | attrs = set() |
paul@765 | 117 | |
paul@765 | 118 | if expr: |
paul@765 | 119 | |
paul@765 | 120 | # Permitting multiple expression types if they provide the |
paul@765 | 121 | # attribute. |
paul@765 | 122 | |
paul@765 | 123 | if isinstance(expr, BaseAttr): |
paul@765 | 124 | exprs = expr.get_values() |
paul@765 | 125 | else: |
paul@765 | 126 | exprs = [expr] |
paul@765 | 127 | |
paul@765 | 128 | # For each expression value try and get a concrete |
paul@765 | 129 | # attribute. |
paul@765 | 130 | |
paul@765 | 131 | for expr in exprs: |
paul@765 | 132 | found_attr = expr.all_attributes().get(attrname) |
paul@765 | 133 | |
paul@765 | 134 | # Where an attribute can be obtained, record its |
paul@765 | 135 | # details. |
paul@765 | 136 | |
paul@765 | 137 | if found_attr: |
paul@765 | 138 | attrs.add((found_attr, found_attr.get_value())) |
paul@765 | 139 | |
paul@765 | 140 | return attrs |
paul@765 | 141 | |
paul@765 | 142 | def possible_accessor_types_from_usage(self, node, defining_users=1): |
paul@765 | 143 | |
paul@765 | 144 | """ |
paul@765 | 145 | Return a set of (target name, static) tuples from an investigation of |
paul@765 | 146 | attribute usage observations stored on the given 'node'. |
paul@765 | 147 | |
paul@765 | 148 | If 'defining_users' is set to a false value, attempt to get the type |
paul@765 | 149 | names specifically applicable to the node, rather than retrieving more |
paul@765 | 150 | general definition-based type observations. |
paul@765 | 151 | """ |
paul@765 | 152 | |
paul@765 | 153 | target_names = set() |
paul@765 | 154 | |
paul@765 | 155 | if node._attrusers: |
paul@765 | 156 | |
paul@765 | 157 | # Visit each attribute user. |
paul@765 | 158 | |
paul@765 | 159 | for user in node._attrusers: |
paul@765 | 160 | |
paul@765 | 161 | # Since users such as branches may not provide type information, |
paul@765 | 162 | # attempt to find defining users. |
paul@765 | 163 | |
paul@765 | 164 | if defining_users: |
paul@765 | 165 | for def_user in user._attrdefs or [user]: |
paul@765 | 166 | for target_name, is_static in def_user._attrtypes.get(node._username, []): |
paul@765 | 167 | target_names.add((target_name, is_static)) |
paul@765 | 168 | else: |
paul@765 | 169 | for target_name, is_static in user._attrspecifictypes.get(node._username, []): |
paul@765 | 170 | target_names.add((target_name, is_static)) |
paul@765 | 171 | |
paul@765 | 172 | return target_names |
paul@765 | 173 | |
paul@765 | 174 | def possible_accessors_from_usage(self, node, defining_users=1): |
paul@765 | 175 | |
paul@765 | 176 | """ |
paul@765 | 177 | Return possible accessors from the usage recorded on the given 'node'. |
paul@765 | 178 | |
paul@765 | 179 | If 'defining_users' is set to a false value, attempt to get the type |
paul@765 | 180 | names specifically applicable to the node, rather than retrieving more |
paul@765 | 181 | general definition-based type observations. |
paul@765 | 182 | """ |
paul@765 | 183 | |
paul@765 | 184 | targets = set() |
paul@765 | 185 | target_names = self.possible_accessor_types_from_usage(node, defining_users) |
paul@765 | 186 | |
paul@765 | 187 | if target_names: |
paul@765 | 188 | for target_name, is_static in target_names: |
paul@765 | 189 | targets.add(self.objtable.get_object(target_name)) |
paul@765 | 190 | |
paul@765 | 191 | return targets |
paul@765 | 192 | |
paul@765 | 193 | def possible_accessors_for_attribute(self, attrname): |
paul@765 | 194 | |
paul@765 | 195 | "Return possible accessors given the single 'attrname'." |
paul@765 | 196 | |
paul@765 | 197 | targets = set() |
paul@765 | 198 | |
paul@765 | 199 | for target_name in self.objtable.any_possible_objects([attrname]): |
paul@765 | 200 | targets.add(self.objtable.get_object(target_name)) |
paul@765 | 201 | |
paul@765 | 202 | return targets |
paul@765 | 203 | |
paul@765 | 204 | # Visitor methods. |
paul@765 | 205 | |
paul@646 | 206 | def _visitUnit(self, node): |
paul@646 | 207 | |
paul@646 | 208 | """ |
paul@646 | 209 | Track entry into program units in order to support various attribute |
paul@646 | 210 | access operations. |
paul@646 | 211 | """ |
paul@646 | 212 | |
paul@646 | 213 | self.units.append(node.unit) |
paul@646 | 214 | self.dispatch(node.node) |
paul@646 | 215 | self.units.pop() |
paul@646 | 216 | |
paul@786 | 217 | visitModule = _visitUnit |
paul@649 | 218 | |
paul@786 | 219 | def visitClass(self, node): |
paul@786 | 220 | |
paul@786 | 221 | "Optionally visit a class, depending on whether it is used." |
paul@649 | 222 | |
paul@649 | 223 | if not used_by_unit(node): |
paul@649 | 224 | return |
paul@649 | 225 | self._visitUnit(node) |
paul@649 | 226 | |
paul@786 | 227 | def visitFunction(self, node): |
paul@786 | 228 | |
paul@786 | 229 | "Optionally visit a function, depending on whether it is used." |
paul@786 | 230 | |
paul@786 | 231 | if not used_by_unit(node): |
paul@786 | 232 | return |
paul@786 | 233 | |
paul@786 | 234 | self.units.append(node.unit) |
paul@786 | 235 | self.dispatch(node.code) |
paul@786 | 236 | self.units.pop() |
paul@786 | 237 | |
paul@786 | 238 | node._guard_types = {} |
paul@786 | 239 | node._guards = {} |
paul@786 | 240 | |
paul@786 | 241 | for name in node.unit.positional_names: |
paul@786 | 242 | # NOTE: Test for tuple parameters (see micropython.report.Report._parameters). |
paul@786 | 243 | # NOTE: No _values usage here because it is mostly useless information, except perhaps for defaults. |
paul@786 | 244 | attrtypes = node._attrtypes.get(name) |
paul@786 | 245 | self._visitGuardForNameDeduced(node, name, attrtypes) |
paul@646 | 246 | |
paul@646 | 247 | def _visitAttr(self, node): |
paul@646 | 248 | |
paul@646 | 249 | """ |
paul@646 | 250 | Perform deductions on attribute accesses, adding annotations to the node |
paul@646 | 251 | that can be used by subsequent activities. |
paul@646 | 252 | """ |
paul@646 | 253 | |
paul@704 | 254 | # Remember to permit deductions on the expression node. Here, we may |
paul@704 | 255 | # also obtain a concrete type associated with an instantiation. |
paul@650 | 256 | |
paul@704 | 257 | expr_type = self.dispatch(node.expr) |
paul@704 | 258 | if not node._expr or isinstance(node._expr, Instance): |
paul@704 | 259 | node._expr = expr_type |
paul@650 | 260 | |
paul@646 | 261 | # The target, on which the access is performed, may influence the effect |
paul@646 | 262 | # on the context. We can only reliably assume that a literal constant is |
paul@646 | 263 | # an instance: all other "instances" may actually be classes in certain |
paul@646 | 264 | # cases. |
paul@646 | 265 | |
paul@764 | 266 | self._annotateAttr(node, node._expr, node.attrname) |
paul@764 | 267 | |
paul@764 | 268 | def _annotateAttr(self, node, target, attrname): |
paul@764 | 269 | |
paul@764 | 270 | """ |
paul@764 | 271 | Annotate the access on the given 'node' using the 'target' and |
paul@764 | 272 | 'attrname' information. |
paul@764 | 273 | """ |
paul@764 | 274 | |
paul@764 | 275 | unit = self.get_unit() |
paul@764 | 276 | |
paul@710 | 277 | instance_target = isinstance(target, TypedInstance) |
paul@717 | 278 | typed_instance_attr = isinstance(target, BaseAttr) and isinstance(target.get_value(), TypedInstance) |
paul@764 | 279 | self_access = self.provides_self_access(target, unit) |
paul@646 | 280 | |
paul@646 | 281 | # Attempt to deduce attributes from explicit annotations. |
paul@646 | 282 | |
paul@764 | 283 | node._attrs_deduced = attrs = self.possible_attributes_from_annotation(target, node._attr, attrname) |
paul@646 | 284 | |
paul@646 | 285 | if len(attrs) == 1: |
paul@646 | 286 | for attr, value in attrs: |
paul@646 | 287 | |
paul@646 | 288 | # Constant values can be obtained directly. |
paul@646 | 289 | |
paul@646 | 290 | if self.provides_constant_result(value): |
paul@646 | 291 | node._access_type = "constant" |
paul@646 | 292 | node._value_deduced = value |
paul@646 | 293 | return |
paul@646 | 294 | |
paul@646 | 295 | # Static attributes can be obtained via their parent. |
paul@646 | 296 | |
paul@646 | 297 | if attr.is_static_attribute(): |
paul@646 | 298 | node._access_type = "static" |
paul@646 | 299 | node._attr_deduced = attr |
paul@646 | 300 | node._set_context = instance_target and "set" or None |
paul@646 | 301 | return |
paul@646 | 302 | |
paul@698 | 303 | # If a reliable target was originally specified, any usable attributes |
paul@698 | 304 | # should have been detected above, and any attributes deduced by other |
paul@698 | 305 | # means will not be compatible with the target. Thus, the nature of the |
paul@698 | 306 | # target is investigated: it must be an inspectable namespace or be an |
paul@698 | 307 | # attribute only providing such namespaces; otherwise, it is possible |
paul@698 | 308 | # that deduced attributes might be appropriate. |
paul@698 | 309 | |
paul@698 | 310 | if target and ( |
paul@698 | 311 | isinstance(target, (Class, Module)) or |
paul@717 | 312 | isinstance(target, BaseAttr) and not [v for v in target.get_values() if not isinstance(v, (Class, Module))] |
paul@698 | 313 | ): |
paul@698 | 314 | node._access_type = "impossible" |
paul@698 | 315 | return |
paul@698 | 316 | |
paul@710 | 317 | # Attributes of self, which is by definition an instance, or typed |
paul@710 | 318 | # instances, which act somewhat like self in that their properties |
paul@710 | 319 | # should be known. |
paul@710 | 320 | |
paul@710 | 321 | if instance_target or typed_instance_attr or self_access: |
paul@646 | 322 | |
paul@710 | 323 | if instance_target: |
paul@710 | 324 | value = target |
paul@710 | 325 | elif typed_instance_attr: |
paul@710 | 326 | value = target.get_value() |
paul@710 | 327 | |
paul@710 | 328 | # Find the class of the instance. |
paul@710 | 329 | |
paul@710 | 330 | if instance_target or typed_instance_attr: |
paul@710 | 331 | if isinstance(value, Const): |
paul@710 | 332 | cls = get_constant_class(value.get_class_name()) |
paul@710 | 333 | else: |
paul@710 | 334 | cls = value.cls |
paul@710 | 335 | else: |
paul@710 | 336 | cls = unit.parent |
paul@646 | 337 | |
paul@646 | 338 | # Find instance attributes. |
paul@646 | 339 | |
paul@764 | 340 | attr = cls.instance_attributes().get(attrname) |
paul@710 | 341 | |
paul@710 | 342 | # Where self is involved, descendants can also provide attributes. |
paul@710 | 343 | |
paul@764 | 344 | attrs = self_access and filter(None, [desc.instance_attributes().get(attrname) for desc in cls.descendants]) or [] |
paul@646 | 345 | |
paul@693 | 346 | # A "leaf" class whose instances provide an attribute. |
paul@693 | 347 | |
paul@693 | 348 | if attr and not attrs: |
paul@646 | 349 | node._access_type = "instance" |
paul@646 | 350 | node._attr_deduced = attr |
paul@646 | 351 | return |
paul@646 | 352 | |
paul@693 | 353 | # A class where instances of subclasses may provide an attribute. |
paul@693 | 354 | |
paul@722 | 355 | elif attrs and self_access: |
paul@693 | 356 | if attr: |
paul@693 | 357 | attrs.append(attr) |
paul@693 | 358 | |
paul@693 | 359 | node._attrs_deduced = [(a, a.get_value()) for a in attrs] |
paul@693 | 360 | |
paul@693 | 361 | # The instances may provide the attribute at the same position. |
paul@693 | 362 | |
paul@693 | 363 | positions = set([a.position for a in attrs]) |
paul@693 | 364 | if len(positions) == 1: |
paul@693 | 365 | for position in positions: |
paul@693 | 366 | node._access_type = "positioned" |
paul@693 | 367 | node._position_deduced = position |
paul@693 | 368 | return |
paul@693 | 369 | |
paul@693 | 370 | # Otherwise, accessing the attributes is more work. |
paul@693 | 371 | |
paul@693 | 372 | node._access_type = "instance" |
paul@693 | 373 | return |
paul@693 | 374 | |
paul@646 | 375 | # Find class attributes. |
paul@646 | 376 | # The context will be overridden for compatible class attributes |
paul@646 | 377 | # only. |
paul@646 | 378 | |
paul@764 | 379 | attr = cls.all_class_attributes().get(attrname) |
paul@646 | 380 | |
paul@646 | 381 | if attr: |
paul@646 | 382 | |
paul@646 | 383 | # Constant attributes. |
paul@646 | 384 | |
paul@646 | 385 | if attr.is_strict_constant(): |
paul@646 | 386 | if self.provides_constant_result(attr.get_value()): |
paul@646 | 387 | node._access_type = "constant" |
paul@646 | 388 | node._value_deduced = attr.get_value() |
paul@710 | 389 | node._set_context = "set" |
paul@646 | 390 | return |
paul@646 | 391 | |
paul@646 | 392 | # Compatible class attributes. |
paul@646 | 393 | |
paul@646 | 394 | if attr.defined_within_hierarchy(): |
paul@646 | 395 | node._access_type = "static" |
paul@646 | 396 | node._attr_deduced = attr |
paul@646 | 397 | node._set_context = "set" |
paul@646 | 398 | return |
paul@646 | 399 | |
paul@646 | 400 | # Incompatible class attributes. |
paul@646 | 401 | |
paul@646 | 402 | elif attr.defined_outside_hierarchy(): |
paul@646 | 403 | node._access_type = "static" |
paul@646 | 404 | node._attr_deduced = attr |
paul@646 | 405 | return |
paul@646 | 406 | |
paul@646 | 407 | # Unknown or mixed compatibility. |
paul@646 | 408 | |
paul@646 | 409 | node._access_type = "static" |
paul@646 | 410 | node._attr_deduced = attr |
paul@646 | 411 | node._set_context = "cond" |
paul@646 | 412 | return |
paul@646 | 413 | |
paul@646 | 414 | # Usage observations, both specific to this node's region of the program |
paul@646 | 415 | # and also applicable to the lifespan of the affected name. |
paul@646 | 416 | |
paul@646 | 417 | specific_targets = self.possible_accessors_from_usage(node, defining_users=0) |
paul@646 | 418 | targets = self.possible_accessors_from_usage(node, defining_users=1) |
paul@646 | 419 | |
paul@646 | 420 | # Record whether types were already deduced. If not, get types using |
paul@646 | 421 | # only this attribute. |
paul@646 | 422 | |
paul@646 | 423 | if not specific_targets or not targets: |
paul@764 | 424 | attribute_targets = self.possible_accessors_for_attribute(attrname) |
paul@646 | 425 | if not specific_targets: |
paul@646 | 426 | specific_targets = attribute_targets |
paul@646 | 427 | if not targets: |
paul@646 | 428 | targets = attribute_targets |
paul@646 | 429 | |
paul@698 | 430 | # Get the attributes from the deduced targets. |
paul@698 | 431 | |
paul@764 | 432 | node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, attrname) |
paul@764 | 433 | node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, attrname) |
paul@646 | 434 | |
paul@646 | 435 | # Generate optimisations where only a single attribute applies. |
paul@646 | 436 | |
paul@646 | 437 | if attrs and len(attrs) == 1: |
paul@646 | 438 | for attr in attrs: |
paul@646 | 439 | |
paul@646 | 440 | # Static attributes, but potentially non-static targets. |
paul@646 | 441 | |
paul@646 | 442 | if attr.is_static_attribute(): |
paul@646 | 443 | |
paul@646 | 444 | # Static attributes may be accompanied by a different context |
paul@646 | 445 | # depending on the accessor. |
paul@646 | 446 | # NOTE: Should determine whether the context is always replaced. |
paul@646 | 447 | |
paul@646 | 448 | node._access_type = "static" |
paul@646 | 449 | node._attr_deduced = attr |
paul@646 | 450 | node._set_context = instance_target and "set" or "cond" |
paul@646 | 451 | return |
paul@646 | 452 | |
paul@646 | 453 | # Non-static attributes. |
paul@646 | 454 | |
paul@646 | 455 | node._access_type = "instance" |
paul@646 | 456 | node._attr_deduced = attr |
paul@646 | 457 | return |
paul@646 | 458 | |
paul@646 | 459 | # Test for compatible attribute positioning. |
paul@646 | 460 | |
paul@646 | 461 | elif attrs: |
paul@646 | 462 | positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) |
paul@646 | 463 | |
paul@646 | 464 | # Permit a position-based access only on non-static attributes since |
paul@646 | 465 | # access to static attributes may happen via instances and thus not |
paul@646 | 466 | # be relative to the accessor but to its parent. |
paul@646 | 467 | |
paul@646 | 468 | if len(positions) == 1: |
paul@646 | 469 | for position in positions: |
paul@646 | 470 | if not position[0]: |
paul@646 | 471 | node._access_type = "positioned" |
paul@646 | 472 | node._position_deduced = position[0] |
paul@646 | 473 | return |
paul@646 | 474 | |
paul@646 | 475 | # With no usable deductions, generate a table-based access. |
paul@646 | 476 | |
paul@646 | 477 | node._access_type = "unknown" |
paul@646 | 478 | node._set_context = "cond" |
paul@646 | 479 | |
paul@646 | 480 | visitAssAttr = visitGetattr = _visitAttr |
paul@646 | 481 | |
paul@710 | 482 | def visitAssign(self, node): |
paul@710 | 483 | self.expr = self.dispatch(node.expr) |
paul@710 | 484 | for n in node.nodes: |
paul@710 | 485 | self.dispatch(n) |
paul@710 | 486 | |
paul@714 | 487 | def visitAssList(self, node): |
paul@714 | 488 | expr = self.expr |
paul@714 | 489 | self.expr = make_instance() |
paul@714 | 490 | for n in node.nodes: |
paul@714 | 491 | self.dispatch(n) |
paul@714 | 492 | self.expr = expr |
paul@714 | 493 | |
paul@714 | 494 | visitAssTuple = visitAssList |
paul@714 | 495 | |
paul@710 | 496 | def visitAssName(self, node): |
paul@710 | 497 | if self.expr: |
paul@717 | 498 | if isinstance(self.expr, BaseAttr): |
paul@710 | 499 | expr = self.expr.get_value() |
paul@710 | 500 | elif isinstance(self.expr, TypedInstance): |
paul@710 | 501 | expr = self.expr |
paul@710 | 502 | else: |
paul@786 | 503 | self._visitGuard(node) |
paul@710 | 504 | return |
paul@710 | 505 | else: |
paul@786 | 506 | self._visitGuard(node) |
paul@710 | 507 | return |
paul@710 | 508 | |
paul@717 | 509 | attr = node._values and node._values.get(node.name) or None |
paul@710 | 510 | |
paul@712 | 511 | # Need to replace any uncertain value with a concrete value. |
paul@710 | 512 | |
paul@717 | 513 | if attr: |
paul@717 | 514 | if isinstance(attr, BaseAttr): |
paul@717 | 515 | value = attr.get_value() |
paul@717 | 516 | else: |
paul@717 | 517 | value = attr |
paul@717 | 518 | |
paul@717 | 519 | if value and isinstance(value, Instance) and not isinstance(value, TypedInstance): |
paul@783 | 520 | node._values[node.name] = expr |
paul@710 | 521 | |
paul@786 | 522 | self._visitGuard(node) |
paul@786 | 523 | |
paul@786 | 524 | def _visitGuard(self, node): |
paul@786 | 525 | node._guard_types = {} |
paul@786 | 526 | node._guards = {} |
paul@786 | 527 | self._visitGuardForName(node, node.name) |
paul@786 | 528 | |
paul@786 | 529 | def _visitGuardForName(self, node, name): |
paul@786 | 530 | |
paul@786 | 531 | "Make an annotation stating the acceptable types for a name." |
paul@786 | 532 | |
paul@786 | 533 | # Need to check any concrete value against deductions. |
paul@786 | 534 | |
paul@786 | 535 | attrtypes = node._attrtypes.get(name) |
paul@786 | 536 | value = node._values.get(name) |
paul@786 | 537 | |
paul@786 | 538 | # Where a concrete type conflicts with deductions, the usage of |
paul@786 | 539 | # attributes cannot be supported and is thus impossible. |
paul@786 | 540 | |
paul@786 | 541 | if value: |
paul@786 | 542 | if attrtypes and value not in attrtypes: |
paul@786 | 543 | node._guard_types[name] = "impossible" |
paul@786 | 544 | else: |
paul@786 | 545 | node._guard_types[name] = "single" |
paul@786 | 546 | node._guards[name] = set([value]) |
paul@786 | 547 | |
paul@786 | 548 | # Where no concrete type is known, usage must be checked. |
paul@786 | 549 | |
paul@786 | 550 | elif attrtypes: |
paul@786 | 551 | self._visitGuardForNameDeduced(node, name, attrtypes) |
paul@786 | 552 | |
paul@786 | 553 | # Where no deductions are made, no guards are needed. |
paul@786 | 554 | |
paul@786 | 555 | def _visitGuardForNameDeduced(self, node, name, attrtypes): |
paul@786 | 556 | if not attrtypes: |
paul@786 | 557 | return |
paul@786 | 558 | if len(attrtypes) == 1: |
paul@786 | 559 | node._guard_types[name] = "single" |
paul@786 | 560 | else: |
paul@786 | 561 | node._guard_types[name] = "multiple" |
paul@786 | 562 | node._guards[name] = attrtypes |
paul@786 | 563 | |
paul@704 | 564 | def visitCallFunc(self, node): |
paul@704 | 565 | |
paul@704 | 566 | "Identify any concrete types involved with instantiation." |
paul@704 | 567 | |
paul@704 | 568 | for n in node.getChildNodes(): |
paul@704 | 569 | self.dispatch(n) |
paul@704 | 570 | |
paul@704 | 571 | # Determine whether the target of the invocation refers to a class. |
paul@704 | 572 | |
paul@704 | 573 | attr = node.node._attr |
paul@704 | 574 | |
paul@786 | 575 | if attr and not isinstance(attr, Instance): |
paul@704 | 576 | value = attr.get_value() |
paul@704 | 577 | if value and isinstance(value, Class): |
paul@710 | 578 | return TypedInstance(value) |
paul@704 | 579 | |
paul@764 | 580 | def _visitOperator(self, node): |
paul@764 | 581 | |
paul@764 | 582 | "Annotate operators with function information." |
paul@764 | 583 | |
paul@764 | 584 | self._annotateAttr(node, node._module, node._attr.name) |
paul@764 | 585 | |
paul@764 | 586 | visitAdd = \ |
paul@764 | 587 | visitBitand = \ |
paul@764 | 588 | visitBitor = \ |
paul@764 | 589 | visitBitxor = \ |
paul@764 | 590 | visitDiv = \ |
paul@764 | 591 | visitFloorDiv = \ |
paul@764 | 592 | visitInvert = \ |
paul@764 | 593 | visitLeftShift = \ |
paul@764 | 594 | visitMod = \ |
paul@764 | 595 | visitMul = \ |
paul@764 | 596 | visitPower = \ |
paul@764 | 597 | visitRightShift = \ |
paul@764 | 598 | visitSub = \ |
paul@764 | 599 | visitUnaryAdd = \ |
paul@764 | 600 | visitUnarySub = _visitOperator |
paul@764 | 601 | |
paul@646 | 602 | # Convenience functions. |
paul@646 | 603 | |
paul@646 | 604 | def deduce(program): |
paul@646 | 605 | for module in program.get_importer().get_modules(): |
paul@646 | 606 | DeducedSource(module, program).deduce() |
paul@646 | 607 | |
paul@646 | 608 | # vim: tabstop=4 expandtab shiftwidth=4 |