paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Inspect and obtain module structure. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, |
paul@0 | 7 | 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 8 | |
paul@0 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 12 | version. |
paul@0 | 13 | |
paul@0 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 17 | details. |
paul@0 | 18 | |
paul@0 | 19 | You should have received a copy of the GNU General Public License along with |
paul@0 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 21 | """ |
paul@0 | 22 | |
paul@0 | 23 | from branching import BranchTracker |
paul@0 | 24 | from common import * |
paul@0 | 25 | from modules import * |
paul@0 | 26 | from os import listdir |
paul@0 | 27 | from os.path import extsep, split, splitext |
paul@0 | 28 | from referencing import Reference |
paul@0 | 29 | import compiler |
paul@0 | 30 | import sys |
paul@0 | 31 | |
paul@0 | 32 | class AccessRef(Result): |
paul@0 | 33 | |
paul@0 | 34 | """ |
paul@0 | 35 | A reference to an attribute access that is generally only returned from a |
paul@0 | 36 | processed access for possible initialiser resolution for assignments. |
paul@0 | 37 | """ |
paul@0 | 38 | |
paul@0 | 39 | def __init__(self, original_name, attrnames, number): |
paul@0 | 40 | self.original_name = original_name |
paul@0 | 41 | self.attrnames = attrnames |
paul@0 | 42 | self.number = number |
paul@0 | 43 | |
paul@0 | 44 | def reference(self): |
paul@0 | 45 | return None |
paul@0 | 46 | |
paul@0 | 47 | def __repr__(self): |
paul@0 | 48 | return "AccessRef(%r, %r, %r)" % (self.original_name, self.attrnames, self.number) |
paul@0 | 49 | |
paul@0 | 50 | class InvocationRef(Result): |
paul@0 | 51 | |
paul@0 | 52 | "An invocation of a name reference." |
paul@0 | 53 | |
paul@0 | 54 | def __init__(self, name_ref): |
paul@0 | 55 | self.name_ref = name_ref |
paul@0 | 56 | |
paul@0 | 57 | def __repr__(self): |
paul@0 | 58 | return "InvocationRef(%r)" % self.name_ref |
paul@0 | 59 | |
paul@0 | 60 | class InspectedModule(BasicModule, CacheWritingModule): |
paul@0 | 61 | |
paul@0 | 62 | "A module inspector." |
paul@0 | 63 | |
paul@0 | 64 | def __init__(self, name, importer): |
paul@0 | 65 | BasicModule.__init__(self, name, importer) |
paul@0 | 66 | self.in_class = False |
paul@0 | 67 | self.in_conditional = False |
paul@0 | 68 | self.global_attr_accesses = {} |
paul@0 | 69 | |
paul@0 | 70 | # Nested scope handling. |
paul@0 | 71 | |
paul@0 | 72 | self.parent_function = None |
paul@0 | 73 | self.propagated_names = {} |
paul@0 | 74 | |
paul@0 | 75 | # Usage tracking. |
paul@0 | 76 | |
paul@0 | 77 | self.trackers = [] |
paul@0 | 78 | self.attr_accessor_branches = {} |
paul@0 | 79 | |
paul@0 | 80 | def __repr__(self): |
paul@0 | 81 | return "InspectedModule(%r, %r)" % (self.name, self.importer) |
paul@0 | 82 | |
paul@0 | 83 | def parse(self, filename): |
paul@0 | 84 | |
paul@0 | 85 | "Parse the file having the given 'filename'." |
paul@0 | 86 | |
paul@0 | 87 | self.parse_file(filename) |
paul@0 | 88 | |
paul@0 | 89 | # Inspect the module. |
paul@0 | 90 | |
paul@0 | 91 | self.start_tracking_in_module() |
paul@0 | 92 | |
paul@0 | 93 | # Detect and record imports and globals declared in the module. |
paul@0 | 94 | |
paul@0 | 95 | self.assign_general_local("__name__", self.get_constant("str", self.name)) |
paul@0 | 96 | self.assign_general_local("__file__", self.get_constant("str", filename)) |
paul@0 | 97 | self.process_structure(self.astnode) |
paul@0 | 98 | |
paul@0 | 99 | # Set the class of the module after the definition has occurred. |
paul@0 | 100 | |
paul@0 | 101 | ref = self.get_builtin("object") |
paul@0 | 102 | self.set_name("__class__", ref) |
paul@0 | 103 | |
paul@0 | 104 | # Get module-level attribute usage details. |
paul@0 | 105 | |
paul@0 | 106 | self.stop_tracking_in_module() |
paul@0 | 107 | |
paul@0 | 108 | # Check names used and resolve them. |
paul@0 | 109 | |
paul@0 | 110 | self.register_submodules() |
paul@0 | 111 | self.loaded = True |
paul@0 | 112 | |
paul@0 | 113 | def register_submodules(self): |
paul@0 | 114 | |
paul@0 | 115 | "For package modules add submodule details." |
paul@0 | 116 | |
paul@0 | 117 | if splitext(split(self.filename)[1])[0] == "__init__": |
paul@0 | 118 | for subname in listdir(split(self.filename)[0]): |
paul@0 | 119 | name, ext = splitext(subname) |
paul@0 | 120 | |
paul@0 | 121 | # Add modules whose names are not already defined as globals. |
paul@0 | 122 | |
paul@0 | 123 | if ext == ("%spy" % extsep) and name != "__init__" and not self.get_global(name): |
paul@0 | 124 | module_name = self.get_global_path(name) |
paul@0 | 125 | top, submodule = self.get_module(module_name, True) |
paul@0 | 126 | self.set_module(name, submodule, hidden=True) |
paul@0 | 127 | |
paul@0 | 128 | def check_special(self): |
paul@0 | 129 | |
paul@0 | 130 | "Check special names." |
paul@0 | 131 | |
paul@0 | 132 | for name, value in self.special.items(): |
paul@0 | 133 | if value.has_kind("<depends>"): |
paul@0 | 134 | self.find_imported_name(name, self.name) |
paul@0 | 135 | self.special[name] = self.get_object(value.get_origin()) |
paul@0 | 136 | |
paul@0 | 137 | def check_names_used(self): |
paul@0 | 138 | |
paul@0 | 139 | "Check the names used by each function." |
paul@0 | 140 | |
paul@0 | 141 | for path in self.names_used.keys(): |
paul@0 | 142 | self.check_names_used_for_path(path) |
paul@0 | 143 | |
paul@0 | 144 | def check_names_used_for_path(self, path): |
paul@0 | 145 | |
paul@0 | 146 | "Check the names used by the given 'path'." |
paul@0 | 147 | |
paul@0 | 148 | names = self.names_used.get(path) |
paul@0 | 149 | if not names: |
paul@0 | 150 | return |
paul@0 | 151 | |
paul@0 | 152 | in_function = self.function_locals.has_key(path) |
paul@0 | 153 | |
paul@0 | 154 | for name in names: |
paul@0 | 155 | if name in predefined_constants or in_function and name in self.function_locals[path]: |
paul@0 | 156 | continue |
paul@0 | 157 | |
paul@0 | 158 | # Resolve names that have been imported locally. |
paul@0 | 159 | |
paul@0 | 160 | self.find_imported_name(name, path) |
paul@0 | 161 | |
paul@0 | 162 | # Find local definitions. |
paul@0 | 163 | |
paul@0 | 164 | key = "%s.%s" % (path, name) |
paul@0 | 165 | ref = self.get_object(key) |
paul@0 | 166 | if ref: |
paul@0 | 167 | self.name_references[key] = ref.final() or key |
paul@0 | 168 | self.resolve_accesses(path, name, ref) |
paul@0 | 169 | continue |
paul@0 | 170 | |
paul@0 | 171 | # Resolve names that have been imported globally. |
paul@0 | 172 | |
paul@0 | 173 | self.find_imported_name(name, self.name) |
paul@0 | 174 | |
paul@0 | 175 | # Find global or built-in definitions. |
paul@0 | 176 | |
paul@0 | 177 | ref = self.get_global_or_builtin(name) |
paul@0 | 178 | objpath = ref and (ref.final() or ref.get_name()) |
paul@0 | 179 | if objpath: |
paul@0 | 180 | self.name_references[key] = objpath |
paul@0 | 181 | self.resolve_accesses(path, name, ref) |
paul@0 | 182 | continue |
paul@0 | 183 | |
paul@0 | 184 | print >>sys.stderr, "Name not recognised: %s in %s" % (name, path) |
paul@0 | 185 | init_item(self.names_missing, path, set) |
paul@0 | 186 | self.names_missing[path].add(name) |
paul@0 | 187 | |
paul@0 | 188 | def resolve_members(self): |
paul@0 | 189 | |
paul@0 | 190 | "Resolve any members referring to deferred references." |
paul@0 | 191 | |
paul@0 | 192 | for name, ref in self.objects.items(): |
paul@0 | 193 | if ref.has_kind("<depends>"): |
paul@0 | 194 | ref = self.get_object(ref.get_origin()) |
paul@0 | 195 | ref = ref.alias(name) |
paul@0 | 196 | self.importer.objects[name] = self.objects[name] = ref |
paul@0 | 197 | |
paul@0 | 198 | def resolve_accesses(self, path, name, ref): |
paul@0 | 199 | |
paul@0 | 200 | """ |
paul@0 | 201 | Resolve any unresolved accesses in the function at the given 'path' |
paul@0 | 202 | for the given 'name' corresponding to the indicated 'ref'. Note that |
paul@0 | 203 | this mechanism cannot resolve things like inherited methods because |
paul@0 | 204 | they are not recorded as program objects in their inherited locations. |
paul@0 | 205 | """ |
paul@0 | 206 | |
paul@0 | 207 | attr_accesses = self.global_attr_accesses.get(path) |
paul@0 | 208 | all_attrnames = attr_accesses and attr_accesses.get(name) |
paul@0 | 209 | |
paul@0 | 210 | if not all_attrnames: |
paul@0 | 211 | return |
paul@0 | 212 | |
paul@0 | 213 | # Insist on constant accessors. |
paul@0 | 214 | |
paul@0 | 215 | if not ref.has_kind(["<class>", "<module>"]): |
paul@0 | 216 | return |
paul@0 | 217 | |
paul@0 | 218 | found_attrnames = set() |
paul@0 | 219 | |
paul@0 | 220 | for attrnames in all_attrnames: |
paul@0 | 221 | |
paul@0 | 222 | # Start with the resolved name, adding attributes. |
paul@0 | 223 | |
paul@0 | 224 | attrs = ref.get_path() |
paul@0 | 225 | remaining = attrnames.split(".") |
paul@0 | 226 | last_ref = ref |
paul@0 | 227 | |
paul@0 | 228 | # Add each component, testing for a constant object. |
paul@0 | 229 | |
paul@0 | 230 | while remaining: |
paul@0 | 231 | attrname = remaining[0] |
paul@0 | 232 | attrs.append(attrname) |
paul@0 | 233 | del remaining[0] |
paul@0 | 234 | |
paul@0 | 235 | # Find any constant object reference. |
paul@0 | 236 | |
paul@0 | 237 | attr_ref = self.get_object(".".join(attrs)) |
paul@0 | 238 | |
paul@0 | 239 | # Non-constant accessors terminate the traversal. |
paul@0 | 240 | |
paul@0 | 241 | if not attr_ref.has_kind(["<class>", "<module>", "<function>"]): |
paul@0 | 242 | |
paul@0 | 243 | # Provide the furthest constant accessor unless the final |
paul@0 | 244 | # access can be resolved. |
paul@0 | 245 | |
paul@0 | 246 | if remaining: |
paul@0 | 247 | remaining.insert(0, attrs.pop()) |
paul@0 | 248 | else: |
paul@0 | 249 | last_ref = attr_ref |
paul@0 | 250 | break |
paul@0 | 251 | |
paul@0 | 252 | # A module may expose an attribute imported from a hidden |
paul@0 | 253 | # module. |
paul@0 | 254 | |
paul@0 | 255 | elif last_ref.has_kind("<module>"): |
paul@0 | 256 | module, leaf_module = self.get_module(last_ref.get_origin()) |
paul@0 | 257 | self.find_imported_name(attrname, module.name, module) |
paul@0 | 258 | |
paul@0 | 259 | # Follow any reference to a constant object. |
paul@0 | 260 | # Where the given name refers to an object in another location, |
paul@0 | 261 | # switch to the other location in order to be able to test its |
paul@0 | 262 | # attributes. |
paul@0 | 263 | |
paul@0 | 264 | last_ref = attr_ref |
paul@0 | 265 | attrs = attr_ref.get_path() |
paul@0 | 266 | |
paul@0 | 267 | # Record a constant accessor only if an object was found |
paul@0 | 268 | # that is different from the namespace involved. |
paul@0 | 269 | |
paul@0 | 270 | if last_ref: |
paul@0 | 271 | objpath = ".".join(attrs) |
paul@0 | 272 | if objpath != path: |
paul@0 | 273 | |
paul@0 | 274 | # Establish a constant access. |
paul@0 | 275 | |
paul@0 | 276 | init_item(self.const_accesses, path, dict) |
paul@0 | 277 | self.const_accesses[path][(name, attrnames)] = (objpath, last_ref, ".".join(remaining)) |
paul@0 | 278 | |
paul@0 | 279 | if len(attrs) > 1: |
paul@0 | 280 | found_attrnames.add(attrs[1]) |
paul@0 | 281 | |
paul@0 | 282 | # Remove any usage records for the name. |
paul@0 | 283 | |
paul@0 | 284 | if found_attrnames: |
paul@0 | 285 | |
paul@0 | 286 | # NOTE: Should be only one name version. |
paul@0 | 287 | |
paul@0 | 288 | versions = [] |
paul@0 | 289 | for version in self.attr_usage[path][name]: |
paul@0 | 290 | new_usage = set() |
paul@0 | 291 | for usage in version: |
paul@0 | 292 | if found_attrnames.intersection(usage): |
paul@0 | 293 | new_usage.add(tuple(set(usage).difference(found_attrnames))) |
paul@0 | 294 | else: |
paul@0 | 295 | new_usage.add(usage) |
paul@0 | 296 | versions.append(new_usage) |
paul@0 | 297 | |
paul@0 | 298 | self.attr_usage[path][name] = versions |
paul@0 | 299 | |
paul@0 | 300 | def resolve_initialisers(self): |
paul@0 | 301 | |
paul@0 | 302 | "Resolve initialiser values for names." |
paul@0 | 303 | |
paul@0 | 304 | # Get the initialisers in each namespace. |
paul@0 | 305 | |
paul@0 | 306 | for path, name_initialisers in self.name_initialisers.items(): |
paul@0 | 307 | const_accesses = self.const_accesses.get(path) |
paul@0 | 308 | |
paul@0 | 309 | # Resolve values for each name in a scope. |
paul@0 | 310 | |
paul@0 | 311 | for name, values in name_initialisers.items(): |
paul@0 | 312 | if path == self.name: |
paul@0 | 313 | assigned_path = name |
paul@0 | 314 | else: |
paul@0 | 315 | assigned_path = "%s.%s" % (path, name) |
paul@0 | 316 | |
paul@0 | 317 | initialised_names = {} |
paul@0 | 318 | aliased_names = {} |
paul@0 | 319 | |
paul@0 | 320 | for i, name_ref in enumerate(values): |
paul@0 | 321 | |
paul@0 | 322 | # Unwrap invocations. |
paul@0 | 323 | |
paul@0 | 324 | if isinstance(name_ref, InvocationRef): |
paul@0 | 325 | invocation = True |
paul@0 | 326 | name_ref = name_ref.name_ref |
paul@0 | 327 | else: |
paul@0 | 328 | invocation = False |
paul@0 | 329 | |
paul@0 | 330 | # Obtain a usable reference from names or constants. |
paul@0 | 331 | |
paul@0 | 332 | if isinstance(name_ref, ResolvedNameRef): |
paul@0 | 333 | if not name_ref.reference(): |
paul@0 | 334 | continue |
paul@0 | 335 | ref = name_ref.reference() |
paul@0 | 336 | |
paul@0 | 337 | # Obtain a reference from instances. |
paul@0 | 338 | |
paul@0 | 339 | elif isinstance(name_ref, InstanceRef): |
paul@0 | 340 | if not name_ref.reference(): |
paul@0 | 341 | continue |
paul@0 | 342 | ref = name_ref.reference() |
paul@0 | 343 | |
paul@0 | 344 | # Resolve accesses that employ constants. |
paul@0 | 345 | |
paul@0 | 346 | elif isinstance(name_ref, AccessRef): |
paul@0 | 347 | ref = None |
paul@0 | 348 | |
paul@0 | 349 | if const_accesses: |
paul@0 | 350 | resolved_access = const_accesses.get((name_ref.original_name, name_ref.attrnames)) |
paul@0 | 351 | if resolved_access: |
paul@0 | 352 | objpath, ref, remaining_attrnames = resolved_access |
paul@0 | 353 | if remaining_attrnames: |
paul@0 | 354 | ref = None |
paul@0 | 355 | |
paul@0 | 356 | # Accesses that do not employ constants cannot be resolved, |
paul@0 | 357 | # but they may be resolvable later. |
paul@0 | 358 | |
paul@0 | 359 | if not ref: |
paul@0 | 360 | if not invocation: |
paul@0 | 361 | aliased_names[i] = name_ref.original_name, name_ref.attrnames, name_ref.number |
paul@0 | 362 | continue |
paul@0 | 363 | |
paul@0 | 364 | # Attempt to resolve a plain name reference. |
paul@0 | 365 | |
paul@0 | 366 | elif isinstance(name_ref, LocalNameRef): |
paul@0 | 367 | key = "%s.%s" % (path, name_ref.name) |
paul@0 | 368 | origin = self.name_references.get(key) |
paul@0 | 369 | |
paul@0 | 370 | # Accesses that do not refer to known static objects |
paul@0 | 371 | # cannot be resolved, but they may be resolvable later. |
paul@0 | 372 | |
paul@0 | 373 | if not origin: |
paul@0 | 374 | if not invocation: |
paul@0 | 375 | aliased_names[i] = name_ref.name, None, name_ref.number |
paul@0 | 376 | continue |
paul@0 | 377 | |
paul@0 | 378 | ref = self.get_object(origin) |
paul@0 | 379 | if not ref: |
paul@0 | 380 | continue |
paul@0 | 381 | |
paul@0 | 382 | elif isinstance(name_ref, NameRef): |
paul@0 | 383 | key = "%s.%s" % (path, name_ref.name) |
paul@0 | 384 | origin = self.name_references.get(key) |
paul@0 | 385 | if not origin: |
paul@0 | 386 | continue |
paul@0 | 387 | |
paul@0 | 388 | ref = self.get_object(origin) |
paul@0 | 389 | if not ref: |
paul@0 | 390 | continue |
paul@0 | 391 | |
paul@0 | 392 | else: |
paul@0 | 393 | continue |
paul@0 | 394 | |
paul@0 | 395 | # Convert class invocations to instances. |
paul@0 | 396 | |
paul@0 | 397 | if invocation: |
paul@0 | 398 | ref = ref.has_kind("<class>") and ref.instance_of() or None |
paul@0 | 399 | |
paul@0 | 400 | if ref: |
paul@0 | 401 | initialised_names[i] = ref |
paul@0 | 402 | |
paul@0 | 403 | if initialised_names: |
paul@0 | 404 | self.initialised_names[assigned_path] = initialised_names |
paul@0 | 405 | if aliased_names: |
paul@0 | 406 | self.aliased_names[assigned_path] = aliased_names |
paul@0 | 407 | |
paul@0 | 408 | def resolve_literals(self): |
paul@0 | 409 | |
paul@0 | 410 | "Resolve constant value types." |
paul@0 | 411 | |
paul@0 | 412 | # Get the constants defined in each namespace. |
paul@0 | 413 | |
paul@0 | 414 | for path, constants in self.constants.items(): |
paul@0 | 415 | for constant, n in constants.items(): |
paul@0 | 416 | objpath = "%s.$c%d" % (path, n) |
paul@0 | 417 | _constant, value_type = self.constant_values[objpath] |
paul@0 | 418 | self.initialised_names[objpath] = {0 : Reference("<instance>", value_type)} |
paul@0 | 419 | |
paul@0 | 420 | # Get the literals defined in each namespace. |
paul@0 | 421 | |
paul@0 | 422 | for path, literals in self.literals.items(): |
paul@0 | 423 | for n in range(0, literals): |
paul@0 | 424 | objpath = "%s.$C%d" % (path, n) |
paul@0 | 425 | value_type = self.literal_types[objpath] |
paul@0 | 426 | self.initialised_names[objpath] = {0 : Reference("<instance>", value_type)} |
paul@0 | 427 | |
paul@0 | 428 | def remove_redundant_accessors(self): |
paul@0 | 429 | |
paul@0 | 430 | "Remove now-redundant modifier and accessor information." |
paul@0 | 431 | |
paul@0 | 432 | for path, const_accesses in self.const_accesses.items(): |
paul@0 | 433 | accesses = self.attr_accessors.get(path) |
paul@0 | 434 | modifiers = self.attr_access_modifiers.get(path) |
paul@0 | 435 | if not accesses: |
paul@0 | 436 | continue |
paul@0 | 437 | for access in const_accesses.keys(): |
paul@0 | 438 | if accesses.has_key(access): |
paul@0 | 439 | del accesses[access] |
paul@0 | 440 | if modifiers and modifiers.has_key(access): |
paul@0 | 441 | del modifiers[access] |
paul@0 | 442 | |
paul@0 | 443 | def set_invocation_usage(self): |
paul@0 | 444 | |
paul@0 | 445 | """ |
paul@0 | 446 | Discard the current invocation storage figures, retaining the maximum |
paul@0 | 447 | values. |
paul@0 | 448 | """ |
paul@0 | 449 | |
paul@0 | 450 | for path, (current, maximum) in self.function_targets.items(): |
paul@0 | 451 | self.importer.function_targets[path] = self.function_targets[path] = maximum |
paul@0 | 452 | |
paul@0 | 453 | for path, (current, maximum) in self.function_arguments.items(): |
paul@0 | 454 | self.importer.function_arguments[path] = self.function_arguments[path] = maximum |
paul@0 | 455 | |
paul@0 | 456 | # Module structure traversal. |
paul@0 | 457 | |
paul@0 | 458 | def process_structure_node(self, n): |
paul@0 | 459 | |
paul@0 | 460 | "Process the individual node 'n'." |
paul@0 | 461 | |
paul@0 | 462 | # Module global detection. |
paul@0 | 463 | |
paul@0 | 464 | if isinstance(n, compiler.ast.Global): |
paul@0 | 465 | self.process_global_node(n) |
paul@0 | 466 | |
paul@0 | 467 | # Module import declarations. |
paul@0 | 468 | |
paul@0 | 469 | elif isinstance(n, compiler.ast.From): |
paul@0 | 470 | self.process_from_node(n) |
paul@0 | 471 | |
paul@0 | 472 | elif isinstance(n, compiler.ast.Import): |
paul@0 | 473 | self.process_import_node(n) |
paul@0 | 474 | |
paul@0 | 475 | # Nodes using operator module functions. |
paul@0 | 476 | |
paul@0 | 477 | elif isinstance(n, compiler.ast.Operator): |
paul@0 | 478 | return self.process_operator_node(n) |
paul@0 | 479 | |
paul@0 | 480 | elif isinstance(n, compiler.ast.AugAssign): |
paul@0 | 481 | self.process_augassign_node(n) |
paul@0 | 482 | |
paul@0 | 483 | elif isinstance(n, compiler.ast.Compare): |
paul@0 | 484 | return self.process_compare_node(n) |
paul@0 | 485 | |
paul@0 | 486 | elif isinstance(n, compiler.ast.Slice): |
paul@0 | 487 | return self.process_slice_node(n) |
paul@0 | 488 | |
paul@0 | 489 | elif isinstance(n, compiler.ast.Sliceobj): |
paul@0 | 490 | return self.process_sliceobj_node(n) |
paul@0 | 491 | |
paul@0 | 492 | elif isinstance(n, compiler.ast.Subscript): |
paul@0 | 493 | return self.process_subscript_node(n) |
paul@0 | 494 | |
paul@0 | 495 | # Namespaces within modules. |
paul@0 | 496 | |
paul@0 | 497 | elif isinstance(n, compiler.ast.Class): |
paul@0 | 498 | self.process_class_node(n) |
paul@0 | 499 | |
paul@0 | 500 | elif isinstance(n, compiler.ast.Function): |
paul@0 | 501 | self.process_function_node(n, n.name) |
paul@0 | 502 | |
paul@0 | 503 | elif isinstance(n, compiler.ast.Lambda): |
paul@0 | 504 | return self.process_lambda_node(n) |
paul@0 | 505 | |
paul@0 | 506 | # Assignments. |
paul@0 | 507 | |
paul@0 | 508 | elif isinstance(n, compiler.ast.Assign): |
paul@0 | 509 | |
paul@0 | 510 | # Handle each assignment node. |
paul@0 | 511 | |
paul@0 | 512 | for node in n.nodes: |
paul@0 | 513 | self.process_assignment_node(node, n.expr) |
paul@0 | 514 | |
paul@0 | 515 | # Assignments within non-Assign nodes. |
paul@0 | 516 | |
paul@0 | 517 | elif isinstance(n, compiler.ast.AssName): |
paul@0 | 518 | self.process_assignment_node(n, None) |
paul@0 | 519 | |
paul@0 | 520 | elif isinstance(n, compiler.ast.AssAttr): |
paul@0 | 521 | self.process_attribute_access(n) |
paul@0 | 522 | |
paul@0 | 523 | # Accesses. |
paul@0 | 524 | |
paul@0 | 525 | elif isinstance(n, compiler.ast.Getattr): |
paul@0 | 526 | return self.process_attribute_access(n) |
paul@0 | 527 | |
paul@0 | 528 | # Name recording for later testing. |
paul@0 | 529 | |
paul@0 | 530 | elif isinstance(n, compiler.ast.Name): |
paul@0 | 531 | return self.process_name_node(n) |
paul@0 | 532 | |
paul@0 | 533 | # Conditional statement tracking. |
paul@0 | 534 | |
paul@0 | 535 | elif isinstance(n, compiler.ast.For): |
paul@0 | 536 | self.process_for_node(n) |
paul@0 | 537 | |
paul@0 | 538 | elif isinstance(n, compiler.ast.While): |
paul@0 | 539 | self.process_while_node(n) |
paul@0 | 540 | |
paul@0 | 541 | elif isinstance(n, compiler.ast.If): |
paul@0 | 542 | self.process_if_node(n) |
paul@0 | 543 | |
paul@0 | 544 | elif isinstance(n, (compiler.ast.And, compiler.ast.Or)): |
paul@0 | 545 | return self.process_logical_node(n) |
paul@0 | 546 | |
paul@0 | 547 | # Exception control-flow tracking. |
paul@0 | 548 | |
paul@0 | 549 | elif isinstance(n, compiler.ast.TryExcept): |
paul@0 | 550 | self.process_try_node(n) |
paul@0 | 551 | |
paul@0 | 552 | elif isinstance(n, compiler.ast.TryFinally): |
paul@0 | 553 | self.process_try_finally_node(n) |
paul@0 | 554 | |
paul@0 | 555 | # Control-flow modification statements. |
paul@0 | 556 | |
paul@0 | 557 | elif isinstance(n, compiler.ast.Break): |
paul@0 | 558 | self.trackers[-1].suspend_broken_branch() |
paul@0 | 559 | |
paul@0 | 560 | elif isinstance(n, compiler.ast.Continue): |
paul@0 | 561 | self.trackers[-1].suspend_continuing_branch() |
paul@0 | 562 | |
paul@0 | 563 | elif isinstance(n, compiler.ast.Raise): |
paul@0 | 564 | self.process_structure(n) |
paul@0 | 565 | self.trackers[-1].abandon_branch() |
paul@0 | 566 | |
paul@0 | 567 | elif isinstance(n, compiler.ast.Return): |
paul@0 | 568 | self.process_structure(n) |
paul@0 | 569 | self.trackers[-1].abandon_returning_branch() |
paul@0 | 570 | |
paul@0 | 571 | # Invocations. |
paul@0 | 572 | |
paul@0 | 573 | elif isinstance(n, compiler.ast.CallFunc): |
paul@0 | 574 | return self.process_invocation_node(n) |
paul@0 | 575 | |
paul@0 | 576 | # Constant usage. |
paul@0 | 577 | |
paul@0 | 578 | elif isinstance(n, compiler.ast.Const): |
paul@0 | 579 | return self.get_literal_instance(n, n.value.__class__.__name__) |
paul@0 | 580 | |
paul@0 | 581 | elif isinstance(n, compiler.ast.Dict): |
paul@0 | 582 | return self.get_literal_instance(n, "dict") |
paul@0 | 583 | |
paul@0 | 584 | elif isinstance(n, compiler.ast.List): |
paul@0 | 585 | return self.get_literal_instance(n, "list") |
paul@0 | 586 | |
paul@0 | 587 | elif isinstance(n, compiler.ast.Tuple): |
paul@0 | 588 | return self.get_literal_instance(n, "tuple") |
paul@0 | 589 | |
paul@0 | 590 | # List comprehensions and if expressions. |
paul@0 | 591 | |
paul@0 | 592 | elif isinstance(n, compiler.ast.ListComp): |
paul@0 | 593 | self.process_listcomp_node(n) |
paul@0 | 594 | |
paul@0 | 595 | elif isinstance(n, compiler.ast.IfExp): |
paul@0 | 596 | self.process_ifexp_node(n) |
paul@0 | 597 | |
paul@0 | 598 | # All other nodes are processed depth-first. |
paul@0 | 599 | |
paul@0 | 600 | else: |
paul@0 | 601 | self.process_structure(n) |
paul@0 | 602 | |
paul@0 | 603 | # By default, no expression details are returned. |
paul@0 | 604 | |
paul@0 | 605 | return None |
paul@0 | 606 | |
paul@0 | 607 | # Specific node handling. |
paul@0 | 608 | |
paul@0 | 609 | def process_assignment_node(self, n, expr): |
paul@0 | 610 | |
paul@0 | 611 | "Process the individual node 'n' to be assigned the contents of 'expr'." |
paul@0 | 612 | |
paul@0 | 613 | # Names and attributes are assigned the entire expression. |
paul@0 | 614 | |
paul@0 | 615 | if isinstance(n, compiler.ast.AssName): |
paul@0 | 616 | |
paul@0 | 617 | name_ref = expr and self.process_structure_node(expr) |
paul@0 | 618 | |
paul@0 | 619 | # Name assignments populate either function namespaces or the |
paul@0 | 620 | # general namespace hierarchy. |
paul@0 | 621 | |
paul@0 | 622 | self.assign_general_local(n.name, name_ref) |
paul@0 | 623 | |
paul@0 | 624 | # Record usage of the name. |
paul@0 | 625 | |
paul@0 | 626 | self.record_name(n.name) |
paul@0 | 627 | |
paul@0 | 628 | elif isinstance(n, compiler.ast.AssAttr): |
paul@0 | 629 | if expr: self.process_structure_node(expr) |
paul@0 | 630 | self.process_attribute_access(n) |
paul@0 | 631 | |
paul@0 | 632 | # Lists and tuples are matched against the expression and their |
paul@0 | 633 | # items assigned to expression items. |
paul@0 | 634 | |
paul@0 | 635 | elif isinstance(n, (compiler.ast.AssList, compiler.ast.AssTuple)): |
paul@0 | 636 | self.process_assignment_node_items(n, expr) |
paul@0 | 637 | |
paul@0 | 638 | # Slices and subscripts are permitted within assignment nodes. |
paul@0 | 639 | |
paul@0 | 640 | elif isinstance(n, compiler.ast.Slice): |
paul@0 | 641 | self.process_slice_node(n, expr) |
paul@0 | 642 | |
paul@0 | 643 | elif isinstance(n, compiler.ast.Subscript): |
paul@0 | 644 | self.process_subscript_node(n, expr) |
paul@0 | 645 | |
paul@0 | 646 | def process_attribute_access(self, n): |
paul@0 | 647 | |
paul@0 | 648 | "Process the given attribute access node 'n'." |
paul@0 | 649 | |
paul@0 | 650 | # Obtain any completed chain and return the reference to it. |
paul@0 | 651 | |
paul@0 | 652 | name_ref = self.process_attribute_chain(n) |
paul@0 | 653 | if self.have_access_expression(n): |
paul@0 | 654 | return name_ref |
paul@0 | 655 | |
paul@0 | 656 | # Where the start of the chain of attributes has been reached, determine |
paul@0 | 657 | # the complete access. |
paul@0 | 658 | |
paul@0 | 659 | # Given a non-access node, this chain can be handled in its entirety, |
paul@0 | 660 | # either being name-based and thus an access rooted on a name, or being |
paul@0 | 661 | # based on some other node and thus an anonymous access of some kind. |
paul@0 | 662 | |
paul@0 | 663 | path = self.get_namespace_path() |
paul@0 | 664 | |
paul@0 | 665 | # Start with the the full attribute chain. |
paul@0 | 666 | |
paul@0 | 667 | remaining = self.attrs |
paul@0 | 668 | attrnames = ".".join(remaining) |
paul@0 | 669 | |
paul@0 | 670 | # If the accessor cannot be identified, or where attributes |
paul@0 | 671 | # remain in an attribute chain, record the anonymous accesses. |
paul@0 | 672 | |
paul@0 | 673 | if not isinstance(name_ref, NameRef): # includes ResolvedNameRef |
paul@0 | 674 | |
paul@0 | 675 | assignment = isinstance(n, compiler.ast.AssAttr) |
paul@0 | 676 | |
paul@0 | 677 | init_item(self.attr_accesses, path, set) |
paul@0 | 678 | self.attr_accesses[path].add(attrnames) |
paul@0 | 679 | |
paul@0 | 680 | self.record_access_details(None, attrnames, assignment) |
paul@0 | 681 | del self.attrs[0] |
paul@0 | 682 | return |
paul@0 | 683 | |
paul@0 | 684 | # Name-based accesses will handle the first attribute in a |
paul@0 | 685 | # chain. |
paul@0 | 686 | |
paul@0 | 687 | else: |
paul@0 | 688 | attrname = remaining[0] |
paul@0 | 689 | |
paul@0 | 690 | # Attribute assignments are used to identify instance attributes. |
paul@0 | 691 | |
paul@0 | 692 | if isinstance(n, compiler.ast.AssAttr) and \ |
paul@0 | 693 | self.in_class and self.in_function and n.expr.name == "self": |
paul@0 | 694 | |
paul@0 | 695 | self.set_instance_attr(attrname) |
paul@0 | 696 | |
paul@0 | 697 | # Record attribute usage using any name local to this namespace, |
paul@0 | 698 | # if assigned in the namespace, or using an external name |
paul@0 | 699 | # (presently just globals within classes). |
paul@0 | 700 | |
paul@0 | 701 | name = self.get_name_for_tracking(name_ref.name, name_ref.final()) |
paul@0 | 702 | tracker = self.trackers[-1] |
paul@0 | 703 | |
paul@0 | 704 | immediate_access = len(self.attrs) == 1 |
paul@0 | 705 | assignment = immediate_access and isinstance(n, compiler.ast.AssAttr) |
paul@0 | 706 | |
paul@0 | 707 | del self.attrs[0] |
paul@0 | 708 | |
paul@0 | 709 | # Record global-based chains for subsequent resolution. |
paul@0 | 710 | |
paul@0 | 711 | is_global = self.in_function and not self.function_locals[path].has_key(name) or \ |
paul@0 | 712 | not self.in_function |
paul@0 | 713 | |
paul@0 | 714 | if is_global: |
paul@0 | 715 | self.record_global_access_details(name, attrnames) |
paul@0 | 716 | |
paul@0 | 717 | # Make sure the name is being tracked: global names will not |
paul@0 | 718 | # already be initialised in a branch and must be added |
paul@0 | 719 | # explicitly. |
paul@0 | 720 | |
paul@0 | 721 | if not tracker.have_name(name): |
paul@0 | 722 | tracker.assign_names([name]) |
paul@0 | 723 | if self.in_function: |
paul@0 | 724 | self.scope_globals[path].add(name) |
paul@0 | 725 | |
paul@0 | 726 | # Record attribute usage in the tracker, and record the branch |
paul@0 | 727 | # information for the access. |
paul@0 | 728 | |
paul@0 | 729 | branches = tracker.use_attribute(name, attrname) |
paul@0 | 730 | |
paul@0 | 731 | if not branches: |
paul@0 | 732 | print >>sys.stderr, "In %s, name %s is accessed using %s before an assignment." % ( |
paul@0 | 733 | path, name, attrname) |
paul@0 | 734 | branches = tracker.use_attribute(name, attrname) |
paul@0 | 735 | |
paul@0 | 736 | self.record_branches_for_access(branches, name, attrnames) |
paul@0 | 737 | access_number = self.record_access_details(name, attrnames, assignment) |
paul@0 | 738 | return AccessRef(name, attrnames, access_number) |
paul@0 | 739 | |
paul@0 | 740 | def process_class_node(self, n): |
paul@0 | 741 | |
paul@0 | 742 | "Process the given class node 'n'." |
paul@0 | 743 | |
paul@0 | 744 | path = self.get_namespace_path() |
paul@0 | 745 | |
paul@0 | 746 | # To avoid notions of class "versions" where the same definition |
paul@0 | 747 | # might be parameterised with different state and be referenced |
paul@0 | 748 | # elsewhere (as base classes, for example), classes in functions or |
paul@0 | 749 | # conditions are forbidden. |
paul@0 | 750 | |
paul@0 | 751 | if self.in_function or self.in_conditional: |
paul@0 | 752 | print >>sys.stderr, "In %s, class %s in function or conditional statement ignored." % ( |
paul@0 | 753 | path, n.name) |
paul@0 | 754 | return |
paul@0 | 755 | |
paul@0 | 756 | # Resolve base classes. |
paul@0 | 757 | |
paul@0 | 758 | bases = [] |
paul@0 | 759 | |
paul@0 | 760 | for base in n.bases: |
paul@0 | 761 | base_class = self.get_class(base) |
paul@0 | 762 | |
paul@0 | 763 | if not base_class: |
paul@0 | 764 | print >>sys.stderr, "In %s, class %s has unidentifiable base classes." % ( |
paul@0 | 765 | path, n.name) |
paul@0 | 766 | return |
paul@0 | 767 | else: |
paul@0 | 768 | bases.append(base_class) |
paul@0 | 769 | |
paul@0 | 770 | # Record bases for the class and retain the class name. |
paul@0 | 771 | |
paul@0 | 772 | class_name = self.get_object_path(n.name) |
paul@0 | 773 | |
paul@0 | 774 | if not bases and class_name != "__builtins__.core.object": |
paul@0 | 775 | ref = self.get_object("__builtins__.object") |
paul@0 | 776 | bases.append(ref) |
paul@0 | 777 | |
paul@0 | 778 | self.importer.classes[class_name] = self.classes[class_name] = bases |
paul@0 | 779 | self.importer.subclasses[class_name] = set() |
paul@0 | 780 | self.scope_globals[class_name] = set() |
paul@0 | 781 | |
paul@0 | 782 | # Set the definition before entering the namespace rather than |
paul@0 | 783 | # afterwards because methods may reference it. In normal Python, |
paul@0 | 784 | # a class is not accessible until the definition is complete, but |
paul@0 | 785 | # methods can generally reference it since upon being called the |
paul@0 | 786 | # class will already exist. |
paul@0 | 787 | |
paul@0 | 788 | self.set_definition(n.name, "<class>") |
paul@0 | 789 | |
paul@0 | 790 | in_class = self.in_class |
paul@0 | 791 | self.in_class = class_name |
paul@0 | 792 | self.set_instance_attr("__class__", Reference("<class>", class_name)) |
paul@0 | 793 | self.enter_namespace(n.name) |
paul@0 | 794 | self.set_name("__fn__") # special instantiator attribute |
paul@0 | 795 | self.set_name("__args__") # special instantiator attribute |
paul@0 | 796 | self.assign_general_local("__name__", self.get_constant("str", class_name)) |
paul@0 | 797 | self.process_structure_node(n.code) |
paul@0 | 798 | self.exit_namespace() |
paul@0 | 799 | self.in_class = in_class |
paul@0 | 800 | |
paul@0 | 801 | def process_from_node(self, n): |
paul@0 | 802 | |
paul@0 | 803 | "Process the given node 'n', importing from another module." |
paul@0 | 804 | |
paul@0 | 805 | path = self.get_namespace_path() |
paul@0 | 806 | |
paul@0 | 807 | modname, names = self.get_module_name(n) |
paul@0 | 808 | |
paul@0 | 809 | # Load the mentioned module. |
paul@0 | 810 | |
paul@0 | 811 | top, module = self.get_module(modname, True) |
paul@0 | 812 | self.set_module(None, module, hidden=True) |
paul@0 | 813 | |
paul@0 | 814 | if not module: |
paul@0 | 815 | print >>sys.stderr, "In %s, from statement importing from %s failed." % ( |
paul@0 | 816 | path, modname) |
paul@0 | 817 | |
paul@0 | 818 | # Attempt to obtain the referenced objects. |
paul@0 | 819 | |
paul@0 | 820 | for name, alias in n.names: |
paul@0 | 821 | |
paul@0 | 822 | # NOTE: Package submodules are not implicitly imported. |
paul@0 | 823 | |
paul@0 | 824 | if name == "*": |
paul@0 | 825 | if module: |
paul@0 | 826 | |
paul@0 | 827 | # Warn about a circular import that probably doesn't find |
paul@0 | 828 | # the names. |
paul@0 | 829 | |
paul@0 | 830 | if not module.loaded: |
paul@0 | 831 | print >>sys.stderr, "In %s, from statement performs circular import %s of %s." % ( |
paul@0 | 832 | path, modname) |
paul@0 | 833 | |
paul@0 | 834 | for name, value in module.get_globals().items(): |
paul@0 | 835 | if name != "__name__": |
paul@0 | 836 | value = ResolvedNameRef(name, value) |
paul@0 | 837 | self.set_general_local(name, value) |
paul@0 | 838 | self.set_imported_name(name, modname) |
paul@0 | 839 | break |
paul@0 | 840 | |
paul@0 | 841 | # Explicit names. |
paul@0 | 842 | |
paul@0 | 843 | ref = self.import_name_from_module(name, modname, module, alias) |
paul@0 | 844 | value = ResolvedNameRef(alias or name, ref) |
paul@0 | 845 | self.set_general_local(alias or name, value) |
paul@0 | 846 | self.set_imported_name(name, modname, alias) |
paul@0 | 847 | |
paul@0 | 848 | def import_name_from_module(self, name, modname, module, alias=None): |
paul@0 | 849 | |
paul@0 | 850 | """ |
paul@0 | 851 | Import 'name' from the module having the given 'modname', with 'module' |
paul@0 | 852 | having been obtained for the module name, using 'alias' for the imported |
paul@0 | 853 | name in the current namespace. |
paul@0 | 854 | """ |
paul@0 | 855 | |
paul@0 | 856 | path = self.get_namespace_path() |
paul@0 | 857 | |
paul@0 | 858 | if module and module.get_global(name): |
paul@0 | 859 | value = module.get_global(name) |
paul@0 | 860 | |
paul@0 | 861 | # Warn about an import that fails to provide a name, perhaps due |
paul@0 | 862 | # to a circular import. |
paul@0 | 863 | |
paul@0 | 864 | if not value: |
paul@0 | 865 | print >>sys.stderr, "In %s, from statement cannot import %s from %s%s." % ( |
paul@0 | 866 | path, name, modname, not module.loaded and "(circular import)") |
paul@0 | 867 | |
paul@0 | 868 | return value |
paul@0 | 869 | |
paul@0 | 870 | # Record the name as a dependency. |
paul@0 | 871 | |
paul@0 | 872 | else: |
paul@0 | 873 | return Reference("<depends>", "%s.%s" % (modname, name)) |
paul@0 | 874 | |
paul@0 | 875 | def process_function_node(self, n, name): |
paul@0 | 876 | |
paul@0 | 877 | """ |
paul@0 | 878 | Process the given function or lambda node 'n' with the given 'name'. |
paul@0 | 879 | """ |
paul@0 | 880 | |
paul@0 | 881 | is_lambda = isinstance(n, compiler.ast.Lambda) |
paul@0 | 882 | |
paul@0 | 883 | # Where a function is declared conditionally, use a separate name for |
paul@0 | 884 | # the definition, and assign the definition to the stated name. |
paul@0 | 885 | |
paul@0 | 886 | if (self.in_conditional or self.in_function) and not is_lambda: |
paul@0 | 887 | original_name = name |
paul@0 | 888 | name = self.get_lambda_name() |
paul@0 | 889 | else: |
paul@0 | 890 | original_name = None |
paul@0 | 891 | |
paul@0 | 892 | # Initialise argument and local records. |
paul@0 | 893 | |
paul@0 | 894 | function_name = self.get_object_path(name) |
paul@0 | 895 | |
paul@0 | 896 | argnames = self.importer.function_parameters[function_name] = \ |
paul@0 | 897 | self.function_parameters[function_name] = get_argnames(n.argnames) |
paul@0 | 898 | locals = self.function_locals[function_name] = {} |
paul@0 | 899 | |
paul@0 | 900 | for argname in argnames: |
paul@0 | 901 | locals[argname] = Reference("<var>") |
paul@0 | 902 | |
paul@0 | 903 | globals = self.scope_globals[function_name] = set() |
paul@0 | 904 | |
paul@0 | 905 | # Process the defaults. |
paul@0 | 906 | |
paul@0 | 907 | defaults = self.importer.function_defaults[function_name] = \ |
paul@0 | 908 | self.function_defaults[function_name] = [] |
paul@0 | 909 | |
paul@0 | 910 | for argname, default in compiler.ast.get_defaults(n): |
paul@0 | 911 | if default: |
paul@0 | 912 | |
paul@0 | 913 | # Obtain any reference for the default. |
paul@0 | 914 | |
paul@0 | 915 | name_ref = self.process_structure_node(default) |
paul@0 | 916 | defaults.append((argname, name_ref.is_name() and name_ref.reference() or Reference("<var>"))) |
paul@0 | 917 | |
paul@0 | 918 | # Reset conditional tracking to focus on the function contents. |
paul@0 | 919 | |
paul@0 | 920 | parent_function = self.parent_function |
paul@0 | 921 | self.parent_function = self.in_function and self.get_namespace_path() or None |
paul@0 | 922 | |
paul@0 | 923 | in_conditional = self.in_conditional |
paul@0 | 924 | self.in_conditional = False |
paul@0 | 925 | |
paul@0 | 926 | in_function = self.in_function |
paul@0 | 927 | self.in_function = function_name |
paul@0 | 928 | |
paul@0 | 929 | self.enter_namespace(name) |
paul@0 | 930 | |
paul@0 | 931 | # Track attribute usage within the namespace. |
paul@0 | 932 | |
paul@0 | 933 | path = self.get_namespace_path() |
paul@0 | 934 | init_item(self.propagated_names, path, set) |
paul@0 | 935 | |
paul@0 | 936 | self.start_tracking(locals) |
paul@0 | 937 | self.process_structure_node(n.code) |
paul@0 | 938 | self.stop_tracking() |
paul@0 | 939 | |
paul@0 | 940 | # Propagate names from parent scopes. |
paul@0 | 941 | |
paul@0 | 942 | for local in self.propagated_names[path]: |
paul@0 | 943 | if not local in argnames and self.trackers[-1].have_name(local): |
paul@0 | 944 | argnames.append(local) |
paul@0 | 945 | defaults.append((local, Reference("<var>"))) |
paul@0 | 946 | self.set_function_local(local) |
paul@0 | 947 | |
paul@0 | 948 | # Exit to the parent and note propagated names. |
paul@0 | 949 | |
paul@0 | 950 | self.exit_namespace() |
paul@0 | 951 | |
paul@0 | 952 | parent = self.get_namespace_path() |
paul@0 | 953 | if self.propagated_names.has_key(parent): |
paul@0 | 954 | for local in self.propagated_names[path]: |
paul@0 | 955 | self.propagated_names[parent].add(local) |
paul@0 | 956 | |
paul@0 | 957 | # Update flags. |
paul@0 | 958 | |
paul@0 | 959 | self.in_function = in_function |
paul@0 | 960 | self.in_conditional = in_conditional |
paul@0 | 961 | self.parent_function = parent_function |
paul@0 | 962 | |
paul@0 | 963 | # Define the function using the appropriate name. |
paul@0 | 964 | |
paul@0 | 965 | self.set_definition(name, "<function>") |
paul@0 | 966 | |
paul@0 | 967 | # Where a function is set conditionally, assign the name. |
paul@0 | 968 | |
paul@0 | 969 | if original_name: |
paul@0 | 970 | self.process_assignment_for_function(original_name, name) |
paul@0 | 971 | |
paul@0 | 972 | def process_global_node(self, n): |
paul@0 | 973 | |
paul@0 | 974 | """ |
paul@0 | 975 | Process the given "global" node 'n'. |
paul@0 | 976 | """ |
paul@0 | 977 | |
paul@0 | 978 | path = self.get_namespace_path() |
paul@0 | 979 | |
paul@0 | 980 | if path != self.name: |
paul@0 | 981 | self.scope_globals[path].update(n.names) |
paul@0 | 982 | |
paul@0 | 983 | def process_if_node(self, n): |
paul@0 | 984 | |
paul@0 | 985 | """ |
paul@0 | 986 | Process the given "if" node 'n'. |
paul@0 | 987 | """ |
paul@0 | 988 | |
paul@0 | 989 | tracker = self.trackers[-1] |
paul@0 | 990 | tracker.new_branchpoint() |
paul@0 | 991 | |
paul@0 | 992 | for test, body in n.tests: |
paul@0 | 993 | self.process_structure_node(test) |
paul@0 | 994 | |
paul@0 | 995 | tracker.new_branch() |
paul@0 | 996 | |
paul@0 | 997 | in_conditional = self.in_conditional |
paul@0 | 998 | self.in_conditional = True |
paul@0 | 999 | self.process_structure_node(body) |
paul@0 | 1000 | self.in_conditional = in_conditional |
paul@0 | 1001 | |
paul@0 | 1002 | tracker.shelve_branch() |
paul@0 | 1003 | |
paul@0 | 1004 | # Maintain a branch for the else clause. |
paul@0 | 1005 | |
paul@0 | 1006 | tracker.new_branch() |
paul@0 | 1007 | if n.else_: |
paul@0 | 1008 | self.process_structure_node(n.else_) |
paul@0 | 1009 | tracker.shelve_branch() |
paul@0 | 1010 | |
paul@0 | 1011 | tracker.merge_branches() |
paul@0 | 1012 | |
paul@0 | 1013 | def process_ifexp_node(self, n): |
paul@0 | 1014 | |
paul@0 | 1015 | "Process the given if expression node 'n'." |
paul@0 | 1016 | |
paul@0 | 1017 | name_ref = self.process_structure_node(self.convert_ifexp_node(n)) |
paul@0 | 1018 | |
paul@0 | 1019 | path = self.get_namespace_path() |
paul@0 | 1020 | self.allocate_arguments(path, self.function_defaults[name_ref.get_origin()]) |
paul@0 | 1021 | self.deallocate_arguments(path, self.function_defaults[name_ref.get_origin()]) |
paul@0 | 1022 | |
paul@0 | 1023 | return InvocationRef(name_ref) |
paul@0 | 1024 | |
paul@0 | 1025 | def process_import_node(self, n): |
paul@0 | 1026 | |
paul@0 | 1027 | "Process the given import node 'n'." |
paul@0 | 1028 | |
paul@0 | 1029 | path = self.get_namespace_path() |
paul@0 | 1030 | |
paul@0 | 1031 | # Load the mentioned module. |
paul@0 | 1032 | |
paul@0 | 1033 | for name, alias in n.names: |
paul@0 | 1034 | module, leaf_module = self.get_module(name, alias) |
paul@0 | 1035 | |
paul@0 | 1036 | if not module: |
paul@0 | 1037 | print >>sys.stderr, "In %s, import statement importing from %s failed." % ( |
paul@0 | 1038 | path, name) |
paul@0 | 1039 | if module and not module.loaded: |
paul@0 | 1040 | print >>sys.stderr, "In %s, import statement performs circular import of %s." % ( |
paul@0 | 1041 | path, name) |
paul@0 | 1042 | |
paul@0 | 1043 | self.set_module(alias or name.split(".")[0], module, leaf_module) |
paul@0 | 1044 | |
paul@0 | 1045 | def process_invocation_node(self, n): |
paul@0 | 1046 | |
paul@0 | 1047 | "Process the given invocation node 'n'." |
paul@0 | 1048 | |
paul@0 | 1049 | path = self.get_namespace_path() |
paul@0 | 1050 | |
paul@0 | 1051 | self.allocate_arguments(path, n.args) |
paul@0 | 1052 | |
paul@0 | 1053 | try: |
paul@0 | 1054 | # Process the expression, obtaining any identified reference. |
paul@0 | 1055 | |
paul@0 | 1056 | name_ref = self.process_structure_node(n.node) |
paul@0 | 1057 | |
paul@0 | 1058 | # Process the arguments. |
paul@0 | 1059 | |
paul@0 | 1060 | for arg in n.args: |
paul@0 | 1061 | self.process_structure_node(arg) |
paul@0 | 1062 | |
paul@0 | 1063 | # Detect class invocations. |
paul@0 | 1064 | |
paul@0 | 1065 | if isinstance(name_ref, ResolvedNameRef) and name_ref.has_kind("<class>"): |
paul@0 | 1066 | return InstanceRef(name_ref.reference().instance_of()) |
paul@0 | 1067 | |
paul@0 | 1068 | elif isinstance(name_ref, NameRef): |
paul@0 | 1069 | return InvocationRef(name_ref) |
paul@0 | 1070 | |
paul@0 | 1071 | return None |
paul@0 | 1072 | |
paul@0 | 1073 | finally: |
paul@0 | 1074 | self.deallocate_arguments(path, n.args) |
paul@0 | 1075 | |
paul@0 | 1076 | def process_lambda_node(self, n): |
paul@0 | 1077 | |
paul@0 | 1078 | "Process the given lambda node 'n'." |
paul@0 | 1079 | |
paul@0 | 1080 | name = self.get_lambda_name() |
paul@0 | 1081 | self.process_function_node(n, name) |
paul@0 | 1082 | |
paul@0 | 1083 | origin = self.get_object_path(name) |
paul@0 | 1084 | return ResolvedNameRef(name, Reference("<function>", origin)) |
paul@0 | 1085 | |
paul@0 | 1086 | def process_listcomp_node(self, n): |
paul@0 | 1087 | |
paul@0 | 1088 | "Process the given list comprehension node 'n'." |
paul@0 | 1089 | |
paul@0 | 1090 | name_ref = self.process_structure_node(self.convert_listcomp_node(n)) |
paul@0 | 1091 | |
paul@0 | 1092 | path = self.get_namespace_path() |
paul@0 | 1093 | self.allocate_arguments(path, self.function_defaults[name_ref.get_origin()]) |
paul@0 | 1094 | self.deallocate_arguments(path, self.function_defaults[name_ref.get_origin()]) |
paul@0 | 1095 | |
paul@0 | 1096 | return InvocationRef(name_ref) |
paul@0 | 1097 | |
paul@0 | 1098 | def process_logical_node(self, n): |
paul@0 | 1099 | |
paul@0 | 1100 | "Process the given operator node 'n'." |
paul@0 | 1101 | |
paul@0 | 1102 | self.process_operator_chain(n.nodes, self.process_structure_node) |
paul@0 | 1103 | |
paul@0 | 1104 | def process_name_node(self, n): |
paul@0 | 1105 | |
paul@0 | 1106 | "Process the given name node 'n'." |
paul@0 | 1107 | |
paul@0 | 1108 | path = self.get_namespace_path() |
paul@0 | 1109 | |
paul@0 | 1110 | # Special names. |
paul@0 | 1111 | |
paul@0 | 1112 | if n.name.startswith("$"): |
paul@0 | 1113 | value = self.get_special(n.name) |
paul@0 | 1114 | if value: |
paul@0 | 1115 | return value |
paul@0 | 1116 | |
paul@0 | 1117 | # Special case for operator functions introduced through code |
paul@0 | 1118 | # transformations. |
paul@0 | 1119 | |
paul@0 | 1120 | if n.name.startswith("$op"): |
paul@0 | 1121 | |
paul@0 | 1122 | # Obtain the location of the actual function defined in the operator |
paul@0 | 1123 | # package. |
paul@0 | 1124 | |
paul@0 | 1125 | op = n.name[len("$op"):] |
paul@0 | 1126 | |
paul@0 | 1127 | # Access the operator module. |
paul@0 | 1128 | |
paul@0 | 1129 | top, module = self.get_module("operator", True) |
paul@0 | 1130 | self.set_module(None, module, hidden=True) |
paul@0 | 1131 | |
paul@0 | 1132 | # Link the operation to the operator module definition in this |
paul@0 | 1133 | # module. |
paul@0 | 1134 | |
paul@0 | 1135 | self.set_imported_name(op, "operator", n.name, self.name) |
paul@0 | 1136 | |
paul@0 | 1137 | # Attempt to get a reference. |
paul@0 | 1138 | |
paul@0 | 1139 | ref = self.import_name_from_module(op, "operator", module) |
paul@0 | 1140 | ref = self.get_object("operator.%s" % op) or ref |
paul@0 | 1141 | |
paul@0 | 1142 | # Record the imported name and provide the resolved name reference. |
paul@0 | 1143 | |
paul@0 | 1144 | value = ResolvedNameRef(n.name, ref) |
paul@0 | 1145 | self.set_special(n.name, value) |
paul@0 | 1146 | return value |
paul@0 | 1147 | |
paul@0 | 1148 | # Record usage of the name. |
paul@0 | 1149 | |
paul@0 | 1150 | self.record_name(n.name) |
paul@0 | 1151 | |
paul@0 | 1152 | # Search for unknown names in non-function scopes immediately. |
paul@0 | 1153 | # External names in functions are resolved later. |
paul@0 | 1154 | |
paul@0 | 1155 | ref = self.find_name(n.name) |
paul@0 | 1156 | if ref: |
paul@0 | 1157 | return ResolvedNameRef(n.name, ref) |
paul@0 | 1158 | |
paul@0 | 1159 | # Global name. |
paul@0 | 1160 | |
paul@0 | 1161 | elif self.in_function and n.name in self.scope_globals[path]: |
paul@0 | 1162 | return NameRef(n.name) |
paul@0 | 1163 | |
paul@0 | 1164 | # Examine other names. |
paul@0 | 1165 | |
paul@0 | 1166 | else: |
paul@0 | 1167 | tracker = self.trackers[-1] |
paul@0 | 1168 | |
paul@0 | 1169 | # Check local names. |
paul@0 | 1170 | |
paul@0 | 1171 | branches = tracker.tracking_name(n.name) |
paul@0 | 1172 | |
paul@0 | 1173 | # Find names inherited from a parent scope. |
paul@0 | 1174 | |
paul@0 | 1175 | if not branches and self.parent_function: |
paul@0 | 1176 | branches = tracker.have_name(n.name) |
paul@0 | 1177 | if branches: |
paul@0 | 1178 | self.propagate_name(n.name) |
paul@0 | 1179 | |
paul@0 | 1180 | # Local or inherited name. |
paul@0 | 1181 | |
paul@0 | 1182 | if branches: |
paul@0 | 1183 | self.record_branches_for_access(branches, n.name, None) |
paul@0 | 1184 | access_number = self.record_access_details(n.name, None, False) |
paul@0 | 1185 | return LocalNameRef(n.name, access_number) |
paul@0 | 1186 | |
paul@0 | 1187 | # Possible global name. |
paul@0 | 1188 | |
paul@0 | 1189 | else: |
paul@0 | 1190 | return NameRef(n.name) |
paul@0 | 1191 | |
paul@0 | 1192 | def process_operator_chain(self, nodes, fn): |
paul@0 | 1193 | |
paul@0 | 1194 | """ |
paul@0 | 1195 | Process the given chain of 'nodes', applying 'fn' to each node or item. |
paul@0 | 1196 | Each node starts a new conditional region, effectively making a deeply- |
paul@0 | 1197 | nested collection of if-like statements. |
paul@0 | 1198 | """ |
paul@0 | 1199 | |
paul@0 | 1200 | tracker = self.trackers[-1] |
paul@0 | 1201 | |
paul@0 | 1202 | for item in nodes: |
paul@0 | 1203 | tracker.new_branchpoint() |
paul@0 | 1204 | tracker.new_branch() |
paul@0 | 1205 | fn(item) |
paul@0 | 1206 | |
paul@0 | 1207 | for item in nodes[:-1]: |
paul@0 | 1208 | tracker.shelve_branch() |
paul@0 | 1209 | tracker.new_branch() |
paul@0 | 1210 | tracker.shelve_branch() |
paul@0 | 1211 | tracker.merge_branches() |
paul@0 | 1212 | |
paul@0 | 1213 | tracker.shelve_branch() |
paul@0 | 1214 | tracker.merge_branches() |
paul@0 | 1215 | |
paul@0 | 1216 | def process_try_node(self, n): |
paul@0 | 1217 | |
paul@0 | 1218 | """ |
paul@0 | 1219 | Process the given "try...except" node 'n'. |
paul@0 | 1220 | """ |
paul@0 | 1221 | |
paul@0 | 1222 | tracker = self.trackers[-1] |
paul@0 | 1223 | tracker.new_branchpoint() |
paul@0 | 1224 | |
paul@0 | 1225 | self.process_structure_node(n.body) |
paul@0 | 1226 | |
paul@0 | 1227 | for name, var, handler in n.handlers: |
paul@0 | 1228 | if name is not None: |
paul@0 | 1229 | self.process_structure_node(name) |
paul@0 | 1230 | |
paul@0 | 1231 | # Any abandoned branches from the body can now be resumed in a new |
paul@0 | 1232 | # branch. |
paul@0 | 1233 | |
paul@0 | 1234 | tracker.resume_abandoned_branches() |
paul@0 | 1235 | |
paul@0 | 1236 | # Establish the local for the handler. |
paul@0 | 1237 | |
paul@0 | 1238 | if var is not None: |
paul@0 | 1239 | self.process_structure_node(var) |
paul@0 | 1240 | if handler is not None: |
paul@0 | 1241 | self.process_structure_node(handler) |
paul@0 | 1242 | |
paul@0 | 1243 | tracker.shelve_branch() |
paul@0 | 1244 | |
paul@0 | 1245 | # The else clause maintains the usage from the body but without the |
paul@0 | 1246 | # abandoned branches since they would never lead to the else clause |
paul@0 | 1247 | # being executed. |
paul@0 | 1248 | |
paul@0 | 1249 | if n.else_: |
paul@0 | 1250 | tracker.new_branch() |
paul@0 | 1251 | self.process_structure_node(n.else_) |
paul@0 | 1252 | tracker.shelve_branch() |
paul@0 | 1253 | |
paul@0 | 1254 | # Without an else clause, a null branch propagates the successful |
paul@0 | 1255 | # outcome. |
paul@0 | 1256 | |
paul@0 | 1257 | else: |
paul@0 | 1258 | tracker.new_branch() |
paul@0 | 1259 | tracker.shelve_branch() |
paul@0 | 1260 | |
paul@0 | 1261 | tracker.merge_branches() |
paul@0 | 1262 | |
paul@0 | 1263 | def process_try_finally_node(self, n): |
paul@0 | 1264 | |
paul@0 | 1265 | """ |
paul@0 | 1266 | Process the given "try...finally" node 'n'. |
paul@0 | 1267 | """ |
paul@0 | 1268 | |
paul@0 | 1269 | tracker = self.trackers[-1] |
paul@0 | 1270 | self.process_structure_node(n.body) |
paul@0 | 1271 | |
paul@0 | 1272 | # Any abandoned branches from the body can now be resumed. |
paul@0 | 1273 | |
paul@0 | 1274 | branches = tracker.resume_all_abandoned_branches() |
paul@0 | 1275 | self.process_structure_node(n.final) |
paul@0 | 1276 | |
paul@0 | 1277 | # At the end of the finally clause, abandoned branches are discarded. |
paul@0 | 1278 | |
paul@0 | 1279 | tracker.restore_active_branches(branches) |
paul@0 | 1280 | |
paul@0 | 1281 | def process_while_node(self, n): |
paul@0 | 1282 | |
paul@0 | 1283 | "Process the given while node 'n'." |
paul@0 | 1284 | |
paul@0 | 1285 | tracker = self.trackers[-1] |
paul@0 | 1286 | tracker.new_branchpoint(loop_node=True) |
paul@0 | 1287 | |
paul@0 | 1288 | # Evaluate any test or iterator outside the loop. |
paul@0 | 1289 | |
paul@0 | 1290 | self.process_structure_node(n.test) |
paul@0 | 1291 | |
paul@0 | 1292 | # Propagate attribute usage to branches. |
paul@0 | 1293 | |
paul@0 | 1294 | tracker.new_branch(loop_node=True) |
paul@0 | 1295 | |
paul@0 | 1296 | # Enter the loop. |
paul@0 | 1297 | |
paul@0 | 1298 | in_conditional = self.in_conditional |
paul@0 | 1299 | self.in_conditional = True |
paul@0 | 1300 | self.process_structure_node(n.body) |
paul@0 | 1301 | self.in_conditional = in_conditional |
paul@0 | 1302 | |
paul@0 | 1303 | # Continuing branches are resumed before any test. |
paul@0 | 1304 | |
paul@0 | 1305 | tracker.resume_continuing_branches() |
paul@0 | 1306 | |
paul@0 | 1307 | # Evaluate any continuation test within the body. |
paul@0 | 1308 | |
paul@0 | 1309 | self.process_structure_node(n.test) |
paul@0 | 1310 | |
paul@0 | 1311 | tracker.shelve_branch(loop_node=True) |
paul@0 | 1312 | |
paul@0 | 1313 | # Support the non-looping condition. |
paul@0 | 1314 | |
paul@0 | 1315 | tracker.new_branch() |
paul@0 | 1316 | tracker.shelve_branch() |
paul@0 | 1317 | |
paul@0 | 1318 | tracker.merge_branches() |
paul@0 | 1319 | |
paul@0 | 1320 | # Evaluate any else clause outside branches. |
paul@0 | 1321 | |
paul@0 | 1322 | if n.else_: |
paul@0 | 1323 | self.process_structure_node(n.else_) |
paul@0 | 1324 | |
paul@0 | 1325 | # Connect broken branches to the code after any loop. |
paul@0 | 1326 | |
paul@0 | 1327 | tracker.resume_broken_branches() |
paul@0 | 1328 | |
paul@0 | 1329 | # Branch tracking methods. |
paul@0 | 1330 | |
paul@0 | 1331 | def start_tracking(self, names): |
paul@0 | 1332 | |
paul@0 | 1333 | """ |
paul@0 | 1334 | Start tracking attribute usage for names in the current namespace, |
paul@0 | 1335 | immediately registering the given 'names'. |
paul@0 | 1336 | """ |
paul@0 | 1337 | |
paul@0 | 1338 | path = self.get_namespace_path() |
paul@0 | 1339 | parent = self.trackers[-1] |
paul@0 | 1340 | tracker = BranchTracker() |
paul@0 | 1341 | self.trackers.append(tracker) |
paul@0 | 1342 | |
paul@0 | 1343 | # For functions created from expressions or for functions within |
paul@0 | 1344 | # functions, propagate usage to the new namespace. |
paul@0 | 1345 | |
paul@0 | 1346 | if self.parent_function: |
paul@0 | 1347 | tracker.inherit_branches(parent, names) |
paul@0 | 1348 | |
paul@0 | 1349 | # Record the given names established as new branches. |
paul@0 | 1350 | |
paul@0 | 1351 | tracker.assign_names(names) |
paul@0 | 1352 | |
paul@0 | 1353 | def assign_name(self, name, name_ref): |
paul@0 | 1354 | |
paul@0 | 1355 | "Assign to 'name' the given 'name_ref' in the current namespace." |
paul@0 | 1356 | |
paul@0 | 1357 | name = self.get_name_for_tracking(name) |
paul@0 | 1358 | self.trackers[-1].assign_names([name], [name_ref]) |
paul@0 | 1359 | |
paul@0 | 1360 | def stop_tracking(self): |
paul@0 | 1361 | |
paul@0 | 1362 | """ |
paul@0 | 1363 | Stop tracking attribute usage, recording computed usage for the current |
paul@0 | 1364 | namespace. |
paul@0 | 1365 | """ |
paul@0 | 1366 | |
paul@0 | 1367 | path = self.get_namespace_path() |
paul@0 | 1368 | tracker = self.trackers.pop() |
paul@0 | 1369 | self.record_assignments_for_access(tracker) |
paul@0 | 1370 | |
paul@0 | 1371 | self.attr_usage[path] = tracker.get_all_usage() |
paul@0 | 1372 | self.name_initialisers[path] = tracker.get_all_values() |
paul@0 | 1373 | |
paul@0 | 1374 | def start_tracking_in_module(self): |
paul@0 | 1375 | |
paul@0 | 1376 | "Start tracking attribute usage in the module." |
paul@0 | 1377 | |
paul@0 | 1378 | tracker = BranchTracker() |
paul@0 | 1379 | self.trackers.append(tracker) |
paul@0 | 1380 | |
paul@0 | 1381 | def stop_tracking_in_module(self): |
paul@0 | 1382 | |
paul@0 | 1383 | "Stop tracking attribute usage in the module." |
paul@0 | 1384 | |
paul@0 | 1385 | tracker = self.trackers[0] |
paul@0 | 1386 | self.record_assignments_for_access(tracker) |
paul@0 | 1387 | self.attr_usage[self.name] = tracker.get_all_usage() |
paul@0 | 1388 | self.name_initialisers[self.name] = tracker.get_all_values() |
paul@0 | 1389 | |
paul@0 | 1390 | def propagate_name(self, name): |
paul@0 | 1391 | |
paul@0 | 1392 | "Propagate the given 'name' into the current namespace." |
paul@0 | 1393 | |
paul@0 | 1394 | path = self.get_namespace_path() |
paul@0 | 1395 | self.propagated_names[path].add(name) |
paul@0 | 1396 | |
paul@0 | 1397 | def record_assignments_for_access(self, tracker): |
paul@0 | 1398 | |
paul@0 | 1399 | """ |
paul@0 | 1400 | For the current path, use the given 'tracker' to record assignment |
paul@0 | 1401 | version information for attribute accesses. |
paul@0 | 1402 | """ |
paul@0 | 1403 | |
paul@0 | 1404 | path = self.get_path_for_access() |
paul@0 | 1405 | |
paul@0 | 1406 | if not self.attr_accessor_branches.has_key(path): |
paul@0 | 1407 | return |
paul@0 | 1408 | |
paul@0 | 1409 | init_item(self.attr_accessors, path, dict) |
paul@0 | 1410 | attr_accessors = self.attr_accessors[path] |
paul@0 | 1411 | |
paul@0 | 1412 | # Obtain the branches applying during each access. |
paul@0 | 1413 | |
paul@0 | 1414 | for access, all_branches in self.attr_accessor_branches[path].items(): |
paul@0 | 1415 | name, attrnames = access |
paul@0 | 1416 | init_item(attr_accessors, access, list) |
paul@0 | 1417 | |
paul@0 | 1418 | # Obtain the assignments applying to each branch. |
paul@0 | 1419 | |
paul@0 | 1420 | for branches in all_branches: |
paul@0 | 1421 | positions = tracker.get_assignment_positions_for_branches(name, branches) |
paul@0 | 1422 | |
paul@0 | 1423 | # Detect missing name information. |
paul@0 | 1424 | |
paul@0 | 1425 | if None in positions: |
paul@0 | 1426 | globals = self.global_attr_accesses.get(path) |
paul@0 | 1427 | accesses = globals and globals.get(name) |
paul@0 | 1428 | if not accesses: |
paul@0 | 1429 | print >>sys.stderr, "In %s, %s may not be defined when used." % ( |
paul@0 | 1430 | self.get_namespace_path(), name) |
paul@0 | 1431 | positions.remove(None) |
paul@0 | 1432 | |
paul@0 | 1433 | attr_accessors[access].append(positions) |
paul@0 | 1434 | |
paul@0 | 1435 | def record_branches_for_access(self, branches, name, attrnames): |
paul@0 | 1436 | |
paul@0 | 1437 | """ |
paul@0 | 1438 | Record the given 'branches' for an access involving the given 'name' and |
paul@0 | 1439 | 'attrnames'. |
paul@0 | 1440 | """ |
paul@0 | 1441 | |
paul@0 | 1442 | access = name, attrnames |
paul@0 | 1443 | path = self.get_path_for_access() |
paul@0 | 1444 | |
paul@0 | 1445 | init_item(self.attr_accessor_branches, path, dict) |
paul@0 | 1446 | attr_accessor_branches = self.attr_accessor_branches[path] |
paul@0 | 1447 | |
paul@0 | 1448 | init_item(attr_accessor_branches, access, list) |
paul@0 | 1449 | attr_accessor_branches[access].append(branches) |
paul@0 | 1450 | |
paul@0 | 1451 | def record_access_details(self, name, attrnames, assignment): |
paul@0 | 1452 | |
paul@0 | 1453 | """ |
paul@0 | 1454 | For the given 'name' and 'attrnames', record an access indicating |
paul@0 | 1455 | whether 'assignment' is occurring. |
paul@0 | 1456 | |
paul@0 | 1457 | These details correspond to accesses otherwise recorded by the attribute |
paul@0 | 1458 | accessor and attribute access dictionaries. |
paul@0 | 1459 | """ |
paul@0 | 1460 | |
paul@0 | 1461 | access = name, attrnames |
paul@0 | 1462 | path = self.get_path_for_access() |
paul@0 | 1463 | |
paul@0 | 1464 | init_item(self.attr_access_modifiers, path, dict) |
paul@0 | 1465 | init_item(self.attr_access_modifiers[path], access, list) |
paul@0 | 1466 | |
paul@0 | 1467 | access_number = len(self.attr_access_modifiers[path][access]) |
paul@0 | 1468 | self.attr_access_modifiers[path][access].append(assignment) |
paul@0 | 1469 | return access_number |
paul@0 | 1470 | |
paul@0 | 1471 | def record_global_access_details(self, name, attrnames): |
paul@0 | 1472 | |
paul@0 | 1473 | """ |
paul@0 | 1474 | Record details of a global access via the given 'name' involving the |
paul@0 | 1475 | indicated 'attrnames'. |
paul@0 | 1476 | """ |
paul@0 | 1477 | |
paul@0 | 1478 | path = self.get_namespace_path() |
paul@0 | 1479 | |
paul@0 | 1480 | init_item(self.global_attr_accesses, path, dict) |
paul@0 | 1481 | init_item(self.global_attr_accesses[path], name, set) |
paul@0 | 1482 | self.global_attr_accesses[path][name].add(attrnames) |
paul@0 | 1483 | |
paul@0 | 1484 | # Namespace modification. |
paul@0 | 1485 | |
paul@0 | 1486 | def record_name(self, name): |
paul@0 | 1487 | |
paul@0 | 1488 | "Record the use of 'name' in a namespace." |
paul@0 | 1489 | |
paul@0 | 1490 | path = self.get_namespace_path() |
paul@0 | 1491 | init_item(self.names_used, path, set) |
paul@0 | 1492 | self.names_used[path].add(name) |
paul@0 | 1493 | |
paul@0 | 1494 | def set_module(self, name, module, leaf_module=None, hidden=False): |
paul@0 | 1495 | |
paul@0 | 1496 | """ |
paul@0 | 1497 | Set a module in the current namespace using the given 'name' and |
paul@0 | 1498 | corresponding 'module' object, with the 'leaf_module' being recorded |
paul@0 | 1499 | if different. If 'hidden' is a true value, the modules are recorded as |
paul@0 | 1500 | not necessarily being exposed by this module. This module is, however, |
paul@0 | 1501 | recorded as accessing the given modules and is thus dependent on them. |
paul@0 | 1502 | """ |
paul@0 | 1503 | |
paul@0 | 1504 | if name: |
paul@0 | 1505 | self.set_general_local(name, module and Reference("<module>", module.name) or None) |
paul@0 | 1506 | if module: |
paul@0 | 1507 | if hidden: |
paul@0 | 1508 | self.imported_hidden.add(module) |
paul@0 | 1509 | if leaf_module and leaf_module is not module: |
paul@0 | 1510 | self.imported_hidden.add(leaf_module) |
paul@0 | 1511 | else: |
paul@0 | 1512 | self.imported.add(module) |
paul@0 | 1513 | module.accessing_modules.add(self.name) |
paul@0 | 1514 | if leaf_module and leaf_module is not module: |
paul@0 | 1515 | self.imported.add(leaf_module) |
paul@0 | 1516 | leaf_module.accessing_modules.add(self.name) |
paul@0 | 1517 | |
paul@0 | 1518 | def set_definition(self, name, kind): |
paul@0 | 1519 | |
paul@0 | 1520 | """ |
paul@0 | 1521 | Set the definition having the given 'name' and 'kind'. |
paul@0 | 1522 | |
paul@0 | 1523 | Definitions are set in the static namespace hierarchy, but they can also |
paul@0 | 1524 | be recorded for function locals. |
paul@0 | 1525 | """ |
paul@0 | 1526 | |
paul@0 | 1527 | if self.is_global(name): |
paul@0 | 1528 | print >>sys.stderr, "In %s, %s is defined as being global." % ( |
paul@0 | 1529 | self.get_namespace_path(), name) |
paul@0 | 1530 | |
paul@0 | 1531 | path = self.get_object_path(name) |
paul@0 | 1532 | self.set_object(path, kind) |
paul@0 | 1533 | |
paul@0 | 1534 | ref = self.get_object(path) |
paul@0 | 1535 | if ref.get_kind() == "<var>": |
paul@0 | 1536 | print >>sys.stderr, "In %s, %s is defined more than once." % ( |
paul@0 | 1537 | self.get_namespace_path(), name) |
paul@0 | 1538 | |
paul@0 | 1539 | if not self.is_global(name) and self.in_function: |
paul@0 | 1540 | self.set_function_local(name, ref) |
paul@0 | 1541 | |
paul@0 | 1542 | def set_function_local(self, name, ref=None): |
paul@0 | 1543 | |
paul@0 | 1544 | "Set the local with the given 'name' and optional 'ref'." |
paul@0 | 1545 | |
paul@0 | 1546 | locals = self.function_locals[self.get_namespace_path()] |
paul@0 | 1547 | multiple = not ref or locals.has_key(name) and locals[name] != ref |
paul@0 | 1548 | locals[name] = multiple and Reference("<var>") or ref |
paul@0 | 1549 | |
paul@0 | 1550 | def assign_general_local(self, name, name_ref): |
paul@0 | 1551 | |
paul@0 | 1552 | """ |
paul@0 | 1553 | Set for 'name' the given 'name_ref', recording the name for attribute |
paul@0 | 1554 | usage tracking. |
paul@0 | 1555 | """ |
paul@0 | 1556 | |
paul@0 | 1557 | self.set_general_local(name, name_ref) |
paul@0 | 1558 | self.assign_name(name, name_ref) |
paul@0 | 1559 | |
paul@0 | 1560 | def set_general_local(self, name, value=None): |
paul@0 | 1561 | |
paul@0 | 1562 | """ |
paul@0 | 1563 | Set the 'name' with optional 'value' in any kind of local namespace, |
paul@0 | 1564 | where the 'value' should be a reference if specified. |
paul@0 | 1565 | """ |
paul@0 | 1566 | |
paul@0 | 1567 | init_value = self.get_initialising_value(value) |
paul@0 | 1568 | |
paul@0 | 1569 | # Module global names. |
paul@0 | 1570 | |
paul@0 | 1571 | if self.is_global(name): |
paul@0 | 1572 | path = self.get_global_path(name) |
paul@0 | 1573 | self.set_object(path, init_value) |
paul@0 | 1574 | |
paul@0 | 1575 | # Function local names. |
paul@0 | 1576 | |
paul@0 | 1577 | elif self.in_function: |
paul@0 | 1578 | path = self.get_object_path(name) |
paul@0 | 1579 | self.set_function_local(name, init_value) |
paul@0 | 1580 | |
paul@0 | 1581 | # Other namespaces (classes). |
paul@0 | 1582 | |
paul@0 | 1583 | else: |
paul@0 | 1584 | path = self.get_object_path(name) |
paul@0 | 1585 | self.set_name(name, init_value) |
paul@0 | 1586 | |
paul@0 | 1587 | def set_name(self, name, ref=None): |
paul@0 | 1588 | |
paul@0 | 1589 | "Attach the 'name' with optional 'ref' to the current namespace." |
paul@0 | 1590 | |
paul@0 | 1591 | self.set_object(self.get_object_path(name), ref) |
paul@0 | 1592 | |
paul@0 | 1593 | def set_instance_attr(self, name, ref=None): |
paul@0 | 1594 | |
paul@0 | 1595 | """ |
paul@0 | 1596 | Add an instance attribute of the given 'name' to the current class, |
paul@0 | 1597 | using the optional 'ref'. |
paul@0 | 1598 | """ |
paul@0 | 1599 | |
paul@0 | 1600 | init_item(self.instance_attrs, self.in_class, set) |
paul@0 | 1601 | self.instance_attrs[self.in_class].add(name) |
paul@0 | 1602 | |
paul@0 | 1603 | if ref: |
paul@0 | 1604 | init_item(self.instance_attr_constants, self.in_class, dict) |
paul@0 | 1605 | self.instance_attr_constants[self.in_class][name] = ref |
paul@0 | 1606 | |
paul@0 | 1607 | def get_initialising_value(self, value): |
paul@0 | 1608 | |
paul@0 | 1609 | "Return a suitable initialiser reference for 'value'." |
paul@0 | 1610 | |
paul@0 | 1611 | if isinstance(value, (NameRef, AccessRef, InstanceRef)): # includes LiteralSequenceRef, ResolvedNameRef |
paul@0 | 1612 | return value.reference() |
paul@0 | 1613 | |
paul@0 | 1614 | # In general, invocations do not produce known results. However, the |
paul@0 | 1615 | # name initialisers are resolved once a module has been inspected. |
paul@0 | 1616 | |
paul@0 | 1617 | elif isinstance(value, InvocationRef): |
paul@0 | 1618 | return None |
paul@0 | 1619 | |
paul@0 | 1620 | else: |
paul@0 | 1621 | return value |
paul@0 | 1622 | |
paul@0 | 1623 | # Static, program-relative naming. |
paul@0 | 1624 | |
paul@0 | 1625 | def find_name(self, name): |
paul@0 | 1626 | |
paul@0 | 1627 | """ |
paul@0 | 1628 | Return the qualified name for the given 'name' used in the current |
paul@0 | 1629 | non-function namespace. |
paul@0 | 1630 | """ |
paul@0 | 1631 | |
paul@0 | 1632 | path = self.get_namespace_path() |
paul@0 | 1633 | ref = None |
paul@0 | 1634 | |
paul@0 | 1635 | if not self.in_function and name not in predefined_constants: |
paul@0 | 1636 | if self.in_class: |
paul@0 | 1637 | ref = self.get_object(self.get_object_path(name)) |
paul@0 | 1638 | if not ref: |
paul@0 | 1639 | ref = self.get_global_or_builtin(name) |
paul@0 | 1640 | |
paul@0 | 1641 | return ref |
paul@0 | 1642 | |
paul@0 | 1643 | def get_class(self, node): |
paul@0 | 1644 | |
paul@0 | 1645 | """ |
paul@0 | 1646 | Use the given 'node' to obtain the identity of a class. Return a |
paul@0 | 1647 | reference for the class. Unresolved dependencies are permitted and must |
paul@0 | 1648 | be resolved later. |
paul@0 | 1649 | """ |
paul@0 | 1650 | |
paul@0 | 1651 | ref = self._get_class(node) |
paul@0 | 1652 | return ref.has_kind(["<class>", "<depends>"]) and ref or None |
paul@0 | 1653 | |
paul@0 | 1654 | def _get_class(self, node): |
paul@0 | 1655 | |
paul@0 | 1656 | """ |
paul@0 | 1657 | Use the given 'node' to find a class definition. Return a reference to |
paul@0 | 1658 | the class. |
paul@0 | 1659 | """ |
paul@0 | 1660 | |
paul@0 | 1661 | if isinstance(node, compiler.ast.Getattr): |
paul@0 | 1662 | |
paul@0 | 1663 | # Obtain the identity of the access target. |
paul@0 | 1664 | |
paul@0 | 1665 | ref = self._get_class(node.expr) |
paul@0 | 1666 | |
paul@0 | 1667 | # Where the target is a class or module, obtain the identity of the |
paul@0 | 1668 | # attribute. |
paul@0 | 1669 | |
paul@0 | 1670 | if ref.has_kind(["<function>", "<var>"]): |
paul@0 | 1671 | return None |
paul@0 | 1672 | else: |
paul@0 | 1673 | attrname = "%s.%s" % (ref.get_origin(), node.attrname) |
paul@0 | 1674 | return self.get_object(attrname) |
paul@0 | 1675 | |
paul@0 | 1676 | # Names can be module-level or built-in. |
paul@0 | 1677 | |
paul@0 | 1678 | elif isinstance(node, compiler.ast.Name): |
paul@0 | 1679 | |
paul@0 | 1680 | # Record usage of the name and attempt to identify it. |
paul@0 | 1681 | |
paul@0 | 1682 | self.record_name(node.name) |
paul@0 | 1683 | return self.get_global_or_builtin(node.name) |
paul@0 | 1684 | else: |
paul@0 | 1685 | return None |
paul@0 | 1686 | |
paul@0 | 1687 | def get_constant(self, name, value): |
paul@0 | 1688 | |
paul@0 | 1689 | "Return a constant reference for the given type 'name' and 'value'." |
paul@0 | 1690 | |
paul@0 | 1691 | ref = self.get_literal_builtin(name) |
paul@0 | 1692 | return self.get_constant_reference(ref, value) |
paul@0 | 1693 | |
paul@0 | 1694 | def get_literal_instance(self, n, name): |
paul@0 | 1695 | |
paul@0 | 1696 | "For node 'n', return a reference to an instance of 'name'." |
paul@0 | 1697 | |
paul@0 | 1698 | # Get a class reference. |
paul@0 | 1699 | |
paul@0 | 1700 | ref = self.get_literal_builtin(name) |
paul@0 | 1701 | |
paul@0 | 1702 | # Obtain the details of the literal itself. |
paul@0 | 1703 | # An alias to the type is generated for sequences. |
paul@0 | 1704 | |
paul@0 | 1705 | if name in ("dict", "list", "tuple"): |
paul@0 | 1706 | self.set_special_literal(name, ref) |
paul@0 | 1707 | return self.process_literal_sequence_node(n, name, ref, LiteralSequenceRef) |
paul@0 | 1708 | |
paul@0 | 1709 | # Constant values are independently recorded. |
paul@0 | 1710 | |
paul@0 | 1711 | else: |
paul@0 | 1712 | return self.get_constant_reference(ref, n.value) |
paul@0 | 1713 | |
paul@0 | 1714 | def get_literal_builtin(self, name): |
paul@0 | 1715 | |
paul@0 | 1716 | "Return a reference for a built-in literal type of the given 'name'." |
paul@0 | 1717 | |
paul@0 | 1718 | ref = self.get_builtin(name) |
paul@0 | 1719 | true_origin = "__builtins__.%s.%s" % (name, name) |
paul@0 | 1720 | exposed_origin = "__builtins__.%s" % name |
paul@0 | 1721 | |
paul@0 | 1722 | # Obtain fully-imported built-in class references. |
paul@0 | 1723 | |
paul@0 | 1724 | if ref and ref.has_kind("<class>"): |
paul@0 | 1725 | pass |
paul@0 | 1726 | |
paul@0 | 1727 | # Early-stage references need explicit references. |
paul@0 | 1728 | |
paul@0 | 1729 | elif ref: |
paul@0 | 1730 | ref = Reference("<class>", true_origin) |
paul@0 | 1731 | |
paul@0 | 1732 | # Otherwise, the normal locations can be used. |
paul@0 | 1733 | |
paul@0 | 1734 | else: |
paul@0 | 1735 | ref = Reference("<class>", true_origin, exposed_origin) |
paul@0 | 1736 | |
paul@0 | 1737 | return ref |
paul@0 | 1738 | |
paul@0 | 1739 | # Functions and invocations. |
paul@0 | 1740 | |
paul@0 | 1741 | def allocate_arguments(self, path, args): |
paul@0 | 1742 | |
paul@0 | 1743 | """ |
paul@0 | 1744 | Allocate temporary argument storage using current and maximum |
paul@0 | 1745 | requirements for the given 'path' and 'args'. |
paul@0 | 1746 | """ |
paul@0 | 1747 | |
paul@0 | 1748 | init_item(self.function_targets, path, lambda: [0, 0]) |
paul@0 | 1749 | t = self.function_targets[path] |
paul@0 | 1750 | t[0] += 1 |
paul@0 | 1751 | t[1] = max(t[0], t[1]) |
paul@0 | 1752 | |
paul@0 | 1753 | init_item(self.function_arguments, path, lambda: [0, 0]) |
paul@0 | 1754 | t = self.function_arguments[path] |
paul@0 | 1755 | t[0] += len(args) + 1 |
paul@0 | 1756 | t[1] = max(t[0], t[1]) |
paul@0 | 1757 | |
paul@0 | 1758 | def deallocate_arguments(self, path, args): |
paul@0 | 1759 | |
paul@0 | 1760 | "Deallocate temporary argument storage for the given 'path' and 'args'." |
paul@0 | 1761 | |
paul@0 | 1762 | self.function_targets[path][0] -= 1 |
paul@0 | 1763 | self.function_arguments[path][0] -= len(args) + 1 |
paul@0 | 1764 | |
paul@0 | 1765 | # vim: tabstop=4 expandtab shiftwidth=4 |