builtin_inst_kind.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_BUILTIN_INST_KIND_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_BUILTIN_INST_KIND_H_
  6. #include <cstdint>
  7. #include "common/enum_base.h"
  8. namespace Carbon::SemIR {
  9. CARBON_DEFINE_RAW_ENUM_CLASS(BuiltinInstKind, uint8_t) {
  10. #define CARBON_SEM_IR_BUILTIN_INST_KIND_NAME(Name) \
  11. CARBON_RAW_ENUM_ENUMERATOR(Name)
  12. #include "toolchain/sem_ir/builtin_inst_kind.def"
  13. };
  14. class BuiltinInstKind : public CARBON_ENUM_BASE(BuiltinInstKind) {
  15. public:
  16. #define CARBON_SEM_IR_BUILTIN_INST_KIND_NAME(Name) \
  17. CARBON_ENUM_CONSTANT_DECL(Name)
  18. #include "toolchain/sem_ir/builtin_inst_kind.def"
  19. auto label() -> llvm::StringRef;
  20. // The count of enum values excluding Invalid.
  21. //
  22. // Note that we *define* this as `constexpr` making it a true compile-time
  23. // constant.
  24. static const uint8_t ValidCount;
  25. // Support conversion to and from an int32_t for SemIR instruction storage.
  26. using EnumBase::AsInt;
  27. using EnumBase::FromInt;
  28. };
  29. #define CARBON_SEM_IR_BUILTIN_INST_KIND_NAME(Name) \
  30. CARBON_ENUM_CONSTANT_DEFINITION(BuiltinInstKind, Name)
  31. #include "toolchain/sem_ir/builtin_inst_kind.def"
  32. constexpr uint8_t BuiltinInstKind::ValidCount = Invalid.AsInt();
  33. static_assert(
  34. BuiltinInstKind::ValidCount != 0,
  35. "The above `constexpr` definition of `ValidCount` makes it available in "
  36. "a `constexpr` context despite being declared as merely `const`. We use it "
  37. "in a static assert here to ensure that.");
  38. // We expect the builtin kind to fit compactly into 8 bits.
  39. static_assert(sizeof(BuiltinInstKind) == 1, "Kind objects include padding!");
  40. } // namespace Carbon::SemIR
  41. #endif // CARBON_TOOLCHAIN_SEM_IR_BUILTIN_INST_KIND_H_