1 #!/usr/bin/env python 2 3 """ 4 Result abstractions. 5 6 Copyright (C) 2016, 2017 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 referencing import Reference 23 24 # Classes representing inspection and translation observations. 25 26 class Result: 27 28 "An abstract expression result." 29 30 def is_name(self): 31 return False 32 33 def is_global_name(self): 34 return False 35 36 def reference(self): 37 return None 38 39 def references(self): 40 return None 41 42 def access_location(self): 43 return None 44 45 def access_locations(self): 46 return None 47 48 def context_identity(self): 49 return None 50 51 def discards_temporary(self, test=True): 52 return None 53 54 def get_name(self): 55 return None 56 57 def get_origin(self): 58 return None 59 60 def static(self): 61 return None 62 63 def final(self): 64 return None 65 66 def has_kind(self, kinds): 67 return False 68 69 class AccessRef(Result): 70 71 """ 72 A reference to an attribute access that is generally only returned from a 73 processed access for possible initialiser resolution for assignments. 74 """ 75 76 def __init__(self, original_name, attrnames, number): 77 self.original_name = original_name 78 self.attrnames = attrnames 79 self.number = number 80 81 def __repr__(self): 82 return "AccessRef(%r, %r, %r)" % (self.original_name, self.attrnames, self.number) 83 84 class InvocationRef(Result): 85 86 "An invocation of a name reference." 87 88 def __init__(self, name_ref): 89 self.name_ref = name_ref 90 91 def reference(self): 92 origin = self.name_ref.get_origin() 93 if origin: 94 return Reference("<invoke>", origin) 95 else: 96 return Reference("<var>") 97 98 def __repr__(self): 99 return "InvocationRef(%r)" % self.name_ref 100 101 class NameRef(Result): 102 103 "A reference to a name." 104 105 def __init__(self, name, expr=None, is_global=False): 106 self.name = name 107 self.expr = expr 108 self.is_global = is_global 109 110 def is_name(self): 111 return True 112 113 def is_global_name(self): 114 return self.is_global 115 116 def final(self): 117 return None 118 119 def __repr__(self): 120 return "NameRef(%r, %r, %r)" % (self.name, self.expr, self.is_global) 121 122 class LocalNameRef(NameRef): 123 124 "A reference to a local name." 125 126 def __init__(self, name, number): 127 NameRef.__init__(self, name, is_global=False) 128 self.number = number 129 130 def __repr__(self): 131 return "LocalNameRef(%r, %r)" % (self.name, self.number) 132 133 class ResolvedRef: 134 135 "A resolved reference mix-in." 136 137 def __init__(self, ref): 138 self.ref = ref 139 140 def reference(self): 141 return self.ref 142 143 def references(self): 144 return [self.ref] 145 146 def get_name(self): 147 return self.ref and self.ref.get_name() or None 148 149 def get_origin(self): 150 return self.ref and self.ref.get_origin() or None 151 152 def static(self): 153 return self.ref and self.ref.static() or None 154 155 def final(self): 156 return self.ref and self.ref.final() or None 157 158 def has_kind(self, kinds): 159 return self.ref and self.ref.has_kind(kinds) 160 161 def is_constant_alias(self): 162 return self.ref and self.ref.is_constant_alias() 163 164 class ResolvedNameRef(ResolvedRef, NameRef): 165 166 "A resolved name-based reference." 167 168 def __init__(self, name, ref, expr=None, is_global=False): 169 NameRef.__init__(self, name, expr, is_global) 170 ResolvedRef.__init__(self, ref) 171 172 def __repr__(self): 173 return "ResolvedNameRef(%r, %r, %r, %r)" % (self.name, self.ref, self.expr, self.is_global) 174 175 class ConstantValueRef(ResolvedNameRef): 176 177 "A constant reference representing a single literal value." 178 179 def __init__(self, name, ref, value, number=None): 180 ResolvedNameRef.__init__(self, name, ref) 181 self.value = value 182 self.number = number 183 184 def __repr__(self): 185 return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number) 186 187 class InstanceRef(ResolvedRef, Result): 188 189 "An instance reference." 190 191 def reference(self): 192 return self.ref 193 194 def __repr__(self): 195 return "InstanceRef(%r)" % self.ref 196 197 class LiteralSequenceRef(ResolvedNameRef): 198 199 "A reference representing a sequence of values." 200 201 def __init__(self, name, ref, node, items=None): 202 ResolvedNameRef.__init__(self, name, ref) 203 self.node = node 204 self.items = items 205 206 def __repr__(self): 207 return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items) 208 209 class VariableRef(Result): 210 211 "A variable reference." 212 213 def __repr__(self): 214 return "VariableRef()" 215 216 # vim: tabstop=4 expandtab shiftwidth=4