BitmaskEnum.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/ADT/BitmaskEnum.h ----------------------------------*- 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_BITMASKENUM_H
  14. #define LLVM_ADT_BITMASKENUM_H
  15. #include <cassert>
  16. #include <type_traits>
  17. #include <utility>
  18. #include "llvm/Support/MathExtras.h"
  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. /// LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() pulls the operator overloads used
  45. /// by LLVM_MARK_AS_BITMASK_ENUM into the current namespace.
  46. ///
  47. /// Suppose you have an enum foo::bar::MyEnum. Before using
  48. /// LLVM_MARK_AS_BITMASK_ENUM on MyEnum, you must put
  49. /// LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() somewhere inside namespace foo or
  50. /// namespace foo::bar. This allows the relevant operator overloads to be found
  51. /// by ADL.
  52. ///
  53. /// You don't need to use this macro in namespace llvm; it's done at the bottom
  54. /// of this file.
  55. #define LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() \
  56. using ::llvm::BitmaskEnumDetail::operator~; \
  57. using ::llvm::BitmaskEnumDetail::operator|; \
  58. using ::llvm::BitmaskEnumDetail::operator&; \
  59. using ::llvm::BitmaskEnumDetail::operator^; \
  60. using ::llvm::BitmaskEnumDetail::operator|=; \
  61. using ::llvm::BitmaskEnumDetail::operator&=; \
  62. /* Force a semicolon at the end of this macro. */ \
  63. using ::llvm::BitmaskEnumDetail::operator^=
  64. namespace llvm {
  65. /// Traits class to determine whether an enum has a
  66. /// LLVM_BITMASK_LARGEST_ENUMERATOR enumerator.
  67. template <typename E, typename Enable = void>
  68. struct is_bitmask_enum : std::false_type {};
  69. template <typename E>
  70. struct is_bitmask_enum<
  71. E, std::enable_if_t<sizeof(E::LLVM_BITMASK_LARGEST_ENUMERATOR) >= 0>>
  72. : std::true_type {};
  73. namespace BitmaskEnumDetail {
  74. /// Get a bitmask with 1s in all places up to the high-order bit of E's largest
  75. /// value.
  76. template <typename E> std::underlying_type_t<E> Mask() {
  77. // On overflow, NextPowerOf2 returns zero with the type uint64_t, so
  78. // subtracting 1 gives us the mask with all bits set, like we want.
  79. return NextPowerOf2(static_cast<std::underlying_type_t<E>>(
  80. E::LLVM_BITMASK_LARGEST_ENUMERATOR)) -
  81. 1;
  82. }
  83. /// Check that Val is in range for E, and return Val cast to E's underlying
  84. /// type.
  85. template <typename E> std::underlying_type_t<E> Underlying(E Val) {
  86. auto U = static_cast<std::underlying_type_t<E>>(Val);
  87. assert(U >= 0 && "Negative enum values are not allowed.");
  88. assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
  89. return U;
  90. }
  91. constexpr unsigned bitWidth(uint64_t Value) {
  92. return Value ? 1 + bitWidth(Value >> 1) : 0;
  93. }
  94. template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
  95. E operator~(E Val) {
  96. return static_cast<E>(~Underlying(Val) & Mask<E>());
  97. }
  98. template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
  99. E operator|(E LHS, E RHS) {
  100. return static_cast<E>(Underlying(LHS) | Underlying(RHS));
  101. }
  102. template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
  103. E operator&(E LHS, E RHS) {
  104. return static_cast<E>(Underlying(LHS) & Underlying(RHS));
  105. }
  106. template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
  107. E operator^(E LHS, E RHS) {
  108. return static_cast<E>(Underlying(LHS) ^ Underlying(RHS));
  109. }
  110. // |=, &=, and ^= return a reference to LHS, to match the behavior of the
  111. // operators on builtin types.
  112. template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
  113. E &operator|=(E &LHS, E RHS) {
  114. LHS = LHS | RHS;
  115. return LHS;
  116. }
  117. template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
  118. E &operator&=(E &LHS, E RHS) {
  119. LHS = LHS & RHS;
  120. return LHS;
  121. }
  122. template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
  123. E &operator^=(E &LHS, E RHS) {
  124. LHS = LHS ^ RHS;
  125. return LHS;
  126. }
  127. } // namespace BitmaskEnumDetail
  128. // Enable bitmask enums in namespace ::llvm and all nested namespaces.
  129. LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
  130. template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
  131. constexpr unsigned BitWidth = BitmaskEnumDetail::bitWidth(uint64_t{
  132. static_cast<std::underlying_type_t<E>>(
  133. E::LLVM_BITMASK_LARGEST_ENUMERATOR)});
  134. } // namespace llvm
  135. #endif
  136. #ifdef __GNUC__
  137. #pragma GCC diagnostic pop
  138. #endif