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 copy(self): 147 148 "Copy this reference." 149 150 return Reference(self.get_kind(), self.get_origin(), self.get_name()) 151 152 def alias(self, name): 153 154 "Alias this reference employing 'name'." 155 156 return Reference(self.get_kind(), self.get_origin(), name) 157 158 def unaliased(self): 159 160 "Return this reference without any alias." 161 162 return Reference(self.get_kind(), self.get_origin()) 163 164 def mutate(self, ref): 165 166 "Mutate this reference to have the same details as 'ref'." 167 168 self.kind = ref.kind 169 self.origin = ref.origin 170 self.name = ref.name 171 172 def parent(self): 173 174 "Return the parent of this reference's origin." 175 176 if not self.get_origin(): 177 return None 178 179 return self.get_origin().rsplit(".", 1)[0] 180 181 def name_parent(self): 182 183 "Return the parent of this reference's aliased name." 184 185 if not self.get_name(): 186 return None 187 188 return self.get_name().rsplit(".", 1)[0] 189 190 def ancestors(self): 191 192 """ 193 Return ancestors of this reference's origin in order of decreasing 194 depth. 195 """ 196 197 origin = self.get_origin() 198 if not origin: 199 return None 200 201 parts = origin.split(".") 202 ancestors = [] 203 204 for i in range(len(parts) - 1, 0, -1): 205 ancestors.append(".".join(parts[:i])) 206 207 return ancestors 208 209 def is_constant_alias(self): 210 211 "Return whether this reference is an alias for a constant." 212 213 name = self.get_name() 214 return name and name.rsplit(".")[-1].startswith("$c") 215 216 def get_types(self): 217 218 "Return class, instance-only and module types for this reference." 219 220 class_types = self.has_kind("<class>") and [self.get_origin()] or [] 221 instance_types = [] 222 module_types = self.has_kind("<module>") and [self.get_origin()] or [] 223 return class_types, instance_types, module_types 224 225 def decode_reference(s, name=None): 226 227 "Decode 's', making a reference." 228 229 if isinstance(s, Reference): 230 return s.alias(name) 231 232 # Null value. 233 234 elif not s: 235 return Reference("<var>", None, name) 236 237 # Kind and origin. 238 239 elif ":" in s: 240 kind, origin = s.split(":") 241 if ";" in origin: 242 origin, name = origin.split(";") 243 return Reference(kind, origin, name) 244 245 # Kind and name. 246 247 elif ";" in s: 248 kind, name = s.split(";") 249 return Reference(kind, None, name) 250 251 # Kind-only, origin is indicated name. 252 253 elif s[0] == "<": 254 return Reference(s, name, name) 255 256 # Module-only. 257 258 else: 259 return Reference("<module>", s, name) 260 261 262 263 # Type/reference collection functions. 264 265 def is_single_class_type(all_types): 266 267 """ 268 Return whether 'all_types' is a mixture of class and instance kinds for 269 a single class type. 270 """ 271 272 kinds = set() 273 types = set() 274 275 for type in all_types: 276 kinds.add(type.get_kind()) 277 types.add(type.get_origin()) 278 279 return len(types) == 1 and kinds == set(["<class>", "<instance>"]) 280 281 def combine_types(class_types, instance_types, module_types): 282 283 """ 284 Combine 'class_types', 'instance_types', 'module_types' into a single 285 list of references. 286 """ 287 288 all_types = [] 289 for kind, l in [("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types)]: 290 for t in l: 291 all_types.append(Reference(kind, t)) 292 return all_types 293 294 def separate_types(refs): 295 296 """ 297 Separate 'refs' into type-specific lists, returning a tuple containing 298 lists of class types, instance types, module types, function types and 299 unknown "var" types. 300 """ 301 302 class_types = [] 303 instance_types = [] 304 module_types = [] 305 function_types = [] 306 var_types = [] 307 308 for kind, l in [ 309 ("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types), 310 ("<function>", function_types), ("<var>", var_types) 311 ]: 312 313 for ref in refs: 314 if ref.get_kind() == kind: 315 l.append(ref.get_origin()) 316 317 return class_types, instance_types, module_types, function_types, var_types 318 319 # vim: tabstop=4 expandtab shiftwidth=4