-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
inheritance_manager.dart
1257 lines (1207 loc) · 48.4 KB
/
inheritance_manager.dart
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
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:collection';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/exception/exception.dart';
import 'package:analyzer/src/dart/ast/token.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/member.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/type_system.dart';
import 'package:analyzer/src/generated/utilities_dart.dart';
/**
* Instances of the class `InheritanceManager` manage the knowledge of where class members
* (methods, getters & setters) are inherited from.
*/
class InheritanceManager {
/**
* The [LibraryElement] that is managed by this manager.
*/
LibraryElement _library;
/**
* A flag indicating whether abstract methods should be included when looking
* up the superclass chain.
*/
bool _includeAbstractFromSuperclasses;
/**
* This is a mapping between each [ClassElement] and a map between the [String] member
* names and the associated [ExecutableElement] in the mixin and superclass chain.
*/
Map<ClassElement, Map<String, ExecutableElement>> _classLookup;
/**
* This is a mapping between each [ClassElement] and a map between the [String] member
* names and the associated [ExecutableElement] in the interface set.
*/
Map<ClassElement, Map<String, ExecutableElement>> _interfaceLookup;
/**
* A map between each visited [ClassElement] and the set of [AnalysisError]s found on
* the class element.
*/
Map<ClassElement, Set<AnalysisError>> _errorsInClassElement =
new HashMap<ClassElement, Set<AnalysisError>>();
/**
* Indicates whether errors should be ignored.
*
* When this bool is `true`, we skip the logic that figures out which error
* to report; this avoids a crash when the inheritance manager is used in the
* context of summary linking (where there is not enough information available
* to determine error locations).
*/
final bool ignoreErrors;
/**
* Initialize a newly created inheritance manager.
*
* @param library the library element context that the inheritance mappings are being generated
*/
InheritanceManager(LibraryElement library,
{bool includeAbstractFromSuperclasses: false, this.ignoreErrors: false}) {
this._library = library;
_includeAbstractFromSuperclasses = includeAbstractFromSuperclasses;
_classLookup = new HashMap<ClassElement, Map<String, ExecutableElement>>();
_interfaceLookup =
new HashMap<ClassElement, Map<String, ExecutableElement>>();
}
/**
* Set the new library element context.
*
* @param library the new library element
*/
void set libraryElement(LibraryElement library) {
this._library = library;
}
/**
* Return the set of [AnalysisError]s found on the passed [ClassElement], or
* `null` if there are none.
*
* @param classElt the class element to query
* @return the set of [AnalysisError]s found on the passed [ClassElement], or
* `null` if there are none
*/
Set<AnalysisError> getErrors(ClassElement classElt) =>
_errorsInClassElement[classElt];
/**
* Get and return a mapping between the set of all string names of the members inherited from the
* passed [ClassElement] superclass hierarchy, and the associated [ExecutableElement].
*
* @param classElt the class element to query
* @return a mapping between the set of all members inherited from the passed [ClassElement]
* superclass hierarchy, and the associated [ExecutableElement]
*/
@deprecated
MemberMap getMapOfMembersInheritedFromClasses(ClassElement classElt) =>
new MemberMap.fromMap(
_computeClassChainLookupMap(classElt, new HashSet<ClassElement>()));
/**
* Get and return a mapping between the set of all string names of the members inherited from the
* passed [ClassElement] interface hierarchy, and the associated [ExecutableElement].
*
* @param classElt the class element to query
* @return a mapping between the set of all string names of the members inherited from the passed
* [ClassElement] interface hierarchy, and the associated [ExecutableElement].
*/
@deprecated
MemberMap getMapOfMembersInheritedFromInterfaces(ClassElement classElt) =>
new MemberMap.fromMap(
_computeInterfaceLookupMap(classElt, new HashSet<ClassElement>()));
/**
* Return a table mapping the string names of the members inherited from the
* passed [ClassElement]'s superclass hierarchy, and the associated executable
* element.
*/
Map<String, ExecutableElement> getMembersInheritedFromClasses(
ClassElement classElt) =>
_computeClassChainLookupMap(classElt, new HashSet<ClassElement>());
/**
* Return a table mapping the string names of the members inherited from the
* passed [ClassElement]'s interface hierarchy, and the associated executable
* element.
*/
Map<String, ExecutableElement> getMembersInheritedFromInterfaces(
ClassElement classElt) =>
_computeInterfaceLookupMap(classElt, new HashSet<ClassElement>());
/**
* Given some [ClassElement] and some member name, this returns the
* [ExecutableElement] that the class inherits from the mixins,
* superclasses or interfaces, that has the member name, if no member is inherited `null` is
* returned.
*
* @param classElt the class element to query
* @param memberName the name of the executable element to find and return
* @return the inherited executable element with the member name, or `null` if no such
* member exists
*/
ExecutableElement lookupInheritance(
ClassElement classElt, String memberName) {
if (memberName == null || memberName.isEmpty) {
return null;
}
ExecutableElement executable = _computeClassChainLookupMap(
classElt, new HashSet<ClassElement>())[memberName];
if (executable == null) {
return _computeInterfaceLookupMap(
classElt, new HashSet<ClassElement>())[memberName];
}
return executable;
}
/**
* Given some [ClassElement] and some member name, this returns the
* [ExecutableElement] that the class either declares itself, or
* inherits, that has the member name, if no member is inherited `null` is returned.
*
* @param classElt the class element to query
* @param memberName the name of the executable element to find and return
* @return the inherited executable element with the member name, or `null` if no such
* member exists
*/
ExecutableElement lookupMember(ClassElement classElt, String memberName) {
ExecutableElement element = _lookupMemberInClass(classElt, memberName);
if (element != null) {
return element;
}
return lookupInheritance(classElt, memberName);
}
/**
* Determine the set of methods which is overridden by the given class member. If no member is
* inherited, an empty list is returned. If one of the inherited members is a
* [MultiplyInheritedExecutableElement], then it is expanded into its constituent inherited
* elements.
*
* @param classElt the class to query
* @param memberName the name of the class member to query
* @return a list of overridden methods
*/
List<ExecutableElement> lookupOverrides(
ClassElement classElt, String memberName) {
List<ExecutableElement> result = new List<ExecutableElement>();
if (memberName == null || memberName.isEmpty) {
return result;
}
List<Map<String, ExecutableElement>> interfaceMaps =
_gatherInterfaceLookupMaps(classElt, new HashSet<ClassElement>());
if (interfaceMaps != null) {
for (Map<String, ExecutableElement> interfaceMap in interfaceMaps) {
ExecutableElement overriddenElement = interfaceMap[memberName];
if (overriddenElement != null) {
if (overriddenElement is MultiplyInheritedExecutableElement) {
for (ExecutableElement element
in overriddenElement.inheritedElements) {
result.add(element);
}
} else {
result.add(overriddenElement);
}
}
}
}
return result;
}
/**
* This method takes some inherited [FunctionType], and resolves all the parameterized types
* in the function type, dependent on the class in which it is being overridden.
*
* @param baseFunctionType the function type that is being overridden
* @param memberName the name of the member, this is used to lookup the inheritance path of the
* override
* @param definingType the type that is overriding the member
* @return the passed function type with any parameterized types substituted
*/
// TODO(jmesserly): investigate why this is needed in ErrorVerifier's override
// checking. There seems to be some rare cases where we get partially
// substituted type arguments, and the function types don't compare equally.
FunctionType substituteTypeArgumentsInMemberFromInheritance(
FunctionType baseFunctionType,
String memberName,
InterfaceType definingType) {
// if the baseFunctionType is null, or does not have any parameters,
// return it.
if (baseFunctionType == null ||
baseFunctionType.typeArguments.length == 0) {
return baseFunctionType;
}
// First, generate the path from the defining type to the overridden member
Queue<InterfaceType> inheritancePath = new Queue<InterfaceType>();
_computeInheritancePath(inheritancePath, definingType, memberName);
if (inheritancePath == null || inheritancePath.isEmpty) {
// TODO(jwren) log analysis engine error
return baseFunctionType;
}
FunctionType functionTypeToReturn = baseFunctionType;
// loop backward through the list substituting as we go:
while (!inheritancePath.isEmpty) {
InterfaceType lastType = inheritancePath.removeLast();
List<DartType> parameterTypes = lastType.element.type.typeArguments;
List<DartType> argumentTypes = lastType.typeArguments;
functionTypeToReturn =
functionTypeToReturn.substitute2(argumentTypes, parameterTypes);
}
return functionTypeToReturn;
}
/**
* Compute and return a mapping between the set of all string names of the members inherited from
* the passed [ClassElement] superclass hierarchy, and the associated
* [ExecutableElement].
*
* @param classElt the class element to query
* @param visitedClasses a set of visited classes passed back into this method when it calls
* itself recursively
* @return a mapping between the set of all string names of the members inherited from the passed
* [ClassElement] superclass hierarchy, and the associated [ExecutableElement]
*/
Map<String, ExecutableElement> _computeClassChainLookupMap(
ClassElement classElt, Set<ClassElement> visitedClasses) {
Map<String, ExecutableElement> resultMap = _classLookup[classElt];
if (resultMap != null) {
return resultMap;
} else {
resultMap = new Map<String, ExecutableElement>();
}
InterfaceType supertype = classElt.supertype;
if (supertype == null) {
// classElt is Object
_classLookup[classElt] = resultMap;
return resultMap;
}
ClassElement superclassElt = supertype.element;
if (superclassElt != null) {
if (!visitedClasses.contains(superclassElt)) {
visitedClasses.add(superclassElt);
try {
resultMap = new Map<String, ExecutableElement>.from(
_computeClassChainLookupMap(superclassElt, visitedClasses));
//
// Substitute the super types down the hierarchy.
//
_substituteTypeParametersDownHierarchy(supertype, resultMap);
//
// Include the members from the superclass in the resultMap.
//
_recordMapWithClassMembers(
resultMap, supertype, _includeAbstractFromSuperclasses);
} finally {
visitedClasses.remove(superclassElt);
}
} else {
// This case happens only when the superclass was previously visited and
// not in the lookup, meaning this is meant to shorten the compute for
// recursive cases.
_classLookup[superclassElt] = resultMap;
return resultMap;
}
}
//
// Include the members from the mixins in the resultMap. If there are
// multiple mixins, visit them in the order listed so that methods in later
// mixins will overwrite identically-named methods in earlier mixins.
//
List<InterfaceType> mixins = classElt.mixins;
for (InterfaceType mixin in mixins) {
ClassElement mixinElement = mixin.element;
if (mixinElement != null) {
if (!visitedClasses.contains(mixinElement)) {
visitedClasses.add(mixinElement);
try {
Map<String, ExecutableElement> map =
new Map<String, ExecutableElement>();
//
// Include the members from the mixin in the resultMap.
//
_recordMapWithClassMembers(
map, mixin, _includeAbstractFromSuperclasses);
//
// Add the members from map into result map.
//
for (String memberName in map.keys) {
ExecutableElement value = map[memberName];
ClassElement definingClass = value
.getAncestor((Element element) => element is ClassElement);
if (!definingClass.type.isObject) {
ExecutableElement existingValue = resultMap[memberName];
if (existingValue == null ||
(existingValue != null && !_isAbstract(value))) {
resultMap[memberName] = value;
}
}
}
} finally {
visitedClasses.remove(mixinElement);
}
} else {
// This case happens only when the superclass was previously visited
// and not in the lookup, meaning this is meant to shorten the compute
// for recursive cases.
_classLookup[mixinElement] = resultMap;
return resultMap;
}
}
}
_classLookup[classElt] = resultMap;
return resultMap;
}
/**
* Compute and return the inheritance path given the context of a type and a member that is
* overridden in the inheritance path (for which the type is in the path).
*
* @param chain the inheritance path that is built up as this method calls itself recursively,
* when this method is called an empty [LinkedList] should be provided
* @param currentType the current type in the inheritance path
* @param memberName the name of the member that is being looked up the inheritance path
*/
void _computeInheritancePath(Queue<InterfaceType> chain,
InterfaceType currentType, String memberName) {
// TODO (jwren) create a public version of this method which doesn't require
// the initial chain to be provided, then provided tests for this
// functionality in InheritanceManagerTest
chain.add(currentType);
ClassElement classElt = currentType.element;
InterfaceType supertype = classElt.supertype;
// Base case- reached Object
if (supertype == null) {
// Looked up the chain all the way to Object, return null.
// This should never happen.
return;
}
// If we are done, return the chain
// We are not done if this is the first recursive call on this method.
if (chain.length != 1) {
// We are done however if the member is in this classElt
if (_lookupMemberInClass(classElt, memberName) != null) {
return;
}
}
// Mixins- note that mixins call lookupMemberInClass, not lookupMember
List<InterfaceType> mixins = classElt.mixins;
for (int i = mixins.length - 1; i >= 0; i--) {
ClassElement mixinElement = mixins[i].element;
if (mixinElement != null) {
ExecutableElement elt = _lookupMemberInClass(mixinElement, memberName);
if (elt != null) {
// this is equivalent (but faster than) calling this method
// recursively
// (return computeInheritancePath(chain, mixins[i], memberName);)
chain.add(mixins[i]);
return;
}
}
}
// Superclass
ClassElement superclassElt = supertype.element;
if (lookupMember(superclassElt, memberName) != null) {
_computeInheritancePath(chain, supertype, memberName);
return;
}
// Interfaces
List<InterfaceType> interfaces = classElt.interfaces;
for (InterfaceType interfaceType in interfaces) {
ClassElement interfaceElement = interfaceType.element;
if (interfaceElement != null &&
lookupMember(interfaceElement, memberName) != null) {
_computeInheritancePath(chain, interfaceType, memberName);
return;
}
}
}
/**
* Compute and return a mapping between the set of all string names of the members inherited from
* the passed [ClassElement] interface hierarchy, and the associated
* [ExecutableElement].
*
* @param classElt the class element to query
* @param visitedInterfaces a set of visited classes passed back into this method when it calls
* itself recursively
* @return a mapping between the set of all string names of the members inherited from the passed
* [ClassElement] interface hierarchy, and the associated [ExecutableElement]
*/
Map<String, ExecutableElement> _computeInterfaceLookupMap(
ClassElement classElt, HashSet<ClassElement> visitedInterfaces) {
Map<String, ExecutableElement> resultMap = _interfaceLookup[classElt];
if (resultMap != null) {
return resultMap;
}
List<Map<String, ExecutableElement>> lookupMaps =
_gatherInterfaceLookupMaps(classElt, visitedInterfaces);
if (lookupMaps == null) {
resultMap = new Map<String, ExecutableElement>();
} else {
HashMap<String, List<ExecutableElement>> unionMap =
_unionInterfaceLookupMaps(lookupMaps);
resultMap = _resolveInheritanceLookup(classElt, unionMap);
}
_interfaceLookup[classElt] = resultMap;
return resultMap;
}
/**
* Collect a list of interface lookup maps whose elements correspond to all of the classes
* directly above [classElt] in the class hierarchy (the direct superclass if any, all
* mixins, and all direct superinterfaces). Each item in the list is the interface lookup map
* returned by [computeInterfaceLookupMap] for the corresponding super, except with type
* parameters appropriately substituted.
*
* @param classElt the class element to query
* @param visitedInterfaces a set of visited classes passed back into this method when it calls
* itself recursively
* @return `null` if there was a problem (such as a loop in the class hierarchy) or if there
* are no classes above this one in the class hierarchy. Otherwise, a list of interface
* lookup maps.
*/
List<Map<String, ExecutableElement>> _gatherInterfaceLookupMaps(
ClassElement classElt, HashSet<ClassElement> visitedInterfaces) {
InterfaceType supertype = classElt.supertype;
ClassElement superclassElement = supertype?.element;
List<InterfaceType> mixins = classElt.mixins;
List<InterfaceType> interfaces = classElt.interfaces;
// Recursively collect the list of mappings from all of the interface types
List<Map<String, ExecutableElement>> lookupMaps =
new List<Map<String, ExecutableElement>>();
//
// Superclass element
//
if (superclassElement != null) {
if (!visitedInterfaces.contains(superclassElement)) {
try {
visitedInterfaces.add(superclassElement);
//
// Recursively compute the map for the super type.
//
Map<String, ExecutableElement> map =
_computeInterfaceLookupMap(superclassElement, visitedInterfaces);
map = new Map<String, ExecutableElement>.from(map);
//
// Substitute the super type down the hierarchy.
//
_substituteTypeParametersDownHierarchy(supertype, map);
//
// Add any members from the super type into the map as well.
//
_recordMapWithClassMembers(map, supertype, true);
lookupMaps.add(map);
} finally {
visitedInterfaces.remove(superclassElement);
}
} else {
return null;
}
}
//
// Mixin elements
//
for (int i = mixins.length - 1; i >= 0; i--) {
InterfaceType mixinType = mixins[i];
ClassElement mixinElement = mixinType.element;
if (mixinElement != null) {
if (!visitedInterfaces.contains(mixinElement)) {
try {
visitedInterfaces.add(mixinElement);
//
// Recursively compute the map for the mixin.
//
Map<String, ExecutableElement> map =
_computeInterfaceLookupMap(mixinElement, visitedInterfaces);
map = new Map<String, ExecutableElement>.from(map);
//
// Substitute the mixin type down the hierarchy.
//
_substituteTypeParametersDownHierarchy(mixinType, map);
//
// Add any members from the mixin type into the map as well.
//
_recordMapWithClassMembers(map, mixinType, true);
lookupMaps.add(map);
} finally {
visitedInterfaces.remove(mixinElement);
}
} else {
return null;
}
}
}
//
// Interface elements
//
int interfaceLength = interfaces.length;
for (int i = 0; i < interfaceLength; i++) {
InterfaceType interfaceType = interfaces[i];
ClassElement interfaceElement = interfaceType.element;
if (interfaceElement != null) {
if (!visitedInterfaces.contains(interfaceElement)) {
try {
visitedInterfaces.add(interfaceElement);
//
// Recursively compute the map for the interfaces.
//
Map<String, ExecutableElement> map =
_computeInterfaceLookupMap(interfaceElement, visitedInterfaces);
map = new Map<String, ExecutableElement>.from(map);
//
// Substitute the supertypes down the hierarchy
//
_substituteTypeParametersDownHierarchy(interfaceType, map);
//
// And add any members from the interface into the map as well.
//
_recordMapWithClassMembers(map, interfaceType, true);
lookupMaps.add(map);
} finally {
visitedInterfaces.remove(interfaceElement);
}
} else {
return null;
}
}
}
if (lookupMaps.length == 0) {
return null;
}
return lookupMaps;
}
/**
* Given some [classElement], this method finds and returns the executable
* element with the given [memberName] in the class element. Static members,
* members in super types and members not accessible from the current library
* are not considered.
*/
ExecutableElement _lookupMemberInClass(
ClassElement classElement, String memberName) {
List<MethodElement> methods = classElement.methods;
int methodLength = methods.length;
for (int i = 0; i < methodLength; i++) {
MethodElement method = methods[i];
if (memberName == method.name &&
method.isAccessibleIn(_library) &&
!method.isStatic) {
return method;
}
}
List<PropertyAccessorElement> accessors = classElement.accessors;
int accessorLength = accessors.length;
for (int i = 0; i < accessorLength; i++) {
PropertyAccessorElement accessor = accessors[i];
if (memberName == accessor.name &&
accessor.isAccessibleIn(_library) &&
!accessor.isStatic) {
return accessor;
}
}
return null;
}
/**
* Record the passed map with the set of all members (methods, getters and setters) in the type
* into the passed map.
*
* @param map some non-`null` map to put the methods and accessors from the passed
* [ClassElement] into
* @param type the type that will be recorded into the passed map
* @param doIncludeAbstract `true` if abstract members will be put into the map
*/
void _recordMapWithClassMembers(Map<String, ExecutableElement> map,
InterfaceType type, bool doIncludeAbstract) {
Set<InterfaceType> seenTypes = new HashSet<InterfaceType>();
while (type.element.isMixinApplication) {
List<InterfaceType> mixins = type.mixins;
if (!seenTypes.add(type) || mixins.isEmpty) {
// In the case of a circularity in the type hierarchy, just don't add
// any members to the map.
return;
}
type = mixins.last;
}
List<MethodElement> methods = type.methods;
for (MethodElement method in methods) {
if (method.isAccessibleIn(_library) &&
!method.isStatic &&
(doIncludeAbstract || !method.isAbstract)) {
map[method.name] = method;
}
}
List<PropertyAccessorElement> accessors = type.accessors;
for (PropertyAccessorElement accessor in accessors) {
if (accessor.isAccessibleIn(_library) &&
!accessor.isStatic &&
(doIncludeAbstract || !accessor.isAbstract)) {
map[accessor.name] = accessor;
}
}
}
/**
* This method is used to report errors on when they are found computing inheritance information.
* See [ErrorVerifier.checkForInconsistentMethodInheritance] to see where these generated
* error codes are reported back into the analysis engine.
*
* @param classElt the location of the source for which the exception occurred
* @param offset the offset of the location of the error
* @param length the length of the location of the error
* @param errorCode the error code to be associated with this error
* @param arguments the arguments used to build the error message
*/
void _reportError(
ClassElement classElt, ErrorCode errorCode, List<Object> arguments) {
if (ignoreErrors) {
return;
}
HashSet<AnalysisError> errorSet = _errorsInClassElement.putIfAbsent(
classElt, () => new HashSet<AnalysisError>());
errorSet.add(new AnalysisError(classElt.source, classElt.nameOffset,
classElt.nameLength, errorCode, arguments));
}
/**
* Given the set of methods defined by classes above [classElt] in the class hierarchy,
* apply the appropriate inheritance rules to determine those methods inherited by or overridden
* by [classElt]. Also report static warnings
* [StaticTypeWarningCode.INCONSISTENT_METHOD_INHERITANCE] and
* [StaticWarningCode.INCONSISTENT_METHOD_INHERITANCE_GETTER_AND_METHOD] if appropriate.
*
* @param classElt the class element to query.
* @param unionMap a mapping from method name to the set of unique (in terms of signature) methods
* defined in superclasses of [classElt].
* @return the inheritance lookup map for [classElt].
*/
Map<String, ExecutableElement> _resolveInheritanceLookup(
ClassElement classElt, Map<String, List<ExecutableElement>> unionMap) {
Map<String, ExecutableElement> resultMap =
new Map<String, ExecutableElement>();
unionMap.forEach((String key, List<ExecutableElement> list) {
int numOfEltsWithMatchingNames = list.length;
if (numOfEltsWithMatchingNames == 1) {
//
// Example: class A inherits only 1 method named 'm'.
// Since it is the only such method, it is inherited.
// Another example: class A inherits 2 methods named 'm' from 2
// different interfaces, but they both have the same signature, so it is
// the method inherited.
//
resultMap[key] = list[0];
} else {
//
// Then numOfEltsWithMatchingNames > 1, check for the warning cases.
//
bool allMethods = true;
bool allSetters = true;
bool allGetters = true;
for (ExecutableElement executableElement in list) {
if (executableElement is PropertyAccessorElement) {
allMethods = false;
if (executableElement.isSetter) {
allGetters = false;
} else {
allSetters = false;
}
} else {
allGetters = false;
allSetters = false;
}
}
//
// If there isn't a mixture of methods with getters, then continue,
// otherwise create a warning.
//
if (allMethods || allGetters || allSetters) {
//
// Compute the element whose type is the subtype of all of the other
// types.
//
List<ExecutableElement> elements = new List.from(list);
List<FunctionType> executableElementTypes =
new List<FunctionType>(numOfEltsWithMatchingNames);
for (int i = 0; i < numOfEltsWithMatchingNames; i++) {
executableElementTypes[i] = elements[i].type;
}
List<int> subtypesOfAllOtherTypesIndexes = new List<int>();
for (int i = 0; i < numOfEltsWithMatchingNames; i++) {
FunctionType subtype = executableElementTypes[i];
if (subtype == null) {
continue;
}
bool subtypeOfAllTypes = true;
TypeSystem typeSystem = _library.context.typeSystem;
for (int j = 0;
j < numOfEltsWithMatchingNames && subtypeOfAllTypes;
j++) {
if (i != j) {
if (!typeSystem.isSubtypeOf(
subtype, executableElementTypes[j])) {
subtypeOfAllTypes = false;
break;
}
}
}
if (subtypeOfAllTypes) {
subtypesOfAllOtherTypesIndexes.add(i);
}
}
//
// The following is split into three cases determined by the number of
// elements in subtypesOfAllOtherTypes
//
if (subtypesOfAllOtherTypesIndexes.length == 1) {
//
// Example: class A inherited only 2 method named 'm'.
// One has the function type '() -> dynamic' and one has the
// function type '([int]) -> dynamic'. Since the second method is a
// subtype of all the others, it is the inherited method.
// Tests: InheritanceManagerTest.
// test_getMapOfMembersInheritedFromInterfaces_union_oneSubtype_*
//
resultMap[key] = elements[subtypesOfAllOtherTypesIndexes[0]];
} else {
if (subtypesOfAllOtherTypesIndexes.isEmpty) {
//
// Determine if the current class has a method or accessor with
// the member name, if it does then then this class does not
// "inherit" from any of the supertypes. See issue 16134.
//
bool classHasMember = false;
if (allMethods) {
classHasMember = classElt.getMethod(key) != null;
} else {
List<PropertyAccessorElement> accessors = classElt.accessors;
for (int i = 0; i < accessors.length; i++) {
if (accessors[i].name == key) {
classHasMember = true;
}
}
}
//
// Example: class A inherited only 2 method named 'm'.
// One has the function type '() -> int' and one has the function
// type '() -> String'. Since neither is a subtype of the other,
// we create a warning, and have this class inherit nothing.
//
if (!classHasMember) {
String firstTwoFunctionTypesStr =
"${executableElementTypes[0]}, ${executableElementTypes[1]}";
_reportError(
classElt,
StaticTypeWarningCode.INCONSISTENT_METHOD_INHERITANCE,
[key, firstTwoFunctionTypesStr]);
}
} else {
//
// Example: class A inherits 2 methods named 'm'.
// One has the function type '(int) -> dynamic' and one has the
// function type '(num) -> dynamic'. Since they are both a subtype
// of the other, a synthetic function '(dynamic) -> dynamic' is
// inherited.
// Tests: test_getMapOfMembersInheritedFromInterfaces_
// union_multipleSubtypes_*
//
List<ExecutableElement> elementArrayToMerge =
new List<ExecutableElement>(
subtypesOfAllOtherTypesIndexes.length);
for (int i = 0; i < elementArrayToMerge.length; i++) {
elementArrayToMerge[i] =
elements[subtypesOfAllOtherTypesIndexes[i]];
}
ExecutableElement mergedExecutableElement =
_computeMergedExecutableElement(elementArrayToMerge);
resultMap[key] = mergedExecutableElement;
}
}
} else {
_reportError(
classElt,
StaticWarningCode
.INCONSISTENT_METHOD_INHERITANCE_GETTER_AND_METHOD,
[key]);
}
}
});
return resultMap;
}
/**
* Loop through all of the members in the given [map], performing type
* parameter substitutions using a passed [supertype].
*/
void _substituteTypeParametersDownHierarchy(
InterfaceType superType, Map<String, ExecutableElement> map) {
for (String memberName in map.keys) {
ExecutableElement executableElement = map[memberName];
if (executableElement is MethodMember) {
map[memberName] = MethodMember.from(executableElement, superType);
} else if (executableElement is PropertyAccessorMember) {
map[memberName] =
PropertyAccessorMember.from(executableElement, superType);
}
}
}
/**
* Union all of the [lookupMaps] together into a single map, grouping the ExecutableElements
* into a list where none of the elements are equal where equality is determined by having equal
* function types. (We also take note too of the kind of the element: ()->int and () -> int may
* not be equal if one is a getter and the other is a method.)
*
* @param lookupMaps the maps to be unioned together.
* @return the resulting union map.
*/
HashMap<String, List<ExecutableElement>> _unionInterfaceLookupMaps(
List<Map<String, ExecutableElement>> lookupMaps) {
HashMap<String, List<ExecutableElement>> unionMap =
new HashMap<String, List<ExecutableElement>>();
for (Map<String, ExecutableElement> lookupMap in lookupMaps) {
for (String memberName in lookupMap.keys) {
// Get the list value out of the unionMap
List<ExecutableElement> list = unionMap.putIfAbsent(
memberName, () => new List<ExecutableElement>());
// Fetch the entry out of this lookupMap
ExecutableElement newExecutableElementEntry = lookupMap[memberName];
if (list.isEmpty) {
// If the list is empty, just the new value
list.add(newExecutableElementEntry);
} else {
// Otherwise, only add the newExecutableElementEntry if it isn't
// already in the list, this covers situation where a class inherits
// two methods (or two getters) that are identical.
bool alreadyInList = false;
bool isMethod1 = newExecutableElementEntry is MethodElement;
for (ExecutableElement executableElementInList in list) {
bool isMethod2 = executableElementInList is MethodElement;
if (isMethod1 == isMethod2 &&
executableElementInList.type ==
newExecutableElementEntry.type) {
alreadyInList = true;
break;
}
}
if (!alreadyInList) {
list.add(newExecutableElementEntry);
}
}
}
}
return unionMap;
}
/**
* Given some array of [ExecutableElement]s, this method creates a synthetic element as
* described in 8.1.1:
*
* Let <i>numberOfPositionals</i>(<i>f</i>) denote the number of positional parameters of a
* function <i>f</i>, and let <i>numberOfRequiredParams</i>(<i>f</i>) denote the number of
* required parameters of a function <i>f</i>. Furthermore, let <i>s</i> denote the set of all
* named parameters of the <i>m<sub>1</sub>, …, m<sub>k</sub></i>. Then let
* * <i>h = max(numberOfPositionals(m<sub>i</sub>)),</i>
* * <i>r = min(numberOfRequiredParams(m<sub>i</sub>)), for all <i>i</i>, 1 <= i <= k.</i>
* Then <i>I</i> has a method named <i>n</i>, with <i>r</i> required parameters of type
* <b>dynamic</b>, <i>h</i> positional parameters of type <b>dynamic</b>, named parameters
* <i>s</i> of type <b>dynamic</b> and return type <b>dynamic</b>.
*
*/
static ExecutableElement _computeMergedExecutableElement(
List<ExecutableElement> elementArrayToMerge) {
int h = _getNumOfPositionalParameters(elementArrayToMerge[0]);
int r = _getNumOfRequiredParameters(elementArrayToMerge[0]);
Set<String> namedParametersList = new HashSet<String>();
for (int i = 1; i < elementArrayToMerge.length; i++) {
ExecutableElement element = elementArrayToMerge[i];
int numOfPositionalParams = _getNumOfPositionalParameters(element);
if (h < numOfPositionalParams) {
h = numOfPositionalParams;
}
int numOfRequiredParams = _getNumOfRequiredParameters(element);
if (r > numOfRequiredParams) {
r = numOfRequiredParams;
}
namedParametersList.addAll(_getNamedParameterNames(element));
}
return _createSyntheticExecutableElement(
elementArrayToMerge,
elementArrayToMerge[0].displayName,
r,
h - r,
new List.from(namedParametersList));
}
/**
* Used by [computeMergedExecutableElement] to actually create the
* synthetic element.
*
* @param elementArrayToMerge the array used to create the synthetic element
* @param name the name of the method, getter or setter
* @param numOfRequiredParameters the number of required parameters
* @param numOfPositionalParameters the number of positional parameters
* @param namedParameters the list of [String]s that are the named parameters
* @return the created synthetic element
*/
static ExecutableElement _createSyntheticExecutableElement(
List<ExecutableElement> elementArrayToMerge,
String name,
int numOfRequiredParameters,
int numOfPositionalParameters,
List<String> namedParameters) {
DynamicTypeImpl dynamicType = DynamicTypeImpl.instance;
SimpleIdentifier nameIdentifier = astFactory
.simpleIdentifier(new StringToken(TokenType.IDENTIFIER, name, 0));
ExecutableElementImpl executable;
ExecutableElement elementToMerge = elementArrayToMerge[0];
if (elementToMerge is MethodElement) {
MultiplyInheritedMethodElementImpl unionedMethod =
new MultiplyInheritedMethodElementImpl(nameIdentifier);
unionedMethod.inheritedElements = elementArrayToMerge;
executable = unionedMethod;
} else if (elementToMerge is PropertyAccessorElement) {
MultiplyInheritedPropertyAccessorElementImpl unionedPropertyAccessor =
new MultiplyInheritedPropertyAccessorElementImpl(nameIdentifier);
unionedPropertyAccessor.getter = elementToMerge.isGetter;
unionedPropertyAccessor.setter = elementToMerge.isSetter;
unionedPropertyAccessor.inheritedElements = elementArrayToMerge;
executable = unionedPropertyAccessor;
} else {
throw new AnalysisException(
'Invalid class of element in merge: ${elementToMerge.runtimeType}');
}
int numOfParameters = numOfRequiredParameters +
numOfPositionalParameters +
namedParameters.length;
List<ParameterElement> parameters =
new List<ParameterElement>(numOfParameters);
int i = 0;
for (int j = 0; j < numOfRequiredParameters; j++, i++) {
ParameterElementImpl parameter = new ParameterElementImpl("", 0);
parameter.type = dynamicType;
parameter.parameterKind = ParameterKind.REQUIRED;
parameters[i] = parameter;
}
for (int k = 0; k < numOfPositionalParameters; k++, i++) {
ParameterElementImpl parameter = new ParameterElementImpl("", 0);
parameter.type = dynamicType;