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@800 | 25 | from micropython.basicdata import 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@797 | 128 | # For each expression value try and get a concrete attribute. |
paul@765 | 129 | |
paul@797 | 130 | for _expr in exprs: |
paul@797 | 131 | if isinstance(_expr, Instance): |
paul@797 | 132 | continue |
paul@797 | 133 | |
paul@797 | 134 | found_attr = _expr.all_attributes().get(attrname) |
paul@765 | 135 | |
paul@765 | 136 | # Where an attribute can be obtained, record its |
paul@765 | 137 | # details. |
paul@765 | 138 | |
paul@765 | 139 | if found_attr: |
paul@765 | 140 | attrs.add((found_attr, found_attr.get_value())) |
paul@765 | 141 | |
paul@765 | 142 | return attrs |
paul@765 | 143 | |
paul@765 | 144 | def possible_accessor_types_from_usage(self, node, defining_users=1): |
paul@765 | 145 | |
paul@765 | 146 | """ |
paul@765 | 147 | Return a set of (target name, static) tuples from an investigation of |
paul@765 | 148 | attribute usage observations stored on the given 'node'. |
paul@765 | 149 | |
paul@765 | 150 | If 'defining_users' is set to a false value, attempt to get the type |
paul@765 | 151 | names specifically applicable to the node, rather than retrieving more |
paul@765 | 152 | general definition-based type observations. |
paul@765 | 153 | """ |
paul@765 | 154 | |
paul@765 | 155 | target_names = set() |
paul@765 | 156 | |
paul@765 | 157 | if node._attrusers: |
paul@765 | 158 | |
paul@765 | 159 | # Visit each attribute user. |
paul@765 | 160 | |
paul@765 | 161 | for user in node._attrusers: |
paul@765 | 162 | |
paul@765 | 163 | # Since users such as branches may not provide type information, |
paul@765 | 164 | # attempt to find defining users. |
paul@765 | 165 | |
paul@765 | 166 | if defining_users: |
paul@765 | 167 | for def_user in user._attrdefs or [user]: |
paul@765 | 168 | for target_name, is_static in def_user._attrtypes.get(node._username, []): |
paul@765 | 169 | target_names.add((target_name, is_static)) |
paul@765 | 170 | else: |
paul@765 | 171 | for target_name, is_static in user._attrspecifictypes.get(node._username, []): |
paul@765 | 172 | target_names.add((target_name, is_static)) |
paul@765 | 173 | |
paul@765 | 174 | return target_names |
paul@765 | 175 | |
paul@765 | 176 | def possible_accessors_from_usage(self, node, defining_users=1): |
paul@765 | 177 | |
paul@765 | 178 | """ |
paul@765 | 179 | Return possible accessors from the usage recorded on the given 'node'. |
paul@765 | 180 | |
paul@765 | 181 | If 'defining_users' is set to a false value, attempt to get the type |
paul@765 | 182 | names specifically applicable to the node, rather than retrieving more |
paul@765 | 183 | general definition-based type observations. |
paul@765 | 184 | """ |
paul@765 | 185 | |
paul@765 | 186 | target_names = self.possible_accessor_types_from_usage(node, defining_users) |
paul@787 | 187 | return self.get_targets_from_type_names(target_names) |
paul@765 | 188 | |
paul@765 | 189 | def possible_accessors_for_attribute(self, attrname): |
paul@765 | 190 | |
paul@765 | 191 | "Return possible accessors given the single 'attrname'." |
paul@765 | 192 | |
paul@765 | 193 | targets = set() |
paul@765 | 194 | |
paul@765 | 195 | for target_name in self.objtable.any_possible_objects([attrname]): |
paul@765 | 196 | targets.add(self.objtable.get_object(target_name)) |
paul@765 | 197 | |
paul@765 | 198 | return targets |
paul@765 | 199 | |
paul@787 | 200 | def get_targets_from_type_names(self, target_names): |
paul@787 | 201 | |
paul@787 | 202 | """ |
paul@787 | 203 | Given a collection of 'target_names' of the form (name, is_static), |
paul@787 | 204 | return a set of types for the 'name' part of each tuple. |
paul@787 | 205 | """ |
paul@787 | 206 | |
paul@787 | 207 | targets = set() |
paul@787 | 208 | |
paul@787 | 209 | if target_names: |
paul@787 | 210 | for target_name, is_static in target_names: |
paul@787 | 211 | targets.add(self.objtable.get_object(target_name)) |
paul@787 | 212 | |
paul@787 | 213 | return targets |
paul@787 | 214 | |
paul@765 | 215 | # Visitor methods. |
paul@765 | 216 | |
paul@646 | 217 | def _visitUnit(self, node): |
paul@646 | 218 | |
paul@646 | 219 | """ |
paul@646 | 220 | Track entry into program units in order to support various attribute |
paul@646 | 221 | access operations. |
paul@646 | 222 | """ |
paul@646 | 223 | |
paul@646 | 224 | self.units.append(node.unit) |
paul@646 | 225 | self.dispatch(node.node) |
paul@646 | 226 | self.units.pop() |
paul@646 | 227 | |
paul@786 | 228 | visitModule = _visitUnit |
paul@649 | 229 | |
paul@786 | 230 | def visitClass(self, node): |
paul@786 | 231 | |
paul@786 | 232 | "Optionally visit a class, depending on whether it is used." |
paul@649 | 233 | |
paul@649 | 234 | if not used_by_unit(node): |
paul@649 | 235 | return |
paul@649 | 236 | self._visitUnit(node) |
paul@649 | 237 | |
paul@786 | 238 | def visitFunction(self, node): |
paul@786 | 239 | |
paul@786 | 240 | "Optionally visit a function, depending on whether it is used." |
paul@786 | 241 | |
paul@786 | 242 | if not used_by_unit(node): |
paul@786 | 243 | return |
paul@786 | 244 | |
paul@786 | 245 | self.units.append(node.unit) |
paul@786 | 246 | self.dispatch(node.code) |
paul@786 | 247 | self.units.pop() |
paul@786 | 248 | |
paul@786 | 249 | node._guard_types = {} |
paul@786 | 250 | node._guards = {} |
paul@786 | 251 | |
paul@787 | 252 | self._visitParameters(node, node.unit.positional_names) |
paul@787 | 253 | |
paul@787 | 254 | def _visitParameters(self, node, parameters): |
paul@787 | 255 | for name in parameters: |
paul@787 | 256 | if isinstance(name, tuple): |
paul@787 | 257 | self._visitParameters(node, name) |
paul@787 | 258 | else: |
paul@787 | 259 | # NOTE: No _values usage here because it is mostly useless information, except perhaps for defaults. |
paul@787 | 260 | types = self.get_targets_from_type_names(node._attrtypes.get(name)) |
paul@787 | 261 | self._visitGuardForNameDeduced(node, name, types) |
paul@646 | 262 | |
paul@646 | 263 | def _visitAttr(self, node): |
paul@646 | 264 | |
paul@646 | 265 | """ |
paul@646 | 266 | Perform deductions on attribute accesses, adding annotations to the node |
paul@646 | 267 | that can be used by subsequent activities. |
paul@646 | 268 | """ |
paul@646 | 269 | |
paul@704 | 270 | # Remember to permit deductions on the expression node. Here, we may |
paul@704 | 271 | # also obtain a concrete type associated with an instantiation. |
paul@650 | 272 | |
paul@704 | 273 | expr_type = self.dispatch(node.expr) |
paul@704 | 274 | if not node._expr or isinstance(node._expr, Instance): |
paul@704 | 275 | node._expr = expr_type |
paul@650 | 276 | |
paul@788 | 277 | self._annotateAttr(node, node._expr, node.attrname) |
paul@788 | 278 | |
paul@788 | 279 | # Make a summary annotation of the deductions. |
paul@646 | 280 | |
paul@788 | 281 | attributes = node._value_deduced and [self.get_attribute_and_value(node._value_deduced)] or \ |
paul@788 | 282 | node._attr_deduced and [self.get_attribute_and_value(node._attr_deduced)] or \ |
paul@788 | 283 | node._attrs_deduced or \ |
paul@788 | 284 | map(self.get_attribute_and_value, node._attrs_deduced_from_specific_usage or []) |
paul@788 | 285 | |
paul@788 | 286 | node._access_attrs = attributes |
paul@764 | 287 | |
paul@797 | 288 | # Return the _attr annotation for instantiation detection. |
paul@797 | 289 | |
paul@797 | 290 | return node._attr |
paul@797 | 291 | |
paul@764 | 292 | def _annotateAttr(self, node, target, attrname): |
paul@764 | 293 | |
paul@764 | 294 | """ |
paul@764 | 295 | Annotate the access on the given 'node' using the 'target' and |
paul@764 | 296 | 'attrname' information. |
paul@764 | 297 | """ |
paul@764 | 298 | |
paul@764 | 299 | unit = self.get_unit() |
paul@764 | 300 | |
paul@788 | 301 | # The target, on which the access is performed, may influence the effect |
paul@788 | 302 | # on the context. We can only reliably assume that a literal constant is |
paul@788 | 303 | # an instance: all other "instances" may actually be classes in certain |
paul@788 | 304 | # cases. |
paul@788 | 305 | |
paul@710 | 306 | instance_target = isinstance(target, TypedInstance) |
paul@717 | 307 | typed_instance_attr = isinstance(target, BaseAttr) and isinstance(target.get_value(), TypedInstance) |
paul@764 | 308 | self_access = self.provides_self_access(target, unit) |
paul@646 | 309 | |
paul@646 | 310 | # Attempt to deduce attributes from explicit annotations. |
paul@646 | 311 | |
paul@764 | 312 | node._attrs_deduced = attrs = self.possible_attributes_from_annotation(target, node._attr, attrname) |
paul@646 | 313 | |
paul@646 | 314 | if len(attrs) == 1: |
paul@646 | 315 | for attr, value in attrs: |
paul@646 | 316 | |
paul@646 | 317 | # Constant values can be obtained directly. |
paul@646 | 318 | |
paul@646 | 319 | if self.provides_constant_result(value): |
paul@646 | 320 | node._access_type = "constant" |
paul@646 | 321 | node._value_deduced = value |
paul@646 | 322 | return |
paul@646 | 323 | |
paul@646 | 324 | # Static attributes can be obtained via their parent. |
paul@646 | 325 | |
paul@646 | 326 | if attr.is_static_attribute(): |
paul@646 | 327 | node._access_type = "static" |
paul@646 | 328 | node._attr_deduced = attr |
paul@646 | 329 | node._set_context = instance_target and "set" or None |
paul@646 | 330 | return |
paul@646 | 331 | |
paul@698 | 332 | # If a reliable target was originally specified, any usable attributes |
paul@698 | 333 | # should have been detected above, and any attributes deduced by other |
paul@698 | 334 | # means will not be compatible with the target. Thus, the nature of the |
paul@698 | 335 | # target is investigated: it must be an inspectable namespace or be an |
paul@698 | 336 | # attribute only providing such namespaces; otherwise, it is possible |
paul@698 | 337 | # that deduced attributes might be appropriate. |
paul@698 | 338 | |
paul@698 | 339 | if target and ( |
paul@698 | 340 | isinstance(target, (Class, Module)) or |
paul@717 | 341 | isinstance(target, BaseAttr) and not [v for v in target.get_values() if not isinstance(v, (Class, Module))] |
paul@698 | 342 | ): |
paul@698 | 343 | node._access_type = "impossible" |
paul@698 | 344 | return |
paul@698 | 345 | |
paul@710 | 346 | # Attributes of self, which is by definition an instance, or typed |
paul@710 | 347 | # instances, which act somewhat like self in that their properties |
paul@710 | 348 | # should be known. |
paul@710 | 349 | |
paul@710 | 350 | if instance_target or typed_instance_attr or self_access: |
paul@646 | 351 | |
paul@710 | 352 | if instance_target: |
paul@710 | 353 | value = target |
paul@710 | 354 | elif typed_instance_attr: |
paul@710 | 355 | value = target.get_value() |
paul@710 | 356 | |
paul@710 | 357 | # Find the class of the instance. |
paul@710 | 358 | |
paul@710 | 359 | if instance_target or typed_instance_attr: |
paul@710 | 360 | if isinstance(value, Const): |
paul@710 | 361 | cls = get_constant_class(value.get_class_name()) |
paul@710 | 362 | else: |
paul@710 | 363 | cls = value.cls |
paul@710 | 364 | else: |
paul@710 | 365 | cls = unit.parent |
paul@646 | 366 | |
paul@646 | 367 | # Find instance attributes. |
paul@646 | 368 | |
paul@764 | 369 | attr = cls.instance_attributes().get(attrname) |
paul@710 | 370 | |
paul@710 | 371 | # Where self is involved, descendants can also provide attributes. |
paul@710 | 372 | |
paul@764 | 373 | attrs = self_access and filter(None, [desc.instance_attributes().get(attrname) for desc in cls.descendants]) or [] |
paul@646 | 374 | |
paul@693 | 375 | # A "leaf" class whose instances provide an attribute. |
paul@693 | 376 | |
paul@693 | 377 | if attr and not attrs: |
paul@646 | 378 | node._access_type = "instance" |
paul@646 | 379 | node._attr_deduced = attr |
paul@646 | 380 | return |
paul@646 | 381 | |
paul@693 | 382 | # A class where instances of subclasses may provide an attribute. |
paul@693 | 383 | |
paul@722 | 384 | elif attrs and self_access: |
paul@693 | 385 | if attr: |
paul@693 | 386 | attrs.append(attr) |
paul@693 | 387 | |
paul@693 | 388 | node._attrs_deduced = [(a, a.get_value()) for a in attrs] |
paul@693 | 389 | |
paul@693 | 390 | # The instances may provide the attribute at the same position. |
paul@693 | 391 | |
paul@693 | 392 | positions = set([a.position for a in attrs]) |
paul@693 | 393 | if len(positions) == 1: |
paul@693 | 394 | for position in positions: |
paul@693 | 395 | node._access_type = "positioned" |
paul@693 | 396 | node._position_deduced = position |
paul@693 | 397 | return |
paul@693 | 398 | |
paul@693 | 399 | # Otherwise, accessing the attributes is more work. |
paul@693 | 400 | |
paul@693 | 401 | node._access_type = "instance" |
paul@693 | 402 | return |
paul@693 | 403 | |
paul@646 | 404 | # Find class attributes. |
paul@646 | 405 | # The context will be overridden for compatible class attributes |
paul@646 | 406 | # only. |
paul@646 | 407 | |
paul@764 | 408 | attr = cls.all_class_attributes().get(attrname) |
paul@646 | 409 | |
paul@646 | 410 | if attr: |
paul@646 | 411 | |
paul@646 | 412 | # Constant attributes. |
paul@646 | 413 | |
paul@646 | 414 | if attr.is_strict_constant(): |
paul@646 | 415 | if self.provides_constant_result(attr.get_value()): |
paul@646 | 416 | node._access_type = "constant" |
paul@646 | 417 | node._value_deduced = attr.get_value() |
paul@710 | 418 | node._set_context = "set" |
paul@646 | 419 | return |
paul@646 | 420 | |
paul@646 | 421 | # Compatible class attributes. |
paul@646 | 422 | |
paul@646 | 423 | if attr.defined_within_hierarchy(): |
paul@646 | 424 | node._access_type = "static" |
paul@646 | 425 | node._attr_deduced = attr |
paul@646 | 426 | node._set_context = "set" |
paul@646 | 427 | return |
paul@646 | 428 | |
paul@646 | 429 | # Incompatible class attributes. |
paul@646 | 430 | |
paul@646 | 431 | elif attr.defined_outside_hierarchy(): |
paul@646 | 432 | node._access_type = "static" |
paul@646 | 433 | node._attr_deduced = attr |
paul@646 | 434 | return |
paul@646 | 435 | |
paul@646 | 436 | # Unknown or mixed compatibility. |
paul@646 | 437 | |
paul@646 | 438 | node._access_type = "static" |
paul@646 | 439 | node._attr_deduced = attr |
paul@646 | 440 | node._set_context = "cond" |
paul@646 | 441 | return |
paul@646 | 442 | |
paul@646 | 443 | # Usage observations, both specific to this node's region of the program |
paul@646 | 444 | # and also applicable to the lifespan of the affected name. |
paul@646 | 445 | |
paul@646 | 446 | specific_targets = self.possible_accessors_from_usage(node, defining_users=0) |
paul@646 | 447 | targets = self.possible_accessors_from_usage(node, defining_users=1) |
paul@646 | 448 | |
paul@646 | 449 | # Record whether types were already deduced. If not, get types using |
paul@646 | 450 | # only this attribute. |
paul@646 | 451 | |
paul@646 | 452 | if not specific_targets or not targets: |
paul@764 | 453 | attribute_targets = self.possible_accessors_for_attribute(attrname) |
paul@646 | 454 | if not specific_targets: |
paul@646 | 455 | specific_targets = attribute_targets |
paul@646 | 456 | if not targets: |
paul@646 | 457 | targets = attribute_targets |
paul@646 | 458 | |
paul@698 | 459 | # Get the attributes from the deduced targets. |
paul@698 | 460 | |
paul@764 | 461 | node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, attrname) |
paul@764 | 462 | node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, attrname) |
paul@646 | 463 | |
paul@646 | 464 | # Generate optimisations where only a single attribute applies. |
paul@646 | 465 | |
paul@646 | 466 | if attrs and len(attrs) == 1: |
paul@646 | 467 | for attr in attrs: |
paul@646 | 468 | |
paul@646 | 469 | # Static attributes, but potentially non-static targets. |
paul@646 | 470 | |
paul@646 | 471 | if attr.is_static_attribute(): |
paul@646 | 472 | |
paul@646 | 473 | # Static attributes may be accompanied by a different context |
paul@646 | 474 | # depending on the accessor. |
paul@646 | 475 | # NOTE: Should determine whether the context is always replaced. |
paul@646 | 476 | |
paul@646 | 477 | node._access_type = "static" |
paul@646 | 478 | node._attr_deduced = attr |
paul@646 | 479 | node._set_context = instance_target and "set" or "cond" |
paul@646 | 480 | return |
paul@646 | 481 | |
paul@646 | 482 | # Non-static attributes. |
paul@646 | 483 | |
paul@646 | 484 | node._access_type = "instance" |
paul@646 | 485 | node._attr_deduced = attr |
paul@646 | 486 | return |
paul@646 | 487 | |
paul@646 | 488 | # Test for compatible attribute positioning. |
paul@646 | 489 | |
paul@646 | 490 | elif attrs: |
paul@646 | 491 | positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) |
paul@646 | 492 | |
paul@646 | 493 | # Permit a position-based access only on non-static attributes since |
paul@646 | 494 | # access to static attributes may happen via instances and thus not |
paul@646 | 495 | # be relative to the accessor but to its parent. |
paul@646 | 496 | |
paul@646 | 497 | if len(positions) == 1: |
paul@646 | 498 | for position in positions: |
paul@646 | 499 | if not position[0]: |
paul@646 | 500 | node._access_type = "positioned" |
paul@646 | 501 | node._position_deduced = position[0] |
paul@646 | 502 | return |
paul@646 | 503 | |
paul@646 | 504 | # With no usable deductions, generate a table-based access. |
paul@646 | 505 | |
paul@646 | 506 | node._access_type = "unknown" |
paul@646 | 507 | node._set_context = "cond" |
paul@646 | 508 | |
paul@646 | 509 | visitAssAttr = visitGetattr = _visitAttr |
paul@646 | 510 | |
paul@710 | 511 | def visitAssign(self, node): |
paul@710 | 512 | self.expr = self.dispatch(node.expr) |
paul@710 | 513 | for n in node.nodes: |
paul@710 | 514 | self.dispatch(n) |
paul@710 | 515 | |
paul@714 | 516 | def visitAssList(self, node): |
paul@714 | 517 | expr = self.expr |
paul@714 | 518 | self.expr = make_instance() |
paul@714 | 519 | for n in node.nodes: |
paul@714 | 520 | self.dispatch(n) |
paul@714 | 521 | self.expr = expr |
paul@714 | 522 | |
paul@714 | 523 | visitAssTuple = visitAssList |
paul@714 | 524 | |
paul@710 | 525 | def visitAssName(self, node): |
paul@710 | 526 | if self.expr: |
paul@717 | 527 | if isinstance(self.expr, BaseAttr): |
paul@710 | 528 | expr = self.expr.get_value() |
paul@710 | 529 | elif isinstance(self.expr, TypedInstance): |
paul@710 | 530 | expr = self.expr |
paul@710 | 531 | else: |
paul@786 | 532 | self._visitGuard(node) |
paul@710 | 533 | return |
paul@710 | 534 | else: |
paul@786 | 535 | self._visitGuard(node) |
paul@710 | 536 | return |
paul@710 | 537 | |
paul@717 | 538 | attr = node._values and node._values.get(node.name) or None |
paul@710 | 539 | |
paul@712 | 540 | # Need to replace any uncertain value with a concrete value. |
paul@710 | 541 | |
paul@717 | 542 | if attr: |
paul@717 | 543 | if isinstance(attr, BaseAttr): |
paul@717 | 544 | value = attr.get_value() |
paul@717 | 545 | else: |
paul@717 | 546 | value = attr |
paul@717 | 547 | |
paul@717 | 548 | if value and isinstance(value, Instance) and not isinstance(value, TypedInstance): |
paul@797 | 549 | node._values[node.name] = expr or make_instance() |
paul@710 | 550 | |
paul@786 | 551 | self._visitGuard(node) |
paul@786 | 552 | |
paul@786 | 553 | def _visitGuard(self, node): |
paul@786 | 554 | node._guard_types = {} |
paul@786 | 555 | node._guards = {} |
paul@786 | 556 | self._visitGuardForName(node, node.name) |
paul@786 | 557 | |
paul@786 | 558 | def _visitGuardForName(self, node, name): |
paul@786 | 559 | |
paul@786 | 560 | "Make an annotation stating the acceptable types for a name." |
paul@786 | 561 | |
paul@786 | 562 | # Need to check any concrete value against deductions. |
paul@797 | 563 | # Where no local observations exist, no guard can usefully be asserted. |
paul@797 | 564 | |
paul@797 | 565 | if not node._attrtypes and not node._values: |
paul@797 | 566 | return |
paul@786 | 567 | |
paul@787 | 568 | types = self.get_targets_from_type_names(node._attrtypes.get(name)) |
paul@786 | 569 | value = node._values.get(name) |
paul@786 | 570 | |
paul@800 | 571 | if isinstance(value, BaseAttr): |
paul@800 | 572 | values = value.get_values() |
paul@800 | 573 | else: |
paul@800 | 574 | values = [value] |
paul@800 | 575 | |
paul@800 | 576 | # Obtain values with identifiable types. |
paul@800 | 577 | |
paul@800 | 578 | typed_values = set() |
paul@800 | 579 | |
paul@800 | 580 | for v in values: |
paul@800 | 581 | if isinstance(v, Instance): |
paul@800 | 582 | if not isinstance(v, TypedInstance): |
paul@800 | 583 | return |
paul@800 | 584 | typed_values.add(v.cls) |
paul@800 | 585 | else: |
paul@800 | 586 | typed_values.add(v) |
paul@800 | 587 | |
paul@786 | 588 | # Where a concrete type conflicts with deductions, the usage of |
paul@786 | 589 | # attributes cannot be supported and is thus impossible. |
paul@786 | 590 | |
paul@800 | 591 | if typed_values: |
paul@800 | 592 | if types and not typed_values.intersection(types): |
paul@786 | 593 | node._guard_types[name] = "impossible" |
paul@786 | 594 | else: |
paul@800 | 595 | node._guard_types[name] = len(typed_values) == 1 and "single" or "multiple" |
paul@800 | 596 | node._guards[name] = typed_values |
paul@786 | 597 | |
paul@786 | 598 | # Where no concrete type is known, usage must be checked. |
paul@786 | 599 | |
paul@787 | 600 | elif types: |
paul@787 | 601 | self._visitGuardForNameDeduced(node, name, types) |
paul@786 | 602 | |
paul@786 | 603 | # Where no deductions are made, no guards are needed. |
paul@786 | 604 | |
paul@787 | 605 | def _visitGuardForNameDeduced(self, node, name, types): |
paul@787 | 606 | if not types: |
paul@786 | 607 | return |
paul@787 | 608 | if len(types) == 1: |
paul@786 | 609 | node._guard_types[name] = "single" |
paul@786 | 610 | else: |
paul@786 | 611 | node._guard_types[name] = "multiple" |
paul@787 | 612 | node._guards[name] = types |
paul@786 | 613 | |
paul@704 | 614 | def visitCallFunc(self, node): |
paul@704 | 615 | |
paul@704 | 616 | "Identify any concrete types involved with instantiation." |
paul@704 | 617 | |
paul@797 | 618 | for n in node.getChildNodes()[1:]: # ignore node.node |
paul@704 | 619 | self.dispatch(n) |
paul@704 | 620 | |
paul@704 | 621 | # Determine whether the target of the invocation refers to a class. |
paul@704 | 622 | |
paul@797 | 623 | attr = self.dispatch(node.node) |
paul@704 | 624 | |
paul@797 | 625 | if attr: |
paul@797 | 626 | if not isinstance(attr, (Instance, UnresolvedName)): |
paul@797 | 627 | value = attr.get_value() |
paul@797 | 628 | if value and isinstance(value, Class): |
paul@797 | 629 | return TypedInstance(value) |
paul@704 | 630 | |
paul@764 | 631 | def _visitOperator(self, node): |
paul@764 | 632 | |
paul@764 | 633 | "Annotate operators with function information." |
paul@764 | 634 | |
paul@764 | 635 | self._annotateAttr(node, node._module, node._attr.name) |
paul@764 | 636 | |
paul@764 | 637 | visitAdd = \ |
paul@764 | 638 | visitBitand = \ |
paul@764 | 639 | visitBitor = \ |
paul@764 | 640 | visitBitxor = \ |
paul@764 | 641 | visitDiv = \ |
paul@764 | 642 | visitFloorDiv = \ |
paul@764 | 643 | visitInvert = \ |
paul@764 | 644 | visitLeftShift = \ |
paul@764 | 645 | visitMod = \ |
paul@764 | 646 | visitMul = \ |
paul@764 | 647 | visitPower = \ |
paul@764 | 648 | visitRightShift = \ |
paul@764 | 649 | visitSub = \ |
paul@764 | 650 | visitUnaryAdd = \ |
paul@764 | 651 | visitUnarySub = _visitOperator |
paul@764 | 652 | |
paul@646 | 653 | # Convenience functions. |
paul@646 | 654 | |
paul@646 | 655 | def deduce(program): |
paul@646 | 656 | for module in program.get_importer().get_modules(): |
paul@646 | 657 | DeducedSource(module, program).deduce() |
paul@646 | 658 | |
paul@646 | 659 | # vim: tabstop=4 expandtab shiftwidth=4 |