Skip to content

Commit ce43bd2

Browse files
committedMar 11, 2017
[coroutines] Refactor SuspendExpr to create just one OpaqueValue (almost NFC)
Summary: Create only one OpaqueValue for await_ready/await_suspend/await_resume. Store OpaqueValue used in the CoroutineSuspendExpr node, so that CodeGen does not have to hunt looking for it. Reviewers: rsmith, EricWF, aaron.ballman Reviewed By: EricWF Subscribers: mehdi_amini, cfe-commits Differential Revision: https://reviews.llvm.org/D30775 llvm-svn: 297541
1 parent ca9ca54 commit ce43bd2

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed
 

Diff for: ‎clang/include/clang/AST/ExprCXX.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -4123,16 +4123,18 @@ class CoroutineSuspendExpr : public Expr {
41234123

41244124
enum SubExpr { Common, Ready, Suspend, Resume, Count };
41254125
Stmt *SubExprs[SubExpr::Count];
4126+
OpaqueValueExpr *OpaqueValue = nullptr;
41264127

41274128
friend class ASTStmtReader;
41284129
public:
41294130
CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, Expr *Common,
4130-
Expr *Ready, Expr *Suspend, Expr *Resume)
4131+
Expr *Ready, Expr *Suspend, Expr *Resume,
4132+
OpaqueValueExpr *OpaqueValue)
41314133
: Expr(SC, Resume->getType(), Resume->getValueKind(),
41324134
Resume->getObjectKind(), Resume->isTypeDependent(),
41334135
Resume->isValueDependent(), Common->isInstantiationDependent(),
41344136
Common->containsUnexpandedParameterPack()),
4135-
KeywordLoc(KeywordLoc) {
4137+
KeywordLoc(KeywordLoc), OpaqueValue(OpaqueValue) {
41364138
SubExprs[SubExpr::Common] = Common;
41374139
SubExprs[SubExpr::Ready] = Ready;
41384140
SubExprs[SubExpr::Suspend] = Suspend;
@@ -4161,6 +4163,8 @@ class CoroutineSuspendExpr : public Expr {
41614163
Expr *getCommonExpr() const {
41624164
return static_cast<Expr*>(SubExprs[SubExpr::Common]);
41634165
}
4166+
/// \brief getOpaqueValue - Return the opaque value placeholder.
4167+
OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; }
41644168

41654169
Expr *getReadyExpr() const {
41664170
return static_cast<Expr*>(SubExprs[SubExpr::Ready]);
@@ -4194,10 +4198,11 @@ class CoawaitExpr : public CoroutineSuspendExpr {
41944198
friend class ASTStmtReader;
41954199
public:
41964200
CoawaitExpr(SourceLocation CoawaitLoc, Expr *Operand, Expr *Ready,
4197-
Expr *Suspend, Expr *Resume, bool IsImplicit = false)
4201+
Expr *Suspend, Expr *Resume, OpaqueValueExpr *OpaqueValue,
4202+
bool IsImplicit = false)
41984203
: CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Operand, Ready,
4199-
Suspend, Resume) {
4200-
CoawaitBits.IsImplicit = IsImplicit;
4204+
Suspend, Resume, OpaqueValue) {
4205+
CoawaitBits.IsImplicit = IsImplicit;
42014206
}
42024207
CoawaitExpr(SourceLocation CoawaitLoc, QualType Ty, Expr *Operand,
42034208
bool IsImplicit = false)
@@ -4270,9 +4275,9 @@ class CoyieldExpr : public CoroutineSuspendExpr {
42704275
friend class ASTStmtReader;
42714276
public:
42724277
CoyieldExpr(SourceLocation CoyieldLoc, Expr *Operand, Expr *Ready,
4273-
Expr *Suspend, Expr *Resume)
4278+
Expr *Suspend, Expr *Resume, OpaqueValueExpr *OpaqueValue)
42744279
: CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Operand, Ready,
4275-
Suspend, Resume) {}
4280+
Suspend, Resume, OpaqueValue) {}
42764281
CoyieldExpr(SourceLocation CoyieldLoc, QualType Ty, Expr *Operand)
42774282
: CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Ty, Operand) {}
42784283
CoyieldExpr(EmptyShell Empty)

Diff for: ‎clang/lib/Sema/SemaCoroutine.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ static ExprResult buildCoroutineHandle(Sema &S, QualType PromiseType,
312312
}
313313

314314
struct ReadySuspendResumeResult {
315-
bool IsInvalid;
316315
Expr *Results[3];
316+
OpaqueValueExpr *OpaqueValue;
317+
bool IsInvalid;
317318
};
318319

319320
static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc,
@@ -336,8 +337,11 @@ static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc,
336337
/// expression.
337338
static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise,
338339
SourceLocation Loc, Expr *E) {
340+
OpaqueValueExpr *Operand = new (S.Context)
341+
OpaqueValueExpr(Loc, E->getType(), VK_LValue, E->getObjectKind(), E);
342+
339343
// Assume invalid until we see otherwise.
340-
ReadySuspendResumeResult Calls = {true, {}};
344+
ReadySuspendResumeResult Calls = {{}, Operand, /*IsInvalid=*/true};
341345

342346
ExprResult CoroHandleRes = buildCoroutineHandle(S, CoroPromise->getType(), Loc);
343347
if (CoroHandleRes.isInvalid())
@@ -347,9 +351,6 @@ static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise,
347351
const StringRef Funcs[] = {"await_ready", "await_suspend", "await_resume"};
348352
MultiExprArg Args[] = {None, CoroHandle, None};
349353
for (size_t I = 0, N = llvm::array_lengthof(Funcs); I != N; ++I) {
350-
Expr *Operand = new (S.Context) OpaqueValueExpr(
351-
Loc, E->getType(), VK_LValue, E->getObjectKind(), E);
352-
353354
ExprResult Result = buildMemberCall(S, Operand, Loc, Funcs[I], Args[I]);
354355
if (Result.isInvalid())
355356
return Calls;
@@ -556,8 +557,9 @@ ExprResult Sema::BuildResolvedCoawaitExpr(SourceLocation Loc, Expr *E,
556557
if (RSS.IsInvalid)
557558
return ExprError();
558559

559-
Expr *Res = new (Context) CoawaitExpr(Loc, E, RSS.Results[0], RSS.Results[1],
560-
RSS.Results[2], IsImplicit);
560+
Expr *Res =
561+
new (Context) CoawaitExpr(Loc, E, RSS.Results[0], RSS.Results[1],
562+
RSS.Results[2], RSS.OpaqueValue, IsImplicit);
561563
if (!IsImplicit)
562564
Coroutine->CoroutineStmts.push_back(Res);
563565
return Res;
@@ -611,7 +613,7 @@ ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) {
611613
return ExprError();
612614

613615
Expr *Res = new (Context) CoyieldExpr(Loc, E, RSS.Results[0], RSS.Results[1],
614-
RSS.Results[2]);
616+
RSS.Results[2], RSS.OpaqueValue);
615617
Coroutine->CoroutineStmts.push_back(Res);
616618
return Res;
617619
}

0 commit comments

Comments
 (0)
Please sign in to comment.