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