1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___COROUTINE_NOOP_COROUTINE_HANDLE_H
10#define _LIBCPP___COROUTINE_NOOP_COROUTINE_HANDLE_H
11
12#include <__config>
13#include <__coroutine/coroutine_handle.h>
14
15#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16#  pragma GCC system_header
17#endif
18
19#if _LIBCPP_STD_VER >= 20
20
21_LIBCPP_BEGIN_NAMESPACE_STD
22
23#  if __has_builtin(__builtin_coro_noop) || defined(_LIBCPP_COMPILER_GCC)
24
25// [coroutine.noop]
26// [coroutine.promise.noop]
27struct noop_coroutine_promise {};
28
29// [coroutine.handle.noop]
30template <>
31struct _LIBCPP_TEMPLATE_VIS coroutine_handle<noop_coroutine_promise> {
32public:
33  // [coroutine.handle.noop.conv], conversion
34  _LIBCPP_HIDE_FROM_ABI constexpr operator coroutine_handle<>() const noexcept {
35    return coroutine_handle<>::from_address(address());
36  }
37
38  // [coroutine.handle.noop.observers], observers
39  _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return true; }
40  _LIBCPP_HIDE_FROM_ABI constexpr bool done() const noexcept { return false; }
41
42  // [coroutine.handle.noop.resumption], resumption
43  _LIBCPP_HIDE_FROM_ABI constexpr void operator()() const noexcept {}
44  _LIBCPP_HIDE_FROM_ABI constexpr void resume() const noexcept {}
45  _LIBCPP_HIDE_FROM_ABI constexpr void destroy() const noexcept {}
46
47  // [coroutine.handle.noop.promise], promise access
48  _LIBCPP_HIDE_FROM_ABI noop_coroutine_promise& promise() const noexcept {
49    return *static_cast<noop_coroutine_promise*>(
50        __builtin_coro_promise(this->__handle_, alignof(noop_coroutine_promise), false));
51  }
52
53  // [coroutine.handle.noop.address], address
54  _LIBCPP_HIDE_FROM_ABI constexpr void* address() const noexcept { return __handle_; }
55
56private:
57  _LIBCPP_HIDE_FROM_ABI friend coroutine_handle<noop_coroutine_promise> noop_coroutine() noexcept;
58
59#    if __has_builtin(__builtin_coro_noop)
60  _LIBCPP_HIDE_FROM_ABI coroutine_handle() noexcept { this->__handle_ = __builtin_coro_noop(); }
61
62  void* __handle_ = nullptr;
63
64#    elif defined(_LIBCPP_COMPILER_GCC)
65  // GCC doesn't implement __builtin_coro_noop().
66  // Construct the coroutine frame manually instead.
67  struct __noop_coroutine_frame_ty_ {
68    static void __dummy_resume_destroy_func() {}
69
70    void (*__resume_)()  = __dummy_resume_destroy_func;
71    void (*__destroy_)() = __dummy_resume_destroy_func;
72    struct noop_coroutine_promise __promise_;
73  };
74
75  static __noop_coroutine_frame_ty_ __noop_coroutine_frame_;
76
77  void* __handle_ = &__noop_coroutine_frame_;
78
79  _LIBCPP_HIDE_FROM_ABI coroutine_handle() noexcept = default;
80
81#    endif // __has_builtin(__builtin_coro_noop)
82};
83
84using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;
85
86#    if defined(_LIBCPP_COMPILER_GCC)
87inline noop_coroutine_handle::__noop_coroutine_frame_ty_ noop_coroutine_handle::__noop_coroutine_frame_{};
88#    endif
89
90// [coroutine.noop.coroutine]
91inline _LIBCPP_HIDE_FROM_ABI noop_coroutine_handle noop_coroutine() noexcept { return noop_coroutine_handle(); }
92
93#  endif // __has_builtin(__builtin_coro_noop) || defined(_LIBCPP_COMPILER_GCC)
94
95_LIBCPP_END_NAMESPACE_STD
96
97#endif // __LIBCPP_STD_VER >= 20
98
99#endif // _LIBCPP___COROUTINE_NOOP_COROUTINE_HANDLE_H
100