type_enum.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_TYPE_ENUM_H_
  5. #define CARBON_COMMON_TYPE_ENUM_H_
  6. #include <algorithm>
  7. #include "common/ostream.h"
  8. namespace Carbon {
  9. // An enum whose values are the specified types.
  10. template <typename... Types>
  11. class TypeEnum : public Printable<TypeEnum<Types...>> {
  12. public:
  13. using TypeTuple = std::tuple<Types...>;
  14. static constexpr size_t NumTypes = sizeof...(Types);
  15. static constexpr size_t NumValues = NumTypes + 2;
  16. static_assert(NumValues <= 256, "Too many types for raw enum.");
  17. // The underlying raw enumeration type.
  18. //
  19. // The enum_extensibility attribute indicates that this enum is intended to
  20. // take values that do not correspond to its declared enumerators.
  21. enum class [[clang::enum_extensibility(open)]] RawEnumType : uint8_t {
  22. // The first sizeof...(Types) values correspond to the types.
  23. // An explicitly invalid value.
  24. Invalid = NumTypes,
  25. // Indicates that no type should be used.
  26. // TODO: This doesn't really fit the model of this type, but it's convenient
  27. // for all of its users.
  28. None,
  29. };
  30. // Accesses the type given an enum value.
  31. template <RawEnumType K>
  32. requires(K != RawEnumType::Invalid)
  33. using TypeFor = __type_pack_element<static_cast<size_t>(K), Types...>;
  34. // Workaround for Clang bug https://github.com/llvm/llvm-project/issues/85461
  35. template <RawEnumType Value>
  36. static constexpr auto FromRaw = TypeEnum(Value);
  37. // Names for the `Invalid` and `None` enumeration values.
  38. static constexpr const TypeEnum& Invalid = FromRaw<RawEnumType::Invalid>;
  39. static constexpr const TypeEnum& None = FromRaw<RawEnumType::None>;
  40. // Accesses the enumeration value for the type `Type`. If `AllowInvalid` is
  41. // set, any unexpected type is mapped to `Invalid`, otherwise an invalid type
  42. // results in a compile error.
  43. //
  44. // The `Self` parameter is an implementation detail to allow `ForImpl` to be
  45. // defined after this template, and should not be specified.
  46. template <typename Type, bool AllowInvalid = false, typename Self = TypeEnum>
  47. static constexpr auto For = Self::template ForImpl<Type, AllowInvalid>();
  48. // This bool indicates whether the specified type corresponds to a value in
  49. // this enum.
  50. template <typename Type>
  51. static constexpr bool Contains = For<Type, true>.is_valid();
  52. // Explicitly convert from the raw enum type.
  53. explicit constexpr TypeEnum(RawEnumType value) : value_(value) {}
  54. // Implicitly convert to the raw enum type, for use in `switch`.
  55. //
  56. // NOLINTNEXTLINE(google-explicit-constructor)
  57. explicit(false) constexpr operator RawEnumType() const { return value_; }
  58. // Conversion to bool is deleted to prevent direct use in an `if` condition
  59. // instead of comparing with another value.
  60. explicit operator bool() const = delete;
  61. // Returns the raw enum value.
  62. constexpr auto ToRaw() const -> RawEnumType { return value_; }
  63. // Returns a value that can be used as an array index. Returned value will be
  64. // < NumValues.
  65. constexpr auto ToIndex() const -> size_t {
  66. return static_cast<size_t>(value_);
  67. }
  68. // Returns whether this is a valid value, not `Invalid`.
  69. constexpr auto is_valid() const -> bool {
  70. return value_ != RawEnumType::Invalid;
  71. }
  72. auto Print(llvm::raw_ostream& out) const -> void {
  73. out << "TypeEnum(";
  74. if (value_ == RawEnumType::None) {
  75. out << "None";
  76. } else {
  77. static constexpr std::array<llvm::StringLiteral, sizeof...(Types)> Names =
  78. {
  79. Types::Label...,
  80. };
  81. out << Names[static_cast<int>(value_)];
  82. }
  83. out << ")";
  84. }
  85. private:
  86. // Translates a type to its enum value, or `Invalid`.
  87. template <typename IdT, bool AllowInvalid>
  88. static constexpr auto ForImpl() -> TypeEnum {
  89. // A bool for each type saying whether it matches. The result is the index
  90. // of the first `true` in this list. If none matches, then the result is the
  91. // length of the list, which is mapped to `Invalid`.
  92. constexpr bool TypeMatches[] = {std::same_as<IdT, Types>...};
  93. constexpr int Index =
  94. std::find(TypeMatches, TypeMatches + NumTypes, true) - TypeMatches;
  95. static_assert(Index != NumTypes || AllowInvalid,
  96. "Unexpected type passed to TypeEnum::For<...>");
  97. return TypeEnum(static_cast<RawEnumType>(Index));
  98. }
  99. RawEnumType value_;
  100. };
  101. } // namespace Carbon
  102. #endif // CARBON_COMMON_TYPE_ENUM_H_