Skip to content

Commit e616b2a

Browse files
rintaronkcsgexi
authored andcommitted
[Syntax] Use initializer clause syntax node for function parameter (#13430)
1 parent 79e224c commit e616b2a

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ static ParserStatus parseDefaultArgument(Parser &P,
6666
unsigned argIndex,
6767
Expr *&init,
6868
Parser::ParameterContextKind paramContext) {
69+
SyntaxParsingContext DefaultArgContext(P.SyntaxContext,
70+
SyntaxKind::InitializerClause);
6971
SourceLoc equalLoc = P.consumeToken(tok::equal);
7072

7173
// Enter a fresh default-argument context with a meaningless parent.
@@ -1144,4 +1146,3 @@ bool Parser::canParseTypedPattern() {
11441146
return true;
11451147
}
11461148

1147-

lib/Parse/ParseType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,8 @@ ParserResult<TupleTypeRepr> Parser::parseTypeTupleBody() {
948948
// Parse '= expr' here so we can complain about it directly, rather
949949
// than dying when we see it.
950950
if (Tok.is(tok::equal)) {
951-
SyntaxParsingContext InitContext(SyntaxContext, SyntaxKind::Initializer);
951+
SyntaxParsingContext InitContext(SyntaxContext,
952+
SyntaxKind::InitializerClause);
952953
SourceLoc equalLoc = consumeToken(tok::equal);
953954
auto init = parseExpr(diag::expected_init_value);
954955
auto inFlight = diagnose(equalLoc, diag::tuple_type_init);

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ typealias C = <ArrayType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>]</Arr
6565
typealias D = <DictionaryType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>: <SimpleTypeIdentifier>String</SimpleTypeIdentifier>]</DictionaryType>
6666
typealias E = <MetatypeType><OptionalType><SimpleTypeIdentifier>Int</SimpleTypeIdentifier>?</OptionalType>.Protocol</MetatypeType>
6767
typealias F = <MetatypeType><ImplicitlyUnwrappedOptionalType><ArrayType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>]</ArrayType>!</ImplicitlyUnwrappedOptionalType>.Type</MetatypeType>
68-
typealias G = <FunctionType>(<TupleTypeElement>a x: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, </TupleTypeElement><TupleTypeElement>_ y: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>... <Initializer>= <IntegerLiteralExpr>1</IntegerLiteralExpr></Initializer></TupleTypeElement>) throw -> <FunctionType>() -> <TupleType>()</TupleType></FunctionType></FunctionType>
68+
typealias G = <FunctionType>(<TupleTypeElement>a x: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, </TupleTypeElement><TupleTypeElement>_ y: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>... <InitializerClause>= <IntegerLiteralExpr>1</IntegerLiteralExpr></InitializerClause></TupleTypeElement>) throw -> <FunctionType>() -> <TupleType>()</TupleType></FunctionType></FunctionType>
6969
typealias H = <FunctionType>() rethrows -> <TupleType>()</TupleType></FunctionType><StructDecl>
7070

7171
struct foo <MemberDeclBlock>{<StructDecl>
@@ -97,10 +97,10 @@ private </DeclModifier>struct S<GenericParameterClause><<GenericParameter>A, </G
9797
protocol P<TypeInheritanceClause>: <InheritedType>class </InheritedType></TypeInheritanceClause><MemberDeclBlock>{}</MemberDeclBlock></ProtocolDecl><FunctionDecl>
9898

9999
func foo<FunctionSignature><ParameterClause>(<FunctionParameter>_ _: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>,</FunctionParameter><FunctionParameter>
100-
a b: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>= <SequenceExpr><IntegerLiteralExpr>3 </IntegerLiteralExpr><BinaryOperatorExpr>+ </BinaryOperatorExpr><IntegerLiteralExpr>2</IntegerLiteralExpr></SequenceExpr>,</FunctionParameter><FunctionParameter>
101-
_ c: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>= <IntegerLiteralExpr>2</IntegerLiteralExpr>,</FunctionParameter><FunctionParameter>
102-
d _: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>= <SequenceExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>2</IntegerLiteralExpr>: <IntegerLiteralExpr>3</IntegerLiteralExpr></TernaryExpr></SequenceExpr>,</FunctionParameter><FunctionParameter><Attribute>
103-
@objc </Attribute>e: <SimpleTypeIdentifier>X </SimpleTypeIdentifier>= <BooleanLiteralExpr>true</BooleanLiteralExpr>,</FunctionParameter><FunctionParameter>
100+
a b: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><InitializerClause>= <SequenceExpr><IntegerLiteralExpr>3 </IntegerLiteralExpr><BinaryOperatorExpr>+ </BinaryOperatorExpr><IntegerLiteralExpr>2</IntegerLiteralExpr></SequenceExpr></InitializerClause>,</FunctionParameter><FunctionParameter>
101+
_ c: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><InitializerClause>= <IntegerLiteralExpr>2</IntegerLiteralExpr></InitializerClause>,</FunctionParameter><FunctionParameter>
102+
d _: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><InitializerClause>= <SequenceExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>2</IntegerLiteralExpr>: <IntegerLiteralExpr>3</IntegerLiteralExpr></TernaryExpr></SequenceExpr></InitializerClause>,</FunctionParameter><FunctionParameter><Attribute>
103+
@objc </Attribute>e: <SimpleTypeIdentifier>X </SimpleTypeIdentifier><InitializerClause>= <BooleanLiteralExpr>true</BooleanLiteralExpr></InitializerClause>,</FunctionParameter><FunctionParameter>
104104
f: inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>,</FunctionParameter><FunctionParameter>
105105
g: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>...</FunctionParameter>) </ParameterClause>throws <ReturnClause>-> <DictionaryType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>: <SimpleTypeIdentifier>String</SimpleTypeIdentifier>] </DictionaryType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl>
106106

unittests/Syntax/DeclSyntaxTests.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,12 @@ FunctionParameterSyntax getCannedFunctionParameter() {
217217
auto OneDigits = SyntaxFactory::makeIntegerLiteral("1", {}, {});
218218
auto One = SyntaxFactory::makePrefixOperatorExpr(Sign,
219219
SyntaxFactory::makeIntegerLiteralExpr(OneDigits));
220+
auto DefaultArg = SyntaxFactory::makeInitializerClause(Equal, One);
220221
auto Comma = SyntaxFactory::makeCommaToken({}, Trivia::spaces(1));
221222

222-
return SyntaxFactory::makeFunctionParameter(None, ExternalName, LocalName, Colon,
223-
Int, NoEllipsis, Equal,
224-
One, Comma);
223+
return SyntaxFactory::makeFunctionParameter(None, ExternalName, LocalName,
224+
Colon, Int, NoEllipsis,
225+
DefaultArg, Comma);
225226
}
226227

227228
TEST(DeclSyntaxTests, FunctionParameterMakeAPIs) {
@@ -254,11 +255,12 @@ TEST(DeclSyntaxTests, FunctionParameterGetAPIs) {
254255
auto OneDigits = SyntaxFactory::makeIntegerLiteral("1", {}, {});
255256
auto One = SyntaxFactory::makePrefixOperatorExpr(Sign,
256257
SyntaxFactory::makeIntegerLiteralExpr(OneDigits));
258+
auto DefaultArg = SyntaxFactory::makeInitializerClause(Equal, One);
257259
auto Comma = SyntaxFactory::makeCommaToken({}, {});
258260

259-
auto Param = SyntaxFactory::makeFunctionParameter(None, ExternalName, LocalName,
260-
Colon, Int,
261-
NoEllipsis, Equal, One,
261+
auto Param = SyntaxFactory::makeFunctionParameter(None, ExternalName,
262+
LocalName, Colon, Int,
263+
NoEllipsis, DefaultArg,
262264
Comma);
263265

264266
ASSERT_EQ(ExternalName.getRaw(), Param.getFirstName().getRaw());
@@ -269,21 +271,21 @@ TEST(DeclSyntaxTests, FunctionParameterGetAPIs) {
269271
auto GottenType2 = Param.getTypeAnnotation();
270272
ASSERT_TRUE(GottenType.hasSameIdentityAs(GottenType2));
271273

272-
ASSERT_EQ(Equal.getRaw(), Param.getDefaultEquals()->getRaw());
274+
ASSERT_EQ(DefaultArg.getRaw(), Param.getDefaultArgument()->getRaw());
273275

274-
auto GottenDefaultValue = Param.getDefaultValue().getValue();
275-
auto GottenDefaultValue2 = Param.getDefaultValue().getValue();
276+
auto GottenDefaultValue = Param.getDefaultArgument()->getValue();
277+
auto GottenDefaultValue2 = Param.getDefaultArgument()->getValue();
276278
ASSERT_TRUE(GottenDefaultValue.hasSameIdentityAs(GottenDefaultValue2));
277279

278280
ASSERT_EQ(Comma.getRaw(), Param.getTrailingComma()->getRaw());
279281

280282
// Test that llvm::None is returned for non-token missing children:
281283
auto Decimated = Param
282284
.withTypeAnnotation(llvm::None)
283-
.withDefaultValue(llvm::None);
285+
.withDefaultArgument(llvm::None);
284286

285287
ASSERT_TRUE(Decimated.getTypeAnnotation().isMissing());
286-
ASSERT_FALSE(Decimated.getDefaultValue().hasValue());
288+
ASSERT_FALSE(Decimated.getDefaultArgument().hasValue());
287289
}
288290

289291
TEST(DeclSyntaxTests, FunctionParameterWithAPIs) {
@@ -300,6 +302,7 @@ TEST(DeclSyntaxTests, FunctionParameterWithAPIs) {
300302
auto NoSign = TokenSyntax::missingToken(tok::oper_prefix, "");
301303
auto OneDigits = SyntaxFactory::makeIntegerLiteral("1", {}, {});
302304
auto One = SyntaxFactory::makeIntegerLiteralExpr(OneDigits);
305+
auto DefaultArg = SyntaxFactory::makeInitializerClause(Equal, One);
303306
auto Comma = SyntaxFactory::makeCommaToken({}, {});
304307

305308
{
@@ -310,8 +313,7 @@ TEST(DeclSyntaxTests, FunctionParameterWithAPIs) {
310313
.withSecondName(LocalName)
311314
.withColon(Colon)
312315
.withTypeAnnotation(Int)
313-
.withDefaultEquals(Equal)
314-
.withDefaultValue(One)
316+
.withDefaultArgument(DefaultArg)
315317
.withTrailingComma(Comma)
316318
.print(OS);
317319
ASSERT_EQ(OS.str().str(), "for integer : Int = 1,");
@@ -321,9 +323,9 @@ TEST(DeclSyntaxTests, FunctionParameterWithAPIs) {
321323
llvm::raw_svector_ostream OS(Scratch);
322324
getCannedFunctionParameter()
323325
.withTypeAnnotation(llvm::None)
324-
.withDefaultValue(llvm::None)
326+
.withDefaultArgument(llvm::None)
325327
.print(OS);
326-
ASSERT_EQ(OS.str().str(), "with radius: = , ");
328+
ASSERT_EQ(OS.str().str(), "with radius: , ");
327329
}
328330
}
329331

utils/gyb_syntax_support/DeclNodes.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@
167167
Child('Body', kind='StmtList')
168168
]),
169169

170+
# initializer -> '=' expr
171+
Node('InitializerClause', kind='Syntax',
172+
children=[
173+
Child('Equal', kind='EqualToken'),
174+
Child('Value', kind='Expr'),
175+
]),
176+
170177
# parameter ->
171178
# external-parameter-name? local-parameter-name ':'
172179
# type '...'? '='? expression? ','?
@@ -191,9 +198,7 @@
191198
Child('TypeAnnotation', kind='Type'),
192199
Child('Ellipsis', kind='Token',
193200
is_optional=True),
194-
Child('DefaultEquals', kind='EqualToken',
195-
is_optional=True),
196-
Child('DefaultValue', kind='Expr',
201+
Child('DefaultArgument', kind='InitializerClause',
197202
is_optional=True),
198203
Child('TrailingComma', kind='CommaToken',
199204
is_optional=True),

utils/gyb_syntax_support/TypeNodes.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@
7979
Child('ExclamationMark', kind='ExclamationMarkToken'),
8080
]),
8181

82-
Node('Initializer', kind='Syntax',
83-
children=[
84-
Child('Equal', kind='EqualToken'),
85-
Child('Value', kind='Expr'),
86-
]),
87-
8882
# tuple-type-element -> identifier? ':'? type-annotation ','?
8983
Node('TupleTypeElement', kind='Syntax',
9084
children=[
@@ -107,7 +101,7 @@
107101
Child('Type', kind='Type'),
108102
Child('Ellipsis', kind='Token',
109103
is_optional=True),
110-
Child('Initializer', kind='Initializer',
104+
Child('Initializer', kind='InitializerClause',
111105
is_optional=True),
112106
Child('TrailingComma', kind='CommaToken',
113107
is_optional=True),

0 commit comments

Comments
 (0)