LLVM 22.0.0git
BitmaskEnum.h
Go to the documentation of this file.
1//===-- llvm/ADT/BitmaskEnum.h ----------------------------------*- 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_BITMASKENUM_H
10#define LLVM_ADT_BITMASKENUM_H
11
12#include <cassert>
13#include <type_traits>
14#include <utility>
15
17#include "llvm/ADT/bit.h"
19
20/// LLVM_MARK_AS_BITMASK_ENUM lets you opt in an individual enum type so you can
21/// perform bitwise operations on it without putting static_cast everywhere.
22///
23/// \code
24/// enum MyEnum {
25/// E1 = 1, E2 = 2, E3 = 4, E4 = 8,
26/// LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ E4)
27/// };
28///
29/// void Foo() {
30/// MyEnum A = (E1 | E2) & E3 ^ ~E4; // Look, ma: No static_cast!
31/// }
32/// \endcode
33///
34/// Normally when you do a bitwise operation on an enum value, you get back an
35/// instance of the underlying type (e.g. int). But using this macro, bitwise
36/// ops on your enum will return you back instances of the enum. This is
37/// particularly useful for enums which represent a combination of flags.
38///
39/// The parameter to LLVM_MARK_AS_BITMASK_ENUM should be the largest individual
40/// value in your enum.
41///
42/// All of the enum's values must be non-negative.
43#define LLVM_MARK_AS_BITMASK_ENUM(LargestValue) \
44 LLVM_BITMASK_LARGEST_ENUMERATOR = LargestValue
45
46/// LLVM_DECLARE_ENUM_AS_BITMASK can be used to declare an enum type as a bit
47/// set, so that bitwise operation on such enum does not require static_cast.
48///
49/// \code
50/// enum MyEnum { E1 = 1, E2 = 2, E3 = 4, E4 = 8 };
51/// LLVM_DECLARE_ENUM_AS_BITMASK(MyEnum, E4);
52///
53/// void Foo() {
54/// MyEnum A = (E1 | E2) & E3 ^ ~E4; // No static_cast
55/// }
56/// \endcode
57///
58/// The second parameter to LLVM_DECLARE_ENUM_AS_BITMASK specifies the largest
59/// bit value of the enum type.
60///
61/// LLVM_DECLARE_ENUM_AS_BITMASK should be used in llvm namespace.
62///
63/// This a non-intrusive alternative for LLVM_MARK_AS_BITMASK_ENUM. It allows
64/// declaring more than one non-scoped enumerations as bitmask types in the same
65/// scope. Otherwise it provides the same functionality as
66/// LLVM_MARK_AS_BITMASK_ENUM.
67#define LLVM_DECLARE_ENUM_AS_BITMASK(Enum, LargestValue) \
68 template <> struct is_bitmask_enum<Enum> : std::true_type {}; \
69 template <> struct largest_bitmask_enum_bit<Enum> { \
70 static constexpr std::underlying_type_t<Enum> value = LargestValue; \
71 }
72
73/// LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() pulls the operator overloads used
74/// by LLVM_MARK_AS_BITMASK_ENUM into the current namespace.
75///
76/// Suppose you have an enum foo::bar::MyEnum. Before using
77/// LLVM_MARK_AS_BITMASK_ENUM on MyEnum, you must put
78/// LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() somewhere inside namespace foo or
79/// namespace foo::bar. This allows the relevant operator overloads to be found
80/// by ADL.
81///
82/// You don't need to use this macro in namespace llvm; it's done at the bottom
83/// of this file.
84#define LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() \
85 using ::llvm::BitmaskEnumDetail::operator~; \
86 using ::llvm::BitmaskEnumDetail::operator|; \
87 using ::llvm::BitmaskEnumDetail::operator&; \
88 using ::llvm::BitmaskEnumDetail::operator^; \
89 using ::llvm::BitmaskEnumDetail::operator<<; \
90 using ::llvm::BitmaskEnumDetail::operator>>; \
91 using ::llvm::BitmaskEnumDetail::operator|=; \
92 using ::llvm::BitmaskEnumDetail::operator&=; \
93 using ::llvm::BitmaskEnumDetail::operator^=; \
94 using ::llvm::BitmaskEnumDetail::operator<<=; \
95 using ::llvm::BitmaskEnumDetail::operator>>=; \
96 using ::llvm::BitmaskEnumDetail::operator!; \
97 /* Force a semicolon at the end of this macro. */ \
98 using ::llvm::BitmaskEnumDetail::any
99
100namespace llvm {
101
102/// Traits class to determine whether an enum has a
103/// LLVM_BITMASK_LARGEST_ENUMERATOR enumerator.
104template <typename E, typename Enable = void>
105struct is_bitmask_enum : std::false_type {};
106
107template <typename E>
109 E, std::enable_if_t<sizeof(E::LLVM_BITMASK_LARGEST_ENUMERATOR) >= 0>>
110 : std::true_type {};
111
112/// Trait class to determine bitmask enumeration largest bit.
113template <typename E, typename Enable = void> struct largest_bitmask_enum_bit;
114
115template <typename E>
117 E, std::enable_if_t<sizeof(E::LLVM_BITMASK_LARGEST_ENUMERATOR) >= 0>> {
118 using UnderlyingTy = std::underlying_type_t<E>;
119 static constexpr UnderlyingTy value =
120 static_cast<UnderlyingTy>(E::LLVM_BITMASK_LARGEST_ENUMERATOR);
121};
122
124
125/// Get a bitmask with 1s in all places up to the high-order bit of E's largest
126/// value.
127template <typename E> constexpr std::underlying_type_t<E> Mask() {
128 // On overflow, NextPowerOf2 returns zero with the type uint64_t, so
129 // subtracting 1 gives us the mask with all bits set, like we want.
131}
132
133/// Check that Val is in range for E, and return Val cast to E's underlying
134/// type.
135template <typename E> constexpr std::underlying_type_t<E> Underlying(E Val) {
136 auto U = llvm::to_underlying(Val);
137 assert(U >= 0 && "Negative enum values are not allowed.");
138 assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
139 return U;
140}
141
142template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
143constexpr bool operator!(E Val) {
144 return Val == static_cast<E>(0);
145}
146
147template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
148constexpr bool any(E Val) {
149 return Val != static_cast<E>(0);
150}
151
152template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
153constexpr E operator~(E Val) {
154 return static_cast<E>(~Underlying(Val) & Mask<E>());
155}
156
157template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
158constexpr E operator|(E LHS, E RHS) {
159 return static_cast<E>(Underlying(LHS) | Underlying(RHS));
160}
161
162template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
163constexpr E operator&(E LHS, E RHS) {
164 return static_cast<E>(Underlying(LHS) & Underlying(RHS));
165}
166
167template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
168constexpr E operator^(E LHS, E RHS) {
169 return static_cast<E>(Underlying(LHS) ^ Underlying(RHS));
170}
171
172template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
173constexpr E operator<<(E LHS, E RHS) {
174 return static_cast<E>(Underlying(LHS) << Underlying(RHS));
175}
176
177template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
178constexpr E operator>>(E LHS, E RHS) {
179 return static_cast<E>(Underlying(LHS) >> Underlying(RHS));
180}
181
182// |=, &=, and ^= return a reference to LHS, to match the behavior of the
183// operators on builtin types.
184
185template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
187 LHS = LHS | RHS;
188 return LHS;
189}
190
191template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
193 LHS = LHS & RHS;
194 return LHS;
195}
196
197template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
199 LHS = LHS ^ RHS;
200 return LHS;
201}
202
203template <typename e, typename = std::enable_if_t<is_bitmask_enum<e>::value>>
204e &operator<<=(e &lhs, e rhs) {
205 lhs = lhs << rhs;
206 return lhs;
207}
208
209template <typename e, typename = std::enable_if_t<is_bitmask_enum<e>::value>>
210e &operator>>=(e &lhs, e rhs) {
211 lhs = lhs >> rhs;
212 return lhs;
213}
214
215} // namespace BitmaskEnumDetail
216
217// Enable bitmask enums in namespace ::llvm and all nested namespaces.
219template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
221 uint64_t{llvm::to_underlying(E::LLVM_BITMASK_LARGEST_ENUMERATOR)});
222
223} // namespace llvm
224
225#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains library features backported from future STL versions.
Value * RHS
Value * LHS
This file implements the C++20 <bit> header.
e & operator>>=(e &lhs, e rhs)
constexpr E operator>>(E LHS, E RHS)
constexpr E operator^(E LHS, E RHS)
constexpr bool any(E Val)
constexpr E operator~(E Val)
constexpr std::underlying_type_t< E > Underlying(E Val)
Check that Val is in range for E, and return Val cast to E's underlying type.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
constexpr bool operator!(E Val)
E & operator^=(E &LHS, E RHS)
E & operator&=(E &LHS, E RHS)
constexpr E operator&(E LHS, E RHS)
e & operator<<=(e &lhs, e rhs)
constexpr E operator<<(E LHS, E RHS)
E & operator|=(E &LHS, E RHS)
constexpr E operator|(E LHS, E RHS)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE()
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
constexpr unsigned BitWidth
constexpr int bit_width_constexpr(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:301
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:384
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
Traits class to determine whether an enum has a LLVM_BITMASK_LARGEST_ENUMERATOR enumerator.
Trait class to determine bitmask enumeration largest bit.