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