Skip to content

Commit 2e8c2a5

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Create explicit internal ast nodes for extension access
This adds explicit internal ast nodes for the various kinds of extension access in order to avoid lowering during body building and to make the encoding directly match the information from the source code. TEST=existing Change-Id: Ic831b2871ed0e66221a7ca5dadde4333647c2d3c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/446381 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]> Reviewed-by: Alexander Markov <[email protected]> Reviewed-by: Erik Ernst <[email protected]>
1 parent 1717621 commit 2e8c2a5

File tree

108 files changed

+6882
-2500
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+6882
-2500
lines changed

pkg/front_end/lib/src/kernel/body_builder.dart

Lines changed: 36 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,10 @@ class BodyBuilder extends StackListenerImpl
11371137
} else {
11381138
Expression value = toValue(node);
11391139
if (!forest.isThrow(node)) {
1140+
// TODO(johnniwinther): Derive the message position from the [node]
1141+
// and not the [value]. For instance this occurs for `super()?.foo()`
1142+
// in an initializer list, pointing to `foo` as expecting an
1143+
// initializer.
11401144
value = wrapInProblem(
11411145
value, cfe.codeExpectedAnInitializer, value.fileOffset, noLength);
11421146
}
@@ -1161,13 +1165,11 @@ class BodyBuilder extends StackListenerImpl
11611165
if (formal.isNamed) {
11621166
(superParametersAsArguments ??= <Object>[]).add(new NamedExpression(
11631167
formal.name,
1164-
createVariableGet(formal.variable!, formal.fileOffset,
1165-
forNullGuardedAccess: false))
1168+
createVariableGet(formal.variable!, formal.fileOffset))
11661169
..fileOffset = formal.fileOffset);
11671170
} else {
1168-
(superParametersAsArguments ??= <Object>[]).add(createVariableGet(
1169-
formal.variable!, formal.fileOffset,
1170-
forNullGuardedAccess: false));
1171+
(superParametersAsArguments ??= <Object>[])
1172+
.add(createVariableGet(formal.variable!, formal.fileOffset));
11711173
}
11721174
}
11731175
}
@@ -1853,18 +1855,16 @@ class BodyBuilder extends StackListenerImpl
18531855
if (formal.isNamed) {
18541856
NamedExpression superParameterAsArgument = new NamedExpression(
18551857
formal.name,
1856-
createVariableGet(formal.variable!, formal.fileOffset,
1857-
forNullGuardedAccess: false))
1858+
createVariableGet(formal.variable!, formal.fileOffset))
18581859
..fileOffset = formal.fileOffset;
18591860
(namedSuperParametersAsArguments ??= <NamedExpression>[])
18601861
.add(superParameterAsArgument);
18611862
(namedSuperParameterNames ??= <String>{}).add(formal.name);
18621863
(superParametersAsArguments ??= <Object>[])
18631864
.add(superParameterAsArgument);
18641865
} else {
1865-
Expression superParameterAsArgument = createVariableGet(
1866-
formal.variable!, formal.fileOffset,
1867-
forNullGuardedAccess: false);
1866+
Expression superParameterAsArgument =
1867+
createVariableGet(formal.variable!, formal.fileOffset);
18681868
(positionalSuperParametersAsArguments ??= <Expression>[])
18691869
.add(superParameterAsArgument);
18701870
(superParametersAsArguments ??= <Object>[])
@@ -1929,11 +1929,9 @@ class BodyBuilder extends StackListenerImpl
19291929
if (_context.isEnumClass && libraryFeatures.enhancedEnums.isEnabled) {
19301930
ArgumentsImpl arguments = last.arguments as ArgumentsImpl;
19311931
List<Expression> enumSyntheticArguments = [
1932-
new VariableGetImpl(function.positionalParameters[0],
1933-
forNullGuardedAccess: false)
1932+
new VariableGet(function.positionalParameters[0])
19341933
..parent = last.arguments,
1935-
new VariableGetImpl(function.positionalParameters[1],
1936-
forNullGuardedAccess: false)
1934+
new VariableGet(function.positionalParameters[1])
19371935
..parent = last.arguments
19381936
];
19391937
arguments.positional.insertAll(0, enumSyntheticArguments);
@@ -1981,10 +1979,8 @@ class BodyBuilder extends StackListenerImpl
19811979
function.positionalParameters[0].name == "#index" &&
19821980
function.positionalParameters[1].name == "#name");
19831981
(positionalArguments ??= <Expression>[]).insertAll(0, [
1984-
new VariableGetImpl(function.positionalParameters[0],
1985-
forNullGuardedAccess: false),
1986-
new VariableGetImpl(function.positionalParameters[1],
1987-
forNullGuardedAccess: false)
1982+
new VariableGet(function.positionalParameters[0]),
1983+
new VariableGet(function.positionalParameters[1])
19881984
]);
19891985
}
19901986

