-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
element_map_impl.dart
2319 lines (2039 loc) · 78.7 KB
/
element_map_impl.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) 2017, 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.
library dart2js.kernel.element_map;
import 'package:kernel/ast.dart' as ir;
import '../closure.dart' show BoxLocal;
import '../common.dart';
import '../common/names.dart' show Identifiers;
import '../common/resolution.dart';
import '../compile_time_constants.dart';
import '../constants/constant_system.dart';
import '../constants/constructors.dart';
import '../constants/evaluation.dart';
import '../constants/expressions.dart';
import '../constants/values.dart';
import '../common_elements.dart';
import '../elements/elements.dart';
import '../elements/entities.dart';
import '../elements/entity_utils.dart' as utils;
import '../elements/names.dart';
import '../elements/types.dart';
import '../environment.dart';
import '../frontend_strategy.dart';
import '../js_backend/backend_usage.dart';
import '../js_backend/constant_system_javascript.dart';
import '../js_backend/interceptor_data.dart';
import '../js_backend/native_data.dart';
import '../js_backend/no_such_method_registry.dart';
import '../js_backend/runtime_types.dart';
import '../js_model/closure.dart';
import '../js_model/elements.dart';
import '../native/enqueue.dart';
import '../native/native.dart' as native;
import '../native/resolver.dart';
import '../ordered_typeset.dart';
import '../options.dart';
import '../ssa/kernel_impact.dart';
import '../universe/class_set.dart';
import '../universe/selector.dart';
import '../universe/world_builder.dart';
import '../world.dart';
import '../util/util.dart' show Link, LinkBuilder;
import 'element_map.dart';
import 'element_map_mixins.dart';
import 'elements.dart';
import 'env.dart';
import 'kelements.dart';
part 'native_basic_data.dart';
part 'no_such_method_resolver.dart';
part 'types.dart';
/// Interface for kernel queries needed to implement the [CodegenWorldBuilder].
abstract class KernelToWorldBuilder implements KernelToElementMapForBuilding {
/// Returns `true` if [field] has a constant initializer.
bool hasConstantFieldInitializer(FieldEntity field);
/// Returns the constant initializer for [field].
ConstantValue getConstantFieldInitializer(FieldEntity field);
/// Calls [f] for each parameter of [function] providing the type and name of
/// the parameter and the [defaultValue] if the parameter is optional.
void forEachParameter(FunctionEntity function,
void f(DartType type, String name, ConstantValue defaultValue));
}
abstract class KernelToElementMapBase extends KernelToElementMapBaseMixin {
final DiagnosticReporter reporter;
CommonElements _commonElements;
ElementEnvironment _elementEnvironment;
DartTypeConverter _typeConverter;
KernelConstantEnvironment _constantEnvironment;
_KernelDartTypes _types;
/// Library environment. Used for fast lookup.
ProgramEnv _env = new ProgramEnv();
List<LibraryEntity> _libraryList = <LibraryEntity>[];
List<ClassEntity> _classList = <ClassEntity>[];
List<MemberEntity> _memberList = <MemberEntity>[];
List<TypeVariableEntity> _typeVariableList = <TypeVariableEntity>[];
List<TypedefEntity> _typedefList = <TypedefEntity>[];
/// List of library environments by `IndexedLibrary.libraryIndex`. This is
/// used for fast lookup into library classes and members.
List<LibraryEnv> _libraryEnvs = <LibraryEnv>[];
/// List of library data by `IndexedLibrary.libraryIndex`. This is used for
/// fast lookup into library properties.
List<LibraryData> _libraryData = <LibraryData>[];
/// List of class environments by `IndexedClass.classIndex`. This is used for
/// fast lookup into class members.
List<ClassEnv> _classEnvs = <ClassEnv>[];
/// List of class data by `IndexedClass.classIndex`. This is used for
/// fast lookup into class properties.
List<ClassData> _classData = <ClassData>[];
/// List of member data by `IndexedMember.memberIndex`. This is used for
/// fast lookup into member properties.
List<MemberData> _memberData = <MemberData>[];
/// List of typedef data by `IndexedTypedef.typedefIndex`. This is used for
/// fast lookup into typedef properties.
List<TypedefData> _typedefData = <TypedefData>[];
KernelToElementMapBase(this.reporter, Environment environment) {
_elementEnvironment = new KernelElementEnvironment(this);
_commonElements = new CommonElements(_elementEnvironment);
_constantEnvironment = new KernelConstantEnvironment(this, environment);
_typeConverter = new DartTypeConverter(this);
_types = new _KernelDartTypes(this);
}
bool checkFamily(Entity entity);
DartTypes get types => _types;
@override
ElementEnvironment get elementEnvironment => _elementEnvironment;
@override
CommonElements get commonElements => _commonElements;
/// NativeBasicData is need for computation of the default super class.
NativeBasicData get nativeBasicData;
FunctionEntity get _mainFunction {
return _env.mainMethod != null ? _getMethod(_env.mainMethod) : null;
}
LibraryEntity get _mainLibrary {
return _env.mainMethod != null
? _getLibrary(_env.mainMethod.enclosingLibrary)
: null;
}
Iterable<LibraryEntity> get _libraries;
SourceSpan getSourceSpan(Spannable spannable, Entity currentElement) {
SourceSpan fromSpannable(Spannable spannable) {
if (spannable is IndexedLibrary &&
spannable.libraryIndex < _libraryEnvs.length) {
LibraryEnv env = _libraryEnvs[spannable.libraryIndex];
return computeSourceSpanFromTreeNode(env.library);
} else if (spannable is IndexedClass &&
spannable.classIndex < _classEnvs.length) {
ClassData data = _classData[spannable.classIndex];
return data.definition.location;
} else if (spannable is IndexedMember &&
spannable.memberIndex < _memberData.length) {
MemberData data = _memberData[spannable.memberIndex];
return data.definition.location;
}
return null;
}
SourceSpan sourceSpan = fromSpannable(spannable);
sourceSpan ??= fromSpannable(currentElement);
return sourceSpan;
}
LibraryEntity lookupLibrary(Uri uri) {
LibraryEnv libraryEnv = _env.lookupLibrary(uri);
if (libraryEnv == null) return null;
return _getLibrary(libraryEnv.library, libraryEnv);
}
String _getLibraryName(IndexedLibrary library) {
assert(checkFamily(library));
LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
return libraryEnv.library.name ?? '';
}
MemberEntity lookupLibraryMember(IndexedLibrary library, String name,
{bool setter: false}) {
assert(checkFamily(library));
LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
ir.Member member = libraryEnv.lookupMember(name, setter: setter);
return member != null ? getMember(member) : null;
}
void _forEachLibraryMember(
IndexedLibrary library, void f(MemberEntity member)) {
assert(checkFamily(library));
LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
libraryEnv.forEachMember((ir.Member node) {
f(getMember(node));
});
}
ClassEntity lookupClass(IndexedLibrary library, String name) {
assert(checkFamily(library));
LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
ClassEnv classEnv = libraryEnv.lookupClass(name);
if (classEnv != null) {
return _getClass(classEnv.cls, classEnv);
}
return null;
}
void _forEachClass(IndexedLibrary library, void f(ClassEntity cls)) {
assert(checkFamily(library));
LibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex];
libraryEnv.forEachClass((ClassEnv classEnv) {
if (!classEnv.isUnnamedMixinApplication) {
f(_getClass(classEnv.cls, classEnv));
}
});
}
MemberEntity lookupClassMember(IndexedClass cls, String name,
{bool setter: false}) {
assert(checkFamily(cls));
ClassEnv classEnv = _classEnvs[cls.classIndex];
return classEnv.lookupMember(this, name, setter: setter);
}
ConstructorEntity lookupConstructor(IndexedClass cls, String name) {
assert(checkFamily(cls));
ClassEnv classEnv = _classEnvs[cls.classIndex];
return classEnv.lookupConstructor(this, name);
}
@override
InterfaceType createInterfaceType(
ir.Class cls, List<ir.DartType> typeArguments) {
return new InterfaceType(getClass(cls), getDartTypes(typeArguments));
}
LibraryEntity getLibrary(ir.Library node) => _getLibrary(node);
LibraryEntity _getLibrary(ir.Library node, [LibraryEnv libraryEnv]);
@override
ClassEntity getClass(ir.Class node) => _getClass(node);
ClassEntity _getClass(ir.Class node, [ClassEnv classEnv]);
InterfaceType _getSuperType(IndexedClass cls) {
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureSupertypes(cls, data);
return data.supertype;
}
void _ensureThisAndRawType(ClassEntity cls, ClassData data) {
assert(checkFamily(cls));
if (data.thisType == null) {
ir.Class node = data.cls;
if (node.typeParameters.isEmpty) {
data.thisType =
data.rawType = new InterfaceType(cls, const <DartType>[]);
} else {
data.thisType = new InterfaceType(
cls,
new List<DartType>.generate(node.typeParameters.length,
(int index) {
return new TypeVariableType(
_getTypeVariable(node.typeParameters[index]));
}));
data.rawType = new InterfaceType(
cls,
new List<DartType>.filled(
node.typeParameters.length, const DynamicType()));
}
}
}
TypeVariableEntity getTypeVariable(ir.TypeParameter node) =>
_getTypeVariable(node);
TypeVariableEntity _getTypeVariable(ir.TypeParameter node);
void _ensureSupertypes(ClassEntity cls, ClassData data) {
assert(checkFamily(cls));
if (data.orderedTypeSet == null) {
_ensureThisAndRawType(cls, data);
ir.Class node = data.cls;
if (node.supertype == null) {
data.orderedTypeSet = new OrderedTypeSet.singleton(data.thisType);
data.isMixinApplication = false;
data.interfaces = const <InterfaceType>[];
} else {
InterfaceType processSupertype(ir.Supertype node) {
InterfaceType supertype = _typeConverter.visitSupertype(node);
IndexedClass superclass = supertype.element;
ClassData superdata = _classData[superclass.classIndex];
_ensureSupertypes(superclass, superdata);
return supertype;
}
InterfaceType supertype = processSupertype(node.supertype);
if (supertype == _commonElements.objectType) {
ClassEntity defaultSuperclass =
_commonElements.getDefaultSuperclass(cls, nativeBasicData);
data.supertype = _elementEnvironment.getRawType(defaultSuperclass);
} else {
data.supertype = supertype;
}
LinkBuilder<InterfaceType> linkBuilder =
new LinkBuilder<InterfaceType>();
if (node.mixedInType != null) {
data.isMixinApplication = true;
linkBuilder
.addLast(data.mixedInType = processSupertype(node.mixedInType));
} else {
data.isMixinApplication = false;
}
node.implementedTypes.forEach((ir.Supertype supertype) {
linkBuilder.addLast(processSupertype(supertype));
});
Link<InterfaceType> interfaces = linkBuilder.toLink();
OrderedTypeSetBuilder setBuilder =
new _KernelOrderedTypeSetBuilder(this, cls);
data.orderedTypeSet = setBuilder.createOrderedTypeSet(
data.supertype, interfaces.reverse());
data.interfaces = new List<InterfaceType>.from(interfaces.toList());
}
}
}
@override
TypedefType getTypedefType(ir.Typedef node) {
IndexedTypedef typedef = _getTypedef(node);
return _typedefData[typedef.typedefIndex].rawType;
}
TypedefEntity _getTypedef(ir.Typedef node);
@override
MemberEntity getMember(ir.Member node) {
if (node is ir.Field) {
return _getField(node);
} else if (node is ir.Constructor) {
return _getConstructor(node);
} else if (node is ir.Procedure) {
if (node.kind == ir.ProcedureKind.Factory) {
return _getConstructor(node);
} else {
return _getMethod(node);
}
}
throw new UnsupportedError("Unexpected member: $node");
}
MemberEntity getSuperMember(ir.Member context, ir.Name name, ir.Member target,
{bool setter: false}) {
if (target != null) {
return getMember(target);
}
ClassEntity cls = getMember(context).enclosingClass;
IndexedClass superclass = _getSuperType(cls)?.element;
while (superclass != null) {
ClassEnv env = _classEnvs[superclass.classIndex];
MemberEntity superMember =
env.lookupMember(this, name.name, setter: setter);
if (superMember != null) {
return superMember;
}
superclass = _getSuperType(superclass)?.element;
}
throw failedAt(cls, "No super method member found for ${name} in $cls.");
}
@override
ConstructorEntity getConstructor(ir.Member node) => _getConstructor(node);
ConstructorEntity _getConstructor(ir.Member node);
ConstructorEntity getSuperConstructor(
ir.Constructor sourceNode, ir.Member targetNode) {
ConstructorEntity source = getConstructor(sourceNode);
ClassEntity sourceClass = source.enclosingClass;
ConstructorEntity target = getConstructor(targetNode);
ClassEntity targetClass = target.enclosingClass;
IndexedClass superClass = _getSuperType(sourceClass)?.element;
if (superClass == targetClass) {
return target;
}
ClassEnv env = _classEnvs[superClass.classIndex];
ConstructorEntity constructor = env.lookupConstructor(this, target.name);
if (constructor != null) {
return constructor;
}
throw failedAt(source, "Super constructor for $source not found.");
}
@override
FunctionEntity getMethod(ir.Procedure node) => _getMethod(node);
FunctionEntity _getMethod(ir.Procedure node);
@override
FieldEntity getField(ir.Field node) => _getField(node);
FieldEntity _getField(ir.Field node);
@override
DartType getDartType(ir.DartType type) => _typeConverter.convert(type);
List<DartType> getDartTypes(List<ir.DartType> types) {
List<DartType> list = <DartType>[];
types.forEach((ir.DartType type) {
list.add(getDartType(type));
});
return list;
}
@override
InterfaceType getInterfaceType(ir.InterfaceType type) =>
_typeConverter.convert(type);
@override
FunctionType getFunctionType(ir.FunctionNode node) {
DartType returnType;
if (node.parent is ir.Constructor) {
// The return type on generative constructors is `void`, but we need
// `dynamic` type to match the element model.
returnType = const DynamicType();
} else {
returnType = getDartType(node.returnType);
}
List<DartType> parameterTypes = <DartType>[];
List<DartType> optionalParameterTypes = <DartType>[];
for (ir.VariableDeclaration variable in node.positionalParameters) {
if (parameterTypes.length == node.requiredParameterCount) {
optionalParameterTypes.add(getDartType(variable.type));
} else {
parameterTypes.add(getDartType(variable.type));
}
}
List<String> namedParameters = <String>[];
List<DartType> namedParameterTypes = <DartType>[];
List<ir.VariableDeclaration> sortedNamedParameters =
node.namedParameters.toList()..sort((a, b) => a.name.compareTo(b.name));
for (ir.VariableDeclaration variable in sortedNamedParameters) {
namedParameters.add(variable.name);
namedParameterTypes.add(getDartType(variable.type));
}
return new FunctionType(returnType, parameterTypes, optionalParameterTypes,
namedParameters, namedParameterTypes);
}
@override
ConstantValue computeConstantValue(ConstantExpression constant,
{bool requireConstant: true}) {
return _constantEnvironment.getConstantValue(constant);
}
DartType _substByContext(DartType type, InterfaceType context) {
return type.subst(
context.typeArguments, _getThisType(context.element).typeArguments);
}
// TODO(johnniwinther): Remove this when call-type is provided by fasta.
void _ensureCallType(IndexedClass cls, ClassData data) {
if (!data.isCallTypeComputed) {
data.isCallTypeComputed = true;
MemberEntity callMethod = lookupClassMember(cls, Identifiers.call);
if (callMethod != null) {
if (callMethod.isFunction) {
data.callType = _getFunctionType(callMethod);
} else {
data.callType = const DynamicType();
}
return;
}
Set<FunctionType> inheritedCallTypes = new Set<FunctionType>();
bool inheritsInvalidCallMember = false;
void addCallType(InterfaceType supertype) {
if (supertype == null) return;
DartType type = _getCallType(supertype);
if (type == null) return;
if (type.isFunctionType) {
inheritedCallTypes.add(type);
} else {
inheritsInvalidCallMember = true;
}
}
addCallType(_getSuperType(cls));
_getInterfaces(cls).forEach(addCallType);
// Following §11.1.1 in the spec.
if (inheritsInvalidCallMember) {
// From §11.1.1 in the spec (continued):
//
// If some but not all of the m_i, 1 ≤ i ≤ k are getters none of the m_i
// are inherited, and a static warning is issued.
data.callType = const DynamicType();
} else if (inheritedCallTypes.isEmpty) {
return;
} else if (inheritedCallTypes.length == 1) {
data.callType = inheritedCallTypes.single;
} else {
// From §11.1.1 in the spec (continued):
//
// Otherwise, if the static types T_1, ... , T_k of the members
// m_1, ..., m_k are not identical, then there must be a member m_x such
// that T_x <: T_i, 1 ≤ x ≤ k for all i ∈ 1..k, or a static type warning
// occurs.
List<FunctionType> subtypesOfAllInherited = <FunctionType>[];
outer:
for (FunctionType a in inheritedCallTypes) {
for (FunctionType b in inheritedCallTypes) {
if (identical(a, b)) continue;
if (!types.isSubtype(a, b)) continue outer;
}
subtypesOfAllInherited.add(a);
}
if (subtypesOfAllInherited.length == 1) {
// From §11.1.1 in the spec (continued):
//
// The member that is inherited is m_x, if it exists.
data.callType = subtypesOfAllInherited.single;
return;
}
// From §11.1.1 in the spec (continued):
//
// Otherwise: let numberOfPositionals(f) denote the number of
// positional parameters of a function f, and let
// numberOfRequiredParams(f) denote the number of required parameters of
// a function f. Furthermore, let s denote the set of all named
// parameters of the m_1, . . . , m_k. Then let
//
// h = max(numberOfPositionals(mi)),
// r = min(numberOfRequiredParams(mi)), i ∈ 1..k.
// Then I has a method named n, with r required parameters of type
// dynamic, h positional parameters of type dynamic, named parameters s
// of type dynamic and return type dynamic.
// Multiple signatures with different types => create the synthesized
// version.
int minRequiredParameters;
int maxPositionalParameters;
Set<String> names = new Set<String>();
for (FunctionType type in inheritedCallTypes) {
type.namedParameters.forEach((String name) => names.add(name));
int requiredParameters = type.parameterTypes.length;
int optionalParameters = type.optionalParameterTypes.length;
int positionalParameters = requiredParameters + optionalParameters;
if (minRequiredParameters == null ||
minRequiredParameters > requiredParameters) {
minRequiredParameters = requiredParameters;
}
if (maxPositionalParameters == null ||
maxPositionalParameters < positionalParameters) {
maxPositionalParameters = positionalParameters;
}
}
int optionalParameters =
maxPositionalParameters - minRequiredParameters;
// TODO(johnniwinther): Support function types with both optional
// and named parameters?
if (optionalParameters == 0 || names.isEmpty) {
DartType dynamic = const DynamicType();
List<DartType> requiredParameterTypes =
new List.filled(minRequiredParameters, dynamic);
List<DartType> optionalParameterTypes =
new List.filled(optionalParameters, dynamic);
List<String> namedParameters = names.toList()
..sort((a, b) => a.compareTo(b));
List<DartType> namedParameterTypes =
new List.filled(namedParameters.length, dynamic);
data.callType = new FunctionType(dynamic, requiredParameterTypes,
optionalParameterTypes, namedParameters, namedParameterTypes);
} else {
// The function type is not valid.
data.callType = const DynamicType();
}
}
}
}
/// Returns the type of the `call` method on 'type'.
///
/// If [type] doesn't have a `call` member `null` is returned. If [type] has
/// an invalid `call` member (non-method or a synthesized method with both
/// optional and named parameters) a [DynamicType] is returned.
DartType _getCallType(InterfaceType type) {
IndexedClass cls = type.element;
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureCallType(cls, data);
if (data.callType != null) {
return _substByContext(data.callType, type);
}
return null;
}
InterfaceType _getThisType(IndexedClass cls) {
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureThisAndRawType(cls, data);
return data.thisType;
}
InterfaceType _getRawType(IndexedClass cls) {
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureThisAndRawType(cls, data);
return data.rawType;
}
FunctionType _getFunctionType(IndexedFunction function) {
assert(checkFamily(function));
FunctionData data = _memberData[function.memberIndex];
return data.getFunctionType(this);
}
ClassEntity _getAppliedMixin(IndexedClass cls) {
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureSupertypes(cls, data);
return data.mixedInType?.element;
}
bool _isMixinApplication(IndexedClass cls) {
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureSupertypes(cls, data);
return data.isMixinApplication;
}
bool _isUnnamedMixinApplication(IndexedClass cls) {
assert(checkFamily(cls));
ClassEnv env = _classEnvs[cls.classIndex];
return env.isUnnamedMixinApplication;
}
void _forEachSupertype(IndexedClass cls, void f(InterfaceType supertype)) {
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureSupertypes(cls, data);
data.orderedTypeSet.supertypes.forEach(f);
}
void _forEachMixin(IndexedClass cls, void f(ClassEntity mixin)) {
assert(checkFamily(cls));
while (cls != null) {
ClassData data = _classData[cls.classIndex];
_ensureSupertypes(cls, data);
if (data.mixedInType != null) {
f(data.mixedInType.element);
}
cls = data.supertype?.element;
}
}
void _forEachConstructor(IndexedClass cls, void f(ConstructorEntity member)) {
assert(checkFamily(cls));
ClassEnv env = _classEnvs[cls.classIndex];
env.forEachConstructor(this, f);
}
void _forEachConstructorBody(
IndexedClass cls, void f(ConstructorBodyEntity member)) {
throw new UnsupportedError(
'KernelToElementMapBase._forEachConstructorBody');
}
void _forEachClassMember(
IndexedClass cls, void f(ClassEntity cls, MemberEntity member)) {
assert(checkFamily(cls));
ClassEnv env = _classEnvs[cls.classIndex];
env.forEachMember(this, (MemberEntity member) {
f(cls, member);
});
ClassData data = _classData[cls.classIndex];
_ensureSupertypes(cls, data);
if (data.supertype != null) {
_forEachClassMember(data.supertype.element, f);
}
}
ConstantConstructor _getConstructorConstant(IndexedConstructor constructor) {
assert(checkFamily(constructor));
ConstructorData data = _memberData[constructor.memberIndex];
return data.getConstructorConstant(this, constructor);
}
ConstantExpression _getFieldConstant(IndexedField field) {
assert(checkFamily(field));
FieldData data = _memberData[field.memberIndex];
return data.getFieldConstant(this, field);
}
InterfaceType _asInstanceOf(InterfaceType type, ClassEntity cls) {
assert(checkFamily(cls));
OrderedTypeSet orderedTypeSet = _getOrderedTypeSet(type.element);
InterfaceType supertype =
orderedTypeSet.asInstanceOf(cls, _getHierarchyDepth(cls));
if (supertype != null) {
supertype = _substByContext(supertype, type);
}
return supertype;
}
OrderedTypeSet _getOrderedTypeSet(IndexedClass cls) {
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureSupertypes(cls, data);
return data.orderedTypeSet;
}
int _getHierarchyDepth(IndexedClass cls) {
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureSupertypes(cls, data);
return data.orderedTypeSet.maxDepth;
}
Iterable<InterfaceType> _getInterfaces(IndexedClass cls) {
assert(checkFamily(cls));
ClassData data = _classData[cls.classIndex];
_ensureSupertypes(cls, data);
return data.interfaces;
}
Spannable _getSpannable(MemberEntity member, ir.Node node) {
SourceSpan sourceSpan;
if (node is ir.TreeNode) {
sourceSpan = computeSourceSpanFromTreeNode(node);
}
sourceSpan ??= getSourceSpan(member, null);
return sourceSpan;
}
MemberDefinition _getMemberDefinition(covariant IndexedMember member) {
assert(checkFamily(member));
return _memberData[member.memberIndex].definition;
}
ClassDefinition _getClassDefinition(covariant IndexedClass cls) {
assert(checkFamily(cls));
return _classData[cls.classIndex].definition;
}
}
/// Mixin that implements the abstract methods in [KernelToElementMapBase].
abstract class ElementCreatorMixin {
ProgramEnv get _env;
List<LibraryEntity> get _libraryList;
List<LibraryEnv> get _libraryEnvs;
List<LibraryData> get _libraryData;
List<ClassEntity> get _classList;
List<ClassEnv> get _classEnvs;
List<ClassData> get _classData;
List<MemberEntity> get _memberList;
List<MemberData> get _memberData;
List<TypeVariableEntity> get _typeVariableList;
List<TypedefEntity> get _typedefList;
List<TypedefData> get _typedefData;
Map<ir.Library, IndexedLibrary> _libraryMap = <ir.Library, IndexedLibrary>{};
Map<ir.Class, IndexedClass> _classMap = <ir.Class, IndexedClass>{};
Map<ir.Typedef, IndexedTypedef> _typedefMap = <ir.Typedef, IndexedTypedef>{};
Map<ir.TypeParameter, IndexedTypeVariable> _typeVariableMap =
<ir.TypeParameter, IndexedTypeVariable>{};
Map<ir.Member, IndexedConstructor> _constructorMap =
<ir.Member, IndexedConstructor>{};
Map<ir.Procedure, IndexedFunction> _methodMap =
<ir.Procedure, IndexedFunction>{};
Map<ir.Field, IndexedField> _fieldMap = <ir.Field, IndexedField>{};
Map<ir.TreeNode, Local> _localFunctionMap = <ir.TreeNode, Local>{};
Name getName(ir.Name node);
FunctionType getFunctionType(ir.FunctionNode node);
MemberEntity getMember(ir.Member node);
Iterable<LibraryEntity> get _libraries {
if (_env.length != _libraryMap.length) {
// Create a [KLibrary] for each library.
_env.forEachLibrary((LibraryEnv env) {
_getLibrary(env.library, env);
});
}
return _libraryMap.values;
}
LibraryEntity _getLibrary(ir.Library node, [LibraryEnv libraryEnv]) {
return _libraryMap.putIfAbsent(node, () {
Uri canonicalUri = node.importUri;
_libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(canonicalUri));
String name = node.name;
if (name == null) {
// Use the file name as script name.
String path = canonicalUri.path;
name = path.substring(path.lastIndexOf('/') + 1);
}
LibraryEntity library =
createLibrary(_libraryMap.length, name, canonicalUri);
_libraryList.add(library);
_libraryData.add(new LibraryData(node));
return library;
});
}
ClassEntity _getClass(ir.Class node, [ClassEnv classEnv]) {
return _classMap.putIfAbsent(node, () {
KLibrary library = _getLibrary(node.enclosingLibrary);
if (classEnv == null) {
classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name);
}
_classEnvs.add(classEnv);
ClassEntity cls = createClass(library, _classList.length, node.name,
isAbstract: node.isAbstract);
_classData
.add(new ClassData(node, new RegularClassDefinition(cls, node)));
_classList.add(cls);
return cls;
});
}
TypedefEntity _getTypedef(ir.Typedef node) {
return _typedefMap.putIfAbsent(node, () {
IndexedLibrary library = _getLibrary(node.enclosingLibrary);
TypedefEntity typedef =
createTypedef(library, _typedefList.length, node.name);
TypedefType typedefType = new TypedefType(
typedef,
new List<DartType>.filled(
node.typeParameters.length, const DynamicType()));
_typedefData.add(new TypedefData(node, typedef, typedefType));
_typedefList.add(typedef);
return typedef;
});
}
TypeVariableEntity _getTypeVariable(ir.TypeParameter node) {
return _typeVariableMap.putIfAbsent(node, () {
if (node.parent is ir.Class) {
int typeVariableIndex = _typeVariableList.length;
ir.Class cls = node.parent;
int index = cls.typeParameters.indexOf(node);
TypeVariableEntity typeVariable = createTypeVariable(
typeVariableIndex, _getClass(cls), node.name, index);
_typeVariableList.add(typeVariable);
return typeVariable;
}
if (node.parent is ir.FunctionNode) {
ir.FunctionNode func = node.parent;
int index = func.typeParameters.indexOf(node);
if (func.parent is ir.Constructor) {
ir.Constructor constructor = func.parent;
ir.Class cls = constructor.enclosingClass;
return _getTypeVariable(cls.typeParameters[index]);
}
if (func.parent is ir.Procedure) {
ir.Procedure procedure = func.parent;
if (procedure.kind == ir.ProcedureKind.Factory) {
ir.Class cls = procedure.enclosingClass;
return _getTypeVariable(cls.typeParameters[index]);
} else {
int typeVariableIndex = _typeVariableList.length;
TypeVariableEntity typeVariable = createTypeVariable(
typeVariableIndex, _getMethod(procedure), node.name, index);
_typeVariableList.add(typeVariable);
return typeVariable;
}
}
}
throw new UnsupportedError('Unsupported type parameter type node $node.');
});
}
ConstructorEntity _getConstructor(ir.Member node) {
return _constructorMap.putIfAbsent(node, () {
int memberIndex = _memberData.length;
ConstructorEntity constructor;
ClassEntity enclosingClass = _getClass(node.enclosingClass);
Name name = getName(node.name);
bool isExternal = node.isExternal;
ir.FunctionNode functionNode;
MemberDefinition definition;
if (node is ir.Constructor) {
functionNode = node.function;
constructor = createGenerativeConstructor(memberIndex, enclosingClass,
name, _getParameterStructure(functionNode),
isExternal: isExternal, isConst: node.isConst);
definition = new SpecialMemberDefinition(
constructor, node, MemberKind.constructor);
} else if (node is ir.Procedure) {
functionNode = node.function;
bool isFromEnvironment = isExternal &&
name.text == 'fromEnvironment' &&
const ['int', 'bool', 'String'].contains(enclosingClass.name);
constructor = createFactoryConstructor(memberIndex, enclosingClass,
name, _getParameterStructure(functionNode),
isExternal: isExternal,
isConst: node.isConst,
isFromEnvironmentConstructor: isFromEnvironment);
definition = new RegularMemberDefinition(constructor, node);
} else {
// TODO(johnniwinther): Convert `node.location` to a [SourceSpan].
throw failedAt(
NO_LOCATION_SPANNABLE, "Unexpected constructor node: ${node}.");
}
_memberData.add(new ConstructorDataImpl(node, functionNode, definition));
_memberList.add(constructor);
return constructor;
});
}
AsyncMarker _getAsyncMarker(ir.FunctionNode node) {
switch (node.asyncMarker) {
case ir.AsyncMarker.Async:
return AsyncMarker.ASYNC;
case ir.AsyncMarker.AsyncStar:
return AsyncMarker.ASYNC_STAR;
case ir.AsyncMarker.Sync:
return AsyncMarker.SYNC;
case ir.AsyncMarker.SyncStar:
return AsyncMarker.SYNC_STAR;
case ir.AsyncMarker.SyncYielding:
default:
throw new UnsupportedError(
"Async marker ${node.asyncMarker} is not supported.");
}
}
FunctionEntity _getMethod(ir.Procedure node) {
return _methodMap.putIfAbsent(node, () {
int memberIndex = _memberData.length;
LibraryEntity library;
ClassEntity enclosingClass;
if (node.enclosingClass != null) {
enclosingClass = _getClass(node.enclosingClass);
library = enclosingClass.library;
} else {
library = _getLibrary(node.enclosingLibrary);
}
Name name = getName(node.name);
bool isStatic = node.isStatic;
bool isExternal = node.isExternal;
bool isAbstract = node.isAbstract;
IndexedFunction function;
AsyncMarker asyncMarker = _getAsyncMarker(node.function);
switch (node.kind) {
case ir.ProcedureKind.Factory:
throw new UnsupportedError("Cannot create method from factory.");
case ir.ProcedureKind.Getter:
function = createGetter(
memberIndex, library, enclosingClass, name, asyncMarker,
isStatic: isStatic,
isExternal: isExternal,
isAbstract: isAbstract);
break;
case ir.ProcedureKind.Method:
case ir.ProcedureKind.Operator:
function = createMethod(memberIndex, library, enclosingClass, name,
_getParameterStructure(node.function), asyncMarker,
isStatic: isStatic,
isExternal: isExternal,
isAbstract: isAbstract);
break;
case ir.ProcedureKind.Setter:
assert(asyncMarker == AsyncMarker.SYNC);
function = createSetter(
memberIndex, library, enclosingClass, name.setter,
isStatic: isStatic,
isExternal: isExternal,
isAbstract: isAbstract);
break;
}
_memberData.add(new FunctionDataImpl(
node, node.function, new RegularMemberDefinition(function, node)));
_memberList.add(function);
return function;
});
}
FieldEntity _getField(ir.Field node) {
return _fieldMap.putIfAbsent(node, () {
int memberIndex = _memberData.length;
LibraryEntity library;
ClassEntity enclosingClass;
if (node.enclosingClass != null) {
enclosingClass = _getClass(node.enclosingClass);
library = enclosingClass.library;
} else {
library = _getLibrary(node.enclosingLibrary);
}
Name name = getName(node.name);
bool isStatic = node.isStatic;
FieldEntity field = createField(
memberIndex, library, enclosingClass, name,
isStatic: isStatic,