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