Skip to content

Commit 20d2667

Browse files
committed
NFC: Pass through an ASTContext to IsSingleValueStmtRequest
1 parent 231e179 commit 20d2667

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
@@ -4039,18 +4039,21 @@ class IsSingleValueStmtResult {
40394039
};
40404040

40414041
/// Computes whether a given statement can be treated as a SingleValueStmtExpr.
4042+
///
4043+
// TODO: We ought to consider storing a reference to the ASTContext on the
4044+
// evaluator.
40424045
class IsSingleValueStmtRequest
40434046
: public SimpleRequest<IsSingleValueStmtRequest,
4044-
IsSingleValueStmtResult(const Stmt *),
4047+
IsSingleValueStmtResult(const Stmt *, ASTContext *),
40454048
RequestFlags::Cached> {
40464049
public:
40474050
using SimpleRequest::SimpleRequest;
40484051

40494052
private:
40504053
friend SimpleRequest;
40514054

4052-
IsSingleValueStmtResult
4053-
evaluate(Evaluator &evaluator, const Stmt *stmt) const;
4055+
IsSingleValueStmtResult evaluate(Evaluator &evaluator, const Stmt *stmt,
4056+
ASTContext *ctx) const;
40544057

40554058
public:
40564059
bool isCached() const { return true; }

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ SWIFT_REQUEST(TypeChecker, PreCheckReturnStmtRequest,
448448
Stmt *(ReturnStmt *, DeclContext *),
449449
Cached, NoLocationInfo)
450450
SWIFT_REQUEST(TypeChecker, IsSingleValueStmtRequest,
451-
IsSingleValueStmtResult(const Stmt *),
451+
IsSingleValueStmtResult(const Stmt *, ASTContext *),
452452
Cached, NoLocationInfo)
453453
SWIFT_REQUEST(TypeChecker, MacroDefinitionRequest,
454454
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
@@ -2696,7 +2696,7 @@ PreCheckFunctionBodyRequest::evaluate(Evaluator &evaluator,
26962696
// single expression return, do so now.
26972697
if (!func->getResultInterfaceType()->isVoid()) {
26982698
if (auto *S = body->getSingleActiveStatement()) {
2699-
if (S->mayProduceSingleValue(evaluator)) {
2699+
if (S->mayProduceSingleValue(ctx)) {
27002700
auto *SVE = SingleValueStmtExpr::createWithWrappedBranches(
27012701
ctx, S, /*DC*/ func, /*mustBeExpr*/ false);
27022702
auto *RS = new (ctx) ReturnStmt(SourceLoc(), SVE);
@@ -2943,7 +2943,7 @@ static bool doesBraceEndWithThrow(BraceStmt *BS) {
29432943

29442944
/// Whether the given brace statement is considered to produce a result for
29452945
/// an if/switch expression.
2946-
static bool doesBraceProduceResult(BraceStmt *BS, Evaluator &eval) {
2946+
static bool doesBraceProduceResult(BraceStmt *BS, ASTContext &ctx) {
29472947
if (BS->empty())
29482948
return false;
29492949

@@ -2955,14 +2955,14 @@ static bool doesBraceProduceResult(BraceStmt *BS, Evaluator &eval) {
29552955
return true;
29562956

29572957
if (auto *S = BS->getSingleActiveStatement()) {
2958-
if (S->mayProduceSingleValue(eval))
2958+
if (S->mayProduceSingleValue(ctx))
29592959
return true;
29602960
}
29612961
return SingleValueStmtExpr::hasResult(BS);
29622962
}
29632963

29642964
IsSingleValueStmtResult
2965-
areBranchesValidForSingleValueStmt(Evaluator &eval, ArrayRef<Stmt *> branches) {
2965+
areBranchesValidForSingleValueStmt(ASTContext &ctx, ArrayRef<Stmt *> branches) {
29662966
TinyPtrVector<Stmt *> invalidJumps;
29672967
TinyPtrVector<Stmt *> unterminatedBranches;
29682968
JumpOutOfContextFinder jumpFinder(invalidJumps);
@@ -2979,7 +2979,7 @@ areBranchesValidForSingleValueStmt(Evaluator &eval, ArrayRef<Stmt *> branches) {
29792979
BS->walk(jumpFinder);
29802980

29812981
// Check to see if a result is produced from the branch.
2982-
if (doesBraceProduceResult(BS, eval)) {
2982+
if (doesBraceProduceResult(BS, ctx)) {
29832983
hadResult = true;
29842984
continue;
29852985
}
@@ -3005,7 +3005,11 @@ areBranchesValidForSingleValueStmt(Evaluator &eval, ArrayRef<Stmt *> branches) {
30053005
}
30063006

30073007
IsSingleValueStmtResult
3008-
IsSingleValueStmtRequest::evaluate(Evaluator &eval, const Stmt *S) const {
3008+
IsSingleValueStmtRequest::evaluate(Evaluator &eval, const Stmt *S,
3009+
ASTContext *_ctx) const {
3010+
assert(_ctx);
3011+
auto &ctx = *_ctx;
3012+
30093013
if (!isa<IfStmt>(S) && !isa<SwitchStmt>(S))
30103014
return IsSingleValueStmtResult::unhandledStmt();
30113015

@@ -3020,11 +3024,11 @@ IsSingleValueStmtRequest::evaluate(Evaluator &eval, const Stmt *S) const {
30203024
return IsSingleValueStmtResult::nonExhaustiveIf();
30213025

30223026
SmallVector<Stmt *, 4> scratch;
3023-
return areBranchesValidForSingleValueStmt(eval, IS->getBranches(scratch));
3027+
return areBranchesValidForSingleValueStmt(ctx, IS->getBranches(scratch));
30243028
}
30253029
if (auto *SS = dyn_cast<SwitchStmt>(S)) {
30263030
SmallVector<Stmt *, 4> scratch;
3027-
return areBranchesValidForSingleValueStmt(eval, SS->getBranches(scratch));
3031+
return areBranchesValidForSingleValueStmt(ctx, SS->getBranches(scratch));
30283032
}
30293033
llvm_unreachable("Unhandled case");
30303034
}

0 commit comments

Comments
 (0)