-
Notifications
You must be signed in to change notification settings - Fork 0
/
deducer.py
2969 lines (2035 loc) · 110 KB
/
deducer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
Deduce types for usage observations.
Copyright (C) 2014, 2015, 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
from common import first, get_assigned_attributes, get_attrnames, \
get_invoked_attributes, get_name_path, init_item, \
order_dependencies_partial, sorted_output, \
AccessLocation, CommonOutput, Location
from encoders import encode_access_location, encode_alias_location, \
encode_constrained, encode_instruction, encode_location, \
encode_usage, get_kinds, \
test_label_for_kind, test_label_for_type
from errors import DeduceError
from os.path import join
from referencing import combine_types, is_single_class_type, separate_types, \
Reference
class Deducer(CommonOutput):
"Deduce types in a program."
root_class_type = "__builtins__.object"
def __init__(self, importer, output):
"""
Initialise an instance using the given 'importer' that will perform
deductions on the program information, writing the results to the given
'output' directory.
"""
self.importer = importer
self.output = output
# Descendants of classes.
self.descendants = {}
self.init_descendants()
self.init_special_attributes()
# Map locations to usage in order to determine specific types.
self.location_index = {}
# Map access locations to definition locations.
self.access_index = {}
# Map definition locations to affected accesses.
self.access_index_rev = {}
# Map aliases to accesses that define them.
self.alias_index = {}
# Map accesses to aliases whose initial values are influenced by them.
self.alias_index_rev = {}
# Map definitions and accesses to initialised names.
self.initialised_names = {}
self.initialised_accesses = {}
# Map constant accesses to redefined accesses.
self.const_accesses = {}
self.const_accesses_rev = {}
# Map usage observations to assigned attributes.
self.assigned_attrs = {}
# Map usage observations to objects.
self.attr_class_types = {}
self.attr_instance_types = {}
self.attr_module_types = {}
# All known attribute names.
self.all_attrnames = set()
# Modified attributes from usage observations.
self.modified_attributes = {}
# Accesses that are assignments or invocations.
self.reference_assignments = set()
self.reference_invocations = {}
self.reference_invocations_unsuitable = {}
# Map locations to types, constrained indicators and attributes.
self.accessor_class_types = {}
self.accessor_instance_types = {}
self.accessor_module_types = {}
self.provider_class_types = {}
self.provider_instance_types = {}
self.provider_module_types = {}
self.accessor_constrained = set()
self.access_constrained = set()
self.referenced_attrs = {}
self.referenced_objects = {}
# Details of access operations.
self.access_plans = {}
# Specific attribute access information.
self.access_instructions = {}
self.accessor_kinds = {}
# Accumulated information about accessors and providers.
self.accessor_general_class_types = {}
self.accessor_general_instance_types = {}
self.accessor_general_module_types = {}
self.accessor_all_types = {}
self.accessor_all_general_types = {}
self.provider_all_types = {}
self.accessor_guard_tests = {}
# Accumulated information about accessed attributes and
# access/attribute-specific accessor tests.
self.reference_all_attrs = {}
self.reference_all_providers = {}
self.reference_all_provider_kinds = {}
self.reference_all_accessor_types = {}
self.reference_all_accessor_general_types = {}
self.reference_test_types = {}
self.reference_test_accessor_type = {}
# The processing workflow itself.
self.init_usage_index()
self.init_attr_type_indexes()
self.init_combined_attribute_index()
self.init_accessors()
self.init_accesses()
self.init_aliases()
self.modify_mutated_attributes()
self.identify_references()
self.classify_accessors()
self.classify_accesses()
self.initialise_access_plans()
self.initialise_access_instructions()
self.identify_dependencies()
def to_output(self):
"Write the output files using deduction information."
self.check_output()
self.write_mutations()
self.write_accessors()
self.write_accesses()
self.write_access_plans()
def write_mutations(self):
"""
Write mutation-related output in the following format:
qualified name " " original object type
Object type can be "<class>", "<function>" or "<var>".
"""
f = open(join(self.output, "mutations"), "w")
try:
attrs = self.modified_attributes.items()
attrs.sort()
for attr, value in attrs:
print >>f, attr, value
finally:
f.close()
def write_accessors(self):
"""
Write reference-related output in the following format for types:
location " " ( "constrained" | "deduced" ) " " attribute type " " most general type names " " number of specific types
Note that multiple lines can be given for each location, one for each
attribute type.
Locations have the following format:
qualified name of scope ":" local name ":" name version
The attribute type can be "<class>", "<instance>", "<module>" or "<>",
where the latter indicates an absence of suitable references.
Type names indicate the type providing the attributes, being either a
class or module qualified name.
----
A summary of accessor types is formatted as follows:
location " " ( "constrained" | "deduced" ) " " ( "specific" | "common" | "unguarded" ) " " most general type names " " number of specific types
This summary groups all attribute types (class, instance, module) into a
single line in order to determine the complexity of identifying an
accessor.
----
References that cannot be supported by any types are written to a
warnings file in the following format:
location
----
For each location where a guard would be asserted to guarantee the
nature of an object, the following format is employed:
location " " ( "specific" | "common" ) " " object kind " " object types
Object kind can be "<class>", "<instance>" or "<module>".
----
A summary of aliases is formatted as follows:
location " " locations
"""
f_type_summary = open(join(self.output, "type_summary"), "w")
f_types = open(join(self.output, "types"), "w")
f_warnings = open(join(self.output, "type_warnings"), "w")
f_guards = open(join(self.output, "guards"), "w")
f_aliases = open(join(self.output, "aliases"), "w")
try:
locations = self.accessor_class_types.keys()
locations.sort()
for location in locations:
constrained = location in self.accessor_constrained
# Accessor information.
class_types = self.accessor_class_types[location]
instance_types = self.accessor_instance_types[location]
module_types = self.accessor_module_types[location]
general_class_types = self.accessor_general_class_types[location]
general_instance_types = self.accessor_general_instance_types[location]
general_module_types = self.accessor_general_module_types[location]
all_types = self.accessor_all_types[location]
all_general_types = self.accessor_all_general_types[location]
if class_types:
print >>f_types, encode_location(location), encode_constrained(constrained), "<class>", \
sorted_output(general_class_types), len(class_types)
if instance_types:
print >>f_types, encode_location(location), encode_constrained(constrained), "<instance>", \
sorted_output(general_instance_types), len(instance_types)
if module_types:
print >>f_types, encode_location(location), encode_constrained(constrained), "<module>", \
sorted_output(general_module_types), len(module_types)
if not all_types:
print >>f_types, encode_location(location), "deduced", "<>", 0
attrnames = list(self.location_index[location])
attrnames.sort()
print >>f_warnings, encode_location(location), "; ".join(map(encode_usage, attrnames))
guard_test = self.accessor_guard_tests.get(location)
if guard_test:
guard_test_type, guard_test_arg = guard_test
# Write specific type guard details.
if guard_test and guard_test_type == "specific":
print >>f_guards, encode_location(location), "-".join(guard_test), \
first(get_kinds(all_types)), \
sorted_output(all_types)
# Write common type guard details.
elif guard_test and guard_test_type == "common":
print >>f_guards, encode_location(location), "-".join(guard_test), \
first(get_kinds(all_general_types)), \
sorted_output(all_general_types)
print >>f_type_summary, encode_location(location), encode_constrained(constrained), \
guard_test and "-".join(guard_test) or "unguarded", sorted_output(all_general_types), len(all_types)
# Aliases are visited separately from accessors, even though they
# are often the same thing.
locations = self.alias_index.keys()
locations.sort()
for location in locations:
accesses = []
for access_location in self.alias_index[location]:
invocation = access_location in self.reference_invocations
accesses.append(encode_alias_location(access_location, invocation))
invocation = location in self.reference_invocations
print >>f_aliases, encode_alias_location(location, invocation), ", ".join(accesses)
finally:
f_type_summary.close()
f_types.close()
f_warnings.close()
f_guards.close()
f_aliases.close()
def write_accesses(self):
"""
Specific attribute output is produced in the following format:
location " " ( "constrained" | "deduced" ) " " attribute type " " attribute references
Note that multiple lines can be given for each location and attribute
name, one for each attribute type.
Locations have the following format:
qualified name of scope ":" local name ":" attribute name ":" access number
The attribute type can be "<class>", "<instance>", "<module>" or "<>",
where the latter indicates an absence of suitable references.
Attribute references have the following format:
object type ":" qualified name
Object type can be "<class>", "<function>" or "<var>".
----
A summary of attributes is formatted as follows:
location " " attribute name " " ( "constrained" | "deduced" ) " " test " " attribute references
This summary groups all attribute types (class, instance, module) into a
single line in order to determine the complexity of each access.
Tests can be "validate", "specific", "untested", "guarded-validate" or "guarded-specific".
----
For each access where a test would be asserted to guarantee the
nature of an attribute, the following formats are employed:
location " " attribute name " " "validate"
location " " attribute name " " "specific" " " attribute type " " object type
----
References that cannot be supported by any types are written to a
warnings file in the following format:
location
"""
f_attr_summary = open(join(self.output, "attribute_summary"), "w")
f_attrs = open(join(self.output, "attributes"), "w")
f_tests = open(join(self.output, "tests"), "w")
f_warnings = open(join(self.output, "attribute_warnings"), "w")
f_unsuitable = open(join(self.output, "invocation_warnings"), "w")
try:
locations = self.referenced_attrs.keys()
locations.sort()
for location in locations:
constrained = location in self.access_constrained
# Attribute information, both name-based and anonymous.
referenced_attrs = self.referenced_attrs[location]
if referenced_attrs:
attrname = location.get_attrname()
all_accessed_attrs = list(set(self.reference_all_attrs[location]))
all_accessed_attrs.sort()
for attrtype, attrs in self.get_referenced_attrs(location):
print >>f_attrs, encode_access_location(location), encode_constrained(constrained), attrtype, sorted_output(attrs)
test_type = self.reference_test_types.get(location)
# Write the need to test at run time.
if test_type[0] == "validate":
print >>f_tests, encode_access_location(location), "-".join(test_type)
# Write any type checks for anonymous accesses.
elif test_type and self.reference_test_accessor_type.get(location):
print >>f_tests, encode_access_location(location), "-".join(test_type), \
sorted_output(all_accessed_attrs), \
self.reference_test_accessor_type[location]
print >>f_attr_summary, encode_access_location(location), encode_constrained(constrained), \
test_type and "-".join(test_type) or "untested", sorted_output(all_accessed_attrs)
# Write details of potentially unsuitable invocation
# occurrences.
unsuitable = self.reference_invocations_unsuitable.get(location)
if unsuitable:
unsuitable = map(str, unsuitable)
unsuitable.sort()
print >>f_unsuitable, encode_access_location(location), ", ".join(unsuitable)
else:
print >>f_warnings, encode_access_location(location)
finally:
f_attr_summary.close()
f_attrs.close()
f_tests.close()
f_warnings.close()
f_unsuitable.close()
def write_access_plans(self):
"""
Write access and instruction plans.
Each attribute access is written out as a plan of the following form:
location " " name " " test " " test type " " base " " traversed attributes
" " traversal access modes " " attributes to traverse
" " context " " context test " " first access method
" " final access method " " static attribute " " accessor kinds
Locations have the following format:
qualified name of scope "." local name ":" name version
Traversal access modes are either "class" (obtain accessor class to
access attribute) or "object" (obtain attribute directly from accessor).
"""
f_attrs = open(join(self.output, "attribute_plans"), "w")
try:
locations = self.access_plans.keys()
locations.sort()
for location in locations:
name, test, test_type, base, \
traversed, traversal_modes, attrnames, \
context, context_test, \
first_method, final_method, \
attr, accessor_kinds = self.access_plans[location]
print >>f_attrs, encode_access_location(location), \
name or "{}", \
test and "-".join(test) or "{}", \
test_type or "{}", \
base or "{}", \
".".join(traversed) or "{}", \
".".join(traversal_modes) or "{}", \
".".join(attrnames) or "{}", \
context, context_test, \
first_method, final_method, attr or "{}", \
",".join(accessor_kinds)
finally:
f_attrs.close()
f = open(join(self.output, "instruction_plans"), "w")
try:
access_instructions = self.access_instructions.items()
access_instructions.sort()
for location, instructions in access_instructions:
print >>f, encode_access_location(location), "..."
for instruction in instructions:
print >>f, encode_instruction(instruction)
print >>f
finally:
f.close()
def classify_accessors(self):
"For each program location, classify accessors."
# Where instance and module types are defined, class types are also
# defined. See: init_definition_details
locations = self.accessor_class_types.keys()
for location in locations:
constrained = location in self.accessor_constrained
# Provider information.
class_types = self.provider_class_types[location]
instance_types = self.provider_instance_types[location]
module_types = self.provider_module_types[location]
# Collect specific and general type information.
self.provider_all_types[location] = \
combine_types(class_types, instance_types, module_types)
# Accessor information.
class_types = self.accessor_class_types[location]
self.accessor_general_class_types[location] = \
general_class_types = self.get_most_general_class_types(class_types)
instance_types = self.accessor_instance_types[location]
self.accessor_general_instance_types[location] = \
general_instance_types = self.get_most_general_class_types(instance_types)
module_types = self.accessor_module_types[location]
self.accessor_general_module_types[location] = \
general_module_types = self.get_most_general_module_types(module_types)
# Collect specific and general type information.
self.accessor_all_types[location] = all_types = \
combine_types(class_types, instance_types, module_types)
self.accessor_all_general_types[location] = all_general_types = \
combine_types(general_class_types, general_instance_types, general_module_types)
# Record guard information but only for accessors employed by
# accesses. There are no attribute accesses to guard, otherwise.
if not constrained and self.access_index_rev.get(location):
# Test for self parameters in methods that could not be
# constrained, potentially due to usage unsupported by the class
# defining the method (such as in mix-in classes).
if location.name != "self" or self.in_method(location.path):
self.record_guard(location, all_types, all_general_types)
def record_guard(self, location, all_types, all_general_types):
"""
For the accessor 'location', record a guard if 'all_types' or
'all_general_types' are appropriate. Where no guard is recorded, access
tests will need to be applied.
"""
# Record specific type guard details.
if len(all_types) == 1:
self.accessor_guard_tests[location] = ("specific", test_label_for_type(first(all_types)))
elif is_single_class_type(all_types):
self.accessor_guard_tests[location] = ("specific", "object")
# Record common type guard details.
elif len(all_general_types) == 1:
self.accessor_guard_tests[location] = ("common", test_label_for_type(first(all_types)))
elif is_single_class_type(all_general_types):
self.accessor_guard_tests[location] = ("common", "object")
# Otherwise, no convenient guard can be defined.
def classify_accesses(self):
"For each program location, classify accesses."
# Attribute accesses use potentially different locations to those of
# accessors.
locations = self.referenced_attrs.keys()
for location in locations:
constrained = location in self.access_constrained
# Combine type information from all accessors supplying the access.
accessor_locations = self.get_accessors_for_access(location)
all_provider_types = set()
all_accessor_types = set()
all_accessor_general_types = set()
for accessor_location in accessor_locations:
# Obtain the provider types for guard-related attribute access
# checks.
all_provider_types.update(self.provider_all_types.get(accessor_location))
# Obtain the accessor guard types (specific and general).
all_accessor_types.update(self.accessor_all_types.get(accessor_location))
all_accessor_general_types.update(self.accessor_all_general_types.get(accessor_location))
# Obtain basic properties of the types involved in the access.
single_accessor_type = len(all_accessor_types) == 1
single_accessor_class_type = is_single_class_type(all_accessor_types)
single_accessor_general_type = len(all_accessor_general_types) == 1
single_accessor_general_class_type = is_single_class_type(all_accessor_general_types)
# Determine whether the attribute access is guarded or not.
guarded = (
single_accessor_type or single_accessor_class_type or
single_accessor_general_type or single_accessor_general_class_type
)
if guarded:
(guard_class_types, guard_instance_types, guard_module_types,
_function_types, _var_types) = separate_types(all_provider_types)
self.reference_all_accessor_types[location] = all_accessor_types
self.reference_all_accessor_general_types[location] = all_accessor_general_types
# Attribute information, both name-based and anonymous.
referenced_attrs = self.referenced_attrs[location]
if not referenced_attrs:
raise DeduceError("In %s, access via %s to attribute %s (occurrence %d) cannot be identified." %
(location.path, location.name, location.get_attrname(), location.access_number))
# Record attribute information for each name used on the
# accessor.
attrname = location.get_attrname()
self.reference_all_attrs[location] = all_accessed_attrs = set()
self.reference_all_providers[location] = all_providers = set()
self.reference_all_provider_kinds[location] = all_provider_kinds = set()
# Obtain provider and attribute details for this kind of
# object.
for attrtype, object_type, attr in referenced_attrs:
all_accessed_attrs.add(attr)
all_providers.add(object_type)
all_provider_kinds.add(attrtype)
# Obtain reference and provider information as sets for the
# operations below, retaining the list forms for use with
# instruction plan preparation.
all_general_providers = self.get_most_general_types(all_providers)
# Determine which attributes would be provided by the
# accessor types upheld by a guard.
if guarded:
guard_attrs = set()
for _attrtype, object_type, attr in \
self._identify_reference_attribute(location, attrname, guard_class_types, guard_instance_types, guard_module_types):
guard_attrs.add(attr)
else:
guard_attrs = None
# Constrained accesses guarantee the nature of the accessor.
# However, there may still be many types involved.
if constrained:
if single_accessor_type:
self.reference_test_types[location] = ("constrained", "specific", test_label_for_type(first(all_accessor_types)))
elif single_accessor_class_type:
self.reference_test_types[location] = ("constrained", "specific", "object")
elif single_accessor_general_type:
self.reference_test_types[location] = ("constrained", "common", test_label_for_type(first(all_accessor_general_types)))
elif single_accessor_general_class_type:
self.reference_test_types[location] = ("constrained", "common", "object")
else:
self.reference_test_types[location] = ("constrained", "many")
# Suitably guarded accesses, where the nature of the
# accessor can be guaranteed, do not require the attribute
# involved to be validated. Otherwise, for unguarded
# accesses, access-level tests are required.
elif guarded and all_accessed_attrs.issubset(guard_attrs):
if single_accessor_type:
self.reference_test_types[location] = ("guarded", "specific", test_label_for_type(first(all_accessor_types)))
elif single_accessor_class_type:
self.reference_test_types[location] = ("guarded", "specific", "object")
elif single_accessor_general_type:
self.reference_test_types[location] = ("guarded", "common", test_label_for_type(first(all_accessor_general_types)))
elif single_accessor_general_class_type:
self.reference_test_types[location] = ("guarded", "common", "object")
# Record the need to test the type of anonymous and
# unconstrained accessors.
elif len(all_providers) == 1:
provider = first(all_providers)
if provider != self.root_class_type:
all_accessor_kinds = set(get_kinds(all_accessor_types))
if len(all_accessor_kinds) == 1:
test_type = ("test", "specific", test_label_for_kind(first(all_accessor_kinds)))
else:
test_type = ("test", "specific", "object")
self.reference_test_types[location] = test_type
self.reference_test_accessor_type[location] = provider
elif len(all_general_providers) == 1:
provider = first(all_general_providers)
if provider != self.root_class_type:
all_accessor_kinds = set(get_kinds(all_accessor_general_types))
if len(all_accessor_kinds) == 1:
test_type = ("test", "common", test_label_for_kind(first(all_accessor_kinds)))
else:
test_type = ("test", "common", "object")
self.reference_test_types[location] = test_type
self.reference_test_accessor_type[location] = provider
# Record the need to test the identity of the attribute.
else:
self.reference_test_types[location] = ("validate",)
def initialise_access_plans(self):
"Define attribute access plans."
for location in self.referenced_attrs.keys():
original_location = self.const_accesses_rev.get(location)
self.access_plans[original_location or location] = self.get_access_plan(location)
def identify_dependencies(self):
"Introduce more module dependencies to the importer."
for location, referenced_attrs in self.referenced_attrs.items():
# Identify references providing dependencies.
for attrtype, objtype, attr in referenced_attrs:
self.importer.add_dependency(location.path, attr.get_origin())
def get_referenced_attrs(self, location):
"""
Return attributes referenced at the given access 'location' by the given
'attrname' as a list of (attribute type, attribute set) tuples.
"""
d = {}
for attrtype, objtype, attr in self.referenced_attrs[location]:
init_item(d, attrtype, set)
d[attrtype].add(attr.unaliased())
l = d.items()
l.sort() # class, module, instance
return l
# Initialisation methods.
def init_descendants(self):
"Identify descendants of each class."
for name in self.importer.classes.keys():
self.get_descendants_for_class(name)
def get_descendants_for_class(self, name):
"""
Use subclass information to deduce the descendants for the class of the
given 'name'.
"""
if not self.descendants.has_key(name):
descendants = set()
for subclass in self.importer.subclasses[name]:
descendants.update(self.get_descendants_for_class(subclass))
descendants.add(subclass)
self.descendants[name] = descendants
return self.descendants[name]
def init_special_attributes(self):
"Add special attributes to the classes for inheritance-related tests."
all_class_attrs = self.importer.all_class_attrs
for name, descendants in self.descendants.items():
for descendant in descendants:
all_class_attrs[descendant]["#%s" % name] = name
for name in all_class_attrs.keys():
all_class_attrs[name]["#%s" % name] = name
def init_usage_index(self):
"""
Create indexes for module and function attribute usage and for anonymous
accesses.
"""
for module in self.importer.get_modules():
for path, assignments in module.attr_usage.items():
self.add_usage(assignments, path)
for path, all_attrnames in self.importer.all_attr_accesses.items():
for attrnames in all_attrnames:
attrname = get_attrnames(attrnames)[-1]
access_location = AccessLocation(path, None, attrnames, 0)
self.add_usage_term(access_location, ((attrname, False, False),))
def add_usage(self, assignments, path):
"""
Collect usage from the given 'assignments', adding 'path' details to
each record if specified. Add the usage to an index mapping to location
information, as well as to an index mapping locations to usages.
"""
for name, versions in assignments.items():
for i, usages in enumerate(versions):
location = Location(path, name, None, i)
for usage in usages:
self.add_usage_term(location, usage)
def add_usage_term(self, location, usage):
"""
For 'location' and using 'usage' as a description of usage, record
in the usage index a mapping from the usage to 'location', and record in
the location index a mapping from 'location' to the usage.
"""
init_item(self.location_index, location, set)
self.location_index[location].add(usage)
def init_accessors(self):
"Create indexes for module and function accessor information."
for module in self.importer.get_modules():
for path, all_accesses in module.attr_accessors.items():
self.add_accessors(all_accesses, path)
def add_accessors(self, all_accesses, path):
"""
For attribute accesses described by the mapping of 'all_accesses' from
name details to accessor details, record the locations of the accessors
for each access.
"""
# Get details for each access combining the given name and attribute.
for (name, attrnames), accesses in all_accesses.items():
# Obtain the usage details using the access information.
for access_number, versions in enumerate(accesses):
access_location = AccessLocation(path, name, attrnames, access_number)
locations = []
for version in versions:
location = Location(path, name, None, version)
locations.append(location)
# Map accessors to affected accesses.
l = init_item(self.access_index_rev, location, set)
l.add(access_location)
# Map accesses to supplying accessors.
self.access_index[access_location] = locations
def get_accessors_for_access(self, access_location):
"Find a definition providing accessor details, if necessary."
try:
return self.access_index[access_location]
except KeyError:
return [access_location]
def init_accesses(self):
"""
Check that attributes used in accesses are actually defined on some
object. This can be overlooked if unknown attributes are employed in
attribute chains.
Initialise collections for accesses involving assignments.
"""
# For each scope, obtain access details.
for path, all_accesses in self.importer.all_attr_access_modifiers.items():
# For each combination of name and attribute names, obtain
# applicable modifiers.
for (name, attrname_str), modifiers in all_accesses.items():
# For each access, determine the name versions affected by
# assignments.
for access_number, (assignment, invocation) in enumerate(modifiers):
if name:
access_location = AccessLocation(path, name, attrname_str, access_number)
else:
access_location = AccessLocation(path, None, attrname_str, 0)
# Plain name accesses do not employ attributes and are
# ignored. Whether they are invoked is of interest, however.
if not attrname_str:
if invocation:
self.reference_invocations[access_location] = invocation
continue
attrnames = get_attrnames(attrname_str)
# Check the attribute names.
for attrname in attrnames:
if not attrname in self.all_attrnames:
raise DeduceError("In %s, attribute %s is not defined in the program." % (path, attrname))
# Now only process assignments and invocations.
if invocation:
self.reference_invocations[access_location] = invocation
continue
elif not assignment:
continue
# Associate assignments with usage.
self.reference_assignments.add(access_location)
# Assignment occurs for the only attribute.
if len(attrnames) == 1:
accessor_locations = self.get_accessors_for_access(access_location)
for location in accessor_locations:
for usage in self.location_index[location]:
init_item(self.assigned_attrs, usage, set)
self.assigned_attrs[usage].add((path, name, attrnames[0]))
# Assignment occurs for the final attribute.
else:
usage = ((attrnames[-1], False, False),)
init_item(self.assigned_attrs, usage, set)
self.assigned_attrs[usage].add((path, name, attrnames[-1]))
def init_aliases(self):
"Expand aliases so that alias-based accesses can be resolved."
# Get aliased names with details of their accesses.
for (path, name), all_aliases in self.importer.all_aliased_names.items():
# For each version of the name, obtain the access locations.