Index: include/experimental/coroutine =================================================================== --- include/experimental/coroutine +++ include/experimental/coroutine @@ -213,7 +213,7 @@ _LIBCPP_INLINE_VISIBILITY _Promise& promise() const { - return *reinterpret_cast<_Promise*>( + return *static_cast<_Promise*>( __builtin_coro_promise(this->__handle_, __alignof(_Promise), false)); } @@ -259,6 +259,45 @@ } }; +#if __has_builtin(__builtin_coro_noop) +struct noop_coroutine_promise {}; + +template <> +class _LIBCPP_TEMPLATE_VIS coroutine_handle + : public coroutine_handle<> { + using _Base = coroutine_handle<>; + using _Promise = noop_coroutine_promise; +public: + + _LIBCPP_INLINE_VISIBILITY + _Promise& promise() const { + return *static_cast<_Promise*>( + __builtin_coro_promise(this->__handle_, __alignof(_Promise), false)); + } + + _LIBCPP_CONSTEXPR explicit operator bool() const _NOEXCEPT { return true; } + _LIBCPP_CONSTEXPR bool done() const _NOEXCEPT { return false; } + + _LIBCPP_CONSTEXPR void operator()() const _NOEXCEPT {} + _LIBCPP_CONSTEXPR void resume() const _NOEXCEPT {} + _LIBCPP_CONSTEXPR void destroy() const _NOEXCEPT {} + +private: + friend coroutine_handle noop_coroutine() _NOEXCEPT; + + coroutine_handle() _NOEXCEPT { + this->__handle_ = __builtin_coro_noop(); + } +}; + +using noop_coroutine_handle = coroutine_handle; + +inline _LIBCPP_INLINE_VISIBILITY +noop_coroutine_handle noop_coroutine() _NOEXCEPT { + return {}; +} +#endif // __has_builtin(__builtin_coro_noop) + struct _LIBCPP_TYPE_VIS suspend_never { _LIBCPP_ALWAYS_INLINE bool await_ready() const _NOEXCEPT { return true; } Index: test/std/experimental/language.support/support.coroutines/coroutine.handle/coroutine.handle.noop/noop_coroutine.pass.cpp =================================================================== --- /dev/null +++ test/std/experimental/language.support/support.coroutines/coroutine.handle/coroutine.handle.noop/noop_coroutine.pass.cpp @@ -0,0 +1,66 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11 +// XFAIL: clang-5, clang-6 +// +// struct noop_coroutine_promise; +// using noop_coroutine_handle = coroutine_handle; +// noop_coroutine_handle noop_coroutine() noexcept; + +#include +#include +#include + +namespace coro = std::experimental::coroutines_v1; + +static_assert(std::is_same, coro::noop_coroutine_handle>::value, ""); +static_assert(std::is_same::value, ""); + +// template <> struct coroutine_handle : coroutine_handle<> +// { +// // 18.11.2.7 noop observers +// constexpr explicit operator bool() const noexcept; +// constexpr bool done() const noexcept; + +// // 18.11.2.8 noop resumption +// constexpr void operator()() const noexcept; +// constexpr void resume() const noexcept; +// constexpr void destroy() const noexcept; + +// // 18.11.2.9 noop promise access +// noop_coroutine_promise& promise() const noexcept; + +// // 18.11.2.10 noop address +// constexpr void* address() const noexcept; + +int main() +{ + auto h = coro::noop_coroutine(); + coro::coroutine_handle<> base = h; + + assert(h); + assert(base); + + assert(!h.done()); + assert(!base.done()); + + h.resume(); + h.destroy(); + h(); + static_assert(h.done() == false, ""); + static_assert(h, ""); + + h.promise(); + assert(h.address() == base.address()); + assert(h.address() != nullptr); + assert(coro::coroutine_handle<>::from_address(h.address()) == base); +} +