Skip to content

libSyntax: support function parameter nodes. #13324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
return parseList(tok::r_paren, leftParenLoc, rightParenLoc,
/*AllowSepAfterLast=*/false,
diag::expected_rparen_parameter,
SyntaxKind::Unknown,
SyntaxKind::FunctionParameterList,
[&]() -> ParserStatus {
ParsedParameter param;
ParserStatus status;
Expand Down
2 changes: 2 additions & 0 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ static SyntaxKind getListElementKind(SyntaxKind ListKind) {
return SyntaxKind::DictionaryElement;
case SyntaxKind::TupleElementList:
return SyntaxKind::TupleElement;
case SyntaxKind::FunctionParameterList:
return SyntaxKind::FunctionParameter;
default:
return SyntaxKind::Unknown;
}
Expand Down
18 changes: 13 additions & 5 deletions test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// RUN: diff -u %S/Outputs/round_trip_parse_gen.swift.withkinds %t.withkinds

class C {
func bar(_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>) <CodeBlock>{}</CodeBlock>
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>
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>
func bar3(a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
func bar4(_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
func bar(<FunctionParameter>_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) <CodeBlock>{}</CodeBlock>
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>
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>
func bar3(<FunctionParameter>a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
func bar4(<FunctionParameter>_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) -> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><CodeBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</CodeBlock>
func foo() <CodeBlock>{
var a = <StringLiteralExpr>/*comment*/"ab\(x)c"</StringLiteralExpr>/*comment*/
var b = <PrefixOperatorExpr>/*comment*/+<IntegerLiteralExpr>2</IntegerLiteralExpr></PrefixOperatorExpr><IdentifierExpr>/*comment*/
Expand Down Expand Up @@ -93,3 +93,11 @@ private </DeclModifier>struct S<GenericParameterClause><<GenericParameter>A, </G
}</MemberDeclBlock></StructDecl>

protocol P<TypeInheritanceClause>: <InheritedType>class </InheritedType></TypeInheritanceClause>{}

func foo(<FunctionParameter>_ _: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>,</FunctionParameter><FunctionParameter>
a b: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>= <SequenceExpr><IntegerLiteralExpr>3 </IntegerLiteralExpr><BinaryOperatorExpr>+ </BinaryOperatorExpr><IntegerLiteralExpr>2</IntegerLiteralExpr></SequenceExpr>,</FunctionParameter><FunctionParameter>
_ c: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>= <IntegerLiteralExpr>2</IntegerLiteralExpr>,</FunctionParameter><FunctionParameter>
d _: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>= <SequenceExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>2</IntegerLiteralExpr>: <IntegerLiteralExpr>3</IntegerLiteralExpr></TernaryExpr></SequenceExpr>,</FunctionParameter><FunctionParameter><Attribute>
@objc </Attribute>e: <SimpleTypeIdentifier>X </SimpleTypeIdentifier>= <BooleanLiteralExpr>true</BooleanLiteralExpr>,</FunctionParameter><FunctionParameter>
f: inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>,</FunctionParameter><FunctionParameter>
g: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>...</FunctionParameter>) <CodeBlock>{}</CodeBlock>
8 changes: 8 additions & 0 deletions test/Syntax/round_trip_parse_gen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ private struct S<A, B>: Base where A: B {
}

protocol P: class {}

func foo(_ _: Int,
a b: Int = 3 + 2,
_ c: Int = 2,
d _: Int = true ? 2: 3,
@objc e: X = true,
f: inout Int,
g: Int...) {}
18 changes: 9 additions & 9 deletions unittests/Syntax/DeclSyntaxTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ FunctionParameterSyntax getCannedFunctionParameter() {
SyntaxFactory::makeIntegerLiteralExpr(OneDigits));
auto Comma = SyntaxFactory::makeCommaToken({}, Trivia::spaces(1));

return SyntaxFactory::makeFunctionParameter(ExternalName, LocalName, Colon,
IntAnnotation, NoEllipsis, Equal,
return SyntaxFactory::makeFunctionParameter(None, ExternalName, LocalName, Colon,
Int, NoEllipsis, Equal,
One, Comma);
}

Expand Down Expand Up @@ -256,13 +256,13 @@ TEST(DeclSyntaxTests, FunctionParameterGetAPIs) {
SyntaxFactory::makeIntegerLiteralExpr(OneDigits));
auto Comma = SyntaxFactory::makeCommaToken({}, {});

auto Param = SyntaxFactory::makeFunctionParameter(ExternalName, LocalName,
Colon, IntAnnotation,
auto Param = SyntaxFactory::makeFunctionParameter(None, ExternalName, LocalName,
Colon, Int,
NoEllipsis, Equal, One,
Comma);

ASSERT_EQ(ExternalName.getRaw(), Param.getExternalName()->getRaw());
ASSERT_EQ(LocalName.getRaw(), Param.getLocalName().getRaw());
ASSERT_EQ(ExternalName.getRaw(), Param.getFirstName().getRaw());
ASSERT_EQ(LocalName.getRaw(), Param.getSecondName()->getRaw());
ASSERT_EQ(Colon.getRaw(), Param.getColon().getRaw());

auto GottenType = Param.getTypeAnnotation();
Expand Down Expand Up @@ -306,10 +306,10 @@ TEST(DeclSyntaxTests, FunctionParameterWithAPIs) {
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
getCannedFunctionParameter()
.withExternalName(ExternalName)
.withLocalName(LocalName)
.withFirstName(ExternalName)
.withSecondName(LocalName)
.withColon(Colon)
.withTypeAnnotation(IntAnnotation)
.withTypeAnnotation(Int)
.withDefaultEquals(Equal)
.withDefaultValue(One)
.withTrailingComma(Comma)
Expand Down
18 changes: 15 additions & 3 deletions utils/gyb_syntax_support/DeclNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,23 @@
# type '...'? '='? expression? ','?
Node('FunctionParameter', kind='Syntax',
children=[
Child('ExternalName', kind='IdentifierToken',
Child('Attributes', kind='AttributeList',
is_optional=True),
Child('FirstName', kind='Token',
token_choices=[
'IdentifierToken',
'WildcardToken',
]),
# One of these two names needs be optional, we choose the second
# name to avoid backtracking.
Child('SecondName', kind='Token',
token_choices=[
'IdentifierToken',
'WildcardToken',
],
is_optional=True),
Child('LocalName', kind='IdentifierToken'),
Child('Colon', kind='ColonToken'),
Child('TypeAnnotation', kind='TypeAnnotation'),
Child('TypeAnnotation', kind='Type'),
Child('Ellipsis', kind='Token',
is_optional=True),
Child('DefaultEquals', kind='EqualToken',
Expand Down