builtin_kind.h 1.7 KB

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