Skip to content

Commit 1b24d23

Browse files
authored
libSyntax: support function parameter nodes. (swiftlang#13324)
1 parent 6d2c465 commit 1b24d23

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
167167
return parseList(tok::r_paren, leftParenLoc, rightParenLoc,
168168
/*AllowSepAfterLast=*/false,
169169
diag::expected_rparen_parameter,
170-
SyntaxKind::Unknown,
170+
SyntaxKind::FunctionParameterList,
171171
[&]() -> ParserStatus {
172172
ParsedParameter param;
173173
ParserStatus status;

lib/Parse/Parser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,8 @@ static SyntaxKind getListElementKind(SyntaxKind ListKind) {
842842
return SyntaxKind::DictionaryElement;
843843
case SyntaxKind::TupleElementList:
844844
return SyntaxKind::TupleElement;
845+
case SyntaxKind::FunctionParameterList:
846+
return SyntaxKind::FunctionParameter;
845847
default:
846848
return SyntaxKind::Unknown;
847849
}

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
// RUN: diff -u %S/Outputs/round_trip_parse_gen.swift.withkinds %t.withkinds
55

66
class C {
7-
func bar(_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>) <CodeBlock>{}</CodeBlock>
8-
func bar1(_ a: <SimpleTypeIdentifier>Float</SimpleTypeIdentifier>) -> <SimpleTypeIdentifier>Float </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <SequenceExpr><PrefixOperatorExpr>-<FloatLiteralExpr>0.6 </FloatLiteralExpr></PrefixOperatorExpr><BinaryOperatorExpr>+ </BinaryOperatorExpr><FloatLiteralExpr>0.1 </FloatLiteralExpr><BinaryOperatorExpr>- </BinaryOperatorExpr><FloatLiteralExpr>0.3 </FloatLiteralExpr></SequenceExpr></ReturnStmt>}</CodeBlock>
9-
func bar2(a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, b: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, c:<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
10-
func bar3(a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
11-
func bar4(_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
7+
func bar(<FunctionParameter>_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) <CodeBlock>{}</CodeBlock>
8+
func bar1(<FunctionParameter>_ a: <SimpleTypeIdentifier>Float</SimpleTypeIdentifier></FunctionParameter>) -> <SimpleTypeIdentifier>Float </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <SequenceExpr><PrefixOperatorExpr>-<FloatLiteralExpr>0.6 </FloatLiteralExpr></PrefixOperatorExpr><BinaryOperatorExpr>+ </BinaryOperatorExpr><FloatLiteralExpr>0.1 </FloatLiteralExpr><BinaryOperatorExpr>- </BinaryOperatorExpr><FloatLiteralExpr>0.3 </FloatLiteralExpr></SequenceExpr></ReturnStmt>}</CodeBlock>
9+
func bar2(<FunctionParameter>a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, </FunctionParameter><FunctionParameter>b: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, </FunctionParameter><FunctionParameter>c:<SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
10+
func bar3(<FunctionParameter>a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
11+
func bar4(<FunctionParameter>_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
1212
func foo() <CodeBlock>{
1313
var a = <StringLiteralExpr>/*comment*/"ab\(x)c"</StringLiteralExpr>/*comment*/
1414
var b = <PrefixOperatorExpr>/*comment*/+<IntegerLiteralExpr>2</IntegerLiteralExpr></PrefixOperatorExpr><IdentifierExpr>/*comment*/
@@ -93,3 +93,11 @@ private </DeclModifier>struct S<GenericParameterClause><<GenericParameter>A, </G
9393
}</MemberDeclBlock></StructDecl>
9494

9595
protocol P<TypeInheritanceClause>: <InheritedType>class </InheritedType></TypeInheritanceClause>{}
96+
97+
func foo(<FunctionParameter>_ _: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>,</FunctionParameter><FunctionParameter>
98+
a b: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>= <SequenceExpr><IntegerLiteralExpr>3 </IntegerLiteralExpr><BinaryOperatorExpr>+ </BinaryOperatorExpr><IntegerLiteralExpr>2</IntegerLiteralExpr></SequenceExpr>,</FunctionParameter><FunctionParameter>
99+
_ c: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>= <IntegerLiteralExpr>2</IntegerLiteralExpr>,</FunctionParameter><FunctionParameter>
100+
d _: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>= <SequenceExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>2</IntegerLiteralExpr>: <IntegerLiteralExpr>3</IntegerLiteralExpr></TernaryExpr></SequenceExpr>,</FunctionParameter><FunctionParameter><Attribute>
101+
@objc </Attribute>e: <SimpleTypeIdentifier>X </SimpleTypeIdentifier>= <BooleanLiteralExpr>true</BooleanLiteralExpr>,</FunctionParameter><FunctionParameter>
102+
f: inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>,</FunctionParameter><FunctionParameter>
103+
g: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>...</FunctionParameter>) <CodeBlock>{}</CodeBlock>

test/Syntax/round_trip_parse_gen.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,11 @@ private struct S<A, B>: Base where A: B {
9393
}
9494

9595
protocol P: class {}
96+
97+
func foo(_ _: Int,
98+
a b: Int = 3 + 2,
99+
_ c: Int = 2,
100+
d _: Int = true ? 2: 3,
101+
@objc e: X = true,
102+
f: inout Int,
103+
g: Int...) {}

unittests/Syntax/DeclSyntaxTests.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ FunctionParameterSyntax getCannedFunctionParameter() {
219219
SyntaxFactory::makeIntegerLiteralExpr(OneDigits));
220220
auto Comma = SyntaxFactory::makeCommaToken({}, Trivia::spaces(1));
221221

222-
return SyntaxFactory::makeFunctionParameter(ExternalName, LocalName, Colon,
223-
IntAnnotation, NoEllipsis, Equal,
222+
return SyntaxFactory::makeFunctionParameter(None, ExternalName, LocalName, Colon,
223+
Int, NoEllipsis, Equal,
224224
One, Comma);
225225
}
226226

@@ -256,13 +256,13 @@ TEST(DeclSyntaxTests, FunctionParameterGetAPIs) {
256256
SyntaxFactory::makeIntegerLiteralExpr(OneDigits));
257257
auto Comma = SyntaxFactory::makeCommaToken({}, {});
258258

259-
auto Param = SyntaxFactory::makeFunctionParameter(ExternalName, LocalName,
260-
Colon, IntAnnotation,
259+
auto Param = SyntaxFactory::makeFunctionParameter(None, ExternalName, LocalName,
260+
Colon, Int,
261261
NoEllipsis, Equal, One,
262262
Comma);
263263

264-
ASSERT_EQ(ExternalName.getRaw(), Param.getExternalName()->getRaw());
265-
ASSERT_EQ(LocalName.getRaw(), Param.getLocalName().getRaw());
264+
ASSERT_EQ(ExternalName.getRaw(), Param.getFirstName().getRaw());
265+
ASSERT_EQ(LocalName.getRaw(), Param.getSecondName()->getRaw());
266266
ASSERT_EQ(Colon.getRaw(), Param.getColon().getRaw());
267267

268268
auto GottenType = Param.getTypeAnnotation();
@@ -306,10 +306,10 @@ TEST(DeclSyntaxTests, FunctionParameterWithAPIs) {
306306
SmallString<48> Scratch;
307307
llvm::raw_svector_ostream OS(Scratch);
308308
getCannedFunctionParameter()
309-
.withExternalName(ExternalName)
310-
.withLocalName(LocalName)
309+
.withFirstName(ExternalName)
310+
.withSecondName(LocalName)
311311
.withColon(Colon)
312-
.withTypeAnnotation(IntAnnotation)
312+
.withTypeAnnotation(Int)
313313
.withDefaultEquals(Equal)
314314
.withDefaultValue(One)
315315
.withTrailingComma(Comma)

utils/gyb_syntax_support/DeclNodes.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,23 @@
150150
# type '...'? '='? expression? ','?
151151
Node('FunctionParameter', kind='Syntax',
152152
children=[
153-
Child('ExternalName', kind='IdentifierToken',
153+
Child('Attributes', kind='AttributeList',
154+
is_optional=True),
155+
Child('FirstName', kind='Token',
156+
token_choices=[
157+
'IdentifierToken',
158+
'WildcardToken',
159+
]),
160+
# One of these two names needs be optional, we choose the second
161+
# name to avoid backtracking.
162+
Child('SecondName', kind='Token',
163+
token_choices=[
164+
'IdentifierToken',
165+
'WildcardToken',
166+
],
154167
is_optional=True),
155-
Child('LocalName', kind='IdentifierToken'),
156168
Child('Colon', kind='ColonToken'),
157-
Child('TypeAnnotation', kind='TypeAnnotation'),
169+
Child('TypeAnnotation', kind='Type'),
158170
Child('Ellipsis', kind='Token',
159171
is_optional=True),
160172
Child('DefaultEquals', kind='EqualToken',

0 commit comments

Comments
 (0)