1// <generator> -*- C++ -*-
3// Copyright (C) 2023-2025 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/generator
26 * This is a Standard C++ Library header.
29#ifndef _GLIBCXX_GENERATOR
30#define _GLIBCXX_GENERATOR
34#pragma GCC system_header
37#include <bits/c++config.h>
39#define __glibcxx_want_generator
40#include <bits/version.h>
42#ifdef __cpp_lib_generator // C++ >= 23 && __glibcxx_coroutine
45#include <bits/ranges_util.h>
46#include <bits/elements_of.h>
47#include <bits/uses_allocator.h>
48#include <bits/exception_ptr.h>
59# include <bits/memory_resource.h>
62namespace std _GLIBCXX_VISIBILITY(default)
64_GLIBCXX_BEGIN_NAMESPACE_VERSION
67 * @defgroup generator_coros Range generator coroutines
73 /** @brief A range specified using a yielding coroutine.
75 * `std::generator` is a utility class for defining ranges using coroutines
76 * that yield elements as a range. Generator coroutines are synchronous.
78 * @headerfile generator
81 template<typename _Ref, typename _Val = void, typename _Alloc = void>
84 /// @cond undocumented
87 /// _Reference type for a generator whose reference (first argument) and
88 /// value (second argument) types are _Ref and _Val.
89 template<typename _Ref, typename _Val>
90 using _Reference_t = __conditional_t<is_void_v<_Val>,
93 /// Type yielded by a generator whose _Reference type is _Reference.
94 template<typename _Reference>
95 using _Yield_t = __conditional_t<is_reference_v<_Reference>,
99 /// _Yield_t * _Reference_t
100 template<typename _Ref, typename _Val>
101 using _Yield2_t = _Yield_t<_Reference_t<_Ref, _Val>>;
103 template<typename> constexpr bool __is_generator = false;
104 template<typename _Val, typename _Ref, typename _Alloc>
105 constexpr bool __is_generator<std::generator<_Val, _Ref, _Alloc>> = true;
107 /// Allocator and value type erased generator promise type.
108 /// \tparam _Yielded The corresponding generators yielded type.
109 template<typename _Yielded>
110 class _Promise_erased
112 static_assert(is_reference_v<_Yielded>);
113 using _Yielded_deref = remove_reference_t<_Yielded>;
114 using _Yielded_decvref = remove_cvref_t<_Yielded>;
115 using _ValuePtr = add_pointer_t<_Yielded>;
116 using _Coro_handle = std::coroutine_handle<_Promise_erased>;
118 template<typename, typename, typename>
119 friend class std::generator;
121 template<typename _Gen>
122 struct _Recursive_awaiter;
124 friend struct _Recursive_awaiter;
125 struct _Copy_awaiter;
126 struct _Subyield_state;
127 struct _Final_awaiter;
130 initial_suspend() const noexcept
134 yield_value(_Yielded __val) noexcept
136 _M_bottom_value() = ::std::addressof(__val);
141 yield_value(const _Yielded_deref& __val)
142 noexcept (is_nothrow_constructible_v<_Yielded_decvref,
143 const _Yielded_deref&>)
144 requires (is_rvalue_reference_v<_Yielded>
145 && constructible_from<_Yielded_decvref,
146 const _Yielded_deref&>)
147 { return _Copy_awaiter(_Yielded_decvref(__val), _M_bottom_value()); }
149 template<typename _R2, typename _V2, typename _A2, typename _U2>
150 requires std::same_as<_Yield2_t<_R2, _V2>, _Yielded>
152 yield_value(ranges::elements_of<generator<_R2, _V2, _A2>&&, _U2> __r)
154 { return _Recursive_awaiter { std::move(__r.range) }; }
156 template<ranges::input_range _R, typename _Alloc>
157 requires convertible_to<ranges::range_reference_t<_R>, _Yielded>
159 yield_value(ranges::elements_of<_R, _Alloc> __r)
161 auto __n = [] (allocator_arg_t, _Alloc,
162 ranges::iterator_t<_R> __i,
163 ranges::sentinel_t<_R> __s)
164 -> generator<_Yielded, ranges::range_value_t<_R>, _Alloc> {
165 for (; __i != __s; ++__i)
166 co_yield static_cast<_Yielded>(*__i);
168 return yield_value(ranges::elements_of(__n(allocator_arg,
170 ranges::begin(__r.range),
171 ranges::end(__r.range))));
176 final_suspend() noexcept
180 unhandled_exception()
182 // To get to this point, this coroutine must have been active. In that
183 // case, it must be the top of the stack. The current coroutine is
184 // the sole entry of the stack iff it is both the top and the bottom. As
185 // it is the top implicitly in this context it will be the sole entry iff
187 if (_M_nest._M_is_bottom())
190 this->_M_except = std::current_exception();
193 void await_transform() = delete;
194 void return_void() const noexcept {}
198 _M_bottom_value() noexcept
199 { return _M_nest._M_bottom_value(*this); }
203 { return _M_nest._M_value(*this); }
205 _Subyield_state _M_nest;
206 std::exception_ptr _M_except;
209 template<typename _Yielded>
210 struct _Promise_erased<_Yielded>::_Subyield_state
214 _Coro_handle _M_bottom;
215 _Coro_handle _M_parent;
221 _ValuePtr _M_value = nullptr;
230 _M_is_bottom() const noexcept
231 { return !std::holds_alternative<_Frame>(this->_M_stack); }
236 if (auto __f = std::get_if<_Frame>(&this->_M_stack))
237 return __f->_M_bottom.promise()._M_nest._M_top();
239 auto __bf = std::get_if<_Bottom_frame>(&this->_M_stack);
240 __glibcxx_assert(__bf);
245 _M_push(_Coro_handle __current, _Coro_handle __subyield) noexcept
247 __glibcxx_assert(&__current.promise()._M_nest == this);
248 __glibcxx_assert(this->_M_top() == __current);
250 __subyield.promise()._M_nest._M_jump_in(__current, __subyield);
253 std::coroutine_handle<>
256 if (auto __f = std::get_if<_Frame>(&this->_M_stack))
258 // We aren't a bottom coroutine. Restore the parent to the top
260 auto __p = this->_M_top() = __f->_M_parent;
264 // Otherwise, there's nothing to resume.
265 return std::noop_coroutine();
269 _M_jump_in(_Coro_handle __rest, _Coro_handle __new) noexcept
271 __glibcxx_assert(&__new.promise()._M_nest == this);
272 __glibcxx_assert(this->_M_is_bottom());
273 // We're bottom. We're also top if top is unset (note that this is
274 // not true if something was added to the coro stack and then popped,
275 // but in that case we can't possibly be yielded from, as it would
276 // require rerunning begin()).
277 __glibcxx_assert(!this->_M_top());
279 auto& __rn = __rest.promise()._M_nest;
280 __rn._M_top() = __new;
282 // Presume we're the second frame...
283 auto __bott = __rest;
284 if (auto __f = std::get_if<_Frame>(&__rn._M_stack))
285 // But, if we aren't, get the actual bottom. We're only the second
286 // frame if our parent is the bottom frame, i.e. it doesn't have a
288 __bott = __f->_M_bottom;
290 this->_M_stack = _Frame {
297 _M_bottom_value(_Promise_erased& __current) noexcept
299 __glibcxx_assert(&__current._M_nest == this);
300 if (auto __bf = std::get_if<_Bottom_frame>(&this->_M_stack))
301 return __bf->_M_value;
302 auto __f = std::get_if<_Frame>(&this->_M_stack);
303 __glibcxx_assert(__f);
304 auto& __p = __f->_M_bottom.promise();
305 return __p._M_nest._M_value(__p);
309 _M_value(_Promise_erased& __current) noexcept
311 __glibcxx_assert(&__current._M_nest == this);
312 auto __bf = std::get_if<_Bottom_frame>(&this->_M_stack);
313 __glibcxx_assert(__bf);
314 return __bf->_M_value;
318 template<typename _Yielded>
319 struct _Promise_erased<_Yielded>::_Final_awaiter
321 bool await_ready() noexcept
324 template<typename _Promise>
325 auto await_suspend(std::coroutine_handle<_Promise> __c) noexcept
327#ifdef __glibcxx_is_pointer_interconvertible
328 static_assert(is_pointer_interconvertible_base_of_v<
329 _Promise_erased, _Promise>);
332 auto& __n = __c.promise()._M_nest;
336 void await_resume() noexcept {}
339 template<typename _Yielded>
340 struct _Promise_erased<_Yielded>::_Copy_awaiter
342 _Yielded_decvref _M_value;
343 _ValuePtr& _M_bottom_value;
345 constexpr bool await_ready() noexcept
348 template<typename _Promise>
349 void await_suspend(std::coroutine_handle<_Promise>) noexcept
351#ifdef __glibcxx_is_pointer_interconvertible
352 static_assert(is_pointer_interconvertible_base_of_v<
353 _Promise_erased, _Promise>);
355 _M_bottom_value = ::std::addressof(_M_value);
359 await_resume() const noexcept
363 template<typename _Yielded>
364 template<typename _Gen>
365 struct _Promise_erased<_Yielded>::_Recursive_awaiter
368 static_assert(__is_generator<_Gen>);
369 static_assert(std::same_as<typename _Gen::yielded, _Yielded>);
371 _Recursive_awaiter(_Gen __gen) noexcept
372 : _M_gen(std::move(__gen))
373 { this->_M_gen._M_mark_as_started(); }
376 await_ready() const noexcept
380 template<typename _Promise>
381 std::coroutine_handle<>
382 await_suspend(std::coroutine_handle<_Promise> __p) noexcept
384#ifdef __glibcxx_is_pointer_interconvertible
385 static_assert(is_pointer_interconvertible_base_of_v<
386 _Promise_erased, _Promise>);
389 auto __c = _Coro_handle::from_address(__p.address());
390 auto __t = _Coro_handle::from_address(this->_M_gen._M_coro.address());
391 __p.promise()._M_nest._M_push(__c, __t);
397 if (auto __e = _M_gen._M_coro.promise()._M_except)
398 std::rethrow_exception(__e);
404 alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
405 char _M_data[__STDCPP_DEFAULT_NEW_ALIGNMENT__];
408 _M_cnt(std::size_t __sz) noexcept
410 auto __blksz = sizeof(_Alloc_block);
411 return (__sz + __blksz - 1) / __blksz;
415 template<typename _All>
416 concept _Stateless_alloc = (allocator_traits<_All>::is_always_equal::value
417 && default_initializable<_All>);
419 template<typename _Allocator>
422 using _Rebound = __alloc_rebind<_Allocator, _Alloc_block>;
423 using _Rebound_ATr = allocator_traits<_Rebound>;
424 static_assert(is_pointer_v<typename _Rebound_ATr::pointer>,
425 "Must use allocators for true pointers with generators");
428 _M_alloc_address(std::uintptr_t __fn, std::uintptr_t __fsz) noexcept
430 auto __an = __fn + __fsz;
431 auto __ba = alignof(_Rebound);
432 return reinterpret_cast<_Rebound*>(((__an + __ba - 1) / __ba) * __ba);
436 _M_alloc_size(std::size_t __csz) noexcept
438 auto __ba = alignof(_Rebound);
439 // Our desired layout is placing the coroutine frame, then pad out to
440 // align, then place the allocator. The total size of that is the
441 // size of the coroutine frame, plus up to __ba bytes, plus the size
443 return __csz + __ba + sizeof(_Rebound);
447 _M_allocate(_Rebound __b, std::size_t __csz)
449 if constexpr (_Stateless_alloc<_Rebound>)
450 // Only need room for the coroutine.
451 return __b.allocate(_Alloc_block::_M_cnt(__csz));
454 auto __nsz = _Alloc_block::_M_cnt(_M_alloc_size(__csz));
455 auto __f = __b.allocate(__nsz);
456 auto __fn = reinterpret_cast<std::uintptr_t>(__f);
457 auto __an = _M_alloc_address(__fn, __csz);
458 ::new (__an) _Rebound(std::move(__b));
465 operator new(std::size_t __sz)
466 requires default_initializable<_Rebound> // _Allocator is non-void
467 { return _M_allocate({}, __sz); }
469 // _GLIBCXX_RESOLVE_LIB_DEFECTS
470 // 3900. The allocator_arg_t overloads of promise_type::operator new
471 // should not be constrained
472 template<typename _Alloc, typename... _Args>
474 operator new(std::size_t __sz,
475 allocator_arg_t, const _Alloc& __a,
478 static_assert(convertible_to<const _Alloc&, _Allocator>,
479 "the allocator argument to the coroutine must be "
480 "convertible to the generator's allocator type");
481 return _M_allocate(_Rebound(_Allocator(__a)), __sz);
484 template<typename _This, typename _Alloc, typename... _Args>
486 operator new(std::size_t __sz,
488 allocator_arg_t, const _Alloc& __a,
491 static_assert(convertible_to<const _Alloc&, _Allocator>,
492 "the allocator argument to the coroutine must be "
493 "convertible to the generator's allocator type");
494 return _M_allocate(_Rebound(_Allocator(__a)), __sz);
498 operator delete(void* __ptr, std::size_t __csz) noexcept
500 if constexpr (_Stateless_alloc<_Rebound>)
503 return __b.deallocate(reinterpret_cast<_Alloc_block*>(__ptr),
504 _Alloc_block::_M_cnt(__csz));
508 auto __nsz = _Alloc_block::_M_cnt(_M_alloc_size(__csz));
509 auto __fn = reinterpret_cast<std::uintptr_t>(__ptr);
510 auto __an = _M_alloc_address(__fn, __csz);
511 _Rebound __b(std::move(*__an));
513 __b.deallocate(reinterpret_cast<_Alloc_block*>(__ptr), __nsz);
519 class _Promise_alloc<void>
521 using _Dealloc_fn = void (*)(void*, std::size_t);
524 _M_dealloc_address(std::uintptr_t __fn, std::uintptr_t __fsz) noexcept
526 auto __an = __fn + __fsz;
527 auto __ba = alignof(_Dealloc_fn);
528 auto __aligned = ((__an + __ba - 1) / __ba) * __ba;
529 return reinterpret_cast<_Dealloc_fn*>(__aligned);
532 template<typename _Rebound>
534 _M_alloc_address(std::uintptr_t __fn, std::uintptr_t __fsz) noexcept
535 requires (!_Stateless_alloc<_Rebound>)
537 auto __ba = alignof(_Rebound);
538 auto __da = _M_dealloc_address(__fn, __fsz);
539 auto __aan = reinterpret_cast<std::uintptr_t>(__da);
540 __aan += sizeof(_Dealloc_fn);
541 auto __aligned = ((__aan + __ba - 1) / __ba) * __ba;
542 return reinterpret_cast<_Rebound*>(__aligned);
545 template<typename _Rebound>
547 _M_alloc_size(std::size_t __csz) noexcept
549 // This time, we want the coroutine frame, then the deallocator
550 // pointer, then the allocator itself, if any.
551 std::size_t __aa = 0;
552 std::size_t __as = 0;
553 if constexpr (!std::same_as<_Rebound, void>)
555 __aa = alignof(_Rebound);
556 __as = sizeof(_Rebound);
558 auto __ba = __aa + alignof(_Dealloc_fn);
559 return __csz + __ba + __as + sizeof(_Dealloc_fn);
562 template<typename _Rebound>
564 _M_deallocator(void* __ptr, std::size_t __csz) noexcept
566 auto __asz = _M_alloc_size<_Rebound>(__csz);
567 auto __nblk = _Alloc_block::_M_cnt(__asz);
569 if constexpr (_Stateless_alloc<_Rebound>)
572 __b.deallocate(reinterpret_cast<_Alloc_block*>(__ptr), __nblk);
576 auto __fn = reinterpret_cast<std::uintptr_t>(__ptr);
577 auto __an = _M_alloc_address<_Rebound>(__fn, __csz);
578 _Rebound __b(std::move(*__an));
580 __b.deallocate(reinterpret_cast<_Alloc_block*>(__ptr), __nblk);
584 template<typename _Alloc>
586 _M_allocate(const _Alloc& __a, std::size_t __csz)
588 using _Rebound = __alloc_rebind<_Alloc, _Alloc_block>;
589 using _Rebound_ATr = allocator_traits<_Rebound>;
591 static_assert(is_pointer_v<typename _Rebound_ATr::pointer>,
592 "Must use allocators for true pointers with generators");
594 _Dealloc_fn __d = &_M_deallocator<_Rebound>;
595 auto __b = static_cast<_Rebound>(__a);
596 auto __asz = _M_alloc_size<_Rebound>(__csz);
597 auto __nblk = _Alloc_block::_M_cnt(__asz);
598 void* __p = __b.allocate(__nblk);
599 auto __pn = reinterpret_cast<std::uintptr_t>(__p);
600 *_M_dealloc_address(__pn, __csz) = __d;
601 if constexpr (!_Stateless_alloc<_Rebound>)
603 auto __an = _M_alloc_address<_Rebound>(__pn, __csz);
604 ::new (__an) _Rebound(std::move(__b));
610 operator new(std::size_t __sz)
612 auto __nsz = _M_alloc_size<void>(__sz);
613 _Dealloc_fn __d = [] (void* __ptr, std::size_t __sz)
615 ::operator delete(__ptr, _M_alloc_size<void>(__sz));
617 auto __p = ::operator new(__nsz);
618 auto __pn = reinterpret_cast<uintptr_t>(__p);
619 *_M_dealloc_address(__pn, __sz) = __d;
623 template<typename _Alloc, typename... _Args>
625 operator new(std::size_t __sz,
626 allocator_arg_t, const _Alloc& __a,
628 { return _M_allocate(__a, __sz); }
630 template<typename _This, typename _Alloc, typename... _Args>
632 operator new(std::size_t __sz,
634 allocator_arg_t, const _Alloc& __a,
636 { return _M_allocate(__a, __sz); }
639 operator delete(void* __ptr, std::size_t __sz) noexcept
642 auto __pn = reinterpret_cast<uintptr_t>(__ptr);
643 __d = *_M_dealloc_address(__pn, __sz);
648 template<typename _Tp>
649 concept _Cv_unqualified_object = is_object_v<_Tp>
650 && same_as<_Tp, remove_cv_t<_Tp>>;
654 template<typename _Ref, typename _Val, typename _Alloc>
656 : public ranges::view_interface<generator<_Ref, _Val, _Alloc>>
658 using _Value = __conditional_t<is_void_v<_Val>,
659 remove_cvref_t<_Ref>,
661 static_assert(__gen::_Cv_unqualified_object<_Value>,
662 "Generator value must be a cv-unqualified object type");
663 using _Reference = __gen::_Reference_t<_Ref, _Val>;
664 static_assert(is_reference_v<_Reference>
665 || (__gen::_Cv_unqualified_object<_Reference>
666 && copy_constructible<_Reference>),
667 "Generator reference type must be either a cv-unqualified "
668 "object type that is trivially constructible or a "
671 using _RRef = __conditional_t<
672 is_reference_v<_Reference>,
673 remove_reference_t<_Reference>&&,
676 /* Required to model indirectly_readable, and input_iterator. */
677 static_assert(common_reference_with<_Reference&&, _Value&&>);
678 static_assert(common_reference_with<_Reference&&, _RRef&&>);
679 static_assert(common_reference_with<_RRef&&, const _Value&>);
681 using _Yielded = __gen::_Yield_t<_Reference>;
682 using _Erased_promise = __gen::_Promise_erased<_Yielded>;
686 friend _Erased_promise;
687 friend struct _Erased_promise::_Subyield_state;
689 using yielded = _Yielded;
691 struct promise_type : _Erased_promise, __gen::_Promise_alloc<_Alloc>
693 generator get_return_object() noexcept
694 { return { coroutine_handle<promise_type>::from_promise(*this) }; }
697#ifdef __glibcxx_is_pointer_interconvertible
698 static_assert(is_pointer_interconvertible_base_of_v<_Erased_promise,
702 generator(const generator&) = delete;
704 generator(generator&& __other) noexcept
705 : _M_coro(std::__exchange(__other._M_coro, nullptr)),
706 _M_began(std::__exchange(__other._M_began, false))
711 if (auto& __c = this->_M_coro)
716 operator=(generator __other) noexcept
718 swap(__other._M_coro, this->_M_coro);
719 swap(__other._M_began, this->_M_began);
726 this->_M_mark_as_started();
727 auto __h = _Coro_handle::from_promise(_M_coro.promise());
728 __h.promise()._M_nest._M_top() = __h;
734 { return default_sentinel; }
737 using _Coro_handle = std::coroutine_handle<_Erased_promise>;
739 generator(coroutine_handle<promise_type> __coro) noexcept
740 : _M_coro { move(__coro) }
744 _M_mark_as_started() noexcept
746 __glibcxx_assert(!this->_M_began);
747 this->_M_began = true;
750 coroutine_handle<promise_type> _M_coro;
751 bool _M_began = false;
754 template<class _Ref, class _Val, class _Alloc>
755 struct generator<_Ref, _Val, _Alloc>::_Iterator
757 using value_type = _Value;
758 using difference_type = ptrdiff_t;
761 operator==(const _Iterator& __i, default_sentinel_t) noexcept
762 { return __i._M_coro.done(); }
764 friend class generator;
766 _Iterator(_Iterator&& __o) noexcept
767 : _M_coro(std::__exchange(__o._M_coro, {}))
771 operator=(_Iterator&& __o) noexcept
773 this->_M_coro = std::__exchange(__o._M_coro, {});
786 { this->operator++(); }
790 const noexcept(is_nothrow_move_constructible_v<_Reference>)
792 auto& __p = this->_M_coro.promise();
793 return static_cast<_Reference>(*__p._M_value());
797 friend class generator;
799 _Iterator(_Coro_handle __g)
805 auto& __t = this->_M_coro.promise()._M_nest._M_top();
809 _Coro_handle _M_coro;
816 template<typename _Ref, typename _Val = void>
817 using generator = std::generator<_Ref, _Val, polymorphic_allocator<std::byte>>;
821_GLIBCXX_END_NAMESPACE_VERSION
823#endif // __cpp_lib_generator
825#endif // _GLIBCXX_GENERATOR