Skip to content

Commit 3e53290

Browse files
authored
Merge pull request #60052 from DougGregor/syntax-decl-cleanup
[Syntax] Clean up syntax grammar for declaration nodes
2 parents 1c29764 + 79d1b5a commit 3e53290

15 files changed

+257
-192
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ ERROR(expected_lparen_initializer,PointsToFirstBadToken,
422422
"expected '(' for initializer parameters", ())
423423
ERROR(initializer_has_name,PointsToFirstBadToken,
424424
"initializers cannot have a name", ())
425+
ERROR(initializer_result_type,PointsToFirstBadToken,
426+
"initializers cannot have a result type", ())
425427

426428
// Destructor
427429
ERROR(destructor_decl_outside_class,none,

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ class Parser {
14601460
ParameterList *&BodyParams,
14611461
ParameterContextKind paramContext,
14621462
DefaultArgumentInfo &defaultArgs);
1463-
ParserStatus parseFunctionSignature(Identifier functionName,
1463+
ParserStatus parseFunctionSignature(DeclBaseName functionName,
14641464
DeclName &fullName,
14651465
ParameterList *&bodyParams,
14661466
DefaultArgumentInfo &defaultArgs,

lib/Parse/ParseDecl.cpp

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,13 +2033,20 @@ static AttrOptionSwitch<R>
20332033
parseSingleAttrOption(Parser &P, SourceLoc Loc, SourceRange &AttrRange,
20342034
StringRef AttrName, DeclAttrKind DK) {
20352035
bool isModifier = DeclAttribute::isDeclModifier(DK);
2036-
if (!P.consumeIf(tok::l_paren)) {
2036+
if (!P.Tok.is(tok::l_paren)) {
20372037
AttrRange = SourceRange(Loc);
20382038
// Create an AttrOptionSwitch with an empty value. The calls on it will
20392039
// decide whether or not that's valid.
20402040
return AttrOptionSwitch<R>(StringRef(), P, Loc, AttrName, isModifier);
20412041
}
20422042

2043+
llvm::Optional<SyntaxParsingContext> ModDetailContext;
2044+
if (DK == DAK_ReferenceOwnership) {
2045+
ModDetailContext.emplace(P.SyntaxContext, SyntaxKind::DeclModifierDetail);
2046+
}
2047+
2048+
P.consumeToken(tok::l_paren);
2049+
20432050
StringRef parsedName = P.Tok.getText();
20442051
if (!P.consumeIf(tok::identifier)) {
20452052
// Once we have an example of a valid option, diagnose this with
@@ -2288,7 +2295,7 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
22882295
.Case("public", AccessLevel::Public)
22892296
.Case("open", AccessLevel::Open);
22902297

2291-
if (!consumeIf(tok::l_paren)) {
2298+
if (!Tok.is(tok::l_paren)) {
22922299
// Normal access control attribute.
22932300
AttrRange = Loc;
22942301
DuplicateAttribute = Attributes.getAttribute<AccessControlAttr>();
@@ -2297,6 +2304,11 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
22972304
break;
22982305
}
22992306

2307+
SyntaxParsingContext ModDetailContext(
2308+
SyntaxContext, SyntaxKind::DeclModifierDetail);
2309+
2310+
consumeToken(tok::l_paren);
2311+
23002312
// Parse the subject.
23012313
if (Tok.isContextualKeyword("set")) {
23022314
consumeToken();
@@ -4723,7 +4735,7 @@ Parser::parseDecl(ParseDeclOptions Flags,
47234735

47244736
if (Tok.isContextualKeyword("actor") && peekToken().is(tok::identifier)) {
47254737
Tok.setKind(tok::contextual_keyword);
4726-
DeclParsingContext.setCreateSyntax(SyntaxKind::ClassDecl);
4738+
DeclParsingContext.setCreateSyntax(SyntaxKind::ActorDecl);
47274739
DeclResult = parseDeclClass(Flags, Attributes);
47284740
break;
47294741
}
@@ -8395,13 +8407,20 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
83958407
return Status;
83968408
}
83978409

8398-
// Parse the parameters.
83998410
DefaultArgumentInfo DefaultArgs;
8400-
llvm::SmallVector<Identifier, 4> namePieces;
8401-
ParserResult<ParameterList> Params
8402-
= parseSingleParameterClause(ParameterContextKind::Initializer,
8403-
&namePieces, &DefaultArgs);
8404-
Status |= Params;
8411+
TypeRepr *FuncRetTy = nullptr;
8412+
DeclName FullName;
8413+
ParameterList *BodyParams;
8414+
SourceLoc asyncLoc;
8415+
bool reasync;
8416+
SourceLoc throwsLoc;
8417+
bool rethrows;
8418+
Status |= parseFunctionSignature(DeclBaseName::createConstructor(), FullName,
8419+
BodyParams,
8420+
DefaultArgs,
8421+
asyncLoc, reasync,
8422+
throwsLoc, rethrows,
8423+
FuncRetTy);
84058424
if (Status.hasCodeCompletion() && !CodeCompletion) {
84068425
// Trigger delayed parsing, no need to continue.
84078426
return Status;
@@ -8413,17 +8432,23 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
84138432
return nullptr;
84148433
}
84158434

8416-
// Parse 'async' / 'reasync' / 'throws' / 'rethrows'.
8417-
SourceLoc asyncLoc;
8418-
bool reasync = false;
8419-
SourceLoc throwsLoc;
8420-
bool rethrows = false;
8421-
Status |= parseEffectsSpecifiers(SourceLoc(),
8422-
asyncLoc, &reasync,
8423-
throwsLoc, &rethrows);
8424-
if (Status.hasCodeCompletion() && !CodeCompletion) {
8425-
// Trigger delayed parsing, no need to continue.
8426-
return Status;
8435+
// If there was an 'async' modifier, put it in the right place for an
8436+
// initializer.
8437+
bool isAsync = asyncLoc.isValid();
8438+
if (auto asyncAttr = Attributes.getAttribute<AsyncAttr>()) {
8439+
SourceLoc insertLoc = Lexer::getLocForEndOfToken(
8440+
SourceMgr, BodyParams->getRParenLoc());
8441+
8442+
diagnose(asyncAttr->getLocation(), diag::async_func_modifier)
8443+
.fixItRemove(asyncAttr->getRange())
8444+
.fixItInsert(insertLoc, " async");
8445+
asyncAttr->setInvalid();
8446+
isAsync = true;
8447+
}
8448+
8449+
if (FuncRetTy) {
8450+
diagnose(FuncRetTy->getStartLoc(), diag::initializer_result_type)
8451+
.fixItRemove(FuncRetTy->getSourceRange());
84278452
}
84288453

84298454
if (reasync) {
@@ -8435,12 +8460,11 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
84358460

84368461
diagnoseWhereClauseInGenericParamList(GenericParams);
84378462

8438-
DeclName FullName(Context, DeclBaseName::createConstructor(), namePieces);
84398463
auto *CD = new (Context) ConstructorDecl(FullName, ConstructorLoc,
84408464
Failable, FailabilityLoc,
8441-
asyncLoc.isValid(), asyncLoc,
8465+
isAsync, asyncLoc,
84428466
throwsLoc.isValid(), throwsLoc,
8443-
Params.get(), GenericParams,
8467+
BodyParams, GenericParams,
84448468
CurDeclContext);
84458469
CD->setImplicitlyUnwrappedOptional(IUO);
84468470
CD->getAttrs() = Attributes;
@@ -8466,7 +8490,7 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
84668490
CodeCompletion->setParsedDecl(CD);
84678491
}
84688492

8469-
if (ConstructorsNotAllowed || Params.isParseErrorOrHasCompletion()) {
8493+
if (ConstructorsNotAllowed) {
84708494
// Tell the type checker not to touch this constructor.
84718495
CD->setInvalid();
84728496
}

lib/Parse/ParsePattern.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ Parser::parseFunctionArguments(SmallVectorImpl<Identifier> &NamePieces,
863863
///
864864
/// Note that this leaves retType as null if unspecified.
865865
ParserStatus
866-
Parser::parseFunctionSignature(Identifier SimpleName,
866+
Parser::parseFunctionSignature(DeclBaseName SimpleName,
867867
DeclName &FullName,
868868
ParameterList *&bodyParams,
869869
DefaultArgumentInfo &defaultArgs,
@@ -876,8 +876,11 @@ Parser::parseFunctionSignature(Identifier SimpleName,
876876
SmallVector<Identifier, 4> NamePieces;
877877
ParserStatus Status;
878878

879-
ParameterContextKind paramContext = SimpleName.isOperator() ?
880-
ParameterContextKind::Operator : ParameterContextKind::Function;
879+
ParameterContextKind paramContext = SimpleName.isOperator()
880+
? ParameterContextKind::Operator
881+
: (SimpleName == DeclBaseName::createConstructor()
882+
? ParameterContextKind::Initializer
883+
: ParameterContextKind::Function);
881884
Status |= parseFunctionArguments(NamePieces, bodyParams, paramContext,
882885
defaultArgs);
883886
FullName = DeclName(Context, SimpleName, NamePieces);

test/Parse/init_deinit.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
struct FooStructConstructorA {
44
init // expected-error {{expected '('}}
5+
// expected-error@-1{{initializer requires a body}}
56
}
67

78
struct FooStructConstructorB {
@@ -10,10 +11,17 @@ struct FooStructConstructorB {
1011

1112
struct FooStructConstructorC {
1213
init {} // expected-error {{expected '('}}{{7-7=()}}
14+
// expected-note@-1{{'init()' previously declared here}}
1315
init<T> {} // expected-error {{expected '('}} {{10-10=()}}
16+
// expected-error@-1{{generic parameter 'T' is not used in function signature}}
1417
init? { self.init() } // expected-error {{expected '('}} {{8-8=()}}
18+
// expected-error@-1{{invalid redeclaration of 'init()'}}
1519
}
1620

21+
struct FooStructConstructorD {
22+
init() -> FooStructConstructorD { }
23+
// expected-error@-1{{initializers cannot have a result type}}
24+
}
1725

1826
struct FooStructDeinitializerA {
1927
deinit // expected-error {{expected '{' for deinitializer}}

test/Parse/recovery.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ struct InitializerWithNameAndParam {
699699
struct InitializerWithLabels {
700700
init c d: Int {}
701701
// expected-error @-1 {{expected '(' for initializer parameters}}
702+
// expected-error @-2 {{initializer requires a body}}
702703
}
703704

704705
// rdar://20337695

test/Syntax/Inputs/serialize_class_decl.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
"trailingTrivia": " ",
2727
"presence": "Present"
2828
},
29-
null,
30-
null,
3129
null
3230
],
3331
"presence": "Present"

test/Syntax/Inputs/serialize_distributed_actor.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"kind": "CodeBlockItem",
99
"layout": [
1010
{
11-
"kind": "ClassDecl",
11+
"kind": "ActorDecl",
1212
"layout": [
1313
null,
1414
{
@@ -26,8 +26,6 @@
2626
"trailingTrivia": " ",
2727
"presence": "Present"
2828
},
29-
null,
30-
null,
3129
null
3230
],
3331
"presence": "Present"
@@ -92,8 +90,6 @@
9290
"trailingTrivia": " ",
9391
"presence": "Present"
9492
},
95-
null,
96-
null,
9793
null
9894
],
9995
"presence": "Present"

0 commit comments

Comments
 (0)