paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Deduce types for usage observations. |
paul@44 | 5 | |
paul@825 | 6 | Copyright (C) 2014, 2015, 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@44 | 7 | |
paul@44 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@44 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@44 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@44 | 11 | version. |
paul@44 | 12 | |
paul@44 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@44 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@44 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@44 | 16 | details. |
paul@44 | 17 | |
paul@44 | 18 | You should have received a copy of the GNU General Public License along with |
paul@44 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@44 | 20 | """ |
paul@44 | 21 | |
paul@791 | 22 | from common import first, get_assigned_attributes, get_attrnames, \ |
paul@90 | 23 | get_invoked_attributes, get_name_path, init_item, \ |
paul@791 | 24 | order_dependencies_partial, sorted_output, \ |
paul@791 | 25 | AccessLocation, CommonOutput, Location |
paul@789 | 26 | from encoders import encode_access_location, encode_alias_location, \ |
paul@789 | 27 | encode_constrained, encode_instruction, encode_location, \ |
paul@789 | 28 | encode_usage, get_kinds, \ |
paul@789 | 29 | test_label_for_kind, test_label_for_type |
paul@71 | 30 | from errors import DeduceError |
paul@44 | 31 | from os.path import join |
paul@57 | 32 | from referencing import combine_types, is_single_class_type, separate_types, \ |
paul@57 | 33 | Reference |
paul@44 | 34 | |
paul@44 | 35 | class Deducer(CommonOutput): |
paul@44 | 36 | |
paul@44 | 37 | "Deduce types in a program." |
paul@44 | 38 | |
paul@654 | 39 | root_class_type = "__builtins__.object" |
paul@654 | 40 | |
paul@44 | 41 | def __init__(self, importer, output): |
paul@44 | 42 | |
paul@44 | 43 | """ |
paul@44 | 44 | Initialise an instance using the given 'importer' that will perform |
paul@44 | 45 | deductions on the program information, writing the results to the given |
paul@44 | 46 | 'output' directory. |
paul@44 | 47 | """ |
paul@44 | 48 | |
paul@44 | 49 | self.importer = importer |
paul@44 | 50 | self.output = output |
paul@44 | 51 | |
paul@44 | 52 | # Descendants of classes. |
paul@44 | 53 | |
paul@44 | 54 | self.descendants = {} |
paul@44 | 55 | self.init_descendants() |
paul@44 | 56 | self.init_special_attributes() |
paul@44 | 57 | |
paul@44 | 58 | # Map locations to usage in order to determine specific types. |
paul@44 | 59 | |
paul@44 | 60 | self.location_index = {} |
paul@44 | 61 | |
paul@44 | 62 | # Map access locations to definition locations. |
paul@44 | 63 | |
paul@44 | 64 | self.access_index = {} |
paul@44 | 65 | |
paul@742 | 66 | # Map definition locations to affected accesses. |
paul@742 | 67 | |
paul@742 | 68 | self.access_index_rev = {} |
paul@742 | 69 | |
paul@44 | 70 | # Map aliases to accesses that define them. |
paul@44 | 71 | |
paul@44 | 72 | self.alias_index = {} |
paul@44 | 73 | |
paul@742 | 74 | # Map accesses to aliases whose initial values are influenced by them. |
paul@742 | 75 | |
paul@742 | 76 | self.alias_index_rev = {} |
paul@742 | 77 | |
paul@791 | 78 | # Map definitions and accesses to initialised names. |
paul@791 | 79 | |
paul@791 | 80 | self.initialised_names = {} |
paul@791 | 81 | self.initialised_accesses = {} |
paul@791 | 82 | |
paul@64 | 83 | # Map constant accesses to redefined accesses. |
paul@64 | 84 | |
paul@64 | 85 | self.const_accesses = {} |
paul@67 | 86 | self.const_accesses_rev = {} |
paul@64 | 87 | |
paul@44 | 88 | # Map usage observations to assigned attributes. |
paul@44 | 89 | |
paul@44 | 90 | self.assigned_attrs = {} |
paul@44 | 91 | |
paul@44 | 92 | # Map usage observations to objects. |
paul@44 | 93 | |
paul@44 | 94 | self.attr_class_types = {} |
paul@44 | 95 | self.attr_instance_types = {} |
paul@44 | 96 | self.attr_module_types = {} |
paul@44 | 97 | |
paul@501 | 98 | # All known attribute names. |
paul@501 | 99 | |
paul@501 | 100 | self.all_attrnames = set() |
paul@501 | 101 | |
paul@44 | 102 | # Modified attributes from usage observations. |
paul@44 | 103 | |
paul@44 | 104 | self.modified_attributes = {} |
paul@44 | 105 | |
paul@117 | 106 | # Accesses that are assignments or invocations. |
paul@71 | 107 | |
paul@71 | 108 | self.reference_assignments = set() |
paul@553 | 109 | self.reference_invocations = {} |
paul@553 | 110 | self.reference_invocations_unsuitable = {} |
paul@71 | 111 | |
paul@44 | 112 | # Map locations to types, constrained indicators and attributes. |
paul@44 | 113 | |
paul@44 | 114 | self.accessor_class_types = {} |
paul@44 | 115 | self.accessor_instance_types = {} |
paul@44 | 116 | self.accessor_module_types = {} |
paul@44 | 117 | self.provider_class_types = {} |
paul@44 | 118 | self.provider_instance_types = {} |
paul@44 | 119 | self.provider_module_types = {} |
paul@67 | 120 | self.accessor_constrained = set() |
paul@44 | 121 | self.access_constrained = set() |
paul@44 | 122 | self.referenced_attrs = {} |
paul@44 | 123 | self.referenced_objects = {} |
paul@44 | 124 | |
paul@67 | 125 | # Details of access operations. |
paul@67 | 126 | |
paul@67 | 127 | self.access_plans = {} |
paul@67 | 128 | |
paul@653 | 129 | # Specific attribute access information. |
paul@653 | 130 | |
paul@653 | 131 | self.access_instructions = {} |
paul@653 | 132 | self.accessor_kinds = {} |
paul@653 | 133 | |
paul@44 | 134 | # Accumulated information about accessors and providers. |
paul@44 | 135 | |
paul@44 | 136 | self.accessor_general_class_types = {} |
paul@44 | 137 | self.accessor_general_instance_types = {} |
paul@44 | 138 | self.accessor_general_module_types = {} |
paul@44 | 139 | self.accessor_all_types = {} |
paul@44 | 140 | self.accessor_all_general_types = {} |
paul@44 | 141 | self.provider_all_types = {} |
paul@44 | 142 | self.accessor_guard_tests = {} |
paul@44 | 143 | |
paul@44 | 144 | # Accumulated information about accessed attributes and |
paul@67 | 145 | # access/attribute-specific accessor tests. |
paul@44 | 146 | |
paul@44 | 147 | self.reference_all_attrs = {} |
paul@687 | 148 | self.reference_all_providers = {} |
paul@687 | 149 | self.reference_all_provider_kinds = {} |
paul@67 | 150 | self.reference_all_accessor_types = {} |
paul@67 | 151 | self.reference_all_accessor_general_types = {} |
paul@44 | 152 | self.reference_test_types = {} |
paul@77 | 153 | self.reference_test_accessor_type = {} |
paul@44 | 154 | |
paul@44 | 155 | # The processing workflow itself. |
paul@44 | 156 | |
paul@44 | 157 | self.init_usage_index() |
paul@501 | 158 | self.init_attr_type_indexes() |
paul@501 | 159 | self.init_combined_attribute_index() |
paul@44 | 160 | self.init_accessors() |
paul@44 | 161 | self.init_accesses() |
paul@44 | 162 | self.init_aliases() |
paul@44 | 163 | self.modify_mutated_attributes() |
paul@44 | 164 | self.identify_references() |
paul@44 | 165 | self.classify_accessors() |
paul@44 | 166 | self.classify_accesses() |
paul@67 | 167 | self.initialise_access_plans() |
paul@653 | 168 | self.initialise_access_instructions() |
paul@411 | 169 | self.identify_dependencies() |
paul@44 | 170 | |
paul@44 | 171 | def to_output(self): |
paul@44 | 172 | |
paul@44 | 173 | "Write the output files using deduction information." |
paul@44 | 174 | |
paul@44 | 175 | self.check_output() |
paul@44 | 176 | |
paul@44 | 177 | self.write_mutations() |
paul@44 | 178 | self.write_accessors() |
paul@44 | 179 | self.write_accesses() |
paul@65 | 180 | self.write_access_plans() |
paul@44 | 181 | |
paul@44 | 182 | def write_mutations(self): |
paul@44 | 183 | |
paul@44 | 184 | """ |
paul@44 | 185 | Write mutation-related output in the following format: |
paul@44 | 186 | |
paul@44 | 187 | qualified name " " original object type |
paul@44 | 188 | |
paul@44 | 189 | Object type can be "<class>", "<function>" or "<var>". |
paul@44 | 190 | """ |
paul@44 | 191 | |
paul@44 | 192 | f = open(join(self.output, "mutations"), "w") |
paul@44 | 193 | try: |
paul@44 | 194 | attrs = self.modified_attributes.items() |
paul@44 | 195 | attrs.sort() |
paul@44 | 196 | |
paul@44 | 197 | for attr, value in attrs: |
paul@44 | 198 | print >>f, attr, value |
paul@44 | 199 | finally: |
paul@44 | 200 | f.close() |
paul@44 | 201 | |
paul@44 | 202 | def write_accessors(self): |
paul@44 | 203 | |
paul@44 | 204 | """ |
paul@44 | 205 | Write reference-related output in the following format for types: |
paul@44 | 206 | |
paul@44 | 207 | location " " ( "constrained" | "deduced" ) " " attribute type " " most general type names " " number of specific types |
paul@44 | 208 | |
paul@44 | 209 | Note that multiple lines can be given for each location, one for each |
paul@44 | 210 | attribute type. |
paul@44 | 211 | |
paul@44 | 212 | Locations have the following format: |
paul@44 | 213 | |
paul@788 | 214 | qualified name of scope ":" local name ":" name version |
paul@44 | 215 | |
paul@44 | 216 | The attribute type can be "<class>", "<instance>", "<module>" or "<>", |
paul@44 | 217 | where the latter indicates an absence of suitable references. |
paul@44 | 218 | |
paul@44 | 219 | Type names indicate the type providing the attributes, being either a |
paul@44 | 220 | class or module qualified name. |
paul@44 | 221 | |
paul@44 | 222 | ---- |
paul@44 | 223 | |
paul@44 | 224 | A summary of accessor types is formatted as follows: |
paul@44 | 225 | |
paul@44 | 226 | location " " ( "constrained" | "deduced" ) " " ( "specific" | "common" | "unguarded" ) " " most general type names " " number of specific types |
paul@44 | 227 | |
paul@44 | 228 | This summary groups all attribute types (class, instance, module) into a |
paul@44 | 229 | single line in order to determine the complexity of identifying an |
paul@44 | 230 | accessor. |
paul@44 | 231 | |
paul@44 | 232 | ---- |
paul@44 | 233 | |
paul@44 | 234 | References that cannot be supported by any types are written to a |
paul@44 | 235 | warnings file in the following format: |
paul@44 | 236 | |
paul@44 | 237 | location |
paul@44 | 238 | |
paul@44 | 239 | ---- |
paul@44 | 240 | |
paul@44 | 241 | For each location where a guard would be asserted to guarantee the |
paul@44 | 242 | nature of an object, the following format is employed: |
paul@44 | 243 | |
paul@44 | 244 | location " " ( "specific" | "common" ) " " object kind " " object types |
paul@44 | 245 | |
paul@44 | 246 | Object kind can be "<class>", "<instance>" or "<module>". |
paul@788 | 247 | |
paul@788 | 248 | ---- |
paul@788 | 249 | |
paul@788 | 250 | A summary of aliases is formatted as follows: |
paul@788 | 251 | |
paul@788 | 252 | location " " locations |
paul@44 | 253 | """ |
paul@44 | 254 | |
paul@44 | 255 | f_type_summary = open(join(self.output, "type_summary"), "w") |
paul@44 | 256 | f_types = open(join(self.output, "types"), "w") |
paul@44 | 257 | f_warnings = open(join(self.output, "type_warnings"), "w") |
paul@44 | 258 | f_guards = open(join(self.output, "guards"), "w") |
paul@788 | 259 | f_aliases = open(join(self.output, "aliases"), "w") |
paul@44 | 260 | |
paul@44 | 261 | try: |
paul@44 | 262 | locations = self.accessor_class_types.keys() |
paul@44 | 263 | locations.sort() |
paul@44 | 264 | |
paul@44 | 265 | for location in locations: |
paul@67 | 266 | constrained = location in self.accessor_constrained |
paul@44 | 267 | |
paul@44 | 268 | # Accessor information. |
paul@44 | 269 | |
paul@44 | 270 | class_types = self.accessor_class_types[location] |
paul@44 | 271 | instance_types = self.accessor_instance_types[location] |
paul@44 | 272 | module_types = self.accessor_module_types[location] |
paul@44 | 273 | |
paul@44 | 274 | general_class_types = self.accessor_general_class_types[location] |
paul@44 | 275 | general_instance_types = self.accessor_general_instance_types[location] |
paul@44 | 276 | general_module_types = self.accessor_general_module_types[location] |
paul@44 | 277 | |
paul@44 | 278 | all_types = self.accessor_all_types[location] |
paul@44 | 279 | all_general_types = self.accessor_all_general_types[location] |
paul@44 | 280 | |
paul@44 | 281 | if class_types: |
paul@44 | 282 | print >>f_types, encode_location(location), encode_constrained(constrained), "<class>", \ |
paul@44 | 283 | sorted_output(general_class_types), len(class_types) |
paul@44 | 284 | |
paul@44 | 285 | if instance_types: |
paul@44 | 286 | print >>f_types, encode_location(location), encode_constrained(constrained), "<instance>", \ |
paul@44 | 287 | sorted_output(general_instance_types), len(instance_types) |
paul@44 | 288 | |
paul@44 | 289 | if module_types: |
paul@44 | 290 | print >>f_types, encode_location(location), encode_constrained(constrained), "<module>", \ |
paul@44 | 291 | sorted_output(general_module_types), len(module_types) |
paul@44 | 292 | |
paul@44 | 293 | if not all_types: |
paul@44 | 294 | print >>f_types, encode_location(location), "deduced", "<>", 0 |
paul@55 | 295 | attrnames = list(self.location_index[location]) |
paul@55 | 296 | attrnames.sort() |
paul@55 | 297 | print >>f_warnings, encode_location(location), "; ".join(map(encode_usage, attrnames)) |
paul@44 | 298 | |
paul@44 | 299 | guard_test = self.accessor_guard_tests.get(location) |
paul@237 | 300 | if guard_test: |
paul@237 | 301 | guard_test_type, guard_test_arg = guard_test |
paul@44 | 302 | |
paul@44 | 303 | # Write specific type guard details. |
paul@44 | 304 | |
paul@237 | 305 | if guard_test and guard_test_type == "specific": |
paul@237 | 306 | print >>f_guards, encode_location(location), "-".join(guard_test), \ |
paul@237 | 307 | first(get_kinds(all_types)), \ |
paul@44 | 308 | sorted_output(all_types) |
paul@44 | 309 | |
paul@44 | 310 | # Write common type guard details. |
paul@44 | 311 | |
paul@237 | 312 | elif guard_test and guard_test_type == "common": |
paul@237 | 313 | print >>f_guards, encode_location(location), "-".join(guard_test), \ |
paul@237 | 314 | first(get_kinds(all_general_types)), \ |
paul@44 | 315 | sorted_output(all_general_types) |
paul@44 | 316 | |
paul@44 | 317 | print >>f_type_summary, encode_location(location), encode_constrained(constrained), \ |
paul@237 | 318 | guard_test and "-".join(guard_test) or "unguarded", sorted_output(all_general_types), len(all_types) |
paul@44 | 319 | |
paul@788 | 320 | # Aliases are visited separately from accessors, even though they |
paul@788 | 321 | # are often the same thing. |
paul@788 | 322 | |
paul@788 | 323 | locations = self.alias_index.keys() |
paul@788 | 324 | locations.sort() |
paul@788 | 325 | |
paul@788 | 326 | for location in locations: |
paul@788 | 327 | accesses = [] |
paul@788 | 328 | for access_location in self.alias_index[location]: |
paul@789 | 329 | invocation = access_location in self.reference_invocations |
paul@789 | 330 | accesses.append(encode_alias_location(access_location, invocation)) |
paul@789 | 331 | invocation = location in self.reference_invocations |
paul@789 | 332 | print >>f_aliases, encode_alias_location(location, invocation), ", ".join(accesses) |
paul@788 | 333 | |
paul@44 | 334 | finally: |
paul@44 | 335 | f_type_summary.close() |
paul@44 | 336 | f_types.close() |
paul@44 | 337 | f_warnings.close() |
paul@44 | 338 | f_guards.close() |
paul@788 | 339 | f_aliases.close() |
paul@44 | 340 | |
paul@44 | 341 | def write_accesses(self): |
paul@44 | 342 | |
paul@44 | 343 | """ |
paul@44 | 344 | Specific attribute output is produced in the following format: |
paul@44 | 345 | |
paul@44 | 346 | location " " ( "constrained" | "deduced" ) " " attribute type " " attribute references |
paul@44 | 347 | |
paul@44 | 348 | Note that multiple lines can be given for each location and attribute |
paul@44 | 349 | name, one for each attribute type. |
paul@44 | 350 | |
paul@44 | 351 | Locations have the following format: |
paul@44 | 352 | |
paul@788 | 353 | qualified name of scope ":" local name ":" attribute name ":" access number |
paul@44 | 354 | |
paul@44 | 355 | The attribute type can be "<class>", "<instance>", "<module>" or "<>", |
paul@44 | 356 | where the latter indicates an absence of suitable references. |
paul@44 | 357 | |
paul@44 | 358 | Attribute references have the following format: |
paul@44 | 359 | |
paul@44 | 360 | object type ":" qualified name |
paul@44 | 361 | |
paul@44 | 362 | Object type can be "<class>", "<function>" or "<var>". |
paul@44 | 363 | |
paul@44 | 364 | ---- |
paul@44 | 365 | |
paul@44 | 366 | A summary of attributes is formatted as follows: |
paul@44 | 367 | |
paul@44 | 368 | location " " attribute name " " ( "constrained" | "deduced" ) " " test " " attribute references |
paul@44 | 369 | |
paul@44 | 370 | This summary groups all attribute types (class, instance, module) into a |
paul@44 | 371 | single line in order to determine the complexity of each access. |
paul@44 | 372 | |
paul@44 | 373 | Tests can be "validate", "specific", "untested", "guarded-validate" or "guarded-specific". |
paul@44 | 374 | |
paul@44 | 375 | ---- |
paul@44 | 376 | |
paul@44 | 377 | For each access where a test would be asserted to guarantee the |
paul@44 | 378 | nature of an attribute, the following formats are employed: |
paul@44 | 379 | |
paul@44 | 380 | location " " attribute name " " "validate" |
paul@44 | 381 | location " " attribute name " " "specific" " " attribute type " " object type |
paul@44 | 382 | |
paul@44 | 383 | ---- |
paul@44 | 384 | |
paul@44 | 385 | References that cannot be supported by any types are written to a |
paul@44 | 386 | warnings file in the following format: |
paul@44 | 387 | |
paul@44 | 388 | location |
paul@44 | 389 | """ |
paul@44 | 390 | |
paul@44 | 391 | f_attr_summary = open(join(self.output, "attribute_summary"), "w") |
paul@44 | 392 | f_attrs = open(join(self.output, "attributes"), "w") |
paul@44 | 393 | f_tests = open(join(self.output, "tests"), "w") |
paul@44 | 394 | f_warnings = open(join(self.output, "attribute_warnings"), "w") |
paul@553 | 395 | f_unsuitable = open(join(self.output, "invocation_warnings"), "w") |
paul@44 | 396 | |
paul@44 | 397 | try: |
paul@44 | 398 | locations = self.referenced_attrs.keys() |
paul@44 | 399 | locations.sort() |
paul@44 | 400 | |
paul@44 | 401 | for location in locations: |
paul@44 | 402 | constrained = location in self.access_constrained |
paul@44 | 403 | |
paul@44 | 404 | # Attribute information, both name-based and anonymous. |
paul@44 | 405 | |
paul@44 | 406 | referenced_attrs = self.referenced_attrs[location] |
paul@44 | 407 | |
paul@44 | 408 | if referenced_attrs: |
paul@791 | 409 | attrname = location.get_attrname() |
paul@44 | 410 | |
paul@738 | 411 | all_accessed_attrs = list(set(self.reference_all_attrs[location])) |
paul@738 | 412 | all_accessed_attrs.sort() |
paul@44 | 413 | |
paul@44 | 414 | for attrtype, attrs in self.get_referenced_attrs(location): |
paul@44 | 415 | print >>f_attrs, encode_access_location(location), encode_constrained(constrained), attrtype, sorted_output(attrs) |
paul@44 | 416 | |
paul@44 | 417 | test_type = self.reference_test_types.get(location) |
paul@44 | 418 | |
paul@44 | 419 | # Write the need to test at run time. |
paul@44 | 420 | |
paul@237 | 421 | if test_type[0] == "validate": |
paul@237 | 422 | print >>f_tests, encode_access_location(location), "-".join(test_type) |
paul@44 | 423 | |
paul@44 | 424 | # Write any type checks for anonymous accesses. |
paul@44 | 425 | |
paul@77 | 426 | elif test_type and self.reference_test_accessor_type.get(location): |
paul@240 | 427 | print >>f_tests, encode_access_location(location), "-".join(test_type), \ |
paul@44 | 428 | sorted_output(all_accessed_attrs), \ |
paul@77 | 429 | self.reference_test_accessor_type[location] |
paul@44 | 430 | |
paul@44 | 431 | print >>f_attr_summary, encode_access_location(location), encode_constrained(constrained), \ |
paul@237 | 432 | test_type and "-".join(test_type) or "untested", sorted_output(all_accessed_attrs) |
paul@44 | 433 | |
paul@553 | 434 | # Write details of potentially unsuitable invocation |
paul@553 | 435 | # occurrences. |
paul@553 | 436 | |
paul@553 | 437 | unsuitable = self.reference_invocations_unsuitable.get(location) |
paul@553 | 438 | if unsuitable: |
paul@553 | 439 | unsuitable = map(str, unsuitable) |
paul@553 | 440 | unsuitable.sort() |
paul@553 | 441 | print >>f_unsuitable, encode_access_location(location), ", ".join(unsuitable) |
paul@553 | 442 | |
paul@44 | 443 | else: |
paul@44 | 444 | print >>f_warnings, encode_access_location(location) |
paul@44 | 445 | |
paul@44 | 446 | finally: |
paul@44 | 447 | f_attr_summary.close() |
paul@44 | 448 | f_attrs.close() |
paul@44 | 449 | f_tests.close() |
paul@44 | 450 | f_warnings.close() |
paul@553 | 451 | f_unsuitable.close() |
paul@44 | 452 | |
paul@67 | 453 | def write_access_plans(self): |
paul@67 | 454 | |
paul@91 | 455 | """ |
paul@653 | 456 | Write access and instruction plans. |
paul@653 | 457 | |
paul@91 | 458 | Each attribute access is written out as a plan of the following form: |
paul@91 | 459 | |
paul@91 | 460 | location " " name " " test " " test type " " base " " traversed attributes |
paul@653 | 461 | " " traversal access modes " " attributes to traverse |
paul@653 | 462 | " " context " " context test " " first access method |
paul@653 | 463 | " " final access method " " static attribute " " accessor kinds |
paul@653 | 464 | |
paul@653 | 465 | Locations have the following format: |
paul@653 | 466 | |
paul@653 | 467 | qualified name of scope "." local name ":" name version |
paul@653 | 468 | |
paul@653 | 469 | Traversal access modes are either "class" (obtain accessor class to |
paul@653 | 470 | access attribute) or "object" (obtain attribute directly from accessor). |
paul@91 | 471 | """ |
paul@67 | 472 | |
paul@67 | 473 | f_attrs = open(join(self.output, "attribute_plans"), "w") |
paul@67 | 474 | |
paul@67 | 475 | try: |
paul@67 | 476 | locations = self.access_plans.keys() |
paul@67 | 477 | locations.sort() |
paul@67 | 478 | |
paul@67 | 479 | for location in locations: |
paul@234 | 480 | name, test, test_type, base, \ |
paul@234 | 481 | traversed, traversal_modes, attrnames, \ |
paul@102 | 482 | context, context_test, \ |
paul@234 | 483 | first_method, final_method, \ |
paul@234 | 484 | attr, accessor_kinds = self.access_plans[location] |
paul@77 | 485 | |
paul@75 | 486 | print >>f_attrs, encode_access_location(location), \ |
paul@213 | 487 | name or "{}", \ |
paul@237 | 488 | test and "-".join(test) or "{}", \ |
paul@237 | 489 | test_type or "{}", \ |
paul@75 | 490 | base or "{}", \ |
paul@67 | 491 | ".".join(traversed) or "{}", \ |
paul@96 | 492 | ".".join(traversal_modes) or "{}", \ |
paul@67 | 493 | ".".join(attrnames) or "{}", \ |
paul@102 | 494 | context, context_test, \ |
paul@234 | 495 | first_method, final_method, attr or "{}", \ |
paul@234 | 496 | ",".join(accessor_kinds) |
paul@67 | 497 | |
paul@67 | 498 | finally: |
paul@67 | 499 | f_attrs.close() |
paul@67 | 500 | |
paul@653 | 501 | f = open(join(self.output, "instruction_plans"), "w") |
paul@653 | 502 | try: |
paul@653 | 503 | access_instructions = self.access_instructions.items() |
paul@653 | 504 | access_instructions.sort() |
paul@653 | 505 | |
paul@653 | 506 | for location, instructions in access_instructions: |
paul@653 | 507 | print >>f, encode_access_location(location), "..." |
paul@653 | 508 | for instruction in instructions: |
paul@653 | 509 | print >>f, encode_instruction(instruction) |
paul@653 | 510 | print >>f |
paul@653 | 511 | |
paul@653 | 512 | finally: |
paul@653 | 513 | f.close() |
paul@653 | 514 | |
paul@44 | 515 | def classify_accessors(self): |
paul@44 | 516 | |
paul@44 | 517 | "For each program location, classify accessors." |
paul@44 | 518 | |
paul@44 | 519 | # Where instance and module types are defined, class types are also |
paul@44 | 520 | # defined. See: init_definition_details |
paul@44 | 521 | |
paul@44 | 522 | locations = self.accessor_class_types.keys() |
paul@44 | 523 | |
paul@44 | 524 | for location in locations: |
paul@67 | 525 | constrained = location in self.accessor_constrained |
paul@44 | 526 | |
paul@44 | 527 | # Provider information. |
paul@44 | 528 | |
paul@44 | 529 | class_types = self.provider_class_types[location] |
paul@44 | 530 | instance_types = self.provider_instance_types[location] |
paul@44 | 531 | module_types = self.provider_module_types[location] |
paul@44 | 532 | |
paul@44 | 533 | # Collect specific and general type information. |
paul@44 | 534 | |
paul@69 | 535 | self.provider_all_types[location] = \ |
paul@57 | 536 | combine_types(class_types, instance_types, module_types) |
paul@44 | 537 | |
paul@44 | 538 | # Accessor information. |
paul@44 | 539 | |
paul@44 | 540 | class_types = self.accessor_class_types[location] |
paul@44 | 541 | self.accessor_general_class_types[location] = \ |
paul@69 | 542 | general_class_types = self.get_most_general_class_types(class_types) |
paul@44 | 543 | |
paul@44 | 544 | instance_types = self.accessor_instance_types[location] |
paul@44 | 545 | self.accessor_general_instance_types[location] = \ |
paul@69 | 546 | general_instance_types = self.get_most_general_class_types(instance_types) |
paul@44 | 547 | |
paul@44 | 548 | module_types = self.accessor_module_types[location] |
paul@44 | 549 | self.accessor_general_module_types[location] = \ |
paul@44 | 550 | general_module_types = self.get_most_general_module_types(module_types) |
paul@44 | 551 | |
paul@44 | 552 | # Collect specific and general type information. |
paul@44 | 553 | |
paul@44 | 554 | self.accessor_all_types[location] = all_types = \ |
paul@57 | 555 | combine_types(class_types, instance_types, module_types) |
paul@44 | 556 | |
paul@44 | 557 | self.accessor_all_general_types[location] = all_general_types = \ |
paul@57 | 558 | combine_types(general_class_types, general_instance_types, general_module_types) |
paul@44 | 559 | |
paul@742 | 560 | # Record guard information but only for accessors employed by |
paul@742 | 561 | # accesses. There are no attribute accesses to guard, otherwise. |
paul@742 | 562 | |
paul@742 | 563 | if not constrained and self.access_index_rev.get(location): |
paul@44 | 564 | |
paul@816 | 565 | # Test for self parameters in methods that could not be |
paul@816 | 566 | # constrained, potentially due to usage unsupported by the class |
paul@816 | 567 | # defining the method (such as in mix-in classes). |
paul@816 | 568 | |
paul@816 | 569 | if location.name != "self" or self.in_method(location.path): |
paul@816 | 570 | self.record_guard(location, all_types, all_general_types) |
paul@816 | 571 | |
paul@816 | 572 | def record_guard(self, location, all_types, all_general_types): |
paul@816 | 573 | |
paul@816 | 574 | """ |
paul@816 | 575 | For the accessor 'location', record a guard if 'all_types' or |
paul@816 | 576 | 'all_general_types' are appropriate. Where no guard is recorded, access |
paul@816 | 577 | tests will need to be applied. |
paul@816 | 578 | """ |
paul@816 | 579 | |
paul@816 | 580 | # Record specific type guard details. |
paul@816 | 581 | |
paul@816 | 582 | if len(all_types) == 1: |
paul@816 | 583 | self.accessor_guard_tests[location] = ("specific", test_label_for_type(first(all_types))) |
paul@816 | 584 | elif is_single_class_type(all_types): |
paul@816 | 585 | self.accessor_guard_tests[location] = ("specific", "object") |
paul@816 | 586 | |
paul@816 | 587 | # Record common type guard details. |
paul@816 | 588 | |
paul@816 | 589 | elif len(all_general_types) == 1: |
paul@816 | 590 | self.accessor_guard_tests[location] = ("common", test_label_for_type(first(all_types))) |
paul@816 | 591 | elif is_single_class_type(all_general_types): |
paul@816 | 592 | self.accessor_guard_tests[location] = ("common", "object") |
paul@816 | 593 | |
paul@816 | 594 | # Otherwise, no convenient guard can be defined. |
paul@44 | 595 | |
paul@44 | 596 | def classify_accesses(self): |
paul@44 | 597 | |
paul@44 | 598 | "For each program location, classify accesses." |
paul@44 | 599 | |
paul@44 | 600 | # Attribute accesses use potentially different locations to those of |
paul@44 | 601 | # accessors. |
paul@44 | 602 | |
paul@44 | 603 | locations = self.referenced_attrs.keys() |
paul@44 | 604 | |
paul@44 | 605 | for location in locations: |
paul@44 | 606 | constrained = location in self.access_constrained |
paul@44 | 607 | |
paul@69 | 608 | # Combine type information from all accessors supplying the access. |
paul@44 | 609 | |
paul@44 | 610 | accessor_locations = self.get_accessors_for_access(location) |
paul@44 | 611 | |
paul@44 | 612 | all_provider_types = set() |
paul@44 | 613 | all_accessor_types = set() |
paul@44 | 614 | all_accessor_general_types = set() |
paul@44 | 615 | |
paul@44 | 616 | for accessor_location in accessor_locations: |
paul@44 | 617 | |
paul@44 | 618 | # Obtain the provider types for guard-related attribute access |
paul@44 | 619 | # checks. |
paul@44 | 620 | |
paul@67 | 621 | all_provider_types.update(self.provider_all_types.get(accessor_location)) |
paul@67 | 622 | |
paul@67 | 623 | # Obtain the accessor guard types (specific and general). |
paul@67 | 624 | |
paul@67 | 625 | all_accessor_types.update(self.accessor_all_types.get(accessor_location)) |
paul@67 | 626 | all_accessor_general_types.update(self.accessor_all_general_types.get(accessor_location)) |
paul@44 | 627 | |
paul@70 | 628 | # Obtain basic properties of the types involved in the access. |
paul@70 | 629 | |
paul@70 | 630 | single_accessor_type = len(all_accessor_types) == 1 |
paul@70 | 631 | single_accessor_class_type = is_single_class_type(all_accessor_types) |
paul@70 | 632 | single_accessor_general_type = len(all_accessor_general_types) == 1 |
paul@70 | 633 | single_accessor_general_class_type = is_single_class_type(all_accessor_general_types) |
paul@70 | 634 | |
paul@69 | 635 | # Determine whether the attribute access is guarded or not. |
paul@44 | 636 | |
paul@44 | 637 | guarded = ( |
paul@70 | 638 | single_accessor_type or single_accessor_class_type or |
paul@70 | 639 | single_accessor_general_type or single_accessor_general_class_type |
paul@44 | 640 | ) |
paul@44 | 641 | |
paul@44 | 642 | if guarded: |
paul@44 | 643 | (guard_class_types, guard_instance_types, guard_module_types, |
paul@57 | 644 | _function_types, _var_types) = separate_types(all_provider_types) |
paul@44 | 645 | |
paul@67 | 646 | self.reference_all_accessor_types[location] = all_accessor_types |
paul@67 | 647 | self.reference_all_accessor_general_types[location] = all_accessor_general_types |
paul@63 | 648 | |
paul@44 | 649 | # Attribute information, both name-based and anonymous. |
paul@44 | 650 | |
paul@44 | 651 | referenced_attrs = self.referenced_attrs[location] |
paul@44 | 652 | |
paul@71 | 653 | if not referenced_attrs: |
paul@187 | 654 | raise DeduceError("In %s, access via %s to attribute %s (occurrence %d) cannot be identified." % location) |
paul@71 | 655 | |
paul@71 | 656 | # Record attribute information for each name used on the |
paul@71 | 657 | # accessor. |
paul@71 | 658 | |
paul@791 | 659 | attrname = location.get_attrname() |
paul@71 | 660 | |
paul@687 | 661 | self.reference_all_attrs[location] = all_accessed_attrs = [] |
paul@687 | 662 | self.reference_all_providers[location] = all_providers = [] |
paul@687 | 663 | self.reference_all_provider_kinds[location] = all_provider_kinds = set() |
paul@71 | 664 | |
paul@71 | 665 | # Obtain provider and attribute details for this kind of |
paul@71 | 666 | # object. |
paul@71 | 667 | |
paul@71 | 668 | for attrtype, object_type, attr in referenced_attrs: |
paul@687 | 669 | all_accessed_attrs.append(attr) |
paul@687 | 670 | all_providers.append(object_type) |
paul@687 | 671 | all_provider_kinds.add(attrtype) |
paul@687 | 672 | |
paul@687 | 673 | # Obtain reference and provider information as sets for the |
paul@687 | 674 | # operations below, retaining the list forms for use with |
paul@687 | 675 | # instruction plan preparation. |
paul@687 | 676 | |
paul@687 | 677 | all_accessed_attrs = set(all_accessed_attrs) |
paul@687 | 678 | all_providers = set(all_providers) |
paul@71 | 679 | all_general_providers = self.get_most_general_types(all_providers) |
paul@71 | 680 | |
paul@71 | 681 | # Determine which attributes would be provided by the |
paul@71 | 682 | # accessor types upheld by a guard. |
paul@71 | 683 | |
paul@71 | 684 | if guarded: |
paul@71 | 685 | guard_attrs = set() |
paul@555 | 686 | |
paul@71 | 687 | for _attrtype, object_type, attr in \ |
paul@555 | 688 | self._identify_reference_attribute(location, attrname, guard_class_types, guard_instance_types, guard_module_types): |
paul@555 | 689 | |
paul@71 | 690 | guard_attrs.add(attr) |
paul@71 | 691 | else: |
paul@71 | 692 | guard_attrs = None |
paul@71 | 693 | |
paul@71 | 694 | # Constrained accesses guarantee the nature of the accessor. |
paul@71 | 695 | # However, there may still be many types involved. |
paul@71 | 696 | |
paul@71 | 697 | if constrained: |
paul@71 | 698 | if single_accessor_type: |
paul@237 | 699 | self.reference_test_types[location] = ("constrained", "specific", test_label_for_type(first(all_accessor_types))) |
paul@71 | 700 | elif single_accessor_class_type: |
paul@237 | 701 | self.reference_test_types[location] = ("constrained", "specific", "object") |
paul@71 | 702 | elif single_accessor_general_type: |
paul@237 | 703 | self.reference_test_types[location] = ("constrained", "common", test_label_for_type(first(all_accessor_general_types))) |
paul@71 | 704 | elif single_accessor_general_class_type: |
paul@237 | 705 | self.reference_test_types[location] = ("constrained", "common", "object") |
paul@44 | 706 | else: |
paul@237 | 707 | self.reference_test_types[location] = ("constrained", "many") |
paul@71 | 708 | |
paul@71 | 709 | # Suitably guarded accesses, where the nature of the |
paul@71 | 710 | # accessor can be guaranteed, do not require the attribute |
paul@71 | 711 | # involved to be validated. Otherwise, for unguarded |
paul@71 | 712 | # accesses, access-level tests are required. |
paul@71 | 713 | |
paul@71 | 714 | elif guarded and all_accessed_attrs.issubset(guard_attrs): |
paul@71 | 715 | if single_accessor_type: |
paul@237 | 716 | self.reference_test_types[location] = ("guarded", "specific", test_label_for_type(first(all_accessor_types))) |
paul@71 | 717 | elif single_accessor_class_type: |
paul@237 | 718 | self.reference_test_types[location] = ("guarded", "specific", "object") |
paul@71 | 719 | elif single_accessor_general_type: |
paul@237 | 720 | self.reference_test_types[location] = ("guarded", "common", test_label_for_type(first(all_accessor_general_types))) |
paul@71 | 721 | elif single_accessor_general_class_type: |
paul@237 | 722 | self.reference_test_types[location] = ("guarded", "common", "object") |
paul@71 | 723 | |
paul@71 | 724 | # Record the need to test the type of anonymous and |
paul@71 | 725 | # unconstrained accessors. |
paul@71 | 726 | |
paul@71 | 727 | elif len(all_providers) == 1: |
paul@71 | 728 | provider = first(all_providers) |
paul@654 | 729 | if provider != self.root_class_type: |
paul@71 | 730 | all_accessor_kinds = set(get_kinds(all_accessor_types)) |
paul@71 | 731 | if len(all_accessor_kinds) == 1: |
paul@385 | 732 | test_type = ("test", "specific", test_label_for_kind(first(all_accessor_kinds))) |
paul@70 | 733 | else: |
paul@237 | 734 | test_type = ("test", "specific", "object") |
paul@71 | 735 | self.reference_test_types[location] = test_type |
paul@77 | 736 | self.reference_test_accessor_type[location] = provider |
paul@71 | 737 | |
paul@71 | 738 | elif len(all_general_providers) == 1: |
paul@71 | 739 | provider = first(all_general_providers) |
paul@654 | 740 | if provider != self.root_class_type: |
paul@71 | 741 | all_accessor_kinds = set(get_kinds(all_accessor_general_types)) |
paul@71 | 742 | if len(all_accessor_kinds) == 1: |
paul@385 | 743 | test_type = ("test", "common", test_label_for_kind(first(all_accessor_kinds))) |
paul@71 | 744 | else: |
paul@237 | 745 | test_type = ("test", "common", "object") |
paul@71 | 746 | self.reference_test_types[location] = test_type |
paul@77 | 747 | self.reference_test_accessor_type[location] = provider |
paul@71 | 748 | |
paul@71 | 749 | # Record the need to test the identity of the attribute. |
paul@71 | 750 | |
paul@71 | 751 | else: |
paul@237 | 752 | self.reference_test_types[location] = ("validate",) |
paul@44 | 753 | |
paul@67 | 754 | def initialise_access_plans(self): |
paul@67 | 755 | |
paul@67 | 756 | "Define attribute access plans." |
paul@67 | 757 | |
paul@67 | 758 | for location in self.referenced_attrs.keys(): |
paul@76 | 759 | original_location = self.const_accesses_rev.get(location) |
paul@76 | 760 | self.access_plans[original_location or location] = self.get_access_plan(location) |
paul@67 | 761 | |
paul@411 | 762 | def identify_dependencies(self): |
paul@411 | 763 | |
paul@411 | 764 | "Introduce more module dependencies to the importer." |
paul@411 | 765 | |
paul@411 | 766 | for location, referenced_attrs in self.referenced_attrs.items(): |
paul@411 | 767 | |
paul@418 | 768 | # Identify references providing dependencies. |
paul@411 | 769 | |
paul@411 | 770 | for attrtype, objtype, attr in referenced_attrs: |
paul@791 | 771 | self.importer.add_dependency(location.path, attr.get_origin()) |
paul@411 | 772 | |
paul@44 | 773 | def get_referenced_attrs(self, location): |
paul@44 | 774 | |
paul@44 | 775 | """ |
paul@44 | 776 | Return attributes referenced at the given access 'location' by the given |
paul@44 | 777 | 'attrname' as a list of (attribute type, attribute set) tuples. |
paul@44 | 778 | """ |
paul@44 | 779 | |
paul@69 | 780 | d = {} |
paul@69 | 781 | for attrtype, objtype, attr in self.referenced_attrs[location]: |
paul@69 | 782 | init_item(d, attrtype, set) |
paul@246 | 783 | d[attrtype].add(attr.unaliased()) |
paul@69 | 784 | l = d.items() |
paul@69 | 785 | l.sort() # class, module, instance |
paul@44 | 786 | return l |
paul@44 | 787 | |
paul@44 | 788 | # Initialisation methods. |
paul@44 | 789 | |
paul@44 | 790 | def init_descendants(self): |
paul@44 | 791 | |
paul@44 | 792 | "Identify descendants of each class." |
paul@44 | 793 | |
paul@44 | 794 | for name in self.importer.classes.keys(): |
paul@44 | 795 | self.get_descendants_for_class(name) |
paul@44 | 796 | |
paul@44 | 797 | def get_descendants_for_class(self, name): |
paul@44 | 798 | |
paul@44 | 799 | """ |
paul@44 | 800 | Use subclass information to deduce the descendants for the class of the |
paul@44 | 801 | given 'name'. |
paul@44 | 802 | """ |
paul@44 | 803 | |
paul@44 | 804 | if not self.descendants.has_key(name): |
paul@44 | 805 | descendants = set() |
paul@44 | 806 | |
paul@44 | 807 | for subclass in self.importer.subclasses[name]: |
paul@44 | 808 | descendants.update(self.get_descendants_for_class(subclass)) |
paul@44 | 809 | descendants.add(subclass) |
paul@44 | 810 | |
paul@44 | 811 | self.descendants[name] = descendants |
paul@44 | 812 | |
paul@44 | 813 | return self.descendants[name] |
paul@44 | 814 | |
paul@44 | 815 | def init_special_attributes(self): |
paul@44 | 816 | |
paul@44 | 817 | "Add special attributes to the classes for inheritance-related tests." |
paul@44 | 818 | |
paul@44 | 819 | all_class_attrs = self.importer.all_class_attrs |
paul@44 | 820 | |
paul@44 | 821 | for name, descendants in self.descendants.items(): |
paul@44 | 822 | for descendant in descendants: |
paul@44 | 823 | all_class_attrs[descendant]["#%s" % name] = name |
paul@44 | 824 | |
paul@44 | 825 | for name in all_class_attrs.keys(): |
paul@44 | 826 | all_class_attrs[name]["#%s" % name] = name |
paul@44 | 827 | |
paul@44 | 828 | def init_usage_index(self): |
paul@44 | 829 | |
paul@44 | 830 | """ |
paul@44 | 831 | Create indexes for module and function attribute usage and for anonymous |
paul@44 | 832 | accesses. |
paul@44 | 833 | """ |
paul@44 | 834 | |
paul@44 | 835 | for module in self.importer.get_modules(): |
paul@44 | 836 | for path, assignments in module.attr_usage.items(): |
paul@44 | 837 | self.add_usage(assignments, path) |
paul@44 | 838 | |
paul@791 | 839 | for path, all_attrnames in self.importer.all_attr_accesses.items(): |
paul@44 | 840 | for attrnames in all_attrnames: |
paul@44 | 841 | attrname = get_attrnames(attrnames)[-1] |
paul@791 | 842 | access_location = AccessLocation(path, None, attrnames, 0) |
paul@107 | 843 | self.add_usage_term(access_location, ((attrname, False, False),)) |
paul@44 | 844 | |
paul@44 | 845 | def add_usage(self, assignments, path): |
paul@44 | 846 | |
paul@44 | 847 | """ |
paul@44 | 848 | Collect usage from the given 'assignments', adding 'path' details to |
paul@44 | 849 | each record if specified. Add the usage to an index mapping to location |
paul@44 | 850 | information, as well as to an index mapping locations to usages. |
paul@44 | 851 | """ |
paul@44 | 852 | |
paul@44 | 853 | for name, versions in assignments.items(): |
paul@44 | 854 | for i, usages in enumerate(versions): |
paul@791 | 855 | location = Location(path, name, None, i) |
paul@44 | 856 | |
paul@88 | 857 | for usage in usages: |
paul@88 | 858 | self.add_usage_term(location, usage) |
paul@88 | 859 | |
paul@88 | 860 | def add_usage_term(self, location, usage): |
paul@44 | 861 | |
paul@44 | 862 | """ |
paul@88 | 863 | For 'location' and using 'usage' as a description of usage, record |
paul@44 | 864 | in the usage index a mapping from the usage to 'location', and record in |
paul@44 | 865 | the location index a mapping from 'location' to the usage. |
paul@44 | 866 | """ |
paul@44 | 867 | |
paul@44 | 868 | init_item(self.location_index, location, set) |
paul@88 | 869 | self.location_index[location].add(usage) |
paul@44 | 870 | |
paul@44 | 871 | def init_accessors(self): |
paul@44 | 872 | |
paul@44 | 873 | "Create indexes for module and function accessor information." |
paul@44 | 874 | |
paul@44 | 875 | for module in self.importer.get_modules(): |
paul@44 | 876 | for path, all_accesses in module.attr_accessors.items(): |
paul@44 | 877 | self.add_accessors(all_accesses, path) |
paul@44 | 878 | |
paul@44 | 879 | def add_accessors(self, all_accesses, path): |
paul@44 | 880 | |
paul@44 | 881 | """ |
paul@44 | 882 | For attribute accesses described by the mapping of 'all_accesses' from |
paul@44 | 883 | name details to accessor details, record the locations of the accessors |
paul@44 | 884 | for each access. |
paul@44 | 885 | """ |
paul@44 | 886 | |
paul@44 | 887 | # Get details for each access combining the given name and attribute. |
paul@44 | 888 | |
paul@44 | 889 | for (name, attrnames), accesses in all_accesses.items(): |
paul@44 | 890 | |
paul@44 | 891 | # Obtain the usage details using the access information. |
paul@44 | 892 | |
paul@44 | 893 | for access_number, versions in enumerate(accesses): |
paul@791 | 894 | access_location = AccessLocation(path, name, attrnames, access_number) |
paul@44 | 895 | locations = [] |
paul@44 | 896 | |
paul@44 | 897 | for version in versions: |
paul@791 | 898 | location = Location(path, name, None, version) |
paul@44 | 899 | locations.append(location) |
paul@44 | 900 | |
paul@742 | 901 | # Map accessors to affected accesses. |
paul@742 | 902 | |
paul@742 | 903 | l = init_item(self.access_index_rev, location, set) |
paul@742 | 904 | l.add(access_location) |
paul@742 | 905 | |
paul@742 | 906 | # Map accesses to supplying accessors. |
paul@742 | 907 | |
paul@44 | 908 | self.access_index[access_location] = locations |
paul@44 | 909 | |
paul@44 | 910 | def get_accessors_for_access(self, access_location): |
paul@44 | 911 | |
paul@44 | 912 | "Find a definition providing accessor details, if necessary." |
paul@44 | 913 | |
paul@44 | 914 | try: |
paul@44 | 915 | return self.access_index[access_location] |
paul@44 | 916 | except KeyError: |
paul@44 | 917 | return [access_location] |
paul@44 | 918 | |
paul@44 | 919 | def init_accesses(self): |
paul@44 | 920 | |
paul@44 | 921 | """ |
paul@501 | 922 | Check that attributes used in accesses are actually defined on some |
paul@501 | 923 | object. This can be overlooked if unknown attributes are employed in |
paul@501 | 924 | attribute chains. |
paul@501 | 925 | |
paul@44 | 926 | Initialise collections for accesses involving assignments. |
paul@44 | 927 | """ |
paul@44 | 928 | |
paul@44 | 929 | # For each scope, obtain access details. |
paul@44 | 930 | |
paul@44 | 931 | for path, all_accesses in self.importer.all_attr_access_modifiers.items(): |
paul@44 | 932 | |
paul@44 | 933 | # For each combination of name and attribute names, obtain |
paul@44 | 934 | # applicable modifiers. |
paul@44 | 935 | |
paul@112 | 936 | for (name, attrname_str), modifiers in all_accesses.items(): |
paul@44 | 937 | |
paul@44 | 938 | # For each access, determine the name versions affected by |
paul@44 | 939 | # assignments. |
paul@44 | 940 | |
paul@117 | 941 | for access_number, (assignment, invocation) in enumerate(modifiers): |
paul@112 | 942 | |
paul@44 | 943 | if name: |
paul@791 | 944 | access_location = AccessLocation(path, name, attrname_str, access_number) |
paul@44 | 945 | else: |
paul@791 | 946 | access_location = AccessLocation(path, None, attrname_str, 0) |
paul@112 | 947 | |
paul@501 | 948 | # Plain name accesses do not employ attributes and are |
paul@717 | 949 | # ignored. Whether they are invoked is of interest, however. |
paul@501 | 950 | |
paul@502 | 951 | if not attrname_str: |
paul@717 | 952 | if invocation: |
paul@717 | 953 | self.reference_invocations[access_location] = invocation |
paul@501 | 954 | continue |
paul@501 | 955 | |
paul@501 | 956 | attrnames = get_attrnames(attrname_str) |
paul@501 | 957 | |
paul@501 | 958 | # Check the attribute names. |
paul@501 | 959 | |
paul@501 | 960 | for attrname in attrnames: |
paul@501 | 961 | if not attrname in self.all_attrnames: |
paul@501 | 962 | raise DeduceError("In %s, attribute %s is not defined in the program." % (path, attrname)) |
paul@501 | 963 | |
paul@501 | 964 | # Now only process assignments and invocations. |
paul@501 | 965 | |
paul@117 | 966 | if invocation: |
paul@553 | 967 | self.reference_invocations[access_location] = invocation |
paul@117 | 968 | continue |
paul@501 | 969 | elif not assignment: |
paul@501 | 970 | continue |
paul@501 | 971 | |
paul@501 | 972 | # Associate assignments with usage. |
paul@117 | 973 | |
paul@112 | 974 | self.reference_assignments.add(access_location) |
paul@71 | 975 | |
paul@112 | 976 | # Assignment occurs for the only attribute. |
paul@112 | 977 | |
paul@112 | 978 | if len(attrnames) == 1: |
paul@112 | 979 | accessor_locations = self.get_accessors_for_access(access_location) |
paul@112 | 980 | |
paul@112 | 981 | for location in accessor_locations: |
paul@112 | 982 | for usage in self.location_index[location]: |
paul@88 | 983 | init_item(self.assigned_attrs, usage, set) |
paul@112 | 984 | self.assigned_attrs[usage].add((path, name, attrnames[0])) |
paul@112 | 985 | |
paul@112 | 986 | # Assignment occurs for the final attribute. |
paul@112 | 987 | |
paul@112 | 988 | else: |
paul@112 | 989 | usage = ((attrnames[-1], False, False),) |
paul@112 | 990 | init_item(self.assigned_attrs, usage, set) |
paul@112 | 991 | self.assigned_attrs[usage].add((path, name, attrnames[-1])) |
paul@44 | 992 | |
paul@44 | 993 | def init_aliases(self): |
paul@44 | 994 | |
paul@44 | 995 | "Expand aliases so that alias-based accesses can be resolved." |
paul@44 | 996 | |
paul@44 | 997 | # Get aliased names with details of their accesses. |
paul@44 | 998 | |
paul@681 | 999 | for (path, name), all_aliases in self.importer.all_aliased_names.items(): |
paul@44 | 1000 | |
paul@742 | 1001 | # For each version of the name, obtain the access locations. |
paul@742 | 1002 | |
paul@742 | 1003 | for version, aliases in all_aliases.items(): |
paul@742 | 1004 | for (original_path, original_name, attrnames, access_number) in aliases: |
paul@791 | 1005 | accessor_location = Location(path, name, None, version) |
paul@791 | 1006 | access_location = AccessLocation(original_path, original_name, attrnames, access_number) |
paul@742 | 1007 | init_item(self.alias_index, accessor_location, list) |
paul@742 | 1008 | self.alias_index[accessor_location].append(access_location) |
paul@44 | 1009 | |
paul@791 | 1010 | # Get initialised name information for accessors and accesses. |
paul@791 | 1011 | |
paul@791 | 1012 | for (path, name), versions in self.importer.all_initialised_names.items(): |
paul@791 | 1013 | for version, ref in versions.items(): |
paul@791 | 1014 | location = Location(path, name, None, version) |
paul@791 | 1015 | |
paul@791 | 1016 | self.initialised_names[location] = ref |
paul@791 | 1017 | |
paul@791 | 1018 | access_locations = self.access_index_rev.get(location) |
paul@791 | 1019 | if access_locations: |
paul@791 | 1020 | for access_location in access_locations: |
paul@791 | 1021 | init_item(self.initialised_accesses, access_location, set) |
paul@791 | 1022 | self.initialised_accesses[access_location].add(ref) |
paul@44 | 1023 | |
paul@742 | 1024 | # Get a mapping from accesses to affected aliases. |
paul@735 | 1025 | |
paul@735 | 1026 | for accessor_location, access_locations in self.alias_index.items(): |
paul@735 | 1027 | for access_location in access_locations: |
paul@735 | 1028 | init_item(self.alias_index_rev, access_location, set) |
paul@735 | 1029 | self.alias_index_rev[access_location].add(accessor_location) |
paul@735 | 1030 | |
paul@44 | 1031 | # Attribute mutation for types. |
paul@44 | 1032 | |
paul@44 | 1033 | def modify_mutated_attributes(self): |
paul@44 | 1034 | |
paul@44 | 1035 | "Identify known, mutated attributes and change their state." |
paul@44 | 1036 | |
paul@44 | 1037 | # Usage-based accesses. |
paul@44 | 1038 | |
paul@44 | 1039 | for usage, all_attrnames in self.assigned_attrs.items(): |
paul@44 | 1040 | if not usage: |
paul@44 | 1041 | continue |
paul@44 | 1042 | |
paul@112 | 1043 | for path, name, attrname in all_attrnames: |
paul@44 | 1044 | class_types = self.get_class_types_for_usage(usage) |
paul@44 | 1045 | only_instance_types = set(self.get_instance_types_for_usage(usage)).difference(class_types) |
paul@44 | 1046 | module_types = self.get_module_types_for_usage(usage) |
paul@44 | 1047 | |
paul@44 | 1048 | # Detect self usage within methods in order to narrow the scope |
paul@44 | 1049 | # of the mutation. |
paul@44 | 1050 | |
paul@44 | 1051 | t = name == "self" and self.constrain_self_reference(path, class_types, only_instance_types) |
paul@44 | 1052 | if t: |
paul@44 | 1053 | class_types, only_instance_types, module_types, constrained = t |
paul@44 | 1054 | objects = set(class_types).union(only_instance_types).union(module_types) |
paul@44 | 1055 | |
paul@112 | 1056 | self.mutate_attribute(objects, attrname) |
paul@112 | 1057 | |
paul@112 | 1058 | def mutate_attribute(self, objects, attrname): |
paul@112 | 1059 | |
paul@112 | 1060 | "Mutate static 'objects' with the given 'attrname'." |
paul@44 | 1061 | |
paul@44 | 1062 | for name in objects: |
paul@112 | 1063 | attr = "%s.%s" % (name, attrname) |
paul@44 | 1064 | value = self.importer.get_object(attr) |
paul@44 | 1065 | |
paul@44 | 1066 | # If the value is None, the attribute is |
paul@44 | 1067 | # inherited and need not be set explicitly on |
paul@44 | 1068 | # the class concerned. |
paul@44 | 1069 | |
paul@44 | 1070 | if value: |
paul@44 | 1071 | self.modified_attributes[attr] = value |
paul@44 | 1072 | self.importer.set_object(attr, value.as_var()) |
paul@44 | 1073 | |
paul@44 | 1074 | # Simplification of types. |
paul@44 | 1075 | |
paul@69 | 1076 | def get_most_general_types(self, types): |
paul@69 | 1077 | |
paul@69 | 1078 | "Return the most general types for the given 'types'." |
paul@69 | 1079 | |
paul@69 | 1080 | module_types = set() |
paul@69 | 1081 | class_types = set() |
paul@69 | 1082 | |
paul@69 | 1083 | for type in types: |
paul@69 | 1084 | ref = self.importer.identify(type) |
paul@69 | 1085 | if ref.has_kind("<module>"): |
paul@69 | 1086 | module_types.add(type) |
paul@69 | 1087 | else: |
paul@69 | 1088 | class_types.add(type) |
paul@69 | 1089 | |
paul@69 | 1090 | types = set(self.get_most_general_module_types(module_types)) |
paul@69 | 1091 | types.update(self.get_most_general_class_types(class_types)) |
paul@69 | 1092 | return types |
paul@69 | 1093 | |
paul@69 | 1094 | def get_most_general_class_types(self, class_types): |
paul@44 | 1095 | |
paul@44 | 1096 | "Return the most general types for the given 'class_types'." |
paul@44 | 1097 | |
paul@44 | 1098 | class_types = set(class_types) |
paul@44 | 1099 | to_remove = set() |
paul@44 | 1100 | |
paul@44 | 1101 | for class_type in class_types: |
paul@44 | 1102 | for base in self.importer.classes[class_type]: |
paul@44 | 1103 | base = base.get_origin() |
paul@44 | 1104 | descendants = self.descendants[base] |
paul@44 | 1105 | if base in class_types and descendants.issubset(class_types): |
paul@44 | 1106 | to_remove.update(descendants) |
paul@44 | 1107 | |
paul@44 | 1108 | class_types.difference_update(to_remove) |
paul@44 | 1109 | return class_types |
paul@44 | 1110 | |
paul@44 | 1111 | def get_most_general_module_types(self, module_types): |
paul@44 | 1112 | |
paul@44 | 1113 | "Return the most general type for the given 'module_types'." |
paul@44 | 1114 | |
paul@44 | 1115 | # Where all modules are provided, an object would provide the same |
paul@44 | 1116 | # attributes. |
paul@44 | 1117 | |
paul@44 | 1118 | if len(module_types) == len(self.importer.modules): |
paul@654 | 1119 | return [self.root_class_type] |
paul@44 | 1120 | else: |
paul@44 | 1121 | return module_types |
paul@44 | 1122 | |
paul@44 | 1123 | # More efficient usage-to-type indexing and retrieval. |
paul@44 | 1124 | |
paul@44 | 1125 | def init_attr_type_indexes(self): |
paul@44 | 1126 | |
paul@44 | 1127 | "Identify the types that can support each attribute name." |
paul@44 | 1128 | |
paul@44 | 1129 | self._init_attr_type_index(self.attr_class_types, self.importer.all_class_attrs) |
paul@107 | 1130 | self._init_attr_type_index(self.attr_instance_types, self.importer.all_instance_attrs, True) |
paul@107 | 1131 | self._init_attr_type_index(self.attr_instance_types, self.importer.all_combined_attrs, False) |
paul@44 | 1132 | self._init_attr_type_index(self.attr_module_types, self.importer.all_module_attrs) |
paul@44 | 1133 | |
paul@107 | 1134 | def _init_attr_type_index(self, attr_types, attrs, assignment=None): |
paul@44 | 1135 | |
paul@44 | 1136 | """ |
paul@44 | 1137 | Initialise the 'attr_types' attribute-to-types mapping using the given |
paul@44 | 1138 | 'attrs' type-to-attributes mapping. |
paul@44 | 1139 | """ |
paul@44 | 1140 | |
paul@44 | 1141 | for name, attrnames in attrs.items(): |
paul@44 | 1142 | for attrname in attrnames: |
paul@107 | 1143 | |
paul@107 | 1144 | # Permit general access for certain kinds of object. |
paul@107 | 1145 | |
paul@107 | 1146 | if assignment is None: |
paul@107 | 1147 | init_item(attr_types, (attrname, False), set) |
paul@107 | 1148 | init_item(attr_types, (attrname, True), set) |
paul@107 | 1149 | attr_types[(attrname, False)].add(name) |
paul@107 | 1150 | attr_types[(attrname, True)].add(name) |
paul@107 | 1151 | |
paul@107 | 1152 | # Restrict attribute assignment for instances. |
paul@107 | 1153 | |
paul@107 | 1154 | else: |
paul@107 | 1155 | init_item(attr_types, (attrname, assignment), set) |
paul@107 | 1156 | attr_types[(attrname, assignment)].add(name) |
paul@44 | 1157 | |
paul@88 | 1158 | def get_class_types_for_usage(self, usage): |
paul@88 | 1159 | |
paul@88 | 1160 | "Return names of classes supporting the given 'usage'." |
paul@88 | 1161 | |
paul@88 | 1162 | return self._get_types_for_usage(usage, self.attr_class_types, self.importer.all_class_attrs) |
paul@88 | 1163 | |
paul@88 | 1164 | def get_instance_types_for_usage(self, usage): |
paul@44 | 1165 | |
paul@44 | 1166 | """ |
paul@88 | 1167 | Return names of classes whose instances support the given 'usage' |
paul@44 | 1168 | (as either class or instance attributes). |
paul@44 | 1169 | """ |
paul@44 | 1170 | |
paul@88 | 1171 | return self._get_types_for_usage(usage, self.attr_instance_types, self.importer.all_combined_attrs) |
paul@88 | 1172 | |
paul@88 | 1173 | def get_module_types_for_usage(self, usage): |
paul@88 | 1174 | |
paul@88 | 1175 | "Return names of modules supporting the given 'usage'." |
paul@88 | 1176 | |
paul@88 | 1177 | return self._get_types_for_usage(usage, self.attr_module_types, self.importer.all_module_attrs) |
paul@88 | 1178 | |
paul@88 | 1179 | def _get_types_for_usage(self, usage, attr_types, attrs): |
paul@44 | 1180 | |
paul@44 | 1181 | """ |
paul@88 | 1182 | For the given 'usage' representing attribute usage, return types |
paul@44 | 1183 | recorded in the 'attr_types' attribute-to-types mapping that support |
paul@44 | 1184 | such usage, with the given 'attrs' type-to-attributes mapping used to |
paul@44 | 1185 | quickly assess whether a type supports all of the stated attributes. |
paul@44 | 1186 | """ |
paul@44 | 1187 | |
paul@44 | 1188 | # Where no attributes are used, any type would be acceptable. |
paul@44 | 1189 | |
paul@88 | 1190 | if not usage: |
paul@44 | 1191 | return attrs.keys() |
paul@44 | 1192 | |
paul@107 | 1193 | keys = [] |
paul@107 | 1194 | for attrname, invocation, assignment in usage: |
paul@107 | 1195 | keys.append((attrname, assignment)) |
paul@107 | 1196 | |
paul@107 | 1197 | # Obtain types supporting the first (attribute name, assignment) key... |
paul@107 | 1198 | |
paul@107 | 1199 | types = set(attr_types.get(keys[0]) or []) |
paul@107 | 1200 | |
paul@107 | 1201 | for key in keys[1:]: |
paul@107 | 1202 | |
paul@44 | 1203 | # Record types that support all of the other attributes as well. |
paul@44 | 1204 | |
paul@107 | 1205 | types.intersection_update(attr_types.get(key) or []) |
paul@44 | 1206 | |
paul@44 | 1207 | return types |
paul@44 | 1208 | |
paul@501 | 1209 | def init_combined_attribute_index(self): |
paul@501 | 1210 | |
paul@501 | 1211 | "Initialise a combined index for the detection of invalid attributes." |
paul@501 | 1212 | |
paul@501 | 1213 | self.all_attrnames = set() |
paul@501 | 1214 | for attrs in (self.importer.all_combined_attrs, self.importer.all_module_attrs): |
paul@501 | 1215 | for name, attrnames in attrs.items(): |
paul@501 | 1216 | self.all_attrnames.update(attrnames) |
paul@501 | 1217 | |
paul@44 | 1218 | # Reference identification. |
paul@44 | 1219 | |
paul@44 | 1220 | def identify_references(self): |
paul@44 | 1221 | |
paul@44 | 1222 | "Identify references using usage and name reference information." |
paul@44 | 1223 | |
paul@44 | 1224 | # Names with associated attribute usage. |
paul@44 | 1225 | |
paul@44 | 1226 | for location, usages in self.location_index.items(): |
paul@44 | 1227 | |
paul@44 | 1228 | # Obtain attribute usage associated with a name, deducing the nature |
paul@44 | 1229 | # of the name. Obtain types only for branches involving attribute |
paul@44 | 1230 | # usage. (In the absence of usage, any type could be involved, but |
paul@44 | 1231 | # then no accesses exist to require knowledge of the type.) |
paul@44 | 1232 | |
paul@44 | 1233 | have_usage = False |
paul@44 | 1234 | have_no_usage_branch = False |
paul@44 | 1235 | |
paul@44 | 1236 | for usage in usages: |
paul@44 | 1237 | if not usage: |
paul@44 | 1238 | have_no_usage_branch = True |
paul@44 | 1239 | continue |
paul@44 | 1240 | elif not have_usage: |
paul@44 | 1241 | self.init_definition_details(location) |
paul@44 | 1242 | have_usage = True |
paul@44 | 1243 | self.record_types_for_usage(location, usage) |
paul@44 | 1244 | |
paul@44 | 1245 | # Where some usage occurs, but where branches without usage also |
paul@44 | 1246 | # occur, record the types for those branches anyway. |
paul@44 | 1247 | |
paul@44 | 1248 | if have_usage and have_no_usage_branch: |
paul@44 | 1249 | self.init_definition_details(location) |
paul@44 | 1250 | self.record_types_for_usage(location, None) |
paul@44 | 1251 | |
paul@44 | 1252 | # Specific name-based attribute accesses. |
paul@44 | 1253 | |
paul@44 | 1254 | for access_location, accessor_locations in self.access_index.items(): |
paul@742 | 1255 | self.record_types_for_access(access_location, accessor_locations) |
paul@44 | 1256 | |
paul@44 | 1257 | # Anonymous references with attribute chains. |
paul@44 | 1258 | |
paul@791 | 1259 | for path, accesses in self.importer.all_attr_accesses.items(): |
paul@44 | 1260 | |
paul@44 | 1261 | # Get distinct attribute names. |
paul@44 | 1262 | |
paul@44 | 1263 | all_attrnames = set() |
paul@44 | 1264 | |
paul@44 | 1265 | for attrnames in accesses: |
paul@44 | 1266 | all_attrnames.update(get_attrnames(attrnames)) |
paul@44 | 1267 | |
paul@44 | 1268 | # Get attribute and accessor details for each attribute name. |
paul@44 | 1269 | |
paul@44 | 1270 | for attrname in all_attrnames: |
paul@791 | 1271 | access_location = AccessLocation(path, None, attrname, 0) |
paul@44 | 1272 | self.record_types_for_attribute(access_location, attrname) |
paul@44 | 1273 | |
paul@44 | 1274 | # References via constant/identified objects. |
paul@44 | 1275 | |
paul@791 | 1276 | for path, name_accesses in self.importer.all_const_accesses.items(): |
paul@44 | 1277 | |
paul@44 | 1278 | # A mapping from the original name and attributes to resolved access |
paul@44 | 1279 | # details. |
paul@44 | 1280 | |
paul@44 | 1281 | for original_access, access in name_accesses.items(): |
paul@44 | 1282 | original_name, original_attrnames = original_access |
paul@44 | 1283 | objpath, ref, attrnames = access |
paul@44 | 1284 | |
paul@44 | 1285 | # Build an accessor combining the name and attribute names used. |
paul@44 | 1286 | |
paul@44 | 1287 | original_accessor = tuple([original_name] + original_attrnames.split(".")) |
paul@44 | 1288 | |
paul@44 | 1289 | # Direct accesses to attributes. |
paul@44 | 1290 | |
paul@44 | 1291 | if not attrnames: |
paul@44 | 1292 | |
paul@44 | 1293 | # Build a descriptive location based on the original |
paul@44 | 1294 | # details, exposing the final attribute name. |
paul@44 | 1295 | |
paul@44 | 1296 | oa, attrname = original_accessor[:-1], original_accessor[-1] |
paul@44 | 1297 | oa = ".".join(oa) |
paul@44 | 1298 | |
paul@791 | 1299 | access_location = AccessLocation(path, oa, attrname, 0) |
paul@791 | 1300 | accessor_location = Location(path, oa, None, 0) |
paul@44 | 1301 | self.access_index[access_location] = [accessor_location] |
paul@44 | 1302 | |
paul@44 | 1303 | self.init_access_details(access_location) |
paul@44 | 1304 | self.init_definition_details(accessor_location) |
paul@44 | 1305 | |
paul@44 | 1306 | # Obtain a reference for the accessor in order to properly |
paul@44 | 1307 | # determine its type. |
paul@44 | 1308 | |
paul@44 | 1309 | if ref.get_kind() != "<instance>": |
paul@44 | 1310 | objpath = ref.get_origin() |
paul@44 | 1311 | |
paul@44 | 1312 | objpath = objpath.rsplit(".", 1)[0] |
paul@44 | 1313 | |
paul@44 | 1314 | # Where the object name conflicts with the module |
paul@44 | 1315 | # providing it, obtain the module details. |
paul@44 | 1316 | |
paul@44 | 1317 | if objpath in self.importer.modules: |
paul@44 | 1318 | accessor = Reference("<module>", objpath) |
paul@44 | 1319 | else: |
paul@44 | 1320 | accessor = self.importer.get_object(objpath) |
paul@44 | 1321 | |
paul@44 | 1322 | self.referenced_attrs[access_location] = [(accessor.get_kind(), accessor.get_origin(), ref)] |
paul@44 | 1323 | self.access_constrained.add(access_location) |
paul@44 | 1324 | |
paul@57 | 1325 | class_types, instance_types, module_types = accessor.get_types() |
paul@44 | 1326 | self.record_reference_types(accessor_location, class_types, instance_types, module_types, True, True) |
paul@64 | 1327 | |
paul@64 | 1328 | else: |
paul@44 | 1329 | |
paul@64 | 1330 | # Build a descriptive location based on the original |
paul@64 | 1331 | # details, employing the first remaining attribute name. |
paul@64 | 1332 | |
paul@64 | 1333 | l = get_attrnames(attrnames) |
paul@64 | 1334 | attrname = l[0] |
paul@44 | 1335 | |
paul@64 | 1336 | oa = original_accessor[:-len(l)] |
paul@64 | 1337 | oa = ".".join(oa) |
paul@44 | 1338 | |
paul@791 | 1339 | access_location = AccessLocation(path, oa, attrnames, 0) |
paul@791 | 1340 | accessor_location = Location(path, oa, None, 0) |
paul@64 | 1341 | self.access_index[access_location] = [accessor_location] |
paul@64 | 1342 | |
paul@64 | 1343 | self.init_access_details(access_location) |
paul@64 | 1344 | self.init_definition_details(accessor_location) |
paul@44 | 1345 | |
paul@64 | 1346 | class_types, instance_types, module_types = ref.get_types() |
paul@64 | 1347 | |
paul@64 | 1348 | self.identify_reference_attributes(access_location, attrname, class_types, instance_types, module_types, True) |
paul@64 | 1349 | self.record_reference_types(accessor_location, class_types, instance_types, module_types, True, True) |
paul@64 | 1350 | |
paul@679 | 1351 | # Define mappings between the original and access locations |
paul@679 | 1352 | # so that translation can work from the source details. |
paul@679 | 1353 | |
paul@791 | 1354 | original_location = AccessLocation(path, original_name, original_attrnames, 0) |
paul@64 | 1355 | |
paul@64 | 1356 | if original_location != access_location: |
paul@64 | 1357 | self.const_accesses[original_location] = access_location |
paul@67 | 1358 | self.const_accesses_rev[access_location] = original_location |
paul@44 | 1359 | |
paul@735 | 1360 | # Propagate alias-related information. |
paul@735 | 1361 | |
paul@735 | 1362 | affected_aliases = set(self.alias_index.keys()) |
paul@735 | 1363 | |
paul@735 | 1364 | while True: |
paul@735 | 1365 | |
paul@735 | 1366 | # Aliased name definitions. All aliases with usage will have been |
paul@735 | 1367 | # defined, but they may be refined according to referenced accesses. |
paul@735 | 1368 | |
paul@735 | 1369 | updated_aliases = set() |
paul@735 | 1370 | |
paul@735 | 1371 | for location in affected_aliases: |
paul@735 | 1372 | if self.record_types_for_alias(location): |
paul@735 | 1373 | updated_aliases.add(location) |
paul@735 | 1374 | |
paul@742 | 1375 | # Define accesses employing aliases. |
paul@742 | 1376 | |
paul@742 | 1377 | alias_accesses = set() |
paul@742 | 1378 | affected_aliases = set() |
paul@742 | 1379 | |
paul@742 | 1380 | for alias in updated_aliases: |
paul@742 | 1381 | |
paul@742 | 1382 | # Access affected by the alias. |
paul@742 | 1383 | |
paul@742 | 1384 | if self.access_index_rev.has_key(alias): |
paul@742 | 1385 | alias_accesses.update(self.access_index_rev[alias]) |
paul@742 | 1386 | |
paul@742 | 1387 | # Another alias affected by the alias. |
paul@742 | 1388 | |
paul@742 | 1389 | elif self.alias_index_rev.has_key(alias): |
paul@742 | 1390 | affected_aliases.update(self.alias_index_rev[alias]) |
paul@742 | 1391 | |
paul@735 | 1392 | # Update accesses employing aliases. |
paul@735 | 1393 | |
paul@735 | 1394 | updated_accesses = set() |
paul@735 | 1395 | |
paul@735 | 1396 | for access_location in alias_accesses: |
paul@735 | 1397 | if self.record_types_for_access(access_location, self.access_index[access_location]): |
paul@735 | 1398 | updated_accesses.add(access_location) |
paul@735 | 1399 | |
paul@742 | 1400 | # Determine which aliases are affected by the updated accesses. |
paul@735 | 1401 | |
paul@735 | 1402 | for access_location in updated_accesses: |
paul@735 | 1403 | if self.alias_index_rev.has_key(access_location): |
paul@735 | 1404 | affected_aliases.update(self.alias_index_rev[access_location]) |
paul@735 | 1405 | |
paul@735 | 1406 | if not affected_aliases: |
paul@735 | 1407 | break |
paul@44 | 1408 | |
paul@44 | 1409 | def constrain_types(self, path, class_types, instance_types, module_types): |
paul@44 | 1410 | |
paul@44 | 1411 | """ |
paul@44 | 1412 | Using the given 'path' to an object, constrain the given 'class_types', |
paul@44 | 1413 | 'instance_types' and 'module_types'. |
paul@44 | 1414 | |
paul@44 | 1415 | Return the class, instance, module types plus whether the types are |
paul@44 | 1416 | constrained to a specific kind of type. |
paul@44 | 1417 | """ |
paul@44 | 1418 | |
paul@44 | 1419 | ref = self.importer.identify(path) |
paul@44 | 1420 | if ref: |
paul@44 | 1421 | |
paul@44 | 1422 | # Constrain usage suggestions using the identified object. |
paul@44 | 1423 | |
paul@44 | 1424 | if ref.has_kind("<class>"): |
paul@44 | 1425 | return ( |
paul@44 | 1426 | set(class_types).intersection([ref.get_origin()]), [], [], True |
paul@44 | 1427 | ) |
paul@44 | 1428 | elif ref.has_kind("<module>"): |
paul@44 | 1429 | return ( |
paul@44 | 1430 | [], [], set(module_types).intersection([ref.get_origin()]), True |
paul@44 | 1431 | ) |
paul@44 | 1432 | |
paul@44 | 1433 | return class_types, instance_types, module_types, False |
paul@44 | 1434 | |
paul@44 | 1435 | def get_target_types(self, location, usage): |
paul@44 | 1436 | |
paul@44 | 1437 | """ |
paul@44 | 1438 | Return the class, instance and module types constrained for the name at |
paul@44 | 1439 | the given 'location' exhibiting the given 'usage'. Whether the types |
paul@44 | 1440 | have been constrained using contextual information is also indicated, |
paul@44 | 1441 | plus whether the types have been constrained to a specific kind of type. |
paul@44 | 1442 | """ |
paul@44 | 1443 | |
paul@107 | 1444 | have_assignments = get_assigned_attributes(usage) |
paul@44 | 1445 | |
paul@44 | 1446 | # Detect any initialised name for the location. |
paul@44 | 1447 | |
paul@791 | 1448 | if location.name: |
paul@742 | 1449 | refs = self.get_initialised_name(location) |
paul@742 | 1450 | if refs: |
paul@44 | 1451 | (class_types, only_instance_types, module_types, |
paul@742 | 1452 | _function_types, _var_types) = separate_types(refs) |
paul@107 | 1453 | return class_types, only_instance_types, module_types, True, have_assignments |
paul@44 | 1454 | |
paul@44 | 1455 | # Retrieve the recorded types for the usage. |
paul@44 | 1456 | |
paul@44 | 1457 | class_types = self.get_class_types_for_usage(usage) |
paul@44 | 1458 | only_instance_types = set(self.get_instance_types_for_usage(usage)).difference(class_types) |
paul@44 | 1459 | module_types = self.get_module_types_for_usage(usage) |
paul@44 | 1460 | |
paul@44 | 1461 | # Merge usage deductions with observations to obtain reference types |
paul@44 | 1462 | # for names involved with attribute accesses. |
paul@44 | 1463 | |
paul@791 | 1464 | if not location.name: |
paul@107 | 1465 | return class_types, only_instance_types, module_types, False, have_assignments |
paul@44 | 1466 | |
paul@44 | 1467 | # Obtain references to known objects. |
paul@44 | 1468 | |
paul@791 | 1469 | path = get_name_path(location.path, location.name) |
paul@44 | 1470 | |
paul@44 | 1471 | class_types, only_instance_types, module_types, constrained_specific = \ |
paul@44 | 1472 | self.constrain_types(path, class_types, only_instance_types, module_types) |
paul@44 | 1473 | |
paul@44 | 1474 | if constrained_specific: |
paul@107 | 1475 | return class_types, only_instance_types, module_types, constrained_specific, \ |
paul@107 | 1476 | constrained_specific or have_assignments |
paul@44 | 1477 | |
paul@44 | 1478 | # Constrain "self" references. |
paul@44 | 1479 | |
paul@816 | 1480 | class_name = self.in_method(location.path) |
paul@816 | 1481 | constrained = False |
paul@816 | 1482 | |
paul@816 | 1483 | if class_name and location.name == "self": |
paul@816 | 1484 | |
paul@816 | 1485 | # Constrain the types to the class's hierarchy. |
paul@816 | 1486 | |
paul@816 | 1487 | class_types, only_instance_types, module_types, constrained = \ |
paul@816 | 1488 | self.constrain_to_class(class_name, class_types, only_instance_types) |
paul@816 | 1489 | |
paul@816 | 1490 | # Without any deduced types, produce an error. |
paul@816 | 1491 | |
paul@816 | 1492 | if not class_types and not only_instance_types: |
paul@350 | 1493 | raise DeduceError("In %s, usage {%s} is not directly supported by class %s or its instances." % |
paul@791 | 1494 | (location.path, encode_usage(usage), class_name)) |
paul@350 | 1495 | |
paul@816 | 1496 | # If the class defining the method does not appear amongst the |
paul@816 | 1497 | # types supporting the usage, remove the constrained status of the |
paul@816 | 1498 | # name. |
paul@816 | 1499 | |
paul@816 | 1500 | constrained = constrained and (class_name in class_types or class_name in only_instance_types) |
paul@816 | 1501 | |
paul@816 | 1502 | return class_types, only_instance_types, module_types, constrained, have_assignments |
paul@44 | 1503 | |
paul@44 | 1504 | def constrain_self_reference(self, unit_path, class_types, only_instance_types): |
paul@44 | 1505 | |
paul@44 | 1506 | """ |
paul@44 | 1507 | Where the name "self" appears in a method, attempt to constrain the |
paul@44 | 1508 | classes involved. |
paul@44 | 1509 | |
paul@44 | 1510 | Return the class, instance, module types plus whether the types are |
paul@44 | 1511 | constrained. |
paul@44 | 1512 | """ |
paul@44 | 1513 | |
paul@44 | 1514 | class_name = self.in_method(unit_path) |
paul@44 | 1515 | |
paul@44 | 1516 | if not class_name: |
paul@44 | 1517 | return None |
paul@44 | 1518 | |
paul@816 | 1519 | return self.constrain_to_class(class_name, class_types, only_instance_types) |
paul@816 | 1520 | |
paul@816 | 1521 | def constrain_to_class(self, class_name, class_types, only_instance_types): |
paul@816 | 1522 | |
paul@816 | 1523 | """ |
paul@816 | 1524 | Constrain to 'class_name' and its descendants, the given 'class_types' |
paul@816 | 1525 | and 'only_instance_types'. |
paul@816 | 1526 | |
paul@816 | 1527 | Return the class, instance, module types plus whether the types are |
paul@816 | 1528 | constrained. |
paul@816 | 1529 | """ |
paul@816 | 1530 | |
paul@44 | 1531 | classes = set([class_name]) |
paul@44 | 1532 | classes.update(self.get_descendants_for_class(class_name)) |
paul@44 | 1533 | |
paul@44 | 1534 | # Note that only instances will be expected for these references but |
paul@44 | 1535 | # either classes or instances may provide the attributes. |
paul@44 | 1536 | |
paul@44 | 1537 | return ( |
paul@44 | 1538 | set(class_types).intersection(classes), |
paul@44 | 1539 | set(only_instance_types).intersection(classes), |
paul@44 | 1540 | [], True |
paul@44 | 1541 | ) |
paul@44 | 1542 | |
paul@44 | 1543 | def in_method(self, path): |
paul@44 | 1544 | |
paul@44 | 1545 | "Return whether 'path' refers to a method." |
paul@44 | 1546 | |
paul@816 | 1547 | t = path.rsplit(".", 1) |
paul@816 | 1548 | if len(t) == 1: |
paul@816 | 1549 | return False |
paul@816 | 1550 | |
paul@816 | 1551 | class_name, method_name = t |
paul@274 | 1552 | return class_name != "__builtins__.core.type" and self.importer.classes.has_key(class_name) and class_name |
paul@44 | 1553 | |
paul@44 | 1554 | def init_reference_details(self, location): |
paul@44 | 1555 | |
paul@44 | 1556 | "Initialise reference-related details for 'location'." |
paul@44 | 1557 | |
paul@44 | 1558 | self.init_definition_details(location) |
paul@44 | 1559 | self.init_access_details(location) |
paul@44 | 1560 | |
paul@44 | 1561 | def init_definition_details(self, location): |
paul@44 | 1562 | |
paul@44 | 1563 | "Initialise name definition details for 'location'." |
paul@44 | 1564 | |
paul@44 | 1565 | self.accessor_class_types[location] = set() |
paul@44 | 1566 | self.accessor_instance_types[location] = set() |
paul@44 | 1567 | self.accessor_module_types[location] = set() |
paul@44 | 1568 | self.provider_class_types[location] = set() |
paul@44 | 1569 | self.provider_instance_types[location] = set() |
paul@44 | 1570 | self.provider_module_types[location] = set() |
paul@44 | 1571 | |
paul@44 | 1572 | def init_access_details(self, location): |
paul@44 | 1573 | |
paul@44 | 1574 | "Initialise access details at 'location'." |
paul@44 | 1575 | |
paul@44 | 1576 | self.referenced_attrs[location] = {} |
paul@44 | 1577 | |
paul@742 | 1578 | def record_types_for_access(self, access_location, accessor_locations): |
paul@44 | 1579 | |
paul@44 | 1580 | """ |
paul@44 | 1581 | Define types for the 'access_location' associated with the given |
paul@735 | 1582 | 'accessor_locations'. Return whether referenced attributes were updated. |
paul@44 | 1583 | """ |
paul@44 | 1584 | |
paul@791 | 1585 | attrname = access_location.get_attrname() |
paul@91 | 1586 | if not attrname: |
paul@735 | 1587 | return False |
paul@44 | 1588 | |
paul@720 | 1589 | invocation = access_location in self.reference_invocations |
paul@720 | 1590 | assignment = access_location in self.reference_assignments |
paul@720 | 1591 | |
paul@44 | 1592 | # Collect all suggested types for the accessors. Accesses may |
paul@44 | 1593 | # require accessors from of a subset of the complete set of types. |
paul@44 | 1594 | |
paul@44 | 1595 | class_types = set() |
paul@44 | 1596 | module_types = set() |
paul@44 | 1597 | instance_types = set() |
paul@44 | 1598 | |
paul@44 | 1599 | constrained = True |
paul@44 | 1600 | |
paul@735 | 1601 | old_referenced_attrs = self.referenced_attrs.get(access_location) |
paul@735 | 1602 | |
paul@44 | 1603 | for location in accessor_locations: |
paul@44 | 1604 | |
paul@44 | 1605 | # Use the type information deduced for names from above. |
paul@44 | 1606 | |
paul@44 | 1607 | if self.accessor_class_types.has_key(location): |
paul@44 | 1608 | class_types.update(self.accessor_class_types[location]) |
paul@44 | 1609 | module_types.update(self.accessor_module_types[location]) |
paul@44 | 1610 | instance_types.update(self.accessor_instance_types[location]) |
paul@44 | 1611 | |
paul@44 | 1612 | # Where accesses are associated with assignments but where no |
paul@44 | 1613 | # attribute usage observations have caused such an association, |
paul@44 | 1614 | # the attribute name is considered by itself. |
paul@44 | 1615 | |
paul@44 | 1616 | else: |
paul@44 | 1617 | self.init_definition_details(location) |
paul@720 | 1618 | self.record_types_for_usage(location, [(attrname, invocation, assignment)]) |
paul@44 | 1619 | |
paul@67 | 1620 | constrained = location in self.accessor_constrained and constrained |
paul@44 | 1621 | |
paul@44 | 1622 | self.init_access_details(access_location) |
paul@44 | 1623 | self.identify_reference_attributes(access_location, attrname, class_types, instance_types, module_types, constrained) |
paul@44 | 1624 | |
paul@735 | 1625 | # Return whether the referenced attributes have changed. |
paul@735 | 1626 | |
paul@735 | 1627 | return old_referenced_attrs != self.referenced_attrs.get(access_location) |
paul@735 | 1628 | |
paul@44 | 1629 | def record_types_for_usage(self, accessor_location, usage): |
paul@44 | 1630 | |
paul@44 | 1631 | """ |
paul@44 | 1632 | Record types for the given 'accessor_location' according to the given |
paul@44 | 1633 | 'usage' observations which may be None to indicate an absence of usage. |
paul@44 | 1634 | """ |
paul@44 | 1635 | |
paul@44 | 1636 | (class_types, |
paul@44 | 1637 | instance_types, |
paul@44 | 1638 | module_types, |
paul@44 | 1639 | constrained, |
paul@44 | 1640 | constrained_specific) = self.get_target_types(accessor_location, usage) |
paul@44 | 1641 | |
paul@90 | 1642 | invocations = get_invoked_attributes(usage) |
paul@90 | 1643 | |
paul@107 | 1644 | self.record_reference_types(accessor_location, class_types, instance_types, |
paul@107 | 1645 | module_types, constrained, constrained_specific, invocations) |
paul@44 | 1646 | |
paul@44 | 1647 | def record_types_for_attribute(self, access_location, attrname): |
paul@44 | 1648 | |
paul@44 | 1649 | """ |
paul@44 | 1650 | Record types for the 'access_location' employing only the given |
paul@44 | 1651 | 'attrname' for type deduction. |
paul@44 | 1652 | """ |
paul@44 | 1653 | |
paul@102 | 1654 | (class_types, |
paul@102 | 1655 | only_instance_types, |
paul@102 | 1656 | module_types) = self.get_types_for_attribute(attrname) |
paul@102 | 1657 | |
paul@102 | 1658 | self.init_reference_details(access_location) |
paul@102 | 1659 | |
paul@102 | 1660 | self.identify_reference_attributes(access_location, attrname, class_types, only_instance_types, module_types, False) |
paul@102 | 1661 | self.record_reference_types(access_location, class_types, only_instance_types, module_types, False) |
paul@102 | 1662 | |
paul@102 | 1663 | def get_types_for_attribute(self, attrname): |
paul@102 | 1664 | |
paul@102 | 1665 | "Return class, instance-only and module types supporting 'attrname'." |
paul@102 | 1666 | |
paul@107 | 1667 | usage = ((attrname, False, False),) |
paul@44 | 1668 | |
paul@44 | 1669 | class_types = self.get_class_types_for_usage(usage) |
paul@44 | 1670 | only_instance_types = set(self.get_instance_types_for_usage(usage)).difference(class_types) |
paul@44 | 1671 | module_types = self.get_module_types_for_usage(usage) |
paul@44 | 1672 | |
paul@102 | 1673 | return class_types, only_instance_types, module_types |
paul@44 | 1674 | |
paul@44 | 1675 | def record_types_for_alias(self, accessor_location): |
paul@44 | 1676 | |
paul@44 | 1677 | """ |
paul@44 | 1678 | Define types for the 'accessor_location' not having associated usage. |
paul@735 | 1679 | Return whether the types were updated. |
paul@44 | 1680 | """ |
paul@44 | 1681 | |
paul@44 | 1682 | have_access = self.provider_class_types.has_key(accessor_location) |
paul@44 | 1683 | |
paul@44 | 1684 | # With an access, attempt to narrow the existing selection of provider |
paul@709 | 1685 | # types. Invocations attempt to find return value information, with |
paul@709 | 1686 | # instance return values also yielding class providers (since attributes |
paul@709 | 1687 | # on instances could be provided by classes). |
paul@44 | 1688 | |
paul@44 | 1689 | if have_access: |
paul@44 | 1690 | provider_class_types = self.provider_class_types[accessor_location] |
paul@44 | 1691 | provider_instance_types = self.provider_instance_types[accessor_location] |
paul@44 | 1692 | provider_module_types = self.provider_module_types[accessor_location] |
paul@44 | 1693 | |
paul@721 | 1694 | accessor_class_types = self.accessor_class_types[accessor_location] |
paul@721 | 1695 | accessor_instance_types = self.accessor_instance_types[accessor_location] |
paul@721 | 1696 | accessor_module_types = self.accessor_module_types[accessor_location] |
paul@721 | 1697 | |
paul@44 | 1698 | # Find details for any corresponding access. |
paul@44 | 1699 | |
paul@721 | 1700 | new_provider_class_types = set() |
paul@721 | 1701 | new_provider_instance_types = set() |
paul@721 | 1702 | new_provider_module_types = set() |
paul@721 | 1703 | |
paul@721 | 1704 | new_accessor_class_types = set() |
paul@721 | 1705 | new_accessor_instance_types = set() |
paul@721 | 1706 | new_accessor_module_types = set() |
paul@44 | 1707 | |
paul@742 | 1708 | refs = set() |
paul@742 | 1709 | |
paul@44 | 1710 | for access_location in self.alias_index[accessor_location]: |
paul@791 | 1711 | attrnames = access_location.attrnames |
paul@791 | 1712 | invocation = self.reference_invocations.has_key(access_location) |
paul@44 | 1713 | |
paul@715 | 1714 | attrnames = attrnames and attrnames.split(".") |
paul@715 | 1715 | remaining = attrnames and len(attrnames) > 1 |
paul@715 | 1716 | |
paul@715 | 1717 | # Alias has remaining attributes: reference details do not |
paul@715 | 1718 | # correspond to the accessor; the remaining attributes would |
paul@715 | 1719 | # need to be traversed first. |
paul@715 | 1720 | |
paul@715 | 1721 | if remaining: |
paul@735 | 1722 | return False |
paul@715 | 1723 | |
paul@44 | 1724 | # Alias references an attribute access. |
paul@44 | 1725 | |
paul@44 | 1726 | if attrnames: |
paul@44 | 1727 | |
paul@709 | 1728 | # Obtain references and attribute types for the access. |
paul@44 | 1729 | |
paul@708 | 1730 | attrs = self.get_references_for_access(access_location) |
paul@721 | 1731 | |
paul@744 | 1732 | if attrs: |
paul@744 | 1733 | |
paul@744 | 1734 | # Invocations converting class accessors to instances do |
paul@744 | 1735 | # not change the nature of class providers. |
paul@744 | 1736 | |
paul@744 | 1737 | provider_attrs = self.convert_invocation_providers(attrs, invocation) |
paul@744 | 1738 | |
paul@744 | 1739 | # Accessors are updated separately, employing invocation |
paul@744 | 1740 | # result details. |
paul@744 | 1741 | |
paul@744 | 1742 | accessor_attrs = self.convert_invocations(attrs, invocation) |
paul@742 | 1743 | |
paul@44 | 1744 | # Alias references a name, not an access. |
paul@44 | 1745 | |
paul@44 | 1746 | else: |
paul@742 | 1747 | # Attempt to refine the types using initialised names or |
paul@742 | 1748 | # accessors. |
paul@742 | 1749 | |
paul@791 | 1750 | attrs = self.get_initialised_access(access_location) |
paul@742 | 1751 | |
paul@742 | 1752 | if attrs: |
paul@721 | 1753 | provider_attrs = self.convert_invocation_providers(attrs, invocation) |
paul@721 | 1754 | accessor_attrs = self.convert_invocations(attrs, invocation) |
paul@742 | 1755 | else: |
paul@742 | 1756 | provider_attrs = self.get_provider_references(access_location) |
paul@742 | 1757 | attrs = accessor_attrs = self.get_accessor_references(access_location) |
paul@44 | 1758 | |
paul@744 | 1759 | # Where no specific attributes are defined, do not attempt |
paul@744 | 1760 | # to refine the alias's types. |
paul@744 | 1761 | |
paul@744 | 1762 | if not attrs: |
paul@744 | 1763 | return False |
paul@744 | 1764 | |
paul@744 | 1765 | (class_types, instance_types, module_types, function_types, |
paul@744 | 1766 | var_types) = separate_types(provider_attrs) |
paul@744 | 1767 | |
paul@744 | 1768 | # Where non-accessor types are found, do not attempt to refine |
paul@744 | 1769 | # the defined accessor types. |
paul@744 | 1770 | |
paul@744 | 1771 | if function_types or var_types: |
paul@744 | 1772 | return False |
paul@744 | 1773 | |
paul@744 | 1774 | class_types = set(provider_class_types).intersection(class_types) |
paul@744 | 1775 | instance_types = set(provider_instance_types).intersection(instance_types) |
paul@744 | 1776 | module_types = set(provider_module_types).intersection(module_types) |
paul@744 | 1777 | |
paul@744 | 1778 | new_provider_class_types.update(class_types) |
paul@744 | 1779 | new_provider_instance_types.update(instance_types) |
paul@744 | 1780 | new_provider_module_types.update(module_types) |
paul@744 | 1781 | |
paul@744 | 1782 | (class_types, instance_types, module_types, function_types, |
paul@744 | 1783 | var_types) = separate_types(accessor_attrs) |
paul@744 | 1784 | |
paul@744 | 1785 | class_types = set(accessor_class_types).intersection(class_types) |
paul@744 | 1786 | instance_types = set(accessor_instance_types).intersection(instance_types) |
paul@744 | 1787 | module_types = set(accessor_module_types).intersection(module_types) |
paul@744 | 1788 | |
paul@744 | 1789 | new_accessor_class_types.update(class_types) |
paul@744 | 1790 | new_accessor_instance_types.update(instance_types) |
paul@744 | 1791 | new_accessor_module_types.update(module_types) |
paul@744 | 1792 | |
paul@744 | 1793 | refs.update(accessor_attrs) |
paul@742 | 1794 | |
paul@742 | 1795 | # Update the alias relationships for invocations. |
paul@742 | 1796 | |
paul@742 | 1797 | self.update_alias_accesses(access_location, attrs) |
paul@742 | 1798 | |
paul@44 | 1799 | # Record refined type details for the alias as an accessor. |
paul@44 | 1800 | |
paul@44 | 1801 | self.init_definition_details(accessor_location) |
paul@721 | 1802 | self.update_provider_types(accessor_location, new_provider_class_types, new_provider_instance_types, new_provider_module_types) |
paul@721 | 1803 | self.update_accessor_types(accessor_location, new_accessor_class_types, new_accessor_instance_types, new_accessor_module_types) |
paul@44 | 1804 | |
paul@742 | 1805 | # Record reference details for the alias separately from accessors. |
paul@742 | 1806 | |
paul@742 | 1807 | self.referenced_objects[accessor_location] = refs |
paul@742 | 1808 | |
paul@735 | 1809 | return new_accessor_class_types != accessor_class_types or \ |
paul@735 | 1810 | new_accessor_instance_types != accessor_instance_types or \ |
paul@735 | 1811 | new_accessor_module_types != accessor_module_types |
paul@735 | 1812 | |
paul@44 | 1813 | # Without an access, attempt to identify references for the alias. |
paul@709 | 1814 | # Invocations convert classes to instances and also attempt to find |
paul@709 | 1815 | # return value information. |
paul@44 | 1816 | |
paul@44 | 1817 | else: |
paul@44 | 1818 | refs = set() |
paul@44 | 1819 | |
paul@44 | 1820 | for access_location in self.alias_index[accessor_location]: |
paul@791 | 1821 | attrnames = access_location.attrnames |
paul@791 | 1822 | invocation = self.reference_invocations.has_key(access_location) |
paul@709 | 1823 | |
paul@683 | 1824 | attrnames = attrnames and attrnames.split(".") |
paul@683 | 1825 | remaining = attrnames and len(attrnames) > 1 |
paul@683 | 1826 | |
paul@683 | 1827 | # Alias has remaining attributes: reference details do not |
paul@683 | 1828 | # correspond to the accessor; the remaining attributes would |
paul@683 | 1829 | # need to be traversed first. |
paul@683 | 1830 | |
paul@683 | 1831 | if remaining: |
paul@735 | 1832 | return False |
paul@44 | 1833 | |
paul@44 | 1834 | # Alias references an attribute access. |
paul@44 | 1835 | |
paul@721 | 1836 | if attrnames: |
paul@709 | 1837 | |
paul@709 | 1838 | # Obtain references and attribute types for the access. |
paul@709 | 1839 | |
paul@708 | 1840 | attrs = self.get_references_for_access(access_location) |
paul@742 | 1841 | |
paul@44 | 1842 | # Alias references a name, not an access. |
paul@44 | 1843 | |
paul@44 | 1844 | else: |
paul@709 | 1845 | |
paul@709 | 1846 | # Obtain initialiser information. |
paul@709 | 1847 | |
paul@791 | 1848 | attrs = self.get_initialised_access(access_location) |
paul@742 | 1849 | |
paul@742 | 1850 | if attrs: |
paul@742 | 1851 | provider_attrs = self.convert_invocation_providers(attrs, invocation) |
paul@742 | 1852 | accessor_attrs = self.convert_invocations(attrs, invocation) |
paul@742 | 1853 | else: |
paul@742 | 1854 | provider_attrs = self.get_provider_references(access_location) |
paul@742 | 1855 | attrs = accessor_attrs = self.get_accessor_references(access_location) |
paul@742 | 1856 | |
paul@744 | 1857 | # Where no further information is found, do not attempt to |
paul@744 | 1858 | # refine the defined accessor types. |
paul@744 | 1859 | |
paul@744 | 1860 | if not attrs: |
paul@744 | 1861 | return False |
paul@744 | 1862 | |
paul@744 | 1863 | refs.update(self.convert_invocations(attrs, invocation)) |
paul@742 | 1864 | |
paul@742 | 1865 | # Update the alias relationships for invocations. |
paul@742 | 1866 | |
paul@742 | 1867 | self.update_alias_accesses(access_location, attrs) |
paul@742 | 1868 | |
paul@742 | 1869 | # Record refined type details for the alias as an accessor. |
paul@742 | 1870 | |
paul@742 | 1871 | (class_types, instance_types, module_types, function_types, |
paul@742 | 1872 | var_types) = separate_types(refs) |
paul@742 | 1873 | |
paul@742 | 1874 | # Where non-accessor types are found, do not attempt to refine |
paul@742 | 1875 | # the defined accessor types. |
paul@742 | 1876 | |
paul@742 | 1877 | if not function_types and not var_types: |
paul@742 | 1878 | self.init_definition_details(accessor_location) |
paul@742 | 1879 | self.update_provider_types(accessor_location, class_types + instance_types, class_types + instance_types, module_types) |
paul@742 | 1880 | self.update_accessor_types(accessor_location, class_types, instance_types, module_types) |
paul@709 | 1881 | |
paul@44 | 1882 | # Record reference details for the alias separately from accessors. |
paul@44 | 1883 | |
paul@44 | 1884 | self.referenced_objects[accessor_location] = refs |
paul@44 | 1885 | |
paul@742 | 1886 | return True |
paul@742 | 1887 | |
paul@742 | 1888 | def update_alias_accesses(self, access_location, refs): |
paul@742 | 1889 | |
paul@742 | 1890 | """ |
paul@742 | 1891 | Record 'access_location' as a location affected by the return values of |
paul@742 | 1892 | 'refs' if an invocation is involved. |
paul@742 | 1893 | """ |
paul@742 | 1894 | |
paul@742 | 1895 | if not self.reference_invocations.has_key(access_location): |
paul@742 | 1896 | return |
paul@742 | 1897 | |
paul@742 | 1898 | for ref in refs: |
paul@742 | 1899 | |
paul@742 | 1900 | # Initialising accesses originate from the return values. |
paul@742 | 1901 | |
paul@742 | 1902 | key = (ref.get_origin(), "$return") |
paul@742 | 1903 | self._update_alias_accesses(access_location, key, self.importer.all_initialised_names) |
paul@742 | 1904 | self._update_alias_accesses(access_location, key, self.importer.all_aliased_names) |
paul@742 | 1905 | |
paul@742 | 1906 | def _update_alias_accesses(self, access_location, key, names): |
paul@742 | 1907 | |
paul@742 | 1908 | """ |
paul@742 | 1909 | Make each return value provide information to the given |
paul@742 | 1910 | 'access_location', using 'key' to reference the return value and 'names' |
paul@742 | 1911 | as the collection of definitions. |
paul@742 | 1912 | """ |
paul@742 | 1913 | |
paul@742 | 1914 | versions = names.get(key) |
paul@742 | 1915 | if not versions: |
paul@742 | 1916 | return |
paul@742 | 1917 | |
paul@791 | 1918 | path, name = key |
paul@791 | 1919 | |
paul@742 | 1920 | for version in versions.keys(): |
paul@791 | 1921 | location = Location(path, name, None, version) |
paul@742 | 1922 | l = init_item(self.alias_index_rev, location, set) |
paul@742 | 1923 | l.add(access_location) |
paul@742 | 1924 | |
paul@742 | 1925 | l = init_item(self.alias_index, access_location, set) |
paul@742 | 1926 | l.add(location) |
paul@735 | 1927 | |
paul@708 | 1928 | def get_references_for_access(self, access_location): |
paul@708 | 1929 | |
paul@708 | 1930 | "Return the references identified for 'access_location'." |
paul@708 | 1931 | |
paul@708 | 1932 | attrs = [] |
paul@708 | 1933 | for attrtype, object_type, attr in self.referenced_attrs[access_location]: |
paul@708 | 1934 | attrs.append(attr) |
paul@708 | 1935 | return attrs |
paul@708 | 1936 | |
paul@709 | 1937 | def convert_invocation_providers(self, refs, invocation): |
paul@709 | 1938 | |
paul@709 | 1939 | """ |
paul@709 | 1940 | Convert 'refs' to providers corresponding to the results of invoking |
paul@709 | 1941 | each of the given references, if 'invocation' is set to a true value. |
paul@709 | 1942 | """ |
paul@709 | 1943 | |
paul@709 | 1944 | if not invocation: |
paul@709 | 1945 | return refs |
paul@709 | 1946 | |
paul@709 | 1947 | providers = set() |
paul@709 | 1948 | |
paul@709 | 1949 | for ref in refs: |
paul@742 | 1950 | invocation_providers = self.convert_accessors_to_providers(self.convert_invocation_provider(ref)) |
paul@742 | 1951 | providers.update(invocation_providers) |
paul@742 | 1952 | |
paul@742 | 1953 | return self.references_or_var(providers) |
paul@742 | 1954 | |
paul@742 | 1955 | def convert_accessors_to_providers(self, refs): |
paul@742 | 1956 | |
paul@742 | 1957 | "Convert accessor 'refs' to provider references." |
paul@742 | 1958 | |
paul@742 | 1959 | providers = set() |
paul@742 | 1960 | for ref in refs: |
paul@742 | 1961 | if ref.has_kind("<instance>"): |
paul@742 | 1962 | providers.add(Reference("<class>", ref.get_origin())) |
paul@742 | 1963 | providers.add(ref) |
paul@709 | 1964 | return providers |
paul@709 | 1965 | |
paul@709 | 1966 | def convert_invocation_provider(self, ref): |
paul@709 | 1967 | |
paul@709 | 1968 | "Convert 'ref' to a provider appropriate to its invocation result." |
paul@709 | 1969 | |
paul@712 | 1970 | if ref: |
paul@712 | 1971 | if ref.has_kind("<class>"): |
paul@742 | 1972 | return [ref] |
paul@712 | 1973 | elif ref.has_kind("<function>"): |
paul@731 | 1974 | return self.convert_function_invocation(ref) |
paul@709 | 1975 | |
paul@742 | 1976 | return [Reference("<var>")] |
paul@709 | 1977 | |
paul@709 | 1978 | def convert_invocations(self, refs, invocation): |
paul@709 | 1979 | |
paul@709 | 1980 | """ |
paul@709 | 1981 | Convert 'refs' to invocation results if 'invocation' is set to a true |
paul@709 | 1982 | value. |
paul@709 | 1983 | """ |
paul@709 | 1984 | |
paul@739 | 1985 | if not invocation: |
paul@739 | 1986 | return refs |
paul@739 | 1987 | |
paul@739 | 1988 | invocation_refs = set() |
paul@739 | 1989 | |
paul@739 | 1990 | for ref in refs: |
paul@739 | 1991 | invocation_refs.update(self.convert_invocation(ref)) |
paul@739 | 1992 | |
paul@742 | 1993 | return self.references_or_var(invocation_refs) |
paul@709 | 1994 | |
paul@709 | 1995 | def convert_invocation(self, ref): |
paul@709 | 1996 | |
paul@709 | 1997 | "Convert 'ref' to its invocation result." |
paul@709 | 1998 | |
paul@712 | 1999 | if ref: |
paul@712 | 2000 | if ref.has_kind("<class>"): |
paul@739 | 2001 | return [ref.instance_of()] |
paul@712 | 2002 | elif ref.has_kind("<function>"): |
paul@731 | 2003 | return self.convert_function_invocation(ref) |
paul@731 | 2004 | |
paul@739 | 2005 | return [Reference("<var>")] |
paul@731 | 2006 | |
paul@731 | 2007 | def convert_function_invocation(self, ref): |
paul@731 | 2008 | |
paul@731 | 2009 | "Convert the function 'ref' to its return value reference." |
paul@731 | 2010 | |
paul@742 | 2011 | initialisers = self.importer.all_initialised_names.get((ref.get_origin(), "$return")) |
paul@742 | 2012 | aliases = self.importer.all_aliased_names.get((ref.get_origin(), "$return")) |
paul@742 | 2013 | |
paul@742 | 2014 | if initialisers and not aliases: |
paul@742 | 2015 | return set(initialisers.values()) |
paul@739 | 2016 | |
paul@739 | 2017 | return [Reference("<var>")] |
paul@709 | 2018 | |
paul@742 | 2019 | def references_or_var(self, refs): |
paul@742 | 2020 | |
paul@742 | 2021 | var = Reference("<var>") |
paul@742 | 2022 | |
paul@742 | 2023 | if var in refs: |
paul@742 | 2024 | return set([var]) |
paul@742 | 2025 | else: |
paul@742 | 2026 | return refs |
paul@742 | 2027 | |
paul@791 | 2028 | def get_initialised_name(self, location): |
paul@44 | 2029 | |
paul@44 | 2030 | """ |
paul@791 | 2031 | Return references for any initialised names at 'location', or None if no |
paul@791 | 2032 | such references exist. |
paul@44 | 2033 | """ |
paul@44 | 2034 | |
paul@791 | 2035 | ref = self.initialised_names.get(location) |
paul@791 | 2036 | return ref and [ref] or None |
paul@791 | 2037 | |
paul@791 | 2038 | def get_initialised_access(self, location): |
paul@791 | 2039 | |
paul@791 | 2040 | """ |
paul@791 | 2041 | Return references for any initialised names supplying the given access |
paul@791 | 2042 | 'location', or None if no such references exist. |
paul@791 | 2043 | """ |
paul@791 | 2044 | |
paul@791 | 2045 | if location.access_number is not None: |
paul@791 | 2046 | return self.initialised_accesses.get(location) |
paul@742 | 2047 | else: |
paul@791 | 2048 | return self.get_initialised_name(location) |
paul@742 | 2049 | |
paul@742 | 2050 | def get_accessor_references(self, access_location): |
paul@742 | 2051 | |
paul@742 | 2052 | """ |
paul@742 | 2053 | Return references corresponding to accessor details at the given |
paul@742 | 2054 | 'access_location'. |
paul@742 | 2055 | """ |
paul@742 | 2056 | |
paul@742 | 2057 | if self.accessor_class_types.has_key(access_location): |
paul@742 | 2058 | class_types = self.accessor_class_types[access_location] |
paul@742 | 2059 | instance_types = self.accessor_instance_types[access_location] |
paul@742 | 2060 | module_types = self.accessor_module_types[access_location] |
paul@742 | 2061 | return combine_types(class_types, instance_types, module_types) |
paul@742 | 2062 | else: |
paul@742 | 2063 | return None |
paul@742 | 2064 | |
paul@742 | 2065 | def get_provider_references(self, access_location): |
paul@742 | 2066 | |
paul@742 | 2067 | """ |
paul@742 | 2068 | Return references corresponding to provider details at the given |
paul@742 | 2069 | 'access_location'. |
paul@742 | 2070 | """ |
paul@742 | 2071 | |
paul@742 | 2072 | if self.provider_class_types.has_key(access_location): |
paul@742 | 2073 | class_types = self.provider_class_types[access_location] |
paul@742 | 2074 | instance_types = self.provider_instance_types[access_location] |
paul@742 | 2075 | module_types = self.provider_module_types[access_location] |
paul@742 | 2076 | return combine_types(class_types, instance_types, module_types) |
paul@44 | 2077 | else: |
paul@44 | 2078 | return None |
paul@44 | 2079 | |
paul@44 | 2080 | def record_reference_types(self, location, class_types, instance_types, |
paul@90 | 2081 | module_types, constrained, constrained_specific=False, invocations=None): |
paul@44 | 2082 | |
paul@44 | 2083 | """ |
paul@44 | 2084 | Associate attribute provider types with the given 'location', consisting |
paul@44 | 2085 | of the given 'class_types', 'instance_types' and 'module_types'. |
paul@44 | 2086 | |
paul@44 | 2087 | If 'constrained' is indicated, the constrained nature of the accessor is |
paul@44 | 2088 | recorded for the location. |
paul@44 | 2089 | |
paul@44 | 2090 | If 'constrained_specific' is indicated using a true value, instance types |
paul@44 | 2091 | will not be added to class types to permit access via instances at the |
paul@44 | 2092 | given location. This is only useful where a specific accessor is known |
paul@44 | 2093 | to be a class. |
paul@44 | 2094 | |
paul@105 | 2095 | If 'invocations' is given, the given attribute names indicate those |
paul@105 | 2096 | which are involved in invocations. Such invocations, if involving |
paul@105 | 2097 | functions, will employ those functions as bound methods and will |
paul@105 | 2098 | therefore not support classes as accessors, only instances of such |
paul@105 | 2099 | classes. |
paul@105 | 2100 | |
paul@44 | 2101 | Note that the specified types only indicate the provider types for |
paul@44 | 2102 | attributes, whereas the recorded accessor types indicate the possible |
paul@44 | 2103 | types of the actual objects used to access attributes. |
paul@44 | 2104 | """ |
paul@44 | 2105 | |
paul@44 | 2106 | # Update the type details for the location. |
paul@44 | 2107 | |
paul@721 | 2108 | self.update_provider_types(location, class_types, instance_types, module_types) |
paul@44 | 2109 | |
paul@44 | 2110 | # Class types support classes and instances as accessors. |
paul@44 | 2111 | # Instance-only and module types support only their own kinds as |
paul@44 | 2112 | # accessors. |
paul@44 | 2113 | |
paul@791 | 2114 | path, name = location.path, location.name |
paul@90 | 2115 | |
paul@90 | 2116 | if invocations: |
paul@90 | 2117 | class_only_types = self.filter_for_invocations(class_types, invocations) |
paul@90 | 2118 | else: |
paul@90 | 2119 | class_only_types = class_types |
paul@90 | 2120 | |
paul@44 | 2121 | # However, the nature of accessors can be further determined. |
paul@44 | 2122 | # Any self variable may only refer to an instance. |
paul@44 | 2123 | |
paul@44 | 2124 | if name != "self" or not self.in_method(path): |
paul@90 | 2125 | self.accessor_class_types[location].update(class_only_types) |
paul@44 | 2126 | |
paul@44 | 2127 | if not constrained_specific: |
paul@44 | 2128 | self.accessor_instance_types[location].update(class_types) |
paul@44 | 2129 | |
paul@44 | 2130 | self.accessor_instance_types[location].update(instance_types) |
paul@44 | 2131 | |
paul@44 | 2132 | if name != "self" or not self.in_method(path): |
paul@44 | 2133 | self.accessor_module_types[location].update(module_types) |
paul@44 | 2134 | |
paul@44 | 2135 | if constrained: |
paul@67 | 2136 | self.accessor_constrained.add(location) |
paul@44 | 2137 | |
paul@721 | 2138 | def update_provider_types(self, location, class_types, instance_types, module_types): |
paul@721 | 2139 | |
paul@721 | 2140 | """ |
paul@721 | 2141 | Update provider types for the given 'location', adding 'class_types', |
paul@721 | 2142 | 'instance_types' and 'module_types' to those already stored. |
paul@721 | 2143 | """ |
paul@721 | 2144 | |
paul@721 | 2145 | self.provider_class_types[location].update(class_types) |
paul@721 | 2146 | self.provider_instance_types[location].update(instance_types) |
paul@721 | 2147 | self.provider_module_types[location].update(module_types) |
paul@721 | 2148 | |
paul@721 | 2149 | def update_accessor_types(self, location, class_types, instance_types, module_types): |
paul@721 | 2150 | |
paul@721 | 2151 | """ |
paul@721 | 2152 | Update accessor types for the given 'location', adding 'class_types', |
paul@721 | 2153 | 'instance_types' and 'module_types' to those already stored. |
paul@721 | 2154 | """ |
paul@721 | 2155 | |
paul@721 | 2156 | self.accessor_class_types[location].update(class_types) |
paul@721 | 2157 | self.accessor_instance_types[location].update(instance_types) |
paul@721 | 2158 | self.accessor_module_types[location].update(module_types) |
paul@721 | 2159 | |
paul@90 | 2160 | def filter_for_invocations(self, class_types, attrnames): |
paul@90 | 2161 | |
paul@90 | 2162 | """ |
paul@90 | 2163 | From the given 'class_types', identify methods for the given |
paul@90 | 2164 | 'attrnames' that are being invoked, returning a filtered collection of |
paul@90 | 2165 | class types. |
paul@553 | 2166 | |
paul@553 | 2167 | This method may be used to remove class types from consideration where |
paul@553 | 2168 | their attributes are methods that are directly invoked: method |
paul@553 | 2169 | invocations must involve instance accessors. |
paul@90 | 2170 | """ |
paul@90 | 2171 | |
paul@90 | 2172 | to_filter = set() |
paul@90 | 2173 | |
paul@90 | 2174 | for class_type in class_types: |
paul@90 | 2175 | for attrname in attrnames: |
paul@553 | 2176 | |
paul@553 | 2177 | # Attempt to obtain a class attribute of the given name. This |
paul@553 | 2178 | # may return an attribute provided by an ancestor class. |
paul@553 | 2179 | |
paul@90 | 2180 | ref = self.importer.get_class_attribute(class_type, attrname) |
paul@90 | 2181 | parent_class = ref and ref.parent() |
paul@90 | 2182 | |
paul@553 | 2183 | # If such an attribute is a method and would be available on |
paul@553 | 2184 | # the given class, record the class for filtering. |
paul@553 | 2185 | |
paul@90 | 2186 | if ref and ref.has_kind("<function>") and ( |
paul@90 | 2187 | parent_class == class_type or |
paul@90 | 2188 | class_type in self.descendants[parent_class]): |
paul@90 | 2189 | |
paul@90 | 2190 | to_filter.add(class_type) |
paul@90 | 2191 | break |
paul@90 | 2192 | |
paul@90 | 2193 | return set(class_types).difference(to_filter) |
paul@90 | 2194 | |
paul@44 | 2195 | def identify_reference_attributes(self, location, attrname, class_types, instance_types, module_types, constrained): |
paul@44 | 2196 | |
paul@44 | 2197 | """ |
paul@44 | 2198 | Identify reference attributes, associating them with the given |
paul@44 | 2199 | 'location', identifying the given 'attrname', employing the given |
paul@44 | 2200 | 'class_types', 'instance_types' and 'module_types'. |
paul@44 | 2201 | |
paul@44 | 2202 | If 'constrained' is indicated, the constrained nature of the access is |
paul@44 | 2203 | recorded for the location. |
paul@44 | 2204 | """ |
paul@44 | 2205 | |
paul@44 | 2206 | # Record the referenced objects. |
paul@44 | 2207 | |
paul@44 | 2208 | self.referenced_attrs[location] = \ |
paul@555 | 2209 | self._identify_reference_attribute(location, attrname, class_types, instance_types, module_types) |
paul@44 | 2210 | |
paul@44 | 2211 | if constrained: |
paul@44 | 2212 | self.access_constrained.add(location) |
paul@44 | 2213 | |
paul@555 | 2214 | def _identify_reference_attribute(self, location, attrname, class_types, instance_types, module_types): |
paul@44 | 2215 | |
paul@44 | 2216 | """ |
paul@555 | 2217 | Identify the reference attribute at the given access 'location', using |
paul@555 | 2218 | the given 'attrname', and employing the given 'class_types', |
paul@555 | 2219 | 'instance_types' and 'module_types'. |
paul@44 | 2220 | """ |
paul@44 | 2221 | |
paul@44 | 2222 | attrs = set() |
paul@44 | 2223 | |
paul@44 | 2224 | # The class types expose class attributes either directly or via |
paul@44 | 2225 | # instances. |
paul@44 | 2226 | |
paul@44 | 2227 | for object_type in class_types: |
paul@44 | 2228 | ref = self.importer.get_class_attribute(object_type, attrname) |
paul@555 | 2229 | if ref and self.is_compatible_callable(location, object_type, ref): |
paul@44 | 2230 | attrs.add(("<class>", object_type, ref)) |
paul@44 | 2231 | |
paul@44 | 2232 | # Add any distinct instance attributes that would be provided |
paul@44 | 2233 | # by instances also providing indirect class attribute access. |
paul@44 | 2234 | |
paul@44 | 2235 | for ref in self.importer.get_instance_attributes(object_type, attrname): |
paul@555 | 2236 | if self.is_compatible_callable(location, object_type, ref): |
paul@555 | 2237 | attrs.add(("<instance>", object_type, ref)) |
paul@44 | 2238 | |
paul@44 | 2239 | # The instance-only types expose instance attributes, but although |
paul@44 | 2240 | # classes are excluded as potential accessors (since they do not provide |
paul@44 | 2241 | # the instance attributes), the class types may still provide some |
paul@44 | 2242 | # attributes. |
paul@44 | 2243 | |
paul@44 | 2244 | for object_type in instance_types: |
paul@44 | 2245 | instance_attrs = self.importer.get_instance_attributes(object_type, attrname) |
paul@44 | 2246 | |
paul@44 | 2247 | if instance_attrs: |
paul@44 | 2248 | for ref in instance_attrs: |
paul@555 | 2249 | if self.is_compatible_callable(location, object_type, ref): |
paul@555 | 2250 | attrs.add(("<instance>", object_type, ref)) |
paul@44 | 2251 | else: |
paul@44 | 2252 | ref = self.importer.get_class_attribute(object_type, attrname) |
paul@555 | 2253 | if ref and self.is_compatible_callable(location, object_type, ref): |
paul@44 | 2254 | attrs.add(("<class>", object_type, ref)) |
paul@44 | 2255 | |
paul@44 | 2256 | # Module types expose module attributes for module accessors. |
paul@44 | 2257 | |
paul@44 | 2258 | for object_type in module_types: |
paul@44 | 2259 | ref = self.importer.get_module_attribute(object_type, attrname) |
paul@555 | 2260 | if ref and self.is_compatible_callable(location, object_type, ref): |
paul@44 | 2261 | attrs.add(("<module>", object_type, ref)) |
paul@44 | 2262 | |
paul@44 | 2263 | return attrs |
paul@44 | 2264 | |
paul@555 | 2265 | def is_compatible_callable(self, location, object_type, ref): |
paul@555 | 2266 | |
paul@555 | 2267 | """ |
paul@555 | 2268 | Return whether any invocation at 'location' involving an attribute of |
paul@555 | 2269 | 'object_type' identified by 'ref' is compatible with any arguments used. |
paul@555 | 2270 | """ |
paul@555 | 2271 | |
paul@557 | 2272 | invocation = self.reference_invocations.get(location) |
paul@557 | 2273 | if invocation is None: |
paul@555 | 2274 | return True |
paul@555 | 2275 | |
paul@555 | 2276 | objpath = ref.get_origin() |
paul@555 | 2277 | if not objpath: |
paul@555 | 2278 | return True |
paul@555 | 2279 | |
paul@555 | 2280 | parameters = self.importer.function_parameters.get(objpath) |
paul@555 | 2281 | if not parameters: |
paul@555 | 2282 | return True |
paul@555 | 2283 | |
paul@555 | 2284 | defaults = self.importer.function_defaults.get(objpath) |
paul@557 | 2285 | arguments, keywords = invocation |
paul@557 | 2286 | names = set(parameters) |
paul@555 | 2287 | |
paul@555 | 2288 | # Determine whether the specified arguments are |
paul@555 | 2289 | # compatible with the callable signature. |
paul@555 | 2290 | |
paul@555 | 2291 | if arguments >= len(parameters) - len(defaults) and \ |
paul@557 | 2292 | arguments <= len(parameters) and \ |
paul@557 | 2293 | names.issuperset(keywords): |
paul@555 | 2294 | |
paul@555 | 2295 | return True |
paul@555 | 2296 | else: |
paul@555 | 2297 | init_item(self.reference_invocations_unsuitable, location, set) |
paul@555 | 2298 | self.reference_invocations_unsuitable[location].add(ref) |
paul@555 | 2299 | return False |
paul@555 | 2300 | |
paul@553 | 2301 | # Attribute access plan formulation. |
paul@553 | 2302 | |
paul@67 | 2303 | class_tests = ( |
paul@237 | 2304 | ("guarded", "specific", "type"), |
paul@237 | 2305 | ("guarded", "common", "type"), |
paul@237 | 2306 | ("test", "specific", "type"), |
paul@237 | 2307 | ("test", "common", "type"), |
paul@67 | 2308 | ) |
paul@67 | 2309 | |
paul@67 | 2310 | def get_access_plan(self, location): |
paul@65 | 2311 | |
paul@77 | 2312 | """ |
paul@77 | 2313 | Return details of the access at the given 'location'. The details are as |
paul@77 | 2314 | follows: |
paul@77 | 2315 | |
paul@77 | 2316 | * the initial accessor (from which accesses will be performed if no |
paul@77 | 2317 | computed static accessor is found) |
paul@77 | 2318 | * details of any test required on the initial accessor |
paul@77 | 2319 | * details of any type employed by the test |
paul@77 | 2320 | * any static accessor (from which accesses will be performed in |
paul@77 | 2321 | preference to the initial accessor) |
paul@77 | 2322 | * attributes needing to be traversed from the base that yield |
paul@77 | 2323 | unambiguous objects |
paul@98 | 2324 | * access modes for each of the unambiguously-traversed attributes |
paul@77 | 2325 | * remaining attributes needing to be tested and traversed |
paul@77 | 2326 | * details of the context |
paul@102 | 2327 | * any test to apply to the context |
paul@653 | 2328 | * the method of obtaining the first attribute |
paul@77 | 2329 | * the method of obtaining the final attribute |
paul@77 | 2330 | * any static final attribute |
paul@325 | 2331 | * the kinds of objects providing the final attribute |
paul@77 | 2332 | """ |
paul@65 | 2333 | |
paul@256 | 2334 | const_access = self.const_accesses_rev.get(location) |
paul@65 | 2335 | |
paul@791 | 2336 | path, name, attrnames = location.path, location.name, location.attrnames |
paul@75 | 2337 | remaining = attrnames.split(".") |
paul@75 | 2338 | attrname = remaining[0] |
paul@65 | 2339 | |
paul@687 | 2340 | # Obtain reference, provider and provider kind information. |
paul@687 | 2341 | |
paul@687 | 2342 | attrs = self.reference_all_attrs[location] |
paul@687 | 2343 | provider_types = self.reference_all_providers[location] |
paul@687 | 2344 | provider_kinds = self.reference_all_provider_kinds[location] |
paul@67 | 2345 | |
paul@67 | 2346 | # Obtain accessor type and kind information. |
paul@67 | 2347 | |
paul@67 | 2348 | accessor_types = self.reference_all_accessor_types[location] |
paul@67 | 2349 | accessor_general_types = self.reference_all_accessor_general_types[location] |
paul@824 | 2350 | accessor_kinds = set(get_kinds(accessor_general_types)) |
paul@67 | 2351 | |
paul@67 | 2352 | # Determine any guard or test requirements. |
paul@67 | 2353 | |
paul@67 | 2354 | constrained = location in self.access_constrained |
paul@70 | 2355 | test = self.reference_test_types[location] |
paul@77 | 2356 | test_type = self.reference_test_accessor_type.get(location) |
paul@67 | 2357 | |
paul@67 | 2358 | # Determine the accessor and provider properties. |
paul@67 | 2359 | |
paul@67 | 2360 | class_accessor = "<class>" in accessor_kinds |
paul@67 | 2361 | module_accessor = "<module>" in accessor_kinds |
paul@67 | 2362 | instance_accessor = "<instance>" in accessor_kinds |
paul@67 | 2363 | provided_by_class = "<class>" in provider_kinds |
paul@67 | 2364 | provided_by_instance = "<instance>" in provider_kinds |
paul@67 | 2365 | |
paul@74 | 2366 | # Determine how attributes may be accessed relative to the accessor. |
paul@74 | 2367 | |
paul@74 | 2368 | object_relative = class_accessor or module_accessor or provided_by_instance |
paul@74 | 2369 | class_relative = instance_accessor and provided_by_class |
paul@74 | 2370 | |
paul@67 | 2371 | # Identify the last static attribute for context acquisition. |
paul@67 | 2372 | |
paul@67 | 2373 | base = None |
paul@67 | 2374 | dynamic_base = None |
paul@67 | 2375 | |
paul@687 | 2376 | # Constant accesses have static providers. |
paul@65 | 2377 | |
paul@65 | 2378 | if const_access: |
paul@687 | 2379 | base = len(provider_types) == 1 and first(provider_types) |
paul@67 | 2380 | |
paul@263 | 2381 | # Name-based accesses. |
paul@67 | 2382 | |
paul@213 | 2383 | elif name: |
paul@65 | 2384 | ref = self.importer.identify("%s.%s" % (path, name)) |
paul@263 | 2385 | |
paul@263 | 2386 | # Constant accessors are static. |
paul@263 | 2387 | |
paul@263 | 2388 | if ref and ref.static(): |
paul@65 | 2389 | base = ref.get_origin() |
paul@65 | 2390 | |
paul@70 | 2391 | # Usage of previously-generated guard and test details. |
paul@70 | 2392 | |
paul@237 | 2393 | elif test[:2] == ("constrained", "specific"): |
paul@67 | 2394 | ref = first(accessor_types) |
paul@67 | 2395 | |
paul@237 | 2396 | elif test[:2] == ("constrained", "common"): |
paul@67 | 2397 | ref = first(accessor_general_types) |
paul@67 | 2398 | |
paul@237 | 2399 | elif test[:2] == ("guarded", "specific"): |
paul@67 | 2400 | ref = first(accessor_types) |
paul@67 | 2401 | |
paul@237 | 2402 | elif test[:2] == ("guarded", "common"): |
paul@67 | 2403 | ref = first(accessor_general_types) |
paul@67 | 2404 | |
paul@70 | 2405 | # For attribute-based tests, tentatively identify a dynamic base. |
paul@70 | 2406 | # Such tests allow single or multiple kinds of a type. |
paul@70 | 2407 | |
paul@237 | 2408 | elif test[0] == "test" and test[1] in ("common", "specific"): |
paul@77 | 2409 | dynamic_base = test_type |
paul@67 | 2410 | |
paul@67 | 2411 | # Static accessors. |
paul@67 | 2412 | |
paul@70 | 2413 | if not base and test in self.class_tests: |
paul@70 | 2414 | base = ref and ref.get_origin() or dynamic_base |
paul@70 | 2415 | |
paul@70 | 2416 | # Accessors that are not static but whose nature is determined. |
paul@70 | 2417 | |
paul@70 | 2418 | elif not base and ref: |
paul@67 | 2419 | dynamic_base = ref.get_origin() |
paul@67 | 2420 | |
paul@102 | 2421 | # Determine initial accessor details. |
paul@102 | 2422 | |
paul@102 | 2423 | accessor = base or dynamic_base |
paul@102 | 2424 | accessor_kind = len(accessor_kinds) == 1 and first(accessor_kinds) or None |
paul@102 | 2425 | provider_kind = len(provider_kinds) == 1 and first(provider_kinds) or None |
paul@102 | 2426 | |
paul@102 | 2427 | # Traverse remaining attributes. |
paul@102 | 2428 | |
paul@65 | 2429 | traversed = [] |
paul@96 | 2430 | traversal_modes = [] |
paul@65 | 2431 | |
paul@108 | 2432 | while len(attrs) == 1 and not first(attrs).has_kind("<var>"): |
paul@65 | 2433 | attr = first(attrs) |
paul@65 | 2434 | |
paul@65 | 2435 | traversed.append(attrname) |
paul@96 | 2436 | traversal_modes.append(accessor_kind == provider_kind and "object" or "class") |
paul@96 | 2437 | |
paul@102 | 2438 | # Consume attribute names providing unambiguous attributes. |
paul@102 | 2439 | |
paul@75 | 2440 | del remaining[0] |
paul@75 | 2441 | |
paul@75 | 2442 | if not remaining: |
paul@65 | 2443 | break |
paul@65 | 2444 | |
paul@67 | 2445 | # Update the last static attribute. |
paul@67 | 2446 | |
paul@65 | 2447 | if attr.static(): |
paul@65 | 2448 | base = attr.get_origin() |
paul@65 | 2449 | traversed = [] |
paul@96 | 2450 | traversal_modes = [] |
paul@65 | 2451 | |
paul@102 | 2452 | # Get the access details. |
paul@67 | 2453 | |
paul@75 | 2454 | attrname = remaining[0] |
paul@102 | 2455 | accessor = attr.get_origin() |
paul@102 | 2456 | accessor_kind = attr.get_kind() |
paul@102 | 2457 | provider_kind = self.importer.get_attribute_provider(attr, attrname) |
paul@102 | 2458 | accessor_kinds = [accessor_kind] |
paul@102 | 2459 | provider_kinds = [provider_kind] |
paul@102 | 2460 | |
paul@102 | 2461 | # Get the next attribute. |
paul@102 | 2462 | |
paul@65 | 2463 | attrs = self.importer.get_attributes(attr, attrname) |
paul@67 | 2464 | |
paul@67 | 2465 | # Where many attributes are suggested, no single attribute identity can |
paul@67 | 2466 | # be loaded. |
paul@67 | 2467 | |
paul@65 | 2468 | else: |
paul@65 | 2469 | attr = None |
paul@65 | 2470 | |
paul@67 | 2471 | # Determine the method of access. |
paul@67 | 2472 | |
paul@256 | 2473 | is_assignment = location in self.reference_assignments or const_access in self.reference_assignments |
paul@256 | 2474 | is_invocation = location in self.reference_invocations or const_access in self.reference_invocations |
paul@98 | 2475 | |
paul@71 | 2476 | # Identified attribute that must be accessed via its parent. |
paul@71 | 2477 | |
paul@98 | 2478 | if attr and attr.get_name() and is_assignment: |
paul@98 | 2479 | final_method = "static-assign"; origin = attr.get_name() |
paul@71 | 2480 | |
paul@67 | 2481 | # Static, identified attribute. |
paul@67 | 2482 | |
paul@71 | 2483 | elif attr and attr.static(): |
paul@117 | 2484 | final_method = is_assignment and "static-assign" or \ |
paul@117 | 2485 | is_invocation and "static-invoke" or \ |
paul@117 | 2486 | "static" |
paul@98 | 2487 | origin = attr.final() |
paul@94 | 2488 | |
paul@94 | 2489 | # All other methods of access involve traversal. |
paul@94 | 2490 | |
paul@94 | 2491 | else: |
paul@587 | 2492 | final_method = is_assignment and "assign" or \ |
paul@587 | 2493 | is_invocation and "access-invoke" or \ |
paul@587 | 2494 | "access" |
paul@98 | 2495 | origin = None |
paul@67 | 2496 | |
paul@93 | 2497 | # First attribute accessed at a known position via the accessor. |
paul@67 | 2498 | |
paul@495 | 2499 | # Static bases support object-relative accesses only. |
paul@495 | 2500 | |
paul@495 | 2501 | if base: |
paul@495 | 2502 | first_method = "relative-object" |
paul@495 | 2503 | |
paul@495 | 2504 | # Dynamic bases support either object- or class-relative accesses. |
paul@495 | 2505 | |
paul@495 | 2506 | elif dynamic_base: |
paul@94 | 2507 | first_method = "relative" + (object_relative and "-object" or "") + \ |
paul@94 | 2508 | (class_relative and "-class" or "") |
paul@67 | 2509 | |
paul@67 | 2510 | # The fallback case is always run-time testing and access. |
paul@67 | 2511 | |
paul@67 | 2512 | else: |
paul@94 | 2513 | first_method = "check" + (object_relative and "-object" or "") + \ |
paul@94 | 2514 | (class_relative and "-class" or "") |
paul@67 | 2515 | |
paul@102 | 2516 | # Determine whether an unbound method is being accessed via an instance, |
paul@102 | 2517 | # requiring a context test. |
paul@102 | 2518 | |
paul@102 | 2519 | context_test = "ignore" |
paul@102 | 2520 | |
paul@102 | 2521 | # Assignments do not employ the context. |
paul@102 | 2522 | |
paul@102 | 2523 | if is_assignment: |
paul@102 | 2524 | pass |
paul@102 | 2525 | |
paul@102 | 2526 | # Obtain a selection of possible attributes if no unambiguous attribute |
paul@102 | 2527 | # was identified. |
paul@102 | 2528 | |
paul@102 | 2529 | elif not attr: |
paul@102 | 2530 | |
paul@102 | 2531 | # Use previously-deduced attributes for a simple ambiguous access. |
paul@102 | 2532 | # Otherwise, use the final attribute name to obtain possible |
paul@102 | 2533 | # attributes. |
paul@102 | 2534 | |
paul@102 | 2535 | if len(remaining) > 1: |
paul@102 | 2536 | attrname = remaining[-1] |
paul@102 | 2537 | |
paul@102 | 2538 | (class_types, |
paul@102 | 2539 | only_instance_types, |
paul@102 | 2540 | module_types) = self.get_types_for_attribute(attrname) |
paul@102 | 2541 | |
paul@212 | 2542 | accessor_kinds = set() |
paul@212 | 2543 | provider_kinds = set() |
paul@102 | 2544 | |
paul@102 | 2545 | if class_types: |
paul@212 | 2546 | accessor_kinds.add("<class>") |
paul@212 | 2547 | accessor_kinds.add("<instance>") |
paul@212 | 2548 | provider_kinds.add("<class>") |
paul@102 | 2549 | if only_instance_types: |
paul@212 | 2550 | accessor_kinds.add("<instance>") |
paul@212 | 2551 | provider_kinds.add("<instance>") |
paul@102 | 2552 | if module_types: |
paul@212 | 2553 | accessor_kinds.add("<module>") |
paul@212 | 2554 | provider_kinds.add("<module>") |
paul@102 | 2555 | |
paul@102 | 2556 | attrs = set() |
paul@102 | 2557 | for type in combine_types(class_types, only_instance_types, module_types): |
paul@102 | 2558 | attrs.update(self.importer.get_attributes(type, attrname)) |
paul@102 | 2559 | |
paul@102 | 2560 | always_unbound = True |
paul@102 | 2561 | have_function = False |
paul@102 | 2562 | have_var = False |
paul@102 | 2563 | |
paul@102 | 2564 | # Determine whether all attributes are unbound methods and whether |
paul@102 | 2565 | # functions or unidentified attributes occur. |
paul@102 | 2566 | |
paul@102 | 2567 | for attr in attrs: |
paul@102 | 2568 | always_unbound = always_unbound and attr.has_kind("<function>") and attr.name_parent() == attr.parent() |
paul@102 | 2569 | have_function = have_function or attr.has_kind("<function>") |
paul@102 | 2570 | have_var = have_var or attr.has_kind("<var>") |
paul@102 | 2571 | |
paul@102 | 2572 | # Test for class-via-instance accesses. |
paul@102 | 2573 | |
paul@102 | 2574 | if accessor_kind == "<instance>" and \ |
paul@102 | 2575 | provider_kind == "<class>": |
paul@102 | 2576 | |
paul@102 | 2577 | if always_unbound: |
paul@102 | 2578 | context_test = "replace" |
paul@102 | 2579 | else: |
paul@102 | 2580 | context_test = "test" |
paul@102 | 2581 | |
paul@102 | 2582 | # Test for the presence of class-via-instance accesses. |
paul@102 | 2583 | |
paul@102 | 2584 | elif "<instance>" in accessor_kinds and \ |
paul@102 | 2585 | "<class>" in provider_kinds and \ |
paul@102 | 2586 | (have_function or have_var): |
paul@102 | 2587 | |
paul@102 | 2588 | context_test = "test" |
paul@102 | 2589 | |
paul@102 | 2590 | # With an unambiguous attribute, determine whether a test is needed. |
paul@102 | 2591 | |
paul@102 | 2592 | elif accessor_kind == "<instance>" and \ |
paul@102 | 2593 | provider_kind == "<class>" and \ |
paul@102 | 2594 | (attr.has_kind("<var>") or |
paul@102 | 2595 | attr.has_kind("<function>") and |
paul@102 | 2596 | attr.name_parent() == attr.parent()): |
paul@102 | 2597 | |
paul@102 | 2598 | if attr.has_kind("<var>"): |
paul@102 | 2599 | context_test = "test" |
paul@102 | 2600 | else: |
paul@102 | 2601 | context_test = "replace" |
paul@102 | 2602 | |
paul@102 | 2603 | # With an unambiguous attribute with ambiguity in the access method, |
paul@102 | 2604 | # generate a test. |
paul@102 | 2605 | |
paul@102 | 2606 | elif "<instance>" in accessor_kinds and \ |
paul@102 | 2607 | "<class>" in provider_kinds and \ |
paul@102 | 2608 | (attr.has_kind("<var>") or |
paul@102 | 2609 | attr.has_kind("<function>") and |
paul@102 | 2610 | attr.name_parent() == attr.parent()): |
paul@102 | 2611 | |
paul@102 | 2612 | context_test = "test" |
paul@102 | 2613 | |
paul@75 | 2614 | # Determine the nature of the context. |
paul@75 | 2615 | |
paul@102 | 2616 | context = context_test == "ignore" and "unset" or \ |
paul@100 | 2617 | len(traversed + remaining) == 1 and \ |
paul@100 | 2618 | (base and "base" or "original-accessor") or \ |
paul@100 | 2619 | "final-accessor" |
paul@77 | 2620 | |
paul@234 | 2621 | return name, test, test_type, base, \ |
paul@234 | 2622 | traversed, traversal_modes, remaining, \ |
paul@234 | 2623 | context, context_test, \ |
paul@234 | 2624 | first_method, final_method, \ |
paul@234 | 2625 | origin, accessor_kinds |
paul@65 | 2626 | |
paul@653 | 2627 | def initialise_access_instructions(self): |
paul@653 | 2628 | |
paul@653 | 2629 | "Expand access plans into instruction sequences." |
paul@653 | 2630 | |
paul@653 | 2631 | for access_location, access_plan in self.access_plans.items(): |
paul@653 | 2632 | |
paul@653 | 2633 | # Obtain the access details. |
paul@653 | 2634 | |
paul@653 | 2635 | name, test, test_type, base, \ |
paul@653 | 2636 | traversed, traversal_modes, attrnames, \ |
paul@653 | 2637 | context, context_test, \ |
paul@653 | 2638 | first_method, final_method, \ |
paul@653 | 2639 | origin, accessor_kinds = access_plan |
paul@653 | 2640 | |
paul@653 | 2641 | # Emit instructions by appending them to a list. |
paul@653 | 2642 | |
paul@653 | 2643 | instructions = [] |
paul@653 | 2644 | emit = instructions.append |
paul@653 | 2645 | |
paul@653 | 2646 | # Identify any static original accessor. |
paul@653 | 2647 | |
paul@653 | 2648 | if base: |
paul@653 | 2649 | original_accessor = base |
paul@653 | 2650 | |
paul@653 | 2651 | # Employ names as contexts unless the context needs testing and |
paul@653 | 2652 | # potentially updating. In such cases, temporary context storage is |
paul@653 | 2653 | # used instead. |
paul@653 | 2654 | |
paul@653 | 2655 | elif name and not (context_test == "test" and |
paul@653 | 2656 | final_method in ("access-invoke", "static-invoke")): |
paul@653 | 2657 | original_accessor = "<name>" # refers to the name |
paul@653 | 2658 | |
paul@653 | 2659 | # Use a generic placeholder representing the access expression in |
paul@653 | 2660 | # the general case. |
paul@653 | 2661 | |
paul@653 | 2662 | else: |
paul@653 | 2663 | original_accessor = "<expr>" |
paul@653 | 2664 | |
paul@653 | 2665 | # Prepare for any first attribute access. |
paul@653 | 2666 | |
paul@653 | 2667 | if traversed: |
paul@653 | 2668 | attrname = traversed[0] |
paul@653 | 2669 | del traversed[0] |
paul@653 | 2670 | elif attrnames: |
paul@653 | 2671 | attrname = attrnames[0] |
paul@653 | 2672 | del attrnames[0] |
paul@653 | 2673 | |
paul@653 | 2674 | # Perform the first access explicitly if at least one operation |
paul@653 | 2675 | # requires it. |
paul@653 | 2676 | |
paul@653 | 2677 | access_first_attribute = final_method in ("access", "access-invoke", "assign") or traversed or attrnames |
paul@653 | 2678 | |
paul@653 | 2679 | # Determine whether the first access involves assignment. |
paul@653 | 2680 | |
paul@653 | 2681 | assigning = not traversed and not attrnames and final_method == "assign" |
paul@653 | 2682 | set_accessor = assigning and "<set_target_accessor>" or "<set_accessor>" |
paul@653 | 2683 | stored_accessor = assigning and "<target_accessor>" or "<accessor>" |
paul@653 | 2684 | |
paul@653 | 2685 | # Set the context if already available. |
paul@653 | 2686 | |
paul@653 | 2687 | context_var = None |
paul@653 | 2688 | |
paul@653 | 2689 | if context == "base": |
paul@653 | 2690 | accessor = context_var = (base,) |
paul@653 | 2691 | elif context == "original-accessor": |
paul@653 | 2692 | |
paul@653 | 2693 | # Prevent re-evaluation of any dynamic expression by storing it. |
paul@653 | 2694 | |
paul@653 | 2695 | if original_accessor == "<expr>": |
paul@653 | 2696 | if final_method in ("access-invoke", "static-invoke"): |
paul@653 | 2697 | emit(("<set_context>", original_accessor)) |
paul@653 | 2698 | accessor = context_var = ("<context>",) |
paul@653 | 2699 | else: |
paul@653 | 2700 | emit((set_accessor, original_accessor)) |
paul@653 | 2701 | accessor = context_var = (stored_accessor,) |
paul@653 | 2702 | else: |
paul@653 | 2703 | accessor = context_var = (original_accessor,) |
paul@653 | 2704 | |
paul@653 | 2705 | # Assigning does not set the context. |
paul@653 | 2706 | |
paul@653 | 2707 | elif context in ("final-accessor", "unset") and access_first_attribute: |
paul@653 | 2708 | |
paul@653 | 2709 | # Prevent re-evaluation of any dynamic expression by storing it. |
paul@653 | 2710 | |
paul@653 | 2711 | if original_accessor == "<expr>": |
paul@653 | 2712 | emit((set_accessor, original_accessor)) |
paul@653 | 2713 | accessor = (stored_accessor,) |
paul@653 | 2714 | else: |
paul@653 | 2715 | accessor = (original_accessor,) |
paul@825 | 2716 | else: |
paul@825 | 2717 | accessor = None |
paul@653 | 2718 | |
paul@653 | 2719 | # Apply any test. |
paul@653 | 2720 | |
paul@653 | 2721 | if test[0] == "test": |
paul@828 | 2722 | test_accessor = accessor = ("__%s_%s_%s" % test, accessor, test_type) |
paul@825 | 2723 | else: |
paul@825 | 2724 | test_accessor = None |
paul@653 | 2725 | |
paul@653 | 2726 | # Perform the first or final access. |
paul@653 | 2727 | # The access only needs performing if the resulting accessor is used. |
paul@653 | 2728 | |
paul@653 | 2729 | remaining = len(traversed + attrnames) |
paul@653 | 2730 | |
paul@653 | 2731 | if access_first_attribute: |
paul@653 | 2732 | |
paul@653 | 2733 | if first_method == "relative-class": |
paul@653 | 2734 | if assigning: |
paul@653 | 2735 | emit(("__store_via_class", accessor, attrname, "<assexpr>")) |
paul@653 | 2736 | else: |
paul@653 | 2737 | accessor = ("__load_via_class", accessor, attrname) |
paul@653 | 2738 | |
paul@653 | 2739 | elif first_method == "relative-object": |
paul@653 | 2740 | if assigning: |
paul@653 | 2741 | emit(("__store_via_object", accessor, attrname, "<assexpr>")) |
paul@653 | 2742 | else: |
paul@653 | 2743 | accessor = ("__load_via_object", accessor, attrname) |
paul@653 | 2744 | |
paul@653 | 2745 | elif first_method == "relative-object-class": |
paul@653 | 2746 | if assigning: |
paul@653 | 2747 | emit(("__get_class_and_store", accessor, attrname, "<assexpr>")) |
paul@653 | 2748 | else: |
paul@653 | 2749 | accessor = ("__get_class_and_load", accessor, attrname) |
paul@653 | 2750 | |
paul@653 | 2751 | elif first_method == "check-class": |
paul@653 | 2752 | if assigning: |
paul@653 | 2753 | emit(("__check_and_store_via_class", accessor, attrname, "<assexpr>")) |
paul@653 | 2754 | else: |
paul@653 | 2755 | accessor = ("__check_and_load_via_class", accessor, attrname) |
paul@653 | 2756 | |
paul@653 | 2757 | elif first_method == "check-object": |
paul@653 | 2758 | if assigning: |
paul@653 | 2759 | emit(("__check_and_store_via_object", accessor, attrname, "<assexpr>")) |
paul@653 | 2760 | else: |
paul@653 | 2761 | accessor = ("__check_and_load_via_object", accessor, attrname) |
paul@653 | 2762 | |
paul@653 | 2763 | elif first_method == "check-object-class": |
paul@653 | 2764 | if assigning: |
paul@653 | 2765 | emit(("__check_and_store_via_any", accessor, attrname, "<assexpr>")) |
paul@653 | 2766 | else: |
paul@653 | 2767 | accessor = ("__check_and_load_via_any", accessor, attrname) |
paul@653 | 2768 | |
paul@653 | 2769 | # Traverse attributes using the accessor. |
paul@653 | 2770 | |
paul@653 | 2771 | if traversed: |
paul@653 | 2772 | for attrname, traversal_mode in zip(traversed, traversal_modes): |
paul@653 | 2773 | assigning = remaining == 1 and final_method == "assign" |
paul@653 | 2774 | |
paul@653 | 2775 | # Set the context, if appropriate. |
paul@653 | 2776 | |
paul@653 | 2777 | if remaining == 1 and final_method != "assign" and context == "final-accessor": |
paul@653 | 2778 | |
paul@653 | 2779 | # Invoked attributes employ a separate context accessed |
paul@653 | 2780 | # during invocation. |
paul@653 | 2781 | |
paul@653 | 2782 | if final_method in ("access-invoke", "static-invoke"): |
paul@653 | 2783 | emit(("<set_context>", accessor)) |
paul@653 | 2784 | accessor = context_var = "<context>" |
paul@653 | 2785 | |
paul@653 | 2786 | # A private context within the access is otherwise |
paul@653 | 2787 | # retained. |
paul@653 | 2788 | |
paul@653 | 2789 | else: |
paul@653 | 2790 | emit(("<set_private_context>", accessor)) |
paul@653 | 2791 | accessor = context_var = "<private_context>" |
paul@653 | 2792 | |
paul@653 | 2793 | # Perform the access only if not achieved directly. |
paul@653 | 2794 | |
paul@653 | 2795 | if remaining > 1 or final_method in ("access", "access-invoke", "assign"): |
paul@653 | 2796 | |
paul@653 | 2797 | if traversal_mode == "class": |
paul@653 | 2798 | if assigning: |
paul@653 | 2799 | emit(("__store_via_class", accessor, attrname, "<assexpr>")) |
paul@653 | 2800 | else: |
paul@653 | 2801 | accessor = ("__load_via_class", accessor, attrname) |
paul@653 | 2802 | else: |
paul@653 | 2803 | if assigning: |
paul@653 | 2804 | emit(("__store_via_object", accessor, attrname, "<assexpr>")) |
paul@653 | 2805 | else: |
paul@653 | 2806 | accessor = ("__load_via_object", accessor, attrname) |
paul@653 | 2807 | |
paul@653 | 2808 | remaining -= 1 |
paul@653 | 2809 | |
paul@653 | 2810 | if attrnames: |
paul@653 | 2811 | for attrname in attrnames: |
paul@653 | 2812 | assigning = remaining == 1 and final_method == "assign" |
paul@653 | 2813 | |
paul@653 | 2814 | # Set the context, if appropriate. |
paul@653 | 2815 | |
paul@653 | 2816 | if remaining == 1 and final_method != "assign" and context == "final-accessor": |
paul@653 | 2817 | |
paul@653 | 2818 | # Invoked attributes employ a separate context accessed |
paul@653 | 2819 | # during invocation. |
paul@653 | 2820 | |
paul@653 | 2821 | if final_method in ("access-invoke", "static-invoke"): |
paul@653 | 2822 | emit(("<set_context>", accessor)) |
paul@653 | 2823 | accessor = context_var = "<context>" |
paul@653 | 2824 | |
paul@653 | 2825 | # A private context within the access is otherwise |
paul@653 | 2826 | # retained. |
paul@653 | 2827 | |
paul@653 | 2828 | else: |
paul@653 | 2829 | emit(("<set_private_context>", accessor)) |
paul@653 | 2830 | accessor = context_var = "<private_context>" |
paul@653 | 2831 | |
paul@653 | 2832 | # Perform the access only if not achieved directly. |
paul@653 | 2833 | |
paul@653 | 2834 | if remaining > 1 or final_method in ("access", "access-invoke", "assign"): |
paul@653 | 2835 | |
paul@694 | 2836 | # Constrain instructions involving certain special |
paul@694 | 2837 | # attribute names. |
paul@694 | 2838 | |
paul@694 | 2839 | to_search = attrname == "__data__" and "object" or "any" |
paul@694 | 2840 | |
paul@653 | 2841 | if assigning: |
paul@694 | 2842 | emit(("__check_and_store_via_%s" % to_search, accessor, attrname, "<assexpr>")) |
paul@653 | 2843 | else: |
paul@694 | 2844 | accessor = ("__check_and_load_via_%s" % to_search, accessor, attrname) |
paul@653 | 2845 | |
paul@653 | 2846 | remaining -= 1 |
paul@653 | 2847 | |
paul@826 | 2848 | # Make any accessor test available if not emitted. |
paul@826 | 2849 | |
paul@826 | 2850 | test_accessor = not instructions and test_accessor or None |
paul@826 | 2851 | |
paul@653 | 2852 | # Define or emit the means of accessing the actual target. |
paul@653 | 2853 | |
paul@826 | 2854 | if final_method in ("static", "static-assign", "static-invoke"): |
paul@826 | 2855 | |
paul@826 | 2856 | if test_accessor: |
paul@826 | 2857 | emit(test_accessor) |
paul@826 | 2858 | |
paul@826 | 2859 | # Assignments to known attributes. |
paul@826 | 2860 | |
paul@826 | 2861 | if final_method == "static-assign": |
paul@826 | 2862 | parent, attrname = origin.rsplit(".", 1) |
paul@826 | 2863 | emit(("__store_via_object", parent, attrname, "<assexpr>")) |
paul@826 | 2864 | |
paul@826 | 2865 | # Invoked attributes employ a separate context. |
paul@826 | 2866 | |
paul@826 | 2867 | elif final_method in ("static", "static-invoke"): |
paul@826 | 2868 | accessor = ("__load_static_ignore", origin) |
paul@653 | 2869 | |
paul@653 | 2870 | # Wrap accesses in context operations. |
paul@653 | 2871 | |
paul@653 | 2872 | if context_test == "test": |
paul@653 | 2873 | |
paul@653 | 2874 | # Test and combine the context with static attribute details. |
paul@653 | 2875 | |
paul@653 | 2876 | if final_method == "static": |
paul@653 | 2877 | emit(("__load_static_test", context_var, origin)) |
paul@653 | 2878 | |
paul@653 | 2879 | # Test the context, storing it separately if required for the |
paul@653 | 2880 | # immediately invoked static attribute. |
paul@653 | 2881 | |
paul@653 | 2882 | elif final_method == "static-invoke": |
paul@653 | 2883 | emit(("<test_context_static>", context_var, origin)) |
paul@653 | 2884 | |
paul@653 | 2885 | # Test the context, storing it separately if required for an |
paul@653 | 2886 | # immediately invoked attribute. |
paul@653 | 2887 | |
paul@653 | 2888 | elif final_method == "access-invoke": |
paul@653 | 2889 | emit(("<test_context_revert>", context_var, accessor)) |
paul@653 | 2890 | |
paul@653 | 2891 | # Test the context and update the attribute details if |
paul@653 | 2892 | # appropriate. |
paul@653 | 2893 | |
paul@653 | 2894 | else: |
paul@653 | 2895 | emit(("__test_context", context_var, accessor)) |
paul@653 | 2896 | |
paul@653 | 2897 | elif context_test == "replace": |
paul@653 | 2898 | |
paul@653 | 2899 | # Produce an object with updated context. |
paul@653 | 2900 | |
paul@653 | 2901 | if final_method == "static": |
paul@653 | 2902 | emit(("__load_static_replace", context_var, origin)) |
paul@653 | 2903 | |
paul@653 | 2904 | # Omit the context update operation where the target is static |
paul@653 | 2905 | # and the context is recorded separately. |
paul@653 | 2906 | |
paul@653 | 2907 | elif final_method == "static-invoke": |
paul@653 | 2908 | pass |
paul@653 | 2909 | |
paul@653 | 2910 | # If a separate context is used for an immediate invocation, |
paul@653 | 2911 | # produce the attribute details unchanged. |
paul@653 | 2912 | |
paul@653 | 2913 | elif final_method == "access-invoke": |
paul@653 | 2914 | emit(accessor) |
paul@653 | 2915 | |
paul@653 | 2916 | # Update the context in the attribute details. |
paul@653 | 2917 | |
paul@653 | 2918 | else: |
paul@653 | 2919 | emit(("__update_context", context_var, accessor)) |
paul@653 | 2920 | |
paul@653 | 2921 | # Omit the accessor for assignments and for invocations of static |
paul@826 | 2922 | # targets. Otherwise, emit the accessor which may involve the |
paul@826 | 2923 | # invocation of a test. |
paul@653 | 2924 | |
paul@653 | 2925 | elif final_method not in ("assign", "static-assign", "static-invoke"): |
paul@653 | 2926 | emit(accessor) |
paul@653 | 2927 | |
paul@653 | 2928 | # Produce an advisory instruction regarding the context. |
paul@653 | 2929 | |
paul@653 | 2930 | if context_var: |
paul@776 | 2931 | if context_test in ("ignore", "replace"): |
paul@776 | 2932 | emit(("<context_identity_verified>", context_var)) |
paul@776 | 2933 | else: |
paul@776 | 2934 | emit(("<context_identity>", context_var)) |
paul@653 | 2935 | |
paul@752 | 2936 | # Produce an advisory instruction regarding the final attribute. |
paul@752 | 2937 | |
paul@752 | 2938 | if origin: |
paul@752 | 2939 | emit(("<final_identity>", origin)) |
paul@752 | 2940 | |
paul@653 | 2941 | self.access_instructions[access_location] = instructions |
paul@653 | 2942 | self.accessor_kinds[access_location] = accessor_kinds |
paul@653 | 2943 | |
paul@44 | 2944 | # vim: tabstop=4 expandtab shiftwidth=4 |