Skip to content

Commit 46cf1b9

Browse files
[parser][analyzer][cfe] Add parsing support for super-parameters
Part of #47525 Change-Id: I7b824e5e3fc9dbb2c6416dfd15df274037a8e9e9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/219521 Commit-Queue: Chloe Stefantsova <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 727eaf1 commit 46cf1b9

File tree

223 files changed

+3226
-2603
lines changed

Some content is hidden

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

223 files changed

+3226
-2603
lines changed

pkg/_fe_analyzer_shared/lib/src/parser/forwarding_listener.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,14 +728,22 @@ class ForwardingListener implements Listener {
728728
@override
729729
void endFormalParameter(
730730
Token? thisKeyword,
731-
Token? periodAfterThis,
731+
Token? superKeyword,
732+
Token? periodAfterThisOrSuper,
732733
Token nameToken,
733734
Token? initializerStart,
734735
Token? initializerEnd,
735736
FormalParameterKind kind,
736737
MemberKind memberKind) {
737-
listener?.endFormalParameter(thisKeyword, periodAfterThis, nameToken,
738-
initializerStart, initializerEnd, kind, memberKind);
738+
listener?.endFormalParameter(
739+
thisKeyword,
740+
superKeyword,
741+
periodAfterThisOrSuper,
742+
nameToken,
743+
initializerStart,
744+
initializerEnd,
745+
kind,
746+
memberKind);
739747
}
740748

741749
@override

pkg/_fe_analyzer_shared/lib/src/parser/listener.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ class Listener implements UnescapeErrorListener {
363363

364364
void endFormalParameter(
365365
Token? thisKeyword,
366-
Token? periodAfterThis,
366+
Token? superKeyword,
367+
Token? periodAfterThisOrSuper,
367368
Token nameToken,
368369
Token? initializerStart,
369370
Token? initializerEnd,

pkg/_fe_analyzer_shared/lib/src/parser/parser_impl.dart

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,35 +1612,41 @@ class Parser {
16121612
parameterKind == FormalParameterKind.optionalNamed;
16131613

16141614
Token? thisKeyword;
1615-
Token? periodAfterThis;
1615+
Token? superKeyword;
1616+
Token? periodAfterThisOrSuper;
16161617
IdentifierContext nameContext =
16171618
IdentifierContext.formalParameterDeclaration;
16181619

1619-
if (!inFunctionType && optional('this', next)) {
1620+
if (!inFunctionType &&
1621+
(optional('this', next) || optional('super', next))) {
16201622
Token originalToken = token;
1621-
thisKeyword = token = next;
1623+
if (optional('this', next)) {
1624+
thisKeyword = token = next;
1625+
} else {
1626+
superKeyword = token = next;
1627+
}
16221628
next = token.next!;
16231629
if (!optional('.', next)) {
16241630
if (isOneOf(next, okNextValueInFormalParameter)) {
16251631
// Recover by not parsing as 'this' --- an error will be given
16261632
// later that it's not an allowed identifier.
16271633
token = originalToken;
16281634
next = token.next!;
1629-
thisKeyword = null;
1635+
thisKeyword = superKeyword = null;
16301636
} else {
16311637
// Recover from a missing period by inserting one.
16321638
next = rewriteAndRecover(
16331639
token,
16341640
codes.templateExpectedButGot.withArguments('.'),
16351641
new SyntheticToken(TokenType.PERIOD, next.charOffset));
16361642
// These 3 lines are duplicated here and below.
1637-
periodAfterThis = token = next;
1643+
periodAfterThisOrSuper = token = next;
16381644
next = token.next!;
16391645
nameContext = IdentifierContext.fieldInitializer;
16401646
}
16411647
} else {
16421648
// These 3 lines are duplicated here and above.
1643-
periodAfterThis = token = next;
1649+
periodAfterThisOrSuper = token = next;
16441650
next = token.next!;
16451651
nameContext = IdentifierContext.fieldInitializer;
16461652
}
@@ -1711,8 +1717,8 @@ class Parser {
17111717
}
17121718

17131719
Token nameToken;
1714-
if (periodAfterThis != null) {
1715-
token = periodAfterThis;
1720+
if (periodAfterThisOrSuper != null) {
1721+
token = periodAfterThisOrSuper;
17161722
}
17171723
next = token.next!;
17181724
if (inFunctionType &&
@@ -1759,8 +1765,15 @@ class Parser {
17591765
} else {
17601766
listener.handleFormalParameterWithoutValue(next);
17611767
}
1762-
listener.endFormalParameter(thisKeyword, periodAfterThis, nameToken,
1763-
initializerStart, initializerEnd, parameterKind, memberKind);
1768+
listener.endFormalParameter(
1769+
thisKeyword,
1770+
superKeyword,
1771+
periodAfterThisOrSuper,
1772+
nameToken,
1773+
initializerStart,
1774+
initializerEnd,
1775+
parameterKind,
1776+
memberKind);
17641777
return token;
17651778
}
17661779

pkg/analyzer/lib/dart/analysis/features.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ abstract class Feature {
3838
/// Feature information for set literals.
3939
static final set_literals = ExperimentalFeatures.set_literals;
4040

41+
/// Feature information for super parameters.
42+
static final super_parameters = ExperimentalFeatures.super_parameters;
43+
4144
/// Feature information for the triple-shift operator.
4245
static final triple_shift = ExperimentalFeatures.triple_shift;
4346

pkg/analyzer/lib/dart/ast/ast.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,8 @@ abstract class AstVisitor<R> {
570570

571571
R? visitSuperExpression(SuperExpression node);
572572

573+
R? visitSuperFormalParameter(SuperFormalParameter node);
574+
573575
R? visitSwitchCase(SwitchCase node);
574576

575577
R? visitSwitchDefault(SwitchDefault node);
@@ -4059,6 +4061,48 @@ abstract class SuperExpression implements Expression {
40594061
Token get superKeyword;
40604062
}
40614063

4064+
/// A super-initializer formal parameter.
4065+
///
4066+
/// superFormalParameter ::=
4067+
/// ('final' [TypeAnnotation] | 'const' [TypeAnnotation] | 'var' | [TypeAnnotation])?
4068+
/// 'super' '.' [SimpleIdentifier] ([TypeParameterList]? [FormalParameterList])?
4069+
///
4070+
/// Clients may not extend, implement or mix-in this class.
4071+
abstract class SuperFormalParameter implements NormalFormalParameter {
4072+
@override
4073+
SimpleIdentifier get identifier;
4074+
4075+
/// Return the token representing either the 'final', 'const' or 'var'
4076+
/// keyword, or `null` if no keyword was used.
4077+
Token? get keyword;
4078+
4079+
/// Return the parameters of the function-typed parameter, or `null` if this
4080+
/// is not a function-typed field formal parameter.
4081+
FormalParameterList? get parameters;
4082+
4083+
/// Return the token representing the period.
4084+
Token get period;
4085+
4086+
/// If the parameter is function-typed, and has the question mark, then its
4087+
/// function type is nullable. Having a nullable function type means that the
4088+
/// parameter can be null.
4089+
Token? get question;
4090+
4091+
/// Return the token representing the 'super' keyword.
4092+
Token get superKeyword;
4093+
4094+
/// Return the declared type of the parameter, or `null` if the parameter does
4095+
/// not have a declared type.
4096+
///
4097+
/// Note that if this is a function-typed field formal parameter this is the
4098+
/// return type of the function.
4099+
TypeAnnotation? get type;
4100+
4101+
/// Return the type parameters associated with this method, or `null` if this
4102+
/// method is not a generic method.
4103+
TypeParameterList? get typeParameters;
4104+
}
4105+
40624106
/// A case in a switch statement.
40634107
///
40644108
/// switchCase ::=

pkg/analyzer/lib/dart/ast/ast_factory.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,26 @@ abstract class AstFactory {
872872
/// Returns a newly created super expression.
873873
SuperExpression superExpression(Token superKeyword);
874874

875+
/// Returns a newly created super-initializer parameter. Either or both of
876+
/// the [comment] and [metadata] can be `null` if the parameter does not have
877+
/// the corresponding attribute. The [keyword] can be `null` if there is a
878+
/// type. The [type] must be `null` if the keyword is 'var'. The [parameters]
879+
/// can be `null` if this is not a function-typed super-initializer
880+
/// parameter.
881+
SuperFormalParameter superFormalParameter(
882+
{Comment? comment,
883+
List<Annotation>? metadata,
884+
Token? covariantKeyword,
885+
Token? requiredKeyword,
886+
Token? keyword,
887+
TypeAnnotation? type,
888+
required Token superKeyword,
889+
required Token period,
890+
required SimpleIdentifier identifier,
891+
TypeParameterList? typeParameters,
892+
FormalParameterList? parameters,
893+
Token? question});
894+
875895
/// Returns a newly created switch case. The list of [labels] can be `null`
876896
/// if there are no labels.
877897
SwitchCase switchCase(List<Label> labels, Token keyword,

pkg/analyzer/lib/dart/ast/visitor.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ class GeneralizingAstVisitor<R> implements AstVisitor<R> {
560560
@override
561561
R? visitSuperExpression(SuperExpression node) => visitExpression(node);
562562

563+
@override
564+
R? visitSuperFormalParameter(SuperFormalParameter node) =>
565+
visitNormalFormalParameter(node);
566+
563567
@override
564568
R? visitSwitchCase(SwitchCase node) => visitSwitchMember(node);
565569

@@ -1308,6 +1312,12 @@ class RecursiveAstVisitor<R> implements AstVisitor<R> {
13081312
return null;
13091313
}
13101314

1315+
@override
1316+
R? visitSuperFormalParameter(SuperFormalParameter node) {
1317+
node.visitChildren(this);
1318+
return null;
1319+
}
1320+
13111321
@override
13121322
R? visitSwitchCase(SwitchCase node) {
13131323
node.visitChildren(this);
@@ -1770,6 +1780,9 @@ class SimpleAstVisitor<R> implements AstVisitor<R> {
17701780
@override
17711781
R? visitSuperExpression(SuperExpression node) => null;
17721782

1783+
@override
1784+
R? visitSuperFormalParameter(SuperFormalParameter node) => null;
1785+
17731786
@override
17741787
R? visitSwitchCase(SwitchCase node) => null;
17751788

@@ -2183,6 +2196,9 @@ class ThrowingAstVisitor<R> implements AstVisitor<R> {
21832196
@override
21842197
R? visitSuperExpression(SuperExpression node) => _throw(node);
21852198

2199+
@override
2200+
R? visitSuperFormalParameter(SuperFormalParameter node) => _throw(node);
2201+
21862202
@override
21872203
R? visitSwitchCase(SwitchCase node) => _throw(node);
21882204

@@ -3144,6 +3160,14 @@ class TimedAstVisitor<T> implements AstVisitor<T> {
31443160
return result;
31453161
}
31463162

3163+
@override
3164+
T? visitSuperFormalParameter(SuperFormalParameter node) {
3165+
stopwatch.start();
3166+
T? result = _baseVisitor.visitSuperFormalParameter(node);
3167+
stopwatch.stop();
3168+
return result;
3169+
}
3170+
31473171
@override
31483172
T? visitSwitchCase(SwitchCase node) {
31493173
stopwatch.start();
@@ -3667,6 +3691,9 @@ class UnifyingAstVisitor<R> implements AstVisitor<R> {
36673691
@override
36683692
R? visitSuperExpression(SuperExpression node) => visitNode(node);
36693693

3694+
@override
3695+
R? visitSuperFormalParameter(SuperFormalParameter node) => visitNode(node);
3696+
36703697
@override
36713698
R? visitSwitchCase(SwitchCase node) => visitNode(node);
36723699

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import 'package:meta/meta.dart';
8383
/// TODO(scheglov) Clean up the list of implicitly analyzed files.
8484
class AnalysisDriver implements AnalysisDriverGeneric {
8585
/// The version of data format, should be incremented on every format change.
86-
static const int DATA_VERSION = 190;
86+
static const int DATA_VERSION = 191;
8787

8888
/// The number of exception contexts allowed to write. Once this field is
8989
/// zero, we stop writing any new exception contexts in this process.

0 commit comments

Comments
 (0)