|
| 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___TYPE_TRAITS_DISENGAGED_TRAITS_H |
| 10 | +#define _LIBCPP___TYPE_TRAITS_DISENGAGED_TRAITS_H |
| 11 | + |
| 12 | +#include <__assert> |
| 13 | +#include <__config> |
| 14 | +#include <__cstddef/size_t.h> |
| 15 | +#include <__memory/construct_at.h> |
| 16 | +#include <__type_traits/datasizeof.h> |
| 17 | +#include <__type_traits/enable_if.h> |
| 18 | +#include <__type_traits/is_constant_evaluated.h> |
| 19 | +#include <__type_traits/is_fundamental.h> |
| 20 | +#include <__type_traits/is_integral.h> |
| 21 | +#include <__type_traits/is_trivial.h> |
| 22 | +#include <__type_traits/is_trivially_destructible.h> |
| 23 | +#include <__type_traits/remove_cv.h> |
| 24 | +#include <__type_traits/void_t.h> |
| 25 | +#include <__utility/forward.h> |
| 26 | +#include <__utility/move.h> |
| 27 | +#include <__utility/piecewise_construct.h> |
| 28 | +#include <cstdint> |
| 29 | + |
| 30 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 31 | +# pragma GCC system_header |
| 32 | +#endif |
| 33 | + |
| 34 | +#if _LIBCPP_STD_VER >= 17 |
| 35 | + |
| 36 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 37 | + |
| 38 | +template <class> |
| 39 | +struct __tombstone_traits; |
| 40 | + |
| 41 | +// bools have always exactly one bit set. If there is more than one set it's disengaged. |
| 42 | +template <> |
| 43 | +struct __tombstone_traits<bool> { |
| 44 | + static constexpr uint8_t __disengaged_value_ = 3; |
| 45 | + static constexpr size_t __is_disengaged_offset_ = 0; |
| 46 | +}; |
| 47 | + |
| 48 | +struct __tombstone_traits_assume_aligned_pointer { |
| 49 | + static constexpr uint8_t __disengaged_value_ = 1; |
| 50 | +#ifdef _LIBCPP_LITTLE_ENDIAN |
| 51 | + static constexpr size_t __is_disengaged_offset_ = 0; |
| 52 | +#else |
| 53 | + static constexpr size_t __is_disengaged_offset_ = sizeof(void*) - 1; |
| 54 | +#endif |
| 55 | +}; |
| 56 | + |
| 57 | +// TODO: Look into |
| 58 | +// - filesystem::directory_iterator |
| 59 | +// - vector<T> with alignof(T) == 1 |
| 60 | +// - string_view (basic_string_view<T> works with alignof(T) >= 2) |
| 61 | + |
| 62 | +// This is constrained on fundamental types because we might not always know the alignment of a user-defined type. |
| 63 | +// For example, in one TU there may only be a forward declaration and in another there is already the definition |
| 64 | +// available. If we made this optimization conditional on the completeness of the type this would result in a non-benign |
| 65 | +// ODR violation. |
| 66 | +template <class _Tp> |
| 67 | +struct __tombstone_traits<__enable_specialization_if<is_fundamental_v<_Tp> && alignof(_Tp) >= 2, _Tp*>> |
| 68 | + : __tombstone_traits_assume_aligned_pointer {}; |
| 69 | + |
| 70 | +template <class _Tp> |
| 71 | +struct __tombstone_traits<_Tp**> : __tombstone_traits_assume_aligned_pointer { |
| 72 | + static_assert(alignof(_Tp*) >= 2, "alignment of a pointer isn't at least 2!?"); |
| 73 | +}; |
| 74 | + |
| 75 | +inline constexpr struct __init_engaged_t { |
| 76 | +} __init_engaged; |
| 77 | +inline constexpr struct __init_disengaged_t { |
| 78 | +} __init_disengaged; |
| 79 | + |
| 80 | +template <class _Tp, class _Payload, bool = __tombstone_traits<_Tp>::__is_disengaged_offset_ == 0> |
| 81 | +struct __tombstone_is_disengaged { |
| 82 | + using _TombstoneLayout = __tombstone_traits<_Tp>; |
| 83 | + using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>; |
| 84 | + |
| 85 | + char __padding_[_TombstoneLayout::__is_disengaged_offset_]; |
| 86 | + _IsDisengagedT __is_disengaged_; |
| 87 | +}; |
| 88 | + |
| 89 | +template <class _Tp, class _Payload> |
| 90 | +struct __tombstone_is_disengaged<_Tp, _Payload, true> { |
| 91 | + using _TombstoneLayout = __tombstone_traits<_Tp>; |
| 92 | + using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>; |
| 93 | + |
| 94 | + _IsDisengagedT __is_disengaged_; |
| 95 | +}; |
| 96 | + |
| 97 | +template <class _Tp, class _Payload, bool = __tombstone_traits<_Tp>::__is_disengaged_offset_ == 0> |
| 98 | +struct __tombstone_data { |
| 99 | + using _TombstoneLayout = __tombstone_traits<_Tp>; |
| 100 | + using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>; |
| 101 | + |
| 102 | + static_assert(is_trivial<_IsDisengagedT>::value, "disengaged type has to be trivial!"); |
| 103 | + static_assert(_TombstoneLayout::__is_disengaged_offset_ >= __datasizeof_v<_Payload>); |
| 104 | + |
| 105 | + _LIBCPP_NO_UNIQUE_ADDRESS _Payload __payload_; |
| 106 | + char __padding_[_TombstoneLayout::__is_disengaged_offset_ - __datasizeof_v<_Payload>]; |
| 107 | + _IsDisengagedT __is_disengaged_; |
| 108 | + |
| 109 | + template <class... _Args> |
| 110 | + _LIBCPP_HIDE_FROM_ABI constexpr __tombstone_data(_Args&&... __args) |
| 111 | + : __payload_(std::forward<_Args>(__args)...), __is_disengaged_(_TombstoneLayout::__disengaged_value_) {} |
| 112 | +}; |
| 113 | + |
| 114 | +template <class _Tp, class _Payload> |
| 115 | +struct __tombstone_data<_Tp, _Payload, true> { |
| 116 | + using _TombstoneLayout = __tombstone_traits<_Tp>; |
| 117 | + using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>; |
| 118 | + |
| 119 | + _IsDisengagedT __is_disengaged_; |
| 120 | + _LIBCPP_NO_UNIQUE_ADDRESS _Payload __payload_; |
| 121 | + |
| 122 | + template <class... _Args> |
| 123 | + _LIBCPP_HIDE_FROM_ABI constexpr __tombstone_data(_Args&&... __args) |
| 124 | + : __is_disengaged_(_TombstoneLayout::__disengaged_value_), __payload_(std::forward<_Args>(__args)...) {} |
| 125 | +}; |
| 126 | + |
| 127 | +template <class _Tp, class _Payload> |
| 128 | +struct __tombstoned_value final { |
| 129 | + using _TombstoneLayout = __tombstone_traits<_Tp>; |
| 130 | + using _TombstoneData = __tombstone_data<_Tp, _Payload>; |
| 131 | + |
| 132 | + template <bool = is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Payload>> |
| 133 | + union _MaybeTombstone { |
| 134 | + _Tp __value_; |
| 135 | + _TombstoneData __tombstone_; |
| 136 | + |
| 137 | + template <class... _Args> |
| 138 | + constexpr _MaybeTombstone(__init_disengaged_t, _Args&&... __args) : __tombstone_(std::forward<_Args>(__args)...) {} |
| 139 | + |
| 140 | + template <class... _Args> |
| 141 | + constexpr _MaybeTombstone(__init_engaged_t, _Args&&... __args) : __value_(std::forward<_Args>(__args)...) {} |
| 142 | + |
| 143 | + _MaybeTombstone(const _MaybeTombstone&) = default; |
| 144 | + _MaybeTombstone(_MaybeTombstone&&) = default; |
| 145 | + _MaybeTombstone& operator=(const _MaybeTombstone&) = default; |
| 146 | + _MaybeTombstone& operator=(_MaybeTombstone&&) = default; |
| 147 | + |
| 148 | + _LIBCPP_HIDE_FROM_ABI constexpr bool __is_engaged() const noexcept { |
| 149 | + if (__libcpp_is_constant_evaluated()) |
| 150 | + return !__builtin_constant_p(__tombstone_.__is_disengaged_ == _TombstoneLayout::__disengaged_value_); |
| 151 | + __tombstone_is_disengaged<_Tp, _Payload> __is_disengaged; |
| 152 | + static_assert(sizeof(__tombstone_is_disengaged<_Tp, _Payload>) <= sizeof(_MaybeTombstone)); |
| 153 | + __builtin_memcpy(&__is_disengaged, this, sizeof(__tombstone_is_disengaged<_Tp, _Payload>)); |
| 154 | + |
| 155 | + return __is_disengaged.__is_disengaged_ != _TombstoneLayout::__disengaged_value_; |
| 156 | + } |
| 157 | + |
| 158 | + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~_MaybeTombstone() { |
| 159 | + if (__is_engaged()) { |
| 160 | + std::destroy_at(&__value_); |
| 161 | + } else { |
| 162 | + std::destroy_at(&__tombstone_); |
| 163 | + } |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + template <> |
| 168 | + union _MaybeTombstone<true> { |
| 169 | + _Tp __value_; |
| 170 | + _TombstoneData __tombstone_; |
| 171 | + |
| 172 | + template <class... _Args> |
| 173 | + constexpr _MaybeTombstone(__init_disengaged_t, _Args&&... __args) : __tombstone_(std::forward<_Args>(__args)...) {} |
| 174 | + |
| 175 | + template <class... _Args> |
| 176 | + constexpr _MaybeTombstone(__init_engaged_t, _Args&&... __args) : __value_(std::forward<_Args>(__args)...) {} |
| 177 | + |
| 178 | + _MaybeTombstone(const _MaybeTombstone&) = default; |
| 179 | + _MaybeTombstone(_MaybeTombstone&&) = default; |
| 180 | + _MaybeTombstone& operator=(const _MaybeTombstone&) = default; |
| 181 | + _MaybeTombstone& operator=(_MaybeTombstone&&) = default; |
| 182 | + |
| 183 | + _LIBCPP_HIDE_FROM_ABI constexpr bool __is_engaged() const noexcept { |
| 184 | + if (__libcpp_is_constant_evaluated()) |
| 185 | + return !__builtin_constant_p(__tombstone_.__is_disengaged_ == _TombstoneLayout::__disengaged_value_); |
| 186 | + __tombstone_is_disengaged<_Tp, _Payload> __is_disengaged; |
| 187 | + static_assert(sizeof(__tombstone_is_disengaged<_Tp, _Payload>) <= sizeof(_MaybeTombstone)); |
| 188 | + __builtin_memcpy(&__is_disengaged, this, sizeof(__tombstone_is_disengaged<_Tp, _Payload>)); |
| 189 | + |
| 190 | + return __is_disengaged.__is_disengaged_ != _TombstoneLayout::__disengaged_value_; |
| 191 | + } |
| 192 | + |
| 193 | + _LIBCPP_CONSTEXPR_SINCE_CXX20 ~_MaybeTombstone() = default; |
| 194 | + }; |
| 195 | + |
| 196 | + _MaybeTombstone<> __data_; |
| 197 | + |
| 198 | + static_assert(sizeof(__tombstone_data<_Tp, _Payload>) <= sizeof(_Tp)); |
| 199 | + static_assert(is_integral_v<decltype(_TombstoneLayout::__disengaged_value_)>); |
| 200 | + static_assert(__builtin_offsetof(_TombstoneData, __is_disengaged_) == _TombstoneLayout::__is_disengaged_offset_); |
| 201 | + |
| 202 | + template <class... _Args> |
| 203 | + _LIBCPP_HIDE_FROM_ABI constexpr __tombstoned_value(__init_disengaged_t, _Args&&... __args) |
| 204 | + : __data_(__init_disengaged, std::forward<_Args>(__args)...) {} |
| 205 | + |
| 206 | + template <class... _Args> |
| 207 | + _LIBCPP_HIDE_FROM_ABI constexpr __tombstoned_value(__init_engaged_t, _Args&&... __args) |
| 208 | + : __data_(__init_engaged, std::forward<_Args>(__args)...) {} |
| 209 | + |
| 210 | + _LIBCPP_HIDE_FROM_ABI constexpr bool __is_engaged() const noexcept { return __data_.__is_engaged(); } |
| 211 | + |
| 212 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_value() & noexcept { |
| 213 | + _LIBCPP_ASSERT_INTERNAL(__self.__is_engaged(), "Trying to get the value of a disenagaged tombstoned value"); |
| 214 | + return __data_.__value_; |
| 215 | + } |
| 216 | + |
| 217 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_value() const & noexcept { |
| 218 | + _LIBCPP_ASSERT_INTERNAL(__self.__is_engaged(), "Trying to get the value of a disenagaged tombstoned value"); |
| 219 | + return __data_.__value_; |
| 220 | + } |
| 221 | + |
| 222 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_value() && noexcept { |
| 223 | + _LIBCPP_ASSERT_INTERNAL(__self.__is_engaged(), "Trying to get the value of a disenagaged tombstoned value"); |
| 224 | + return std::move(__data_.__value_); |
| 225 | + } |
| 226 | + |
| 227 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_value() const && noexcept { |
| 228 | + _LIBCPP_ASSERT_INTERNAL(__self.__is_engaged(), "Trying to get the value of a disenagaged tombstoned value"); |
| 229 | + return std::move(__data_.__value_); |
| 230 | + } |
| 231 | + |
| 232 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_payload() & noexcept { |
| 233 | + _LIBCPP_ASSERT_INTERNAL(!__self.__is_engaged(), "Trying to get the payload of an enagaged tombstoned value"); |
| 234 | + return __data_.__tombstone_.__payload_; |
| 235 | + } |
| 236 | + |
| 237 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_payload() const & noexcept { |
| 238 | + _LIBCPP_ASSERT_INTERNAL(!__self.__is_engaged(), "Trying to get the payload of an enagaged tombstoned value"); |
| 239 | + return __data_.__tombstone_.__payload_; |
| 240 | + } |
| 241 | + |
| 242 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_payload() && noexcept { |
| 243 | + _LIBCPP_ASSERT_INTERNAL(!__self.__is_engaged(), "Trying to get the payload of an enagaged tombstoned value"); |
| 244 | + return std::move(__data_.__tombstone_.__payload_); |
| 245 | + } |
| 246 | + |
| 247 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_payload() const && noexcept { |
| 248 | + _LIBCPP_ASSERT_INTERNAL(!__self.__is_engaged(), "Trying to get the payload of an enagaged tombstoned value"); |
| 249 | + return std::move(__data_.__tombstone_.__payload_); |
| 250 | + } |
| 251 | + |
| 252 | + template <class... _Args> |
| 253 | + _LIBCPP_HIDE_FROM_ABI constexpr void __engage(piecewise_construct_t, _Args&&... __args) { |
| 254 | + _LIBCPP_ASSERT_INTERNAL(!__self.__is_engaged(), "Trying to enage a already engaged tombstoned value"); |
| 255 | + std::destroy_at(&__data_.__tombstone_); |
| 256 | + std::__construct_at(&__data_.__value_, std::forward<_Args>(__args)...); |
| 257 | + } |
| 258 | + |
| 259 | + template <class... _Args> |
| 260 | + _LIBCPP_HIDE_FROM_ABI constexpr void __disengage(piecewise_construct_t, _Args&&... __args) { |
| 261 | + _LIBCPP_ASSERT_INTERNAL(!__self.__is_engaged(), "Trying to disenage a disengaged tombstoned value"); |
| 262 | + std::destroy_at(&__data_.__value_); |
| 263 | + std::__construct_at(&__data_.__tombstone_, std::forward<_Args>(__args)...); |
| 264 | + } |
| 265 | +}; |
| 266 | + |
| 267 | +template <class _Tp, class = void> |
| 268 | +inline constexpr bool __has_tombstone_v = false; |
| 269 | + |
| 270 | +template <class _Tp> |
| 271 | +inline constexpr bool __has_tombstone_v<_Tp, void_t<decltype(sizeof(__tombstone_traits<_Tp>))>> = true; |
| 272 | + |
| 273 | +_LIBCPP_END_NAMESPACE_STD |
| 274 | + |
| 275 | +#endif // _LIBCPP_STD_VER >= 17 |
| 276 | + |
| 277 | +#endif // _LIBCPP___TYPE_TRAITS_DISENGAGED_TRAITS_H |
0 commit comments