1 #!/usr/bin/env python 2 3 """ 4 Reference abstractions. 5 6 Copyright (C) 2016 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 class Reference: 23 24 "A reference abstraction." 25 26 def __init__(self, kind, origin=None, name=None): 27 28 """ 29 Initialise a reference using 'kind' to indicate the kind of object, 30 'origin' to indicate the actual origin of a referenced object, and a 31 'name' indicating an alias for the object in the program structure. 32 """ 33 34 if isinstance(kind, Reference): 35 raise ValueError, (kind, origin) 36 self.kind = kind 37 self.origin = origin 38 self.name = name 39 40 def __repr__(self): 41 return "Reference(%r, %r, %r)" % (self.kind, self.origin, self.name) 42 43 def __str__(self): 44 45 """ 46 Serialise the reference as '<var>' or a description incorporating the 47 kind and origin. 48 """ 49 50 if self.kind == "<var>": 51 alias = self.name and ";%s" % self.name or "" 52 return "%s%s" % (self.kind, alias) 53 else: 54 alias = self.name and self.name != self.origin and ";%s" % self.name or "" 55 return "%s:%s%s" % (self.kind, self.origin, alias) 56 57 def __hash__(self): 58 59 "Hash instances using the kind and origin only." 60 61 return hash((self.kind, self.get_origin())) 62 63 def __cmp__(self, other): 64 65 "Compare with 'other' using the kind and origin only." 66 67 if isinstance(other, Reference): 68 return cmp((self.kind, self.get_origin()), (other.kind, other.get_origin())) 69 else: 70 return cmp(str(self), other) 71 72 def get_name(self): 73 74 "Return the name used for this reference." 75 76 return self.name 77 78 def get_origin(self): 79 80 "Return the origin of the reference." 81 82 return self.kind != "<var>" and self.origin or None 83 84 def get_kind(self): 85 86 "Return the kind of object referenced." 87 88 return self.kind 89 90 def has_kind(self, kinds): 91 92 """ 93 Return whether the reference describes an object from the given 'kinds', 94 where such kinds may be "<class>", "<function>", "<instance>", 95 "<module>" or "<var>". Unresolved references may also have kinds of 96 "<depends>" and "<invoke>". 97 """ 98 99 if not isinstance(kinds, (list, tuple)): 100 kinds = [kinds] 101 return self.get_kind() in kinds 102 103 def get_path(self): 104 105 "Return the attribute names comprising the path to the origin." 106 107 return self.get_origin().split(".") 108 109 def unresolved(self): 110 111 "Return whether this reference is unresolved." 112 113 return self.has_kind(["<depends>", "<invoke>", "<var>"]) 114 115 def static(self): 116 117 "Return this reference if it refers to a static object, None otherwise." 118 119 return self.has_kind(["<class>", "<function>", "<module>"]) and self or None 120 121 def final(self): 122 123 "Return a reference to either a static object or None." 124 125 static = self.static() 126 return static and static.origin or None 127 128 def instance_of(self, alias=None): 129 130 """ 131 Return a reference to an instance of the referenced class, indicating an 132 'alias' for the instance if specified. 133 """ 134 135 return self.has_kind("<class>") and Reference("<instance>", self.origin, alias) or None 136 137 def as_var(self): 138 139 """ 140 Return a variable version of this reference. Any origin information is 141 discarded since variable references are deliberately ambiguous. 142 """ 143 144 return Reference("<var>", None, self.name) 145 146 def alias(self, name): 147 148 "Alias this reference employing 'name'." 149 150 return Reference(self.get_kind(), self.get_origin(), name) 151 152 def unaliased(self): 153 154 "Return this reference without any alias." 155 156 return Reference(self.get_kind(), self.get_origin()) 157 158 def mutate(self, ref): 159 160 "Mutate this reference to have the same details as 'ref'." 161 162 self.kind = ref.kind 163 self.origin = ref.origin 164 self.name = ref.name 165 166 def parent(self): 167 168 "Return the parent of this reference's origin." 169 170 if not self.get_origin(): 171 return None 172 173 return self.get_origin().rsplit(".", 1)[0] 174 175 def name_parent(self): 176 177 "Return the parent of this reference's aliased name." 178 179 if not self.get_name(): 180 return None 181 182 return self.get_name().rsplit(".", 1)[0] 183 184 def ancestors(self): 185 186 """ 187 Return ancestors of this reference's origin in order of decreasing 188 depth. 189 """ 190 191 if not self.get_origin(): 192 return None 193 194 parts = self.get_origin().split(".") 195 ancestors = [] 196 197 for i in range(len(parts) - 1, 0, -1): 198 ancestors.append(".".join(parts[:i])) 199 200 return ancestors 201 202 def is_constant_alias(self): 203 204 "Return whether this reference is an alias for a constant." 205 206 name = self.get_name() 207 return name and name.rsplit(".")[-1].startswith("$c") 208 209 def get_types(self): 210 211 "Return class, instance-only and module types for this reference." 212 213 class_types = self.has_kind("<class>") and [self.get_origin()] or [] 214 instance_types = [] 215 module_types = self.has_kind("<module>") and [self.get_origin()] or [] 216 return class_types, instance_types, module_types 217 218 def decode_reference(s, name=None): 219 220 "Decode 's', making a reference." 221 222 if isinstance(s, Reference): 223 return s.alias(name) 224 225 # Null value. 226 227 elif not s: 228 return Reference("<var>", None, name) 229 230 # Kind and origin. 231 232 elif ":" in s: 233 kind, origin = s.split(":") 234 if ";" in origin: 235 origin, name = origin.split(";") 236 return Reference(kind, origin, name) 237 238 # Kind and name. 239 240 elif ";" in s: 241 kind, name = s.split(";") 242 return Reference(kind, None, name) 243 244 # Kind-only, origin is indicated name. 245 246 elif s[0] == "<": 247 return Reference(s, name, name) 248 249 # Module-only. 250 251 else: 252 return Reference("<module>", s, name) 253 254 255 256 # Type/reference collection functions. 257 258 def is_single_class_type(all_types): 259 260 """ 261 Return whether 'all_types' is a mixture of class and instance kinds for 262 a single class type. 263 """ 264 265 kinds = set() 266 types = set() 267 268 for type in all_types: 269 kinds.add(type.get_kind()) 270 types.add(type.get_origin()) 271 272 return len(types) == 1 and kinds == set(["<class>", "<instance>"]) 273 274 def combine_types(class_types, instance_types, module_types): 275 276 """ 277 Combine 'class_types', 'instance_types', 'module_types' into a single 278 list of references. 279 """ 280 281 all_types = [] 282 for kind, l in [("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types)]: 283 for t in l: 284 all_types.append(Reference(kind, t)) 285 return all_types 286 287 def separate_types(refs): 288 289 """ 290 Separate 'refs' into type-specific lists, returning a tuple containing 291 lists of class types, instance types, module types, function types and 292 unknown "var" types. 293 """ 294 295 class_types = [] 296 instance_types = [] 297 module_types = [] 298 function_types = [] 299 var_types = [] 300 301 for kind, l in [ 302 ("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types), 303 ("<function>", function_types), ("<var>", var_types) 304 ]: 305 306 for ref in refs: 307 if ref.get_kind() == kind: 308 l.append(ref.get_origin()) 309 310 return class_types, instance_types, module_types, function_types, var_types 311 312 # vim: tabstop=4 expandtab shiftwidth=4