Skip to content

Commit c97d80b

Browse files
committed
[AST] NFC: Add convenience constructors for ReturnStmt
Add `ReturnStmt::createParsed` and `createImplict`.
1 parent 12af066 commit c97d80b

29 files changed

+100
-106
lines changed

include/swift/AST/Stmt.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,25 @@ class BraceStmt final : public Stmt,
241241
class ReturnStmt : public Stmt {
242242
SourceLoc ReturnLoc;
243243
Expr *Result;
244-
244+
245+
ReturnStmt(SourceLoc returnLoc, Expr *result, bool isImplicit)
246+
: Stmt(StmtKind::Return, isImplicit), ReturnLoc(returnLoc),
247+
Result(result) {}
248+
245249
public:
246-
ReturnStmt(SourceLoc ReturnLoc, Expr *Result,
247-
llvm::Optional<bool> implicit = llvm::None)
248-
: Stmt(StmtKind::Return, getDefaultImplicitFlag(implicit, ReturnLoc)),
249-
ReturnLoc(ReturnLoc), Result(Result) {}
250+
static ReturnStmt *createParsed(ASTContext &ctx, SourceLoc returnLoc,
251+
Expr *result) {
252+
return new (ctx) ReturnStmt(returnLoc, result, /*isImplicit*/ false);
253+
}
254+
255+
static ReturnStmt *createImplicit(ASTContext &ctx, SourceLoc returnLoc,
256+
Expr *result) {
257+
return new (ctx) ReturnStmt(returnLoc, result, /*isImplicit*/ true);
258+
}
259+
260+
static ReturnStmt *createImplicit(ASTContext &ctx, Expr *result) {
261+
return createImplicit(ctx, SourceLoc(), result);
262+
}
250263

251264
SourceLoc getReturnLoc() const { return ReturnLoc; }
252265

lib/AST/ASTBridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ BridgedReturnStmt BridgedReturnStmt_createParsed(BridgedASTContext cContext,
14641464
BridgedSourceLoc cLoc,
14651465
BridgedNullableExpr expr) {
14661466
ASTContext &context = cContext.unbridged();
1467-
return new (context) ReturnStmt(cLoc.unbridged(), expr.unbridged());
1467+
return ReturnStmt::createParsed(context, cLoc.unbridged(), expr.unbridged());
14681468
}
14691469

14701470
BridgedSwitchStmt BridgedSwitchStmt_createParsed(

lib/AST/Expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,7 @@ FORWARD_SOURCE_LOCS_TO(AutoClosureExpr, Body)
20722072

20732073
void AutoClosureExpr::setBody(Expr *E) {
20742074
auto &Context = getASTContext();
2075-
auto *RS = new (Context) ReturnStmt(SourceLoc(), E);
2075+
auto *RS = ReturnStmt::createImplicit(Context, E);
20762076
Body = BraceStmt::create(Context, E->getStartLoc(), { RS }, E->getEndLoc());
20772077
}
20782078

lib/ClangImporter/ClangImporter.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5155,8 +5155,7 @@ synthesizeBaseClassMethodBody(AbstractFunctionDecl *afd, void *context) {
51555155
baseMemberCallExpr->setType(baseMember->getResultInterfaceType());
51565156
baseMemberCallExpr->setThrows(nullptr);
51575157

5158-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), baseMemberCallExpr,
5159-
/*implicit=*/true);
5158+
auto *returnStmt = ReturnStmt::createImplicit(ctx, baseMemberCallExpr);
51605159

51615160
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
51625161
/*implicit=*/true);
@@ -5450,8 +5449,7 @@ synthesizeBaseClassFieldGetterOrAddressGetterBody(AbstractFunctionDecl *afd,
54505449
ctx, baseGetterMethod->getResultInterfaceType(), resultType,
54515450
returnExpr);
54525451

5453-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), returnExpr,
5454-
/*implicit=*/true);
5452+
auto *returnStmt = ReturnStmt::createImplicit(ctx, returnExpr);
54555453

54565454
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
54575455
/*implicit=*/true);
@@ -6763,8 +6761,7 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
67636761
ctx, specializedFuncCallExpr, thunkDecl->getResultInterfaceType());
67646762
}
67656763

