semantics_node.h 12 KB

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