@@ -3227,15 +3223,12 @@ class BodyBuilder extends StackListenerImpl
32273223
/// Helper method to create a [VariableGet] of the [variable] using
32283224
/// [charOffset] as the file offset.
32293225
@override
3230-
VariableGet createVariableGet(VariableDeclaration variable, int charOffset,
3231-
{bool forNullGuardedAccess = false}) {
3226+
VariableGet createVariableGet(VariableDeclaration variable, int charOffset) {
32323227
if (!(variable as VariableDeclarationImpl).isLocalFunction &&
32333228
!variable.isWildcard) {
32343229
typeInferrer.assignedVariables.read(variable);
32353230
}
3236-
return new VariableGetImpl(variable,
3237-
forNullGuardedAccess: forNullGuardedAccess)
3238-
..fileOffset = charOffset;
3231+
return new VariableGet(variable)..fileOffset = charOffset;
32393232
}
32403233

32413234
/// Helper method to create a [ReadOnlyAccessGenerator] on the [variable]
@@ -3433,7 +3426,7 @@ class BodyBuilder extends StackListenerImpl
34333426
this,
34343427
nameToken,
34353428
extensionBuilder.extension,
3436-
name,
3429+
memberName,
34373430
thisVariable!,
34383431
thisTypeParameters,
34393432
getable as MemberBuilder?,
@@ -3516,7 +3509,7 @@ class BodyBuilder extends StackListenerImpl
35163509
this,
35173510
nameToken,
35183511
extensionBuilder.extension,
3519-
name,
3512+
memberName,
35203513
thisVariable!,
35213514
thisTypeParameters,
35223515
getable as MemberBuilder?,
@@ -6248,60 +6241,29 @@ class BodyBuilder extends StackListenerImpl
62486241
}
62496242
}
62506243

