semantics_builtin_kind.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/ostream.h"
  8. namespace Carbon {
  9. class SemanticsBuiltinKind {
  10. private:
  11. // Note that this must be declared earlier in the class so that its type can
  12. // be used, for example in the conversion operator.
  13. enum class KindEnum : uint8_t {
  14. #define CARBON_SEMANTICS_BUILTIN_KIND(Name) Name,
  15. #include "toolchain/semantics/semantics_builtin_kind.def"
  16. };
  17. public:
  18. // The count of enum values excluding Invalid.
  19. static constexpr uint8_t ValidCount = static_cast<uint8_t>(KindEnum::Invalid);
  20. // `clang-format` has a bug with spacing around `->` returns in macros. See
  21. // https://bugs.llvm.org/show_bug.cgi?id=48320 for details.
  22. #define CARBON_SEMANTICS_BUILTIN_KIND(Name) \
  23. static constexpr auto Name()->SemanticsBuiltinKind { \
  24. return SemanticsBuiltinKind(KindEnum::Name); \
  25. }
  26. #include "toolchain/semantics/semantics_builtin_kind.def"
  27. // The default constructor is deleted because objects of this type should
  28. // always be constructed using the above factory functions for each unique
  29. // kind.
  30. SemanticsBuiltinKind() = delete;
  31. friend auto operator==(SemanticsBuiltinKind lhs, SemanticsBuiltinKind rhs)
  32. -> bool {
  33. return lhs.kind_ == rhs.kind_;
  34. }
  35. friend auto operator!=(SemanticsBuiltinKind lhs, SemanticsBuiltinKind rhs)
  36. -> bool {
  37. return lhs.kind_ != rhs.kind_;
  38. }
  39. // Gets a friendly name for the token for logging or debugging.
  40. [[nodiscard]] auto name() const -> llvm::StringRef;
  41. // Support conversion to and from an int32_t for SemanticNode storage.
  42. auto AsInt() -> int32_t { return static_cast<int32_t>(kind_); }
  43. static auto FromInt(int32_t val) -> SemanticsBuiltinKind {
  44. return SemanticsBuiltinKind(static_cast<KindEnum>(val));
  45. }
  46. // Enable conversion to our private enum, including in a `constexpr`
  47. // context, to enable usage in `switch` and `case`. The enum remains
  48. // private and nothing else should be using this function.
  49. // NOLINTNEXTLINE(google-explicit-constructor)
  50. constexpr operator KindEnum() const { return kind_; }
  51. void Print(llvm::raw_ostream& out) const { out << name(); }
  52. private:
  53. constexpr explicit SemanticsBuiltinKind(KindEnum k) : kind_(k) {}
  54. KindEnum kind_;
  55. };
  56. // We expect the builtin kind to fit compactly into 8 bits.
  57. static_assert(sizeof(SemanticsBuiltinKind) == 1,
  58. "Kind objects include padding!");
  59. } // namespace Carbon
  60. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_BUILTIN_KIND_H_