File: noop_coroutine_handle.h

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (112 lines) | stat: -rw-r--r-- 3,693 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___COROUTINE_NOOP_COROUTINE_HANDLE_H
#define _LIBCPP___COROUTINE_NOOP_COROUTINE_HANDLE_H

#include <__config>
#include <__coroutine/coroutine_handle.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CXX20_COROUTINES)

_LIBCPP_BEGIN_NAMESPACE_STD

#if __has_builtin(__builtin_coro_noop) || defined(_LIBCPP_COMPILER_GCC)

// [coroutine.noop]
// [coroutine.promise.noop]
struct noop_coroutine_promise {};

// [coroutine.handle.noop]
template <>
struct _LIBCPP_TEMPLATE_VIS coroutine_handle<noop_coroutine_promise> {
public:
    // [coroutine.handle.noop.conv], conversion
    _LIBCPP_HIDE_FROM_ABI
    constexpr operator coroutine_handle<>() const noexcept {
        return coroutine_handle<>::from_address(address());
    }

    // [coroutine.handle.noop.observers], observers
    _LIBCPP_HIDE_FROM_ABI
    constexpr explicit operator bool() const noexcept { return true; }
    _LIBCPP_HIDE_FROM_ABI
    constexpr bool done() const noexcept { return false; }

    // [coroutine.handle.noop.resumption], resumption
    _LIBCPP_HIDE_FROM_ABI
    constexpr void operator()() const noexcept {}
    _LIBCPP_HIDE_FROM_ABI
    constexpr void resume() const noexcept {}
    _LIBCPP_HIDE_FROM_ABI
    constexpr void destroy() const noexcept {}

    // [coroutine.handle.noop.promise], promise access
    _LIBCPP_HIDE_FROM_ABI
    noop_coroutine_promise& promise() const noexcept {
        return *static_cast<noop_coroutine_promise*>(
            __builtin_coro_promise(this->__handle_, alignof(noop_coroutine_promise), false));
    }

    // [coroutine.handle.noop.address], address
    _LIBCPP_HIDE_FROM_ABI
    constexpr void* address() const noexcept { return __handle_; }

private:
    _LIBCPP_HIDE_FROM_ABI
    friend coroutine_handle<noop_coroutine_promise> noop_coroutine() noexcept;

#if __has_builtin(__builtin_coro_noop)
    _LIBCPP_HIDE_FROM_ABI coroutine_handle() noexcept {
        this->__handle_ = __builtin_coro_noop();
    }

    void* __handle_ = nullptr;

#elif defined(_LIBCPP_COMPILER_GCC)
    // GCC doesn't implement __builtin_coro_noop().
    // Construct the coroutine frame manually instead.
    struct __noop_coroutine_frame_ty_ {
        static void __dummy_resume_destroy_func() { }

        void (*__resume_)() = __dummy_resume_destroy_func;
        void (*__destroy_)() = __dummy_resume_destroy_func;
        struct noop_coroutine_promise __promise_;
    };

    static __noop_coroutine_frame_ty_ __noop_coroutine_frame_;

    void* __handle_ = &__noop_coroutine_frame_;

    _LIBCPP_HIDE_FROM_ABI coroutine_handle() noexcept = default;

#endif // __has_builtin(__builtin_coro_noop)
};

using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;

#if defined(_LIBCPP_COMPILER_GCC)
inline noop_coroutine_handle::__noop_coroutine_frame_ty_
    noop_coroutine_handle::__noop_coroutine_frame_{};
#endif

// [coroutine.noop.coroutine]
inline _LIBCPP_HIDE_FROM_ABI
noop_coroutine_handle noop_coroutine() noexcept { return noop_coroutine_handle(); }

#endif // __has_builtin(__builtin_coro_noop) || defined(_LIBCPP_COMPILER_GCC)

_LIBCPP_END_NAMESPACE_STD

#endif // __LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CXX20_COROUTINES)

#endif // _LIBCPP___COROUTINE_NOOP_COROUTINE_HANDLE_H