From 0556a2aa187b86c28a9441aec3e98b9780a2c9ee Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Wed, 25 Jun 2025 19:33:34 +0200 Subject: [PATCH] [ArrayRef] Provide constructors from any type with a data() member (#145735) The assumption here is that if data() returns a pointer and size() exists it's something representing contiguous storage. Modeled after the behavior of C++20 std::span and enables two-way conversion between std::span (or really any span-like type) and ArrayRef. Add a unit test that verifies this if std::span is around. This also means we can get rid of specific conversions for std::vector and SmallVector. --- llvm/include/llvm/ADT/ArrayRef.h | 51 ++++++++--------------------- llvm/unittests/ADT/ArrayRefTest.cpp | 19 ++++++++++- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h index 4819c8847134..ddd2c7ce68c8 100644 --- a/llvm/include/llvm/ADT/ArrayRef.h +++ b/llvm/include/llvm/ADT/ArrayRef.h @@ -84,18 +84,19 @@ namespace llvm { assert(begin <= end); } - /// Construct an ArrayRef from a SmallVector. This is templated in order to - /// avoid instantiating SmallVectorTemplateCommon whenever we - /// copy-construct an ArrayRef. - template - /*implicit*/ ArrayRef(const SmallVectorTemplateCommon &Vec) - : Data(Vec.data()), Length(Vec.size()) { - } - - /// Construct an ArrayRef from a std::vector. - template - /*implicit*/ ArrayRef(const std::vector &Vec) - : Data(Vec.data()), Length(Vec.size()) {} + /// Construct an ArrayRef from a type that has a data() method that returns + /// a pointer convertible to const T *. + template < + typename C, + typename = std::enable_if_t< + std::conjunction_v< + std::is_convertible< + decltype(std::declval().data()) *, + const T *const *>, + std::is_integral().size())>>, + void>> + /*implicit*/ constexpr ArrayRef(const C &V) + : Data(V.data()), Length(V.size()) {} /// Construct an ArrayRef from a std::array template @@ -123,32 +124,6 @@ namespace llvm { #pragma GCC diagnostic pop #endif - /// Construct an ArrayRef from ArrayRef. This uses SFINAE to - /// ensure that only ArrayRefs of pointers can be converted. - template - ArrayRef(const ArrayRef &A, - std::enable_if_t::value> - * = nullptr) - : Data(A.data()), Length(A.size()) {} - - /// Construct an ArrayRef from a SmallVector. This is - /// templated in order to avoid instantiating SmallVectorTemplateCommon - /// whenever we copy-construct an ArrayRef. - template - /*implicit*/ ArrayRef( - const SmallVectorTemplateCommon &Vec, - std::enable_if_t::value> * = - nullptr) - : Data(Vec.data()), Length(Vec.size()) {} - - /// Construct an ArrayRef from std::vector. This uses SFINAE - /// to ensure that only vectors of pointers can be converted. - template - ArrayRef(const std::vector &Vec, - std::enable_if_t::value> - * = nullptr) - : Data(Vec.data()), Length(Vec.size()) {} - /// Construct an ArrayRef from iterator_range. This uses SFINAE /// to ensure that this is only used for iterator ranges over plain pointer /// iterators. diff --git a/llvm/unittests/ADT/ArrayRefTest.cpp b/llvm/unittests/ADT/ArrayRefTest.cpp index 39a4a9b6a178..3858d9064f9c 100644 --- a/llvm/unittests/ADT/ArrayRefTest.cpp +++ b/llvm/unittests/ADT/ArrayRefTest.cpp @@ -8,10 +8,16 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/Support/Allocator.h" -#include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" #include #include +#if __has_include() +#include +#endif +#ifdef __cpp_lib_span +#include +#endif + using namespace llvm; // Check that the ArrayRef-of-pointer converting constructor only allows adding @@ -406,4 +412,15 @@ TEST(ArrayRefTest, MutableArrayRefDeductionGuides) { } } +#ifdef __cpp_lib_span +static_assert(std::is_constructible_v, std::span>, + "should be able to construct ArrayRef from const std::span"); +static_assert(std::is_constructible_v, ArrayRef>, + "should be able to construct const std::span from ArrayRef"); +static_assert(std::is_constructible_v, std::span>, + "should be able to construct ArrayRef from mutable std::span"); +static_assert(!std::is_constructible_v, ArrayRef>, + "cannot construct mutable std::span from ArrayRef"); +#endif + } // end anonymous namespace