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