enum_base.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #ifndef CARBON_COMMON_ENUM_BASE_H_
  5. #define CARBON_COMMON_ENUM_BASE_H_
  6. #include <type_traits>
  7. #include "common/ostream.h"
  8. #include "llvm/ADT/StringRef.h"
  9. namespace Carbon::Internal {
  10. // CRTP-style base class used to define the common pattern of Carbon enum-like
  11. // classes. The result is a class with named constants similar to enumerators,
  12. // but that are normal classes, can contain other methods, and support a `name`
  13. // method and printing the enums. These even work in switch statements and
  14. // support `case MyEnum::Name:`.
  15. //
  16. // It is specifically designed to compose with X-MACRO style `.def` files that
  17. // stamp out all the enumerators.
  18. //
  19. // It also supports some opt-in APIs that classes can enable by `using` the
  20. // names to make them public: `AsInt` and `FromInt` allow converting to and from
  21. // the underlying type of the enumerator.
  22. //
  23. // Users must be in the `Carbon` namespace and should look like the following.
  24. //
  25. // In `my_kind.h`:
  26. // ```
  27. // CARBON_DEFINE_RAW_ENUM_CLASS(MyKind, uint8_t) {
  28. // #define CARBON_MY_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  29. // #include ".../my_kind.def"
  30. // };
  31. //
  32. // class MyKind : public CARBON_ENUM_BASE(MyKind) {
  33. // public:
  34. // #define CARBON_MY_KIND(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name)
  35. // #include ".../my_kind.def"
  36. // };
  37. //
  38. // #define CARBON_MY_KIND(Name) CARBON_ENUM_CONSTANT_DEFINITION(MyKind, Name)
  39. // #include ".../my_kind.def"
  40. // ```
  41. //
  42. // In `my_kind.cpp`:
  43. // ```
  44. // CARBON_DEFINE_ENUM_CLASS_NAMES(MyKind) = {
  45. // #define CARBON_MY_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
  46. // #include ".../my_kind.def"
  47. // };
  48. // ```
  49. template <typename DerivedT, typename EnumT>
  50. class EnumBase {
  51. protected:
  52. // An alias for the raw enum type. This is an implementation detail and
  53. // shouldn't be used, but we need it for a signature so it is declared early.
  54. using RawEnumType = EnumT;
  55. public:
  56. using EnumType = DerivedT;
  57. using UnderlyingType = std::underlying_type_t<RawEnumType>;
  58. // Enable conversion to the raw enum type, including in a `constexpr` context,
  59. // to enable comparisons and usage in `switch` and `case`. The enum type
  60. // remains an implementation detail and nothing else should be using this
  61. // function.
  62. //
  63. // NOLINTNEXTLINE(google-explicit-constructor)
  64. constexpr operator RawEnumType() const { return value_; }
  65. // Conversion to bool is deleted to prevent direct use in an `if` condition
  66. // instead of comparing with another value.
  67. explicit operator bool() const = delete;
  68. // Returns the name of this value.
  69. //
  70. // This method will be automatically defined using the static `names` string
  71. // table in the base class, which is in turn will be populated for each
  72. // derived type using the macro helpers in this file.
  73. [[nodiscard]] auto name() const -> llvm::StringRef;
  74. // Prints this value using its name.
  75. void Print(llvm::raw_ostream& out) const {
  76. out << static_cast<const EnumType*>(this)->name();
  77. }
  78. protected:
  79. // The default constructor is explicitly defaulted (and constexpr) as a
  80. // protected constructor to allow derived classes to be constructed but not
  81. // the base itself. This should only be used in the `Create` function below.
  82. constexpr EnumBase() = default;
  83. // Create an instance from the raw enumerator, for internal use.
  84. static constexpr auto Create(RawEnumType value) -> EnumType {
  85. EnumType result;
  86. result.value_ = value;
  87. return result;
  88. }
  89. // Convert to the underlying integer type. Derived types can choose to expose
  90. // this as part of their API.
  91. constexpr auto AsInt() const -> UnderlyingType {
  92. return static_cast<UnderlyingType>(value_);
  93. }
  94. // Convert from the underlying integer type. Derived types can choose to
  95. // expose this as part of their API.
  96. static constexpr auto FromInt(UnderlyingType value) -> EnumType {
  97. return Create(static_cast<RawEnumType>(value));
  98. }
  99. private:
  100. static llvm::StringLiteral names[];
  101. RawEnumType value_;
  102. };
  103. } // namespace Carbon::Internal
  104. // Use this before defining a class that derives from `EnumBase` to begin the
  105. // definition of the raw `enum class`. It should be followed by the body of that
  106. // raw enum class.
  107. #define CARBON_DEFINE_RAW_ENUM_CLASS(EnumClassName, UnderlyingType) \
  108. namespace Internal { \
  109. /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
  110. enum class EnumClassName##RawEnum : UnderlyingType; \
  111. } \
  112. enum class ::Carbon::Internal::EnumClassName##RawEnum : UnderlyingType
  113. // In CARBON_DEFINE_RAW_ENUM_CLASS block, use this to generate each enumerator.
  114. #define CARBON_RAW_ENUM_ENUMERATOR(Name) Name,
  115. // Use this to compute the `Internal::EnumBase` specialization for a Carbon enum
  116. // class. It both computes the name of the raw enum and ensures all the
  117. // namespaces are correct.
  118. #define CARBON_ENUM_BASE(EnumClassName) \
  119. CARBON_ENUM_BASE_CRTP(EnumClassName, EnumClassName)
  120. // This variant handles the case where the external name for the Carbon enum is
  121. // not the same as the name by which we refer to it from this context.
  122. #define CARBON_ENUM_BASE_CRTP(EnumClassName, LocalTypeNameForEnumClass) \
  123. ::Carbon::Internal::EnumBase<LocalTypeNameForEnumClass, \
  124. ::Carbon::Internal::EnumClassName##RawEnum>
  125. // Use this within the Carbon enum class body to generate named constant
  126. // declarations for each value.
  127. #define CARBON_ENUM_CONSTANT_DECLARATION(Name) static const EnumType Name;
  128. // Use this immediately after the Carbon enum class body to define each named
  129. // constant.
  130. #define CARBON_ENUM_CONSTANT_DEFINITION(EnumClassName, Name) \
  131. constexpr EnumClassName EnumClassName::Name = \
  132. EnumClassName::Create(RawEnumType::Name);
  133. // Alternatively, use this within the Carbon enum class body to declare and
  134. // define each named constant. Due to type completeness constraints, this will
  135. // only work if the enum-like class is templated.
  136. //
  137. // This requires the template to have a member named `Base` that names the
  138. // `EnumBase` base class.
  139. #define CARBON_INLINE_ENUM_CONSTANT_DEFINITION(Name) \
  140. static constexpr const typename Base::EnumType& Name = \
  141. Base::Create(Base::RawEnumType::Name);
  142. // Use this to define a custom `name()` function for an enum-like class. Usage:
  143. //
  144. // CARBON_ENUM_NAME_FUNCTION(MyEnum) {
  145. // // Return a StringRef based on the value of *this.
  146. // }
  147. //
  148. // You should usually use CARBON_DEFINE_ENUM_CLASS_NAMES instead.
  149. #define CARBON_ENUM_NAME_FUNCTION(EnumClassName) \
  150. template <> \
  151. auto \
  152. Internal::EnumBase<EnumClassName, Internal::EnumClassName##RawEnum>::name() \
  153. const->llvm::StringRef
  154. // Use this in the `.cpp` file for an enum class to start the definition of the
  155. // constant names array for each enumerator. It is followed by the desired
  156. // constant initializer.
  157. //
  158. // `clang-format` has a bug with spacing around `->` returns in macros. See
  159. // https://bugs.llvm.org/show_bug.cgi?id=48320 for details.
  160. #define CARBON_DEFINE_ENUM_CLASS_NAMES(EnumClassName) \
  161. /* First declare an explicit specialization of the names array so we can \
  162. * reference it from an explicit function specialization. */ \
  163. template <> \
  164. llvm::StringLiteral Internal::EnumBase< \
  165. EnumClassName, Internal::EnumClassName##RawEnum>::names[]; \
  166. \
  167. /* Now define an explicit function specialization for the `name` method, as \
  168. * it can now reference our specialized array. */ \
  169. CARBON_ENUM_NAME_FUNCTION(EnumClassName) { \
  170. return names[static_cast<int>(value_)]; \
  171. } \
  172. \
  173. /* Finally, open up the definition of our specialized array for the user to \
  174. * populate using the x-macro include. */ \
  175. template <> \
  176. llvm::StringLiteral Internal::EnumBase< \
  177. EnumClassName, Internal::EnumClassName##RawEnum>::names[]
  178. // Use this within the names array initializer to generate a string for each
  179. // name.
  180. #define CARBON_ENUM_CLASS_NAME_STRING(Name) #Name,
  181. #endif // CARBON_COMMON_ENUM_BASE_H_