semantics_node_stack.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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_STACK_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_STACK_H_
  6. #include <type_traits>
  7. #include "common/vlog.h"
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "toolchain/parser/parse_node_kind.h"
  10. #include "toolchain/parser/parse_tree.h"
  11. #include "toolchain/semantics/semantics_node.h"
  12. namespace Carbon {
  13. // Wraps the stack of nodes for SemanticsParseTreeHandler.
  14. //
  15. // All pushes and pops will be vlogged.
  16. //
  17. // Pop APIs will run basic verification:
  18. //
  19. // - If receiving a pop_parse_kind, verify that the parse_node being popped is
  20. // of pop_parse_kind.
  21. // - Validates presence of node_id based on whether it's a solo
  22. // parse_node.
  23. //
  24. // These should be assumed API constraints unless otherwise mentioned on a
  25. // method. The main exception is PopAndIgnore, which doesn't do verification.
  26. class SemanticsNodeStack {
  27. public:
  28. explicit SemanticsNodeStack(const ParseTree& parse_tree,
  29. llvm::raw_ostream* vlog_stream)
  30. : parse_tree_(&parse_tree), vlog_stream_(vlog_stream) {}
  31. // Pushes a solo parse tree node onto the stack. Used when there is no
  32. // IR generated by the node.
  33. auto Push(ParseTree::Node parse_node) -> void {
  34. CARBON_CHECK(ParseNodeKindToIdKind(parse_tree_->node_kind(parse_node)) ==
  35. IdKind::SoloParseNode)
  36. << "Parse kind expects an Id: " << parse_tree_->node_kind(parse_node);
  37. CARBON_VLOG() << "Node Push " << stack_.size() << ": "
  38. << parse_tree_->node_kind(parse_node) << " -> <none>\n";
  39. CARBON_CHECK(stack_.size() < (1 << 20))
  40. << "Excessive stack size: likely infinite loop";
  41. stack_.push_back(Entry(parse_node, SemanticsNodeId::Invalid));
  42. }
  43. // Pushes a parse tree node onto the stack with an ID.
  44. template <typename IdT>
  45. auto Push(ParseTree::Node parse_node, IdT id) -> void {
  46. CARBON_CHECK(ParseNodeKindToIdKind(parse_tree_->node_kind(parse_node)) ==
  47. IdTypeToIdKind<IdT>())
  48. << "Parse kind expected a different IdT: "
  49. << parse_tree_->node_kind(parse_node) << " -> " << id << "\n";
  50. CARBON_CHECK(id.is_valid()) << "Push called with invalid id: "
  51. << parse_tree_->node_kind(parse_node);
  52. CARBON_VLOG() << "Node Push " << stack_.size() << ": "
  53. << parse_tree_->node_kind(parse_node) << " -> " << id << "\n";
  54. CARBON_CHECK(stack_.size() < (1 << 20))
  55. << "Excessive stack size: likely infinite loop";
  56. stack_.push_back(Entry(parse_node, id));
  57. }
  58. // Pops the top of the stack without any verification.
  59. auto PopAndIgnore() -> void { PopEntry<SemanticsNodeId>(); }
  60. // Pops the top of the stack and returns the parse_node.
  61. template <ParseNodeKind::RawEnumType RequiredParseKind>
  62. auto PopForSoloParseNode() -> ParseTree::Node {
  63. Entry back = PopEntry<SemanticsNodeId>();
  64. RequireIdKind(ParseNodeKind::Create(RequiredParseKind),
  65. IdKind::SoloParseNode);
  66. RequireParseKind<RequiredParseKind>(back.parse_node);
  67. return back.parse_node;
  68. }
  69. // Pops the top of the stack.
  70. template <ParseNodeKind::RawEnumType RequiredParseKind>
  71. auto PopAndDiscardSoloParseNode() -> void {
  72. PopForSoloParseNode<RequiredParseKind>();
  73. }
  74. // Pops an expression from the top of the stack and returns the parse_node and
  75. // the ID.
  76. auto PopExpressionWithParseNode()
  77. -> std::pair<ParseTree::Node, SemanticsNodeId> {
  78. return PopWithParseNode<SemanticsNodeId>();
  79. }
  80. // Pops the top of the stack and returns the parse_node and the ID.
  81. template <ParseNodeKind::RawEnumType RequiredParseKind>
  82. auto PopWithParseNode() -> auto {
  83. constexpr IdKind RequiredIdKind =
  84. ParseNodeKindToIdKind(ParseNodeKind::Create(RequiredParseKind));
  85. if constexpr (RequiredIdKind == IdKind::SemanticsNodeId) {
  86. auto back = PopWithParseNode<SemanticsNodeId>();
  87. RequireParseKind<RequiredParseKind>(back.first);
  88. return back;
  89. }
  90. if constexpr (RequiredIdKind == IdKind::SemanticsNodeBlockId) {
  91. auto back = PopWithParseNode<SemanticsNodeBlockId>();
  92. RequireParseKind<RequiredParseKind>(back.first);
  93. return back;
  94. }
  95. if constexpr (RequiredIdKind == IdKind::SemanticsFunctionId) {
  96. auto back = PopWithParseNode<SemanticsFunctionId>();
  97. RequireParseKind<RequiredParseKind>(back.first);
  98. return back;
  99. }
  100. if constexpr (RequiredIdKind == IdKind::SemanticsStringId) {
  101. auto back = PopWithParseNode<SemanticsStringId>();
  102. RequireParseKind<RequiredParseKind>(back.first);
  103. return back;
  104. }
  105. if constexpr (RequiredIdKind == IdKind::SemanticsTypeId) {
  106. auto back = PopWithParseNode<SemanticsTypeId>();
  107. RequireParseKind<RequiredParseKind>(back.first);
  108. return back;
  109. }
  110. CARBON_FATAL() << "Unpoppable IdKind for parse kind: "
  111. << ParseNodeKind::Create(RequiredParseKind)
  112. << "; see value in ParseNodeKindToIdKind";
  113. }
  114. // Pops an expression from the top of the stack and returns the ID.
  115. // Expressions map multiple ParseNodeKinds to SemanticsNodeId always.
  116. auto PopExpression() -> SemanticsNodeId {
  117. return PopExpressionWithParseNode().second;
  118. }
  119. // Pops the top of the stack and returns the ID.
  120. template <ParseNodeKind::RawEnumType RequiredParseKind>
  121. auto Pop() -> auto {
  122. return PopWithParseNode<RequiredParseKind>().second;
  123. }
  124. // Peeks at the parse_node of the top of the stack.
  125. auto PeekParseNode() -> ParseTree::Node { return stack_.back().parse_node; }
  126. // Peeks at the ID of the top of the stack.
  127. template <ParseNodeKind::RawEnumType RequiredParseKind>
  128. auto Peek() -> auto {
  129. Entry back = stack_.back();
  130. RequireParseKind<RequiredParseKind>(back.parse_node);
  131. constexpr IdKind RequiredIdKind =
  132. ParseNodeKindToIdKind(ParseNodeKind::Create(RequiredParseKind));
  133. if constexpr (RequiredIdKind == IdKind::SemanticsNodeId) {
  134. return back.id<SemanticsNodeId>();
  135. }
  136. if constexpr (RequiredIdKind == IdKind::SemanticsNodeBlockId) {
  137. return back.id<SemanticsNodeBlockId>();
  138. }
  139. if constexpr (RequiredIdKind == IdKind::SemanticsFunctionId) {
  140. return back.id<SemanticsFunctionId>();
  141. }
  142. if constexpr (RequiredIdKind == IdKind::SemanticsStringId) {
  143. return back.id<SemanticsStringId>();
  144. }
  145. if constexpr (RequiredIdKind == IdKind::SemanticsTypeId) {
  146. return back.id<SemanticsTypeId>();
  147. }
  148. CARBON_FATAL() << "Unpeekable IdKind for parse kind: "
  149. << ParseNodeKind::Create(RequiredParseKind)
  150. << "; see value in ParseNodeKindToIdKind";
  151. }
  152. // Prints the stack for a stack dump.
  153. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  154. auto empty() const -> bool { return stack_.empty(); }
  155. auto size() const -> size_t { return stack_.size(); }
  156. private:
  157. // Possible associated ID types.
  158. enum class IdKind {
  159. SemanticsNodeId,
  160. SemanticsNodeBlockId,
  161. SemanticsFunctionId,
  162. SemanticsStringId,
  163. SemanticsTypeId,
  164. // No associated ID type.
  165. SoloParseNode,
  166. // Not expected in the node stack.
  167. Unused,
  168. };
  169. // An entry in stack_.
  170. struct Entry {
  171. explicit Entry(ParseTree::Node parse_node, SemanticsNodeId node_id)
  172. : parse_node(parse_node), node_id(node_id) {}
  173. explicit Entry(ParseTree::Node parse_node,
  174. SemanticsNodeBlockId node_block_id)
  175. : parse_node(parse_node), node_block_id(node_block_id) {}
  176. explicit Entry(ParseTree::Node parse_node, SemanticsFunctionId function_id)
  177. : parse_node(parse_node), function_id(function_id) {}
  178. explicit Entry(ParseTree::Node parse_node, SemanticsStringId name_id)
  179. : parse_node(parse_node), name_id(name_id) {}
  180. explicit Entry(ParseTree::Node parse_node, SemanticsTypeId type_id)
  181. : parse_node(parse_node), type_id(type_id) {}
  182. // Returns the appropriate ID basaed on type.
  183. template <typename T>
  184. auto id() -> T& {
  185. if constexpr (std::is_same<T, SemanticsNodeId>()) {
  186. return node_id;
  187. }
  188. if constexpr (std::is_same<T, SemanticsNodeBlockId>()) {
  189. return node_block_id;
  190. }
  191. if constexpr (std::is_same<T, SemanticsFunctionId>()) {
  192. return function_id;
  193. }
  194. if constexpr (std::is_same<T, SemanticsStringId>()) {
  195. return name_id;
  196. }
  197. if constexpr (std::is_same<T, SemanticsTypeId>()) {
  198. return type_id;
  199. }
  200. }
  201. // The node associated with the stack entry.
  202. ParseTree::Node parse_node;
  203. // The entries will evaluate as invalid if and only if they're a solo
  204. // parse_node. Invalid is used instead of optional to save space.
  205. //
  206. // A discriminator isn't needed because the caller can determine which field
  207. // is used based on the ParseNodeKind.
  208. union {
  209. SemanticsNodeId node_id;
  210. SemanticsNodeBlockId node_block_id;
  211. SemanticsFunctionId function_id;
  212. SemanticsStringId name_id;
  213. SemanticsTypeId type_id;
  214. };
  215. };
  216. static_assert(sizeof(Entry) == 8, "Unexpected Entry size");
  217. // Translate a parse node kind to the enum ID kind it should always provide.
  218. static constexpr auto ParseNodeKindToIdKind(ParseNodeKind kind) -> IdKind {
  219. switch (kind) {
  220. case ParseNodeKind::CallExpression:
  221. case ParseNodeKind::CallExpressionStart:
  222. case ParseNodeKind::IfExpressionElse:
  223. case ParseNodeKind::InfixOperator:
  224. case ParseNodeKind::Literal:
  225. case ParseNodeKind::MemberAccessExpression:
  226. case ParseNodeKind::NameExpression:
  227. case ParseNodeKind::ParenExpression:
  228. case ParseNodeKind::PatternBinding:
  229. case ParseNodeKind::PrefixOperator:
  230. case ParseNodeKind::ShortCircuitOperand:
  231. case ParseNodeKind::StructFieldValue:
  232. case ParseNodeKind::StructLiteral:
  233. case ParseNodeKind::StructFieldType:
  234. case ParseNodeKind::StructTypeLiteral:
  235. case ParseNodeKind::TupleLiteral:
  236. return IdKind::SemanticsNodeId;
  237. case ParseNodeKind::IfExpressionThen:
  238. case ParseNodeKind::IfStatementElse:
  239. case ParseNodeKind::ParameterList:
  240. return IdKind::SemanticsNodeBlockId;
  241. case ParseNodeKind::FunctionDefinitionStart:
  242. return IdKind::SemanticsFunctionId;
  243. case ParseNodeKind::Name:
  244. return IdKind::SemanticsStringId;
  245. case ParseNodeKind::ReturnType:
  246. return IdKind::SemanticsTypeId;
  247. case ParseNodeKind::CodeBlockStart:
  248. case ParseNodeKind::FunctionIntroducer:
  249. case ParseNodeKind::IfCondition:
  250. case ParseNodeKind::IfExpressionIf:
  251. case ParseNodeKind::ParameterListStart:
  252. case ParseNodeKind::ParenExpressionOrTupleLiteralStart:
  253. case ParseNodeKind::QualifiedDeclaration:
  254. case ParseNodeKind::ReturnStatementStart:
  255. case ParseNodeKind::StructLiteralOrStructTypeLiteralStart:
  256. case ParseNodeKind::VariableInitializer:
  257. case ParseNodeKind::VariableIntroducer:
  258. return IdKind::SoloParseNode;
  259. default:
  260. return IdKind::Unused;
  261. }
  262. }
  263. // Translates an ID type to the enum ID kind for comparison with
  264. // ParseNodeKindToIdKind.
  265. template <typename IdT>
  266. static constexpr auto IdTypeToIdKind() -> IdKind {
  267. if constexpr (std::is_same_v<IdT, SemanticsNodeId>) {
  268. return IdKind::SemanticsNodeId;
  269. }
  270. if constexpr (std::is_same_v<IdT, SemanticsNodeBlockId>) {
  271. return IdKind::SemanticsNodeBlockId;
  272. }
  273. if constexpr (std::is_same_v<IdT, SemanticsFunctionId>) {
  274. return IdKind::SemanticsFunctionId;
  275. }
  276. if constexpr (std::is_same_v<IdT, SemanticsStringId>) {
  277. return IdKind::SemanticsStringId;
  278. }
  279. if constexpr (std::is_same_v<IdT, SemanticsTypeId>) {
  280. return IdKind::SemanticsTypeId;
  281. }
  282. }
  283. // Pops an entry.
  284. template <typename IdT>
  285. auto PopEntry() -> Entry {
  286. Entry back = stack_.pop_back_val();
  287. CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
  288. << parse_tree_->node_kind(back.parse_node) << " -> "
  289. << back.id<IdT>() << "\n";
  290. return back;
  291. }
  292. // Pops the top of the stack and returns the parse_node and the ID.
  293. template <typename IdT>
  294. auto PopWithParseNode() -> std::pair<ParseTree::Node, IdT> {
  295. Entry back = PopEntry<IdT>();
  296. RequireIdKind(parse_tree_->node_kind(back.parse_node),
  297. IdTypeToIdKind<IdT>());
  298. return {back.parse_node, back.id<IdT>()};
  299. }
  300. // Require a ParseNodeKind be mapped to a particular IdKind.
  301. auto RequireIdKind(ParseNodeKind parse_kind, IdKind id_kind) -> void {
  302. CARBON_CHECK(ParseNodeKindToIdKind(parse_kind) == id_kind)
  303. << "Unexpected IdKind mapping for " << parse_kind;
  304. }
  305. // Require an entry to have the given ParseNodeKind.
  306. template <ParseNodeKind::RawEnumType RequiredParseKind>
  307. auto RequireParseKind(ParseTree::Node parse_node) -> void {
  308. auto actual_kind = parse_tree_->node_kind(parse_node);
  309. CARBON_CHECK(RequiredParseKind == actual_kind)
  310. << "Expected " << ParseNodeKind::Create(RequiredParseKind) << ", found "
  311. << actual_kind;
  312. }
  313. // The file's parse tree.
  314. const ParseTree* parse_tree_;
  315. // Whether to print verbose output.
  316. llvm::raw_ostream* vlog_stream_;
  317. // The actual stack.
  318. // PushEntry and PopEntry control modification in order to centralize
  319. // vlogging.
  320. llvm::SmallVector<Entry> stack_;
  321. };
  322. } // namespace Carbon
  323. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_STACK_H_