6766-
auto returnStmt = new (ctx)
6767-
ReturnStmt(SourceLoc(), resultExpr, /*implicit=*/true);
6764+
auto *returnStmt = ReturnStmt::createImplicit(ctx, resultExpr);
67686765
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
67696766
/*implicit=*/true);
67706767
return {body, /*isTypeChecked=*/true};
@@ -6886,8 +6883,7 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {
68866883
specializedFuncCallExpr->setType(thunkDecl->getResultInterfaceType());
68876884
specializedFuncCallExpr->setThrows(nullptr);
68886885

6889-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), specializedFuncCallExpr,
6890-
/*implicit=*/true);
6886+
auto *returnStmt = ReturnStmt::createImplicit(ctx, specializedFuncCallExpr);
68916887

68926888
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
68936889
/*implicit=*/true);

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ synthesizeErrorDomainGetterBody(AbstractFunctionDecl *afd, void *context) {
594594
domainDeclRef->setType(
595595
getterDecl->mapTypeIntoContext(swiftValueDecl->getInterfaceType()));
596596

597-
auto ret = new (ctx) ReturnStmt(SourceLoc(), domainDeclRef);
597+
auto *ret = ReturnStmt::createImplicit(ctx, domainDeclRef);
598598
return { BraceStmt::create(ctx, SourceLoc(), {ret}, SourceLoc(), isImplicit),
599599
/*isTypeChecked=*/true };
600600
}

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ synthesizeConstantGetterBody(AbstractFunctionDecl *afd, void *voidContext) {
373373
}
374374

375375
// Create the return statement.
376-
auto ret = new (ctx) ReturnStmt(SourceLoc(), expr);
376+
auto ret = ReturnStmt::createImplicit(ctx, expr);
377377

378378
return {BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc()),
379379
/*isTypeChecked=*/true};
@@ -471,7 +471,7 @@ synthesizeStructDefaultConstructorBody(AbstractFunctionDecl *afd,
471471
auto assign = new (ctx) AssignExpr(lhs, SourceLoc(), call, /*implicit*/ true);
472472
assign->setType(emptyTuple);
473473

474-
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
474+
auto *ret = ReturnStmt::createImplicit(ctx, /*expr*/ nullptr);
475475

476476
// Create the function body.
477477
auto body = BraceStmt::create(ctx, SourceLoc(), {assign, ret}, SourceLoc());
@@ -565,8 +565,7 @@ synthesizeValueConstructorBody(AbstractFunctionDecl *afd, void *context) {
565565
}
566566
}
567567

568-
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
569-
stmts.push_back(ret);
568+
stmts.push_back(ReturnStmt::createImplicit(ctx, /*expr*/ nullptr));
570569

571570
// Create the function body.
572571
auto body = BraceStmt::create(ctx, SourceLoc(), stmts, SourceLoc());
@@ -689,7 +688,7 @@ synthesizeRawValueBridgingConstructorBody(AbstractFunctionDecl *afd,
689688
/*Implicit=*/true);
690689
assign->setType(TupleType::getEmpty(ctx));
691690

692-
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
691+
auto *ret = ReturnStmt::createImplicit(ctx, /*expr*/ nullptr);
693692

694693
auto body = BraceStmt::create(ctx, SourceLoc(), {assign, ret}, SourceLoc());
695694
return {body, /*isTypeChecked=*/true};
@@ -852,7 +851,7 @@ synthesizeUnionFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
852851
CallExpr::createImplicit(ctx, reinterpretCastRefExpr, argList);
853852
reinterpreted->setType(importedFieldDecl->getInterfaceType());
854853
reinterpreted->setThrows(nullptr);
855-
auto ret = new (ctx) ReturnStmt(SourceLoc(), reinterpreted);
854+
auto *ret = ReturnStmt::createImplicit(ctx, reinterpreted);
856855
auto body = BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc(),
857856
/*implicit*/ true);
858857
return {body, /*isTypeChecked*/ true};
@@ -1118,7 +1117,7 @@ synthesizeIndirectFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
11181117
DeclNameLoc(), /*implicit*/ true);
11191118
expr->setType(anonymousInnerFieldDecl->getInterfaceType());
11201119

1121-
auto ret = new (ctx) ReturnStmt(SourceLoc(), expr);
1120+
auto *ret = ReturnStmt::createImplicit(ctx, expr);
11221121
auto body = BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc(),
11231122
/*implicit*/ true);
11241123
return {body, /*isTypeChecked=*/true};
@@ -1242,7 +1241,7 @@ synthesizeEnumRawValueConstructorBody(AbstractFunctionDecl *afd,
12421241
/*implicit*/ true);
12431242
assign->setType(TupleType::getEmpty(ctx));
12441243

1245-
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
1244+
auto *ret = ReturnStmt::createImplicit(ctx, /*expr*/ nullptr);
12461245