6251-
@override
6252-
Expression buildExtensionMethodInvocation(
6253-
int fileOffset, Procedure target, Arguments arguments,
6254-
{required bool isTearOff}) {
6255-
List<TypeParameter> typeParameters = target.function.typeParameters;
6256-
LocatedMessage? argMessage = checkArgumentsForFunction(
6257-
target.function, arguments, fileOffset, typeParameters,
6258-
isExtensionMemberInvocation: true);
6259-
if (argMessage != null) {
6260-
return buildUnresolvedError(target.name.text, fileOffset,
6261-
arguments: arguments,
6262-
candidate: target,
6263-
message: argMessage,
6264-
kind: UnresolvedKind.Method);
6265-
}
6266-
6267-
Expression node;
6268-
if (isTearOff) {
6269-
node = new ExtensionTearOff(target, arguments);
6270-
} else {
6271-
node = new StaticInvocation(target, arguments);
6272-
}
6273-
node.fileOffset = fileOffset;
6274-
return node;
6275-
}
6276-
62776244
@override
62786245
LocatedMessage? checkArgumentsForFunction(FunctionNode function,
62796246
Arguments arguments, int offset, List<TypeParameter> typeParameters,
6280-
{bool isExtensionMemberInvocation = false}) {
6281-
int requiredPositionalParameterCountToReport =
6282-
function.requiredParameterCount;
6283-
int positionalParameterCountToReport = function.positionalParameters.length;
6284-
int positionalArgumentCountToReport =
6285-
forest.argumentsPositional(arguments).length;
6286-
if (isExtensionMemberInvocation) {
6247+
{Extension? extension}) {
6248+
int typeParameterCount = typeParameters.length;
6249+
int requiredParameterCount = function.requiredParameterCount;
6250+
int positionalParameterCount = function.positionalParameters.length;
6251+
int positionalArgumentsCount = arguments.positional.length;
6252+
if (extension != null) {
62876253
// Extension member invocations have additional synthetic parameter for
62886254
// `this`.
6289-
--requiredPositionalParameterCountToReport;
6290-
--positionalParameterCountToReport;
6291-
--positionalArgumentCountToReport;
6255+
--requiredParameterCount;
6256+
--positionalParameterCount;
6257+
typeParameterCount -= extension.typeParameters.length;
62926258
}
6293-
if (forest.argumentsPositional(arguments).length <
6294-
function.requiredParameterCount) {
6259+
if (positionalArgumentsCount < requiredParameterCount) {
62956260
return cfe.codeTooFewArguments
6296-
.withArguments(requiredPositionalParameterCountToReport,
6297-
positionalArgumentCountToReport)
6261+
.withArguments(requiredParameterCount, positionalArgumentsCount)
62986262
.withLocation(uri, arguments.fileOffset, noLength);
62996263
}
6300-
if (forest.argumentsPositional(arguments).length >
6301-
function.positionalParameters.length) {
6264+
if (positionalArgumentsCount > positionalParameterCount) {
63026265
return cfe.codeTooManyArguments
6303-
.withArguments(
6304-
positionalParameterCountToReport, positionalArgumentCountToReport)
6266+
.withArguments(positionalParameterCount, positionalArgumentsCount)
63056267
.withLocation(uri, arguments.fileOffset, noLength);
63066268
}
63076269
List<NamedExpression> named = forest.argumentsNamed(arguments);
@@ -6329,16 +6291,16 @@ class BodyBuilder extends StackListenerImpl
63296291
}
63306292
}
63316293

6332-
List<DartType> types = forest.argumentsTypeArguments(arguments);
6333-
if (typeParameters.length != types.length) {
6294+
List<DartType> types = arguments.types;
6295+
if (typeParameterCount != types.length) {
63346296
if (types.length == 0) {
63356297
// Expected `typeParameters.length` type arguments, but none given, so
63366298
// we use type inference.
63376299
} else {
63386300
// A wrong (non-zero) amount of type arguments given. That's an error.
63396301
// TODO(jensj): Position should be on type arguments instead.
63406302
return cfe.codeTypeArgumentMismatch
6341-
.withArguments(typeParameters.length)
6303+
.withArguments(typeParameterCount)
63426304
.withLocation(uri, offset, noLength);
63436305
}
63446306
}
@@ -7808,8 +7770,7 @@ class BodyBuilder extends StackListenerImpl
78087770
/// body;
78097771
/// }
78107772
elements.syntheticAssignment = lvalue.buildAssignment(
7811-
new VariableGetImpl(variable, forNullGuardedAccess: false)
7812-
..fileOffset = inToken.offset,
7773+
new VariableGet(variable)..fileOffset = inToken.offset,
78137774
voidContext: true);
78147775
} else if (lvalue is Pattern) {
78157776
/// We are in the case where `lvalue` is a pattern:
@@ -7824,9 +7785,7 @@ class BodyBuilder extends StackListenerImpl
78247785
/// }
78257786
elements.syntheticAssignment = null;
78267787
elements.expressionEffects = forest.createPatternVariableDeclaration(
7827-
inToken.offset,
7828-
lvalue,
7829-
new VariableGetImpl(variable, forNullGuardedAccess: false),
7788+
inToken.offset, lvalue, new VariableGet(variable),
78307789
isFinal: false);
78317790
} else if (lvalue is InvalidExpression) {
78327791
// Coverage-ignore-block(suite): Not run.

0 commit comments

Comments
 (0)