paul@12 | 1 | #!/usr/bin/env python |
paul@12 | 2 | |
paul@12 | 3 | """ |
paul@12 | 4 | Name resolution. |
paul@12 | 5 | |
paul@12 | 6 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@12 | 7 | |
paul@12 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@12 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@12 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@12 | 11 | version. |
paul@12 | 12 | |
paul@12 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@12 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@12 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@12 | 16 | details. |
paul@12 | 17 | |
paul@12 | 18 | You should have received a copy of the GNU General Public License along with |
paul@12 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@12 | 20 | """ |
paul@12 | 21 | |
paul@12 | 22 | from common import init_item, predefined_constants |
paul@12 | 23 | from results import AccessRef, InstanceRef, InvocationRef, LocalNameRef, \ |
paul@12 | 24 | NameRef, ResolvedNameRef |
paul@12 | 25 | from referencing import Reference |
paul@12 | 26 | import sys |
paul@12 | 27 | |
paul@12 | 28 | class NameResolving: |
paul@12 | 29 | |
paul@12 | 30 | "Resolving names mix-in for inspected modules." |
paul@12 | 31 | |
paul@12 | 32 | # Object resolution. |
paul@12 | 33 | |
paul@12 | 34 | def resolve_object(self, ref): |
paul@12 | 35 | |
paul@12 | 36 | """ |
paul@12 | 37 | Return the given 'ref' in resolved form, given knowledge of the entire |
paul@12 | 38 | program. |
paul@12 | 39 | """ |
paul@12 | 40 | |
paul@12 | 41 | if ref.has_kind("<depends>"): |
paul@12 | 42 | return self.importer.get_object(ref.get_origin()) |
paul@12 | 43 | else: |
paul@12 | 44 | return ref |
paul@12 | 45 | |
paul@12 | 46 | def get_resolved_object(self, path): |
paul@12 | 47 | |
paul@12 | 48 | """ |
paul@12 | 49 | Get the details of an object with the given 'path'. Where the object |
paul@12 | 50 | has not been resolved, None is returned. This differs from the |
paul@12 | 51 | get_object method used elsewhere in that it does not return an |
paul@12 | 52 | unresolved object reference. |
paul@12 | 53 | """ |
paul@12 | 54 | |
paul@12 | 55 | if self.objects.has_key(path): |
paul@12 | 56 | ref = self.objects[path] |
paul@12 | 57 | if ref.has_kind("<depends>"): |
paul@12 | 58 | return None |
paul@12 | 59 | else: |
paul@12 | 60 | return ref |
paul@12 | 61 | else: |
paul@12 | 62 | return None |
paul@12 | 63 | |
paul@12 | 64 | # Post-inspection resolution activities. |
paul@12 | 65 | |
paul@12 | 66 | def resolve(self): |
paul@12 | 67 | |
paul@12 | 68 | "Resolve dependencies and complete definitions." |
paul@12 | 69 | |
paul@12 | 70 | self.resolve_members() |
paul@12 | 71 | self.resolve_class_bases() |
paul@21 | 72 | self.check_special() |
paul@12 | 73 | self.check_names_used() |
paul@12 | 74 | self.resolve_initialisers() |
paul@12 | 75 | self.resolve_literals() |
paul@12 | 76 | self.remove_redundant_accessors() |
paul@12 | 77 | |
paul@12 | 78 | def resolve_members(self): |
paul@12 | 79 | |
paul@12 | 80 | "Resolve any members referring to deferred references." |
paul@12 | 81 | |
paul@12 | 82 | for name, ref in self.objects.items(): |
paul@12 | 83 | if ref.has_kind("<depends>"): |
paul@12 | 84 | ref = self.importer.get_object(name) |
paul@12 | 85 | ref = ref.alias(name) |
paul@12 | 86 | self.importer.objects[name] = self.objects[name] = ref |
paul@12 | 87 | |
paul@12 | 88 | def resolve_class_bases(self): |
paul@12 | 89 | |
paul@12 | 90 | "Resolve all class bases since some of them may have been deferred." |
paul@12 | 91 | |
paul@12 | 92 | for name, bases in self.classes.items(): |
paul@12 | 93 | resolved = [] |
paul@12 | 94 | bad = [] |
paul@12 | 95 | |
paul@12 | 96 | for base in bases: |
paul@12 | 97 | ref = self.resolve_object(base) |
paul@12 | 98 | |
paul@12 | 99 | # Obtain the origin of the base class reference. |
paul@12 | 100 | |
paul@12 | 101 | if not ref or not ref.has_kind("<class>"): |
paul@12 | 102 | bad.append(base) |
paul@12 | 103 | break |
paul@12 | 104 | |
paul@12 | 105 | resolved.append(ref) |
paul@12 | 106 | |
paul@12 | 107 | if bad: |
paul@12 | 108 | print >>sys.stderr, "Bases of class %s were not classes." % (name, ", ".join(map(str, bad))) |
paul@12 | 109 | else: |
paul@12 | 110 | self.importer.classes[name] = self.classes[name] = resolved |
paul@12 | 111 | |
paul@12 | 112 | def check_special(self): |
paul@12 | 113 | |
paul@12 | 114 | "Check special names." |
paul@12 | 115 | |
paul@12 | 116 | for name, value in self.special.items(): |
paul@12 | 117 | self.special[name] = self.get_resolved_object(value.get_origin()) |
paul@12 | 118 | |
paul@12 | 119 | def check_names_used(self): |
paul@12 | 120 | |
paul@12 | 121 | "Check the names used by each function." |
paul@12 | 122 | |
paul@12 | 123 | for path in self.names_used.keys(): |
paul@12 | 124 | self.check_names_used_for_path(path) |
paul@12 | 125 | |
paul@12 | 126 | def check_names_used_for_path(self, path): |
paul@12 | 127 | |
paul@12 | 128 | "Check the names used by the given 'path'." |
paul@12 | 129 | |
paul@12 | 130 | names = self.names_used.get(path) |
paul@12 | 131 | if not names: |
paul@12 | 132 | return |
paul@12 | 133 | |
paul@12 | 134 | in_function = self.function_locals.has_key(path) |
paul@12 | 135 | |
paul@12 | 136 | for name in names: |
paul@12 | 137 | if name in predefined_constants or in_function and name in self.function_locals[path]: |
paul@12 | 138 | continue |
paul@12 | 139 | |
paul@12 | 140 | # Find local definitions (within static namespaces). |
paul@12 | 141 | |
paul@12 | 142 | key = "%s.%s" % (path, name) |
paul@12 | 143 | ref = self.get_resolved_object(key) |
paul@12 | 144 | if ref: |
paul@12 | 145 | self.name_references[key] = ref.final() or key |
paul@12 | 146 | self.resolve_accesses(path, name, ref) |
paul@12 | 147 | continue |
paul@12 | 148 | |
paul@12 | 149 | # Find global or built-in definitions. |
paul@12 | 150 | |
paul@12 | 151 | ref = self.get_global_or_builtin(name) |
paul@12 | 152 | objpath = ref and (ref.final() or ref.get_name()) |
paul@12 | 153 | if objpath: |
paul@12 | 154 | self.name_references[key] = objpath |
paul@12 | 155 | self.resolve_accesses(path, name, ref) |
paul@12 | 156 | continue |
paul@12 | 157 | |
paul@12 | 158 | print >>sys.stderr, "Name not recognised: %s in %s" % (name, path) |
paul@12 | 159 | init_item(self.names_missing, path, set) |
paul@12 | 160 | self.names_missing[path].add(name) |
paul@12 | 161 | |
paul@12 | 162 | def resolve_accesses(self, path, name, ref): |
paul@12 | 163 | |
paul@12 | 164 | """ |
paul@12 | 165 | Resolve any unresolved accesses in the function at the given 'path' |
paul@12 | 166 | for the given 'name' corresponding to the indicated 'ref'. Note that |
paul@12 | 167 | this mechanism cannot resolve things like inherited methods because |
paul@12 | 168 | they are not recorded as program objects in their inherited locations. |
paul@12 | 169 | """ |
paul@12 | 170 | |
paul@12 | 171 | attr_accesses = self.global_attr_accesses.get(path) |
paul@12 | 172 | all_attrnames = attr_accesses and attr_accesses.get(name) |
paul@12 | 173 | |
paul@12 | 174 | if not all_attrnames: |
paul@12 | 175 | return |
paul@12 | 176 | |
paul@12 | 177 | # Insist on constant accessors. |
paul@12 | 178 | |
paul@12 | 179 | if not ref.has_kind(["<class>", "<module>"]): |
paul@12 | 180 | return |
paul@12 | 181 | |
paul@12 | 182 | found_attrnames = set() |
paul@12 | 183 | |
paul@12 | 184 | for attrnames in all_attrnames: |
paul@12 | 185 | |
paul@12 | 186 | # Start with the resolved name, adding attributes. |
paul@12 | 187 | |
paul@12 | 188 | attrs = ref.get_path() |
paul@12 | 189 | remaining = attrnames.split(".") |
paul@12 | 190 | last_ref = ref |
paul@12 | 191 | |
paul@12 | 192 | # Add each component, testing for a constant object. |
paul@12 | 193 | |
paul@12 | 194 | while remaining: |
paul@12 | 195 | attrname = remaining[0] |
paul@12 | 196 | attrs.append(attrname) |
paul@12 | 197 | del remaining[0] |
paul@12 | 198 | |
paul@12 | 199 | # Find any constant object reference. |
paul@12 | 200 | |
paul@12 | 201 | attr_ref = self.get_resolved_object(".".join(attrs)) |
paul@12 | 202 | |
paul@12 | 203 | # Non-constant accessors terminate the traversal. |
paul@12 | 204 | |
paul@12 | 205 | if not attr_ref or not attr_ref.has_kind(["<class>", "<module>", "<function>"]): |
paul@12 | 206 | |
paul@12 | 207 | # Provide the furthest constant accessor unless the final |
paul@12 | 208 | # access can be resolved. |
paul@12 | 209 | |
paul@12 | 210 | if remaining: |
paul@12 | 211 | remaining.insert(0, attrs.pop()) |
paul@12 | 212 | else: |
paul@12 | 213 | last_ref = attr_ref |
paul@12 | 214 | break |
paul@12 | 215 | |
paul@12 | 216 | # Follow any reference to a constant object. |
paul@12 | 217 | # Where the given name refers to an object in another location, |
paul@12 | 218 | # switch to the other location in order to be able to test its |
paul@12 | 219 | # attributes. |
paul@12 | 220 | |
paul@12 | 221 | last_ref = attr_ref |
paul@12 | 222 | attrs = attr_ref.get_path() |
paul@12 | 223 | |
paul@12 | 224 | # Record a constant accessor only if an object was found |
paul@12 | 225 | # that is different from the namespace involved. |
paul@12 | 226 | |
paul@12 | 227 | if last_ref: |
paul@12 | 228 | objpath = ".".join(attrs) |
paul@12 | 229 | if objpath != path: |
paul@12 | 230 | |
paul@12 | 231 | # Establish a constant access. |
paul@12 | 232 | |
paul@12 | 233 | init_item(self.const_accesses, path, dict) |
paul@12 | 234 | self.const_accesses[path][(name, attrnames)] = (objpath, last_ref, ".".join(remaining)) |
paul@12 | 235 | |
paul@12 | 236 | if len(attrs) > 1: |
paul@12 | 237 | found_attrnames.add(attrs[1]) |
paul@12 | 238 | |
paul@12 | 239 | # Remove any usage records for the name. |
paul@12 | 240 | |
paul@12 | 241 | if found_attrnames: |
paul@12 | 242 | |
paul@12 | 243 | # NOTE: Should be only one name version. |
paul@12 | 244 | |
paul@12 | 245 | versions = [] |
paul@12 | 246 | for version in self.attr_usage[path][name]: |
paul@12 | 247 | new_usage = set() |
paul@12 | 248 | for usage in version: |
paul@12 | 249 | if found_attrnames.intersection(usage): |
paul@12 | 250 | new_usage.add(tuple(set(usage).difference(found_attrnames))) |
paul@12 | 251 | else: |
paul@12 | 252 | new_usage.add(usage) |
paul@12 | 253 | versions.append(new_usage) |
paul@12 | 254 | |
paul@12 | 255 | self.attr_usage[path][name] = versions |
paul@12 | 256 | |
paul@12 | 257 | def resolve_initialisers(self): |
paul@12 | 258 | |
paul@12 | 259 | "Resolve initialiser values for names." |
paul@12 | 260 | |
paul@12 | 261 | # Get the initialisers in each namespace. |
paul@12 | 262 | |
paul@12 | 263 | for path, name_initialisers in self.name_initialisers.items(): |
paul@12 | 264 | const_accesses = self.const_accesses.get(path) |
paul@12 | 265 | |
paul@12 | 266 | # Resolve values for each name in a scope. |
paul@12 | 267 | |
paul@12 | 268 | for name, values in name_initialisers.items(): |
paul@12 | 269 | if path == self.name: |
paul@12 | 270 | assigned_path = name |
paul@12 | 271 | else: |
paul@12 | 272 | assigned_path = "%s.%s" % (path, name) |
paul@12 | 273 | |
paul@12 | 274 | initialised_names = {} |
paul@12 | 275 | aliased_names = {} |
paul@12 | 276 | |
paul@12 | 277 | for i, name_ref in enumerate(values): |
paul@12 | 278 | |
paul@12 | 279 | # Unwrap invocations. |
paul@12 | 280 | |
paul@12 | 281 | if isinstance(name_ref, InvocationRef): |
paul@12 | 282 | invocation = True |
paul@12 | 283 | name_ref = name_ref.name_ref |
paul@12 | 284 | else: |
paul@12 | 285 | invocation = False |
paul@12 | 286 | |
paul@12 | 287 | # Obtain a usable reference from names or constants. |
paul@12 | 288 | |
paul@12 | 289 | if isinstance(name_ref, ResolvedNameRef): |
paul@12 | 290 | if not name_ref.reference(): |
paul@12 | 291 | continue |
paul@12 | 292 | ref = name_ref.reference() |
paul@12 | 293 | |
paul@12 | 294 | # Obtain a reference from instances. |
paul@12 | 295 | |
paul@12 | 296 | elif isinstance(name_ref, InstanceRef): |
paul@12 | 297 | if not name_ref.reference(): |
paul@12 | 298 | continue |
paul@12 | 299 | ref = name_ref.reference() |
paul@12 | 300 | |
paul@12 | 301 | # Resolve accesses that employ constants. |
paul@12 | 302 | |
paul@12 | 303 | elif isinstance(name_ref, AccessRef): |
paul@12 | 304 | ref = None |
paul@12 | 305 | |
paul@12 | 306 | if const_accesses: |
paul@12 | 307 | resolved_access = const_accesses.get((name_ref.original_name, name_ref.attrnames)) |
paul@12 | 308 | if resolved_access: |
paul@12 | 309 | objpath, ref, remaining_attrnames = resolved_access |
paul@12 | 310 | if remaining_attrnames: |
paul@12 | 311 | ref = None |
paul@12 | 312 | |
paul@12 | 313 | # Accesses that do not employ constants cannot be resolved, |
paul@12 | 314 | # but they may be resolvable later. |
paul@12 | 315 | |
paul@12 | 316 | if not ref: |
paul@12 | 317 | if not invocation: |
paul@12 | 318 | aliased_names[i] = name_ref.original_name, name_ref.attrnames, name_ref.number |
paul@12 | 319 | continue |
paul@12 | 320 | |
paul@12 | 321 | # Attempt to resolve a plain name reference. |
paul@12 | 322 | |
paul@12 | 323 | elif isinstance(name_ref, LocalNameRef): |
paul@12 | 324 | key = "%s.%s" % (path, name_ref.name) |
paul@12 | 325 | origin = self.name_references.get(key) |
paul@12 | 326 | |
paul@12 | 327 | # Accesses that do not refer to known static objects |
paul@12 | 328 | # cannot be resolved, but they may be resolvable later. |
paul@12 | 329 | |
paul@12 | 330 | if not origin: |
paul@12 | 331 | if not invocation: |
paul@12 | 332 | aliased_names[i] = name_ref.name, None, name_ref.number |
paul@12 | 333 | continue |
paul@12 | 334 | |
paul@12 | 335 | ref = self.get_resolved_object(origin) |
paul@12 | 336 | if not ref: |
paul@12 | 337 | continue |
paul@12 | 338 | |
paul@12 | 339 | elif isinstance(name_ref, NameRef): |
paul@12 | 340 | key = "%s.%s" % (path, name_ref.name) |
paul@12 | 341 | origin = self.name_references.get(key) |
paul@12 | 342 | if not origin: |
paul@12 | 343 | continue |
paul@12 | 344 | |
paul@12 | 345 | ref = self.get_resolved_object(origin) |
paul@12 | 346 | if not ref: |
paul@12 | 347 | continue |
paul@12 | 348 | |
paul@12 | 349 | else: |
paul@12 | 350 | continue |
paul@12 | 351 | |
paul@12 | 352 | # Convert class invocations to instances. |
paul@12 | 353 | |
paul@12 | 354 | if invocation: |
paul@12 | 355 | ref = ref.has_kind("<class>") and ref.instance_of() or None |
paul@12 | 356 | |
paul@12 | 357 | if ref: |
paul@12 | 358 | initialised_names[i] = ref |
paul@12 | 359 | |
paul@12 | 360 | if initialised_names: |
paul@12 | 361 | self.initialised_names[assigned_path] = initialised_names |
paul@12 | 362 | if aliased_names: |
paul@12 | 363 | self.aliased_names[assigned_path] = aliased_names |
paul@12 | 364 | |
paul@12 | 365 | def resolve_literals(self): |
paul@12 | 366 | |
paul@12 | 367 | "Resolve constant value types." |
paul@12 | 368 | |
paul@12 | 369 | # Get the constants defined in each namespace. |
paul@12 | 370 | |
paul@12 | 371 | for path, constants in self.constants.items(): |
paul@12 | 372 | for constant, n in constants.items(): |
paul@12 | 373 | objpath = "%s.$c%d" % (path, n) |
paul@12 | 374 | _constant, value_type = self.constant_values[objpath] |
paul@12 | 375 | self.initialised_names[objpath] = {0 : Reference("<instance>", value_type)} |
paul@12 | 376 | |
paul@12 | 377 | # Get the literals defined in each namespace. |
paul@12 | 378 | |
paul@12 | 379 | for path, literals in self.literals.items(): |
paul@12 | 380 | for n in range(0, literals): |
paul@12 | 381 | objpath = "%s.$C%d" % (path, n) |
paul@12 | 382 | value_type = self.literal_types[objpath] |
paul@12 | 383 | self.initialised_names[objpath] = {0 : Reference("<instance>", value_type)} |
paul@12 | 384 | |
paul@12 | 385 | def remove_redundant_accessors(self): |
paul@12 | 386 | |
paul@12 | 387 | "Remove now-redundant modifier and accessor information." |
paul@12 | 388 | |
paul@12 | 389 | for path, const_accesses in self.const_accesses.items(): |
paul@12 | 390 | accesses = self.attr_accessors.get(path) |
paul@12 | 391 | modifiers = self.attr_access_modifiers.get(path) |
paul@12 | 392 | if not accesses: |
paul@12 | 393 | continue |
paul@12 | 394 | for access in const_accesses.keys(): |
paul@12 | 395 | if accesses.has_key(access): |
paul@12 | 396 | del accesses[access] |
paul@12 | 397 | if modifiers and modifiers.has_key(access): |
paul@12 | 398 | del modifiers[access] |
paul@12 | 399 | |
paul@12 | 400 | # vim: tabstop=4 expandtab shiftwidth=4 |