12471246
auto body = BraceStmt::create(ctx, SourceLoc(), {assign, ret}, SourceLoc(),
12481247
/*implicit*/ true);
@@ -1310,7 +1309,7 @@ synthesizeEnumRawValueGetterBody(AbstractFunctionDecl *afd, void *context) {
13101309
reinterpreted->setType(rawTy);
13111310
reinterpreted->setThrows(nullptr);
13121311

1313-
auto ret = new (ctx) ReturnStmt(SourceLoc(), reinterpreted);
1312+
auto *ret = ReturnStmt::createImplicit(ctx, reinterpreted);
13141313
auto body = BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc(),
13151314
/*implicit*/ true);
13161315
return {body, /*isTypeChecked=*/true};
@@ -1380,7 +1379,7 @@ synthesizeStructRawValueGetterBody(AbstractFunctionDecl *afd, void *context) {
13801379
result = CoerceExpr::createImplicit(ctx, bridge, computedType);
13811380
}
13821381

1383-
auto ret = new (ctx) ReturnStmt(SourceLoc(), result);
1382+
auto ret = ReturnStmt::createImplicit(ctx, result);
13841383
auto body = BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc(),
13851384
/*implicit*/ true);
13861385
return {body, /*isTypeChecked=*/true};
@@ -1565,8 +1564,7 @@ synthesizeUnwrappingGetterOrAddressGetterBody(AbstractFunctionDecl *afd,
15651564
propertyExpr = SwiftDeclSynthesizer::synthesizeReturnReinterpretCast(
15661565
ctx, getterImpl->getResultInterfaceType(), elementTy, propertyExpr);
15671566

1568-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), propertyExpr,
1569-
/*implicit*/ true);
1567+
auto *returnStmt = ReturnStmt::createImplicit(ctx, propertyExpr);
15701568

15711569
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
15721570
/*implicit*/ true);
@@ -1645,8 +1643,7 @@ synthesizeUnwrappingAddressSetterBody(AbstractFunctionDecl *afd,
16451643
auto *setterImplCallExpr =
16461644
createAccessorImplCallExpr(setterImpl, selfArg, nullptr);
16471645

1648-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), setterImplCallExpr,
1649-
/*implicit*/ true);
1646+
auto *returnStmt = ReturnStmt::createImplicit(ctx, setterImplCallExpr);
16501647

16511648
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
16521649
/*implicit*/ true);
@@ -1884,8 +1881,7 @@ synthesizeSuccessorFuncBody(AbstractFunctionDecl *afd, void *context) {
18841881
/*implicit*/ true);
18851882
copyRefRValueExpr->setType(copyDecl->getInterfaceType());
18861883

1887-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), copyRefRValueExpr,
1888-
/*implicit*/ true);
1884+
auto *returnStmt = ReturnStmt::createImplicit(ctx, copyRefRValueExpr);
18891885

18901886
auto body = BraceStmt::create(ctx, SourceLoc(),
18911887
{
@@ -1974,8 +1970,7 @@ synthesizeOperatorMethodBody(AbstractFunctionDecl *afd, void *context) {
19741970
callExpr->setType(funcDecl->getResultInterfaceType());
19751971
callExpr->setThrows(nullptr);
19761972

1977-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), callExpr,
1978-
/*implicit*/ true);
1973+
auto *returnStmt = ReturnStmt::createImplicit(ctx, callExpr);
19791974

19801975
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
19811976
/*implicit*/ true);
@@ -2061,10 +2056,9 @@ synthesizeComputedGetterFromCXXMethod(AbstractFunctionDecl *afd,
20612056
auto selfArg = createSelfArg(accessor);
20622057

20632058
auto *getterImplCallExpr = createAccessorImplCallExpr(method, selfArg);
2064-
auto returnStmt =
2065-
new (method->getASTContext()) ReturnStmt(SourceLoc(), getterImplCallExpr);
2066-
auto body = BraceStmt::create(method->getASTContext(), SourceLoc(),
2067-
{returnStmt}, SourceLoc());
2059+
auto &ctx = method->getASTContext();
2060+
auto *returnStmt = ReturnStmt::createImplicit(ctx, getterImplCallExpr);
2061+
auto *body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc());
20682062

20692063
return {body, /*isTypeChecked*/ true};
20702064
}
@@ -2218,8 +2212,7 @@ synthesizeDefaultArgumentBody(AbstractFunctionDecl *afd, void *context) {
22182212
initCall->setThrows(nullptr);
22192213

22202214
// Synthesize `return __cxx__defaultArg_XYZ()`.
2221-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), initCall,
2222-
/*implicit=*/true);
2215+
auto *returnStmt = ReturnStmt::createImplicit(ctx, initCall);
22232216

