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@44 | 6 | Copyright (C) 2014, 2015, 2016 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@44 | 22 | from common import first, get_attrname_from_location, get_attrnames, \ |
paul@44 | 23 | init_item, make_key, sorted_output, \ |
paul@44 | 24 | CommonOutput |
paul@44 | 25 | from encoders import encode_attrnames, encode_access_location, \ |
paul@55 | 26 | encode_constrained, encode_location, encode_usage |
paul@44 | 27 | from os.path import join |
paul@44 | 28 | from referencing import Reference |
paul@44 | 29 | |
paul@44 | 30 | class Deducer(CommonOutput): |
paul@44 | 31 | |
paul@44 | 32 | "Deduce types in a program." |
paul@44 | 33 | |
paul@44 | 34 | def __init__(self, importer, output): |
paul@44 | 35 | |
paul@44 | 36 | """ |
paul@44 | 37 | Initialise an instance using the given 'importer' that will perform |
paul@44 | 38 | deductions on the program information, writing the results to the given |
paul@44 | 39 | 'output' directory. |
paul@44 | 40 | """ |
paul@44 | 41 | |
paul@44 | 42 | self.importer = importer |
paul@44 | 43 | self.output = output |
paul@44 | 44 | |
paul@44 | 45 | # Descendants of classes. |
paul@44 | 46 | |
paul@44 | 47 | self.descendants = {} |
paul@44 | 48 | self.init_descendants() |
paul@44 | 49 | self.init_special_attributes() |
paul@44 | 50 | |
paul@44 | 51 | # Map locations to usage in order to determine specific types. |
paul@44 | 52 | |
paul@44 | 53 | self.location_index = {} |
paul@44 | 54 | |
paul@44 | 55 | # Map access locations to definition locations. |
paul@44 | 56 | |
paul@44 | 57 | self.access_index = {} |
paul@44 | 58 | |
paul@44 | 59 | # Map aliases to accesses that define them. |
paul@44 | 60 | |
paul@44 | 61 | self.alias_index = {} |
paul@44 | 62 | |
paul@44 | 63 | # Map usage observations to assigned attributes. |
paul@44 | 64 | |
paul@44 | 65 | self.assigned_attrs = {} |
paul@44 | 66 | |
paul@44 | 67 | # Map usage observations to objects. |
paul@44 | 68 | |
paul@44 | 69 | self.attr_class_types = {} |
paul@44 | 70 | self.attr_instance_types = {} |
paul@44 | 71 | self.attr_module_types = {} |
paul@44 | 72 | |
paul@44 | 73 | # Modified attributes from usage observations. |
paul@44 | 74 | |
paul@44 | 75 | self.modified_attributes = {} |
paul@44 | 76 | |
paul@44 | 77 | # Map locations to types, constrained indicators and attributes. |
paul@44 | 78 | |
paul@44 | 79 | self.accessor_class_types = {} |
paul@44 | 80 | self.accessor_instance_types = {} |
paul@44 | 81 | self.accessor_module_types = {} |
paul@44 | 82 | self.provider_class_types = {} |
paul@44 | 83 | self.provider_instance_types = {} |
paul@44 | 84 | self.provider_module_types = {} |
paul@44 | 85 | self.reference_constrained = set() |
paul@44 | 86 | self.access_constrained = set() |
paul@44 | 87 | self.referenced_attrs = {} |
paul@44 | 88 | self.referenced_objects = {} |
paul@44 | 89 | |
paul@44 | 90 | # Accumulated information about accessors and providers. |
paul@44 | 91 | |
paul@44 | 92 | self.accessor_general_class_types = {} |
paul@44 | 93 | self.accessor_general_instance_types = {} |
paul@44 | 94 | self.accessor_general_module_types = {} |
paul@44 | 95 | self.accessor_all_types = {} |
paul@44 | 96 | self.accessor_all_general_types = {} |
paul@44 | 97 | self.provider_all_types = {} |
paul@44 | 98 | self.accessor_guard_tests = {} |
paul@44 | 99 | |
paul@44 | 100 | # Accumulated information about accessed attributes and |
paul@44 | 101 | # attribute-specific accessor tests. |
paul@44 | 102 | |
paul@44 | 103 | self.reference_class_attrs = {} |
paul@44 | 104 | self.reference_instance_attrs = {} |
paul@44 | 105 | self.reference_module_attrs = {} |
paul@44 | 106 | self.reference_all_attrs = {} |
paul@44 | 107 | self.reference_all_attrtypes = {} |
paul@44 | 108 | self.reference_test_types = {} |
paul@44 | 109 | self.reference_test_accessor_types = {} |
paul@44 | 110 | |
paul@44 | 111 | # The processing workflow itself. |
paul@44 | 112 | |
paul@44 | 113 | self.init_usage_index() |
paul@44 | 114 | self.init_accessors() |
paul@44 | 115 | self.init_accesses() |
paul@44 | 116 | self.init_aliases() |
paul@44 | 117 | self.init_attr_type_indexes() |
paul@44 | 118 | self.modify_mutated_attributes() |
paul@44 | 119 | self.identify_references() |
paul@44 | 120 | self.classify_accessors() |
paul@44 | 121 | self.classify_accesses() |
paul@44 | 122 | |
paul@44 | 123 | def to_output(self): |
paul@44 | 124 | |
paul@44 | 125 | "Write the output files using deduction information." |
paul@44 | 126 | |
paul@44 | 127 | self.check_output() |
paul@44 | 128 | |
paul@44 | 129 | self.write_mutations() |
paul@44 | 130 | self.write_accessors() |
paul@44 | 131 | self.write_accesses() |
paul@44 | 132 | |
paul@44 | 133 | def write_mutations(self): |
paul@44 | 134 | |
paul@44 | 135 | """ |
paul@44 | 136 | Write mutation-related output in the following format: |
paul@44 | 137 | |
paul@44 | 138 | qualified name " " original object type |
paul@44 | 139 | |
paul@44 | 140 | Object type can be "<class>", "<function>" or "<var>". |
paul@44 | 141 | """ |
paul@44 | 142 | |
paul@44 | 143 | f = open(join(self.output, "mutations"), "w") |
paul@44 | 144 | try: |
paul@44 | 145 | attrs = self.modified_attributes.items() |
paul@44 | 146 | attrs.sort() |
paul@44 | 147 | |
paul@44 | 148 | for attr, value in attrs: |
paul@44 | 149 | print >>f, attr, value |
paul@44 | 150 | finally: |
paul@44 | 151 | f.close() |
paul@44 | 152 | |
paul@44 | 153 | def write_accessors(self): |
paul@44 | 154 | |
paul@44 | 155 | """ |
paul@44 | 156 | Write reference-related output in the following format for types: |
paul@44 | 157 | |
paul@44 | 158 | location " " ( "constrained" | "deduced" ) " " attribute type " " most general type names " " number of specific types |
paul@44 | 159 | |
paul@44 | 160 | Note that multiple lines can be given for each location, one for each |
paul@44 | 161 | attribute type. |
paul@44 | 162 | |
paul@44 | 163 | Locations have the following format: |
paul@44 | 164 | |
paul@44 | 165 | qualified name of scope "." local name ":" name version |
paul@44 | 166 | |
paul@44 | 167 | The attribute type can be "<class>", "<instance>", "<module>" or "<>", |
paul@44 | 168 | where the latter indicates an absence of suitable references. |
paul@44 | 169 | |
paul@44 | 170 | Type names indicate the type providing the attributes, being either a |
paul@44 | 171 | class or module qualified name. |
paul@44 | 172 | |
paul@44 | 173 | ---- |
paul@44 | 174 | |
paul@44 | 175 | A summary of accessor types is formatted as follows: |
paul@44 | 176 | |
paul@44 | 177 | location " " ( "constrained" | "deduced" ) " " ( "specific" | "common" | "unguarded" ) " " most general type names " " number of specific types |
paul@44 | 178 | |
paul@44 | 179 | This summary groups all attribute types (class, instance, module) into a |
paul@44 | 180 | single line in order to determine the complexity of identifying an |
paul@44 | 181 | accessor. |
paul@44 | 182 | |
paul@44 | 183 | ---- |
paul@44 | 184 | |
paul@44 | 185 | References that cannot be supported by any types are written to a |
paul@44 | 186 | warnings file in the following format: |
paul@44 | 187 | |
paul@44 | 188 | location |
paul@44 | 189 | |
paul@44 | 190 | ---- |
paul@44 | 191 | |
paul@44 | 192 | For each location where a guard would be asserted to guarantee the |
paul@44 | 193 | nature of an object, the following format is employed: |
paul@44 | 194 | |
paul@44 | 195 | location " " ( "specific" | "common" ) " " object kind " " object types |
paul@44 | 196 | |
paul@44 | 197 | Object kind can be "<class>", "<instance>" or "<module>". |
paul@44 | 198 | """ |
paul@44 | 199 | |
paul@44 | 200 | f_type_summary = open(join(self.output, "type_summary"), "w") |
paul@44 | 201 | f_types = open(join(self.output, "types"), "w") |
paul@44 | 202 | f_warnings = open(join(self.output, "type_warnings"), "w") |
paul@44 | 203 | f_guards = open(join(self.output, "guards"), "w") |
paul@44 | 204 | |
paul@44 | 205 | try: |
paul@44 | 206 | locations = self.accessor_class_types.keys() |
paul@44 | 207 | locations.sort() |
paul@44 | 208 | |
paul@44 | 209 | for location in locations: |
paul@44 | 210 | constrained = location in self.reference_constrained |
paul@44 | 211 | |
paul@44 | 212 | # Accessor information. |
paul@44 | 213 | |
paul@44 | 214 | class_types = self.accessor_class_types[location] |
paul@44 | 215 | instance_types = self.accessor_instance_types[location] |
paul@44 | 216 | module_types = self.accessor_module_types[location] |
paul@44 | 217 | |
paul@44 | 218 | general_class_types = self.accessor_general_class_types[location] |
paul@44 | 219 | general_instance_types = self.accessor_general_instance_types[location] |
paul@44 | 220 | general_module_types = self.accessor_general_module_types[location] |
paul@44 | 221 | |
paul@44 | 222 | all_types = self.accessor_all_types[location] |
paul@44 | 223 | all_general_types = self.accessor_all_general_types[location] |
paul@44 | 224 | |
paul@44 | 225 | if class_types: |
paul@44 | 226 | print >>f_types, encode_location(location), encode_constrained(constrained), "<class>", \ |
paul@44 | 227 | sorted_output(general_class_types), len(class_types) |
paul@44 | 228 | |
paul@44 | 229 | if instance_types: |
paul@44 | 230 | print >>f_types, encode_location(location), encode_constrained(constrained), "<instance>", \ |
paul@44 | 231 | sorted_output(general_instance_types), len(instance_types) |
paul@44 | 232 | |
paul@44 | 233 | if module_types: |
paul@44 | 234 | print >>f_types, encode_location(location), encode_constrained(constrained), "<module>", \ |
paul@44 | 235 | sorted_output(general_module_types), len(module_types) |
paul@44 | 236 | |
paul@44 | 237 | if not all_types: |
paul@44 | 238 | print >>f_types, encode_location(location), "deduced", "<>", 0 |
paul@55 | 239 | attrnames = list(self.location_index[location]) |
paul@55 | 240 | attrnames.sort() |
paul@55 | 241 | print >>f_warnings, encode_location(location), "; ".join(map(encode_usage, attrnames)) |
paul@44 | 242 | |
paul@44 | 243 | guard_test = self.accessor_guard_tests.get(location) |
paul@44 | 244 | |
paul@44 | 245 | # Write specific type guard details. |
paul@44 | 246 | |
paul@44 | 247 | if guard_test and guard_test.startswith("specific"): |
paul@44 | 248 | print >>f_guards, encode_location(location), guard_test, \ |
paul@44 | 249 | self.get_kinds(all_types)[0], \ |
paul@44 | 250 | sorted_output(all_types) |
paul@44 | 251 | |
paul@44 | 252 | # Write common type guard details. |
paul@44 | 253 | |
paul@44 | 254 | elif guard_test and guard_test.startswith("common"): |
paul@44 | 255 | print >>f_guards, encode_location(location), guard_test, \ |
paul@44 | 256 | self.get_kinds(all_general_types)[0], \ |
paul@44 | 257 | sorted_output(all_general_types) |
paul@44 | 258 | |
paul@44 | 259 | print >>f_type_summary, encode_location(location), encode_constrained(constrained), \ |
paul@44 | 260 | guard_test or "unguarded", sorted_output(all_general_types), len(all_types) |
paul@44 | 261 | |
paul@44 | 262 | finally: |
paul@44 | 263 | f_type_summary.close() |
paul@44 | 264 | f_types.close() |
paul@44 | 265 | f_warnings.close() |
paul@44 | 266 | f_guards.close() |
paul@44 | 267 | |
paul@44 | 268 | def write_accesses(self): |
paul@44 | 269 | |
paul@44 | 270 | """ |
paul@44 | 271 | Specific attribute output is produced in the following format: |
paul@44 | 272 | |
paul@44 | 273 | location " " ( "constrained" | "deduced" ) " " attribute type " " attribute references |
paul@44 | 274 | |
paul@44 | 275 | Note that multiple lines can be given for each location and attribute |
paul@44 | 276 | name, one for each attribute type. |
paul@44 | 277 | |
paul@44 | 278 | Locations have the following format: |
paul@44 | 279 | |
paul@44 | 280 | qualified name of scope "." local name " " attribute name ":" access number |
paul@44 | 281 | |
paul@44 | 282 | The attribute type can be "<class>", "<instance>", "<module>" or "<>", |
paul@44 | 283 | where the latter indicates an absence of suitable references. |
paul@44 | 284 | |
paul@44 | 285 | Attribute references have the following format: |
paul@44 | 286 | |
paul@44 | 287 | object type ":" qualified name |
paul@44 | 288 | |
paul@44 | 289 | Object type can be "<class>", "<function>" or "<var>". |
paul@44 | 290 | |
paul@44 | 291 | ---- |
paul@44 | 292 | |
paul@44 | 293 | A summary of attributes is formatted as follows: |
paul@44 | 294 | |
paul@44 | 295 | location " " attribute name " " ( "constrained" | "deduced" ) " " test " " attribute references |
paul@44 | 296 | |
paul@44 | 297 | This summary groups all attribute types (class, instance, module) into a |
paul@44 | 298 | single line in order to determine the complexity of each access. |
paul@44 | 299 | |
paul@44 | 300 | Tests can be "validate", "specific", "untested", "guarded-validate" or "guarded-specific". |
paul@44 | 301 | |
paul@44 | 302 | ---- |
paul@44 | 303 | |
paul@44 | 304 | For each access where a test would be asserted to guarantee the |
paul@44 | 305 | nature of an attribute, the following formats are employed: |
paul@44 | 306 | |
paul@44 | 307 | location " " attribute name " " "validate" |
paul@44 | 308 | location " " attribute name " " "specific" " " attribute type " " object type |
paul@44 | 309 | |
paul@44 | 310 | ---- |
paul@44 | 311 | |
paul@44 | 312 | References that cannot be supported by any types are written to a |
paul@44 | 313 | warnings file in the following format: |
paul@44 | 314 | |
paul@44 | 315 | location |
paul@44 | 316 | """ |
paul@44 | 317 | |
paul@44 | 318 | f_attr_summary = open(join(self.output, "attribute_summary"), "w") |
paul@44 | 319 | f_attrs = open(join(self.output, "attributes"), "w") |
paul@44 | 320 | f_tests = open(join(self.output, "tests"), "w") |
paul@44 | 321 | f_warnings = open(join(self.output, "attribute_warnings"), "w") |
paul@44 | 322 | |
paul@44 | 323 | try: |
paul@44 | 324 | locations = self.referenced_attrs.keys() |
paul@44 | 325 | locations.sort() |
paul@44 | 326 | |
paul@44 | 327 | for location in locations: |
paul@44 | 328 | constrained = location in self.access_constrained |
paul@44 | 329 | |
paul@44 | 330 | # Attribute information, both name-based and anonymous. |
paul@44 | 331 | |
paul@44 | 332 | referenced_attrs = self.referenced_attrs[location] |
paul@44 | 333 | |
paul@44 | 334 | if referenced_attrs: |
paul@44 | 335 | attrname = get_attrname_from_location(location) |
paul@44 | 336 | |
paul@44 | 337 | all_accessed_attrs = self.reference_all_attrs[location] |
paul@44 | 338 | |
paul@44 | 339 | for attrtype, attrs in self.get_referenced_attrs(location): |
paul@44 | 340 | print >>f_attrs, encode_access_location(location), encode_constrained(constrained), attrtype, sorted_output(attrs) |
paul@44 | 341 | |
paul@44 | 342 | test_type = self.reference_test_types.get(location) |
paul@44 | 343 | |
paul@44 | 344 | # Write the need to test at run time. |
paul@44 | 345 | |
paul@44 | 346 | if test_type == "validate": |
paul@44 | 347 | print >>f_tests, encode_access_location(location), test_type |
paul@44 | 348 | |
paul@44 | 349 | # Write any type checks for anonymous accesses. |
paul@44 | 350 | |
paul@44 | 351 | elif test_type and self.reference_test_accessor_types.get(location): |
paul@44 | 352 | print >>f_tests, encode_access_location(location), test_type, \ |
paul@44 | 353 | sorted_output(all_accessed_attrs), \ |
paul@44 | 354 | self.reference_test_accessor_types[location] |
paul@44 | 355 | |
paul@44 | 356 | print >>f_attr_summary, encode_access_location(location), encode_constrained(constrained), \ |
paul@44 | 357 | test_type or "untested", sorted_output(all_accessed_attrs) |
paul@44 | 358 | |
paul@44 | 359 | else: |
paul@44 | 360 | print >>f_warnings, encode_access_location(location) |
paul@44 | 361 | |
paul@44 | 362 | finally: |
paul@44 | 363 | f_attr_summary.close() |
paul@44 | 364 | f_attrs.close() |
paul@44 | 365 | f_tests.close() |
paul@44 | 366 | f_warnings.close() |
paul@44 | 367 | |
paul@44 | 368 | def classify_accessors(self): |
paul@44 | 369 | |
paul@44 | 370 | "For each program location, classify accessors." |
paul@44 | 371 | |
paul@44 | 372 | # Where instance and module types are defined, class types are also |
paul@44 | 373 | # defined. See: init_definition_details |
paul@44 | 374 | |
paul@44 | 375 | locations = self.accessor_class_types.keys() |
paul@44 | 376 | |
paul@44 | 377 | for location in locations: |
paul@44 | 378 | constrained = location in self.reference_constrained |
paul@44 | 379 | |
paul@44 | 380 | # Provider information. |
paul@44 | 381 | |
paul@44 | 382 | class_types = self.provider_class_types[location] |
paul@44 | 383 | instance_types = self.provider_instance_types[location] |
paul@44 | 384 | module_types = self.provider_module_types[location] |
paul@44 | 385 | |
paul@44 | 386 | # Collect specific and general type information. |
paul@44 | 387 | |
paul@44 | 388 | self.provider_all_types[location] = all_types = \ |
paul@44 | 389 | self.combine_types(class_types, instance_types, module_types) |
paul@44 | 390 | |
paul@44 | 391 | # Accessor information. |
paul@44 | 392 | |
paul@44 | 393 | class_types = self.accessor_class_types[location] |
paul@44 | 394 | self.accessor_general_class_types[location] = \ |
paul@44 | 395 | general_class_types = self.get_most_general_types(class_types) |
paul@44 | 396 | |
paul@44 | 397 | instance_types = self.accessor_instance_types[location] |
paul@44 | 398 | self.accessor_general_instance_types[location] = \ |
paul@44 | 399 | general_instance_types = self.get_most_general_types(instance_types) |
paul@44 | 400 | |
paul@44 | 401 | module_types = self.accessor_module_types[location] |
paul@44 | 402 | self.accessor_general_module_types[location] = \ |
paul@44 | 403 | general_module_types = self.get_most_general_module_types(module_types) |
paul@44 | 404 | |
paul@44 | 405 | # Collect specific and general type information. |
paul@44 | 406 | |
paul@44 | 407 | self.accessor_all_types[location] = all_types = \ |
paul@44 | 408 | self.combine_types(class_types, instance_types, module_types) |
paul@44 | 409 | |
paul@44 | 410 | self.accessor_all_general_types[location] = all_general_types = \ |
paul@44 | 411 | self.combine_types(general_class_types, general_instance_types, general_module_types) |
paul@44 | 412 | |
paul@44 | 413 | # Record guard information. |
paul@44 | 414 | |
paul@44 | 415 | if not constrained: |
paul@44 | 416 | |
paul@44 | 417 | # Record specific type guard details. |
paul@44 | 418 | |
paul@44 | 419 | if len(all_types) == 1: |
paul@44 | 420 | self.accessor_guard_tests[location] = self.test_for_types("specific", all_types) |
paul@44 | 421 | elif self.is_single_class_type(all_types): |
paul@44 | 422 | self.accessor_guard_tests[location] = "specific-object" |
paul@44 | 423 | |
paul@44 | 424 | # Record common type guard details. |
paul@44 | 425 | |
paul@44 | 426 | elif len(all_general_types) == 1: |
paul@44 | 427 | self.accessor_guard_tests[location] = self.test_for_types("common", all_types) |
paul@44 | 428 | elif self.is_single_class_type(all_general_types): |
paul@44 | 429 | self.accessor_guard_tests[location] = "common-object" |
paul@44 | 430 | |
paul@44 | 431 | # Otherwise, no convenient guard can be defined. |
paul@44 | 432 | |
paul@44 | 433 | def classify_accesses(self): |
paul@44 | 434 | |
paul@44 | 435 | "For each program location, classify accesses." |
paul@44 | 436 | |
paul@44 | 437 | # Attribute accesses use potentially different locations to those of |
paul@44 | 438 | # accessors. |
paul@44 | 439 | |
paul@44 | 440 | locations = self.referenced_attrs.keys() |
paul@44 | 441 | |
paul@44 | 442 | for location in locations: |
paul@44 | 443 | constrained = location in self.access_constrained |
paul@44 | 444 | |
paul@44 | 445 | # Determine whether the attribute access is guarded or not. |
paul@44 | 446 | |
paul@44 | 447 | accessor_locations = self.get_accessors_for_access(location) |
paul@44 | 448 | |
paul@44 | 449 | all_provider_types = set() |
paul@44 | 450 | all_accessor_types = set() |
paul@44 | 451 | all_accessor_general_types = set() |
paul@44 | 452 | |
paul@44 | 453 | for accessor_location in accessor_locations: |
paul@44 | 454 | |
paul@44 | 455 | # Obtain the provider types for guard-related attribute access |
paul@44 | 456 | # checks. |
paul@44 | 457 | |
paul@44 | 458 | provider_guard_types = self.provider_all_types.get(accessor_location) |
paul@44 | 459 | all_provider_types.update(provider_guard_types) |
paul@44 | 460 | |
paul@44 | 461 | # Obtain the accessor types. |
paul@44 | 462 | |
paul@44 | 463 | accessor_guard_types = self.accessor_all_types.get(accessor_location) |
paul@44 | 464 | accessor_general_guard_types = self.accessor_all_general_types.get(accessor_location) |
paul@44 | 465 | all_accessor_types.update(accessor_guard_types) |
paul@44 | 466 | all_accessor_general_types.update(accessor_general_guard_types) |
paul@44 | 467 | |
paul@44 | 468 | # Determine the basis on which the access has been guarded. |
paul@44 | 469 | |
paul@44 | 470 | guarded = ( |
paul@44 | 471 | len(all_accessor_types) == 1 or |
paul@44 | 472 | self.is_single_class_type(all_accessor_types) or |
paul@44 | 473 | len(all_accessor_general_types) == 1 or |
paul@44 | 474 | self.is_single_class_type(all_accessor_general_types) |
paul@44 | 475 | ) |
paul@44 | 476 | |
paul@44 | 477 | if guarded: |
paul@44 | 478 | (guard_class_types, guard_instance_types, guard_module_types, |
paul@44 | 479 | _function_types, _var_types) = self.separate_types(all_provider_types) |
paul@44 | 480 | |
paul@44 | 481 | # Attribute information, both name-based and anonymous. |
paul@44 | 482 | |
paul@44 | 483 | referenced_attrs = self.referenced_attrs[location] |
paul@44 | 484 | |
paul@44 | 485 | if referenced_attrs: |
paul@44 | 486 | |
paul@44 | 487 | # Record attribute information for each name used on the |
paul@44 | 488 | # accessor. |
paul@44 | 489 | |
paul@44 | 490 | attrname = get_attrname_from_location(location) |
paul@44 | 491 | |
paul@44 | 492 | all_accessed_attrs = set() |
paul@44 | 493 | all_accessed_attrtypes = set() |
paul@44 | 494 | all_providers = set() |
paul@44 | 495 | all_general_providers = set() |
paul@44 | 496 | |
paul@44 | 497 | for attrtype, d, general in [ |
paul@44 | 498 | ("<class>", self.reference_class_attrs, self.get_most_general_types), |
paul@44 | 499 | ("<instance>", self.reference_instance_attrs, self.get_most_general_types), |
paul@44 | 500 | ("<module>", self.reference_module_attrs, self.get_most_general_module_types)]: |
paul@44 | 501 | |
paul@44 | 502 | attrs = [attr for _attrtype, object_type, attr in referenced_attrs if _attrtype == attrtype] |
paul@44 | 503 | providers = [object_type for _attrtype, object_type, attr in referenced_attrs if _attrtype == attrtype] |
paul@44 | 504 | general_providers = general(providers) |
paul@44 | 505 | d[location] = set(attrs) |
paul@44 | 506 | |
paul@44 | 507 | if attrs: |
paul@44 | 508 | all_accessed_attrs.update(attrs) |
paul@44 | 509 | all_accessed_attrtypes.add(attrtype) |
paul@44 | 510 | all_providers.update(providers) |
paul@44 | 511 | all_general_providers.update(general_providers) |
paul@44 | 512 | |
paul@44 | 513 | # Determine which attributes would be provided by the |
paul@44 | 514 | # accessor types upheld by a guard. |
paul@44 | 515 | |
paul@44 | 516 | if guarded: |
paul@44 | 517 | guard_attrs = [attr for _attrtype, object_type, attr in |
paul@44 | 518 | self._identify_reference_attribute(attrname, guard_class_types, guard_instance_types, guard_module_types)] |
paul@44 | 519 | else: |
paul@44 | 520 | guard_attrs = None |
paul@44 | 521 | |
paul@44 | 522 | self.reference_all_attrs[location] = all_accessed_attrs |
paul@44 | 523 | self.reference_all_attrtypes[location] = all_accessed_attrtypes |
paul@44 | 524 | |
paul@44 | 525 | # Suitably guarded accesses, where the nature of the |
paul@44 | 526 | # accessor can be guaranteed, do not require the attribute |
paul@44 | 527 | # involved to be validated. Otherwise, for unguarded |
paul@44 | 528 | # accesses, access-level tests are required. |
paul@44 | 529 | |
paul@44 | 530 | if not constrained: |
paul@44 | 531 | |
paul@44 | 532 | # Provide informational test types. |
paul@44 | 533 | |
paul@44 | 534 | if guarded and all_accessed_attrs.issubset(guard_attrs): |
paul@44 | 535 | if len(all_accessor_types) == 1: |
paul@44 | 536 | self.reference_test_types[location] = self.test_for_types("guarded-specific", all_accessor_types) |
paul@44 | 537 | elif self.is_single_class_type(all_accessor_types): |
paul@44 | 538 | self.reference_test_types[location] = "guarded-specific-object" |
paul@44 | 539 | elif len(all_accessor_general_types) == 1: |
paul@44 | 540 | self.reference_test_types[location] = self.test_for_types("guarded-common", all_accessor_general_types) |
paul@44 | 541 | elif self.is_single_class_type(all_accessor_general_types): |
paul@44 | 542 | self.reference_test_types[location] = "guarded-common-object" |
paul@44 | 543 | |
paul@44 | 544 | # Provide active test types. |
paul@44 | 545 | |
paul@44 | 546 | else: |
paul@44 | 547 | # Record the need to test the type of anonymous and |
paul@44 | 548 | # unconstrained accessors. |
paul@44 | 549 | |
paul@44 | 550 | if len(all_providers) == 1: |
paul@44 | 551 | provider = list(all_providers)[0] |
paul@44 | 552 | if provider != '__builtins__.object': |
paul@44 | 553 | all_accessor_kinds = set(self.get_kinds(all_accessor_types)) |
paul@44 | 554 | if len(all_accessor_kinds) == 1: |
paul@44 | 555 | test_type = self.test_for_kinds("specific", all_accessor_kinds) |
paul@44 | 556 | else: |
paul@44 | 557 | test_type = "specific-object" |
paul@44 | 558 | self.reference_test_types[location] = test_type |
paul@44 | 559 | self.reference_test_accessor_types[location] = provider |
paul@44 | 560 | |
paul@44 | 561 | elif len(all_general_providers) == 1: |
paul@44 | 562 | provider = list(all_general_providers)[0] |
paul@44 | 563 | if provider != '__builtins__.object': |
paul@44 | 564 | all_accessor_kinds = set(self.get_kinds(all_accessor_general_types)) |
paul@44 | 565 | if len(all_accessor_kinds) == 1: |
paul@44 | 566 | test_type = self.test_for_kinds("common", all_accessor_kinds) |
paul@44 | 567 | else: |
paul@44 | 568 | test_type = "common-object" |
paul@44 | 569 | self.reference_test_types[location] = test_type |
paul@44 | 570 | self.reference_test_accessor_types[location] = provider |
paul@44 | 571 | |
paul@44 | 572 | # Record the need to test the identity of the attribute. |
paul@44 | 573 | |
paul@44 | 574 | else: |
paul@44 | 575 | self.reference_test_types[location] = "validate" |
paul@44 | 576 | |
paul@44 | 577 | def get_referenced_attrs(self, location): |
paul@44 | 578 | |
paul@44 | 579 | """ |
paul@44 | 580 | Return attributes referenced at the given access 'location' by the given |
paul@44 | 581 | 'attrname' as a list of (attribute type, attribute set) tuples. |
paul@44 | 582 | """ |
paul@44 | 583 | |
paul@44 | 584 | ca = self.reference_class_attrs[location] |
paul@44 | 585 | ia = self.reference_instance_attrs[location] |
paul@44 | 586 | ma = self.reference_module_attrs[location] |
paul@44 | 587 | |
paul@44 | 588 | l = [] |
paul@44 | 589 | for attrtype, attrs in (("<class>", ca), ("<instance>", ia), ("<module>", ma)): |
paul@44 | 590 | if attrs: |
paul@44 | 591 | l.append((attrtype, attrs)) |
paul@44 | 592 | return l |
paul@44 | 593 | |
paul@44 | 594 | # Test generation methods. |
paul@44 | 595 | |
paul@44 | 596 | def get_kinds(self, all_types): |
paul@44 | 597 | |
paul@44 | 598 | """ |
paul@44 | 599 | Return object kind details for 'all_types', being a collection of |
paul@44 | 600 | references for program types. |
paul@44 | 601 | """ |
paul@44 | 602 | |
paul@44 | 603 | return map(lambda ref: ref.get_kind(), all_types) |
paul@44 | 604 | |
paul@44 | 605 | def test_for_types(self, prefix, all_types): |
paul@44 | 606 | |
paul@44 | 607 | """ |
paul@44 | 608 | Return identifiers describing test conditions incorporating the given |
paul@44 | 609 | 'prefix' and involving 'all_types', being a collection of references to |
paul@44 | 610 | program types. |
paul@44 | 611 | """ |
paul@44 | 612 | |
paul@44 | 613 | return self.test_for_kind(prefix, first(all_types).get_kind()) |
paul@44 | 614 | |
paul@44 | 615 | def test_for_kinds(self, prefix, all_kinds): |
paul@44 | 616 | |
paul@44 | 617 | """ |
paul@44 | 618 | Return identifiers describing test conditions incorporating the given |
paul@44 | 619 | 'prefix' and involving 'all_kinds', being a collection of object kinds. |
paul@44 | 620 | """ |
paul@44 | 621 | |
paul@44 | 622 | return self.test_for_kind(prefix, first(all_kinds)) |
paul@44 | 623 | |
paul@44 | 624 | def test_for_kind(self, prefix, kind): |
paul@44 | 625 | |
paul@44 | 626 | "Return a test condition identifier featuring 'prefix' and 'kind'." |
paul@44 | 627 | |
paul@44 | 628 | return "%s-%s" % (prefix, kind == "<instance>" and "instance" or "type") |
paul@44 | 629 | |
paul@44 | 630 | # Type handling methods. |
paul@44 | 631 | |
paul@44 | 632 | def is_single_class_type(self, all_types): |
paul@44 | 633 | |
paul@44 | 634 | """ |
paul@44 | 635 | Return whether 'all_types' is a mixture of class and instance kinds for |
paul@44 | 636 | a single class type. |
paul@44 | 637 | """ |
paul@44 | 638 | |
paul@44 | 639 | kinds = set() |
paul@44 | 640 | types = set() |
paul@44 | 641 | |
paul@44 | 642 | for type in all_types: |
paul@44 | 643 | kinds.add(type.get_kind()) |
paul@44 | 644 | types.add(type.get_origin()) |
paul@44 | 645 | |
paul@44 | 646 | return len(types) == 1 and kinds == set(["<class>", "<instance>"]) |
paul@44 | 647 | |
paul@44 | 648 | def get_types_for_reference(self, ref): |
paul@44 | 649 | |
paul@44 | 650 | "Return class, instance-only and module types for 'ref'." |
paul@44 | 651 | |
paul@44 | 652 | class_types = ref.has_kind("<class>") and [ref.get_origin()] or [] |
paul@44 | 653 | instance_types = [] |
paul@44 | 654 | module_types = ref.has_kind("<module>") and [ref.get_origin()] or [] |
paul@44 | 655 | return class_types, instance_types, module_types |
paul@44 | 656 | |
paul@44 | 657 | def combine_types(self, class_types, instance_types, module_types): |
paul@44 | 658 | |
paul@44 | 659 | """ |
paul@44 | 660 | Combine 'class_types', 'instance_types', 'module_types' into a single |
paul@44 | 661 | list of references. |
paul@44 | 662 | """ |
paul@44 | 663 | |
paul@44 | 664 | all_types = [] |
paul@44 | 665 | for kind, l in [("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types)]: |
paul@44 | 666 | for t in l: |
paul@44 | 667 | all_types.append(Reference(kind, t)) |
paul@44 | 668 | return all_types |
paul@44 | 669 | |
paul@44 | 670 | def separate_types(self, refs): |
paul@44 | 671 | |
paul@44 | 672 | """ |
paul@44 | 673 | Separate 'refs' into type-specific lists, returning a tuple containing |
paul@44 | 674 | lists of class types, instance types, module types, function types and |
paul@44 | 675 | unknown "var" types. |
paul@44 | 676 | """ |
paul@44 | 677 | |
paul@44 | 678 | class_types = [] |
paul@44 | 679 | instance_types = [] |
paul@44 | 680 | module_types = [] |
paul@44 | 681 | function_types = [] |
paul@44 | 682 | var_types = [] |
paul@44 | 683 | |
paul@44 | 684 | for kind, l in [ |
paul@44 | 685 | ("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types), |
paul@44 | 686 | ("<function>", function_types), ("<var>", var_types) |
paul@44 | 687 | ]: |
paul@44 | 688 | |
paul@44 | 689 | for ref in refs: |
paul@44 | 690 | if ref.get_kind() == kind: |
paul@44 | 691 | l.append(ref.get_origin()) |
paul@44 | 692 | |
paul@44 | 693 | return class_types, instance_types, module_types, function_types, var_types |
paul@44 | 694 | |
paul@44 | 695 | # Initialisation methods. |
paul@44 | 696 | |
paul@44 | 697 | def init_descendants(self): |
paul@44 | 698 | |
paul@44 | 699 | "Identify descendants of each class." |
paul@44 | 700 | |
paul@44 | 701 | for name in self.importer.classes.keys(): |
paul@44 | 702 | self.get_descendants_for_class(name) |
paul@44 | 703 | |
paul@44 | 704 | def get_descendants_for_class(self, name): |
paul@44 | 705 | |
paul@44 | 706 | """ |
paul@44 | 707 | Use subclass information to deduce the descendants for the class of the |
paul@44 | 708 | given 'name'. |
paul@44 | 709 | """ |
paul@44 | 710 | |
paul@44 | 711 | if not self.descendants.has_key(name): |
paul@44 | 712 | descendants = set() |
paul@44 | 713 | |
paul@44 | 714 | for subclass in self.importer.subclasses[name]: |
paul@44 | 715 | descendants.update(self.get_descendants_for_class(subclass)) |
paul@44 | 716 | descendants.add(subclass) |
paul@44 | 717 | |
paul@44 | 718 | self.descendants[name] = descendants |
paul@44 | 719 | |
paul@44 | 720 | return self.descendants[name] |
paul@44 | 721 | |
paul@44 | 722 | def init_special_attributes(self): |
paul@44 | 723 | |
paul@44 | 724 | "Add special attributes to the classes for inheritance-related tests." |
paul@44 | 725 | |
paul@44 | 726 | all_class_attrs = self.importer.all_class_attrs |
paul@44 | 727 | |
paul@44 | 728 | for name, descendants in self.descendants.items(): |
paul@44 | 729 | for descendant in descendants: |
paul@44 | 730 | all_class_attrs[descendant]["#%s" % name] = name |
paul@44 | 731 | |
paul@44 | 732 | for name in all_class_attrs.keys(): |
paul@44 | 733 | all_class_attrs[name]["#%s" % name] = name |
paul@44 | 734 | |
paul@44 | 735 | def init_usage_index(self): |
paul@44 | 736 | |
paul@44 | 737 | """ |
paul@44 | 738 | Create indexes for module and function attribute usage and for anonymous |
paul@44 | 739 | accesses. |
paul@44 | 740 | """ |
paul@44 | 741 | |
paul@44 | 742 | for module in self.importer.get_modules(): |
paul@44 | 743 | for path, assignments in module.attr_usage.items(): |
paul@44 | 744 | self.add_usage(assignments, path) |
paul@44 | 745 | |
paul@44 | 746 | for location, all_attrnames in self.importer.all_attr_accesses.items(): |
paul@44 | 747 | for attrnames in all_attrnames: |
paul@44 | 748 | attrname = get_attrnames(attrnames)[-1] |
paul@44 | 749 | access_location = (location, None, attrnames, 0) |
paul@44 | 750 | self.add_usage_term(access_location, [attrname]) |
paul@44 | 751 | |
paul@44 | 752 | def add_usage(self, assignments, path): |
paul@44 | 753 | |
paul@44 | 754 | """ |
paul@44 | 755 | Collect usage from the given 'assignments', adding 'path' details to |
paul@44 | 756 | each record if specified. Add the usage to an index mapping to location |
paul@44 | 757 | information, as well as to an index mapping locations to usages. |
paul@44 | 758 | """ |
paul@44 | 759 | |
paul@44 | 760 | for name, versions in assignments.items(): |
paul@44 | 761 | for i, usages in enumerate(versions): |
paul@44 | 762 | location = (path, name, None, i) |
paul@44 | 763 | |
paul@44 | 764 | for attrnames in usages: |
paul@44 | 765 | self.add_usage_term(location, attrnames) |
paul@44 | 766 | |
paul@44 | 767 | def add_usage_term(self, location, attrnames): |
paul@44 | 768 | |
paul@44 | 769 | """ |
paul@44 | 770 | For 'location' and using 'attrnames' as a description of usage, record |
paul@44 | 771 | in the usage index a mapping from the usage to 'location', and record in |
paul@44 | 772 | the location index a mapping from 'location' to the usage. |
paul@44 | 773 | """ |
paul@44 | 774 | |
paul@44 | 775 | key = make_key(attrnames) |
paul@44 | 776 | |
paul@44 | 777 | init_item(self.location_index, location, set) |
paul@44 | 778 | self.location_index[location].add(key) |
paul@44 | 779 | |
paul@44 | 780 | def init_accessors(self): |
paul@44 | 781 | |
paul@44 | 782 | "Create indexes for module and function accessor information." |
paul@44 | 783 | |
paul@44 | 784 | for module in self.importer.get_modules(): |
paul@44 | 785 | for path, all_accesses in module.attr_accessors.items(): |
paul@44 | 786 | self.add_accessors(all_accesses, path) |
paul@44 | 787 | |
paul@44 | 788 | def add_accessors(self, all_accesses, path): |
paul@44 | 789 | |
paul@44 | 790 | """ |
paul@44 | 791 | For attribute accesses described by the mapping of 'all_accesses' from |
paul@44 | 792 | name details to accessor details, record the locations of the accessors |
paul@44 | 793 | for each access. |
paul@44 | 794 | """ |
paul@44 | 795 | |
paul@44 | 796 | # Get details for each access combining the given name and attribute. |
paul@44 | 797 | |
paul@44 | 798 | for (name, attrnames), accesses in all_accesses.items(): |
paul@44 | 799 | |
paul@44 | 800 | # Obtain the usage details using the access information. |
paul@44 | 801 | |
paul@44 | 802 | for access_number, versions in enumerate(accesses): |
paul@44 | 803 | access_location = (path, name, attrnames, access_number) |
paul@44 | 804 | locations = [] |
paul@44 | 805 | |
paul@44 | 806 | for version in versions: |
paul@44 | 807 | location = (path, name, None, version) |
paul@44 | 808 | locations.append(location) |
paul@44 | 809 | |
paul@44 | 810 | self.access_index[access_location] = locations |
paul@44 | 811 | |
paul@44 | 812 | def get_accessors_for_access(self, access_location): |
paul@44 | 813 | |
paul@44 | 814 | "Find a definition providing accessor details, if necessary." |
paul@44 | 815 | |
paul@44 | 816 | try: |
paul@44 | 817 | return self.access_index[access_location] |
paul@44 | 818 | except KeyError: |
paul@44 | 819 | return [access_location] |
paul@44 | 820 | |
paul@44 | 821 | def init_accesses(self): |
paul@44 | 822 | |
paul@44 | 823 | """ |
paul@44 | 824 | Initialise collections for accesses involving assignments. |
paul@44 | 825 | """ |
paul@44 | 826 | |
paul@44 | 827 | # For each scope, obtain access details. |
paul@44 | 828 | |
paul@44 | 829 | for path, all_accesses in self.importer.all_attr_access_modifiers.items(): |
paul@44 | 830 | |
paul@44 | 831 | # For each combination of name and attribute names, obtain |
paul@44 | 832 | # applicable modifiers. |
paul@44 | 833 | |
paul@44 | 834 | for (name, attrnames), modifiers in all_accesses.items(): |
paul@44 | 835 | |
paul@44 | 836 | # For each access, determine the name versions affected by |
paul@44 | 837 | # assignments. |
paul@44 | 838 | |
paul@44 | 839 | for access_number, assignment in enumerate(modifiers): |
paul@44 | 840 | if name: |
paul@44 | 841 | access_location = (path, name, attrnames, access_number) |
paul@44 | 842 | else: |
paul@44 | 843 | access_location = (path, None, attrnames, 0) |
paul@44 | 844 | |
paul@44 | 845 | # Associate assignments with usage. |
paul@44 | 846 | |
paul@44 | 847 | accessor_locations = self.get_accessors_for_access(access_location) |
paul@44 | 848 | |
paul@44 | 849 | for location in accessor_locations: |
paul@44 | 850 | for usage in self.location_index[location]: |
paul@44 | 851 | key = make_key(usage) |
paul@44 | 852 | |
paul@44 | 853 | if assignment: |
paul@44 | 854 | init_item(self.assigned_attrs, key, set) |
paul@44 | 855 | self.assigned_attrs[key].add((path, name, attrnames)) |
paul@44 | 856 | |
paul@44 | 857 | def init_aliases(self): |
paul@44 | 858 | |
paul@44 | 859 | "Expand aliases so that alias-based accesses can be resolved." |
paul@44 | 860 | |
paul@44 | 861 | # Get aliased names with details of their accesses. |
paul@44 | 862 | |
paul@44 | 863 | for name_path, all_aliases in self.importer.all_aliased_names.items(): |
paul@44 | 864 | path, name = name_path.rsplit(".", 1) |
paul@44 | 865 | |
paul@44 | 866 | # For each version of the name, obtain the access location. |
paul@44 | 867 | |
paul@44 | 868 | for version, (original_name, attrnames, access_number) in all_aliases.items(): |
paul@44 | 869 | accessor_location = (path, name, None, version) |
paul@44 | 870 | access_location = (path, original_name, attrnames, access_number) |
paul@44 | 871 | init_item(self.alias_index, accessor_location, list) |
paul@44 | 872 | self.alias_index[accessor_location].append(access_location) |
paul@44 | 873 | |
paul@44 | 874 | # Get aliases in terms of non-aliases and accesses. |
paul@44 | 875 | |
paul@44 | 876 | for accessor_location, access_locations in self.alias_index.items(): |
paul@44 | 877 | self.update_aliases(accessor_location, access_locations) |
paul@44 | 878 | |
paul@44 | 879 | def update_aliases(self, accessor_location, access_locations, visited=None): |
paul@44 | 880 | |
paul@44 | 881 | """ |
paul@44 | 882 | Update the given 'accessor_location' defining an alias, update |
paul@44 | 883 | 'access_locations' to refer to non-aliases, following name references |
paul@44 | 884 | via the access index. |
paul@44 | 885 | |
paul@44 | 886 | If 'visited' is specified, it contains a set of accessor locations (and |
paul@44 | 887 | thus keys to the alias index) that are currently being defined. |
paul@44 | 888 | """ |
paul@44 | 889 | |
paul@44 | 890 | if visited is None: |
paul@44 | 891 | visited = set() |
paul@44 | 892 | |
paul@44 | 893 | updated_locations = set() |
paul@44 | 894 | |
paul@44 | 895 | for access_location in access_locations: |
paul@44 | 896 | (path, original_name, attrnames, access_number) = access_location |
paul@44 | 897 | |
paul@44 | 898 | # Where an alias refers to a name access, obtain the original name |
paul@44 | 899 | # version details. |
paul@44 | 900 | |
paul@44 | 901 | if attrnames is None: |
paul@44 | 902 | |
paul@44 | 903 | # For each name version, attempt to determine any accesses that |
paul@44 | 904 | # initialise the name. |
paul@44 | 905 | |
paul@44 | 906 | for name_accessor_location in self.access_index[access_location]: |
paul@44 | 907 | |
paul@44 | 908 | # Already-visited aliases do not contribute details. |
paul@44 | 909 | |
paul@44 | 910 | if name_accessor_location in visited: |
paul@44 | 911 | continue |
paul@44 | 912 | |
paul@44 | 913 | visited.add(name_accessor_location) |
paul@44 | 914 | |
paul@44 | 915 | name_access_locations = self.alias_index.get(name_accessor_location) |
paul@44 | 916 | if name_access_locations: |
paul@44 | 917 | updated_locations.update(self.update_aliases(name_accessor_location, name_access_locations, visited)) |
paul@44 | 918 | else: |
paul@44 | 919 | updated_locations.add(name_accessor_location) |
paul@44 | 920 | |
paul@44 | 921 | # Otherwise, record the access details. |
paul@44 | 922 | |
paul@44 | 923 | else: |
paul@44 | 924 | updated_locations.add(access_location) |
paul@44 | 925 | |
paul@44 | 926 | self.alias_index[accessor_location] = updated_locations |
paul@44 | 927 | return updated_locations |
paul@44 | 928 | |
paul@44 | 929 | # Attribute mutation for types. |
paul@44 | 930 | |
paul@44 | 931 | def modify_mutated_attributes(self): |
paul@44 | 932 | |
paul@44 | 933 | "Identify known, mutated attributes and change their state." |
paul@44 | 934 | |
paul@44 | 935 | # Usage-based accesses. |
paul@44 | 936 | |
paul@44 | 937 | for usage, all_attrnames in self.assigned_attrs.items(): |
paul@44 | 938 | if not usage: |
paul@44 | 939 | continue |
paul@44 | 940 | |
paul@44 | 941 | for path, name, attrnames in all_attrnames: |
paul@44 | 942 | class_types = self.get_class_types_for_usage(usage) |
paul@44 | 943 | only_instance_types = set(self.get_instance_types_for_usage(usage)).difference(class_types) |
paul@44 | 944 | module_types = self.get_module_types_for_usage(usage) |
paul@44 | 945 | |
paul@44 | 946 | # Detect self usage within methods in order to narrow the scope |
paul@44 | 947 | # of the mutation. |
paul@44 | 948 | |
paul@44 | 949 | t = name == "self" and self.constrain_self_reference(path, class_types, only_instance_types) |
paul@44 | 950 | if t: |
paul@44 | 951 | class_types, only_instance_types, module_types, constrained = t |
paul@44 | 952 | objects = set(class_types).union(only_instance_types).union(module_types) |
paul@44 | 953 | |
paul@44 | 954 | self.mutate_attribute(objects, attrnames) |
paul@44 | 955 | |
paul@44 | 956 | def mutate_attribute(self, objects, attrnames): |
paul@44 | 957 | |
paul@44 | 958 | "Mutate static 'objects' with the given 'attrnames'." |
paul@44 | 959 | |
paul@44 | 960 | for name in objects: |
paul@44 | 961 | attr = "%s.%s" % (name, attrnames) |
paul@44 | 962 | value = self.importer.get_object(attr) |
paul@44 | 963 | |
paul@44 | 964 | # If the value is None, the attribute is |
paul@44 | 965 | # inherited and need not be set explicitly on |
paul@44 | 966 | # the class concerned. |
paul@44 | 967 | |
paul@44 | 968 | if value: |
paul@44 | 969 | self.modified_attributes[attr] = value |
paul@44 | 970 | self.importer.set_object(attr, value.as_var()) |
paul@44 | 971 | |
paul@44 | 972 | # Simplification of types. |
paul@44 | 973 | |
paul@44 | 974 | def get_most_general_types(self, class_types): |
paul@44 | 975 | |
paul@44 | 976 | "Return the most general types for the given 'class_types'." |
paul@44 | 977 | |
paul@44 | 978 | class_types = set(class_types) |
paul@44 | 979 | to_remove = set() |
paul@44 | 980 | |
paul@44 | 981 | for class_type in class_types: |
paul@44 | 982 | for base in self.importer.classes[class_type]: |
paul@44 | 983 | base = base.get_origin() |
paul@44 | 984 | descendants = self.descendants[base] |
paul@44 | 985 | if base in class_types and descendants.issubset(class_types): |
paul@44 | 986 | to_remove.update(descendants) |
paul@44 | 987 | |
paul@44 | 988 | class_types.difference_update(to_remove) |
paul@44 | 989 | return class_types |
paul@44 | 990 | |
paul@44 | 991 | def get_most_general_module_types(self, module_types): |
paul@44 | 992 | |
paul@44 | 993 | "Return the most general type for the given 'module_types'." |
paul@44 | 994 | |
paul@44 | 995 | # Where all modules are provided, an object would provide the same |
paul@44 | 996 | # attributes. |
paul@44 | 997 | |
paul@44 | 998 | if len(module_types) == len(self.importer.modules): |
paul@44 | 999 | return ["__builtins__.object"] |
paul@44 | 1000 | else: |
paul@44 | 1001 | return module_types |
paul@44 | 1002 | |
paul@44 | 1003 | # Type deduction for usage. |
paul@44 | 1004 | |
paul@44 | 1005 | def get_types_for_usage(self, attrnames, objects): |
paul@44 | 1006 | |
paul@44 | 1007 | """ |
paul@44 | 1008 | Identify the types that can support the given 'attrnames', using the |
paul@44 | 1009 | given 'objects' as the catalogue of type details. |
paul@44 | 1010 | """ |
paul@44 | 1011 | |
paul@44 | 1012 | types = [] |
paul@44 | 1013 | for name, _attrnames in objects.items(): |
paul@44 | 1014 | if set(attrnames).issubset(_attrnames): |
paul@44 | 1015 | types.append(name) |
paul@44 | 1016 | return types |
paul@44 | 1017 | |
paul@44 | 1018 | # More efficient usage-to-type indexing and retrieval. |
paul@44 | 1019 | |
paul@44 | 1020 | def init_attr_type_indexes(self): |
paul@44 | 1021 | |
paul@44 | 1022 | "Identify the types that can support each attribute name." |
paul@44 | 1023 | |
paul@44 | 1024 | self._init_attr_type_index(self.attr_class_types, self.importer.all_class_attrs) |
paul@44 | 1025 | self._init_attr_type_index(self.attr_instance_types, self.importer.all_combined_attrs) |
paul@44 | 1026 | self._init_attr_type_index(self.attr_module_types, self.importer.all_module_attrs) |
paul@44 | 1027 | |
paul@44 | 1028 | def _init_attr_type_index(self, attr_types, attrs): |
paul@44 | 1029 | |
paul@44 | 1030 | """ |
paul@44 | 1031 | Initialise the 'attr_types' attribute-to-types mapping using the given |
paul@44 | 1032 | 'attrs' type-to-attributes mapping. |
paul@44 | 1033 | """ |
paul@44 | 1034 | |
paul@44 | 1035 | for name, attrnames in attrs.items(): |
paul@44 | 1036 | for attrname in attrnames: |
paul@44 | 1037 | init_item(attr_types, attrname, set) |
paul@44 | 1038 | attr_types[attrname].add(name) |
paul@44 | 1039 | |
paul@44 | 1040 | def get_class_types_for_usage(self, attrnames): |
paul@44 | 1041 | |
paul@44 | 1042 | "Return names of classes supporting the given 'attrnames'." |
paul@44 | 1043 | |
paul@44 | 1044 | return self._get_types_for_usage(attrnames, self.attr_class_types, self.importer.all_class_attrs) |
paul@44 | 1045 | |
paul@44 | 1046 | def get_instance_types_for_usage(self, attrnames): |
paul@44 | 1047 | |
paul@44 | 1048 | """ |
paul@44 | 1049 | Return names of classes whose instances support the given 'attrnames' |
paul@44 | 1050 | (as either class or instance attributes). |
paul@44 | 1051 | """ |
paul@44 | 1052 | |
paul@44 | 1053 | return self._get_types_for_usage(attrnames, self.attr_instance_types, self.importer.all_combined_attrs) |
paul@44 | 1054 | |
paul@44 | 1055 | def get_module_types_for_usage(self, attrnames): |
paul@44 | 1056 | |
paul@44 | 1057 | "Return names of modules supporting the given 'attrnames'." |
paul@44 | 1058 | |
paul@44 | 1059 | return self._get_types_for_usage(attrnames, self.attr_module_types, self.importer.all_module_attrs) |
paul@44 | 1060 | |
paul@44 | 1061 | def _get_types_for_usage(self, attrnames, attr_types, attrs): |
paul@44 | 1062 | |
paul@44 | 1063 | """ |
paul@44 | 1064 | For the given 'attrnames' representing attribute usage, return types |
paul@44 | 1065 | recorded in the 'attr_types' attribute-to-types mapping that support |
paul@44 | 1066 | such usage, with the given 'attrs' type-to-attributes mapping used to |
paul@44 | 1067 | quickly assess whether a type supports all of the stated attributes. |
paul@44 | 1068 | """ |
paul@44 | 1069 | |
paul@44 | 1070 | # Where no attributes are used, any type would be acceptable. |
paul@44 | 1071 | |
paul@44 | 1072 | if not attrnames: |
paul@44 | 1073 | return attrs.keys() |
paul@44 | 1074 | |
paul@44 | 1075 | types = [] |
paul@44 | 1076 | |
paul@44 | 1077 | # Obtain types supporting the first attribute name... |
paul@44 | 1078 | |
paul@44 | 1079 | for name in attr_types.get(attrnames[0]) or []: |
paul@44 | 1080 | |
paul@44 | 1081 | # Record types that support all of the other attributes as well. |
paul@44 | 1082 | |
paul@44 | 1083 | _attrnames = attrs[name] |
paul@44 | 1084 | if set(attrnames).issubset(_attrnames): |
paul@44 | 1085 | types.append(name) |
paul@44 | 1086 | |
paul@44 | 1087 | return types |
paul@44 | 1088 | |
paul@44 | 1089 | # Reference identification. |
paul@44 | 1090 | |
paul@44 | 1091 | def identify_references(self): |
paul@44 | 1092 | |
paul@44 | 1093 | "Identify references using usage and name reference information." |
paul@44 | 1094 | |
paul@44 | 1095 | # Names with associated attribute usage. |
paul@44 | 1096 | |
paul@44 | 1097 | for location, usages in self.location_index.items(): |
paul@44 | 1098 | |
paul@44 | 1099 | # Obtain attribute usage associated with a name, deducing the nature |
paul@44 | 1100 | # of the name. Obtain types only for branches involving attribute |
paul@44 | 1101 | # usage. (In the absence of usage, any type could be involved, but |
paul@44 | 1102 | # then no accesses exist to require knowledge of the type.) |
paul@44 | 1103 | |
paul@44 | 1104 | have_usage = False |
paul@44 | 1105 | have_no_usage_branch = False |
paul@44 | 1106 | |
paul@44 | 1107 | for usage in usages: |
paul@44 | 1108 | if not usage: |
paul@44 | 1109 | have_no_usage_branch = True |
paul@44 | 1110 | continue |
paul@44 | 1111 | elif not have_usage: |
paul@44 | 1112 | self.init_definition_details(location) |
paul@44 | 1113 | have_usage = True |
paul@44 | 1114 | self.record_types_for_usage(location, usage) |
paul@44 | 1115 | |
paul@44 | 1116 | # Where some usage occurs, but where branches without usage also |
paul@44 | 1117 | # occur, record the types for those branches anyway. |
paul@44 | 1118 | |
paul@44 | 1119 | if have_usage and have_no_usage_branch: |
paul@44 | 1120 | self.init_definition_details(location) |
paul@44 | 1121 | self.record_types_for_usage(location, None) |
paul@44 | 1122 | |
paul@44 | 1123 | # Specific name-based attribute accesses. |
paul@44 | 1124 | |
paul@44 | 1125 | alias_accesses = set() |
paul@44 | 1126 | |
paul@44 | 1127 | for access_location, accessor_locations in self.access_index.items(): |
paul@44 | 1128 | self.record_types_for_access(access_location, accessor_locations, alias_accesses) |
paul@44 | 1129 | |
paul@44 | 1130 | # Aliased name definitions. All aliases with usage will have been |
paul@44 | 1131 | # defined, but they may be refined according to referenced accesses. |
paul@44 | 1132 | |
paul@44 | 1133 | for accessor_location in self.alias_index.keys(): |
paul@44 | 1134 | self.record_types_for_alias(accessor_location) |
paul@44 | 1135 | |
paul@44 | 1136 | # Update accesses employing aliases. |
paul@44 | 1137 | |
paul@44 | 1138 | for access_location in alias_accesses: |
paul@44 | 1139 | self.record_types_for_access(access_location, self.access_index[access_location]) |
paul@44 | 1140 | |
paul@44 | 1141 | # Anonymous references with attribute chains. |
paul@44 | 1142 | |
paul@44 | 1143 | for location, accesses in self.importer.all_attr_accesses.items(): |
paul@44 | 1144 | |
paul@44 | 1145 | # Get distinct attribute names. |
paul@44 | 1146 | |
paul@44 | 1147 | all_attrnames = set() |
paul@44 | 1148 | |
paul@44 | 1149 | for attrnames in accesses: |
paul@44 | 1150 | all_attrnames.update(get_attrnames(attrnames)) |
paul@44 | 1151 | |
paul@44 | 1152 | # Get attribute and accessor details for each attribute name. |
paul@44 | 1153 | |
paul@44 | 1154 | for attrname in all_attrnames: |
paul@44 | 1155 | access_location = (location, None, attrname, 0) |
paul@44 | 1156 | self.record_types_for_attribute(access_location, attrname) |
paul@44 | 1157 | |
paul@44 | 1158 | # References via constant/identified objects. |
paul@44 | 1159 | |
paul@44 | 1160 | for location, name_accesses in self.importer.all_const_accesses.items(): |
paul@44 | 1161 | |
paul@44 | 1162 | # A mapping from the original name and attributes to resolved access |
paul@44 | 1163 | # details. |
paul@44 | 1164 | |
paul@44 | 1165 | for original_access, access in name_accesses.items(): |
paul@44 | 1166 | original_name, original_attrnames = original_access |
paul@44 | 1167 | objpath, ref, attrnames = access |
paul@44 | 1168 | |
paul@44 | 1169 | # Build an accessor combining the name and attribute names used. |
paul@44 | 1170 | |
paul@44 | 1171 | original_accessor = tuple([original_name] + original_attrnames.split(".")) |
paul@44 | 1172 | |
paul@44 | 1173 | # Direct accesses to attributes. |
paul@44 | 1174 | |
paul@44 | 1175 | if not attrnames: |
paul@44 | 1176 | |
paul@44 | 1177 | # Build a descriptive location based on the original |
paul@44 | 1178 | # details, exposing the final attribute name. |
paul@44 | 1179 | |
paul@44 | 1180 | oa, attrname = original_accessor[:-1], original_accessor[-1] |
paul@44 | 1181 | oa = ".".join(oa) |
paul@44 | 1182 | |
paul@44 | 1183 | access_location = (location, oa, attrname, 0) |
paul@44 | 1184 | accessor_location = (location, oa, None, 0) |
paul@44 | 1185 | self.access_index[access_location] = [accessor_location] |
paul@44 | 1186 | |
paul@44 | 1187 | self.init_access_details(access_location) |
paul@44 | 1188 | self.init_definition_details(accessor_location) |
paul@44 | 1189 | |
paul@44 | 1190 | # Obtain a reference for the accessor in order to properly |
paul@44 | 1191 | # determine its type. |
paul@44 | 1192 | |
paul@44 | 1193 | if ref.get_kind() != "<instance>": |
paul@44 | 1194 | objpath = ref.get_origin() |
paul@44 | 1195 | |
paul@44 | 1196 | objpath = objpath.rsplit(".", 1)[0] |
paul@44 | 1197 | |
paul@44 | 1198 | # Where the object name conflicts with the module |
paul@44 | 1199 | # providing it, obtain the module details. |
paul@44 | 1200 | |
paul@44 | 1201 | if objpath in self.importer.modules: |
paul@44 | 1202 | accessor = Reference("<module>", objpath) |
paul@44 | 1203 | else: |
paul@44 | 1204 | accessor = self.importer.get_object(objpath) |
paul@44 | 1205 | |
paul@44 | 1206 | self.referenced_attrs[access_location] = [(accessor.get_kind(), accessor.get_origin(), ref)] |
paul@44 | 1207 | self.access_constrained.add(access_location) |
paul@44 | 1208 | |
paul@44 | 1209 | class_types, instance_types, module_types = self.get_types_for_reference(accessor) |
paul@44 | 1210 | self.record_reference_types(accessor_location, class_types, instance_types, module_types, True, True) |
paul@44 | 1211 | continue |
paul@44 | 1212 | |
paul@44 | 1213 | # Build a descriptive location based on the original |
paul@44 | 1214 | # details, employing the first remaining attribute name. |
paul@44 | 1215 | |
paul@44 | 1216 | l = get_attrnames(attrnames) |
paul@44 | 1217 | attrname = l[0] |
paul@44 | 1218 | |
paul@44 | 1219 | oa = original_accessor[:-len(l)] |
paul@44 | 1220 | oa = ".".join(oa) |
paul@44 | 1221 | |
paul@44 | 1222 | access_location = (location, oa, attrnames, 0) |
paul@44 | 1223 | accessor_location = (location, oa, None, 0) |
paul@44 | 1224 | self.access_index[access_location] = [accessor_location] |
paul@44 | 1225 | |
paul@44 | 1226 | self.init_access_details(access_location) |
paul@44 | 1227 | self.init_definition_details(accessor_location) |
paul@44 | 1228 | |
paul@44 | 1229 | class_types, instance_types, module_types = self.get_types_for_reference(ref) |
paul@44 | 1230 | |
paul@44 | 1231 | self.identify_reference_attributes(access_location, attrname, class_types, instance_types, module_types, True) |
paul@44 | 1232 | self.record_reference_types(accessor_location, class_types, instance_types, module_types, True, True) |
paul@44 | 1233 | |
paul@44 | 1234 | def constrain_types(self, path, class_types, instance_types, module_types): |
paul@44 | 1235 | |
paul@44 | 1236 | """ |
paul@44 | 1237 | Using the given 'path' to an object, constrain the given 'class_types', |
paul@44 | 1238 | 'instance_types' and 'module_types'. |
paul@44 | 1239 | |
paul@44 | 1240 | Return the class, instance, module types plus whether the types are |
paul@44 | 1241 | constrained to a specific kind of type. |
paul@44 | 1242 | """ |
paul@44 | 1243 | |
paul@44 | 1244 | ref = self.importer.identify(path) |
paul@44 | 1245 | if ref: |
paul@44 | 1246 | |
paul@44 | 1247 | # Constrain usage suggestions using the identified object. |
paul@44 | 1248 | |
paul@44 | 1249 | if ref.has_kind("<class>"): |
paul@44 | 1250 | return ( |
paul@44 | 1251 | set(class_types).intersection([ref.get_origin()]), [], [], True |
paul@44 | 1252 | ) |
paul@44 | 1253 | elif ref.has_kind("<module>"): |
paul@44 | 1254 | return ( |
paul@44 | 1255 | [], [], set(module_types).intersection([ref.get_origin()]), True |
paul@44 | 1256 | ) |
paul@44 | 1257 | |
paul@44 | 1258 | return class_types, instance_types, module_types, False |
paul@44 | 1259 | |
paul@44 | 1260 | def get_target_types(self, location, usage): |
paul@44 | 1261 | |
paul@44 | 1262 | """ |
paul@44 | 1263 | Return the class, instance and module types constrained for the name at |
paul@44 | 1264 | the given 'location' exhibiting the given 'usage'. Whether the types |
paul@44 | 1265 | have been constrained using contextual information is also indicated, |
paul@44 | 1266 | plus whether the types have been constrained to a specific kind of type. |
paul@44 | 1267 | """ |
paul@44 | 1268 | |
paul@44 | 1269 | unit_path, name, attrnames, version = location |
paul@44 | 1270 | |
paul@44 | 1271 | # Detect any initialised name for the location. |
paul@44 | 1272 | |
paul@44 | 1273 | if name: |
paul@44 | 1274 | ref = self.get_initialised_name(location) |
paul@44 | 1275 | if ref: |
paul@44 | 1276 | (class_types, only_instance_types, module_types, |
paul@44 | 1277 | _function_types, _var_types) = self.separate_types([ref]) |
paul@44 | 1278 | return class_types, only_instance_types, module_types, True, False |
paul@44 | 1279 | |
paul@44 | 1280 | # Retrieve the recorded types for the usage. |
paul@44 | 1281 | |
paul@44 | 1282 | class_types = self.get_class_types_for_usage(usage) |
paul@44 | 1283 | only_instance_types = set(self.get_instance_types_for_usage(usage)).difference(class_types) |
paul@44 | 1284 | module_types = self.get_module_types_for_usage(usage) |
paul@44 | 1285 | |
paul@44 | 1286 | # Merge usage deductions with observations to obtain reference types |
paul@44 | 1287 | # for names involved with attribute accesses. |
paul@44 | 1288 | |
paul@44 | 1289 | if not name: |
paul@44 | 1290 | return class_types, only_instance_types, module_types, False, False |
paul@44 | 1291 | |
paul@44 | 1292 | # Obtain references to known objects. |
paul@44 | 1293 | |
paul@44 | 1294 | path = self.get_name_path(unit_path, name) |
paul@44 | 1295 | |
paul@44 | 1296 | class_types, only_instance_types, module_types, constrained_specific = \ |
paul@44 | 1297 | self.constrain_types(path, class_types, only_instance_types, module_types) |
paul@44 | 1298 | |
paul@44 | 1299 | if constrained_specific: |
paul@44 | 1300 | return class_types, only_instance_types, module_types, constrained_specific, constrained_specific |
paul@44 | 1301 | |
paul@44 | 1302 | # Constrain "self" references. |
paul@44 | 1303 | |
paul@44 | 1304 | if name == "self": |
paul@44 | 1305 | t = self.constrain_self_reference(unit_path, class_types, only_instance_types) |
paul@44 | 1306 | if t: |
paul@44 | 1307 | class_types, only_instance_types, module_types, constrained = t |
paul@44 | 1308 | return class_types, only_instance_types, module_types, constrained, False |
paul@44 | 1309 | |
paul@44 | 1310 | return class_types, only_instance_types, module_types, False, False |
paul@44 | 1311 | |
paul@44 | 1312 | def constrain_self_reference(self, unit_path, class_types, only_instance_types): |
paul@44 | 1313 | |
paul@44 | 1314 | """ |
paul@44 | 1315 | Where the name "self" appears in a method, attempt to constrain the |
paul@44 | 1316 | classes involved. |
paul@44 | 1317 | |
paul@44 | 1318 | Return the class, instance, module types plus whether the types are |
paul@44 | 1319 | constrained. |
paul@44 | 1320 | """ |
paul@44 | 1321 | |
paul@44 | 1322 | class_name = self.in_method(unit_path) |
paul@44 | 1323 | |
paul@44 | 1324 | if not class_name: |
paul@44 | 1325 | return None |
paul@44 | 1326 | |
paul@44 | 1327 | classes = set([class_name]) |
paul@44 | 1328 | classes.update(self.get_descendants_for_class(class_name)) |
paul@44 | 1329 | |
paul@44 | 1330 | # Note that only instances will be expected for these references but |
paul@44 | 1331 | # either classes or instances may provide the attributes. |
paul@44 | 1332 | |
paul@44 | 1333 | return ( |
paul@44 | 1334 | set(class_types).intersection(classes), |
paul@44 | 1335 | set(only_instance_types).intersection(classes), |
paul@44 | 1336 | [], True |
paul@44 | 1337 | ) |
paul@44 | 1338 | |
paul@44 | 1339 | def in_method(self, path): |
paul@44 | 1340 | |
paul@44 | 1341 | "Return whether 'path' refers to a method." |
paul@44 | 1342 | |
paul@44 | 1343 | class_name, method_name = path.rsplit(".", 1) |
paul@44 | 1344 | return self.importer.classes.has_key(class_name) and class_name |
paul@44 | 1345 | |
paul@44 | 1346 | def init_reference_details(self, location): |
paul@44 | 1347 | |
paul@44 | 1348 | "Initialise reference-related details for 'location'." |
paul@44 | 1349 | |
paul@44 | 1350 | self.init_definition_details(location) |
paul@44 | 1351 | self.init_access_details(location) |
paul@44 | 1352 | |
paul@44 | 1353 | def init_definition_details(self, location): |
paul@44 | 1354 | |
paul@44 | 1355 | "Initialise name definition details for 'location'." |
paul@44 | 1356 | |
paul@44 | 1357 | self.accessor_class_types[location] = set() |
paul@44 | 1358 | self.accessor_instance_types[location] = set() |
paul@44 | 1359 | self.accessor_module_types[location] = set() |
paul@44 | 1360 | self.provider_class_types[location] = set() |
paul@44 | 1361 | self.provider_instance_types[location] = set() |
paul@44 | 1362 | self.provider_module_types[location] = set() |
paul@44 | 1363 | |
paul@44 | 1364 | def init_access_details(self, location): |
paul@44 | 1365 | |
paul@44 | 1366 | "Initialise access details at 'location'." |
paul@44 | 1367 | |
paul@44 | 1368 | self.referenced_attrs[location] = {} |
paul@44 | 1369 | |
paul@44 | 1370 | def record_types_for_access(self, access_location, accessor_locations, alias_accesses=None): |
paul@44 | 1371 | |
paul@44 | 1372 | """ |
paul@44 | 1373 | Define types for the 'access_location' associated with the given |
paul@44 | 1374 | 'accessor_locations'. |
paul@44 | 1375 | """ |
paul@44 | 1376 | |
paul@44 | 1377 | path, name, attrnames, version = access_location |
paul@44 | 1378 | if not attrnames: |
paul@44 | 1379 | return |
paul@44 | 1380 | |
paul@44 | 1381 | attrname = get_attrnames(attrnames)[0] |
paul@44 | 1382 | |
paul@44 | 1383 | # Collect all suggested types for the accessors. Accesses may |
paul@44 | 1384 | # require accessors from of a subset of the complete set of types. |
paul@44 | 1385 | |
paul@44 | 1386 | class_types = set() |
paul@44 | 1387 | module_types = set() |
paul@44 | 1388 | instance_types = set() |
paul@44 | 1389 | |
paul@44 | 1390 | constrained = True |
paul@44 | 1391 | |
paul@44 | 1392 | for location in accessor_locations: |
paul@44 | 1393 | |
paul@44 | 1394 | # Remember accesses employing aliases. |
paul@44 | 1395 | |
paul@44 | 1396 | if alias_accesses is not None and self.alias_index.has_key(location): |
paul@44 | 1397 | alias_accesses.add(access_location) |
paul@44 | 1398 | |
paul@44 | 1399 | # Use the type information deduced for names from above. |
paul@44 | 1400 | |
paul@44 | 1401 | if self.accessor_class_types.has_key(location): |
paul@44 | 1402 | class_types.update(self.accessor_class_types[location]) |
paul@44 | 1403 | module_types.update(self.accessor_module_types[location]) |
paul@44 | 1404 | instance_types.update(self.accessor_instance_types[location]) |
paul@44 | 1405 | |
paul@44 | 1406 | # Where accesses are associated with assignments but where no |
paul@44 | 1407 | # attribute usage observations have caused such an association, |
paul@44 | 1408 | # the attribute name is considered by itself. |
paul@44 | 1409 | |
paul@44 | 1410 | else: |
paul@44 | 1411 | self.init_definition_details(location) |
paul@44 | 1412 | self.record_types_for_usage(location, [attrname]) |
paul@44 | 1413 | |
paul@44 | 1414 | constrained = location in self.reference_constrained and constrained |
paul@44 | 1415 | |
paul@44 | 1416 | self.init_access_details(access_location) |
paul@44 | 1417 | self.identify_reference_attributes(access_location, attrname, class_types, instance_types, module_types, constrained) |
paul@44 | 1418 | |
paul@44 | 1419 | def record_types_for_usage(self, accessor_location, usage): |
paul@44 | 1420 | |
paul@44 | 1421 | """ |
paul@44 | 1422 | Record types for the given 'accessor_location' according to the given |
paul@44 | 1423 | 'usage' observations which may be None to indicate an absence of usage. |
paul@44 | 1424 | """ |
paul@44 | 1425 | |
paul@44 | 1426 | (class_types, |
paul@44 | 1427 | instance_types, |
paul@44 | 1428 | module_types, |
paul@44 | 1429 | constrained, |
paul@44 | 1430 | constrained_specific) = self.get_target_types(accessor_location, usage) |
paul@44 | 1431 | |
paul@44 | 1432 | self.record_reference_types(accessor_location, class_types, instance_types, module_types, constrained, constrained_specific) |
paul@44 | 1433 | |
paul@44 | 1434 | def record_types_for_attribute(self, access_location, attrname): |
paul@44 | 1435 | |
paul@44 | 1436 | """ |
paul@44 | 1437 | Record types for the 'access_location' employing only the given |
paul@44 | 1438 | 'attrname' for type deduction. |
paul@44 | 1439 | """ |
paul@44 | 1440 | |
paul@44 | 1441 | usage = [attrname] |
paul@44 | 1442 | |
paul@44 | 1443 | class_types = self.get_class_types_for_usage(usage) |
paul@44 | 1444 | only_instance_types = set(self.get_instance_types_for_usage(usage)).difference(class_types) |
paul@44 | 1445 | module_types = self.get_module_types_for_usage(usage) |
paul@44 | 1446 | |
paul@44 | 1447 | self.init_reference_details(access_location) |
paul@44 | 1448 | |
paul@44 | 1449 | self.identify_reference_attributes(access_location, attrname, class_types, only_instance_types, module_types, False) |
paul@44 | 1450 | self.record_reference_types(access_location, class_types, only_instance_types, module_types, False) |
paul@44 | 1451 | |
paul@44 | 1452 | def record_types_for_alias(self, accessor_location): |
paul@44 | 1453 | |
paul@44 | 1454 | """ |
paul@44 | 1455 | Define types for the 'accessor_location' not having associated usage. |
paul@44 | 1456 | """ |
paul@44 | 1457 | |
paul@44 | 1458 | have_access = self.provider_class_types.has_key(accessor_location) |
paul@44 | 1459 | |
paul@44 | 1460 | # With an access, attempt to narrow the existing selection of provider |
paul@44 | 1461 | # types. |
paul@44 | 1462 | |
paul@44 | 1463 | if have_access: |
paul@44 | 1464 | provider_class_types = self.provider_class_types[accessor_location] |
paul@44 | 1465 | provider_instance_types = self.provider_instance_types[accessor_location] |
paul@44 | 1466 | provider_module_types = self.provider_module_types[accessor_location] |
paul@44 | 1467 | |
paul@44 | 1468 | # Find details for any corresponding access. |
paul@44 | 1469 | |
paul@44 | 1470 | all_class_types = set() |
paul@44 | 1471 | all_instance_types = set() |
paul@44 | 1472 | all_module_types = set() |
paul@44 | 1473 | |
paul@44 | 1474 | for access_location in self.alias_index[accessor_location]: |
paul@44 | 1475 | location, name, attrnames, access_number = access_location |
paul@44 | 1476 | |
paul@44 | 1477 | # Alias references an attribute access. |
paul@44 | 1478 | |
paul@44 | 1479 | if attrnames: |
paul@44 | 1480 | |
paul@44 | 1481 | # Obtain attribute references for the access. |
paul@44 | 1482 | |
paul@44 | 1483 | attrs = [attr for _attrtype, object_type, attr in self.referenced_attrs[access_location]] |
paul@44 | 1484 | |
paul@44 | 1485 | # Separate the different attribute types. |
paul@44 | 1486 | |
paul@44 | 1487 | (class_types, instance_types, module_types, |
paul@44 | 1488 | function_types, var_types) = self.separate_types(attrs) |
paul@44 | 1489 | |
paul@44 | 1490 | # Where non-accessor types are found, do not attempt to refine |
paul@44 | 1491 | # the defined accessor types. |
paul@44 | 1492 | |
paul@44 | 1493 | if function_types or var_types: |
paul@44 | 1494 | return |
paul@44 | 1495 | |
paul@44 | 1496 | class_types = set(provider_class_types).intersection(class_types) |
paul@44 | 1497 | instance_types = set(provider_instance_types).intersection(instance_types) |
paul@44 | 1498 | module_types = set(provider_module_types).intersection(module_types) |
paul@44 | 1499 | |
paul@44 | 1500 | # Alias references a name, not an access. |
paul@44 | 1501 | |
paul@44 | 1502 | else: |
paul@44 | 1503 | # Attempt to refine the types using initialised names. |
paul@44 | 1504 | |
paul@44 | 1505 | attr = self.get_initialised_name(access_location) |
paul@44 | 1506 | if attr: |
paul@44 | 1507 | (class_types, instance_types, module_types, |
paul@44 | 1508 | _function_types, _var_types) = self.separate_types([attr]) |
paul@44 | 1509 | |
paul@44 | 1510 | # Where no further information is found, do not attempt to |
paul@44 | 1511 | # refine the defined accessor types. |
paul@44 | 1512 | |
paul@44 | 1513 | else: |
paul@44 | 1514 | return |
paul@44 | 1515 | |
paul@44 | 1516 | all_class_types.update(class_types) |
paul@44 | 1517 | all_instance_types.update(instance_types) |
paul@44 | 1518 | all_module_types.update(module_types) |
paul@44 | 1519 | |
paul@44 | 1520 | # Record refined type details for the alias as an accessor. |
paul@44 | 1521 | |
paul@44 | 1522 | self.init_definition_details(accessor_location) |
paul@44 | 1523 | self.record_reference_types(accessor_location, all_class_types, all_instance_types, all_module_types, False) |
paul@44 | 1524 | |
paul@44 | 1525 | # Without an access, attempt to identify references for the alias. |
paul@44 | 1526 | |
paul@44 | 1527 | else: |
paul@44 | 1528 | refs = set() |
paul@44 | 1529 | |
paul@44 | 1530 | for access_location in self.alias_index[accessor_location]: |
paul@44 | 1531 | location, name, attrnames, access_number = access_location |
paul@44 | 1532 | |
paul@44 | 1533 | # Alias references an attribute access. |
paul@44 | 1534 | |
paul@44 | 1535 | if attrnames: |
paul@44 | 1536 | attrs = [attr for attrtype, object_type, attr in self.referenced_attrs[access_location]] |
paul@44 | 1537 | refs.update(attrs) |
paul@44 | 1538 | |
paul@44 | 1539 | # Alias references a name, not an access. |
paul@44 | 1540 | |
paul@44 | 1541 | else: |
paul@44 | 1542 | attr = self.get_initialised_name(access_location) |
paul@44 | 1543 | attrs = attr and [attr] or [] |
paul@44 | 1544 | if not attrs and self.provider_class_types.has_key(access_location): |
paul@44 | 1545 | class_types = self.provider_class_types[access_location] |
paul@44 | 1546 | instance_types = self.provider_instance_types[access_location] |
paul@44 | 1547 | module_types = self.provider_module_types[access_location] |
paul@44 | 1548 | attrs = self.combine_types(class_types, instance_types, module_types) |
paul@44 | 1549 | if attrs: |
paul@44 | 1550 | refs.update(attrs) |
paul@44 | 1551 | |
paul@44 | 1552 | # Record reference details for the alias separately from accessors. |
paul@44 | 1553 | |
paul@44 | 1554 | self.referenced_objects[accessor_location] = refs |
paul@44 | 1555 | |
paul@44 | 1556 | def get_initialised_name(self, access_location): |
paul@44 | 1557 | |
paul@44 | 1558 | """ |
paul@44 | 1559 | Return references for any initialised names at 'access_location', or |
paul@44 | 1560 | None if no such references exist. |
paul@44 | 1561 | """ |
paul@44 | 1562 | |
paul@44 | 1563 | location, name, attrnames, version = access_location |
paul@44 | 1564 | path = self.get_name_path(location, name) |
paul@44 | 1565 | |
paul@44 | 1566 | # Use initialiser information, if available. |
paul@44 | 1567 | |
paul@44 | 1568 | refs = self.importer.all_initialised_names.get(path) |
paul@44 | 1569 | if refs and refs.has_key(version): |
paul@44 | 1570 | return refs[version] |
paul@44 | 1571 | else: |
paul@44 | 1572 | return None |
paul@44 | 1573 | |
paul@44 | 1574 | def get_name_path(self, path, name): |
paul@44 | 1575 | |
paul@44 | 1576 | "Return a suitable qualified name from the given 'path' and 'name'." |
paul@44 | 1577 | |
paul@44 | 1578 | if "." in name: |
paul@44 | 1579 | return name |
paul@44 | 1580 | else: |
paul@44 | 1581 | return "%s.%s" % (path, name) |
paul@44 | 1582 | |
paul@44 | 1583 | def record_reference_types(self, location, class_types, instance_types, |
paul@44 | 1584 | module_types, constrained, constrained_specific=False): |
paul@44 | 1585 | |
paul@44 | 1586 | """ |
paul@44 | 1587 | Associate attribute provider types with the given 'location', consisting |
paul@44 | 1588 | of the given 'class_types', 'instance_types' and 'module_types'. |
paul@44 | 1589 | |
paul@44 | 1590 | If 'constrained' is indicated, the constrained nature of the accessor is |
paul@44 | 1591 | recorded for the location. |
paul@44 | 1592 | |
paul@44 | 1593 | If 'constrained_specific' is indicated using a true value, instance types |
paul@44 | 1594 | will not be added to class types to permit access via instances at the |
paul@44 | 1595 | given location. This is only useful where a specific accessor is known |
paul@44 | 1596 | to be a class. |
paul@44 | 1597 | |
paul@44 | 1598 | Note that the specified types only indicate the provider types for |
paul@44 | 1599 | attributes, whereas the recorded accessor types indicate the possible |
paul@44 | 1600 | types of the actual objects used to access attributes. |
paul@44 | 1601 | """ |
paul@44 | 1602 | |
paul@44 | 1603 | # Update the type details for the location. |
paul@44 | 1604 | |
paul@44 | 1605 | self.provider_class_types[location].update(class_types) |
paul@44 | 1606 | self.provider_instance_types[location].update(instance_types) |
paul@44 | 1607 | self.provider_module_types[location].update(module_types) |
paul@44 | 1608 | |
paul@44 | 1609 | # Class types support classes and instances as accessors. |
paul@44 | 1610 | # Instance-only and module types support only their own kinds as |
paul@44 | 1611 | # accessors. |
paul@44 | 1612 | |
paul@44 | 1613 | # However, the nature of accessors can be further determined. |
paul@44 | 1614 | # Any self variable may only refer to an instance. |
paul@44 | 1615 | |
paul@44 | 1616 | path, name, version, attrnames = location |
paul@44 | 1617 | if name != "self" or not self.in_method(path): |
paul@44 | 1618 | self.accessor_class_types[location].update(class_types) |
paul@44 | 1619 | |
paul@44 | 1620 | if not constrained_specific: |
paul@44 | 1621 | self.accessor_instance_types[location].update(class_types) |
paul@44 | 1622 | |
paul@44 | 1623 | self.accessor_instance_types[location].update(instance_types) |
paul@44 | 1624 | |
paul@44 | 1625 | if name != "self" or not self.in_method(path): |
paul@44 | 1626 | self.accessor_module_types[location].update(module_types) |
paul@44 | 1627 | |
paul@44 | 1628 | if constrained: |
paul@44 | 1629 | self.reference_constrained.add(location) |
paul@44 | 1630 | |
paul@44 | 1631 | def identify_reference_attributes(self, location, attrname, class_types, instance_types, module_types, constrained): |
paul@44 | 1632 | |
paul@44 | 1633 | """ |
paul@44 | 1634 | Identify reference attributes, associating them with the given |
paul@44 | 1635 | 'location', identifying the given 'attrname', employing the given |
paul@44 | 1636 | 'class_types', 'instance_types' and 'module_types'. |
paul@44 | 1637 | |
paul@44 | 1638 | If 'constrained' is indicated, the constrained nature of the access is |
paul@44 | 1639 | recorded for the location. |
paul@44 | 1640 | """ |
paul@44 | 1641 | |
paul@44 | 1642 | # Record the referenced objects. |
paul@44 | 1643 | |
paul@44 | 1644 | self.referenced_attrs[location] = \ |
paul@44 | 1645 | self._identify_reference_attribute(attrname, class_types, instance_types, module_types) |
paul@44 | 1646 | |
paul@44 | 1647 | if constrained: |
paul@44 | 1648 | self.access_constrained.add(location) |
paul@44 | 1649 | |
paul@44 | 1650 | def _identify_reference_attribute(self, attrname, class_types, instance_types, module_types): |
paul@44 | 1651 | |
paul@44 | 1652 | """ |
paul@44 | 1653 | Identify the reference attribute with the given 'attrname', employing |
paul@44 | 1654 | the given 'class_types', 'instance_types' and 'module_types'. |
paul@44 | 1655 | """ |
paul@44 | 1656 | |
paul@44 | 1657 | attrs = set() |
paul@44 | 1658 | |
paul@44 | 1659 | # The class types expose class attributes either directly or via |
paul@44 | 1660 | # instances. |
paul@44 | 1661 | |
paul@44 | 1662 | for object_type in class_types: |
paul@44 | 1663 | ref = self.importer.get_class_attribute(object_type, attrname) |
paul@44 | 1664 | if ref: |
paul@44 | 1665 | attrs.add(("<class>", object_type, ref)) |
paul@44 | 1666 | |
paul@44 | 1667 | # Add any distinct instance attributes that would be provided |
paul@44 | 1668 | # by instances also providing indirect class attribute access. |
paul@44 | 1669 | |
paul@44 | 1670 | for ref in self.importer.get_instance_attributes(object_type, attrname): |
paul@44 | 1671 | attrs.add(("<instance>", object_type, ref)) |
paul@44 | 1672 | |
paul@44 | 1673 | # The instance-only types expose instance attributes, but although |
paul@44 | 1674 | # classes are excluded as potential accessors (since they do not provide |
paul@44 | 1675 | # the instance attributes), the class types may still provide some |
paul@44 | 1676 | # attributes. |
paul@44 | 1677 | |
paul@44 | 1678 | for object_type in instance_types: |
paul@44 | 1679 | instance_attrs = self.importer.get_instance_attributes(object_type, attrname) |
paul@44 | 1680 | |
paul@44 | 1681 | if instance_attrs: |
paul@44 | 1682 | for ref in instance_attrs: |
paul@44 | 1683 | attrs.add(("<instance>", object_type, ref)) |
paul@44 | 1684 | else: |
paul@44 | 1685 | ref = self.importer.get_class_attribute(object_type, attrname) |
paul@44 | 1686 | if ref: |
paul@44 | 1687 | attrs.add(("<class>", object_type, ref)) |
paul@44 | 1688 | |
paul@44 | 1689 | # Module types expose module attributes for module accessors. |
paul@44 | 1690 | |
paul@44 | 1691 | for object_type in module_types: |
paul@44 | 1692 | ref = self.importer.get_module_attribute(object_type, attrname) |
paul@44 | 1693 | if ref: |
paul@44 | 1694 | attrs.add(("<module>", object_type, ref)) |
paul@44 | 1695 | |
paul@44 | 1696 | return attrs |
paul@44 | 1697 | |
paul@44 | 1698 | # vim: tabstop=4 expandtab shiftwidth=4 |