Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __coroutine / coroutine_handle.h
blob7a4eff745eb7aa81de758f7dd93f56181f24b278
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___COROUTINE_COROUTINE_HANDLE_H
10 #define _LIBCPP___COROUTINE_COROUTINE_HANDLE_H
12 #include <__assert>
13 #include <__config>
14 #include <__functional/hash.h>
15 #include <__memory/addressof.h>
16 #include <__type_traits/remove_cv.h>
17 #include <compare>
18 #include <cstddef>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
22 #endif
24 #if _LIBCPP_STD_VER >= 20
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 // [coroutine.handle]
29 template <class _Promise = void>
30 struct _LIBCPP_TEMPLATE_VIS coroutine_handle;
32 template <>
33 struct _LIBCPP_TEMPLATE_VIS coroutine_handle<void> {
34 public:
35 // [coroutine.handle.con], construct/reset
36 constexpr coroutine_handle() noexcept = default;
38 _LIBCPP_HIDE_FROM_ABI
39 constexpr coroutine_handle(nullptr_t) noexcept {}
41 _LIBCPP_HIDE_FROM_ABI
42 coroutine_handle& operator=(nullptr_t) noexcept {
43 __handle_ = nullptr;
44 return *this;
47 // [coroutine.handle.export.import], export/import
48 _LIBCPP_HIDE_FROM_ABI
49 constexpr void* address() const noexcept { return __handle_; }
51 _LIBCPP_HIDE_FROM_ABI
52 static constexpr coroutine_handle from_address(void* __addr) noexcept {
53 coroutine_handle __tmp;
54 __tmp.__handle_ = __addr;
55 return __tmp;
58 // [coroutine.handle.observers], observers
59 _LIBCPP_HIDE_FROM_ABI
60 constexpr explicit operator bool() const noexcept {
61 return __handle_ != nullptr;
64 _LIBCPP_HIDE_FROM_ABI
65 bool done() const {
66 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "done() can be called only on suspended coroutines");
67 return __builtin_coro_done(__handle_);
70 // [coroutine.handle.resumption], resumption
71 _LIBCPP_HIDE_FROM_ABI
72 void operator()() const { resume(); }
74 _LIBCPP_HIDE_FROM_ABI
75 void resume() const {
76 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "resume() can be called only on suspended coroutines");
77 _LIBCPP_ASSERT_UNCATEGORIZED(!done(), "resume() has undefined behavior when the coroutine is done");
78 __builtin_coro_resume(__handle_);
81 _LIBCPP_HIDE_FROM_ABI
82 void destroy() const {
83 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "destroy() can be called only on suspended coroutines");
84 __builtin_coro_destroy(__handle_);
87 private:
88 _LIBCPP_HIDE_FROM_ABI bool __is_suspended() const {
89 // FIXME actually implement a check for if the coro is suspended.
90 return __handle_ != nullptr;
93 void* __handle_ = nullptr;
96 // [coroutine.handle.compare]
97 inline _LIBCPP_HIDE_FROM_ABI
98 constexpr bool operator==(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
99 return __x.address() == __y.address();
101 inline _LIBCPP_HIDE_FROM_ABI
102 constexpr strong_ordering operator<=>(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
103 return compare_three_way()(__x.address(), __y.address());
106 template <class _Promise>
107 struct _LIBCPP_TEMPLATE_VIS coroutine_handle {
108 public:
109 // [coroutine.handle.con], construct/reset
110 constexpr coroutine_handle() noexcept = default;
112 _LIBCPP_HIDE_FROM_ABI
113 constexpr coroutine_handle(nullptr_t) noexcept {}
115 _LIBCPP_HIDE_FROM_ABI
116 static coroutine_handle from_promise(_Promise& __promise) {
117 using _RawPromise = __remove_cv_t<_Promise>;
118 coroutine_handle __tmp;
119 __tmp.__handle_ =
120 __builtin_coro_promise(_VSTD::addressof(const_cast<_RawPromise&>(__promise)), alignof(_Promise), true);
121 return __tmp;
124 _LIBCPP_HIDE_FROM_ABI
125 coroutine_handle& operator=(nullptr_t) noexcept {
126 __handle_ = nullptr;
127 return *this;
130 // [coroutine.handle.export.import], export/import
131 _LIBCPP_HIDE_FROM_ABI
132 constexpr void* address() const noexcept { return __handle_; }
134 _LIBCPP_HIDE_FROM_ABI
135 static constexpr coroutine_handle from_address(void* __addr) noexcept {
136 coroutine_handle __tmp;
137 __tmp.__handle_ = __addr;
138 return __tmp;
141 // [coroutine.handle.conv], conversion
142 _LIBCPP_HIDE_FROM_ABI
143 constexpr operator coroutine_handle<>() const noexcept {
144 return coroutine_handle<>::from_address(address());
147 // [coroutine.handle.observers], observers
148 _LIBCPP_HIDE_FROM_ABI
149 constexpr explicit operator bool() const noexcept {
150 return __handle_ != nullptr;
153 _LIBCPP_HIDE_FROM_ABI
154 bool done() const {
155 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "done() can be called only on suspended coroutines");
156 return __builtin_coro_done(__handle_);
159 // [coroutine.handle.resumption], resumption
160 _LIBCPP_HIDE_FROM_ABI
161 void operator()() const { resume(); }
163 _LIBCPP_HIDE_FROM_ABI
164 void resume() const {
165 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "resume() can be called only on suspended coroutines");
166 _LIBCPP_ASSERT_UNCATEGORIZED(!done(), "resume() has undefined behavior when the coroutine is done");
167 __builtin_coro_resume(__handle_);
170 _LIBCPP_HIDE_FROM_ABI
171 void destroy() const {
172 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "destroy() can be called only on suspended coroutines");
173 __builtin_coro_destroy(__handle_);
176 // [coroutine.handle.promise], promise access
177 _LIBCPP_HIDE_FROM_ABI
178 _Promise& promise() const {
179 return *static_cast<_Promise*>(__builtin_coro_promise(this->__handle_, alignof(_Promise), false));
182 private:
183 _LIBCPP_HIDE_FROM_ABI bool __is_suspended() const {
184 // FIXME actually implement a check for if the coro is suspended.
185 return __handle_ != nullptr;
187 void* __handle_ = nullptr;
190 // [coroutine.handle.hash]
191 template <class _Tp>
192 struct hash<coroutine_handle<_Tp>> {
193 _LIBCPP_HIDE_FROM_ABI
194 size_t operator()(const coroutine_handle<_Tp>& __v) const noexcept { return hash<void*>()(__v.address()); }
197 _LIBCPP_END_NAMESPACE_STD
199 #endif // __LIBCPP_STD_VER >= 20
201 #endif // _LIBCPP___COROUTINE_COROUTINE_HANDLE_H