/
resolution_types.dart
1839 lines (1592 loc) · 61 KB
/
resolution_types.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) 2012, 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.
/// Implementation of the Dart types hierarchy in 'types.dart' specifically
/// tailored to the resolution phase of the compiler.
library resolution_types;
import 'dart:math' show min;
import '../common.dart';
import '../common/resolution.dart' show Resolution;
import '../common_elements.dart';
import '../ordered_typeset.dart' show OrderedTypeSet;
import '../util/util.dart' show equalElements;
import 'elements.dart';
import 'entities.dart';
import 'modelx.dart' show TypeDeclarationElementX;
import 'names.dart';
import 'types.dart';
enum ResolutionTypeKind {
FUNCTION,
INTERFACE,
TYPEDEF,
TYPE_VARIABLE,
MALFORMED_TYPE,
DYNAMIC,
VOID,
}
abstract class ResolutionDartType implements DartType {
String get name;
ResolutionTypeKind get kind;
const ResolutionDartType();
/**
* Returns the [Element] which declared this type.
*
* This can be [ClassElement] for classes, [TypedefElement] for typedefs,
* [TypeVariableElement] for type variables and [FunctionElement] for
* function types.
*
* Invariant: [element] must be a declaration element.
*/
Element get element;
/**
* Performs the substitution [: [arguments[i]/parameters[i]]this :].
*
* The notation is known from this lambda calculus rule:
*
* (lambda x.e0)e1 -> [e1/x]e0.
*
* See [ResolutionTypeVariableType] for a motivation for this method.
*
* Invariant: There must be the same number of [arguments] and [parameters].
*/
ResolutionDartType subst(covariant List<ResolutionDartType> arguments,
covariant List<ResolutionDartType> parameters);
/// Performs the substitution of the type arguments of [type] for their
/// corresponding type variables in this type.
ResolutionDartType substByContext(GenericType type) {
return subst(type.typeArguments, type.element.typeVariables);
}
/// Computes the unaliased type of this type.
///
/// The unaliased type of a typedef'd type is the unaliased type to which its
/// name is bound. The unaliased version of any other type is the type itself.
///
/// For example, the unaliased type of `typedef A Func<A,B>(B b)` is the
/// function type `(B) -> A` and the unaliased type of `Func<int,String>`
/// is the function type `(String) -> int`.
// TODO(johnniwinther): Maybe move this to [TypedefType].
void computeUnaliased(Resolution resolution) {}
/// Returns the unaliased type of this type.
///
/// The unaliased type of a typedef'd type is the unaliased type to which its
/// name is bound. The unaliased version of any other type is the type itself.
///
/// For example, the unaliased type of `typedef A Func<A,B>(B b)` is the
/// function type `(B) -> A` and the unaliased type of `Func<int,String>`
/// is the function type `(String) -> int`.
ResolutionDartType get unaliased => this;
/**
* If this type is malformed or a generic type created with the wrong number
* of type arguments then [userProvidedBadType] holds the bad type provided
* by the user.
*/
ResolutionDartType get userProvidedBadType => null;
/// Is [: true :] if this type has no explicit type arguments.
bool get isRaw => true;
/// Returns the raw version of this type.
ResolutionDartType asRaw() => this;
/// Is [: true :] if this type has no non-dynamic type arguments.
bool get treatAsRaw => isRaw;
/// Is [: true :] if this type should be treated as the dynamic type.
bool get treatAsDynamic => false;
/// Is [: true :] if this type is the dynamic type.
bool get isDynamic => kind == ResolutionTypeKind.DYNAMIC;
/// Is [: true :] if this type is the void type.
bool get isVoid => kind == ResolutionTypeKind.VOID;
/// Is [: true :] if this is the type of `Object` from dart:core.
bool get isObject => false;
/// Is [: true :] if this type is an interface type.
bool get isInterfaceType => kind == ResolutionTypeKind.INTERFACE;
/// Is [: true :] if this type is a typedef.
bool get isTypedef => kind == ResolutionTypeKind.TYPEDEF;
/// Is [: true :] if this type is a function type.
bool get isFunctionType => kind == ResolutionTypeKind.FUNCTION;
/// Is [: true :] if this type is a type variable.
bool get isTypeVariable => kind == ResolutionTypeKind.TYPE_VARIABLE;
/// Is [: true :] if this type is a malformed type.
bool get isMalformed => false;
/// Is `true` if this type is declared by an enum.
bool get isEnumType => false;
/// Returns an occurrence of a type variable within this type, if any.
ResolutionTypeVariableType get typeVariableOccurrence => null;
/// Applies [f] to each occurence of a [ResolutionTypeVariableType] within
/// this type.
void forEachTypeVariable(f(ResolutionTypeVariableType variable)) {}
ResolutionTypeVariableType _findTypeVariableOccurrence(
List<ResolutionDartType> types) {
for (ResolutionDartType type in types) {
ResolutionTypeVariableType typeVariable = type.typeVariableOccurrence;
if (typeVariable != null) {
return typeVariable;
}
}
return null;
}
/// Is [: true :] if this type contains any type variables.
bool get containsTypeVariables => typeVariableOccurrence != null;
/// Returns a textual representation of this type as if it was the type
/// of a member named [name].
String getStringAsDeclared(String name) {
return new TypeDeclarationFormatter().format(this, name);
}
R accept<R, A>(covariant ResolutionDartTypeVisitor<R, A> visitor, A argument);
void visitChildren<R, A>(
ResolutionDartTypeVisitor<R, A> visitor, A argument) {}
static void visitList<R, A>(List<ResolutionDartType> types,
ResolutionDartTypeVisitor<R, A> visitor, A argument) {
for (ResolutionDartType type in types) {
type.accept(visitor, argument);
}
}
/// Returns a [ResolutionDartType] which corresponds to [this] except that
/// each contained [MethodTypeVariableType] is replaced by a
/// [ResolutionDynamicType].
/// GENERIC_METHODS: Temporary, only used with '--generic-method-syntax'.
ResolutionDartType get dynamifyMethodTypeVariableType => this;
/// Returns true iff [this] is or contains a [MethodTypeVariableType].
/// GENERIC_METHODS: Temporary, only used with '--generic-method-syntax'
bool get containsMethodTypeVariableType => false;
}
/**
* Represents a type variable, that is the type parameters of a class type.
*
* For example, in [: class Array<E> { ... } :], E is a type variable.
*
* Each class should have its own unique type variables, one for each type
* parameter. A class with type parameters is said to be parameterized or
* generic.
*
* Non-static members, constructors, and factories of generic
* class/interface can refer to type variables of the current class
* (not of supertypes).
*
* When using a generic type, also known as an application or
* instantiation of the type, the actual type arguments should be
* substituted for the type variables in the class declaration.
*
* For example, given a box, [: class Box<T> { T value; } :], the
* type of the expression [: new Box<String>().value :] is
* [: String :] because we must substitute [: String :] for the
* the type variable [: T :].
*/
class ResolutionTypeVariableType extends ResolutionDartType
implements TypeVariableType {
final TypeVariableElement element;
ResolutionTypeVariableType(this.element);
ResolutionTypeKind get kind => ResolutionTypeKind.TYPE_VARIABLE;
String get name => element.name;
ResolutionDartType subst(covariant List<ResolutionDartType> arguments,
covariant List<ResolutionDartType> parameters) {
assert(arguments.length == parameters.length);
if (parameters.isEmpty) {
// Return fast on empty substitutions.
return this;
}
for (int index = 0; index < arguments.length; index++) {
ResolutionTypeVariableType parameter = parameters[index];
ResolutionDartType argument = arguments[index];
if (parameter == this) {
return argument;
}
}
// The type variable was not substituted.
return this;
}
ResolutionTypeVariableType get typeVariableOccurrence => this;
void forEachTypeVariable(f(ResolutionTypeVariableType variable)) {
f(this);
}
R accept<R, A>(DartTypeVisitor<R, A> visitor, A argument) {
return visitor.visitTypeVariableType(this, argument);
}
int get hashCode => 17 * element.hashCode;
bool operator ==(other) {
if (other is! ResolutionTypeVariableType) return false;
return identical(other.element, element);
}
String toString() => '${element.typeDeclaration.name}.$name';
}
/// Provides a thin model of method type variables: They are treated as if
/// their value were `dynamic` when used in a type annotation, and as a
/// malformed type when used in an `as` or `is` expression.
class MethodTypeVariableType extends ResolutionTypeVariableType {
MethodTypeVariableType(TypeVariableElement element) : super(element);
@override
bool get treatAsDynamic => true;
@override
bool get isMalformed => true;
@override
ResolutionDartType get dynamifyMethodTypeVariableType =>
const ResolutionDynamicType();
@override
get containsMethodTypeVariableType => true;
}
class ResolutionVoidType extends ResolutionDartType implements VoidType {
const ResolutionVoidType();
ResolutionTypeKind get kind => ResolutionTypeKind.VOID;
String get name => 'void';
Element get element => null;
ResolutionDartType subst(covariant List<ResolutionDartType> arguments,
covariant List<ResolutionDartType> parameters) {
// Void cannot be substituted.
return this;
}
R accept<R, A>(DartTypeVisitor<R, A> visitor, A argument) {
return visitor.visitVoidType(this, argument);
}
String toString() => name;
int get hashCode => 6007;
}
class MalformedType extends ResolutionDartType {
final ErroneousElement element;
/**
* [declaredType] holds the type which the user wrote in code.
*
* For instance, for a resolved but malformed type like [: Map<String> :] the
* [declaredType] is [: Map<String> :] whereas for an unresolved type
* [userProvidedBadType] is [: null :].
*/
final ResolutionDartType userProvidedBadType;
/**
* Type arguments for the malformed typed, if these cannot fit in the
* [declaredType].
*
* This field is for instance used for [: dynamic<int> :] and [: T<int> :]
* where [: T :] is a type variable, in which case [declaredType] holds
* [: dynamic :] and [: T :], respectively, or for [: X<int> :] where [: X :]
* is not resolved or does not imply a type.
*/
final List<ResolutionDartType> typeArguments;
final int hashCode = _nextHash = (_nextHash + 1).toUnsigned(30);
static int _nextHash = 43765;
MalformedType(this.element, this.userProvidedBadType,
[this.typeArguments = null]);
ResolutionTypeKind get kind => ResolutionTypeKind.MALFORMED_TYPE;
String get name => element.name;
ResolutionDartType subst(covariant List<ResolutionDartType> arguments,
covariant List<ResolutionDartType> parameters) {
// Malformed types are not substitutable.
return this;
}
// Malformed types are treated as dynamic.
bool get treatAsDynamic => true;
@override
bool get isMalformed => true;
R accept<R, A>(
covariant ResolutionDartTypeVisitor<R, A> visitor, A argument) {
return visitor.visitMalformedType(this, argument);
}
String toString() {
var sb = new StringBuffer();
if (typeArguments != null) {
if (userProvidedBadType != null) {
sb.write(userProvidedBadType.name);
} else {
sb.write(element.name);
}
if (!typeArguments.isEmpty) {
sb.write('<');
sb.write(typeArguments.join(', '));
sb.write('>');
}
} else {
sb.write(userProvidedBadType.toString());
}
return sb.toString();
}
}
abstract class GenericType extends ResolutionDartType {
final TypeDeclarationElement element;
final List<ResolutionDartType> typeArguments;
GenericType(
TypeDeclarationElement element, List<ResolutionDartType> typeArguments,
{bool checkTypeArgumentCount: true})
: this.element = element,
this.typeArguments = typeArguments,
this.containsMethodTypeVariableType =
typeArguments.any(_typeContainsMethodTypeVariableType) {
assert(
element != null,
failedAt(
CURRENT_ELEMENT_SPANNABLE, "Missing element for generic type."));
assert(() {
if (!checkTypeArgumentCount) return true;
if (element is TypeDeclarationElementX) {
return element.thisTypeCache == null ||
typeArguments.length == element.typeVariables.length;
}
return true;
},
failedAt(
element,
'Invalid type argument count on ${element.thisType}. '
'Provided type arguments: $typeArguments.'));
}
/// Creates a new instance of this type using the provided type arguments.
GenericType createInstantiation(List<ResolutionDartType> newTypeArguments);
GenericType subst(covariant List<ResolutionDartType> arguments,
covariant List<ResolutionDartType> parameters) {
if (typeArguments.isEmpty) {
// Return fast on non-generic types.
return this;
}
if (parameters.isEmpty) {
assert(arguments.isEmpty);
// Return fast on empty substitutions.
return this;
}
List<ResolutionDartType> newTypeArguments =
Types.substTypes(typeArguments, arguments, parameters);
if (!identical(typeArguments, newTypeArguments)) {
// Create a new type only if necessary.
return createInstantiation(newTypeArguments);
}
return this;
}
ResolutionTypeVariableType get typeVariableOccurrence {
return _findTypeVariableOccurrence(typeArguments);
}
void forEachTypeVariable(f(ResolutionTypeVariableType variable)) {
for (ResolutionDartType type in typeArguments) {
type.forEachTypeVariable(f);
}
}
void visitChildren<R, A>(
ResolutionDartTypeVisitor<R, A> visitor, var argument) {
ResolutionDartType.visitList(typeArguments, visitor, argument);
}
String toString() {
StringBuffer sb = new StringBuffer();
sb.write(name);
if (!isRaw) {
sb.write('<');
sb.write(typeArguments.join(', '));
sb.write('>');
}
return sb.toString();
}
@override
final bool containsMethodTypeVariableType;
@override
ResolutionDartType get dynamifyMethodTypeVariableType {
if (!containsMethodTypeVariableType) return this;
List<ResolutionDartType> newTypeArguments = typeArguments
.map((ResolutionDartType type) => type.dynamifyMethodTypeVariableType)
.toList();
return createInstantiation(newTypeArguments);
}
int get hashCode {
int hash = element.hashCode;
for (ResolutionDartType argument in typeArguments) {
int argumentHash = argument != null ? argument.hashCode : 0;
hash = 17 * hash + 3 * argumentHash;
}
return hash;
}
bool operator ==(other) {
if (other is! GenericType) return false;
return kind == other.kind &&
element == other.element &&
equalElements(typeArguments, other.typeArguments);
}
/// Returns `true` if the declaration of this type has type variables.
bool get isGeneric => !typeArguments.isEmpty;
bool get isRaw => typeArguments.isEmpty || identical(this, element.rawType);
GenericType asRaw() => element.rawType;
bool get treatAsRaw {
if (isRaw) return true;
for (ResolutionDartType type in typeArguments) {
if (!type.treatAsDynamic) return false;
}
return true;
}
}
class ResolutionInterfaceType extends GenericType implements InterfaceType {
int _hashCode;
ResolutionInterfaceType(ClassElement element,
[List<ResolutionDartType> typeArguments = const <ResolutionDartType>[]])
: super(element, typeArguments) {
assert(element.isDeclaration, failedAt(element));
}
ResolutionInterfaceType.forUserProvidedBadType(ClassElement element,
[List<ResolutionDartType> typeArguments = const <ResolutionDartType>[]])
: super(element, typeArguments, checkTypeArgumentCount: false);
ClassElement get element => super.element;
ResolutionTypeKind get kind => ResolutionTypeKind.INTERFACE;
String get name => element.name;
bool get isObject => element.isObject;
bool get isEnumType => element.isEnumClass;
ResolutionInterfaceType createInstantiation(
List<ResolutionDartType> newTypeArguments) {
return new ResolutionInterfaceType(element, newTypeArguments);
}
/**
* Returns the type as an instance of class [other], if possible, null
* otherwise.
*/
ResolutionInterfaceType asInstanceOf(ClassElement other) {
other = other.declaration;
if (element == other) return this;
ResolutionInterfaceType supertype = element.asInstanceOf(other);
if (supertype != null) {
List<ResolutionDartType> arguments = Types.substTypes(
supertype.typeArguments, typeArguments, element.typeVariables);
return new ResolutionInterfaceType(supertype.element, arguments);
}
return null;
}
MemberSignature lookupInterfaceMember(Name name) {
MemberSignature member = element.lookupInterfaceMember(name);
if (member != null && isGeneric) {
return new InterfaceMember(this, member);
}
return member;
}
MemberSignature lookupClassMember(Name name) {
MemberSignature member = element.lookupClassMember(name);
if (member != null && isGeneric) {
return new InterfaceMember(this, member);
}
return member;
}
int get hashCode => _hashCode ??= super.hashCode;
ResolutionInterfaceType asRaw() => super.asRaw();
R accept<R, A>(DartTypeVisitor<R, A> visitor, A argument) {
return visitor.visitInterfaceType(this, argument);
}
/// Returns the type of the 'call' method in this interface type, or
/// `null` if the interface type has no 'call' method.
ResolutionFunctionType get callType {
ResolutionFunctionType type = element.callType;
return type != null && isGeneric ? type.substByContext(this) : type;
}
ResolutionInterfaceType subst(covariant List<ResolutionDartType> arguments,
covariant List<ResolutionDartType> parameters) {
return super.subst(arguments, parameters);
}
}
/// Special subclass of [ResolutionInterfaceType] used for generic interface
/// types created with the wrong number of type arguments.
///
/// The type uses `dynamic` for all it s type arguments.
class BadInterfaceType extends ResolutionInterfaceType {
final ResolutionInterfaceType userProvidedBadType;
BadInterfaceType(
ClassElement element, ResolutionInterfaceType this.userProvidedBadType)
: super(element, element.rawType.typeArguments);
String toString() {
return userProvidedBadType.toString();
}
}
/**
* Special subclass of [ResolutionTypedefType] used for generic typedef types
* created with the wrong number of type arguments.
*
* The type uses [:dynamic:] for all it s type arguments.
*/
class BadTypedefType extends ResolutionTypedefType {
final ResolutionTypedefType userProvidedBadType;
BadTypedefType(
TypedefElement element, ResolutionTypedefType this.userProvidedBadType)
: super(element, element.rawType.typeArguments);
String toString() {
return userProvidedBadType.toString();
}
}
class ResolutionFunctionType extends ResolutionDartType
implements FunctionType {
final FunctionTypedElement element;
final ResolutionDartType returnType;
final List<ResolutionDartType> parameterTypes;
final List<ResolutionDartType> optionalParameterTypes;
/**
* The names of the named parameters ordered lexicographically.
*/
final List<String> namedParameters;
/**
* The types of the named parameters in the order corresponding to the
* [namedParameters].
*/
final List<ResolutionDartType> namedParameterTypes;
factory ResolutionFunctionType(FunctionTypedElement element,
[ResolutionDartType returnType = const ResolutionDynamicType(),
List<ResolutionDartType> parameterTypes = const <ResolutionDartType>[],
List<ResolutionDartType> optionalParameterTypes =
const <ResolutionDartType>[],
List<String> namedParameters = const <String>[],
List<ResolutionDartType> namedParameterTypes =
const <ResolutionDartType>[]]) {
assert(element != null, failedAt(CURRENT_ELEMENT_SPANNABLE));
assert(element.isDeclaration, failedAt(element));
return new ResolutionFunctionType.internal(
element,
returnType,
parameterTypes,
optionalParameterTypes,
namedParameters,
namedParameterTypes);
}
factory ResolutionFunctionType.synthesized(
[ResolutionDartType returnType = const ResolutionDynamicType(),
List<ResolutionDartType> parameterTypes = const <ResolutionDartType>[],
List<ResolutionDartType> optionalParameterTypes =
const <ResolutionDartType>[],
List<String> namedParameters = const <String>[],
List<ResolutionDartType> namedParameterTypes =
const <ResolutionDartType>[]]) {
return new ResolutionFunctionType.internal(null, returnType, parameterTypes,
optionalParameterTypes, namedParameters, namedParameterTypes);
}
factory ResolutionFunctionType.generalized(
ResolutionDartType returnType,
List<ResolutionDartType> parameterTypes,
List<ResolutionDartType> optionalParameterTypes,
List<String> namedParameters,
List<ResolutionDartType> namedParameterTypes) {
return new ResolutionFunctionType.internal(null, returnType, parameterTypes,
optionalParameterTypes, namedParameters, namedParameterTypes);
}
ResolutionFunctionType.internal(FunctionTypedElement this.element,
[ResolutionDartType returnType = const ResolutionDynamicType(),
List<ResolutionDartType> parameterTypes = const <ResolutionDartType>[],
List<ResolutionDartType> optionalParameterTypes =
const <ResolutionDartType>[],
List<String> namedParameters = const <String>[],
List<ResolutionDartType> namedParameterTypes =
const <ResolutionDartType>[]])
: this.returnType = returnType,
this.parameterTypes = parameterTypes,
this.optionalParameterTypes = optionalParameterTypes,
this.namedParameters = namedParameters,
this.namedParameterTypes = namedParameterTypes,
this.containsMethodTypeVariableType = returnType
.containsMethodTypeVariableType ||
parameterTypes.any(_typeContainsMethodTypeVariableType) ||
optionalParameterTypes.any(_typeContainsMethodTypeVariableType) ||
namedParameterTypes.any(_typeContainsMethodTypeVariableType) {
assert(element == null || element.isDeclaration,
failedAt(CURRENT_ELEMENT_SPANNABLE));
// Assert that optional and named parameters are not used at the same time.
assert(optionalParameterTypes.isEmpty || namedParameterTypes.isEmpty);
assert(namedParameters.length == namedParameterTypes.length);
}
ResolutionTypeKind get kind => ResolutionTypeKind.FUNCTION;
ResolutionDartType getNamedParameterType(String name) {
for (int i = 0; i < namedParameters.length; i++) {
if (namedParameters[i] == name) {
return namedParameterTypes[i];
}
}
return null;
}
ResolutionDartType subst(covariant List<ResolutionDartType> arguments,
covariant List<ResolutionDartType> parameters) {
if (parameters.isEmpty) {
assert(arguments.isEmpty);
// Return fast on empty substitutions.
return this;
}
ResolutionDartType newReturnType = returnType.subst(arguments, parameters);
bool changed = !identical(newReturnType, returnType);
List<ResolutionDartType> newParameterTypes =
Types.substTypes(parameterTypes, arguments, parameters);
List<ResolutionDartType> newOptionalParameterTypes =
Types.substTypes(optionalParameterTypes, arguments, parameters);
List<ResolutionDartType> newNamedParameterTypes =
Types.substTypes(namedParameterTypes, arguments, parameters);
if (!changed &&
(!identical(parameterTypes, newParameterTypes) ||
!identical(optionalParameterTypes, newOptionalParameterTypes) ||
!identical(namedParameterTypes, newNamedParameterTypes))) {
changed = true;
}
if (changed) {
// Create a new type only if necessary.
return new ResolutionFunctionType.internal(
element,
newReturnType,
newParameterTypes,
newOptionalParameterTypes,
namedParameters,
newNamedParameterTypes);
}
return this;
}
ResolutionTypeVariableType get typeVariableOccurrence {
ResolutionTypeVariableType typeVariableType =
returnType.typeVariableOccurrence;
if (typeVariableType != null) return typeVariableType;
typeVariableType = _findTypeVariableOccurrence(parameterTypes);
if (typeVariableType != null) return typeVariableType;
typeVariableType = _findTypeVariableOccurrence(optionalParameterTypes);
if (typeVariableType != null) return typeVariableType;
return _findTypeVariableOccurrence(namedParameterTypes);
}
void forEachTypeVariable(f(ResolutionTypeVariableType variable)) {
returnType.forEachTypeVariable(f);
parameterTypes.forEach((ResolutionDartType type) {
type.forEachTypeVariable(f);
});
optionalParameterTypes.forEach((ResolutionDartType type) {
type.forEachTypeVariable(f);
});
namedParameterTypes.forEach((ResolutionDartType type) {
type.forEachTypeVariable(f);
});
}
R accept<R, A>(covariant DartTypeVisitor<R, A> visitor, A argument) {
return visitor.visitFunctionType(this, argument);
}
void visitChildren<R, A>(
ResolutionDartTypeVisitor<R, A> visitor, var argument) {
returnType.accept(visitor, argument);
ResolutionDartType.visitList(parameterTypes, visitor, argument);
ResolutionDartType.visitList(optionalParameterTypes, visitor, argument);
ResolutionDartType.visitList(namedParameterTypes, visitor, argument);
}
String toString() {
StringBuffer sb = new StringBuffer();
sb.write('(');
sb.write(parameterTypes.join(', '));
bool first = parameterTypes.isEmpty;
if (!optionalParameterTypes.isEmpty) {
if (!first) {
sb.write(', ');
}
sb.write('[');
sb.write(optionalParameterTypes.join(', '));
sb.write(']');
first = false;
}
if (!namedParameterTypes.isEmpty) {
if (!first) {
sb.write(', ');
}
sb.write('{');
first = true;
for (int i = 0; i < namedParameters.length; i++) {
if (!first) {
sb.write(', ');
}
sb.write(namedParameterTypes[i]);
sb.write(' ');
sb.write(namedParameters[i]);
first = false;
}
sb.write('}');
}
sb.write(') -> ${returnType}');
return sb.toString();
}
String get name => 'Function';
@override
ResolutionDartType get dynamifyMethodTypeVariableType {
if (!containsMethodTypeVariableType) return this;
ResolutionDartType eraseIt(ResolutionDartType type) =>
type.dynamifyMethodTypeVariableType;
ResolutionDartType newReturnType =
returnType.dynamifyMethodTypeVariableType;
List<ResolutionDartType> newParameterTypes =
parameterTypes.map(eraseIt).toList();
List<ResolutionDartType> newOptionalParameterTypes =
optionalParameterTypes.map(eraseIt).toList();
List<ResolutionDartType> newNamedParameterTypes =
namedParameterTypes.map(eraseIt).toList();
return new ResolutionFunctionType.internal(
element,
newReturnType,
newParameterTypes,
newOptionalParameterTypes,
namedParameters,
newNamedParameterTypes);
}
@override
final bool containsMethodTypeVariableType;
int get hashCode {
int hash = 3 * returnType.hashCode;
for (ResolutionDartType parameter in parameterTypes) {
hash = 17 * hash + 5 * parameter.hashCode;
}
for (ResolutionDartType parameter in optionalParameterTypes) {
hash = 19 * hash + 7 * parameter.hashCode;
}
for (String name in namedParameters) {
hash = 23 * hash + 11 * name.hashCode;
}
for (ResolutionDartType parameter in namedParameterTypes) {
hash = 29 * hash + 13 * parameter.hashCode;
}
return hash;
}
bool operator ==(other) {
if (other is! ResolutionFunctionType) return false;
return returnType == other.returnType &&
equalElements(parameterTypes, other.parameterTypes) &&
equalElements(optionalParameterTypes, other.optionalParameterTypes) &&
equalElements(namedParameters, other.namedParameters) &&
equalElements(namedParameterTypes, other.namedParameterTypes);
}
}
bool _typeContainsMethodTypeVariableType(ResolutionDartType type) =>
type.containsMethodTypeVariableType;
class ResolutionTypedefType extends GenericType {
ResolutionDartType _unaliased;
ResolutionTypedefType(TypedefElement element,
[List<ResolutionDartType> typeArguments = const <ResolutionDartType>[]])
: super(element, typeArguments);
ResolutionTypedefType.forUserProvidedBadType(TypedefElement element,
[List<ResolutionDartType> typeArguments = const <ResolutionDartType>[]])
: super(element, typeArguments, checkTypeArgumentCount: false);
TypedefElement get element => super.element;
ResolutionTypeKind get kind => ResolutionTypeKind.TYPEDEF;
String get name => element.name;
ResolutionTypedefType createInstantiation(
List<ResolutionDartType> newTypeArguments) {
return new ResolutionTypedefType(element, newTypeArguments);
}
void computeUnaliased(Resolution resolution) {
if (_unaliased == null) {
element.ensureResolved(resolution);
if (element.isMalformed) {
_unaliased = const ResolutionDynamicType();
return;
}
element.checkCyclicReference(resolution);
element.alias.computeUnaliased(resolution);
_unaliased = element.alias.unaliased.substByContext(this);
}
}
ResolutionDartType get unaliased {
if (_unaliased == null) {
ResolutionDartType definition = element.alias.unaliased;
_unaliased = definition.substByContext(this);
}
return _unaliased;
}
int get hashCode => super.hashCode;
ResolutionTypedefType asRaw() => super.asRaw();
R accept<R, A>(
covariant ResolutionDartTypeVisitor<R, A> visitor, A argument) {
return visitor.visitTypedefType(this, argument);
}
}
/**
* Special type for the `dynamic` type.
*/
class ResolutionDynamicType extends ResolutionDartType implements DynamicType {
const ResolutionDynamicType();
Element get element => null;
String get name => 'dynamic';
bool get treatAsDynamic => true;
ResolutionTypeKind get kind => ResolutionTypeKind.DYNAMIC;
ResolutionDartType subst(covariant List<ResolutionDartType> arguments,
covariant List<ResolutionDartType> parameters) =>
this;
R accept<R, A>(DartTypeVisitor<R, A> visitor, A argument) {
return visitor.visitDynamicType(this, argument);
}
int get hashCode => 91;
String toString() => name;
}
/**
* [InterfaceMember] encapsulates a member (method, field, property) with
* the types of the declarer and receiver in order to do substitution on the
* member type.
*
* Consider for instance these classes and the variable `B<String> b`:
*
* class A<E> {
* E field;
* }
* class B<F> extends A<F> {}
*
* In an [InterfaceMember] for `b.field` the [receiver] is the type
* `B<String>` and the declarer is the type `A<F>`, which is the supertype of
* `B<F>` from which `field` has been inherited. To compute the type of
* `b.field` we must first substitute `E` by `F` using the relation between
* `A<E>` and `A<F>`, and then `F` by `String` using the relation between
* `B<F>` and `B<String>`.
*/
class InterfaceMember implements MemberSignature {
final ResolutionInterfaceType instance;
final MemberSignature member;
InterfaceMember(this.instance, this.member);
Name get name => member.name;
ResolutionDartType get type => member.type.substByContext(instance);
ResolutionFunctionType get functionType =>
member.functionType.substByContext(instance);
bool get isGetter => member.isGetter;
bool get isSetter => member.isSetter;
bool get isMethod => member.isMethod;
Iterable<Member> get declarations => member.declarations;
}
abstract class ResolutionDartTypeVisitor<R, A> extends DartTypeVisitor<R, A> {
const ResolutionDartTypeVisitor();
R visitMalformedType(MalformedType type, A argument) => null;
R visitTypedefType(ResolutionTypedefType type, A argument) => null;
}
abstract class BaseResolutionDartTypeVisitor<R, A>
extends BaseDartTypeVisitor<R, A>
implements ResolutionDartTypeVisitor<R, A> {