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@765 | 22 | from micropython.stdcompiler import compiler |
paul@765 | 23 | from compiler.ast import AssAttr, Getattr, Name |
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@765 | 69 | #if isinstance(node, self.implemented_nodes): |
paul@765 | 70 | # raise |
paul@646 | 71 | return self.visitor.default(node, *args) |
paul@646 | 72 | |
paul@765 | 73 | #implemented_nodes = ( |
paul@765 | 74 | # AssAttr, Assign, AssName, AssList, AssTuple, CallFunc, Getattr, |
paul@765 | 75 | # Add, Bitand, Bitor, Bitxor, Div, FloorDiv, Invert, LeftShift, Mod, Mul, |
paul@765 | 76 | # Power, RightShift, Sub, UnaryAdd, UnarySub |
paul@765 | 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@649 | 217 | def _visitOptionalUnit(self, node): |
paul@649 | 218 | |
paul@649 | 219 | "Optionally visit a unit, depending on whether it is used." |
paul@649 | 220 | |
paul@649 | 221 | if not used_by_unit(node): |
paul@649 | 222 | return |
paul@649 | 223 | self._visitUnit(node) |
paul@649 | 224 | |
paul@649 | 225 | visitModule = _visitUnit |
paul@649 | 226 | visitClass = visitFunction = _visitOptionalUnit |
paul@646 | 227 | |
paul@646 | 228 | def _visitAttr(self, node): |
paul@646 | 229 | |
paul@646 | 230 | """ |
paul@646 | 231 | Perform deductions on attribute accesses, adding annotations to the node |
paul@646 | 232 | that can be used by subsequent activities. |
paul@646 | 233 | """ |
paul@646 | 234 | |
paul@704 | 235 | # Remember to permit deductions on the expression node. Here, we may |
paul@704 | 236 | # also obtain a concrete type associated with an instantiation. |
paul@650 | 237 | |
paul@704 | 238 | expr_type = self.dispatch(node.expr) |
paul@704 | 239 | if not node._expr or isinstance(node._expr, Instance): |
paul@704 | 240 | node._expr = expr_type |
paul@650 | 241 | |
paul@646 | 242 | # The target, on which the access is performed, may influence the effect |
paul@646 | 243 | # on the context. We can only reliably assume that a literal constant is |
paul@646 | 244 | # an instance: all other "instances" may actually be classes in certain |
paul@646 | 245 | # cases. |
paul@646 | 246 | |
paul@764 | 247 | self._annotateAttr(node, node._expr, node.attrname) |
paul@764 | 248 | |
paul@764 | 249 | def _annotateAttr(self, node, target, attrname): |
paul@764 | 250 | |
paul@764 | 251 | """ |
paul@764 | 252 | Annotate the access on the given 'node' using the 'target' and |
paul@764 | 253 | 'attrname' information. |
paul@764 | 254 | """ |
paul@764 | 255 | |
paul@764 | 256 | unit = self.get_unit() |
paul@764 | 257 | |
paul@710 | 258 | instance_target = isinstance(target, TypedInstance) |
paul@717 | 259 | typed_instance_attr = isinstance(target, BaseAttr) and isinstance(target.get_value(), TypedInstance) |
paul@764 | 260 | self_access = self.provides_self_access(target, unit) |
paul@646 | 261 | |
paul@646 | 262 | # Attempt to deduce attributes from explicit annotations. |
paul@646 | 263 | |
paul@764 | 264 | node._attrs_deduced = attrs = self.possible_attributes_from_annotation(target, node._attr, attrname) |
paul@646 | 265 | |
paul@646 | 266 | if len(attrs) == 1: |
paul@646 | 267 | for attr, value in attrs: |
paul@646 | 268 | |
paul@646 | 269 | # Constant values can be obtained directly. |
paul@646 | 270 | |
paul@646 | 271 | if self.provides_constant_result(value): |
paul@646 | 272 | node._access_type = "constant" |
paul@646 | 273 | node._value_deduced = value |
paul@646 | 274 | return |
paul@646 | 275 | |
paul@646 | 276 | # Static attributes can be obtained via their parent. |
paul@646 | 277 | |
paul@646 | 278 | if attr.is_static_attribute(): |
paul@646 | 279 | node._access_type = "static" |
paul@646 | 280 | node._attr_deduced = attr |
paul@646 | 281 | node._set_context = instance_target and "set" or None |
paul@646 | 282 | return |
paul@646 | 283 | |
paul@698 | 284 | # If a reliable target was originally specified, any usable attributes |
paul@698 | 285 | # should have been detected above, and any attributes deduced by other |
paul@698 | 286 | # means will not be compatible with the target. Thus, the nature of the |
paul@698 | 287 | # target is investigated: it must be an inspectable namespace or be an |
paul@698 | 288 | # attribute only providing such namespaces; otherwise, it is possible |
paul@698 | 289 | # that deduced attributes might be appropriate. |
paul@698 | 290 | |
paul@698 | 291 | if target and ( |
paul@698 | 292 | isinstance(target, (Class, Module)) or |
paul@717 | 293 | isinstance(target, BaseAttr) and not [v for v in target.get_values() if not isinstance(v, (Class, Module))] |
paul@698 | 294 | ): |
paul@698 | 295 | node._access_type = "impossible" |
paul@698 | 296 | return |
paul@698 | 297 | |
paul@710 | 298 | # Attributes of self, which is by definition an instance, or typed |
paul@710 | 299 | # instances, which act somewhat like self in that their properties |
paul@710 | 300 | # should be known. |
paul@710 | 301 | |
paul@710 | 302 | if instance_target or typed_instance_attr or self_access: |
paul@646 | 303 | |
paul@710 | 304 | if instance_target: |
paul@710 | 305 | value = target |
paul@710 | 306 | elif typed_instance_attr: |
paul@710 | 307 | value = target.get_value() |
paul@710 | 308 | |
paul@710 | 309 | # Find the class of the instance. |
paul@710 | 310 | |
paul@710 | 311 | if instance_target or typed_instance_attr: |
paul@710 | 312 | if isinstance(value, Const): |
paul@710 | 313 | cls = get_constant_class(value.get_class_name()) |
paul@710 | 314 | else: |
paul@710 | 315 | cls = value.cls |
paul@710 | 316 | else: |
paul@710 | 317 | cls = unit.parent |
paul@646 | 318 | |
paul@646 | 319 | # Find instance attributes. |
paul@646 | 320 | |
paul@764 | 321 | attr = cls.instance_attributes().get(attrname) |
paul@710 | 322 | |
paul@710 | 323 | # Where self is involved, descendants can also provide attributes. |
paul@710 | 324 | |
paul@764 | 325 | attrs = self_access and filter(None, [desc.instance_attributes().get(attrname) for desc in cls.descendants]) or [] |
paul@646 | 326 | |
paul@693 | 327 | # A "leaf" class whose instances provide an attribute. |
paul@693 | 328 | |
paul@693 | 329 | if attr and not attrs: |
paul@646 | 330 | node._access_type = "instance" |
paul@646 | 331 | node._attr_deduced = attr |
paul@646 | 332 | return |
paul@646 | 333 | |
paul@693 | 334 | # A class where instances of subclasses may provide an attribute. |
paul@693 | 335 | |
paul@722 | 336 | elif attrs and self_access: |
paul@693 | 337 | if attr: |
paul@693 | 338 | attrs.append(attr) |
paul@693 | 339 | |
paul@693 | 340 | node._attrs_deduced = [(a, a.get_value()) for a in attrs] |
paul@693 | 341 | |
paul@693 | 342 | # The instances may provide the attribute at the same position. |
paul@693 | 343 | |
paul@693 | 344 | positions = set([a.position for a in attrs]) |
paul@693 | 345 | if len(positions) == 1: |
paul@693 | 346 | for position in positions: |
paul@693 | 347 | node._access_type = "positioned" |
paul@693 | 348 | node._position_deduced = position |
paul@693 | 349 | return |
paul@693 | 350 | |
paul@693 | 351 | # Otherwise, accessing the attributes is more work. |
paul@693 | 352 | |
paul@693 | 353 | node._access_type = "instance" |
paul@693 | 354 | return |
paul@693 | 355 | |
paul@646 | 356 | # Find class attributes. |
paul@646 | 357 | # The context will be overridden for compatible class attributes |
paul@646 | 358 | # only. |
paul@646 | 359 | |
paul@764 | 360 | attr = cls.all_class_attributes().get(attrname) |
paul@646 | 361 | |
paul@646 | 362 | if attr: |
paul@646 | 363 | |
paul@646 | 364 | # Constant attributes. |
paul@646 | 365 | |
paul@646 | 366 | if attr.is_strict_constant(): |
paul@646 | 367 | if self.provides_constant_result(attr.get_value()): |
paul@646 | 368 | node._access_type = "constant" |
paul@646 | 369 | node._value_deduced = attr.get_value() |
paul@710 | 370 | node._set_context = "set" |
paul@646 | 371 | return |
paul@646 | 372 | |
paul@646 | 373 | # Compatible class attributes. |
paul@646 | 374 | |
paul@646 | 375 | if attr.defined_within_hierarchy(): |
paul@646 | 376 | node._access_type = "static" |
paul@646 | 377 | node._attr_deduced = attr |
paul@646 | 378 | node._set_context = "set" |
paul@646 | 379 | return |
paul@646 | 380 | |
paul@646 | 381 | # Incompatible class attributes. |
paul@646 | 382 | |
paul@646 | 383 | elif attr.defined_outside_hierarchy(): |
paul@646 | 384 | node._access_type = "static" |
paul@646 | 385 | node._attr_deduced = attr |
paul@646 | 386 | return |
paul@646 | 387 | |
paul@646 | 388 | # Unknown or mixed compatibility. |
paul@646 | 389 | |
paul@646 | 390 | node._access_type = "static" |
paul@646 | 391 | node._attr_deduced = attr |
paul@646 | 392 | node._set_context = "cond" |
paul@646 | 393 | return |
paul@646 | 394 | |
paul@646 | 395 | # Usage observations, both specific to this node's region of the program |
paul@646 | 396 | # and also applicable to the lifespan of the affected name. |
paul@646 | 397 | |
paul@646 | 398 | specific_targets = self.possible_accessors_from_usage(node, defining_users=0) |
paul@646 | 399 | targets = self.possible_accessors_from_usage(node, defining_users=1) |
paul@646 | 400 | |
paul@646 | 401 | # Record whether types were already deduced. If not, get types using |
paul@646 | 402 | # only this attribute. |
paul@646 | 403 | |
paul@646 | 404 | if not specific_targets or not targets: |
paul@764 | 405 | attribute_targets = self.possible_accessors_for_attribute(attrname) |
paul@646 | 406 | if not specific_targets: |
paul@646 | 407 | specific_targets = attribute_targets |
paul@646 | 408 | if not targets: |
paul@646 | 409 | targets = attribute_targets |
paul@646 | 410 | |
paul@698 | 411 | # Get the attributes from the deduced targets. |
paul@698 | 412 | |
paul@764 | 413 | node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, attrname) |
paul@764 | 414 | node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, attrname) |
paul@646 | 415 | |
paul@646 | 416 | # Generate optimisations where only a single attribute applies. |
paul@646 | 417 | |
paul@646 | 418 | if attrs and len(attrs) == 1: |
paul@646 | 419 | for attr in attrs: |
paul@646 | 420 | |
paul@646 | 421 | # Static attributes, but potentially non-static targets. |
paul@646 | 422 | |
paul@646 | 423 | if attr.is_static_attribute(): |
paul@646 | 424 | |
paul@646 | 425 | # Static attributes may be accompanied by a different context |
paul@646 | 426 | # depending on the accessor. |
paul@646 | 427 | # NOTE: Should determine whether the context is always replaced. |
paul@646 | 428 | |
paul@646 | 429 | node._access_type = "static" |
paul@646 | 430 | node._attr_deduced = attr |
paul@646 | 431 | node._set_context = instance_target and "set" or "cond" |
paul@646 | 432 | return |
paul@646 | 433 | |
paul@646 | 434 | # Non-static attributes. |
paul@646 | 435 | |
paul@646 | 436 | node._access_type = "instance" |
paul@646 | 437 | node._attr_deduced = attr |
paul@646 | 438 | return |
paul@646 | 439 | |
paul@646 | 440 | # Test for compatible attribute positioning. |
paul@646 | 441 | |
paul@646 | 442 | elif attrs: |
paul@646 | 443 | positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) |
paul@646 | 444 | |
paul@646 | 445 | # Permit a position-based access only on non-static attributes since |
paul@646 | 446 | # access to static attributes may happen via instances and thus not |
paul@646 | 447 | # be relative to the accessor but to its parent. |
paul@646 | 448 | |
paul@646 | 449 | if len(positions) == 1: |
paul@646 | 450 | for position in positions: |
paul@646 | 451 | if not position[0]: |
paul@646 | 452 | node._access_type = "positioned" |
paul@646 | 453 | node._position_deduced = position[0] |
paul@646 | 454 | return |
paul@646 | 455 | |
paul@646 | 456 | # With no usable deductions, generate a table-based access. |
paul@646 | 457 | |
paul@646 | 458 | node._access_type = "unknown" |
paul@646 | 459 | node._set_context = "cond" |
paul@646 | 460 | |
paul@646 | 461 | visitAssAttr = visitGetattr = _visitAttr |
paul@646 | 462 | |
paul@710 | 463 | def visitAssign(self, node): |
paul@710 | 464 | self.expr = self.dispatch(node.expr) |
paul@710 | 465 | for n in node.nodes: |
paul@710 | 466 | self.dispatch(n) |
paul@710 | 467 | |
paul@714 | 468 | def visitAssList(self, node): |
paul@714 | 469 | expr = self.expr |
paul@714 | 470 | self.expr = make_instance() |
paul@714 | 471 | for n in node.nodes: |
paul@714 | 472 | self.dispatch(n) |
paul@714 | 473 | self.expr = expr |
paul@714 | 474 | |
paul@714 | 475 | visitAssTuple = visitAssList |
paul@714 | 476 | |
paul@710 | 477 | def visitAssName(self, node): |
paul@710 | 478 | if self.expr: |
paul@717 | 479 | if isinstance(self.expr, BaseAttr): |
paul@710 | 480 | expr = self.expr.get_value() |
paul@710 | 481 | elif isinstance(self.expr, TypedInstance): |
paul@710 | 482 | expr = self.expr |
paul@710 | 483 | else: |
paul@710 | 484 | return |
paul@710 | 485 | else: |
paul@710 | 486 | return |
paul@710 | 487 | |
paul@717 | 488 | attr = node._values and node._values.get(node.name) or None |
paul@710 | 489 | |
paul@712 | 490 | # Need to replace any uncertain value with a concrete value. |
paul@710 | 491 | |
paul@717 | 492 | if attr: |
paul@717 | 493 | if isinstance(attr, BaseAttr): |
paul@717 | 494 | value = attr.get_value() |
paul@717 | 495 | else: |
paul@717 | 496 | value = attr |
paul@717 | 497 | |
paul@717 | 498 | if value and isinstance(value, Instance) and not isinstance(value, TypedInstance): |
paul@783 | 499 | node._values[node.name] = expr |
paul@710 | 500 | |
paul@704 | 501 | def visitCallFunc(self, node): |
paul@704 | 502 | |
paul@704 | 503 | "Identify any concrete types involved with instantiation." |
paul@704 | 504 | |
paul@704 | 505 | for n in node.getChildNodes(): |
paul@704 | 506 | self.dispatch(n) |
paul@704 | 507 | |
paul@704 | 508 | # Determine whether the target of the invocation refers to a class. |
paul@704 | 509 | |
paul@704 | 510 | attr = node.node._attr |
paul@704 | 511 | |
paul@704 | 512 | if attr: |
paul@704 | 513 | value = attr.get_value() |
paul@704 | 514 | if value and isinstance(value, Class): |
paul@710 | 515 | return TypedInstance(value) |
paul@704 | 516 | |
paul@764 | 517 | def _visitOperator(self, node): |
paul@764 | 518 | |
paul@764 | 519 | "Annotate operators with function information." |
paul@764 | 520 | |
paul@764 | 521 | self._annotateAttr(node, node._module, node._attr.name) |
paul@764 | 522 | |
paul@764 | 523 | visitAdd = \ |
paul@764 | 524 | visitBitand = \ |
paul@764 | 525 | visitBitor = \ |
paul@764 | 526 | visitBitxor = \ |
paul@764 | 527 | visitDiv = \ |
paul@764 | 528 | visitFloorDiv = \ |
paul@764 | 529 | visitInvert = \ |
paul@764 | 530 | visitLeftShift = \ |
paul@764 | 531 | visitMod = \ |
paul@764 | 532 | visitMul = \ |
paul@764 | 533 | visitPower = \ |
paul@764 | 534 | visitRightShift = \ |
paul@764 | 535 | visitSub = \ |
paul@764 | 536 | visitUnaryAdd = \ |
paul@764 | 537 | visitUnarySub = _visitOperator |
paul@764 | 538 | |
paul@646 | 539 | # Convenience functions. |
paul@646 | 540 | |
paul@646 | 541 | def deduce(program): |
paul@646 | 542 | for module in program.get_importer().get_modules(): |
paul@646 | 543 | DeducedSource(module, program).deduce() |
paul@646 | 544 | |
paul@646 | 545 | # vim: tabstop=4 expandtab shiftwidth=4 |