LLVM 22.0.0git
STLForwardCompat.h
Go to the documentation of this file.
1//===- STLForwardCompat.h - Library features from future STLs ------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/// \file
10/// This file contains library features backported from future STL versions.
11///
12/// These should be replaced with their STL counterparts as the C++ version LLVM
13/// is compiled with is updated.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_ADT_STLFORWARDCOMPAT_H
18#define LLVM_ADT_STLFORWARDCOMPAT_H
19
20#include <optional>
21#include <type_traits>
22
23namespace llvm {
24
25//===----------------------------------------------------------------------===//
26// Features from C++20
27//===----------------------------------------------------------------------===//
28
29template <typename T>
30struct remove_cvref // NOLINT(readability-identifier-naming)
31{
32 using type = std::remove_cv_t<std::remove_reference_t<T>>;
33};
34
35template <typename T>
36using remove_cvref_t // NOLINT(readability-identifier-naming)
38
39// TODO: Remove this in favor of std::type_identity<T> once we switch to C++20.
40template <typename T>
41struct type_identity // NOLINT(readability-identifier-naming)
42{
43 using type = T;
44};
45
46// TODO: Remove this in favor of std::type_identity_t<T> once we switch to
47// C++20.
48template <typename T>
49using type_identity_t // NOLINT(readability-identifier-naming)
51
52namespace detail {
53template <class, template <class...> class Op, class... Args> struct detector {
54 using value_t = std::false_type;
55};
56template <template <class...> class Op, class... Args>
57struct detector<std::void_t<Op<Args...>>, Op, Args...> {
58 using value_t = std::true_type;
59};
60} // end namespace detail
61
62/// Detects if a given trait holds for some set of arguments 'Args'.
63/// For example, the given trait could be used to detect if a given type
64/// has a copy assignment operator:
65/// template<class T>
66/// using has_copy_assign_t = decltype(std::declval<T&>()
67/// = std::declval<const T&>());
68/// bool fooHasCopyAssign = is_detected<has_copy_assign_t, FooClass>::value;
69template <template <class...> class Op, class... Args>
70using is_detected = typename detail::detector<void, Op, Args...>::value_t;
71
72//===----------------------------------------------------------------------===//
73// Features from C++23
74//===----------------------------------------------------------------------===//
75
76// TODO: Remove this in favor of std::optional<T>::transform once we switch to
77// C++23.
78template <typename Optional, typename Function,
80std::optional<std::invoke_result_t<Function, Value>>
81transformOptional(Optional &&O, Function &&F) {
82 if (O) {
83 return F(*std::forward<Optional>(O));
84 }
85 return std::nullopt;
86}
87
88/// Returns underlying integer value of an enum. Backport of C++23
89/// std::to_underlying.
90template <typename Enum>
91[[nodiscard]] constexpr std::underlying_type_t<Enum> to_underlying(Enum E) {
92 return static_cast<std::underlying_type_t<Enum>>(E);
93}
94
95// A tag for constructors accepting ranges.
97 explicit from_range_t() = default;
98};
99inline constexpr from_range_t from_range{};
100} // namespace llvm
101
102#endif // LLVM_ADT_STLFORWARDCOMPAT_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define F(x, y, z)
Definition MD5.cpp:55
#define T
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
constexpr from_range_t from_range
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
DWARFExpression::Operation Op
typename detail::detector< void, Op, Args... >::value_t is_detected
Detects if a given trait holds for some set of arguments 'Args'.
std::optional< std::invoke_result_t< Function, Value > > transformOptional(Optional &&O, Function &&F)
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
from_range_t()=default
std::remove_cv_t< std::remove_reference_t< T > > type