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