node_stack.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_CHECK_NODE_STACK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_NODE_STACK_H_
  6. #include <type_traits>
  7. #include "common/vlog.h"
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "toolchain/parse/node_kind.h"
  10. #include "toolchain/parse/tree.h"
  11. #include "toolchain/sem_ir/node.h"
  12. namespace Carbon::Check {
  13. // Wraps the stack of nodes for Context.
  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 NodeStack {
  27. public:
  28. explicit NodeStack(const Parse::Tree& 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(Parse::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, SemIR::NodeId::Invalid));
  42. }
  43. // Pushes a parse tree node onto the stack with an ID.
  44. template <typename IdT>
  45. auto Push(Parse::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<SemIR::NodeId>(); }
  60. // Pops the top of the stack and returns the parse_node.
  61. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  62. auto PopForSoloParseNode() -> Parse::Node {
  63. Entry back = PopEntry<SemIR::NodeId>();
  64. RequireIdKind(Parse::NodeKind::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 <Parse::NodeKind::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() -> std::pair<Parse::Node, SemIR::NodeId> {
  77. return PopWithParseNode<SemIR::NodeId>();
  78. }
  79. // Pops the top of the stack and returns the parse_node and the ID.
  80. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  81. auto PopWithParseNode() -> auto {
  82. constexpr IdKind RequiredIdKind =
  83. ParseNodeKindToIdKind(Parse::NodeKind::Create(RequiredParseKind));
  84. if constexpr (RequiredIdKind == IdKind::NodeId) {
  85. auto back = PopWithParseNode<SemIR::NodeId>();
  86. RequireParseKind<RequiredParseKind>(back.first);
  87. return back;
  88. }
  89. if constexpr (RequiredIdKind == IdKind::NodeBlockId) {
  90. auto back = PopWithParseNode<SemIR::NodeBlockId>();
  91. RequireParseKind<RequiredParseKind>(back.first);
  92. return back;
  93. }
  94. if constexpr (RequiredIdKind == IdKind::FunctionId) {
  95. auto back = PopWithParseNode<SemIR::FunctionId>();
  96. RequireParseKind<RequiredParseKind>(back.first);
  97. return back;
  98. }
  99. if constexpr (RequiredIdKind == IdKind::ClassId) {
  100. auto back = PopWithParseNode<SemIR::ClassId>();
  101. RequireParseKind<RequiredParseKind>(back.first);
  102. return back;
  103. }
  104. if constexpr (RequiredIdKind == IdKind::StringId) {
  105. auto back = PopWithParseNode<StringId>();
  106. RequireParseKind<RequiredParseKind>(back.first);
  107. return back;
  108. }
  109. if constexpr (RequiredIdKind == IdKind::TypeId) {
  110. auto back = PopWithParseNode<SemIR::TypeId>();
  111. RequireParseKind<RequiredParseKind>(back.first);
  112. return back;
  113. }
  114. CARBON_FATAL() << "Unpoppable IdKind for parse kind: "
  115. << Parse::NodeKind::Create(RequiredParseKind)
  116. << "; see value in ParseNodeKindToIdKind";
  117. }
  118. // Pops an expression from the top of the stack and returns the ID.
  119. // Expressions map multiple Parse::NodeKinds to SemIR::NodeId always.
  120. auto PopExpression() -> SemIR::NodeId {
  121. return PopExpressionWithParseNode().second;
  122. }
  123. // Pops the top of the stack and returns the ID.
  124. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  125. auto Pop() -> auto {
  126. return PopWithParseNode<RequiredParseKind>().second;
  127. }
  128. // Peeks at the parse_node of the given depth in the stack, or by default the
  129. // top node.
  130. auto PeekParseNode() -> Parse::Node { return stack_.back().parse_node; }
  131. // Peeks at the ID of node at the given depth in the stack, or by default the
  132. // top node.
  133. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  134. auto Peek() -> auto {
  135. Entry back = stack_.back();
  136. RequireParseKind<RequiredParseKind>(back.parse_node);
  137. constexpr IdKind RequiredIdKind =
  138. ParseNodeKindToIdKind(Parse::NodeKind::Create(RequiredParseKind));
  139. if constexpr (RequiredIdKind == IdKind::NodeId) {
  140. return back.id<SemIR::NodeId>();
  141. }
  142. if constexpr (RequiredIdKind == IdKind::NodeBlockId) {
  143. return back.id<SemIR::NodeBlockId>();
  144. }
  145. if constexpr (RequiredIdKind == IdKind::FunctionId) {
  146. return back.id<SemIR::FunctionId>();
  147. }
  148. if constexpr (RequiredIdKind == IdKind::ClassId) {
  149. return back.id<SemIR::ClassId>();
  150. }
  151. if constexpr (RequiredIdKind == IdKind::StringId) {
  152. return back.id<StringId>();
  153. }
  154. if constexpr (RequiredIdKind == IdKind::TypeId) {
  155. return back.id<SemIR::TypeId>();
  156. }
  157. CARBON_FATAL() << "Unpeekable IdKind for parse kind: "
  158. << Parse::NodeKind::Create(RequiredParseKind)
  159. << "; see value in ParseNodeKindToIdKind";
  160. }
  161. // Prints the stack for a stack dump.
  162. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  163. auto empty() const -> bool { return stack_.empty(); }
  164. auto size() const -> size_t { return stack_.size(); }
  165. private:
  166. // Possible associated ID types.
  167. enum class IdKind : int8_t {
  168. NodeId,
  169. NodeBlockId,
  170. FunctionId,
  171. ClassId,
  172. StringId,
  173. TypeId,
  174. // No associated ID type.
  175. SoloParseNode,
  176. // Not expected in the node stack.
  177. Unused,
  178. };
  179. // An entry in stack_.
  180. struct Entry {
  181. explicit Entry(Parse::Node parse_node, SemIR::NodeId node_id)
  182. : parse_node(parse_node), node_id(node_id) {}
  183. explicit Entry(Parse::Node parse_node, SemIR::NodeBlockId node_block_id)
  184. : parse_node(parse_node), node_block_id(node_block_id) {}
  185. explicit Entry(Parse::Node parse_node, SemIR::FunctionId function_id)
  186. : parse_node(parse_node), function_id(function_id) {}
  187. explicit Entry(Parse::Node parse_node, SemIR::ClassId class_id)
  188. : parse_node(parse_node), class_id(class_id) {}
  189. explicit Entry(Parse::Node parse_node, StringId name_id)
  190. : parse_node(parse_node), name_id(name_id) {}
  191. explicit Entry(Parse::Node parse_node, SemIR::TypeId type_id)
  192. : parse_node(parse_node), type_id(type_id) {}
  193. // Returns the appropriate ID basaed on type.
  194. template <typename T>
  195. auto id() -> T& {
  196. if constexpr (std::is_same<T, SemIR::NodeId>()) {
  197. return node_id;
  198. }
  199. if constexpr (std::is_same<T, SemIR::NodeBlockId>()) {
  200. return node_block_id;
  201. }
  202. if constexpr (std::is_same<T, SemIR::FunctionId>()) {
  203. return function_id;
  204. }
  205. if constexpr (std::is_same<T, SemIR::ClassId>()) {
  206. return class_id;
  207. }
  208. if constexpr (std::is_same<T, StringId>()) {
  209. return name_id;
  210. }
  211. if constexpr (std::is_same<T, SemIR::TypeId>()) {
  212. return type_id;
  213. }
  214. }
  215. // The node associated with the stack entry.
  216. Parse::Node parse_node;
  217. // The entries will evaluate as invalid if and only if they're a solo
  218. // parse_node. Invalid is used instead of optional to save space.
  219. //
  220. // A discriminator isn't needed because the caller can determine which field
  221. // is used based on the Parse::NodeKind.
  222. union {
  223. SemIR::NodeId node_id;
  224. SemIR::NodeBlockId node_block_id;
  225. SemIR::FunctionId function_id;
  226. SemIR::ClassId class_id;
  227. StringId name_id;
  228. SemIR::TypeId type_id;
  229. };
  230. };
  231. static_assert(sizeof(Entry) == 8, "Unexpected Entry size");
  232. // Translate a parse node kind to the enum ID kind it should always provide.
  233. static constexpr auto ParseNodeKindToIdKind(Parse::NodeKind kind) -> IdKind {
  234. switch (kind) {
  235. case Parse::NodeKind::ArrayExpression:
  236. case Parse::NodeKind::CallExpression:
  237. case Parse::NodeKind::CallExpressionStart:
  238. case Parse::NodeKind::IfExpressionThen:
  239. case Parse::NodeKind::IfExpressionElse:
  240. case Parse::NodeKind::IndexExpression:
  241. case Parse::NodeKind::InfixOperator:
  242. case Parse::NodeKind::Literal:
  243. case Parse::NodeKind::MemberAccessExpression:
  244. case Parse::NodeKind::NameExpression:
  245. case Parse::NodeKind::ParenExpression:
  246. case Parse::NodeKind::PatternBinding:
  247. case Parse::NodeKind::PostfixOperator:
  248. case Parse::NodeKind::PrefixOperator:
  249. case Parse::NodeKind::ReturnType:
  250. case Parse::NodeKind::ShortCircuitOperand:
  251. case Parse::NodeKind::StructFieldValue:
  252. case Parse::NodeKind::StructLiteral:
  253. case Parse::NodeKind::StructFieldType:
  254. case Parse::NodeKind::StructTypeLiteral:
  255. case Parse::NodeKind::TupleLiteral:
  256. return IdKind::NodeId;
  257. case Parse::NodeKind::IfCondition:
  258. case Parse::NodeKind::IfExpressionIf:
  259. case Parse::NodeKind::ParameterList:
  260. case Parse::NodeKind::WhileCondition:
  261. case Parse::NodeKind::WhileConditionStart:
  262. return IdKind::NodeBlockId;
  263. case Parse::NodeKind::FunctionDefinitionStart:
  264. return IdKind::FunctionId;
  265. case Parse::NodeKind::ClassDefinitionStart:
  266. return IdKind::ClassId;
  267. case Parse::NodeKind::Name:
  268. return IdKind::StringId;
  269. case Parse::NodeKind::ArrayExpressionSemi:
  270. case Parse::NodeKind::ClassIntroducer:
  271. case Parse::NodeKind::CodeBlockStart:
  272. case Parse::NodeKind::FunctionIntroducer:
  273. case Parse::NodeKind::IfStatementElse:
  274. case Parse::NodeKind::LetIntroducer:
  275. case Parse::NodeKind::ParameterListStart:
  276. case Parse::NodeKind::ParenExpressionOrTupleLiteralStart:
  277. case Parse::NodeKind::QualifiedDeclaration:
  278. case Parse::NodeKind::ReturnStatementStart:
  279. case Parse::NodeKind::StructLiteralOrStructTypeLiteralStart:
  280. case Parse::NodeKind::VariableInitializer:
  281. case Parse::NodeKind::VariableIntroducer:
  282. return IdKind::SoloParseNode;
  283. default:
  284. return IdKind::Unused;
  285. }
  286. }
  287. // Translates an ID type to the enum ID kind for comparison with
  288. // ParseNodeKindToIdKind.
  289. template <typename IdT>
  290. static constexpr auto IdTypeToIdKind() -> IdKind {
  291. if constexpr (std::is_same_v<IdT, SemIR::NodeId>) {
  292. return IdKind::NodeId;
  293. }
  294. if constexpr (std::is_same_v<IdT, SemIR::NodeBlockId>) {
  295. return IdKind::NodeBlockId;
  296. }
  297. if constexpr (std::is_same_v<IdT, SemIR::FunctionId>) {
  298. return IdKind::FunctionId;
  299. }
  300. if constexpr (std::is_same_v<IdT, SemIR::ClassId>) {
  301. return IdKind::ClassId;
  302. }
  303. if constexpr (std::is_same_v<IdT, StringId>) {
  304. return IdKind::StringId;
  305. }
  306. if constexpr (std::is_same_v<IdT, SemIR::TypeId>) {
  307. return IdKind::TypeId;
  308. }
  309. }
  310. // Pops an entry.
  311. template <typename IdT>
  312. auto PopEntry() -> Entry {
  313. Entry back = stack_.pop_back_val();
  314. CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
  315. << parse_tree_->node_kind(back.parse_node) << " -> "
  316. << back.id<IdT>() << "\n";
  317. return back;
  318. }
  319. // Pops the top of the stack and returns the parse_node and the ID.
  320. template <typename IdT>
  321. auto PopWithParseNode() -> std::pair<Parse::Node, IdT> {
  322. Entry back = PopEntry<IdT>();
  323. RequireIdKind(parse_tree_->node_kind(back.parse_node),
  324. IdTypeToIdKind<IdT>());
  325. return {back.parse_node, back.id<IdT>()};
  326. }
  327. // Require a Parse::NodeKind be mapped to a particular IdKind.
  328. auto RequireIdKind(Parse::NodeKind parse_kind, IdKind id_kind) -> void {
  329. CARBON_CHECK(ParseNodeKindToIdKind(parse_kind) == id_kind)
  330. << "Unexpected IdKind mapping for " << parse_kind;
  331. }
  332. // Require an entry to have the given Parse::NodeKind.
  333. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  334. auto RequireParseKind(Parse::Node parse_node) -> void {
  335. auto actual_kind = parse_tree_->node_kind(parse_node);
  336. CARBON_CHECK(RequiredParseKind == actual_kind)
  337. << "Expected " << Parse::NodeKind::Create(RequiredParseKind)
  338. << ", found " << actual_kind;
  339. }
  340. // The file's parse tree.
  341. const Parse::Tree* parse_tree_;
  342. // Whether to print verbose output.
  343. llvm::raw_ostream* vlog_stream_;
  344. // The actual stack.
  345. // PushEntry and PopEntry control modification in order to centralize
  346. // vlogging.
  347. llvm::SmallVector<Entry> stack_;
  348. };
  349. } // namespace Carbon::Check
  350. #endif // CARBON_TOOLCHAIN_CHECK_NODE_STACK_H_