semantics_node.h 10 KB

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