semantics_node.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_builtin_kind.h"
  10. #include "toolchain/semantics/semantics_node_kind.h"
  11. namespace Carbon {
  12. // Type-safe storage of Node IDs.
  13. struct SemanticsNodeId {
  14. static constexpr int32_t CrossReferenceBit = 0x8000'0000;
  15. // Constructs a cross-reference node ID.
  16. static auto MakeCrossReference(int32_t id) -> SemanticsNodeId {
  17. return SemanticsNodeId(id | CrossReferenceBit);
  18. }
  19. // Constructs a cross-reference node ID for a builtin. This relies on
  20. // SemanticsIR guarantees for builtin cross-reference placement.
  21. static auto MakeBuiltinReference(SemanticsBuiltinKind kind)
  22. -> SemanticsNodeId {
  23. return MakeCrossReference(kind.AsInt());
  24. }
  25. SemanticsNodeId() : id(-1) {}
  26. explicit SemanticsNodeId(int32_t id) : id(id) {}
  27. SemanticsNodeId(SemanticsNodeId const&) = default;
  28. auto operator=(const SemanticsNodeId& other) -> SemanticsNodeId& = default;
  29. auto is_cross_reference() const -> bool { return id & CrossReferenceBit; }
  30. // Returns the ID for a cross-reference, just handling removal of the marker
  31. // bit.
  32. auto GetAsCrossReference() const -> int32_t {
  33. return id & ~CrossReferenceBit;
  34. }
  35. auto Print(llvm::raw_ostream& out) const -> void {
  36. if (is_cross_reference()) {
  37. out << "node_xref" << GetAsCrossReference();
  38. } else {
  39. out << "node" << id;
  40. }
  41. }
  42. int32_t id;
  43. };
  44. // Type-safe storage of identifiers.
  45. struct SemanticsIdentifierId {
  46. SemanticsIdentifierId() : id(-1) {}
  47. explicit SemanticsIdentifierId(int32_t id) : id(id) {}
  48. auto Print(llvm::raw_ostream& out) const -> void { out << "ident" << id; }
  49. int32_t id;
  50. };
  51. // Type-safe storage of integer literals.
  52. struct SemanticsIntegerLiteralId {
  53. SemanticsIntegerLiteralId() : id(-1) {}
  54. explicit SemanticsIntegerLiteralId(int32_t id) : id(id) {}
  55. auto Print(llvm::raw_ostream& out) const -> void { out << "int" << id; }
  56. int32_t id;
  57. };
  58. // Type-safe storage of node blocks.
  59. struct SemanticsNodeBlockId {
  60. SemanticsNodeBlockId() : id(-1) {}
  61. explicit SemanticsNodeBlockId(int32_t id) : id(id) {}
  62. auto Print(llvm::raw_ostream& out) const -> void { out << "block" << id; }
  63. int32_t id;
  64. };
  65. // The standard structure for nodes.
  66. class SemanticsNode {
  67. public:
  68. struct NoArgs {};
  69. auto GetAsInvalid() const -> NoArgs { CARBON_FATAL() << "Invalid access"; }
  70. static auto MakeBinaryOperatorAdd(SemanticsNodeId lhs, SemanticsNodeId rhs)
  71. -> SemanticsNode {
  72. return SemanticsNode(SemanticsNodeKind::BinaryOperatorAdd(),
  73. SemanticsNodeId(), lhs.id, rhs.id);
  74. }
  75. auto GetAsBinaryOperatorAdd() const
  76. -> std::pair<SemanticsNodeId, SemanticsNodeId> {
  77. CARBON_CHECK(kind_ == SemanticsNodeKind::BinaryOperatorAdd());
  78. return {SemanticsNodeId(arg0_), SemanticsNodeId(arg1_)};
  79. }
  80. static auto MakeBindName(SemanticsIdentifierId name, SemanticsNodeId node)
  81. -> SemanticsNode {
  82. return SemanticsNode(SemanticsNodeKind::BindName(), SemanticsNodeId(),
  83. name.id, node.id);
  84. }
  85. auto GetAsBindName() const
  86. -> std::pair<SemanticsIdentifierId, SemanticsNodeId> {
  87. CARBON_CHECK(kind_ == SemanticsNodeKind::BindName());
  88. return {SemanticsIdentifierId(arg0_), SemanticsNodeId(arg1_)};
  89. }
  90. static auto MakeBuiltin(SemanticsBuiltinKind builtin_kind,
  91. SemanticsNodeId type) -> SemanticsNode {
  92. return SemanticsNode(SemanticsNodeKind::Builtin(), type,
  93. builtin_kind.AsInt());
  94. }
  95. auto GetAsBuiltin() const -> SemanticsBuiltinKind {
  96. CARBON_CHECK(kind_ == SemanticsNodeKind::Builtin());
  97. return SemanticsBuiltinKind::FromInt(arg0_);
  98. }
  99. static auto MakeCodeBlock(SemanticsNodeBlockId node_block) -> SemanticsNode {
  100. return SemanticsNode(SemanticsNodeKind::CodeBlock(), SemanticsNodeId(),
  101. node_block.id);
  102. }
  103. auto GetAsCodeBlock() const -> SemanticsNodeBlockId {
  104. CARBON_CHECK(kind_ == SemanticsNodeKind::CodeBlock());
  105. return SemanticsNodeBlockId(arg0_);
  106. }
  107. // TODO: The signature should be added as a parameter.
  108. static auto MakeFunctionDeclaration() -> SemanticsNode {
  109. return SemanticsNode(SemanticsNodeKind::FunctionDeclaration(),
  110. SemanticsNodeId());
  111. }
  112. auto GetAsFunctionDeclaration() const -> NoArgs {
  113. CARBON_CHECK(kind_ == SemanticsNodeKind::FunctionDeclaration());
  114. return {};
  115. }
  116. static auto MakeFunctionDefinition(SemanticsNodeId decl,
  117. SemanticsNodeBlockId node_block)
  118. -> SemanticsNode {
  119. return SemanticsNode(SemanticsNodeKind::FunctionDefinition(),
  120. SemanticsNodeId(), decl.id, node_block.id);
  121. }
  122. auto GetAsFunctionDefinition() const
  123. -> std::pair<SemanticsNodeId, SemanticsNodeBlockId> {
  124. CARBON_CHECK(kind_ == SemanticsNodeKind::FunctionDefinition());
  125. return {SemanticsNodeId(arg0_), SemanticsNodeBlockId(arg1_)};
  126. }
  127. static auto MakeIntegerLiteral(SemanticsIntegerLiteralId integer)
  128. -> SemanticsNode {
  129. return SemanticsNode(SemanticsNodeKind::IntegerLiteral(),
  130. SemanticsNodeId::MakeBuiltinReference(
  131. SemanticsBuiltinKind::IntegerLiteralType()),
  132. integer.id);
  133. }
  134. auto GetAsIntegerLiteral() const -> SemanticsIntegerLiteralId {
  135. CARBON_CHECK(kind_ == SemanticsNodeKind::IntegerLiteral());
  136. return SemanticsIntegerLiteralId(arg0_);
  137. }
  138. static auto MakeReturn() -> SemanticsNode {
  139. return SemanticsNode(SemanticsNodeKind::Return(), SemanticsNodeId());
  140. }
  141. auto GetAsReturn() const -> NoArgs {
  142. CARBON_CHECK(kind_ == SemanticsNodeKind::Return());
  143. return {};
  144. }
  145. static auto MakeReturnExpression(SemanticsNodeId expr) -> SemanticsNode {
  146. return SemanticsNode(SemanticsNodeKind::ReturnExpression(),
  147. SemanticsNodeId(), expr.id);
  148. }
  149. auto GetAsReturnExpression() const -> SemanticsNodeId {
  150. CARBON_CHECK(kind_ == SemanticsNodeKind::ReturnExpression());
  151. return SemanticsNodeId(arg0_);
  152. }
  153. SemanticsNode()
  154. : SemanticsNode(SemanticsNodeKind::Invalid(), SemanticsNodeId()) {}
  155. auto kind() -> SemanticsNodeKind { return kind_; }
  156. auto type() -> SemanticsNodeId { return type_; }
  157. auto Print(llvm::raw_ostream& out) const -> void;
  158. private:
  159. explicit SemanticsNode(SemanticsNodeKind kind, SemanticsNodeId type,
  160. int32_t arg0 = -1, int32_t arg1 = -1)
  161. : kind_(kind), type_(type), arg0_(arg0), arg1_(arg1) {}
  162. SemanticsNodeKind kind_;
  163. SemanticsNodeId type_;
  164. int32_t arg0_;
  165. int32_t arg1_;
  166. };
  167. // TODO: This is currently 16 bytes because we sometimes have 2 arguments for a
  168. // pair of SemanticsNodes. However, SemanticsNodeKind is 1 byte; if args
  169. // were 3.5 bytes, we could potentially shrink SemanticsNode by 4 bytes. This
  170. // may be worth investigating further.
  171. static_assert(sizeof(SemanticsNode) == 16, "Unexpected SemanticsNode size");
  172. } // namespace Carbon
  173. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_