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 # Attributes of self, which is by definition an instance. 130 131 if self.provides_self_access(node, unit): 132 133 # Find instance attributes. 134 135 attr = unit.parent.instance_attributes().get(node.attrname) 136 attrs = filter(None, [cls.instance_attributes().get(node.attrname) for cls in unit.parent.descendants]) 137 138 # A "leaf" class whose instances provide an attribute. 139 140 if attr and not attrs: 141 node._access_type = "instance" 142 node._attr_deduced = attr 143 return 144 145 # A class where instances of subclasses may provide an attribute. 146 147 elif attrs: 148 if attr: 149 attrs.append(attr) 150 151 node._attrs_deduced = [(a, a.get_value()) for a in attrs] 152 153 # The instances may provide the attribute at the same position. 154 155 positions = set([a.position for a in attrs]) 156 if len(positions) == 1: 157 for position in positions: 158 node._access_type = "positioned" 159 node._position_deduced = position 160 return 161 162 # Otherwise, accessing the attributes is more work. 163 164 node._access_type = "instance" 165 return 166 167 # Find class attributes. 168 # The context will be overridden for compatible class attributes 169 # only. 170 171 attr = unit.parent.get(node.attrname) 172 173 if attr: 174 175 # Constant attributes. 176 177 if attr.is_strict_constant(): 178 if self.provides_constant_result(attr.get_value()): 179 node._access_type = "constant" 180 node._value_deduced = attr.get_value() 181 return 182 183 # Compatible class attributes. 184 185 if attr.defined_within_hierarchy(): 186 node._access_type = "static" 187 node._attr_deduced = attr 188 node._set_context = "set" 189 return 190 191 # Incompatible class attributes. 192 193 elif attr.defined_outside_hierarchy(): 194 node._access_type = "static" 195 node._attr_deduced = attr 196 return 197 198 # Unknown or mixed compatibility. 199 200 node._access_type = "static" 201 node._attr_deduced = attr 202 node._set_context = "cond" 203 return 204 205 # Usage observations, both specific to this node's region of the program 206 # and also applicable to the lifespan of the affected name. 207 208 specific_targets = self.possible_accessors_from_usage(node, defining_users=0) 209 targets = self.possible_accessors_from_usage(node, defining_users=1) 210 211 # Record whether types were already deduced. If not, get types using 212 # only this attribute. 213 214 if not specific_targets or not targets: 215 attribute_targets = self.possible_accessors_for_attribute(node.attrname) 216 if not specific_targets: 217 specific_targets = attribute_targets 218 if not targets: 219 targets = attribute_targets 220 221 node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, node.attrname) 222 node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, node.attrname) 223 224 # Generate optimisations where only a single attribute applies. 225 226 if attrs and len(attrs) == 1: 227 for attr in attrs: 228 229 # Static attributes, but potentially non-static targets. 230 231 if attr.is_static_attribute(): 232 233 # Static attributes may be accompanied by a different context 234 # depending on the accessor. 235 # NOTE: Should determine whether the context is always replaced. 236 237 node._access_type = "static" 238 node._attr_deduced = attr 239 node._set_context = instance_target and "set" or "cond" 240 return 241 242 # Non-static attributes. 243 244 node._access_type = "instance" 245 node._attr_deduced = attr 246 return 247 248 # Test for compatible attribute positioning. 249 250 elif attrs: 251 positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) 252 253 # Permit a position-based access only on non-static attributes since 254 # access to static attributes may happen via instances and thus not 255 # be relative to the accessor but to its parent. 256 257 if len(positions) == 1: 258 for position in positions: 259 if not position[0]: 260 node._access_type = "positioned" 261 node._position_deduced = position[0] 262 return 263 264 # With no usable deductions, generate a table-based access. 265 266 node._access_type = "unknown" 267 node._set_context = "cond" 268 269 visitAssAttr = visitGetattr = _visitAttr 270 271 # Convenience functions. 272 273 def deduce(program): 274 for module in program.get_importer().get_modules(): 275 DeducedSource(module, program).deduce() 276 277 # vim: tabstop=4 expandtab shiftwidth=4