id_kind.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_TOOLCHAIN_SEM_IR_ID_KIND_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_ID_KIND_H_
  6. #include <algorithm>
  7. #include "common/ostream.h"
  8. #include "toolchain/base/int.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::SemIR {
  11. // An enum whose values are the specified types.
  12. template <typename... Types>
  13. class TypeEnum : public Printable<TypeEnum<Types...>> {
  14. public:
  15. using TypeTuple = std::tuple<Types...>;
  16. static constexpr size_t NumTypes = sizeof...(Types);
  17. static constexpr size_t NumValues = NumTypes + 2;
  18. static_assert(NumValues <= 256, "Too many types for raw enum.");
  19. // The underlying raw enumeration type.
  20. //
  21. // The enum_extensibility attribute indicates that this enum is intended to
  22. // take values that do not correspond to its declared enumerators.
  23. enum class [[clang::enum_extensibility(open)]] RawEnumType : uint8_t {
  24. // The first sizeof...(Types) values correspond to the types.
  25. // An explicitly invalid value.
  26. Invalid = NumTypes,
  27. // Indicates that no type should be used.
  28. // TODO: This doesn't really fit the model of this type, but it's convenient
  29. // for all of its users.
  30. None,
  31. };
  32. // Accesses the type given an enum value.
  33. template <RawEnumType K>
  34. requires(K != RawEnumType::Invalid)
  35. using TypeFor = __type_pack_element<static_cast<size_t>(K), Types...>;
  36. // Workaround for Clang bug https://github.com/llvm/llvm-project/issues/85461
  37. template <RawEnumType Value>
  38. static constexpr auto FromRaw = TypeEnum(Value);
  39. // Names for the `Invalid` and `None` enumeration values.
  40. static constexpr const TypeEnum& Invalid = FromRaw<RawEnumType::Invalid>;
  41. static constexpr const TypeEnum& None = FromRaw<RawEnumType::None>;
  42. // Accesses the enumeration value for the type `IdT`. If `AllowInvalid` is
  43. // set, any unexpected type is mapped to `Invalid`, otherwise an invalid type
  44. // results in a compile error.
  45. //
  46. // The `Self` parameter is an implementation detail to allow `ForImpl` to be
  47. // defined after this template, and should not be specified.
  48. template <typename IdT, bool AllowInvalid = false, typename Self = TypeEnum>
  49. static constexpr auto For = Self::template ForImpl<IdT, AllowInvalid>();
  50. // This bool indicates whether the specified type corresponds to a value in
  51. // this enum.
  52. template <typename IdT>
  53. static constexpr bool Contains = For<IdT, true>.is_valid();
  54. // Explicitly convert from the raw enum type.
  55. explicit constexpr TypeEnum(RawEnumType value) : value_(value) {}
  56. // Implicitly convert to the raw enum type, for use in `switch`.
  57. //
  58. // NOLINTNEXTLINE(google-explicit-constructor)
  59. constexpr operator RawEnumType() const { return value_; }
  60. // Conversion to bool is deleted to prevent direct use in an `if` condition
  61. // instead of comparing with another value.
  62. explicit operator bool() const = delete;
  63. // Returns the raw enum value.
  64. constexpr auto ToRaw() const -> RawEnumType { return value_; }
  65. // Returns a value that can be used as an array index. Returned value will be
  66. // < NumValues.
  67. constexpr auto ToIndex() const -> size_t {
  68. return static_cast<size_t>(value_);
  69. }
  70. // Returns whether this is a valid value, not `Invalid`.
  71. constexpr auto is_valid() const -> bool {
  72. return value_ != RawEnumType::Invalid;
  73. }
  74. auto Print(llvm::raw_ostream& out) const -> void {
  75. out << "IdKind(";
  76. if (value_ == RawEnumType::None) {
  77. out << "None";
  78. } else {
  79. static constexpr std::array<llvm::StringLiteral, sizeof...(Types)> Names =
  80. {
  81. Types::Label...,
  82. };
  83. out << Names[static_cast<int>(value_)];
  84. }
  85. out << ")";
  86. }
  87. private:
  88. // Translates a type to its enum value, or `Invalid`.
  89. template <typename IdT, bool AllowInvalid>
  90. static constexpr auto ForImpl() -> TypeEnum {
  91. // A bool for each type saying whether it matches. The result is the index
  92. // of the first `true` in this list. If none matches, then the result is the
  93. // length of the list, which is mapped to `Invalid`.
  94. constexpr bool TypeMatches[] = {std::same_as<IdT, Types>...};
  95. constexpr int Index =
  96. std::find(TypeMatches, TypeMatches + NumTypes, true) - TypeMatches;
  97. static_assert(Index != NumTypes || AllowInvalid,
  98. "Unexpected type passed to TypeEnum::For<...>");
  99. return TypeEnum(static_cast<RawEnumType>(Index));
  100. }
  101. RawEnumType value_;
  102. };
  103. // An enum of all the ID types used as instruction operands.
  104. //
  105. // As instruction operands, the types listed here can appear as fields of typed
  106. // instructions (`toolchain/sem_ir/typed_insts.h`) and must implement the
  107. // `FromRaw` and `ToRaw` protocol in `Inst`. In most cases this is done by
  108. // inheriting from `IdBase` or `IndexBase`.
  109. //
  110. // clang-format off: We want one per line.
  111. using IdKind = TypeEnum<
  112. // From base/value_store.h.
  113. FloatId,
  114. IntId,
  115. RealId,
  116. StringLiteralValueId,
  117. // From sem_ir/ids.h.
  118. AbsoluteInstBlockId,
  119. AbsoluteInstId,
  120. AnyRawId,
  121. AssociatedConstantId,
  122. BoolValue,
  123. CallParamIndex,
  124. ClassId,
  125. CompileTimeBindIndex,
  126. ConstantId,
  127. DeclInstBlockId,
  128. DestInstId,
  129. ElementIndex,
  130. EntityNameId,
  131. ExprRegionId,
  132. FacetTypeId,
  133. FloatKind,
  134. FunctionId,
  135. GenericId,
  136. ImplId,
  137. ImportIRId,
  138. ImportIRInstId,
  139. InstBlockId,
  140. InstId,
  141. InterfaceId,
  142. IntKind,
  143. LabelId,
  144. LibraryNameId,
  145. LocId,
  146. MetaInstId,
  147. NameId,
  148. NameScopeId,
  149. SpecificId,
  150. SpecificInterfaceId,
  151. StructTypeFieldsId,
  152. TypeInstId>;
  153. // clang-format on
  154. } // namespace Carbon::SemIR
  155. #endif // CARBON_TOOLCHAIN_SEM_IR_ID_KIND_H_