semantics_node.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_
  6. #include <cstdint>
  7. #include "common/check.h"
  8. #include "common/ostream.h"
  9. #include "toolchain/semantics/semantics_node_kind.h"
  10. namespace Carbon {
  11. // Type-safe storage of Node IDs.
  12. struct SemanticsNodeId {
  13. SemanticsNodeId() : id(-1) {}
  14. explicit SemanticsNodeId(int32_t id) : id(id) {}
  15. SemanticsNodeId(SemanticsNodeId const&) = default;
  16. auto operator=(const SemanticsNodeId& other) -> SemanticsNodeId& = default;
  17. void Print(llvm::raw_ostream& out) const { out << "node" << id; }
  18. int32_t id;
  19. };
  20. // Type-safe storage of identifiers.
  21. struct SemanticsIdentifierId {
  22. SemanticsIdentifierId() : id(-1) {}
  23. explicit SemanticsIdentifierId(int32_t id) : id(id) {}
  24. void Print(llvm::raw_ostream& out) const { out << "ident" << id; }
  25. int32_t id;
  26. };
  27. // Type-safe storage of integer literals.
  28. struct SemanticsIntegerLiteralId {
  29. SemanticsIntegerLiteralId() : id(-1) {}
  30. explicit SemanticsIntegerLiteralId(int32_t id) : id(id) {}
  31. void Print(llvm::raw_ostream& out) const { out << "int" << id; }
  32. int32_t id;
  33. };
  34. // Type-safe storage of node blocks.
  35. struct SemanticsNodeBlockId {
  36. SemanticsNodeBlockId() : id(-1) {}
  37. explicit SemanticsNodeBlockId(int32_t id) : id(id) {}
  38. void Print(llvm::raw_ostream& out) const { out << "block" << id; }
  39. int32_t id;
  40. };
  41. // The standard structure for nodes.
  42. class SemanticsNode {
  43. public:
  44. struct NoArgs {};
  45. auto GetAsInvalid() const -> NoArgs { CARBON_FATAL() << "Invalid access"; }
  46. static auto MakeBinaryOperatorAdd(SemanticsNodeId lhs, SemanticsNodeId rhs)
  47. -> SemanticsNode {
  48. return SemanticsNode(SemanticsNodeKind::BinaryOperatorAdd(), lhs.id,
  49. rhs.id);
  50. }
  51. auto GetAsBinaryOperatorAdd() const
  52. -> std::pair<SemanticsNodeId, SemanticsNodeId> {
  53. CARBON_CHECK(kind_ == SemanticsNodeKind::BinaryOperatorAdd());
  54. return {SemanticsNodeId(arg0_), SemanticsNodeId(arg1_)};
  55. }
  56. static auto MakeCodeBlock(SemanticsNodeBlockId node_block) -> SemanticsNode {
  57. return SemanticsNode(SemanticsNodeKind::CodeBlock(), node_block.id);
  58. }
  59. auto GetAsCodeBlock() const -> SemanticsNodeBlockId {
  60. CARBON_CHECK(kind_ == SemanticsNodeKind::CodeBlock());
  61. return SemanticsNodeBlockId(arg0_);
  62. }
  63. static auto MakeFunctionDeclaration(SemanticsNodeId name) -> SemanticsNode {
  64. return SemanticsNode(SemanticsNodeKind::FunctionDeclaration(), name.id);
  65. }
  66. auto GetAsFunctionDeclaration() const -> SemanticsNodeId {
  67. CARBON_CHECK(kind_ == SemanticsNodeKind::FunctionDeclaration());
  68. return SemanticsNodeId(arg0_);
  69. }
  70. static auto MakeFunctionDefinition(SemanticsNodeId decl,
  71. SemanticsNodeBlockId node_block)
  72. -> SemanticsNode {
  73. return SemanticsNode(SemanticsNodeKind::FunctionDefinition(), decl.id,
  74. node_block.id);
  75. }
  76. auto GetAsFunctionDefinition() const
  77. -> std::pair<SemanticsNodeId, SemanticsNodeBlockId> {
  78. CARBON_CHECK(kind_ == SemanticsNodeKind::FunctionDefinition());
  79. return {SemanticsNodeId(arg0_), SemanticsNodeBlockId(arg1_)};
  80. }
  81. static auto MakeIdentifier(SemanticsIdentifierId identifier)
  82. -> SemanticsNode {
  83. return SemanticsNode(SemanticsNodeKind::Identifier(), identifier.id);
  84. }
  85. auto GetAsIdentifier() const -> SemanticsIdentifierId {
  86. CARBON_CHECK(kind_ == SemanticsNodeKind::Identifier());
  87. return SemanticsIdentifierId(arg0_);
  88. }
  89. static auto MakeIntegerLiteral(SemanticsIntegerLiteralId integer)
  90. -> SemanticsNode {
  91. return SemanticsNode(SemanticsNodeKind::IntegerLiteral(), integer.id);
  92. }
  93. auto GetAsIntegerLiteral() const -> SemanticsIntegerLiteralId {
  94. CARBON_CHECK(kind_ == SemanticsNodeKind::IntegerLiteral());
  95. return SemanticsIntegerLiteralId(arg0_);
  96. }
  97. static auto MakeReturn() -> SemanticsNode {
  98. return SemanticsNode(SemanticsNodeKind::Return());
  99. }
  100. auto GetAsReturn() const -> NoArgs {
  101. CARBON_CHECK(kind_ == SemanticsNodeKind::Return());
  102. return {};
  103. }
  104. static auto MakeReturnExpression(SemanticsNodeId expr) -> SemanticsNode {
  105. return SemanticsNode(SemanticsNodeKind::ReturnExpression(), expr.id);
  106. }
  107. auto GetAsReturnExpression() const -> SemanticsNodeId {
  108. CARBON_CHECK(kind_ == SemanticsNodeKind::ReturnExpression());
  109. return SemanticsNodeId(arg0_);
  110. }
  111. SemanticsNode() : SemanticsNode(SemanticsNodeKind::Invalid()) {}
  112. auto kind() -> SemanticsNodeKind { return kind_; }
  113. void Print(llvm::raw_ostream& out) const;
  114. private:
  115. explicit SemanticsNode(SemanticsNodeKind kind, int32_t arg0 = -1,
  116. int32_t arg1 = -1)
  117. : kind_(kind), arg0_(arg0), arg1_(arg1) {}
  118. SemanticsNodeKind kind_;
  119. int32_t arg0_;
  120. int32_t arg1_;
  121. };
  122. // TODO: This is currently 12 bytes because we sometimes have 2 arguments for a
  123. // pair of SemanticsNodes. If SemanticsNode was tracked in 3.5 bytes, we could
  124. // potentially change SemanticsNode to 8 bytes. This may be worth investigating
  125. // further.
  126. static_assert(sizeof(SemanticsNode) == 12, "Unexpected SemanticsNode size");
  127. } // namespace Carbon
  128. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_