node_kind.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_NODE_KIND_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_NODE_KIND_H_
  6. #include <cstdint>
  7. #include "common/enum_base.h"
  8. #include "llvm/ADT/FoldingSet.h"
  9. namespace Carbon::SemIR {
  10. // Whether a node produces or represents a value, and if so, what kind of value.
  11. enum class NodeValueKind : int8_t {
  12. // This node doesn't produce a value, and shouldn't be referenced by other
  13. // nodes.
  14. None,
  15. // This node represents an untyped value. It may be referenced by other nodes
  16. // expecting this kind of value.
  17. Untyped,
  18. // This node represents an expression or expression-like construct that
  19. // produces a value of the type indicated by its `type_id` field.
  20. Typed,
  21. };
  22. // Whether a node is a terminator or part of the terminator sequence. The nodes
  23. // in a block appear in the order NotTerminator, then TerminatorSequence, then
  24. // Terminator, which is also the numerical order of these values.
  25. enum class TerminatorKind : int8_t {
  26. // This node is not a terminator.
  27. NotTerminator,
  28. // This node is not itself a terminator, but forms part of a terminator
  29. // sequence.
  30. TerminatorSequence,
  31. // This node is a terminator.
  32. Terminator,
  33. };
  34. CARBON_DEFINE_RAW_ENUM_CLASS(NodeKind, uint8_t) {
  35. #define CARBON_SEMANTICS_NODE_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  36. #include "toolchain/sem_ir/node_kind.def"
  37. };
  38. class NodeKind : public CARBON_ENUM_BASE(NodeKind) {
  39. public:
  40. #define CARBON_SEMANTICS_NODE_KIND(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name)
  41. #include "toolchain/sem_ir/node_kind.def"
  42. using EnumBase::Create;
  43. // Returns the name to use for this node kind in Semantics IR.
  44. [[nodiscard]] auto ir_name() const -> llvm::StringRef;
  45. // Returns whether this kind of node is expected to produce a value.
  46. [[nodiscard]] auto value_kind() const -> NodeValueKind;
  47. // Returns whether this node kind is a code block terminator, such as an
  48. // unconditional branch instruction, or part of the termination sequence,
  49. // such as a conditional branch instruction. The termination sequence of a
  50. // code block appears after all other instructions, and ends with a
  51. // terminator instruction.
  52. [[nodiscard]] auto terminator_kind() const -> TerminatorKind;
  53. // Compute a fingerprint for this node kind, allowing its use as part of the
  54. // key in a `FoldingSet`.
  55. void Profile(llvm::FoldingSetNodeID& id) { id.AddInteger(AsInt()); }
  56. };
  57. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  58. CARBON_ENUM_CONSTANT_DEFINITION(NodeKind, Name)
  59. #include "toolchain/sem_ir/node_kind.def"
  60. // We expect the node kind to fit compactly into 8 bits.
  61. static_assert(sizeof(NodeKind) == 1, "Kind objects include padding!");
  62. } // namespace Carbon::SemIR
  63. #endif // CARBON_TOOLCHAIN_SEM_IR_NODE_KIND_H_