diff --git a/clang/include/clang/AST/APValue.h b/clang/include/clang/AST/APValue.h index c4206b73b1156..300fa9cac5b12 100644 --- a/clang/include/clang/AST/APValue.h +++ b/clang/include/clang/AST/APValue.h @@ -198,6 +198,8 @@ class APValue { /// The QualType, if this is a DynamicAllocLValue. void *DynamicAllocType; }; + public: + uint64_t Metadata{0}; }; /// A FieldDecl or CXXRecordDecl, along with a flag indicating whether we @@ -483,6 +485,8 @@ class APValue { } const LValueBase getLValueBase() const; + uint64_t getLValueMetadata() const; + uint64_t & getLValueMetadata(); CharUnits &getLValueOffset(); const CharUnits &getLValueOffset() const { return const_cast(this)->getLValueOffset(); diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index b025a7681bfac..ff427bca17632 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -4774,3 +4774,40 @@ def ArithmeticFence : LangBuiltin<"ALL_LANGUAGES"> { let Attributes = [CustomTypeChecking, Constexpr]; let Prototype = "void(...)"; } + +// support for pointer tagging +// (ptr & mask) | (val & ~mask) +def TagPointerMaskOr : Builtin { + let Spellings = ["__builtin_tag_pointer_mask_or"]; + let Attributes = [Constexpr, NoThrow]; + let Prototype = "void*(void*, size_t, size_t)"; +} + +// (ptr & mask) -> void * +def TagPointerMask : Builtin { + let Spellings = ["__builtin_tag_pointer_mask"]; + let Attributes = [Constexpr, NoThrow]; + let Prototype = "void*(void*, size_t)"; +} + +// (ptr & mask) -> uintptr_t +def TagPointerMaskAsInt : Builtin { + let Spellings = ["__builtin_tag_pointer_mask_as_int"]; + let Attributes = [Constexpr, NoThrow]; + let Prototype = "size_t(void*, size_t)"; +} + +// (ptr << shift) | (value & ~mask) -> void * +// mask = (1 << shift) - 1 +def TagPointerShiftOr : Builtin { + let Spellings = ["__builtin_tag_pointer_shift_or"]; + let Attributes = [Constexpr, NoThrow]; + let Prototype = "void*(void*, size_t, size_t)"; +} + +// (ptr >> unshift) -> void * +def TagPointerUnshift : Builtin { + let Spellings = ["__builtin_tag_pointer_unshift"]; + let Attributes = [Constexpr, NoThrow]; + let Prototype = "void*(void*, size_t)"; +} diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td b/clang/include/clang/Basic/DiagnosticASTKinds.td index a024f9b2a9f8c..e31fe17bc22cb 100644 --- a/clang/include/clang/Basic/DiagnosticASTKinds.td +++ b/clang/include/clang/Basic/DiagnosticASTKinds.td @@ -219,6 +219,12 @@ def note_constexpr_access_past_end : Note< "destruction of}0 " "dereferenced one-past-the-end pointer is not allowed " "in a constant expression">; +def note_constexpr_dereferencing_tagged_pointer: Note< + "dereferencing tagged pointer">; +def note_constexpr_tagging_with_shift_zero: Note< + "you must shift pointer at least by one bit to store a tag">; +def note_constexpr_tagging_with_empty_mask: Note< + "you must provide non-zero mask for pointer tagging">; def note_constexpr_access_unsized_array : Note< "%select{read of|read of|assignment to|increment of|decrement of|" "member call on|dynamic_cast of|typeid applied to|construction of|" diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp index d8e33ff421c06..acd3b8c9607b0 100644 --- a/clang/lib/AST/APValue.cpp +++ b/clang/lib/AST/APValue.cpp @@ -976,6 +976,16 @@ const APValue::LValueBase APValue::getLValueBase() const { return ((const LV *)(const void *)&Data)->Base; } +uint64_t APValue::getLValueMetadata() const { + assert(isLValue() && "Invalid accessor"); + return ((const LV *)(const void *)&Data)->Base.Metadata; +} + +uint64_t & APValue::getLValueMetadata() { + assert(isLValue() && "Invalid accessor"); + return ((LV *)(void *)&Data)->Base.Metadata; +} + bool APValue::isLValueOnePastTheEnd() const { assert(isLValue() && "Invalid accessor"); return ((const LV *)(const void *)&Data)->IsOnePastTheEnd; diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 4d2d05307a6de..26d6311ded2c6 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -4395,6 +4395,11 @@ handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type, bool WantObjectRepresentation = false) { if (LVal.Designator.Invalid) return false; + + if (LVal.Base.Metadata != 0) { + Info.FFDiag(Conv, diag::note_constexpr_dereferencing_tagged_pointer); + return false; + } // Check for special cases where there is no existing APValue to look at. const Expr *Base = LVal.Base.dyn_cast(); @@ -9627,6 +9632,87 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, return Success(E); switch (BuiltinOp) { + // emulation of pointer tagging without actually touching pointer value + // as there is no such thing as address here, so tag is stored as a metadata in lvalue base + case Builtin::BI__builtin_tag_pointer_mask_or: { + APSInt Value, Mask; + if (!evaluatePointer(E->getArg(0), Result)) + return Error(E); + + if (!EvaluateInteger(E->getArg(1), Value, Info)) + return Error(E); + + if (!EvaluateInteger(E->getArg(2), Mask, Info)) + return Error(E); + + if (Mask.getLimitedValue() == 0) { + CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_empty_mask); + return false; + } + + Result.Base.Metadata = (Result.Base.Metadata & ~Mask.getLimitedValue()) | (Value.getLimitedValue() & Mask.getLimitedValue()); + return true; + } + + // alternative approach to tagging which shifts pointer + // here we are only shifting metadata + case Builtin::BI__builtin_tag_pointer_shift_or: { + APSInt Value, Shift; + if (!evaluatePointer(E->getArg(0), Result)) + return Error(E); + + if (!EvaluateInteger(E->getArg(1), Value, Info)) + return Error(E); + + if (!EvaluateInteger(E->getArg(2), Shift, Info)) + return Error(E); + + if (Shift.getLimitedValue() == 0) { + CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_shift_zero); + return false; + } + + const uint64_t Mask = (1ull << static_cast(Shift.getLimitedValue())) - 1ull; + Result.Base.Metadata = (Result.Base.Metadata << static_cast(Shift.getLimitedValue())) | (Value.getLimitedValue() & Mask); + return true; + } + + // recover pointer by masking metadata + // exprconstant allows dereferencing only metadata == 0 pointer + case Builtin::BI__builtin_tag_pointer_mask: { + APSInt Mask; + if (!evaluatePointer(E->getArg(0), Result)) + return Error(E); + + if (!EvaluateInteger(E->getArg(1), Mask, Info)) + return Error(E); + + if (Mask.getLimitedValue() == 0) { + CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_empty_mask); + return false; + } + + Result.Base.Metadata = (Result.Base.Metadata & Mask.getLimitedValue()); + return true; + } + + // shifting back pointer (also can convert tagged pointer back to normal pointer) + case Builtin::BI__builtin_tag_pointer_unshift: { + APSInt Shift; + if (!evaluatePointer(E->getArg(0), Result)) + return Error(E); + + if (!EvaluateInteger(E->getArg(1), Shift, Info)) + return Error(E); + + if (Shift.getLimitedValue() == 0) { + CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_shift_zero); + return false; + } + + Result.Base.Metadata = (Result.Base.Metadata >> static_cast(Shift.getLimitedValue())); + return true; + } case Builtin::BIaddressof: case Builtin::BI__addressof: case Builtin::BI__builtin_addressof: @@ -12520,6 +12606,25 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, default: return false; + case Builtin::BI__builtin_tag_pointer_mask_as_int: { + LValue Pointer; + APSInt Mask; + + if (!EvaluatePointer(E->getArg(0), Pointer, Info)) + return Error(E); + + if (!EvaluateInteger(E->getArg(1), Mask, Info)) + return Error(E); + + if (Mask.getLimitedValue() == 0) { + CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_empty_mask); + return false; + } + + const uint64_t Result = Pointer.Base.Metadata & (static_cast(Mask.getLimitedValue())); + return Success(Result, E); + } + case Builtin::BI__builtin_dynamic_object_size: case Builtin::BI__builtin_object_size: { // The type was checked when we built the expression. @@ -13904,6 +14009,13 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, return Success(CmpResult::Less, E); if (CompareLHS > CompareRHS) return Success(CmpResult::Greater, E); + + // this makes tagged pointer not equal to original pointer + if (LHSValue.Base.Metadata < RHSValue.Base.Metadata) + return Success(CmpResult::Less, E); + if (LHSValue.Base.Metadata > RHSValue.Base.Metadata) + return Success(CmpResult::Greater, E); + return Success(CmpResult::Equal, E); } diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index d1af7fde157b6..26eddd6e7572d 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -5254,6 +5254,19 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, return RValue::get(Carry); } + + // support for pointer tagging + case Builtin::BI__builtin_tag_pointer_mask_or: + return EmitBuiltinTagPointerMaskOr(E); + case Builtin::BI__builtin_tag_pointer_mask: + return EmitBuiltinTagPointerMask(E); + case Builtin::BI__builtin_tag_pointer_mask_as_int: + return EmitBuiltinTagPointerMaskAsInt(E); + case Builtin::BI__builtin_tag_pointer_shift_or: + return EmitBuiltinTagPointerShiftOr(E); + case Builtin::BI__builtin_tag_pointer_unshift: + return EmitBuiltinTagPointerUnshift(E); + case Builtin::BIaddressof: case Builtin::BI__addressof: case Builtin::BI__builtin_addressof: @@ -21030,6 +21043,95 @@ Value *CodeGenFunction::EmitNVPTXBuiltinExpr(unsigned BuiltinID, } } +/// Generate (x & ~mask) | (value & mask). +RValue CodeGenFunction::EmitBuiltinTagPointerMaskOr(const CallExpr *E) { + llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value * Value = EmitScalarExpr(E->getArg(1)); + llvm::Value * Mask = EmitScalarExpr(E->getArg(2)); + + llvm::IntegerType * IntType = IntegerType::get(getLLVMContext(), CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); + + // TODO: avoid using bitcast and go path of ptr.tag (mirror to ptr.mask) + // to keep pointer's provenance, but this turns out a bit harder to do as it touches + // a lot of places in llvm + llvm::Value * PointerInt = Builder.CreateBitOrPointerCast(Ptr, IntType, "pointer_int"); + llvm::Value * InvertedMask = Builder.CreateNot(Mask, "inverted_mask"); + + llvm::Value * MaskedPtr = Builder.CreateAnd(PointerInt, InvertedMask, "masked_ptr"); + llvm::Value * MaskedValue = Builder.CreateAnd(Value, Mask, "masked_value"); + + llvm::Value * ResultInt = Builder.CreateOr(MaskedPtr, MaskedValue, "result_int"); + llvm::Value * Result = Builder.CreateBitOrPointerCast(ResultInt, Ptr->getType(), "result_ptr"); + + return RValue::get(Result); +} + +/// Generate (x << shift) | (value & ((1 << shift) - 1)). +RValue CodeGenFunction::EmitBuiltinTagPointerShiftOr(const CallExpr *E) { + llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value * Value = EmitScalarExpr(E->getArg(1)); + llvm::Value * Shift = EmitScalarExpr(E->getArg(2)); + + llvm::IntegerType * IntType = IntegerType::get(getLLVMContext(), CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); + + // TODO: again, for now a bitcast, later ptr.shift_tag + llvm::Value * PointerInt = Builder.CreateBitOrPointerCast(Ptr, IntType, "pointer_int"); + llvm::Value * ShiftedPointerInt = Builder.CreateShl(PointerInt, Shift); + + auto *One = llvm::ConstantInt::get(IntType, 1); + + llvm::Value * Mask = Builder.CreateSub(Builder.CreateShl(One, Shift), One, "mask"); + llvm::Value * MaskedValue = Builder.CreateAnd(Value, Mask, "masked_value"); + llvm::Value * PointerWithTag = Builder.CreateOr(ShiftedPointerInt, MaskedValue, "pointer_with_tag_int"); + + llvm::Value * Result = Builder.CreateBitOrPointerCast(PointerWithTag, Ptr->getType(), "result_ptr"); + return RValue::get(Result); +} + +/// Generate (x >> shift) +RValue CodeGenFunction::EmitBuiltinTagPointerUnshift(const CallExpr *E) { + llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value * Shift = EmitScalarExpr(E->getArg(1)); + + llvm::IntegerType * IntType = IntegerType::get(getLLVMContext(), CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); + + // for now I'm going path of bitcast + llvm::Value * PointerInt = Builder.CreateBitOrPointerCast(Ptr, IntType, "pointer_int"); + llvm::Value * UnShiftedPointerInt = Builder.CreateAShr(PointerInt, Shift, "unshifted_pointer_int"); + + llvm::Value * Result = Builder.CreateBitOrPointerCast(UnShiftedPointerInt, Ptr->getType(), "result_ptr"); + return RValue::get(Result); +} + +/// Generate (x & mask). +RValue CodeGenFunction::EmitBuiltinTagPointerMask(const CallExpr *E) { + llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value * Mask = EmitScalarExpr(E->getArg(1)); + + llvm::Value *Result = Builder.CreateIntrinsic( + Intrinsic::ptrmask, {Ptr->getType(), Mask->getType()}, + {Ptr, Mask}, nullptr, "result"); + + return RValue::get(Result); +} + +/// Generate (x & mask) (but return it as number). +RValue CodeGenFunction::EmitBuiltinTagPointerMaskAsInt(const CallExpr *E) { + llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value * Mask = EmitScalarExpr(E->getArg(1)); + + llvm::IntegerType * IntType = IntegerType::get(getLLVMContext(), CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); + + llvm::Value *Result = Builder.CreateIntrinsic( + Intrinsic::ptrmask, {Ptr->getType(), Mask->getType()}, + {Ptr, Mask}, nullptr, "result"); + + llvm::Value * IntResult = Builder.CreateBitOrPointerCast(Result, IntType, "int_result"); + + return RValue::get(IntResult); +} + + namespace { struct BuiltinAlignArgs { llvm::Value *Src = nullptr; diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 1c0a0e117e560..4fc0d5f9ff1fe 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -4546,6 +4546,13 @@ class CodeGenFunction : public CodeGenTypeCache { RValue emitRotate(const CallExpr *E, bool IsRotateRight); + /// Emit IR for pointer tagging + RValue EmitBuiltinTagPointerMaskOr(const CallExpr *E); + RValue EmitBuiltinTagPointerMask(const CallExpr *E); + RValue EmitBuiltinTagPointerMaskAsInt(const CallExpr *E); + RValue EmitBuiltinTagPointerShiftOr(const CallExpr *E); + RValue EmitBuiltinTagPointerUnshift(const CallExpr *E); + /// Emit IR for __builtin_os_log_format. RValue emitBuiltinOSLogFormat(const CallExpr &E); diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 32579272858a8..a3a97a551a124 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -538,6 +538,7 @@ set(files __memory/inout_ptr.h __memory/out_ptr.h __memory/pointer_traits.h + __memory/pointer_tag_pair.h __memory/ranges_construct_at.h __memory/ranges_uninitialized_algorithms.h __memory/raw_storage_iterator.h diff --git a/libcxx/include/__memory/pointer_tag_pair.h b/libcxx/include/__memory/pointer_tag_pair.h new file mode 100644 index 0000000000000..50189322e7461 --- /dev/null +++ b/libcxx/include/__memory/pointer_tag_pair.h @@ -0,0 +1,206 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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___POINTER_TAG_PAIR_H +#define _LIBCPP___POINTER_TAG_PAIR_H + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER >= 26 + +# include <__assert> +# include <__bit/bit_width.h> +# include <__bit/countr.h> +# include <__config> +# include <__tuple/tuple_element.h> +# include <__tuple/tuple_size.h> +# include <__type_traits/conditional.h> +# include <__type_traits/is_enum.h> +# include <__type_traits/is_integral.h> +# include <__type_traits/is_object.h> +# include <__type_traits/make_unsigned.h> +# include <__type_traits/remove_cvref.h> +# include <__type_traits/underlying_type.h> +# include <__utility/swap.h> +# include +# include + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace __impl { +constexpr bool __is_ptr_aligned(const void* _ptr, size_t _alignment) noexcept { +# if __has_builtin(__builtin_is_aligned) + return __builtin_is_aligned(_ptr, _alignment); +# else + return reinterpret_cast(_ptr) % _alignment == 0; +# endif +} + +template +struct _underlying_type_or_identity { + using type = T; +}; + +template + requires(std::is_enum_v) +struct _underlying_type_or_identity { + using type = std::underlying_type_t; +}; + +template +using _underlying_type_or_identity_t = _underlying_type_or_identity::type; +} // namespace __impl + +template + requires((std::is_void_v || std::is_object_v) && std::is_unsigned_v<__impl::_underlying_type_or_identity_t> && + std::is_same_v>) +struct pointer_tag_pair { +public: + using element_type = PointeeT; + using pointer_type = PointeeT*; + using tagged_pointer_type = void*; // I prefer `void *` to avoid roundtrip over an int and losing provenance + using tag_type = TagT; + +private: + static constexpr unsigned _alignment_needed = (1u << Bits); + static constexpr uintptr_t _tag_mask = (_alignment_needed - 1u); + static constexpr uintptr_t _pointer_mask = ~_tag_mask; + + // for clang it can be just `pointer_type` + using _unspecified_pointer_type = pointer_type; + _unspecified_pointer_type _pointer{nullptr}; // required to have size same as sizeof(Pointee *) + + // it doesn't make sense to ask for all or more bits than size of the pointer + static_assert(Bits < sizeof(pointer_type) * CHAR_BIT); + + // internal constructor for `from_overaligned` and `from_tagged` functions + struct _tagged_t {}; + constexpr pointer_tag_pair(_tagged_t, _unspecified_pointer_type _ptr) noexcept : _pointer{_ptr} {} + + static constexpr auto _pass_thru_mask(tag_type _tag) { + auto _value = static_cast<__impl::_underlying_type_or_identity_t>(_tag) & _tag_mask; + _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(static_cast(_value) == _tag, + "Tag must fit requested bits"); + return _value; + } + + static constexpr auto _from_bitfield_tag(std::size_t _tag) -> tag_type { + return static_cast(static_cast<__impl::_underlying_type_or_identity_t>(_tag)); + } + +public: + // constructors + pointer_tag_pair() = default; // always noexcept + constexpr pointer_tag_pair(nullptr_t) noexcept: _pointer{nullptr} { } + pointer_tag_pair(const pointer_tag_pair&) = default; + pointer_tag_pair(pointer_tag_pair&&) = default; + pointer_tag_pair& operator=(const pointer_tag_pair&) = default; + pointer_tag_pair& operator=(pointer_tag_pair&&) = default; + + constexpr pointer_tag_pair(pointer_type _ptr, tag_type _tag) + requires(alignof(element_type) >= _alignment_needed) + { + _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(std::__impl::__is_ptr_aligned(_ptr, _alignment_needed), + "Pointer must be aligned by provided alignment for tagging"); + + _pointer = static_cast<_unspecified_pointer_type>( + __builtin_tag_pointer_mask_or((void*)_ptr, _pass_thru_mask(_tag), _tag_mask)); + } + + // destructor + ~pointer_tag_pair() = default; + + // special + template + static constexpr pointer_tag_pair from_overaligned(pointer_type _ptr, tag_type _tag) + requires(_AlignmentPromised >= _alignment_needed) + { + _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(std::__impl::__is_ptr_aligned(_ptr, _AlignmentPromised), + "Pointer must be aligned by provided alignment for tagging"); + + return pointer_tag_pair{_tagged_t{}, + static_cast<_unspecified_pointer_type>( + __builtin_tag_pointer_mask_or((void*)_ptr, _pass_thru_mask(_tag), _tag_mask))}; + } + + static pointer_tag_pair from_tagged(tagged_pointer_type ptr) { // no-constexpr + // Precondition: valid pointer if untagged + return pointer_tag_pair{_tagged_t{}, reinterpret_cast<_unspecified_pointer_type>(ptr)}; + } + + // accessors + tagged_pointer_type tagged_pointer() const noexcept { return reinterpret_cast(_pointer); } + constexpr pointer_type pointer() const noexcept { + return static_cast(__builtin_tag_pointer_mask((void*)_pointer, _pointer_mask)); + } + constexpr tag_type tag() const noexcept { + return _from_bitfield_tag(__builtin_tag_pointer_mask_as_int((void*)_pointer, _tag_mask)); + } + + // swap + friend constexpr void swap(pointer_tag_pair& _lhs, pointer_tag_pair& _rhs) noexcept { std::swap(_lhs._pointer, _rhs._pointer); } + + // comparing {pointer(), tag()} <=> {pointer(), tag()} for consistency + friend constexpr auto operator<=>(pointer_tag_pair lhs, pointer_tag_pair rhs) noexcept { + const auto _ptr_comp = lhs.pointer() <=> rhs.pointer(); + if (!std::is_eq(_ptr_comp)) { + return _ptr_comp; + } + + return lhs.tag() <=> rhs.tag(); + } + + friend bool operator==(pointer_tag_pair, pointer_tag_pair) = default; +}; + +// support for structured bindings +template +struct tuple_size> : std::integral_constant {}; + +template +struct tuple_element<0, pointer_tag_pair<_Pointee, _Tag, _Bits>> { + using type = _Pointee*; +}; + +template +struct tuple_element<1, pointer_tag_pair<_Pointee, _Tag, _Bits>> { + using type = _Tag; +}; + +template +struct tuple_element<0, const pointer_tag_pair<_Pointee, _Tag, _Bits>> { + using type = _Pointee*; +}; + +template +struct tuple_element<1, const pointer_tag_pair<_Pointee, _Tag, _Bits>> { + using type = _Tag; +}; + +// helpers + +// std::get (with one overload as copying pointer_tag_pair is cheap) +template +constexpr auto get(pointer_tag_pair<_Pointee, _Tag, _Bits> _pair) noexcept + -> tuple_element<_I, pointer_tag_pair<_Pointee, _Tag, _Bits>>::type { + if constexpr (_I == 0) { + return _pair.pointer(); + } else { + static_assert(_I == 1); + return _pair.tag(); + } +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER >= 26 + +#endif // _LIBCPP___POINTER_TAG_PAIR_H \ No newline at end of file diff --git a/libcxx/include/memory b/libcxx/include/memory index b940a32c3ebe6..91fdbfb41b3df 100644 --- a/libcxx/include/memory +++ b/libcxx/include/memory @@ -969,6 +969,10 @@ template # include <__memory/allocate_at_least.h> #endif +#if _LIBCPP_STD_VER >= 26 +# include <__memory/pointer_tag_pair.h> +#endif + #include // [memory.syn]