22242217
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
22252218
/*implicit=*/true);

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8670,8 +8670,7 @@ Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
86708670
SourceLoc LBraceLoc, RBraceLoc;
86718671
LBraceLoc = consumeToken(tok::l_brace);
86728672
auto *CCE = new (Context) CodeCompletionExpr(Tok.getLoc());
8673-
auto *Return =
8674-
new (Context) ReturnStmt(SourceLoc(), CCE, /*implicit=*/true);
8673+
auto *Return = ReturnStmt::createImplicit(Context, CCE);
86758674
CodeCompletionCallbacks->setParsedDecl(accessor);
86768675
CodeCompletionCallbacks->completeAccessorBeginning(CCE);
86778676
RBraceLoc = Tok.getLoc();
@@ -8725,15 +8724,15 @@ Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
87258724
}
87268725
}
87278726
if (isa<FuncDecl>(AFD)) {
8728-
auto RS = new (Context) ReturnStmt(SourceLoc(), E);
8727+
auto RS = ReturnStmt::createImplicit(Context, E);
87298728
BS->setLastElement(RS);
87308729
AFD->setHasSingleExpressionBody();
87318730
AFD->setSingleExpressionBody(E);
87328731
} else if (auto *F = dyn_cast<ConstructorDecl>(AFD)) {
87338732
if (F->isFailable() && isa<NilLiteralExpr>(E)) {
87348733
// If it's a nil literal, just insert return. This is the only
87358734
// legal thing to return.
8736-
auto RS = new (Context) ReturnStmt(E->getStartLoc(), E, /*implicit*/ true);
8735+
auto RS = ReturnStmt::createImplicit(Context, E->getStartLoc(), E);
87378736
BS->setLastElement(RS);
87388737
AFD->setHasSingleExpressionBody();
87398738
AFD->setSingleExpressionBody(E);

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3072,7 +3072,7 @@ ParserResult<Expr> Parser::parseExprClosure() {
30723072
// Create the wrapping return.
30733073
hasSingleExpressionBody = true;
30743074
auto returnExpr = Element.get<Expr*>();
3075-
BS->setLastElement(new (Context) ReturnStmt(SourceLoc(), returnExpr));
3075+
BS->setLastElement(ReturnStmt::createImplicit(Context, returnExpr));
30763076
}
30773077
}
30783078
}

lib/Parse/ParseStmt.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,8 @@ ParserResult<Stmt> Parser::parseStmtReturn(SourceLoc tryLoc) {
764764

765765
if (Tok.is(tok::code_complete)) {
766766
auto CCE = new (Context) CodeCompletionExpr(Tok.getLoc());
767-
auto Result = makeParserResult(new (Context) ReturnStmt(ReturnLoc, CCE));
767+
auto Result =
768+
makeParserResult(ReturnStmt::createParsed(Context, ReturnLoc, CCE));
768769
if (CodeCompletionCallbacks) {
769770
CodeCompletionCallbacks->completeReturnStmt(CCE);
770771
}
@@ -825,13 +826,15 @@ ParserResult<Stmt> Parser::parseStmtReturn(SourceLoc tryLoc) {
825826
}
826827

827828
return makeParserResult(
828-
Result, new (Context) ReturnStmt(ReturnLoc, Result.getPtrOrNull()));
829+
Result,
830+
ReturnStmt::createParsed(Context, ReturnLoc, Result.getPtrOrNull()));
829831
}
830832

831833
if (tryLoc.isValid())
832834
diagnose(tryLoc, diag::try_on_stmt, "return");
833835

834-
return makeParserResult(new (Context) ReturnStmt(ReturnLoc, nullptr));
836+
return makeParserResult(
837+
ReturnStmt::createParsed(Context, ReturnLoc, nullptr));
835838
}
836839

837840
/// parseStmtYield

lib/Sema/BuilderTransform.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ class ResultBuilderTransform
367367
{buildBlockResult}, {Identifier()});
368368
}
369369

370-
elements.push_back(new (ctx) ReturnStmt(resultLoc, buildBlockResult,
371-
/*Implicit=*/true));
370+
elements.push_back(
371+
ReturnStmt::createImplicit(ctx, resultLoc, buildBlockResult));
372372
}
373373

374374
return std::make_pair(false, UnsupportedElt());

0 commit comments

Comments
 (0)