semantics_builtin_kind.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_SEMANTICS_SEMANTICS_BUILTIN_KIND_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_BUILTIN_KIND_H_
  6. #include <cstdint>
  7. #include "common/enum_base.h"
  8. namespace Carbon {
  9. CARBON_DEFINE_RAW_ENUM_CLASS(SemanticsBuiltinKind, uint8_t) {
  10. #define CARBON_SEMANTICS_BUILTIN_KIND_NAME(Name) \
  11. CARBON_RAW_ENUM_ENUMERATOR(Name)
  12. #include "toolchain/semantics/semantics_builtin_kind.def"
  13. };
  14. class SemanticsBuiltinKind : public CARBON_ENUM_BASE(SemanticsBuiltinKind) {
  15. public:
  16. #define CARBON_SEMANTICS_BUILTIN_KIND_NAME(Name) \
  17. CARBON_ENUM_CONSTANT_DECLARATION(Name)
  18. #include "toolchain/semantics/semantics_builtin_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 SemanticNode storage.
  26. using EnumBase::AsInt;
  27. using EnumBase::FromInt;
  28. };
  29. #define CARBON_SEMANTICS_BUILTIN_KIND_NAME(Name) \
  30. CARBON_ENUM_CONSTANT_DEFINITION(SemanticsBuiltinKind, Name)
  31. #include "toolchain/semantics/semantics_builtin_kind.def"
  32. constexpr uint8_t SemanticsBuiltinKind::ValidCount = Invalid.AsInt();
  33. static_assert(
  34. SemanticsBuiltinKind::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(SemanticsBuiltinKind) == 1,
  40. "Kind objects include padding!");
  41. } // namespace Carbon
  42. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_BUILTIN_KIND_H_