Skip to content

Commit ba58a29

Browse files
committed
[Syntax] Parse associatedtype declaration
Also, added generic where clause to typealias declaration.
1 parent 62eb271 commit ba58a29

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 0 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:
@@ -3498,6 +3499,8 @@ ParserResult<TypeDecl> Parser::parseDeclAssociatedType(Parser::ParseDeclOptions
34983499

34993500
ParserResult<TypeRepr> UnderlyingTy;
35003501
if (Tok.is(tok::equal)) {
3502+
SyntaxParsingContext InitContext(SyntaxContext,
3503+
SyntaxKind::TypeInitializerClause);
35013504
consumeToken(tok::equal);
35023505
UnderlyingTy = parseType(diag::expected_type_in_associatedtype);
35033506
Status |= UnderlyingTy;

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,17 @@ class C <MemberDeclBlock>{<FunctionDecl>
9999

100100
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>
101101
subscript<ParameterClause>() </ParameterClause><ReturnClause>-> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier></ReturnClause><AccessorBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</AccessorBlock></SubscriptDecl>
102-
}</MemberDeclBlock></ClassDecl>
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>
103113

104114
#endif</IfConfigDecl><IfConfigDecl>
105115

test/Syntax/round_trip_parse_gen.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ class C {
101101
subscript() -> Int { return 1 }
102102
}
103103

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
112+
}
113+
104114
#endif
105115

106116
#if blah

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: 26 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',

0 commit comments

Comments
 (0)