LLVM 20.0.0git
ADL.h
Go to the documentation of this file.
1//===- llvm/ADT/ADL.h - Argument dependent lookup utilities -----*- C++ -*-===//
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 LLVM_ADT_ADL_H
10#define LLVM_ADT_ADL_H
11
12#include <type_traits>
13#include <iterator>
14#include <utility>
15
16namespace llvm {
17
18// Only used by compiler if both template types are the same. Useful when
19// using SFINAE to test for the existence of member functions.
20template <typename T, T> struct SameType;
21
22namespace adl_detail {
23
24using std::begin;
25
26template <typename RangeT>
27constexpr auto begin_impl(RangeT &&range)
28 -> decltype(begin(std::forward<RangeT>(range))) {
29 return begin(std::forward<RangeT>(range));
30}
31
32using std::end;
33
34template <typename RangeT>
35constexpr auto end_impl(RangeT &&range)
36 -> decltype(end(std::forward<RangeT>(range))) {
37 return end(std::forward<RangeT>(range));
38}
39
40using std::rbegin;
41
42template <typename RangeT>
43constexpr auto rbegin_impl(RangeT &&range)
44 -> decltype(rbegin(std::forward<RangeT>(range))) {
45 return rbegin(std::forward<RangeT>(range));
46}
47
48using std::rend;
49
50template <typename RangeT>
51constexpr auto rend_impl(RangeT &&range)
52 -> decltype(rend(std::forward<RangeT>(range))) {
53 return rend(std::forward<RangeT>(range));
54}
55
56using std::swap;
57
58template <typename T>
59constexpr void swap_impl(T &&lhs,
60 T &&rhs) noexcept(noexcept(swap(std::declval<T>(),
61 std::declval<T>()))) {
62 swap(std::forward<T>(lhs), std::forward<T>(rhs));
63}
64
65using std::size;
66
67template <typename RangeT>
68constexpr auto size_impl(RangeT &&range)
69 -> decltype(size(std::forward<RangeT>(range))) {
70 return size(std::forward<RangeT>(range));
71}
72
73} // end namespace adl_detail
74
75/// Returns the begin iterator to \p range using `std::begin` and
76/// function found through Argument-Dependent Lookup (ADL).
77template <typename RangeT>
78constexpr auto adl_begin(RangeT &&range)
79 -> decltype(adl_detail::begin_impl(std::forward<RangeT>(range))) {
80 return adl_detail::begin_impl(std::forward<RangeT>(range));
81}
82
83/// Returns the end iterator to \p range using `std::end` and
84/// functions found through Argument-Dependent Lookup (ADL).
85template <typename RangeT>
86constexpr auto adl_end(RangeT &&range)
87 -> decltype(adl_detail::end_impl(std::forward<RangeT>(range))) {
88 return adl_detail::end_impl(std::forward<RangeT>(range));
89}
90
91/// Returns the reverse-begin iterator to \p range using `std::rbegin` and
92/// function found through Argument-Dependent Lookup (ADL).
93template <typename RangeT>
94constexpr auto adl_rbegin(RangeT &&range)
95 -> decltype(adl_detail::rbegin_impl(std::forward<RangeT>(range))) {
96 return adl_detail::rbegin_impl(std::forward<RangeT>(range));
97}
98
99/// Returns the reverse-end iterator to \p range using `std::rend` and
100/// functions found through Argument-Dependent Lookup (ADL).
101template <typename RangeT>
102constexpr auto adl_rend(RangeT &&range)
103 -> decltype(adl_detail::rend_impl(std::forward<RangeT>(range))) {
104 return adl_detail::rend_impl(std::forward<RangeT>(range));
105}
106
107/// Swaps \p lhs with \p rhs using `std::swap` and functions found through
108/// Argument-Dependent Lookup (ADL).
109template <typename T>
110constexpr void adl_swap(T &&lhs, T &&rhs) noexcept(
111 noexcept(adl_detail::swap_impl(std::declval<T>(), std::declval<T>()))) {
112 adl_detail::swap_impl(std::forward<T>(lhs), std::forward<T>(rhs));
113}
114
115/// Returns the size of \p range using `std::size` and functions found through
116/// Argument-Dependent Lookup (ADL).
117template <typename RangeT>
118constexpr auto adl_size(RangeT &&range)
119 -> decltype(adl_detail::size_impl(std::forward<RangeT>(range))) {
120 return adl_detail::size_impl(std::forward<RangeT>(range));
121}
122
123namespace detail {
124
125template <typename RangeT>
126using IterOfRange = decltype(adl_begin(std::declval<RangeT &>()));
127
128template <typename RangeT>
130 std::remove_reference_t<decltype(*adl_begin(std::declval<RangeT &>()))>;
131
132} // namespace detail
133} // namespace llvm
134
135#endif // LLVM_ADT_ADL_H
constexpr auto size_impl(RangeT &&range) -> decltype(size(std::forward< RangeT >(range)))
Definition: ADL.h:68
constexpr auto end_impl(RangeT &&range) -> decltype(end(std::forward< RangeT >(range)))
Definition: ADL.h:35
constexpr void swap_impl(T &&lhs, T &&rhs) noexcept(noexcept(swap(std::declval< T >(), std::declval< T >())))
Definition: ADL.h:59
constexpr auto rend_impl(RangeT &&range) -> decltype(rend(std::forward< RangeT >(range)))
Definition: ADL.h:51
constexpr auto rbegin_impl(RangeT &&range) -> decltype(rbegin(std::forward< RangeT >(range)))
Definition: ADL.h:43
constexpr auto begin_impl(RangeT &&range) -> decltype(begin(std::forward< RangeT >(range)))
Definition: ADL.h:27
decltype(adl_begin(std::declval< RangeT & >())) IterOfRange
Definition: ADL.h:126
std::remove_reference_t< decltype(*adl_begin(std::declval< RangeT & >()))> ValueOfRange
Definition: ADL.h:130
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1697
constexpr auto adl_begin(RangeT &&range) -> decltype(adl_detail::begin_impl(std::forward< RangeT >(range)))
Returns the begin iterator to range using std::begin and function found through Argument-Dependent Lo...
Definition: ADL.h:78
constexpr auto adl_end(RangeT &&range) -> decltype(adl_detail::end_impl(std::forward< RangeT >(range)))
Returns the end iterator to range using std::end and functions found through Argument-Dependent Looku...
Definition: ADL.h:86
constexpr auto adl_rbegin(RangeT &&range) -> decltype(adl_detail::rbegin_impl(std::forward< RangeT >(range)))
Returns the reverse-begin iterator to range using std::rbegin and function found through Argument-Dep...
Definition: ADL.h:94
constexpr auto adl_size(RangeT &&range) -> decltype(adl_detail::size_impl(std::forward< RangeT >(range)))
Returns the size of range using std::size and functions found through Argument-Dependent Lookup (ADL)...
Definition: ADL.h:118
constexpr auto adl_rend(RangeT &&range) -> decltype(adl_detail::rend_impl(std::forward< RangeT >(range)))
Returns the reverse-end iterator to range using std::rend and functions found through Argument-Depend...
Definition: ADL.h:102
constexpr void adl_swap(T &&lhs, T &&rhs) noexcept(noexcept(adl_detail::swap_impl(std::declval< T >(), std::declval< T >())))
Swaps lhs with rhs using std::swap and functions found through Argument-Dependent Lookup (ADL).
Definition: ADL.h:110
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860