/
type.dart
2947 lines (2641 loc) · 91.1 KB
/
type.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) 2014, 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 analyzer.src.dart.element.type;
import 'dart:collection';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/member.dart';
import 'package:analyzer/src/generated/engine.dart'
show AnalysisContext, AnalysisEngine;
import 'package:analyzer/src/generated/type_system.dart';
import 'package:analyzer/src/generated/utilities_collection.dart';
import 'package:analyzer/src/generated/utilities_dart.dart';
/**
* Type of callbacks used by [DeferredFunctionTypeImpl].
*/
typedef FunctionTypedElement FunctionTypedElementComputer();
/**
* Computer of type arguments which is used to delay computing of type
* arguments until they are requested, instead of at the [ParameterizedType]
* creation time.
*/
typedef List<DartType> TypeArgumentsComputer();
/**
* A [Type] that represents the type 'bottom'.
*/
class BottomTypeImpl extends TypeImpl {
/**
* The unique instance of this class.
*/
static final BottomTypeImpl instance = new BottomTypeImpl._();
/**
* Prevent the creation of instances of this class.
*/
BottomTypeImpl._() : super(null, "<bottom>");
@override
int get hashCode => 0;
@override
bool get isBottom => true;
@override
bool operator ==(Object object) => identical(object, this);
@override
bool isMoreSpecificThan(DartType type,
[bool withDynamic = false, Set<Element> visitedElements]) =>
true;
@override
bool isSubtypeOf(DartType type) => true;
@override
bool isSupertypeOf(DartType type) => false;
@override
TypeImpl pruned(List<FunctionTypeAliasElement> prune) => this;
@override
BottomTypeImpl substitute2(
List<DartType> argumentTypes, List<DartType> parameterTypes,
[List<FunctionTypeAliasElement> prune]) =>
this;
}
/**
* The type created internally if a circular reference is ever detected in a
* function type.
*/
class CircularFunctionTypeImpl extends DynamicTypeImpl
implements FunctionTypeImpl {
CircularFunctionTypeImpl() : super._circular();
@override
List<ParameterElement> get baseParameters => ParameterElement.EMPTY_LIST;
@override
DartType get baseReturnType => DynamicTypeImpl.instance;
@override
List<TypeParameterElement> get boundTypeParameters =>
TypeParameterElement.EMPTY_LIST;
@override
FunctionTypedElement get element => super.element;
@override
bool get isInstantiated => false;
@override
Map<String, DartType> get namedParameterTypes => <String, DartType>{};
@override
List<FunctionTypeAliasElement> get newPrune =>
FunctionTypeAliasElement.EMPTY_LIST;
@override
List<String> get normalParameterNames => <String>[];
@override
List<DartType> get normalParameterTypes => DartType.EMPTY_LIST;
@override
List<String> get optionalParameterNames => <String>[];
@override
List<DartType> get optionalParameterTypes => DartType.EMPTY_LIST;
@override
List<ParameterElement> get parameters => ParameterElement.EMPTY_LIST;
@override
List<FunctionTypeAliasElement> get prunedTypedefs =>
FunctionTypeAliasElement.EMPTY_LIST;
@override
DartType get returnType => DynamicTypeImpl.instance;
@override
List<DartType> get typeArguments => DartType.EMPTY_LIST;
@override
List<TypeParameterElement> get typeFormals => TypeParameterElement.EMPTY_LIST;
@override
List<TypeParameterElement> get typeParameters =>
TypeParameterElement.EMPTY_LIST;
@override
List<TypeParameterElement> get _explicitTypeParameters =>
TypeParameterElement.EMPTY_LIST;
@override
bool get _isInstantiated => false;
@override
List<ParameterElement> get _parameters => ParameterElement.EMPTY_LIST;
@override
DartType get _returnType => DynamicTypeImpl.instance;
@override
List<DartType> get _typeArguments => DartType.EMPTY_LIST;
@override
void set _typeArguments(List<DartType> arguments) {
throw new UnsupportedError('Cannot have type arguments');
}
@override
List<TypeParameterElement> get _typeParameters =>
TypeParameterElement.EMPTY_LIST;
@override
void set _typeParameters(List<TypeParameterElement> parameters) {
throw new UnsupportedError('Cannot have type parameters');
}
@override
bool operator ==(Object object) => object is CircularFunctionTypeImpl;
@override
void appendTo(StringBuffer buffer, Set<TypeImpl> visitedTypes) {
buffer.write('...');
}
@override
FunctionTypeImpl instantiate(List<DartType> argumentTypes) => this;
@override
FunctionTypeImpl pruned(List<FunctionTypeAliasElement> prune) => this;
@override
FunctionType substitute2(
List<DartType> argumentTypes, List<DartType> parameterTypes,
[List<FunctionTypeAliasElement> prune]) {
return this;
}
@override
FunctionTypeImpl substitute3(List<DartType> argumentTypes) => this;
@override
void _forEachParameterType(
ParameterKind kind, callback(String name, DartType type)) {
// There are no parameters.
}
@override
void _freeVariablesInFunctionType(
FunctionType type, Set<TypeParameterType> free) {
// There are no free variables
}
@override
void _freeVariablesInInterfaceType(
InterfaceType type, Set<TypeParameterType> free) {
// There are no free variables
}
@override
void _freeVariablesInType(DartType type, Set<TypeParameterType> free) {
// There are no free variables
}
}
/**
* Type created internally if a circular reference is ever detected. Behaves
* like `dynamic`, except that when converted to a string it is displayed as
* `...`.
*/
class CircularTypeImpl extends DynamicTypeImpl {
CircularTypeImpl() : super._circular();
@override
bool operator ==(Object object) => object is CircularTypeImpl;
@override
void appendTo(StringBuffer buffer, Set<TypeImpl> visitedTypes) {
buffer.write('...');
}
@override
TypeImpl pruned(List<FunctionTypeAliasElement> prune) => this;
}
/**
* The type of a function, method, constructor, getter, or setter that has been
* resynthesized from a summary. The actual underlying element won't be
* constructed until it's needed.
*/
class DeferredFunctionTypeImpl extends FunctionTypeImpl {
/**
* Callback which should be invoked when the element associated with this
* function type is needed.
*
* Once the callback has been invoked, it is set to `null` to reduce GC
* pressure.
*/
FunctionTypedElementComputer _computeElement;
/**
* If [_computeElement] has been called, the value it returned. Otherwise
* `null`.
*/
FunctionTypedElement _computedElement;
DeferredFunctionTypeImpl(this._computeElement, String name,
List<DartType> typeArguments, bool isInstantiated)
: super._(
null, name, null, typeArguments, null, null, null, isInstantiated);
@override
FunctionTypedElement get element {
if (_computeElement != null) {
_computedElement = _computeElement();
_computeElement = null;
}
return _computedElement;
}
}
/**
* The [Type] representing the type `dynamic`.
*/
class DynamicTypeImpl extends TypeImpl {
/**
* The unique instance of this class.
*/
static final DynamicTypeImpl instance = new DynamicTypeImpl._();
/**
* Prevent the creation of instances of this class.
*/
DynamicTypeImpl._()
: super(new DynamicElementImpl(), Keyword.DYNAMIC.lexeme) {
(element as DynamicElementImpl).type = this;
}
/**
* Constructor used by [CircularTypeImpl].
*/
DynamicTypeImpl._circular() : super(instance.element, Keyword.DYNAMIC.lexeme);
@override
int get hashCode => 1;
@override
bool get isDynamic => true;
@override
bool operator ==(Object object) => identical(object, this);
@override
bool isMoreSpecificThan(DartType type,
[bool withDynamic = false, Set<Element> visitedElements]) {
// T is S
if (identical(this, type)) {
return true;
}
// else
return withDynamic;
}
@override
bool isSubtypeOf(DartType type) => true;
@override
bool isSupertypeOf(DartType type) => true;
@override
TypeImpl pruned(List<FunctionTypeAliasElement> prune) => this;
@override
DartType substitute2(
List<DartType> argumentTypes, List<DartType> parameterTypes,
[List<FunctionTypeAliasElement> prune]) {
int length = parameterTypes.length;
for (int i = 0; i < length; i++) {
if (parameterTypes[i] == this) {
return argumentTypes[i];
}
}
return this;
}
}
/**
* The type of a function, method, constructor, getter, or setter.
*/
class FunctionTypeImpl extends TypeImpl implements FunctionType {
/**
* The list of [typeArguments].
*/
List<DartType> _typeArguments;
/**
* The list of [typeParameters], if it has been computed already. Otherwise
* `null`.
*/
List<TypeParameterElement> _typeParameters;
/**
* The list of [typeParameters], if there is no element from which they can be
* computed, or `null` if they should be computed when necessary.
*/
final List<TypeParameterElement> _explicitTypeParameters;
/**
* The return type of the function, or `null` if the return type should be
* accessed through the element.
*/
final DartType _returnType;
/**
* The parameters to the function, or `null` if the parameters should be
* accessed through the element.
*/
final List<ParameterElement> _parameters;
/**
* True if this type is the result of instantiating type parameters (and thus
* any type parameters bound by the typedef should be considered part of
* [typeParameters] rather than [typeFormals]).
*/
final bool _isInstantiated;
/**
* The set of typedefs which should not be expanded when exploring this type,
* to avoid creating infinite types in response to self-referential typedefs.
*/
final List<FunctionTypeAliasElement> prunedTypedefs;
/**
* Initialize a newly created function type to be declared by the given
* [element], and also initialize [typeArguments] to match the
* [typeParameters], which permits later substitution.
*/
FunctionTypeImpl(FunctionTypedElement element,
[List<FunctionTypeAliasElement> prunedTypedefs])
: this._(element, null, prunedTypedefs, null, null, null, null, false);
/**
* Initialize a newly created function type to be declared by the given
* [element], with the given [name] and [typeArguments].
*/
FunctionTypeImpl.elementWithNameAndArgs(Element element, String name,
List<DartType> typeArguments, bool isInstantiated)
: this._(element, name, null, typeArguments, null, null, null,
isInstantiated);
/**
* Initialize a newly created function type to be declared by the given
* [element].
*/
FunctionTypeImpl.forTypedef(FunctionTypeAliasElement element,
[List<FunctionTypeAliasElement> prunedTypedefs])
: this._(element, element?.name, prunedTypedefs, null, null, null, null,
false);
/**
* Private constructor.
*/
FunctionTypeImpl._(
FunctionTypedElement element,
String name,
this.prunedTypedefs,
this._typeArguments,
this._explicitTypeParameters,
this._returnType,
this._parameters,
this._isInstantiated)
: _typeParameters = _explicitTypeParameters,
super(element, name);
/**
* Return the base parameter elements of this function element.
*/
List<ParameterElement> get baseParameters =>
_parameters ?? element.parameters;
/**
* Return the return type defined by this function's element.
*/
DartType get baseReturnType => _returnType ?? element.returnType;
@deprecated
@override
List<TypeParameterElement> get boundTypeParameters => typeFormals;
@override
String get displayName {
String name = this.name;
// Function types have an empty name when they are defined implicitly by
// either a closure or as part of a parameter declaration.
if (name == null || name.length == 0) {
StringBuffer buffer = new StringBuffer();
appendTo(buffer, new Set.identity());
return buffer.toString();
}
List<DartType> typeArguments = this.typeArguments;
bool areAllTypeArgumentsDynamic() {
for (DartType type in typeArguments) {
if (type != null && !type.isDynamic) {
return false;
}
}
return true;
}
// If there is at least one non-dynamic type, then list them out.
if (!areAllTypeArgumentsDynamic()) {
StringBuffer buffer = new StringBuffer();
buffer.write(name);
buffer.write("<");
for (int i = 0; i < typeArguments.length; i++) {
if (i != 0) {
buffer.write(", ");
}
DartType typeArg = typeArguments[i];
buffer.write(typeArg.displayName);
}
buffer.write(">");
name = buffer.toString();
}
return name;
}
@override
FunctionTypedElement get element => super.element;
@override
int get hashCode {
if (element == null) {
return 0;
}
// Reference the arrays of parameters
List<DartType> normalParameterTypes = this.normalParameterTypes;
List<DartType> optionalParameterTypes = this.optionalParameterTypes;
Iterable<DartType> namedParameterTypes = this.namedParameterTypes.values;
// Generate the hashCode
int code = returnType.hashCode;
for (int i = 0; i < normalParameterTypes.length; i++) {
code = (code << 1) + normalParameterTypes[i].hashCode;
}
for (int i = 0; i < optionalParameterTypes.length; i++) {
code = (code << 1) + optionalParameterTypes[i].hashCode;
}
for (DartType type in namedParameterTypes) {
code = (code << 1) + type.hashCode;
}
return code;
}
/**
* Return `true` if this type is the result of instantiating type parameters.
*/
bool get isInstantiated => _isInstantiated;
@override
Map<String, DartType> get namedParameterTypes {
Map<String, DartType> types = <String, DartType>{};
_forEachParameterType(ParameterKind.NAMED, (name, type) {
types[name] = type;
});
return types;
}
/**
* Determine the new set of typedefs which should be pruned when expanding
* this function type.
*/
List<FunctionTypeAliasElement> get newPrune {
Element element = this.element;
if (element is FunctionTypeAliasElement && !element.isSynthetic) {
// This typedef should be pruned, along with anything that was previously
// pruned.
if (prunedTypedefs == null) {
return <FunctionTypeAliasElement>[element];
} else {
return new List<FunctionTypeAliasElement>.from(prunedTypedefs)
..add(element);
}
} else {
// This is not a typedef, so nothing additional needs to be pruned.
return prunedTypedefs;
}
}
@override
List<String> get normalParameterNames {
return baseParameters
.where((parameter) => parameter.parameterKind == ParameterKind.REQUIRED)
.map((parameter) => parameter.name)
.toList();
}
@override
List<DartType> get normalParameterTypes {
List<DartType> types = <DartType>[];
_forEachParameterType(ParameterKind.REQUIRED, (name, type) {
types.add(type);
});
return types;
}
@override
List<String> get optionalParameterNames {
return baseParameters
.where(
(parameter) => parameter.parameterKind == ParameterKind.POSITIONAL)
.map((parameter) => parameter.name)
.toList();
}
@override
List<DartType> get optionalParameterTypes {
List<DartType> types = <DartType>[];
_forEachParameterType(ParameterKind.POSITIONAL, (name, type) {
types.add(type);
});
return types;
}
@override
List<ParameterElement> get parameters {
List<ParameterElement> baseParameters = this.baseParameters;
// no parameters, quick return
int parameterCount = baseParameters.length;
if (parameterCount == 0) {
return baseParameters;
}
// create specialized parameters
var specializedParams = new List<ParameterElement>(parameterCount);
var parameterTypes = TypeParameterTypeImpl.getTypes(typeParameters);
for (int i = 0; i < parameterCount; i++) {
var parameter = baseParameters[i];
if (parameter?.type == null) {
specializedParams[i] = parameter;
continue;
}
// Check if parameter type depends on defining type type arguments, or
// if it needs to be pruned.
if (parameter is FieldFormalParameterElement) {
// TODO(jmesserly): this seems like it won't handle pruning correctly.
specializedParams[i] = new FieldFormalParameterMember(parameter, this);
continue;
}
var baseType = parameter.type as TypeImpl;
TypeImpl type;
if (typeArguments.isEmpty ||
typeArguments.length != typeParameters.length) {
type = baseType.pruned(newPrune);
} else {
type = baseType.substitute2(typeArguments, parameterTypes, newPrune);
}
specializedParams[i] = identical(type, baseType)
? parameter
: new ParameterMember(parameter, this, type);
}
return specializedParams;
}
@override
DartType get returnType {
DartType baseReturnType = this.baseReturnType;
if (baseReturnType == null) {
// TODO(brianwilkerson) This is a patch. The return type should never be
// null and we need to understand why it is and fix it.
return DynamicTypeImpl.instance;
}
// If there are no arguments to substitute, or if the arguments size doesn't
// match the parameter size, return the base return type.
if (typeArguments.length == 0 ||
typeArguments.length != typeParameters.length) {
return (baseReturnType as TypeImpl).pruned(newPrune);
}
return (baseReturnType as TypeImpl).substitute2(typeArguments,
TypeParameterTypeImpl.getTypes(typeParameters), newPrune);
}
/**
* A list containing the actual types of the type arguments.
*/
List<DartType> get typeArguments {
if (_typeArguments == null) {
// TODO(jmesserly): reuse TypeParameterTypeImpl.getTypes once we can
// make it generic, which will allow it to return List<DartType> instead
// of List<TypeParameterType>.
if (typeParameters.isEmpty) {
_typeArguments = DartType.EMPTY_LIST;
} else {
_typeArguments = new List<DartType>.from(
typeParameters.map((t) => t.type),
growable: false);
}
}
return _typeArguments;
}
@override
List<TypeParameterElement> get typeFormals {
if (_isInstantiated || element == null) {
return TypeParameterElement.EMPTY_LIST;
}
List<TypeParameterElement> baseTypeFormals = element.typeParameters;
int formalCount = baseTypeFormals.length;
if (formalCount == 0) {
return TypeParameterElement.EMPTY_LIST;
}
// Create type formals with specialized bounds.
// For example `<U extends T>` where T comes from an outer scope.
return TypeParameterMember.from(baseTypeFormals, this);
}
@override
List<TypeParameterElement> get typeParameters {
if (_typeParameters == null) {
// Combine the generic type variables from all enclosing contexts, except
// for this generic function's type variables. Those variables are
// tracked in [boundTypeParameters].
_typeParameters = <TypeParameterElement>[];
Element e = element;
while (e != null) {
// If a static method, skip the enclosing class type parameters.
if (e is MethodElement && e.isStatic) {
e = e.enclosingElement;
}
e = e.enclosingElement;
if (e is TypeParameterizedElement) {
_typeParameters.addAll(e.typeParameters);
}
}
if (_isInstantiated) {
// Once the type has been instantiated, type parameters defined at the
// site of the declaration of the method are no longer considered part
// [boundTypeParameters]; they are part of [typeParameters].
List<TypeParameterElement> parametersToAdd = element?.typeParameters;
if (parametersToAdd != null) {
_typeParameters.addAll(parametersToAdd);
}
}
}
return _typeParameters;
}
@override
bool operator ==(Object object) {
if (object is FunctionTypeImpl) {
if (typeFormals.length != object.typeFormals.length) {
return false;
}
// `<T>T -> T` should be equal to `<U>U -> U`
// To test this, we instantiate both types with the same (unique) type
// variables, and see if the result is equal.
if (typeFormals.isNotEmpty) {
List<DartType> freshVariables =
relateTypeFormals(this, object, (t, s) => t == s);
if (freshVariables == null) {
return false;
}
return instantiate(freshVariables) ==
object.instantiate(freshVariables);
}
return returnType == object.returnType &&
TypeImpl.equalArrays(
normalParameterTypes, object.normalParameterTypes) &&
TypeImpl.equalArrays(
optionalParameterTypes, object.optionalParameterTypes) &&
_equals(namedParameterTypes, object.namedParameterTypes) &&
TypeImpl.equalArrays(typeArguments, object.typeArguments);
}
return false;
}
@override
void appendTo(StringBuffer buffer, Set<TypeImpl> visitedTypes) {
if (visitedTypes.add(this)) {
if (typeFormals.isNotEmpty) {
// To print a type with type variables, first make sure we have unique
// variable names to print.
Set<TypeParameterType> freeVariables = new HashSet<TypeParameterType>();
_freeVariablesInFunctionType(this, freeVariables);
Set<String> namesToAvoid = new HashSet<String>();
for (DartType arg in freeVariables) {
if (arg is TypeParameterType) {
namesToAvoid.add(arg.displayName);
}
}
List<DartType> instantiateTypeArgs = <DartType>[];
List<DartType> variables = <DartType>[];
buffer.write("<");
for (TypeParameterElement e in typeFormals) {
if (e != typeFormals[0]) {
buffer.write(",");
}
String name = e.name;
int counter = 0;
while (!namesToAvoid.add(name)) {
// Unicode subscript-zero is U+2080, zero is U+0030. Other digits
// are sequential from there. Thus +0x2050 will get us the subscript.
String subscript = new String.fromCharCodes(
counter.toString().codeUnits.map((n) => n + 0x2050));
name = e.name + subscript;
counter++;
}
TypeParameterTypeImpl t =
new TypeParameterTypeImpl(new TypeParameterElementImpl(name, -1));
t.appendTo(buffer, visitedTypes);
instantiateTypeArgs.add(t);
variables.add(e.type);
if (e.bound != null) {
buffer.write(" extends ");
TypeImpl renamed =
e.bound.substitute2(instantiateTypeArgs, variables);
renamed.appendTo(buffer, visitedTypes);
}
}
buffer.write(">");
// Instantiate it and print the resulting type. After instantiation, it
// will no longer have typeFormals, so we will continue below.
this.instantiate(instantiateTypeArgs).appendTo(buffer, visitedTypes);
return;
}
List<DartType> normalParameterTypes = this.normalParameterTypes;
List<DartType> optionalParameterTypes = this.optionalParameterTypes;
Map<String, DartType> namedParameterTypes = this.namedParameterTypes;
DartType returnType = this.returnType;
bool needsComma = false;
void writeSeparator() {
if (needsComma) {
buffer.write(", ");
} else {
needsComma = true;
}
}
void startOptionalParameters() {
if (needsComma) {
buffer.write(", ");
needsComma = false;
}
}
buffer.write("(");
if (normalParameterTypes.isNotEmpty) {
for (DartType type in normalParameterTypes) {
writeSeparator();
(type as TypeImpl).appendTo(buffer, visitedTypes);
}
}
if (optionalParameterTypes.isNotEmpty) {
startOptionalParameters();
buffer.write("[");
for (DartType type in optionalParameterTypes) {
writeSeparator();
(type as TypeImpl).appendTo(buffer, visitedTypes);
}
buffer.write("]");
needsComma = true;
}
if (namedParameterTypes.isNotEmpty) {
startOptionalParameters();
buffer.write("{");
namedParameterTypes.forEach((String name, DartType type) {
writeSeparator();
buffer.write(name);
buffer.write(": ");
(type as TypeImpl).appendTo(buffer, visitedTypes);
});
buffer.write("}");
needsComma = true;
}
buffer.write(")");
buffer.write(ElementImpl.RIGHT_ARROW);
if (returnType == null) {
buffer.write("null");
} else {
(returnType as TypeImpl).appendTo(buffer, visitedTypes);
}
visitedTypes.remove(this);
} else {
buffer.write('<recursive>');
}
}
@override
FunctionTypeImpl instantiate(List<DartType> argumentTypes) {
if (argumentTypes.length != typeFormals.length) {
throw new ArgumentError(
"argumentTypes.length (${argumentTypes.length}) != "
"typeFormals.length (${typeFormals.length})");
}
if (argumentTypes.isEmpty) {
return this;
}
// Given:
// {U/T} <S> T -> S
// Where {U/T} represents the typeArguments (U) and typeParameters (T) list,
// and <S> represents the typeFormals.
//
// Now instantiate([V]), and the result should be:
// {U/T, V/S} T -> S.
List<DartType> newTypeArgs = <DartType>[];
newTypeArgs.addAll(typeArguments);
newTypeArgs.addAll(argumentTypes);
return new FunctionTypeImpl._(element, name, prunedTypedefs, newTypeArgs,
_explicitTypeParameters, _returnType, _parameters, true);
}
@override
bool isAssignableTo(DartType type) {
// A function type T may be assigned to a function type S, written T <=> S,
// iff T <: S.
return isSubtypeOf(type);
}
@override
bool isMoreSpecificThan(DartType type,
[bool withDynamic = false, Set<Element> visitedElements]) {
// Note: visitedElements is only used for breaking recursion in the type
// hierarchy; we don't use it when recursing into the function type.
return relate(
this,
type,
(DartType t, DartType s, _, __) =>
(t as TypeImpl).isMoreSpecificThan(s, withDynamic),
new TypeSystemImpl(null).instantiateToBounds);
}
@override
bool isSubtypeOf(DartType type) {
var typeSystem = new TypeSystemImpl(null);
return relate(
typeSystem.instantiateToBounds(this),
typeSystem.instantiateToBounds(type),
(DartType t, DartType s, _, __) => t.isAssignableTo(s),
typeSystem.instantiateToBounds);
}
@override
FunctionTypeImpl pruned(List<FunctionTypeAliasElement> prune) {
if (prune == null) {
return this;
} else if (prune.contains(element)) {
// Circularity found. Prune the type declaration.
return new CircularFunctionTypeImpl();
} else {
// There should never be a reason to prune a type that has already been
// pruned, since pruning is only done when expanding a function type
// alias, and function type aliases are always expanded by starting with
// base types.
assert(this.prunedTypedefs == null);
List<DartType> typeArgs = typeArguments
.map((DartType t) => (t as TypeImpl).pruned(prune))
.toList(growable: false);
return new FunctionTypeImpl._(element, name, prune, typeArgs,
_explicitTypeParameters, _returnType, _parameters, _isInstantiated);
}
}
@override
FunctionType substitute2(
List<DartType> argumentTypes, List<DartType> parameterTypes,
[List<FunctionTypeAliasElement> prune]) {
// Pruned types should only ever result from performing type variable
// substitution, and it doesn't make sense to substitute again after
// substituting once.
assert(this.prunedTypedefs == null);
if (argumentTypes.length != parameterTypes.length) {
throw new ArgumentError(
"argumentTypes.length (${argumentTypes.length}) != parameterTypes.length (${parameterTypes.length})");
}
Element element = this.element;
if (prune != null && prune.contains(element)) {
// Circularity found. Prune the type declaration.
return new CircularFunctionTypeImpl();
}
if (argumentTypes.length == 0) {
return this.pruned(prune);
}
List<DartType> typeArgs =
TypeImpl.substitute(typeArguments, argumentTypes, parameterTypes);
return new FunctionTypeImpl._(element, name, prune, typeArgs,
_explicitTypeParameters, _returnType, _parameters, _isInstantiated);
}
@override
FunctionTypeImpl substitute3(List<DartType> argumentTypes) =>
substitute2(argumentTypes, typeArguments);
/**
* Invokes [callback] for each parameter of [kind] with the parameter's [name]
* and type after any type parameters have been applied.
*/
void _forEachParameterType(
ParameterKind kind, callback(String name, DartType type)) {
List<ParameterElement> parameters = baseParameters;
if (parameters.isEmpty) {
return;
}
List<DartType> typeParameters =
TypeParameterTypeImpl.getTypes(this.typeParameters);
int length = parameters.length;
for (int i = 0; i < length; i++) {
ParameterElement parameter = parameters[i];
if (parameter.parameterKind == kind) {
TypeImpl type = parameter.type ?? DynamicTypeImpl.instance;
if (typeArguments.length != 0 &&
typeArguments.length == typeParameters.length) {
type = type.substitute2(typeArguments, typeParameters, newPrune);
} else {
type = type.pruned(newPrune);
}
callback(parameter.name, type);
}
}
}
void _freeVariablesInFunctionType(
FunctionType type, Set<TypeParameterType> free) {
// Make some fresh variables to avoid capture.
List<DartType> typeArgs = DartType.EMPTY_LIST;
if (type.typeFormals.isNotEmpty) {
typeArgs = new List<DartType>.from(type.typeFormals.map((e) =>
new TypeParameterTypeImpl(new TypeParameterElementImpl(e.name, -1))));