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 # The target, on which the access is performed, may influence the effect 96 # on the context. We can only reliably assume that a literal constant is 97 # an instance: all other "instances" may actually be classes in certain 98 # cases. 99 100 target = node._expr 101 instance_target = isinstance(target, Const) 102 103 # Attempt to deduce attributes from explicit annotations. 104 105 node._attrs_deduced = attrs = self.possible_attributes_from_annotation(node) 106 107 if len(attrs) == 1: 108 for attr, value in attrs: 109 110 # Constant values can be obtained directly. 111 112 if self.provides_constant_result(value): 113 node._access_type = "constant" 114 node._value_deduced = value 115 return 116 117 # Static attributes can be obtained via their parent. 118 119 if attr.is_static_attribute(): 120 node._access_type = "static" 121 node._attr_deduced = attr 122 node._set_context = instance_target and "set" or None 123 return 124 125 # Attributes of self, which is by definition an instance. 126 127 if self.provides_self_access(node, unit): 128 129 # Find instance attributes. 130 131 attr = unit.parent.instance_attributes().get(node.attrname) 132 133 if attr: 134 node._access_type = "instance" 135 node._attr_deduced = attr 136 return 137 138 # Find class attributes. 139 # The context will be overridden for compatible class attributes 140 # only. 141 142 attr = unit.parent.get(node.attrname) 143 144 if attr: 145 146 # Constant attributes. 147 148 if attr.is_strict_constant(): 149 if self.provides_constant_result(attr.get_value()): 150 node._access_type = "constant" 151 node._value_deduced = attr.get_value() 152 return 153 154 # Compatible class attributes. 155 156 if attr.defined_within_hierarchy(): 157 node._access_type = "static" 158 node._attr_deduced = attr 159 node._set_context = "set" 160 return 161 162 # Incompatible class attributes. 163 164 elif attr.defined_outside_hierarchy(): 165 node._access_type = "static" 166 node._attr_deduced = attr 167 return 168 169 # Unknown or mixed compatibility. 170 171 node._access_type = "static" 172 node._attr_deduced = attr 173 node._set_context = "cond" 174 return 175 176 # Usage observations, both specific to this node's region of the program 177 # and also applicable to the lifespan of the affected name. 178 179 specific_targets = self.possible_accessors_from_usage(node, defining_users=0) 180 targets = self.possible_accessors_from_usage(node, defining_users=1) 181 182 # Record whether types were already deduced. If not, get types using 183 # only this attribute. 184 185 if not specific_targets or not targets: 186 attribute_targets = self.possible_accessors_for_attribute(node.attrname) 187 if not specific_targets: 188 specific_targets = attribute_targets 189 if not targets: 190 targets = attribute_targets 191 192 node._attrs_deduced_from_specific_usage = self.get_attributes(specific_targets, node.attrname) 193 node._attrs_deduced_from_usage = attrs = self.get_attributes(targets, node.attrname) 194 195 # Generate optimisations where only a single attribute applies. 196 197 if attrs and len(attrs) == 1: 198 for attr in attrs: 199 200 # Static attributes, but potentially non-static targets. 201 202 if attr.is_static_attribute(): 203 204 # Static attributes may be accompanied by a different context 205 # depending on the accessor. 206 # NOTE: Should determine whether the context is always replaced. 207 208 node._access_type = "static" 209 node._attr_deduced = attr 210 node._set_context = instance_target and "set" or "cond" 211 return 212 213 # Non-static attributes. 214 215 node._access_type = "instance" 216 node._attr_deduced = attr 217 return 218 219 # Test for compatible attribute positioning. 220 221 elif attrs: 222 positions = set([(attr.is_static_attribute(), attr.position) for attr in attrs]) 223 224 # Permit a position-based access only on non-static attributes since 225 # access to static attributes may happen via instances and thus not 226 # be relative to the accessor but to its parent. 227 228 if len(positions) == 1: 229 for position in positions: 230 if not position[0]: 231 node._access_type = "positioned" 232 node._position_deduced = position[0] 233 return 234 235 # With no usable deductions, generate a table-based access. 236 237 node._access_type = "unknown" 238 node._set_context = "cond" 239 240 visitAssAttr = visitGetattr = _visitAttr 241 242 # Convenience functions. 243 244 def deduce(program): 245 for module in program.get_importer().get_modules(): 246 DeducedSource(module, program).deduce() 247 248 # vim: tabstop=4 expandtab shiftwidth=4