id_kind.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "toolchain/sem_ir/ids.h"
  8. namespace Carbon::SemIR {
  9. // An enum whose values are the specified types.
  10. template <typename... Types>
  11. class TypeEnum {
  12. public:
  13. static constexpr std::size_t NumTypes = sizeof...(Types);
  14. static constexpr std::size_t NumValues = NumTypes + 2;
  15. static_assert(NumValues <= 256, "Too many types for raw enum.");
  16. // TODO: Works around a clang-format bug:
  17. // https://github.com/llvm/llvm-project/issues/85476
  18. #define CARBON_OPEN_ENUM [[clang::enum_extensibility(open)]]
  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 CARBON_OPEN_ENUM 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. #undef CARBON_OPEN_ENUM
  33. // Accesses the type given an enum value.
  34. template <RawEnumType K>
  35. requires(K != RawEnumType::Invalid)
  36. using TypeFor = __type_pack_element<static_cast<size_t>(K), Types...>;
  37. // Workarond for Clang bug https://github.com/llvm/llvm-project/issues/85461
  38. template <RawEnumType Value>
  39. static constexpr auto FromRaw = TypeEnum(Value);
  40. // Names for the `Invalid` and `None` enumeration values.
  41. static constexpr const TypeEnum& Invalid = FromRaw<RawEnumType::Invalid>;
  42. static constexpr const TypeEnum& None = FromRaw<RawEnumType::None>;
  43. // Accesses the enumeration value for the type `IdT`. If `AllowInvalid` is
  44. // set, any unexpected type is mapped to `Invalid`, otherwise an invalid type
  45. // results in a compile error.
  46. //
  47. // The `Self` parameter is an implementation detail to allow `ForImpl` to be
  48. // defined after this template, and should not be specified.
  49. template <typename IdT, bool AllowInvalid = false, typename Self = TypeEnum>
  50. static constexpr auto For = Self::template ForImpl<IdT, AllowInvalid>();
  51. // This bool indicates whether the specified type corresponds to a value in
  52. // this enum.
  53. template <typename IdT>
  54. static constexpr bool Contains = For<IdT, true>.is_valid();
  55. // Explicitly convert from the raw enum type.
  56. explicit constexpr TypeEnum(RawEnumType value) : value_(value) {}
  57. // Implicitly convert to the raw enum type, for use in `switch`.
  58. //
  59. // NOLINTNEXTLINE(google-explicit-constructor)
  60. constexpr operator RawEnumType() const { return value_; }
  61. // Conversion to bool is deleted to prevent direct use in an `if` condition
  62. // instead of comparing with another value.
  63. explicit operator bool() const = delete;
  64. // Returns the raw enum value.
  65. constexpr auto ToRaw() const -> RawEnumType { return value_; }
  66. // Returns a value that can be used as an array index. Returned value will be
  67. // < NumValues.
  68. constexpr auto ToIndex() const -> std::size_t {
  69. return static_cast<std::size_t>(value_);
  70. }
  71. // Returns whether this is a valid value, not `Invalid`.
  72. constexpr auto is_valid() const -> bool {
  73. return value_ != RawEnumType::Invalid;
  74. }
  75. private:
  76. // Translates a type to its enum value, or `Invalid`.
  77. template <typename IdT, bool AllowInvalid>
  78. static constexpr auto ForImpl() -> TypeEnum {
  79. // A bool for each type saying whether it matches. The result is the index
  80. // of the first `true` in this list. If none matches, then the result is the
  81. // length of the list, which is mapped to `Invalid`.
  82. constexpr bool TypeMatches[] = {std::same_as<IdT, Types>...};
  83. constexpr int Index =
  84. std::find(TypeMatches, TypeMatches + NumTypes, true) - TypeMatches;
  85. static_assert(Index != NumTypes || AllowInvalid,
  86. "Unexpected type passed to TypeEnum::For<...>");
  87. return TypeEnum(static_cast<RawEnumType>(Index));
  88. }
  89. RawEnumType value_;
  90. };
  91. // An enum of all the ID types used as instruction operands.
  92. using IdKind = TypeEnum<
  93. // From sem_ir/builtin_inst_kind.h.
  94. BuiltinInstKind,
  95. // From base/value_store.h.
  96. IntId, RealId, FloatId, StringLiteralValueId,
  97. // From sem_ir/id.h.
  98. InstId, AbsoluteInstId, MatchingInstId, ConstantId, EntityNameId,
  99. CompileTimeBindIndex, RuntimeParamIndex, FunctionId, ClassId, InterfaceId,
  100. ImplId, GenericId, SpecificId, ImportIRId, ImportIRInstId, LocId, BoolValue,
  101. IntKind, NameId, NameScopeId, InstBlockId, TypeId, TypeBlockId,
  102. ElementIndex, LibraryNameId, FloatKind>;
  103. } // namespace Carbon::SemIR
  104. #endif // CARBON_TOOLCHAIN_SEM_IR_ID_KIND_H_