ArrayRef.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ArrayRef.h - Array Reference Wrapper ---------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_ARRAYREF_H
  14. #define LLVM_ADT_ARRAYREF_H
  15. #include "llvm/ADT/Hashing.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/Support/Compiler.h"
  19. #include <algorithm>
  20. #include <array>
  21. #include <cassert>
  22. #include <cstddef>
  23. #include <initializer_list>
  24. #include <iterator>
  25. #include <memory>
  26. #include <type_traits>
  27. #include <vector>
  28. namespace llvm {
  29. template<typename T> class [[nodiscard]] MutableArrayRef;
  30. /// ArrayRef - Represent a constant reference to an array (0 or more elements
  31. /// consecutively in memory), i.e. a start pointer and a length. It allows
  32. /// various APIs to take consecutive elements easily and conveniently.
  33. ///
  34. /// This class does not own the underlying data, it is expected to be used in
  35. /// situations where the data resides in some other buffer, whose lifetime
  36. /// extends past that of the ArrayRef. For this reason, it is not in general
  37. /// safe to store an ArrayRef.
  38. ///
  39. /// This is intended to be trivially copyable, so it should be passed by
  40. /// value.
  41. template<typename T>
  42. class LLVM_GSL_POINTER [[nodiscard]] ArrayRef {
  43. public:
  44. using value_type = T;
  45. using pointer = value_type *;
  46. using const_pointer = const value_type *;
  47. using reference = value_type &;
  48. using const_reference = const value_type &;
  49. using iterator = const_pointer;
  50. using const_iterator = const_pointer;
  51. using reverse_iterator = std::reverse_iterator<iterator>;
  52. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  53. using size_type = size_t;
  54. using difference_type = ptrdiff_t;
  55. private:
  56. /// The start of the array, in an external buffer.
  57. const T *Data = nullptr;
  58. /// The number of elements.
  59. size_type Length = 0;
  60. public:
  61. /// @name Constructors
  62. /// @{
  63. /// Construct an empty ArrayRef.
  64. /*implicit*/ ArrayRef() = default;
  65. /// Construct an empty ArrayRef from std::nullopt.
  66. /*implicit*/ ArrayRef(std::nullopt_t) {}
  67. /// Construct an ArrayRef from a single element.
  68. /*implicit*/ ArrayRef(const T &OneElt)
  69. : Data(&OneElt), Length(1) {}
  70. /// Construct an ArrayRef from a pointer and length.
  71. constexpr /*implicit*/ ArrayRef(const T *data, size_t length)
  72. : Data(data), Length(length) {}
  73. /// Construct an ArrayRef from a range.
  74. constexpr ArrayRef(const T *begin, const T *end)
  75. : Data(begin), Length(end - begin) {}
  76. /// Construct an ArrayRef from a SmallVector. This is templated in order to
  77. /// avoid instantiating SmallVectorTemplateCommon<T> whenever we
  78. /// copy-construct an ArrayRef.
  79. template<typename U>
  80. /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
  81. : Data(Vec.data()), Length(Vec.size()) {
  82. }
  83. /// Construct an ArrayRef from a std::vector.
  84. template<typename A>
  85. /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
  86. : Data(Vec.data()), Length(Vec.size()) {}
  87. /// Construct an ArrayRef from a std::array
  88. template <size_t N>
  89. /*implicit*/ constexpr ArrayRef(const std::array<T, N> &Arr)
  90. : Data(Arr.data()), Length(N) {}
  91. /// Construct an ArrayRef from a C array.
  92. template <size_t N>
  93. /*implicit*/ constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {}
  94. /// Construct an ArrayRef from a std::initializer_list.
  95. #if LLVM_GNUC_PREREQ(9, 0, 0)
  96. // Disable gcc's warning in this constructor as it generates an enormous amount
  97. // of messages. Anyone using ArrayRef should already be aware of the fact that
  98. // it does not do lifetime extension.
  99. #pragma GCC diagnostic push
  100. #pragma GCC diagnostic ignored "-Winit-list-lifetime"
  101. #endif
  102. constexpr /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
  103. : Data(Vec.begin() == Vec.end() ? (T *)nullptr : Vec.begin()),
  104. Length(Vec.size()) {}
  105. #if LLVM_GNUC_PREREQ(9, 0, 0)
  106. #pragma GCC diagnostic pop
  107. #endif
  108. /// Construct an ArrayRef<const T*> from ArrayRef<T*>. This uses SFINAE to
  109. /// ensure that only ArrayRefs of pointers can be converted.
  110. template <typename U>
  111. ArrayRef(const ArrayRef<U *> &A,
  112. std::enable_if_t<std::is_convertible<U *const *, T const *>::value>
  113. * = nullptr)
  114. : Data(A.data()), Length(A.size()) {}
  115. /// Construct an ArrayRef<const T*> from a SmallVector<T*>. This is
  116. /// templated in order to avoid instantiating SmallVectorTemplateCommon<T>
  117. /// whenever we copy-construct an ArrayRef.
  118. template <typename U, typename DummyT>
  119. /*implicit*/ ArrayRef(
  120. const SmallVectorTemplateCommon<U *, DummyT> &Vec,
  121. std::enable_if_t<std::is_convertible<U *const *, T const *>::value> * =
  122. nullptr)
  123. : Data(Vec.data()), Length(Vec.size()) {}
  124. /// Construct an ArrayRef<const T*> from std::vector<T*>. This uses SFINAE
  125. /// to ensure that only vectors of pointers can be converted.
  126. template <typename U, typename A>
  127. ArrayRef(const std::vector<U *, A> &Vec,
  128. std::enable_if_t<std::is_convertible<U *const *, T const *>::value>
  129. * = nullptr)
  130. : Data(Vec.data()), Length(Vec.size()) {}
  131. /// @}
  132. /// @name Simple Operations
  133. /// @{
  134. iterator begin() const { return Data; }
  135. iterator end() const { return Data + Length; }
  136. reverse_iterator rbegin() const { return reverse_iterator(end()); }
  137. reverse_iterator rend() const { return reverse_iterator(begin()); }
  138. /// empty - Check if the array is empty.
  139. bool empty() const { return Length == 0; }
  140. const T *data() const { return Data; }
  141. /// size - Get the array size.
  142. size_t size() const { return Length; }
  143. /// front - Get the first element.
  144. const T &front() const {
  145. assert(!empty());
  146. return Data[0];
  147. }
  148. /// back - Get the last element.
  149. const T &back() const {
  150. assert(!empty());
  151. return Data[Length-1];
  152. }
  153. // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
  154. template <typename Allocator> MutableArrayRef<T> copy(Allocator &A) {
  155. T *Buff = A.template Allocate<T>(Length);
  156. std::uninitialized_copy(begin(), end(), Buff);
  157. return MutableArrayRef<T>(Buff, Length);
  158. }
  159. /// equals - Check for element-wise equality.
  160. bool equals(ArrayRef RHS) const {
  161. if (Length != RHS.Length)
  162. return false;
  163. return std::equal(begin(), end(), RHS.begin());
  164. }
  165. /// slice(n, m) - Chop off the first N elements of the array, and keep M
  166. /// elements in the array.
  167. ArrayRef<T> slice(size_t N, size_t M) const {
  168. assert(N+M <= size() && "Invalid specifier");
  169. return ArrayRef<T>(data()+N, M);
  170. }
  171. /// slice(n) - Chop off the first N elements of the array.
  172. ArrayRef<T> slice(size_t N) const { return slice(N, size() - N); }
  173. /// Drop the first \p N elements of the array.
  174. ArrayRef<T> drop_front(size_t N = 1) const {
  175. assert(size() >= N && "Dropping more elements than exist");
  176. return slice(N, size() - N);
  177. }
  178. /// Drop the last \p N elements of the array.
  179. ArrayRef<T> drop_back(size_t N = 1) const {
  180. assert(size() >= N && "Dropping more elements than exist");
  181. return slice(0, size() - N);
  182. }
  183. /// Return a copy of *this with the first N elements satisfying the
  184. /// given predicate removed.
  185. template <class PredicateT> ArrayRef<T> drop_while(PredicateT Pred) const {
  186. return ArrayRef<T>(find_if_not(*this, Pred), end());
  187. }
  188. /// Return a copy of *this with the first N elements not satisfying
  189. /// the given predicate removed.
  190. template <class PredicateT> ArrayRef<T> drop_until(PredicateT Pred) const {
  191. return ArrayRef<T>(find_if(*this, Pred), end());
  192. }
  193. /// Return a copy of *this with only the first \p N elements.
  194. ArrayRef<T> take_front(size_t N = 1) const {
  195. if (N >= size())
  196. return *this;
  197. return drop_back(size() - N);
  198. }
  199. /// Return a copy of *this with only the last \p N elements.
  200. ArrayRef<T> take_back(size_t N = 1) const {
  201. if (N >= size())
  202. return *this;
  203. return drop_front(size() - N);
  204. }
  205. /// Return the first N elements of this Array that satisfy the given
  206. /// predicate.
  207. template <class PredicateT> ArrayRef<T> take_while(PredicateT Pred) const {
  208. return ArrayRef<T>(begin(), find_if_not(*this, Pred));
  209. }
  210. /// Return the first N elements of this Array that don't satisfy the
  211. /// given predicate.
  212. template <class PredicateT> ArrayRef<T> take_until(PredicateT Pred) const {
  213. return ArrayRef<T>(begin(), find_if(*this, Pred));
  214. }
  215. /// @}
  216. /// @name Operator Overloads
  217. /// @{
  218. const T &operator[](size_t Index) const {
  219. assert(Index < Length && "Invalid index!");
  220. return Data[Index];
  221. }
  222. /// Disallow accidental assignment from a temporary.
  223. ///
  224. /// The declaration here is extra complicated so that "arrayRef = {}"
  225. /// continues to select the move assignment operator.
  226. template <typename U>
  227. std::enable_if_t<std::is_same<U, T>::value, ArrayRef<T>> &
  228. operator=(U &&Temporary) = delete;
  229. /// Disallow accidental assignment from a temporary.
  230. ///
  231. /// The declaration here is extra complicated so that "arrayRef = {}"
  232. /// continues to select the move assignment operator.
  233. template <typename U>
  234. std::enable_if_t<std::is_same<U, T>::value, ArrayRef<T>> &
  235. operator=(std::initializer_list<U>) = delete;
  236. /// @}
  237. /// @name Expensive Operations
  238. /// @{
  239. std::vector<T> vec() const {
  240. return std::vector<T>(Data, Data+Length);
  241. }
  242. /// @}
  243. /// @name Conversion operators
  244. /// @{
  245. operator std::vector<T>() const {
  246. return std::vector<T>(Data, Data+Length);
  247. }
  248. /// @}
  249. };
  250. /// MutableArrayRef - Represent a mutable reference to an array (0 or more
  251. /// elements consecutively in memory), i.e. a start pointer and a length. It
  252. /// allows various APIs to take and modify consecutive elements easily and
  253. /// conveniently.
  254. ///
  255. /// This class does not own the underlying data, it is expected to be used in
  256. /// situations where the data resides in some other buffer, whose lifetime
  257. /// extends past that of the MutableArrayRef. For this reason, it is not in
  258. /// general safe to store a MutableArrayRef.
  259. ///
  260. /// This is intended to be trivially copyable, so it should be passed by
  261. /// value.
  262. template<typename T>
  263. class [[nodiscard]] MutableArrayRef : public ArrayRef<T> {
  264. public:
  265. using value_type = T;
  266. using pointer = value_type *;
  267. using const_pointer = const value_type *;
  268. using reference = value_type &;
  269. using const_reference = const value_type &;
  270. using iterator = pointer;
  271. using const_iterator = const_pointer;
  272. using reverse_iterator = std::reverse_iterator<iterator>;
  273. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  274. using size_type = size_t;
  275. using difference_type = ptrdiff_t;
  276. /// Construct an empty MutableArrayRef.
  277. /*implicit*/ MutableArrayRef() = default;
  278. /// Construct an empty MutableArrayRef from std::nullopt.
  279. /*implicit*/ MutableArrayRef(std::nullopt_t) : ArrayRef<T>() {}
  280. /// Construct a MutableArrayRef from a single element.
  281. /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
  282. /// Construct a MutableArrayRef from a pointer and length.
  283. /*implicit*/ MutableArrayRef(T *data, size_t length)
  284. : ArrayRef<T>(data, length) {}
  285. /// Construct a MutableArrayRef from a range.
  286. MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
  287. /// Construct a MutableArrayRef from a SmallVector.
  288. /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
  289. : ArrayRef<T>(Vec) {}
  290. /// Construct a MutableArrayRef from a std::vector.
  291. /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
  292. : ArrayRef<T>(Vec) {}
  293. /// Construct a MutableArrayRef from a std::array
  294. template <size_t N>
  295. /*implicit*/ constexpr MutableArrayRef(std::array<T, N> &Arr)
  296. : ArrayRef<T>(Arr) {}
  297. /// Construct a MutableArrayRef from a C array.
  298. template <size_t N>
  299. /*implicit*/ constexpr MutableArrayRef(T (&Arr)[N]) : ArrayRef<T>(Arr) {}
  300. T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
  301. iterator begin() const { return data(); }
  302. iterator end() const { return data() + this->size(); }
  303. reverse_iterator rbegin() const { return reverse_iterator(end()); }
  304. reverse_iterator rend() const { return reverse_iterator(begin()); }
  305. /// front - Get the first element.
  306. T &front() const {
  307. assert(!this->empty());
  308. return data()[0];
  309. }
  310. /// back - Get the last element.
  311. T &back() const {
  312. assert(!this->empty());
  313. return data()[this->size()-1];
  314. }
  315. /// slice(n, m) - Chop off the first N elements of the array, and keep M
  316. /// elements in the array.
  317. MutableArrayRef<T> slice(size_t N, size_t M) const {
  318. assert(N + M <= this->size() && "Invalid specifier");
  319. return MutableArrayRef<T>(this->data() + N, M);
  320. }
  321. /// slice(n) - Chop off the first N elements of the array.
  322. MutableArrayRef<T> slice(size_t N) const {
  323. return slice(N, this->size() - N);
  324. }
  325. /// Drop the first \p N elements of the array.
  326. MutableArrayRef<T> drop_front(size_t N = 1) const {
  327. assert(this->size() >= N && "Dropping more elements than exist");
  328. return slice(N, this->size() - N);
  329. }
  330. MutableArrayRef<T> drop_back(size_t N = 1) const {
  331. assert(this->size() >= N && "Dropping more elements than exist");
  332. return slice(0, this->size() - N);
  333. }
  334. /// Return a copy of *this with the first N elements satisfying the
  335. /// given predicate removed.
  336. template <class PredicateT>
  337. MutableArrayRef<T> drop_while(PredicateT Pred) const {
  338. return MutableArrayRef<T>(find_if_not(*this, Pred), end());
  339. }
  340. /// Return a copy of *this with the first N elements not satisfying
  341. /// the given predicate removed.
  342. template <class PredicateT>
  343. MutableArrayRef<T> drop_until(PredicateT Pred) const {
  344. return MutableArrayRef<T>(find_if(*this, Pred), end());
  345. }
  346. /// Return a copy of *this with only the first \p N elements.
  347. MutableArrayRef<T> take_front(size_t N = 1) const {
  348. if (N >= this->size())
  349. return *this;
  350. return drop_back(this->size() - N);
  351. }
  352. /// Return a copy of *this with only the last \p N elements.
  353. MutableArrayRef<T> take_back(size_t N = 1) const {
  354. if (N >= this->size())
  355. return *this;
  356. return drop_front(this->size() - N);
  357. }
  358. /// Return the first N elements of this Array that satisfy the given
  359. /// predicate.
  360. template <class PredicateT>
  361. MutableArrayRef<T> take_while(PredicateT Pred) const {
  362. return MutableArrayRef<T>(begin(), find_if_not(*this, Pred));
  363. }
  364. /// Return the first N elements of this Array that don't satisfy the
  365. /// given predicate.
  366. template <class PredicateT>
  367. MutableArrayRef<T> take_until(PredicateT Pred) const {
  368. return MutableArrayRef<T>(begin(), find_if(*this, Pred));
  369. }
  370. /// @}
  371. /// @name Operator Overloads
  372. /// @{
  373. T &operator[](size_t Index) const {
  374. assert(Index < this->size() && "Invalid index!");
  375. return data()[Index];
  376. }
  377. };
  378. /// This is a MutableArrayRef that owns its array.
  379. template <typename T> class OwningArrayRef : public MutableArrayRef<T> {
  380. public:
  381. OwningArrayRef() = default;
  382. OwningArrayRef(size_t Size) : MutableArrayRef<T>(new T[Size], Size) {}
  383. OwningArrayRef(ArrayRef<T> Data)
  384. : MutableArrayRef<T>(new T[Data.size()], Data.size()) {
  385. std::copy(Data.begin(), Data.end(), this->begin());
  386. }
  387. OwningArrayRef(OwningArrayRef &&Other) { *this = std::move(Other); }
  388. OwningArrayRef &operator=(OwningArrayRef &&Other) {
  389. delete[] this->data();
  390. this->MutableArrayRef<T>::operator=(Other);
  391. Other.MutableArrayRef<T>::operator=(MutableArrayRef<T>());
  392. return *this;
  393. }
  394. ~OwningArrayRef() { delete[] this->data(); }
  395. };
  396. /// @name ArrayRef Deduction guides
  397. /// @{
  398. /// Deduction guide to construct an ArrayRef from a single element.
  399. template <typename T> ArrayRef(const T &OneElt) -> ArrayRef<T>;
  400. /// Deduction guide to construct an ArrayRef from a pointer and length
  401. template <typename T> ArrayRef(const T *data, size_t length) -> ArrayRef<T>;
  402. /// Deduction guide to construct an ArrayRef from a range
  403. template <typename T> ArrayRef(const T *data, const T *end) -> ArrayRef<T>;
  404. /// Deduction guide to construct an ArrayRef from a SmallVector
  405. template <typename T> ArrayRef(const SmallVectorImpl<T> &Vec) -> ArrayRef<T>;
  406. /// Deduction guide to construct an ArrayRef from a SmallVector
  407. template <typename T, unsigned N>
  408. ArrayRef(const SmallVector<T, N> &Vec) -> ArrayRef<T>;
  409. /// Deduction guide to construct an ArrayRef from a std::vector
  410. template <typename T> ArrayRef(const std::vector<T> &Vec) -> ArrayRef<T>;
  411. /// Deduction guide to construct an ArrayRef from a std::array
  412. template <typename T, std::size_t N>
  413. ArrayRef(const std::array<T, N> &Vec) -> ArrayRef<T>;
  414. /// Deduction guide to construct an ArrayRef from an ArrayRef (const)
  415. template <typename T> ArrayRef(const ArrayRef<T> &Vec) -> ArrayRef<T>;
  416. /// Deduction guide to construct an ArrayRef from an ArrayRef
  417. template <typename T> ArrayRef(ArrayRef<T> &Vec) -> ArrayRef<T>;
  418. /// Deduction guide to construct an ArrayRef from a C array.
  419. template <typename T, size_t N> ArrayRef(const T (&Arr)[N]) -> ArrayRef<T>;
  420. /// @}
  421. /// @name ArrayRef Convenience constructors
  422. /// @{
  423. /// Construct an ArrayRef from a single element.
  424. template <typename T>
  425. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  426. ArrayRef<T> makeArrayRef(const T &OneElt) {
  427. return OneElt;
  428. }
  429. /// Construct an ArrayRef from a pointer and length.
  430. template <typename T>
  431. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  432. ArrayRef<T> makeArrayRef(const T *data, size_t length) {
  433. return ArrayRef<T>(data, length);
  434. }
  435. /// Construct an ArrayRef from a range.
  436. template <typename T>
  437. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  438. ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
  439. return ArrayRef<T>(begin, end);
  440. }
  441. /// Construct an ArrayRef from a SmallVector.
  442. template <typename T>
  443. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  444. ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
  445. return Vec;
  446. }
  447. /// Construct an ArrayRef from a SmallVector.
  448. template <typename T, unsigned N>
  449. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  450. ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
  451. return Vec;
  452. }
  453. /// Construct an ArrayRef from a std::vector.
  454. template <typename T>
  455. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  456. ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
  457. return Vec;
  458. }
  459. /// Construct an ArrayRef from a std::array.
  460. template <typename T, std::size_t N>
  461. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  462. ArrayRef<T> makeArrayRef(const std::array<T, N> &Arr) {
  463. return Arr;
  464. }
  465. /// Construct an ArrayRef from an ArrayRef (no-op) (const)
  466. template <typename T>
  467. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  468. ArrayRef<T> makeArrayRef(const ArrayRef<T> &Vec) {
  469. return Vec;
  470. }
  471. /// Construct an ArrayRef from an ArrayRef (no-op)
  472. template <typename T>
  473. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  474. ArrayRef<T> &makeArrayRef(ArrayRef<T> &Vec) {
  475. return Vec;
  476. }
  477. /// Construct an ArrayRef from a C array.
  478. template <typename T, size_t N>
  479. LLVM_DEPRECATED("Use deduction guide instead", "ArrayRef")
  480. ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
  481. return ArrayRef<T>(Arr);
  482. }
  483. /// @name MutableArrayRef Deduction guides
  484. /// @{
  485. /// Deduction guide to construct a `MutableArrayRef` from a single element
  486. template <class T> MutableArrayRef(T &OneElt) -> MutableArrayRef<T>;
  487. /// Deduction guide to construct a `MutableArrayRef` from a pointer and
  488. /// length.
  489. template <class T>
  490. MutableArrayRef(T *data, size_t length) -> MutableArrayRef<T>;
  491. /// Deduction guide to construct a `MutableArrayRef` from a `SmallVector`.
  492. template <class T>
  493. MutableArrayRef(SmallVectorImpl<T> &Vec) -> MutableArrayRef<T>;
  494. template <class T, unsigned N>
  495. MutableArrayRef(SmallVector<T, N> &Vec) -> MutableArrayRef<T>;
  496. /// Deduction guide to construct a `MutableArrayRef` from a `std::vector`.
  497. template <class T> MutableArrayRef(std::vector<T> &Vec) -> MutableArrayRef<T>;
  498. /// Deduction guide to construct a `MutableArrayRef` from a `std::array`.
  499. template <class T, std::size_t N>
  500. MutableArrayRef(std::array<T, N> &Vec) -> MutableArrayRef<T>;
  501. /// Deduction guide to construct a `MutableArrayRef` from a C array.
  502. template <typename T, size_t N>
  503. MutableArrayRef(T (&Arr)[N]) -> MutableArrayRef<T>;
  504. /// @}
  505. /// Construct a MutableArrayRef from a single element.
  506. template <typename T>
  507. LLVM_DEPRECATED("Use deduction guide instead", "MutableArrayRef")
  508. MutableArrayRef<T> makeMutableArrayRef(T &OneElt) {
  509. return OneElt;
  510. }
  511. /// Construct a MutableArrayRef from a pointer and length.
  512. template <typename T>
  513. LLVM_DEPRECATED("Use deduction guide instead", "MutableArrayRef")
  514. MutableArrayRef<T> makeMutableArrayRef(T *data, size_t length) {
  515. return MutableArrayRef<T>(data, length);
  516. }
  517. /// Construct a MutableArrayRef from a SmallVector.
  518. template <typename T>
  519. LLVM_DEPRECATED("Use deduction guide instead", "MutableArrayRef")
  520. MutableArrayRef<T> makeMutableArrayRef(SmallVectorImpl<T> &Vec) {
  521. return Vec;
  522. }
  523. /// Construct a MutableArrayRef from a SmallVector.
  524. template <typename T, unsigned N>
  525. LLVM_DEPRECATED("Use deduction guide instead", "MutableArrayRef")
  526. MutableArrayRef<T> makeMutableArrayRef(SmallVector<T, N> &Vec) {
  527. return Vec;
  528. }
  529. /// Construct a MutableArrayRef from a std::vector.
  530. template <typename T>
  531. LLVM_DEPRECATED("Use deduction guide instead", "MutableArrayRef")
  532. MutableArrayRef<T> makeMutableArrayRef(std::vector<T> &Vec) {
  533. return Vec;
  534. }
  535. /// Construct a MutableArrayRef from a std::array.
  536. template <typename T, std::size_t N>
  537. LLVM_DEPRECATED("Use deduction guide instead", "MutableArrayRef")
  538. MutableArrayRef<T> makeMutableArrayRef(std::array<T, N> &Arr) {
  539. return Arr;
  540. }
  541. /// Construct a MutableArrayRef from a MutableArrayRef (no-op) (const)
  542. template <typename T>
  543. LLVM_DEPRECATED("Use deduction guide instead", "MutableArrayRef")
  544. MutableArrayRef<T> makeMutableArrayRef(const MutableArrayRef<T> &Vec) {
  545. return Vec;
  546. }
  547. /// Construct a MutableArrayRef from a C array.
  548. template <typename T, size_t N>
  549. LLVM_DEPRECATED("Use deduction guide instead", "MutableArrayRef")
  550. MutableArrayRef<T> makeMutableArrayRef(T (&Arr)[N]) {
  551. return MutableArrayRef<T>(Arr);
  552. }
  553. /// @}
  554. /// @name ArrayRef Comparison Operators
  555. /// @{
  556. template<typename T>
  557. inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
  558. return LHS.equals(RHS);
  559. }
  560. template <typename T>
  561. inline bool operator==(SmallVectorImpl<T> &LHS, ArrayRef<T> RHS) {
  562. return ArrayRef<T>(LHS).equals(RHS);
  563. }
  564. template <typename T>
  565. inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
  566. return !(LHS == RHS);
  567. }
  568. template <typename T>
  569. inline bool operator!=(SmallVectorImpl<T> &LHS, ArrayRef<T> RHS) {
  570. return !(LHS == RHS);
  571. }
  572. /// @}
  573. template <typename T> hash_code hash_value(ArrayRef<T> S) {
  574. return hash_combine_range(S.begin(), S.end());
  575. }
  576. // Provide DenseMapInfo for ArrayRefs.
  577. template <typename T> struct DenseMapInfo<ArrayRef<T>, void> {
  578. static inline ArrayRef<T> getEmptyKey() {
  579. return ArrayRef<T>(
  580. reinterpret_cast<const T *>(~static_cast<uintptr_t>(0)), size_t(0));
  581. }
  582. static inline ArrayRef<T> getTombstoneKey() {
  583. return ArrayRef<T>(
  584. reinterpret_cast<const T *>(~static_cast<uintptr_t>(1)), size_t(0));
  585. }
  586. static unsigned getHashValue(ArrayRef<T> Val) {
  587. assert(Val.data() != getEmptyKey().data() &&
  588. "Cannot hash the empty key!");
  589. assert(Val.data() != getTombstoneKey().data() &&
  590. "Cannot hash the tombstone key!");
  591. return (unsigned)(hash_value(Val));
  592. }
  593. static bool isEqual(ArrayRef<T> LHS, ArrayRef<T> RHS) {
  594. if (RHS.data() == getEmptyKey().data())
  595. return LHS.data() == getEmptyKey().data();
  596. if (RHS.data() == getTombstoneKey().data())
  597. return LHS.data() == getTombstoneKey().data();
  598. return LHS == RHS;
  599. }
  600. };
  601. } // end namespace llvm
  602. #endif // LLVM_ADT_ARRAYREF_H
  603. #ifdef __GNUC__
  604. #pragma GCC diagnostic pop
  605. #endif