Skip to content

Commit ded74db

Browse files
committed
NFC: Pass through an ASTContext to IsSingleValueStmtRequest
1 parent 2042dc1 commit ded74db

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

include/swift/AST/Stmt.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ class alignas(8) Stmt : public ASTAllocated<Stmt> {
144144

145145
/// Whether the statement can produce a single value, and as such may be
146146
/// treated as an expression.
147-
IsSingleValueStmtResult mayProduceSingleValue(Evaluator &eval) const;
148147
IsSingleValueStmtResult mayProduceSingleValue(ASTContext &ctx) const;
149148

150149
/// isImplicit - Determines whether this statement was implicitly-generated,

include/swift/AST/TypeCheckRequests.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4018,18 +4018,21 @@ class IsSingleValueStmtResult {
40184018
};
40194019

40204020
/// Computes whether a given statement can be treated as a SingleValueStmtExpr.
4021+
///
4022+
// TODO: We ought to consider storing a reference to the ASTContext on the
4023+
// evaluator.
40214024
class IsSingleValueStmtRequest
40224025
: public SimpleRequest<IsSingleValueStmtRequest,
4023-
IsSingleValueStmtResult(const Stmt *),
4026+
IsSingleValueStmtResult(const Stmt *, ASTContext *),
40244027
RequestFlags::Cached> {
40254028
public:
40264029
using SimpleRequest::SimpleRequest;
40274030

40284031
private:
40294032
friend SimpleRequest;
40304033

4031-
IsSingleValueStmtResult
4032-
evaluate(Evaluator &evaluator, const Stmt *stmt) const;
4034+
IsSingleValueStmtResult evaluate(Evaluator &evaluator, const Stmt *stmt,
4035+
ASTContext *ctx) const;
40334036

40344037
public:
40354038
bool isCached() const { return true; }

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ SWIFT_REQUEST(TypeChecker, PreCheckReturnStmtRequest,
445445
Stmt *(ReturnStmt *, DeclContext *),
446446
Cached, NoLocationInfo)
447447
SWIFT_REQUEST(TypeChecker, IsSingleValueStmtRequest,
448-
IsSingleValueStmtResult(const Stmt *),
448+
IsSingleValueStmtResult(const Stmt *, ASTContext *),
449449
Cached, NoLocationInfo)
450450
SWIFT_REQUEST(TypeChecker, MacroDefinitionRequest,
451451
MacroDefinition(MacroDecl *),

lib/AST/Stmt.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,9 @@ Stmt *BraceStmt::getSingleActiveStatement() const {
333333
return getSingleActiveElement().dyn_cast<Stmt *>();
334334
}
335335

336-
IsSingleValueStmtResult Stmt::mayProduceSingleValue(Evaluator &eval) const {
337-
return evaluateOrDefault(eval, IsSingleValueStmtRequest{this},
338-
IsSingleValueStmtResult::circularReference());
339-
}
340-
341336
IsSingleValueStmtResult Stmt::mayProduceSingleValue(ASTContext &ctx) const {
342-
return mayProduceSingleValue(ctx.evaluator);
337+
return evaluateOrDefault(ctx.evaluator, IsSingleValueStmtRequest{this, &ctx},
338+
IsSingleValueStmtResult::circularReference());
343339
}
344340

345341
SourceLoc ReturnStmt::getStartLoc() const {

lib/Sema/TypeCheckStmt.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,7 +2680,7 @@ PreCheckFunctionBodyRequest::evaluate(Evaluator &evaluator,
26802680
// single expression return, do so now.
26812681
if (!func->getResultInterfaceType()->isVoid()) {
26822682
if (auto *S = body->getSingleActiveStatement()) {
2683-
if (S->mayProduceSingleValue(evaluator)) {
2683+
if (S->mayProduceSingleValue(ctx)) {
26842684
auto *SVE = SingleValueStmtExpr::createWithWrappedBranches(
26852685
ctx, S, /*DC*/ func, /*mustBeExpr*/ false);
26862686
auto *RS = new (ctx) ReturnStmt(SourceLoc(), SVE);
@@ -2927,7 +2927,7 @@ static bool doesBraceEndWithThrow(BraceStmt *BS) {
29272927

29282928
/// Whether the given brace statement is considered to produce a result for
29292929
/// an if/switch expression.
2930-
static bool doesBraceProduceResult(BraceStmt *BS, Evaluator &eval) {
2930+
static bool doesBraceProduceResult(BraceStmt *BS, ASTContext &ctx) {
29312931
if (BS->empty())
29322932
return false;
29332933

@@ -2939,14 +2939,14 @@ static bool doesBraceProduceResult(BraceStmt *BS, Evaluator &eval) {
29392939
return true;
29402940

29412941
if (auto *S = BS->getSingleActiveStatement()) {
2942-
if (S->mayProduceSingleValue(eval))
2942+
if (S->mayProduceSingleValue(ctx))
29432943
return true;
29442944
}
29452945
return SingleValueStmtExpr::hasResult(BS);
29462946
}
29472947

29482948
IsSingleValueStmtResult
2949-
areBranchesValidForSingleValueStmt(Evaluator &eval, ArrayRef<Stmt *> branches) {
2949+
areBranchesValidForSingleValueStmt(ASTContext &ctx, ArrayRef<Stmt *> branches) {
29502950
TinyPtrVector<Stmt *> invalidJumps;
29512951
TinyPtrVector<Stmt *> unterminatedBranches;
29522952
JumpOutOfContextFinder jumpFinder(invalidJumps);
@@ -2963,7 +2963,7 @@ areBranchesValidForSingleValueStmt(Evaluator &eval, ArrayRef<Stmt *> branches) {
29632963
BS->walk(jumpFinder);
29642964

29652965
// Check to see if a result is produced from the branch.
2966-
if (doesBraceProduceResult(BS, eval)) {
2966+
if (doesBraceProduceResult(BS, ctx)) {
29672967
hadResult = true;
29682968
continue;
29692969
}
@@ -2989,7 +2989,11 @@ areBranchesValidForSingleValueStmt(Evaluator &eval, ArrayRef<Stmt *> branches) {
29892989
}
29902990

29912991
IsSingleValueStmtResult
2992-
IsSingleValueStmtRequest::evaluate(Evaluator &eval, const Stmt *S) const {
2992+
IsSingleValueStmtRequest::evaluate(Evaluator &eval, const Stmt *S,
2993+
ASTContext *_ctx) const {
2994+
assert(_ctx);
2995+
auto &ctx = *_ctx;
2996+
29932997
if (!isa<IfStmt>(S) && !isa<SwitchStmt>(S))
29942998
return IsSingleValueStmtResult::unhandledStmt();
29952999

@@ -3004,11 +3008,11 @@ IsSingleValueStmtRequest::evaluate(Evaluator &eval, const Stmt *S) const {
30043008
return IsSingleValueStmtResult::nonExhaustiveIf();
30053009

30063010
SmallVector<Stmt *, 4> scratch;
3007-
return areBranchesValidForSingleValueStmt(eval, IS->getBranches(scratch));
3011+
return areBranchesValidForSingleValueStmt(ctx, IS->getBranches(scratch));
30083012
}
30093013
if (auto *SS = dyn_cast<SwitchStmt>(S)) {
30103014
SmallVector<Stmt *, 4> scratch;
3011-
return areBranchesValidForSingleValueStmt(eval, SS->getBranches(scratch));
3015+
return areBranchesValidForSingleValueStmt(ctx, SS->getBranches(scratch));
30123016
}
30133017
llvm_unreachable("Unhandled case");
30143018
}

0 commit comments

Comments
 (0)