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 import compiler.ast 26 27 # Source code classes. 28 29 class DeducedSource(ASTVisitor): 30 31 "A module upon which deductions of code behaviour are made." 32 33 def __init__(self, module, program): 34 self.visitor = self 35 self.module = module 36 self.program = program 37 self.objtable = program.get_object_table() 38 self.units = [] 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. 96 97 self.dispatch(node.expr) 98 99 # The target, on which the access is performed, may influence the effect 100 # on the context. We can only reliably assume that a literal constant is 101 # an instance: all other "instances" may actually be classes in certain 102 # cases. 103 104 target = node._expr 105 instance_target = isinstance(target, Const) 106 107 # Attempt to deduce attributes from explicit annotations. 108 109 node._attrs_deduced = attrs = self.possible_attributes_from_annotation(node) 110 111 if len(attrs) == 1: 112 for attr, value in attrs: 113 114 # Constant values can be obtained directly. 115 116 if self.provides_constant_result(value): 117 node._access_type = "constant" 118 node._value_deduced = value 119 return 120 121 # Static attributes can be obtained via their parent. 122 123 if attr.is_static_attribute(): 124 node._access_type = "static" 125 node._attr_deduced = attr 126 node._set_context = instance_target and "set" or None 127 return 128 129 # If a reliable target was originally specified, any usable attributes 130 # should have been detected above, and any attributes deduced by other 131 # means will not be compatible with the target. Thus, the nature of the 132 # target is investigated: it must be an inspectable namespace or be an 133 # attribute only providing such namespaces; otherwise, it is possible 134 # that deduced attributes might be appropriate. 135 136 if target and ( 137 isinstance(target, (Class, Module)) or 138 isinstance(target, Attr) and not [v for v in target.get_values() if not isinstance(v, (Class, Module))] 139 ): 140 node._access_type = "impossible" 141 return 142 143 # Attributes of self, which is by definition an instance. 144 145 if self.provides_self_access(node, unit): 146 147 # Find instance attributes. 148 149 attr = unit.parent.instance_attributes().get(node.attrname) 150 attrs = filter(None, [cls.instance_attributes().get(node.attrname) for cls in unit.parent.descendants]) 151 152 # A "leaf" class whose instances provide an attribute. 153 154 if attr and not attrs: 155 node._access_type = "instance" 156 node._attr_deduced = attr 157 return 158 159 # A class where instances of subclasses may provide an attribute. 160 161 elif attrs: 162 if attr: 163 attrs.append(attr) 164 165 node._attrs_deduced = [(a, a.get_value()) for a in attrs] 166 167 # The instances may provide the attribute at the same position. 168 169 positions = set([a.position for a in attrs]) 170 if len(positions) == 1: 171 for position in positions: 172 node._access_type = "positioned" 173 node._position_deduced = position 174 return 175 176 # Otherwise, accessing the attributes is more work. 177 178 node._access_type = "instance" 179 return 180 181 # Find class attributes. 182 # The context will be overridden for compatible class attributes 183 # only. 184 185 attr = unit.parent.get(node.attrname) 186 187 if attr: 188 189 # Constant attributes. 190 191 if attr.is_strict_constant(): 192 if self.provides_constant_result(attr.get_value()): 193 node._access_type = "constant" 194 node._value_deduced = attr.get_value() 195 return 196 197 # Compatible class attributes. 198 199 if attr.defined_within_hierarchy(): 200 node._access_type = "static" 201 node._attr_deduced = attr 202 node._set_context = "set" 203 return 204 205 # Incompatible class attributes. 206 207 elif attr.defined_outside_hierarchy(): 208 node._access_type = "static" 209 node._attr_deduced = attr 210 return 211 212 # Unknown or mixed compatibility. 213 214 node._access_type = "static" 215 node._attr_deduced = attr 216 node._set_context = "cond" 217 return 218 219 # Usage observations, both specific to this node's region of the program 220 # and also applicable to the lifespan of the affected name. 221 222 specific_targets = self.possible_accessors_from_usage(node, defining_users=0) 223 targets = self.possible_accessors_from_usage(node, defining_users=1) 224 225 # Record whether types were already deduced. If not, get types using 226 # only this attribute. 227 228 if not specific_targets or not targets: 229 attribute_targets = self.possible_accessors_for_attribute(node.attrname) 230 if not specific_targets: 231 specific_targets = attribute_targets 232 if not targets: 233 targets = attribute_targets 234 235 # Get the attributes from the deduced targets. 236 237 node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, node.attrname) 238 node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, node.attrname) 239 240 # Generate optimisations where only a single attribute applies. 241 242 if attrs and len(attrs) == 1: 243 for attr in attrs: 244 245 # Static attributes, but potentially non-static targets. 246 247 if attr.is_static_attribute(): 248 249 # Static attributes may be accompanied by a different context 250 # depending on the accessor. 251 # NOTE: Should determine whether the context is always replaced. 252 253 node._access_type = "static" 254 node._attr_deduced = attr 255 node._set_context = instance_target and "set" or "cond" 256 return 257 258 # Non-static attributes. 259 260 node._access_type = "instance" 261 node._attr_deduced = attr 262 return 263 264 # Test for compatible attribute positioning. 265 266 elif attrs: 267 positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) 268 269 # Permit a position-based access only on non-static attributes since 270 # access to static attributes may happen via instances and thus not 271 # be relative to the accessor but to its parent. 272 273 if len(positions) == 1: 274 for position in positions: 275 if not position[0]: 276 node._access_type = "positioned" 277 node._position_deduced = position[0] 278 return 279 280 # With no usable deductions, generate a table-based access. 281 282 node._access_type = "unknown" 283 node._set_context = "cond" 284 285 visitAssAttr = visitGetattr = _visitAttr 286 287 # Convenience functions. 288 289 def deduce(program): 290 for module in program.get_importer().get_modules(): 291 DeducedSource(module, program).deduce() 292 293 # vim: tabstop=4 expandtab shiftwidth=4