semantics_node.h 9.5 KB

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