|
| 1 | +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM |
| 2 | +// Exceptions. See /LICENSE for license information. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + |
| 5 | +#ifndef CARBON_COMMON_ENUM_MASK_BASE_H_ |
| 6 | +#define CARBON_COMMON_ENUM_MASK_BASE_H_ |
| 7 | + |
| 8 | +#include <bit> |
| 9 | + |
| 10 | +#include "common/enum_base.h" |
| 11 | +#include "llvm/ADT/StringExtras.h" |
| 12 | + |
| 13 | +namespace Carbon::Internal { |
| 14 | + |
| 15 | +// CRTP-style base class similar to `EnumBase`, but supporting mask enums. |
| 16 | +// |
| 17 | +// Users must be in the `Carbon` namespace and should look like the following. |
| 18 | +// |
| 19 | +// In `my_kind.h`: |
| 20 | +// ``` |
| 21 | +// #define CARBON_MY_KIND(X) \ |
| 22 | +// X(Enumerator1) \ |
| 23 | +// X(Enumerator2) \ |
| 24 | +// X(Enumerator3) \ |
| 25 | +// ... |
| 26 | +// |
| 27 | +// CARBON_DEFINE_RAW_ENUM_MASK(MyKind) { |
| 28 | +// CARBON_MY_KIND(CARBON_RAW_ENUM_MASK_ENUMERATOR) |
| 29 | +// }; |
| 30 | +// |
| 31 | +// class MyKind : public CARBON_ENUM_MASK_BASE(MyKind) { |
| 32 | +// public: |
| 33 | +// CARBON_MY_KIND(CARBON_ENUM_MASK_CONSTANT_DECL) |
| 34 | +// |
| 35 | +// // Plus, anything else you wish to include. |
| 36 | +// }; |
| 37 | +// |
| 38 | +// #define CARBON_MY_KIND_WITH_TYPE(X) \ |
| 39 | +// CARBON_ENUM_MASK_CONSTANT_DEFINITION(MyKind, X) |
| 40 | +// CARBON_MY_KIND(CARBON_MY_KIND_WITH_TYPE) |
| 41 | +// #undef CARBON_MY_KIND_WITH_TYPE |
| 42 | +// ``` |
| 43 | +// |
| 44 | +// In `my_kind.cpp`: |
| 45 | +// ``` |
| 46 | +// CARBON_DEFINE_ENUM_MASK_NAMES(MyKind) = { |
| 47 | +// CARBON_MY_KIND(CARBON_ENUM_MASK_NAME_STRING)}; |
| 48 | +// ``` |
| 49 | +template <typename DerivedT, typename EnumT, const llvm::StringLiteral Names[]> |
| 50 | +class EnumMaskBase : public EnumBase<DerivedT, EnumT, Names> { |
| 51 | + public: |
| 52 | + // Provide a standard `None`. |
| 53 | + // |
| 54 | + // This uses a `&` to trigger slightly different instantiation behaviors in |
| 55 | + // Clang. For context on why this is needed, see http://wg21.link/CWG2800. |
| 56 | + // NOLINTNEXTLINE(readability-identifier-naming) |
| 57 | + static const DerivedT& None; |
| 58 | + |
| 59 | + // Returns true if there's a non-empty set intersection. |
| 60 | + constexpr auto HasAnyOf(DerivedT other) const -> bool { |
| 61 | + return !(*this & other).empty(); |
| 62 | + } |
| 63 | + |
| 64 | + // Adds entries to the mask. |
| 65 | + auto Add(DerivedT other) -> void { *this = *this | other; } |
| 66 | + |
| 67 | + // Removes entries from the mask. |
| 68 | + auto Remove(DerivedT other) -> void { *this = *this & ~other; } |
| 69 | + |
| 70 | + constexpr auto operator|(DerivedT other) const -> DerivedT { |
| 71 | + return DerivedT::FromInt(this->AsInt() | other.AsInt()); |
| 72 | + } |
| 73 | + |
| 74 | + constexpr auto operator&(DerivedT other) const -> DerivedT { |
| 75 | + return DerivedT::FromInt(this->AsInt() & other.AsInt()); |
| 76 | + } |
| 77 | + |
| 78 | + constexpr auto operator~() const -> DerivedT { |
| 79 | + return DerivedT::FromInt(~this->AsInt()); |
| 80 | + } |
| 81 | + |
| 82 | + constexpr auto empty() const -> bool { return this->AsInt() == 0; } |
| 83 | + |
| 84 | + // Returns the name of this value. Requires it to be a single value, not |
| 85 | + // combined. |
| 86 | + // |
| 87 | + // This method will be automatically defined using the static `names` string |
| 88 | + // table in the base class, which is in turn will be populated for each |
| 89 | + // derived type using the macro helpers in this file. |
| 90 | + // |
| 91 | + // This shadows EnumBase::name. |
| 92 | + auto name() const -> llvm::StringRef { |
| 93 | + CARBON_CHECK(std::has_single_bit(this->AsInt()), "Not a single bit: {0}", |
| 94 | + this->AsInt()); |
| 95 | + return Names[std::bit_width(this->AsInt())]; |
| 96 | + } |
| 97 | + |
| 98 | + // Prints this value as a `|`-separated list of mask entries, or `None`. |
| 99 | + // |
| 100 | + // This shadows EnumBase::Print. |
| 101 | + auto Print(llvm::raw_ostream& out) const -> void { |
| 102 | + int value = this->AsInt(); |
| 103 | + if (value == 0) { |
| 104 | + out << "None"; |
| 105 | + return; |
| 106 | + } |
| 107 | + llvm::ListSeparator sep("|"); |
| 108 | + for (int bit = 0; value != 0; value >>= 1, ++bit) { |
| 109 | + if (value & 1) { |
| 110 | + out << sep << Names[bit]; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +template <typename DerivedT, typename EnumT, const llvm::StringLiteral Names[]> |
| 117 | +constexpr const DerivedT& EnumMaskBase<DerivedT, EnumT, Names>::None = |
| 118 | + DerivedT::FromInt(0); |
| 119 | + |
| 120 | +} // namespace Carbon::Internal |
| 121 | + |
| 122 | +// Use this before defining a class that derives from `EnumBase` to begin the |
| 123 | +// definition of the raw `enum class`. It should be followed by the body of that |
| 124 | +// raw enum class. |
| 125 | +#define CARBON_DEFINE_RAW_ENUM_MASK(EnumMaskName, UnderlyingType) \ |
| 126 | + namespace Internal { \ |
| 127 | + struct EnumMaskName##Data { \ |
| 128 | + static const llvm::StringLiteral Names[]; \ |
| 129 | + /* For bit shifts, track the initial counter value. This will increment on \ |
| 130 | + * each enum entry. */ \ |
| 131 | + static constexpr uint64_t BitShiftCounter = __COUNTER__ + 1; \ |
| 132 | + enum class RawEnum : UnderlyingType; \ |
| 133 | + }; \ |
| 134 | + } \ |
| 135 | + enum class Internal::EnumMaskName##Data::RawEnum : UnderlyingType |
| 136 | + |
| 137 | +// In the `CARBON_DEFINE_RAW_ENUM_MASK` block, use this to generate each |
| 138 | +// enumerator. |
| 139 | +#define CARBON_RAW_ENUM_MASK_ENUMERATOR(Name) \ |
| 140 | + Name = 1 << (__COUNTER__ - BitShiftCounter), |
| 141 | + |
| 142 | +// Use this to compute the `Internal::EnumMaskBase` specialization for a Carbon |
| 143 | +// enum mask. It both computes the name of the raw enum and ensures all the |
| 144 | +// namespaces are correct. |
| 145 | +#define CARBON_ENUM_MASK_BASE(EnumMaskName) \ |
| 146 | + ::Carbon::Internal::EnumMaskBase<EnumMaskName, \ |
| 147 | + Internal::EnumMaskName##Data::RawEnum, \ |
| 148 | + Internal::EnumMaskName##Data::Names> |
| 149 | + |
| 150 | +// Constants and names are declared equivalently as to `EnumBase`. |
| 151 | +#define CARBON_ENUM_MASK_CONSTANT_DECL(Name) CARBON_ENUM_CONSTANT_DECL(Name) |
| 152 | +#define CARBON_ENUM_MASK_CONSTANT_DEFINITION(EnumMaskName, Name) \ |
| 153 | + CARBON_ENUM_CONSTANT_DEFINITION(EnumMaskName, Name) |
| 154 | +#define CARBON_DEFINE_ENUM_MASK_NAMES(EnumMaskName) \ |
| 155 | + CARBON_DEFINE_ENUM_CLASS_NAMES(EnumMaskName) |
| 156 | +#define CARBON_ENUM_MASK_NAME_STRING(Name) CARBON_ENUM_CLASS_NAME_STRING(Name) |
| 157 | + |
| 158 | +#endif // CARBON_COMMON_ENUM_MASK_BASE_H_ |
0 commit comments