Skip to content

Commit 637e55d

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
authored andcommitted
[kernel] Add VariableDeclarations to represent formals of Typedefs
Change-Id: Ic1575dadb4fcf644dfdeb436612e2bed72d41a03 Reviewed-on: https://dart-review.googlesource.com/68083 Reviewed-by: Jens Johansen <jensj@google.com> Commit-Queue: Dmitry Stefantsov <dmitryas@google.com>
1 parent 4c455e7 commit 637e55d

File tree

14 files changed

+107
-70
lines changed

14 files changed

+107
-70
lines changed

pkg/analyzer/lib/src/kernel/resynthesize.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,14 @@ class KernelResynthesizer implements ElementResynthesizer {
413413
int positionalCount = type.positionalParameters.length;
414414
var positionalParameters =
415415
new List<kernel.VariableDeclaration>(positionalCount);
416+
var knownPositionalParameters = const <kernel.VariableDeclaration>[];
417+
if (type.typedefReference != null) {
418+
knownPositionalParameters =
419+
type.typedefReference.asTypedef.positionalParameters;
420+
}
416421
for (int i = 0; i < positionalCount; i++) {
417-
String name = i < type.positionalParameterNames.length
418-
? type.positionalParameterNames[i]
422+
String name = i < knownPositionalParameters.length
423+
? (knownPositionalParameters[i].name ?? '')
419424
: 'arg_$i';
420425
positionalParameters[i] = new kernel.VariableDeclaration(name,
421426
type: type.positionalParameters[i]);

pkg/front_end/lib/src/fasta/kernel/kernel_function_type_alias_builder.dart

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import 'package:kernel/ast.dart'
1111
FunctionType,
1212
InvalidType,
1313
TypeParameter,
14-
Typedef;
14+
Typedef,
15+
VariableDeclaration;
1516

16-
import 'package:kernel/type_algebra.dart' show substitute;
17+
import 'package:kernel/type_algebra.dart'
18+
show FreshTypeParameters, getFreshTypeParameters, substitute;
1719

1820
import '../fasta_codes.dart'
1921
show noLength, templateCyclicTypedef, templateTypeArgumentMismatch;
@@ -23,6 +25,7 @@ import '../problems.dart' show unhandled;
2325
import 'kernel_builder.dart'
2426
show
2527
FunctionTypeAliasBuilder,
28+
KernelFormalParameterBuilder,
2629
KernelFunctionTypeBuilder,
2730
KernelTypeBuilder,
2831
KernelTypeVariableBuilder,
@@ -51,7 +54,34 @@ class KernelFunctionTypeAliasBuilder
5154
super(metadata, name, typeVariables, type, parent, charOffset);
5255

5356
Typedef build(LibraryBuilder libraryBuilder) {
54-
return target..type ??= buildThisType(libraryBuilder);
57+
target..type ??= buildThisType(libraryBuilder);
58+
59+
if (type != null) {
60+
List<TypeParameter> typeParameters =
61+
new List<TypeParameter>(type.typeVariables?.length ?? 0);
62+
for (int i = 0; i < typeParameters.length; ++i) {
63+
KernelTypeVariableBuilder typeVariable = type.typeVariables[i];
64+
typeParameters[i] = typeVariable.parameter;
65+
}
66+
FreshTypeParameters freshTypeParameters =
67+
getFreshTypeParameters(typeParameters);
68+
target.typeParametersOfFunctionType
69+
.addAll(freshTypeParameters.freshTypeParameters);
70+
71+
if (type.formals != null) {
72+
for (KernelFormalParameterBuilder formal in type.formals) {
73+
VariableDeclaration parameter = formal.build(libraryBuilder);
74+
parameter.type = freshTypeParameters.substitute(parameter.type);
75+
if (formal.isNamed) {
76+
target.namedParameters.add(parameter);
77+
} else {
78+
target.positionalParameters.add(parameter);
79+
}
80+
}
81+
}
82+
}
83+
84+
return target;
5585
}
5686

5787
DartType buildThisType(LibraryBuilder library) {

pkg/front_end/lib/src/fasta/kernel/kernel_function_type_builder.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,13 @@ class KernelFunctionTypeBuilder extends FunctionTypeBuilder
4747
DartType builtReturnType =
4848
returnType?.build(library) ?? const DynamicType();
4949
List<DartType> positionalParameters = <DartType>[];
50-
List<String> positionalParameterNames = <String>[];
5150
List<NamedType> namedParameters;
5251
int requiredParameterCount = 0;
5352
if (formals != null) {
5453
for (KernelFormalParameterBuilder formal in formals) {
5554
DartType type = formal.type?.build(library) ?? const DynamicType();
5655
if (formal.isPositional) {
5756
positionalParameters.add(type);
58-
positionalParameterNames.add(formal.name ?? '');
5957
if (formal.isRequired) requiredParameterCount++;
6058
} else if (formal.isNamed) {
6159
namedParameters ??= <NamedType>[];
@@ -76,8 +74,7 @@ class KernelFunctionTypeBuilder extends FunctionTypeBuilder
7674
var type = new FunctionType(positionalParameters, builtReturnType,
7775
namedParameters: namedParameters ?? const <NamedType>[],
7876
typeParameters: typeParameters ?? const <TypeParameter>[],
79-
requiredParameterCount: requiredParameterCount,
80-
positionalParameterNames: positionalParameterNames);
77+
requiredParameterCount: requiredParameterCount);
8178
outlineListener?.store(charOffset, false, type: type);
8279
return type;
8380
}

pkg/kernel/binary.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ type CanonicalName {
131131

132132
type ComponentFile {
133133
UInt32 magic = 0x90ABCDEF;
134-
UInt32 formatVersion = 9;
134+
UInt32 formatVersion = 10;
135135
Library[] libraries;
136136
UriSource sourceMap;
137137
List<CanonicalName> canonicalNames;
@@ -253,6 +253,9 @@ type Typedef {
253253
List<Expression> annotations;
254254
List<TypeParameter> typeParameters;
255255
DartType type;
256+
List<TypeParameter> typeParametersOfFunctionType;
257+
List<VariableDeclaration> positionalParameters;
258+
List<VariableDeclaration> namedParameters;
256259
}
257260

258261
type Combinator {
@@ -1167,7 +1170,6 @@ type FunctionType extends DartType {
11671170
UInt totalParameterCount;
11681171
List<DartType> positionalParameters;
11691172
List<NamedDartType> namedParameters;
1170-
List<StringReference> positionalParameterNames;
11711173
CanonicalNameReference typedefReference;
11721174
DartType returnType;
11731175
}

pkg/kernel/lib/ast.dart

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,25 @@ class Typedef extends NamedNode implements FileUriNode {
581581
final List<TypeParameter> typeParameters;
582582
DartType type;
583583

584+
// The following two fields describe parameters of the underlying function
585+
// type. They are needed to keep such attributes as names and annotations.
586+
final List<TypeParameter> typeParametersOfFunctionType;
587+
final List<VariableDeclaration> positionalParameters;
588+
final List<VariableDeclaration> namedParameters;
589+
584590
Typedef(this.name, this.type,
585-
{Reference reference, this.fileUri, List<TypeParameter> typeParameters})
591+
{Reference reference,
592+
this.fileUri,
593+
List<TypeParameter> typeParameters,
594+
List<TypeParameter> typeParametersOfFunctionType,
595+
List<VariableDeclaration> positionalParameters,
596+
List<VariableDeclaration> namedParameters})
586597
: this.typeParameters = typeParameters ?? <TypeParameter>[],
598+
this.typeParametersOfFunctionType =
599+
typeParametersOfFunctionType ?? <TypeParameter>[],
600+
this.positionalParameters =
601+
positionalParameters ?? <VariableDeclaration>[],
602+
this.namedParameters = namedParameters ?? <VariableDeclaration>[],
587603
super(reference) {
588604
setParents(this.typeParameters, this);
589605
}
@@ -2224,8 +2240,7 @@ class PropertyGet extends Expression {
22242240
if (interfaceTarget != null) {
22252241
Class superclass = interfaceTarget.enclosingClass;
22262242
var receiverType = receiver.getStaticTypeAsInstanceOf(superclass, types);
2227-
return Substitution
2228-
.fromInterfaceType(receiverType)
2243+
return Substitution.fromInterfaceType(receiverType)
22292244
.substituteType(interfaceTarget.getterType);
22302245
}
22312246
// Treat the properties of Object specially.
@@ -2342,8 +2357,7 @@ class DirectPropertyGet extends Expression {
23422357
DartType getStaticType(TypeEnvironment types) {
23432358
Class superclass = target.enclosingClass;
23442359
var receiverType = receiver.getStaticTypeAsInstanceOf(superclass, types);
2345-
return Substitution
2346-
.fromInterfaceType(receiverType)
2360+
return Substitution.fromInterfaceType(receiverType)
23472361
.substituteType(target.getterType);
23482362
}
23492363
}
@@ -2446,11 +2460,10 @@ class DirectMethodInvocation extends InvocationExpression {
24462460
}
24472461
Class superclass = target.enclosingClass;
24482462
var receiverType = receiver.getStaticTypeAsInstanceOf(superclass, types);
2449-
var returnType = Substitution
2450-
.fromInterfaceType(receiverType)
2463+
var returnType = Substitution.fromInterfaceType(receiverType)
24512464
.substituteType(target.function.returnType);
2452-
return Substitution
2453-
.fromPairs(target.function.typeParameters, arguments.types)
2465+
return Substitution.fromPairs(
2466+
target.function.typeParameters, arguments.types)
24542467
.substituteType(returnType);
24552468
}
24562469
}
@@ -2481,8 +2494,7 @@ class SuperPropertyGet extends Expression {
24812494
}
24822495
var receiver =
24832496
types.hierarchy.getTypeAsInstanceOf(types.thisType, declaringClass);
2484-
return Substitution
2485-
.fromInterfaceType(receiver)
2497+
return Substitution.fromInterfaceType(receiver)
24862498
.substituteType(interfaceTarget.getterType);
24872499
}
24882500

@@ -2727,12 +2739,11 @@ class MethodInvocation extends InvocationExpression {
27272739
}
27282740
Class superclass = interfaceTarget.enclosingClass;
27292741
var receiverType = receiver.getStaticTypeAsInstanceOf(superclass, types);
2730-
var getterType = Substitution
2731-
.fromInterfaceType(receiverType)
2742+
var getterType = Substitution.fromInterfaceType(receiverType)
27322743
.substituteType(interfaceTarget.getterType);
27332744
if (getterType is FunctionType) {
2734-
return Substitution
2735-
.fromPairs(getterType.typeParameters, arguments.types)
2745+
return Substitution.fromPairs(
2746+
getterType.typeParameters, arguments.types)
27362747
.substituteType(getterType.returnType);
27372748
} else {
27382749
return const DynamicType();
@@ -2744,8 +2755,8 @@ class MethodInvocation extends InvocationExpression {
27442755
if (receiverType.typeParameters.length != arguments.types.length) {
27452756
return const BottomType();
27462757
}
2747-
return Substitution
2748-
.fromPairs(receiverType.typeParameters, arguments.types)
2758+
return Substitution.fromPairs(
2759+
receiverType.typeParameters, arguments.types)
27492760
.substituteType(receiverType.returnType);
27502761
}
27512762
}
@@ -2806,11 +2817,10 @@ class SuperMethodInvocation extends InvocationExpression {
28062817
Class superclass = interfaceTarget.enclosingClass;
28072818
var receiverType =
28082819
types.hierarchy.getTypeAsInstanceOf(types.thisType, superclass);
2809-
var returnType = Substitution
2810-
.fromInterfaceType(receiverType)
2820+
var returnType = Substitution.fromInterfaceType(receiverType)
28112821
.substituteType(interfaceTarget.function.returnType);
2812-
return Substitution
2813-
.fromPairs(interfaceTarget.function.typeParameters, arguments.types)
2822+
return Substitution.fromPairs(
2823+
interfaceTarget.function.typeParameters, arguments.types)
28142824
.substituteType(returnType);
28152825
}
28162826

@@ -2859,8 +2869,8 @@ class StaticInvocation extends InvocationExpression {
28592869
}
28602870

28612871
DartType getStaticType(TypeEnvironment types) {
2862-
return Substitution
2863-
.fromPairs(target.function.typeParameters, arguments.types)
2872+
return Substitution.fromPairs(
2873+
target.function.typeParameters, arguments.types)
28642874
.substituteType(target.function.returnType);
28652875
}
28662876

@@ -2951,8 +2961,7 @@ class Instantiation extends Expression {
29512961

29522962
DartType getStaticType(TypeEnvironment types) {
29532963
FunctionType type = expression.getStaticType(types);
2954-
return Substitution
2955-
.fromPairs(type.typeParameters, typeArguments)
2964+
return Substitution.fromPairs(type.typeParameters, typeArguments)
29562965
.substituteType(type.withoutTypeParameters);
29572966
}
29582967

@@ -4804,11 +4813,6 @@ class FunctionType extends DartType {
48044813
final List<DartType> positionalParameters;
48054814
final List<NamedType> namedParameters; // Must be sorted.
48064815

4807-
/// The optional names of [positionalParameters], not `null`, but might be
4808-
/// empty if information is not available.
4809-
@informative
4810-
final List<String> positionalParameterNames;
4811-
48124816
/// The [Typedef] this function type is created for.
48134817
@nocoq
48144818
Reference typedefReference;
@@ -4820,7 +4824,6 @@ class FunctionType extends DartType {
48204824
{this.namedParameters: const <NamedType>[],
48214825
this.typeParameters: const <TypeParameter>[],
48224826
int requiredParameterCount,
4823-
this.positionalParameterNames: const <String>[],
48244827
this.typedefReference})
48254828
: this.positionalParameters = positionalParameters,
48264829
this.requiredParameterCount =

pkg/kernel/lib/binary/ast_from_binary.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,11 @@ class BinaryBuilder {
867867
node.annotations = readAnnotationList(node);
868868
readAndPushTypeParameterList(node.typeParameters, node);
869869
var type = readDartType();
870+
readAndPushTypeParameterList(node.typeParametersOfFunctionType, node);
871+
node.positionalParameters.addAll(readAndPushVariableDeclarationList());
872+
node.namedParameters.addAll(readAndPushVariableDeclarationList());
870873
typeParameterStack.length = 0;
874+
variableStack.length = 0;
871875
if (shouldWriteData) {
872876
node.fileOffset = fileOffset;
873877
node.name = name;
@@ -1828,7 +1832,6 @@ class BinaryBuilder {
18281832
var totalParameterCount = readUInt();
18291833
var positional = readDartTypeList();
18301834
var named = readNamedTypeList();
1831-
var positionalNames = readStringReferenceList();
18321835
var typedefReference = readTypedefReference();
18331836
assert(positional.length + named.length == totalParameterCount);
18341837
var returnType = readDartType();
@@ -1837,14 +1840,11 @@ class BinaryBuilder {
18371840
typeParameters: typeParameters,
18381841
requiredParameterCount: requiredParameterCount,
18391842
namedParameters: named,
1840-
positionalParameterNames: positionalNames,
18411843
typedefReference: typedefReference);
18421844
case Tag.SimpleFunctionType:
18431845
var positional = readDartTypeList();
1844-
var positionalNames = readStringReferenceList();
18451846
var returnType = readDartType();
1846-
return new FunctionType(positional, returnType,
1847-
positionalParameterNames: positionalNames);
1847+
return new FunctionType(positional, returnType);
18481848
case Tag.TypeParameterType:
18491849
int index = readUInt();
18501850
var bound = readDartTypeOption();

pkg/kernel/lib/binary/ast_to_binary.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
689689
}
690690

691691
void visitTypedef(Typedef node) {
692+
_variableIndexer ??= new VariableIndexer();
692693
writeCanonicalNameReference(getCanonicalNameOfTypedef(node));
693694

694695
final Uri activeFileUriSaved = _activeFileUri;
@@ -697,10 +698,18 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
697698
writeOffset(node.fileOffset);
698699
writeStringReference(node.name);
699700
writeAnnotationList(node.annotations);
700-
enterScope(typeParameters: node.typeParameters);
701+
702+
enterScope(typeParameters: node.typeParameters, variableScope: true);
701703
writeNodeList(node.typeParameters);
702704
writeNode(node.type);
703-
leaveScope(typeParameters: node.typeParameters);
705+
706+
enterScope(typeParameters: node.typeParametersOfFunctionType);
707+
writeNodeList(node.typeParametersOfFunctionType);
708+
writeVariableDeclarationList(node.positionalParameters);
709+
writeVariableDeclarationList(node.namedParameters);
710+
711+
leaveScope(typeParameters: node.typeParametersOfFunctionType);
712+
leaveScope(typeParameters: node.typeParameters, variableScope: true);
704713

705714
_activeFileUri = activeFileUriSaved;
706715
}
@@ -1673,7 +1682,6 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
16731682
node.typedefReference == null) {
16741683
writeByte(Tag.SimpleFunctionType);
16751684
writeNodeList(node.positionalParameters);
1676-
writeStringReferenceList(node.positionalParameterNames);
16771685
writeNode(node.returnType);
16781686
} else {
16791687
writeByte(Tag.FunctionType);
@@ -1684,7 +1692,6 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
16841692
node.positionalParameters.length + node.namedParameters.length);
16851693
writeNodeList(node.positionalParameters);
16861694
writeNodeList(node.namedParameters);
1687-
writeStringReferenceList(node.positionalParameterNames);
16881695
writeReference(node.typedefReference);
16891696
writeNode(node.returnType);
16901697
leaveScope(typeParameters: node.typeParameters);

pkg/kernel/lib/binary/tag.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class Tag {
135135
/// Internal version of kernel binary format.
136136
/// Bump it when making incompatible changes in kernel binaries.
137137
/// Keep in sync with runtime/vm/kernel_binary.h, pkg/kernel/binary.md.
138-
static const int BinaryFormatVersion = 9;
138+
static const int BinaryFormatVersion = 10;
139139
}
140140

141141
abstract class ConstantTag {

pkg/kernel/lib/type_algebra.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ class FreshTypeParameters {
248248
namedParameters: type.namedParameters.map(substituteNamed).toList(),
249249
typeParameters: freshTypeParameters,
250250
requiredParameterCount: type.requiredParameterCount,
251-
positionalParameterNames: type.positionalParameterNames,
252251
typedefReference: type.typedefReference);
253252

254253
DartType substitute(DartType type) => substitution.substituteType(type);

pkg/kernel/lib/type_environment.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ class TypeEnvironment extends SubtypeTester {
204204
namedParameters: replacedNamedParameters,
205205
typeParameters: type.typeParameters,
206206
requiredParameterCount: type.requiredParameterCount,
207-
positionalParameterNames: type.positionalParameterNames,
208207
typedefReference: type.typedefReference);
209208
}
210209
return type;

0 commit comments

Comments
 (0)