semantics_node_kind.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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_NODE_KIND_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_KIND_H_
  6. #include <cstdint>
  7. #include "common/enum_base.h"
  8. #include "llvm/ADT/FoldingSet.h"
  9. namespace Carbon {
  10. CARBON_DEFINE_RAW_ENUM_CLASS(SemanticsNodeKind, uint8_t) {
  11. #define CARBON_SEMANTICS_NODE_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  12. #include "toolchain/semantics/semantics_node_kind.def"
  13. };
  14. // Whether a node is a terminator or part of the terminator sequence. The nodes
  15. // in a block appear in the order NotTerminator, then TerminatorSequence, then
  16. // Terminator, which is also the numerical order of these values.
  17. enum class SemanticsTerminatorKind {
  18. // This node is not a terminator.
  19. NotTerminator,
  20. // This node is not itself a terminator, but forms part of a terminator
  21. // sequence.
  22. TerminatorSequence,
  23. // This node is a terminator.
  24. Terminator,
  25. };
  26. class SemanticsNodeKind : public CARBON_ENUM_BASE(SemanticsNodeKind) {
  27. public:
  28. #define CARBON_SEMANTICS_NODE_KIND(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name)
  29. #include "toolchain/semantics/semantics_node_kind.def"
  30. using EnumBase::Create;
  31. // Returns whether this node kind is a code block terminator, such as an
  32. // unconditional branch instruction, or part of the termination sequence,
  33. // such as a conditional branch instruction. The termination sequence of a
  34. // code block appears after all other instructions, and ends with a
  35. // terminator instruction.
  36. [[nodiscard]] auto terminator_kind() const -> SemanticsTerminatorKind;
  37. // Compute a fingerprint for this node kind, allowing its use as part of the
  38. // key in a `FoldingSet`.
  39. void Profile(llvm::FoldingSetNodeID& id) { id.AddInteger(AsInt()); }
  40. };
  41. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  42. CARBON_ENUM_CONSTANT_DEFINITION(SemanticsNodeKind, Name)
  43. #include "toolchain/semantics/semantics_node_kind.def"
  44. // We expect the node kind to fit compactly into 8 bits.
  45. static_assert(sizeof(SemanticsNodeKind) == 1, "Kind objects include padding!");
  46. } // namespace Carbon
  47. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_KIND_H_