Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions CoroutinesCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,9 +932,21 @@ void CoroutinesCodeGenerator::InsertArg(const CoroutineSuspendExpr* stmt)
StmtsContainer bodyStmts{};
Expr* initializeInitialAwaitResume = nullptr;

auto addInitialAwaitSuspendCalled = [&] {
bodyStmts.Add(bop);
const bool canThrow{[&] {
if(const auto* e = dyn_cast_or_null<ExprWithCleanups>(stmt->getSuspendExpr())) {
if(const auto* ce = dyn_cast_or_null<CallExpr>(e->getSubExpr())) {
if(const FunctionDecl* fd = ce->getDirectCallee()) {
if(const FunctionProtoType* fpt = fd->getType()->getAs<FunctionProtoType>()) {
return not fpt->isNothrow(/*ResultIfDependent=*/false);
}
}
}
}

return true;
}()};

auto addInitialAwaitSuspendCalled = [&] {
if(eState::InitialSuspend == mState) {
mState = eState::Body;
// https://timsong-cpp.github.io/cppwp/n4861/dcl.fct.def.coroutine#5.3
Expand All @@ -944,8 +956,24 @@ void CoroutinesCodeGenerator::InsertArg(const CoroutineSuspendExpr* stmt)
}
};

auto insertTryCatchIfNecessary = [&](StmtsContainer& cont) {
if(canThrow) {
auto* tryBody = mkCompoundStmt(cont);

StmtsContainer catchBodyStmts{
Assign(mASTData.mSuspendIndexAccess, mASTData.mSuspendIndexField, Int32(mSuspendsCount - 1)), Throw()};

cont.clear();
cont.Add(Try(tryBody, Catch(catchBodyStmts)));
}
};

if(returnsVoid) {
bodyStmts.Add(bop);
bodyStmts.Add(stmt->getSuspendExpr());

insertTryCatchIfNecessary(bodyStmts);

addInitialAwaitSuspendCalled();
bodyStmts.Add(Return());

Expand All @@ -957,7 +985,10 @@ void CoroutinesCodeGenerator::InsertArg(const CoroutineSuspendExpr* stmt)

auto* ifSuspend = If(stmt->getSuspendExpr(), bodyStmts);

InsertArg(If(Not(stmt->getReadyExpr()), ifSuspend));
StmtsContainer innerBodyStmts{bop, ifSuspend};
insertTryCatchIfNecessary(innerBodyStmts);

InsertArg(If(Not(stmt->getReadyExpr()), innerBodyStmts));
}

if(not returnsVoid and initializeInitialAwaitResume) {
Expand Down
4 changes: 2 additions & 2 deletions tests/EduCoroutineAllocFailureTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ void __fun_intResume(__fun_intFrame * __f)
/* co_await EduCoroutineAllocFailureTest.cpp:40 */
__f->__suspend_40_14 = __f->__promise.initial_suspend();
if(!__f->__suspend_40_14.await_ready()) {
__f->__suspend_40_14.await_suspend(std::coroutine_handle<generator<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 1;
__f->__suspend_40_14.await_suspend(std::coroutine_handle<generator<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__initial_await_suspend_called = true;
return;
}
Expand All @@ -234,8 +234,8 @@ void __fun_intResume(__fun_intFrame * __f)
/* co_await EduCoroutineAllocFailureTest.cpp:40 */
__f->__suspend_40_14_1 = __f->__promise.final_suspend();
if(!__f->__suspend_40_14_1.await_ready()) {
__f->__suspend_40_14_1.await_suspend(std::coroutine_handle<generator<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 2;
__f->__suspend_40_14_1.await_suspend(std::coroutine_handle<generator<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
return;
}

Expand Down
12 changes: 6 additions & 6 deletions tests/EduCoroutineBinaryExprTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ void __seqResume(__seqFrame * __f)
/* co_await EduCoroutineBinaryExprTest.cpp:35 */
__f->__suspend_35_11 = __f->__promise.initial_suspend();
if(!__f->__suspend_35_11.await_ready()) {
__f->__suspend_35_11.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 1;
__f->__suspend_35_11.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__initial_await_suspend_called = true;
return;
}
Expand All @@ -160,8 +160,8 @@ void __seqResume(__seqFrame * __f)
/* co_yield EduCoroutineBinaryExprTest.cpp:42 */
__f->__suspend_42_5 = __f->__promise.yield_value(__f->s.t);
if(!__f->__suspend_42_5.await_ready()) {
__f->__suspend_42_5.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 2;
__f->__suspend_42_5.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
return;
}

Expand All @@ -171,8 +171,8 @@ void __seqResume(__seqFrame * __f)
/* co_yield EduCoroutineBinaryExprTest.cpp:43 */
__f->__suspend_43_12 = __f->__promise.yield_value(__f->i);
if(!__f->__suspend_43_12.await_ready()) {
__f->__suspend_43_12.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 3;
__f->__suspend_43_12.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
return;
}

Expand All @@ -182,8 +182,8 @@ void __seqResume(__seqFrame * __f)
/* co_yield EduCoroutineBinaryExprTest.cpp:44 */
__f->__suspend_44_23 = __f->__promise.yield_value(__f->i);
if(!__f->__suspend_44_23.await_ready()) {
__f->__suspend_44_23.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 4;
__f->__suspend_44_23.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
return;
}

Expand All @@ -193,8 +193,8 @@ void __seqResume(__seqFrame * __f)
/* co_yield EduCoroutineBinaryExprTest.cpp:45 */
__f->__suspend_45_5 = __f->__promise.yield_value(__f->i + 1);
if(!__f->__suspend_45_5.await_ready()) {
__f->__suspend_45_5.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 5;
__f->__suspend_45_5.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
return;
}

Expand All @@ -216,8 +216,8 @@ void __seqResume(__seqFrame * __f)
/* co_await EduCoroutineBinaryExprTest.cpp:35 */
__f->__suspend_35_11_1 = __f->__promise.final_suspend();
if(!__f->__suspend_35_11_1.await_ready()) {
__f->__suspend_35_11_1.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 6;
__f->__suspend_35_11_1.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
return;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/EduCoroutineCaptureConstTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ void __seqResume(__seqFrame * __f)
/* co_await EduCoroutineCaptureConstTest.cpp:35 */
__f->__suspend_35_11 = __f->__promise.initial_suspend();
if(!__f->__suspend_35_11.await_ready()) {
__f->__suspend_35_11.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 1;
__f->__suspend_35_11.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__initial_await_suspend_called = true;
return;
}
Expand All @@ -162,8 +162,8 @@ void __seqResume(__seqFrame * __f)
/* co_await EduCoroutineCaptureConstTest.cpp:35 */
__f->__suspend_35_11_1 = __f->__promise.final_suspend();
if(!__f->__suspend_35_11_1.await_ready()) {
__f->__suspend_35_11_1.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 2;
__f->__suspend_35_11_1.await_suspend(std::coroutine_handle<generator::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
return;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/EduCoroutineCaptureThisTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ inline my_resumable coro(int x)
/* co_await EduCoroutineCaptureThisTest.cpp:31 */
__f->__suspend_31_16 = __f->__promise.initial_suspend();
if(!__f->__suspend_31_16.await_ready()) {
__f->__suspend_31_16.await_suspend(std::coroutine_handle<my_resumable::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 1;
__f->__suspend_31_16.await_suspend(std::coroutine_handle<my_resumable::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__initial_await_suspend_called = true;
return;
}
Expand All @@ -150,8 +150,8 @@ inline my_resumable coro(int x)
/* co_await EduCoroutineCaptureThisTest.cpp:31 */
__f->__suspend_31_16_1 = __f->__promise.final_suspend();
if(!__f->__suspend_31_16_1.await_ready()) {
__f->__suspend_31_16_1.await_suspend(std::coroutine_handle<my_resumable::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 2;
__f->__suspend_31_16_1.await_suspend(std::coroutine_handle<my_resumable::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
return;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/EduCoroutineCoAwaitOperatorTest.cerr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.tmp.cpp:167:3: error: unknown type name 'awaiter'
167 | awaiter __suspend_54_5;
| ^
.tmp.cpp:250:10: warning: expression result unused [-Wunused-value]
250 | __f->__suspend_56_14_res;
.tmp.cpp:262:10: warning: expression result unused [-Wunused-value]
262 | __f->__suspend_56_14_res;
| ~~~ ^~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
24 changes: 18 additions & 6 deletions tests/EduCoroutineCoAwaitOperatorTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ void __gResume(__gFrame * __f)
/* co_await EduCoroutineCoAwaitOperatorTest.cpp:51 */
__f->__suspend_51_16 = __f->__promise.initial_suspend();
if(!__f->__suspend_51_16.await_ready()) {
__f->__suspend_51_16.await_suspend(std::coroutine_handle<my_future<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 1;
__f->__suspend_51_16.await_suspend(std::coroutine_handle<my_future<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__initial_await_suspend_called = true;
return;
}
Expand All @@ -228,8 +228,14 @@ void __gResume(__gFrame * __f)
/* co_await EduCoroutineCoAwaitOperatorTest.cpp:54 */
__f->__suspend_54_5 = operator co_await(std::operator""ms(10ULL));
if(!__f->__suspend_54_5.await_ready()) {
__f->__suspend_54_5.await_suspend(std::coroutine_handle<my_future<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 2;
try
{
__f->__suspend_index = 2;
__f->__suspend_54_5.await_suspend(std::coroutine_handle<my_future<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
} catch(...) {
__f->__suspend_index = 1;
throw ;
}
return;
}

Expand All @@ -240,8 +246,14 @@ void __gResume(__gFrame * __f)
/* co_await EduCoroutineCoAwaitOperatorTest.cpp:56 */
__f->__suspend_56_14 = h();
if(!__f->__suspend_56_14.await_ready()) {
__f->__suspend_56_14.await_suspend(std::coroutine_handle<my_future<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 3;
try
{
__f->__suspend_index = 3;
__f->__suspend_56_14.await_suspend(std::coroutine_handle<my_future<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
} catch(...) {
__f->__suspend_index = 2;
throw ;
}
return;
}

Expand All @@ -264,8 +276,8 @@ void __gResume(__gFrame * __f)
/* co_await EduCoroutineCoAwaitOperatorTest.cpp:51 */
__f->__suspend_51_16_1 = __f->__promise.final_suspend();
if(!__f->__suspend_51_16_1.await_ready()) {
__f->__suspend_51_16_1.await_suspend(std::coroutine_handle<my_future<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
__f->__suspend_index = 4;
__f->__suspend_51_16_1.await_suspend(std::coroutine_handle<my_future<int>::promise_type>::from_address(static_cast<void *>(__f)).operator std::coroutine_handle<void>());
return;
}

Expand Down
24 changes: 12 additions & 12 deletions tests/EduCoroutineCoreturnWithCoawaitTest.cerr
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@
.tmp.cpp:69:10: note: copy assignment operator is implicitly deleted because 'generator' has a user-declared move constructor
69 | inline generator(generator && rhs)
| ^
.tmp.cpp:289:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
289 | __f->__suspend_56_51 = simpleReturn(__f->v + 1);
.tmp.cpp:295:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
295 | __f->__suspend_56_51 = simpleReturn(__f->v + 1);
| ^
.tmp.cpp:69:10: note: copy assignment operator is implicitly deleted because 'generator' has a user-declared move constructor
69 | inline generator(generator && rhs)
| ^
.tmp.cpp:414:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
414 | __f->__suspend_60_24 = simpleReturn(__f->v);
.tmp.cpp:426:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
426 | __f->__suspend_60_24 = simpleReturn(__f->v);
| ^
.tmp.cpp:69:10: note: copy assignment operator is implicitly deleted because 'generator' has a user-declared move constructor
69 | inline generator(generator && rhs)
| ^
.tmp.cpp:425:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
425 | __f->__suspend_60_51 = simpleReturn(__f->v + 1);
.tmp.cpp:443:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
443 | __f->__suspend_60_51 = simpleReturn(__f->v + 1);
| ^
.tmp.cpp:69:10: note: copy assignment operator is implicitly deleted because 'generator' has a user-declared move constructor
69 | inline generator(generator && rhs)
| ^
.tmp.cpp:436:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
436 | __f->__suspend_60_80 = simpleReturn(__f->v + 2);
.tmp.cpp:460:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
460 | __f->__suspend_60_80 = simpleReturn(__f->v + 2);
| ^
.tmp.cpp:69:10: note: copy assignment operator is implicitly deleted because 'generator' has a user-declared move constructor
69 | inline generator(generator && rhs)
| ^
.tmp.cpp:558:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
558 | __f->__suspend_67_24 = simpleReturn(__f->v);
.tmp.cpp:588:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
588 | __f->__suspend_67_24 = simpleReturn(__f->v);
| ^
.tmp.cpp:69:10: note: copy assignment operator is implicitly deleted because 'generator' has a user-declared move constructor
69 | inline generator(generator && rhs)
| ^
.tmp.cpp:569:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
569 | __f->__suspend_67_51 = simpleReturn(__f->v + 1);
.tmp.cpp:605:26: error: object of type 'generator' cannot be assigned because its copy assignment operator is implicitly deleted
605 | __f->__suspend_67_51 = simpleReturn(__f->v + 1);
| ^
.tmp.cpp:69:10: note: copy assignment operator is implicitly deleted because 'generator' has a user-declared move constructor
69 | inline generator(generator && rhs)
Expand Down
Loading
Loading