semantics_node.h 8.5 KB

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