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