Skip to content

Commit 6829ff6

Browse files
authored
Merge pull request #14406 from rintaro/syntax-parse-initializer
[Syntax] Parse several decl syntax
2 parents 062be8f + a9670a7 commit 6829ff6

File tree

6 files changed

+170
-33
lines changed

6 files changed

+170
-33
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,6 +2456,7 @@ Parser::parseDecl(ParseDeclOptions Flags,
24562456
MayNeedOverrideCompletion = true;
24572457
break;
24582458
case tok::kw_associatedtype:
2459+
DeclParsingContext.setCreateSyntax(SyntaxKind::AssociatedtypeDecl);
24592460
DeclResult = parseDeclAssociatedType(Flags, Attributes);
24602461
break;
24612462
case tok::kw_enum:
@@ -2475,9 +2476,13 @@ Parser::parseDecl(ParseDeclOptions Flags,
24752476
break;
24762477
}
24772478
case tok::kw_init:
2479+
DeclParsingContext.collectNodesInPlace(SyntaxKind::ModifierList);
2480+
DeclParsingContext.setCreateSyntax(SyntaxKind::InitializerDecl);
24782481
DeclResult = parseDeclInit(Flags, Attributes);
24792482
break;
24802483
case tok::kw_deinit:
2484+
DeclParsingContext.collectNodesInPlace(SyntaxKind::ModifierList);
2485+
DeclParsingContext.setCreateSyntax(SyntaxKind::DeinitializerDecl);
24812486
DeclResult = parseDeclDeinit(Flags, Attributes);
24822487
break;
24832488
case tok::kw_operator:
@@ -2501,6 +2506,8 @@ Parser::parseDecl(ParseDeclOptions Flags,
25012506
break;
25022507

25032508
case tok::kw_subscript: {
2509+
DeclParsingContext.collectNodesInPlace(SyntaxKind::ModifierList);
2510+
DeclParsingContext.setCreateSyntax(SyntaxKind::SubscriptDecl);
25042511
if (StaticLoc.isValid()) {
25052512
diagnose(Tok, diag::subscript_static, StaticSpelling)
25062513
.fixItRemove(SourceRange(StaticLoc));
@@ -3492,6 +3499,8 @@ ParserResult<TypeDecl> Parser::parseDeclAssociatedType(Parser::ParseDeclOptions
34923499

34933500
ParserResult<TypeRepr> UnderlyingTy;
34943501
if (Tok.is(tok::equal)) {
3502+
SyntaxParsingContext InitContext(SyntaxContext,
3503+
SyntaxKind::TypeInitializerClause);
34953504
consumeToken(tok::equal);
34963505
UnderlyingTy = parseType(diag::expected_type_in_associatedtype);
34973506
Status |= UnderlyingTy;
@@ -5821,30 +5830,36 @@ Parser::parseDeclSubscript(ParseDeclOptions Flags,
58215830
if (SignatureHasCodeCompletion && !CodeCompletion)
58225831
return makeParserCodeCompletionStatus();
58235832

5824-
// '->'
58255833
SourceLoc ArrowLoc;
5826-
if (!consumeIf(tok::arrow, ArrowLoc)) {
5827-
if (!Indices.isParseError())
5828-
diagnose(Tok, diag::expected_arrow_subscript);
5829-
Status.setIsParseError();
5830-
}
5834+
ParserResult<TypeRepr> ElementTy;
5835+
{
5836+
SyntaxParsingContext ReturnCtxt(SyntaxContext, SyntaxKind::ReturnClause);
58315837

5832-
if (!ArrowLoc.isValid() && (Indices.isNull() || Indices.get()->size() == 0)) {
5833-
// This doesn't look much like a subscript, so let regular recovery take
5834-
// care of it.
5835-
return Status;
5836-
}
5837-
5838-
// type
5839-
ParserResult<TypeRepr> ElementTy = parseType(diag::expected_type_subscript);
5840-
Status |= ElementTy;
5841-
SignatureHasCodeCompletion |= ElementTy.hasCodeCompletion();
5842-
if (SignatureHasCodeCompletion && !CodeCompletion) {
5843-
return makeParserCodeCompletionStatus();
5844-
}
5845-
if (ElementTy.isNull()) {
5846-
// Always set an element type.
5847-
ElementTy = makeParserResult(ElementTy, new (Context) ErrorTypeRepr());
5838+
// '->'
5839+
if (!consumeIf(tok::arrow, ArrowLoc)) {
5840+
if (!Indices.isParseError())
5841+
diagnose(Tok, diag::expected_arrow_subscript);
5842+
Status.setIsParseError();
5843+
}
5844+
5845+
if (!ArrowLoc.isValid() &&
5846+
(Indices.isNull() || Indices.get()->size() == 0)) {
5847+
// This doesn't look much like a subscript, so let regular recovery take
5848+
// care of it.
5849+
return Status;
5850+
}
5851+
5852+
// type
5853+
ElementTy = parseType(diag::expected_type_subscript);
5854+
Status |= ElementTy;
5855+
SignatureHasCodeCompletion |= ElementTy.hasCodeCompletion();
5856+
if (SignatureHasCodeCompletion && !CodeCompletion) {
5857+
return makeParserCodeCompletionStatus();
5858+
}
5859+
if (ElementTy.isNull()) {
5860+
// Always set an element type.
5861+
ElementTy = makeParserResult(ElementTy, new (Context) ErrorTypeRepr());
5862+
}
58485863
}
58495864

58505865
diagnoseWhereClauseInGenericParamList(GenericParams);

lib/Syntax/Status.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@
6262
* FuncDecl
6363
* ProtocolDecl
6464
* ImportDecl
65+
* AssociatedTypeDecl
6566
* TypeAliasDecl
6667
* IfConfigDecl
6768
* PatternBindingDecl
6869
* VarDecl
6970
* ExtensionDecl
71+
* SubscriptDecl
72+
* ConstructorDecl
73+
* DestructorDecl
7074

7175
### In-progress (UnknownDecl):
7276

@@ -76,11 +80,7 @@
7680
* InfixOperatorDecl
7781
* PrefixOperatorDecl
7882
* PostfixOperatorDecl
79-
* AssociatedTypeDecl
8083
* EnumDecl
81-
* SubscriptDecl
82-
* ConstructorDecl
83-
* DestructorDecl
8484
* EnumElementDecl
8585

8686
## Statement

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,30 @@ class C <MemberDeclBlock>{<FunctionDecl>
8686
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><FunctionCallExpr><ImplicitMemberExpr>.foo </ImplicitMemberExpr><ClosureExpr>{ <IntegerLiteralExpr>12 </IntegerLiteralExpr>}</ClosureExpr></FunctionCallExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
8787
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><SubscriptExpr><ImplicitMemberExpr>.foo</ImplicitMemberExpr>[<FunctionCallArgument><IntegerLiteralExpr>12</IntegerLiteralExpr></FunctionCallArgument>]</SubscriptExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
8888
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><MemberAccessExpr><ImplicitMemberExpr>.foo</ImplicitMemberExpr>.bar</MemberAccessExpr></SequenceExpr>
89-
}</CodeBlock></FunctionDecl>
90-
}</MemberDeclBlock></ClassDecl>
89+
}</CodeBlock></FunctionDecl><InitializerDecl>
90+
91+
init<ParameterClause>() </ParameterClause><CodeBlock>{}</CodeBlock></InitializerDecl><InitializerDecl><Attribute>
92+
@objc </Attribute><DeclModifier>private </DeclModifier>init<ParameterClause>(<FunctionParameter>a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>)</ParameterClause></InitializerDecl><InitializerDecl>
93+
init!<ParameterClause>(<FunctionParameter>a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) </ParameterClause><CodeBlock>{}</CodeBlock></InitializerDecl><InitializerDecl>
94+
init?<ParameterClause>(<FunctionParameter>a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) </ParameterClause><CodeBlock>{}</CodeBlock></InitializerDecl><InitializerDecl><DeclModifier>
95+
public </DeclModifier>init<ParameterClause>(<FunctionParameter>a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) </ParameterClause>throws <CodeBlock>{}</CodeBlock></InitializerDecl><DeinitializerDecl><Attribute>
96+
97+
@objc </Attribute>deinit <CodeBlock>{}</CodeBlock></DeinitializerDecl><DeinitializerDecl><DeclModifier>
98+
private </DeclModifier>deinit <CodeBlock>{}</CodeBlock></DeinitializerDecl><SubscriptDecl><DeclModifier>
99+
100+
internal </DeclModifier>subscript<ParameterClause>(<FunctionParameter>x: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) </ParameterClause><ReturnClause>-> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier></ReturnClause><AccessorBlock>{ <AccessorDecl>get <CodeBlock>{} </CodeBlock></AccessorDecl><AccessorDecl>set <CodeBlock>{} </CodeBlock></AccessorDecl>}</AccessorBlock></SubscriptDecl><SubscriptDecl>
101+
subscript<ParameterClause>() </ParameterClause><ReturnClause>-> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier></ReturnClause><AccessorBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</AccessorBlock></SubscriptDecl>
102+
}</MemberDeclBlock></ClassDecl><ProtocolDecl>
103+
104+
protocol PP <MemberDeclBlock>{<AssociatedtypeDecl>
105+
associatedtype A</AssociatedtypeDecl><AssociatedtypeDecl>
106+
associatedtype B<TypeInheritanceClause>: <InheritedType><SimpleTypeIdentifier>Sequence</SimpleTypeIdentifier></InheritedType></TypeInheritanceClause></AssociatedtypeDecl><AssociatedtypeDecl>
107+
associatedtype C <TypeInitializerClause>= <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></TypeInitializerClause></AssociatedtypeDecl><AssociatedtypeDecl>
108+
associatedtype D<TypeInheritanceClause>: <InheritedType><SimpleTypeIdentifier>Sequence </SimpleTypeIdentifier></InheritedType></TypeInheritanceClause><TypeInitializerClause>= <ArrayType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>]</ArrayType></TypeInitializerClause></AssociatedtypeDecl><AssociatedtypeDecl>
109+
associatedtype E<TypeInheritanceClause>: <InheritedType><SimpleTypeIdentifier>Sequence </SimpleTypeIdentifier></InheritedType></TypeInheritanceClause><TypeInitializerClause>= <ArrayType>[<ArrayType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>]</ArrayType>] </ArrayType></TypeInitializerClause><GenericWhereClause>where <ConformanceRequirement><MemberTypeIdentifier><SimpleTypeIdentifier>A</SimpleTypeIdentifier>.Element </MemberTypeIdentifier>: <SimpleTypeIdentifier>Sequence</SimpleTypeIdentifier></ConformanceRequirement></GenericWhereClause></AssociatedtypeDecl><AssociatedtypeDecl><DeclModifier>
110+
private </DeclModifier>associatedtype F</AssociatedtypeDecl><AssociatedtypeDecl><Attribute>
111+
@objc </Attribute>associatedtype G</AssociatedtypeDecl>
112+
}</MemberDeclBlock></ProtocolDecl>
91113

92114
#endif</IfConfigDecl><IfConfigDecl>
93115

test/Syntax/round_trip_parse_gen.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@ class C {
8787
_ = .foo[12]
8888
_ = .foo.bar
8989
}
90+
91+
init() {}
92+
@objc private init(a: Int)
93+
init!(a: Int) {}
94+
init?(a: Int) {}
95+
public init(a: Int) throws {}
96+
97+
@objc deinit {}
98+
private deinit {}
99+
100+
internal subscript(x: Int) -> Int { get {} set {} }
101+
subscript() -> Int { return 1 }
102+
}
103+
104+
protocol PP {
105+
associatedtype A
106+
associatedtype B: Sequence
107+
associatedtype C = Int
108+
associatedtype D: Sequence = [Int]
109+
associatedtype E: Sequence = [[Int]] where A.Element : Sequence
110+
private associatedtype F
111+
@objc associatedtype G
90112
}
91113

92114
#endif

unittests/Syntax/DeclSyntaxTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ TEST(DeclSyntaxTests, TypealiasMakeAPIs) {
106106
auto TypeInit = SyntaxFactory::makeTypeInitializerClause(Assignment,
107107
Array_Int);
108108
SyntaxFactory::makeTypealiasDecl(None, None, Typealias,
109-
Subsequence, GenericParams, TypeInit)
109+
Subsequence, GenericParams, TypeInit, None)
110110
.print(OS);
111111
ASSERT_EQ(OS.str().str(),
112112
"typealias MyCollection<Element> = Array<Element>");

utils/gyb_syntax_support/DeclNodes.py

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
DECL_NODES = [
7-
# initializer -> '=' type
7+
# type-assignment -> '=' type
88
Node('TypeInitializerClause', kind='Syntax',
99
children=[
1010
Child('Equal', kind='EqualToken'),
@@ -13,9 +13,8 @@
1313

1414
# typealias-declaration -> attributes? access-level-modifier? 'typealias'
1515
# typealias-name generic-parameter-clause?
16-
# typealias-assignment
16+
# type-assignment
1717
# typealias-name -> identifier
18-
# typealias-assignment -> = type
1918
Node('TypealiasDecl', kind='Decl',
2019
children=[
2120
Child('Attributes', kind='AttributeList',
@@ -27,7 +26,30 @@
2726
Child('GenericParameterClause', kind='GenericParameterClause',
2827
is_optional=True),
2928
Child('Initializer', kind='TypeInitializerClause',
30-
is_optional=True)
29+
is_optional=True),
30+
Child('GenericWhereClause', kind='GenericWhereClause',
31+
is_optional=True),
32+
]),
33+
34+
# associatedtype-declaration -> attributes? access-level-modifier?
35+
# 'associatedtype' associatedtype-name
36+
# inheritance-clause? type-assignment?
37+
# generic-where-clause?
38+
# associatedtype-name -> identifier
39+
Node('AssociatedtypeDecl', kind='Decl',
40+
children=[
41+
Child('Attributes', kind='AttributeList',
42+
is_optional=True),
43+
Child('AccessLevelModifier', kind='DeclModifier',
44+
is_optional=True),
45+
Child('AssociatedtypeKeyword', kind='AssociatedtypeToken'),
46+
Child('Identifier', kind='IdentifierToken'),
47+
Child('InheritanceClause', kind='TypeInheritanceClause',
48+
is_optional=True),
49+
Child('Initializer', kind='TypeInitializerClause',
50+
is_optional=True),
51+
Child('GenericWhereClause', kind='GenericWhereClause',
52+
is_optional=True),
3153
]),
3254

3355
Node('FunctionParameterList', kind='SyntaxCollection',
@@ -321,6 +343,62 @@
321343
Child('Body', kind='CodeBlock', is_optional=True),
322344
]),
323345

346+
Node('InitializerDecl', kind='Decl',
347+
children=[
348+
Child('Attributes', kind='AttributeList',
349+
is_optional=True),
350+
Child('Modifiers', kind='ModifierList',
351+
is_optional=True),
352+
Child('InitKeyword', kind='InitToken'),
353+
Child('OptionalMark', kind='Token',
354+
token_choices=[
355+
'PostfixQuestionMarkToken',
356+
'InfixQuestionMarkToken',
357+
'ExclamationMarkToken',
358+
],
359+
is_optional=True),
360+
Child('GenericParameterClause', kind='GenericParameterClause',
361+
is_optional=True),
362+
Child('Parameters', kind='ParameterClause'),
363+
Child('ThrowsOrRethrowsKeyword', kind='Token',
364+
is_optional=True,
365+
token_choices=[
366+
'ThrowsToken',
367+
'RethrowsToken',
368+
]),
369+
Child('GenericWhereClause', kind='GenericWhereClause',
370+
is_optional=True),
371+
# the body is not necessary inside a protocol definition
372+
Child('Body', kind='CodeBlock', is_optional=True),
373+
]),
374+
375+
Node('DeinitializerDecl', kind='Decl',
376+
children=[
377+
Child('Attributes', kind='AttributeList',
378+
is_optional=True),
379+
Child('Modifiers', kind='ModifierList',
380+
is_optional=True),
381+
Child('DeinitKeyword', kind='DeinitToken'),
382+
Child('Body', kind='CodeBlock'),
383+
]),
384+
385+
Node('SubscriptDecl', kind='Decl',
386+
children=[
387+
Child('Attributes', kind='AttributeList',
388+
is_optional=True),
389+
Child('Modifiers', kind='ModifierList',
390+
is_optional=True),
391+
Child('SubscriptKeyword', kind='SubscriptToken'),
392+
Child('GenericParameterClause', kind='GenericParameterClause',
393+
is_optional=True),
394+
Child('Indices', kind='ParameterClause'),
395+
Child('Result', kind='ReturnClause'),
396+
Child('GenericWhereClause', kind='GenericWhereClause',
397+
is_optional=True),
398+
# the body is not necessary inside a protocol definition
399+
Child('Accessor', kind='AccessorBlock', is_optional=True),
400+
]),
401+
324402
# else-if-directive-clause-list -> else-if-directive-clause
325403
# else-if-directive-clause-list?
326404
Node('ElseifDirectiveClauseList', kind='SyntaxCollection',

0 commit comments

Comments
 (0)