semantics_builtin_kind.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 "toolchain/common/enum_base.h"
  7. namespace Carbon {
  8. CARBON_DEFINE_RAW_ENUM_CLASS(SemanticsBuiltinKind, uint8_t) {
  9. #define CARBON_SEMANTICS_BUILTIN_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  10. #include "toolchain/semantics/semantics_builtin_kind.def"
  11. };
  12. class SemanticsBuiltinKind : public CARBON_ENUM_BASE(SemanticsBuiltinKind) {
  13. public:
  14. #define CARBON_SEMANTICS_BUILTIN_KIND(Name) \
  15. CARBON_ENUM_CONSTANT_DECLARATION(Name)
  16. #include "toolchain/semantics/semantics_builtin_kind.def"
  17. // The count of enum values excluding Invalid.
  18. //
  19. // Note that we *define* this as `constexpr` making it a true compile-time
  20. // constant, and so we name it accordingly and disable the lint error here.
  21. // NOLINTNEXTLINE(readability-identifier-naming)
  22. static const uint8_t ValidCount;
  23. // Support conversion to and from an int32_t for SemanticNode storage.
  24. using EnumBase::AsInt;
  25. using EnumBase::FromInt;
  26. };
  27. #define CARBON_SEMANTICS_BUILTIN_KIND(Name) \
  28. CARBON_ENUM_CONSTANT_DEFINITION(SemanticsBuiltinKind, Name)
  29. #include "toolchain/semantics/semantics_builtin_kind.def"
  30. constexpr uint8_t SemanticsBuiltinKind::ValidCount = Invalid.AsInt();
  31. static_assert(
  32. SemanticsBuiltinKind::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(SemanticsBuiltinKind) == 1,
  38. "Kind objects include padding!");
  39. } // namespace Carbon
  40. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_BUILTIN_KIND_H_