node_stack.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/inst.h"
  12. namespace Carbon::Check {
  13. // Wraps the stack of parse nodes for Context. Each parse node can have an
  14. // associated id of some kind (instruction, instruction block, function, class,
  15. // ...).
  16. //
  17. // All pushes and pops will be vlogged.
  18. //
  19. // Pop APIs will run basic verification:
  20. //
  21. // - If receiving a pop_parse_kind, verify that the parse_node being popped is
  22. // of pop_parse_kind.
  23. // - Validates presence of inst_id based on whether it's a solo
  24. // parse_node.
  25. //
  26. // These should be assumed API constraints unless otherwise mentioned on a
  27. // method. The main exception is PopAndIgnore, which doesn't do verification.
  28. class NodeStack {
  29. public:
  30. explicit NodeStack(const Parse::Tree& parse_tree,
  31. llvm::raw_ostream* vlog_stream)
  32. : parse_tree_(&parse_tree), vlog_stream_(vlog_stream) {}
  33. // Pushes a solo parse tree node onto the stack. Used when there is no
  34. // IR generated by the node.
  35. auto Push(Parse::Node parse_node) -> void {
  36. CARBON_CHECK(ParseNodeKindToIdKind(parse_tree_->node_kind(parse_node)) ==
  37. IdKind::SoloParseNode)
  38. << "Parse kind expects an Id: " << parse_tree_->node_kind(parse_node);
  39. CARBON_VLOG() << "Node Push " << stack_.size() << ": "
  40. << parse_tree_->node_kind(parse_node) << " -> <none>\n";
  41. CARBON_CHECK(stack_.size() < (1 << 20))
  42. << "Excessive stack size: likely infinite loop";
  43. stack_.push_back(Entry(parse_node, SemIR::InstId::Invalid));
  44. }
  45. // Pushes a parse tree node onto the stack with an ID.
  46. template <typename IdT>
  47. auto Push(Parse::Node parse_node, IdT id) -> void {
  48. CARBON_CHECK(ParseNodeKindToIdKind(parse_tree_->node_kind(parse_node)) ==
  49. IdTypeToIdKind<IdT>())
  50. << "Parse kind expected a different IdT: "
  51. << parse_tree_->node_kind(parse_node) << " -> " << id << "\n";
  52. CARBON_CHECK(id.is_valid()) << "Push called with invalid id: "
  53. << parse_tree_->node_kind(parse_node);
  54. CARBON_VLOG() << "Node Push " << stack_.size() << ": "
  55. << parse_tree_->node_kind(parse_node) << " -> " << id << "\n";
  56. CARBON_CHECK(stack_.size() < (1 << 20))
  57. << "Excessive stack size: likely infinite loop";
  58. stack_.push_back(Entry(parse_node, id));
  59. }
  60. // Returns whether the node on the top of the stack is the specified kind.
  61. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  62. auto PeekIs() const -> bool {
  63. return parse_tree_->node_kind(PeekParseNode()) == RequiredParseKind;
  64. }
  65. // Pops the top of the stack without any verification.
  66. auto PopAndIgnore() -> void { PopEntry<SemIR::InstId>(); }
  67. // Pops the top of the stack and returns the parse_node.
  68. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  69. auto PopForSoloParseNode() -> Parse::Node {
  70. Entry back = PopEntry<SemIR::InstId>();
  71. RequireIdKind(Parse::NodeKind::Create(RequiredParseKind),
  72. IdKind::SoloParseNode);
  73. RequireParseKind<RequiredParseKind>(back.parse_node);
  74. return back.parse_node;
  75. }
  76. // Pops the top of the stack if it is the given kind, and returns the
  77. // parse_node. Otherwise, returns std::nullopt.
  78. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  79. auto PopForSoloParseNodeIf() -> std::optional<Parse::Node> {
  80. if (PeekIs<RequiredParseKind>()) {
  81. return PopForSoloParseNode<RequiredParseKind>();
  82. }
  83. return std::nullopt;
  84. }
  85. // Pops the top of the stack.
  86. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  87. auto PopAndDiscardSoloParseNode() -> void {
  88. PopForSoloParseNode<RequiredParseKind>();
  89. }
  90. // Pops an expression from the top of the stack and returns the parse_node and
  91. // the ID.
  92. auto PopExprWithParseNode() -> std::pair<Parse::Node, SemIR::InstId> {
  93. return PopWithParseNode<SemIR::InstId>();
  94. }
  95. // Pops the top of the stack and returns the parse_node and the ID.
  96. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  97. auto PopWithParseNode() -> auto {
  98. constexpr IdKind RequiredIdKind =
  99. ParseNodeKindToIdKind(Parse::NodeKind::Create(RequiredParseKind));
  100. if constexpr (RequiredIdKind == IdKind::InstId) {
  101. auto back = PopWithParseNode<SemIR::InstId>();
  102. RequireParseKind<RequiredParseKind>(back.first);
  103. return back;
  104. }
  105. if constexpr (RequiredIdKind == IdKind::InstBlockId) {
  106. auto back = PopWithParseNode<SemIR::InstBlockId>();
  107. RequireParseKind<RequiredParseKind>(back.first);
  108. return back;
  109. }
  110. if constexpr (RequiredIdKind == IdKind::FunctionId) {
  111. auto back = PopWithParseNode<SemIR::FunctionId>();
  112. RequireParseKind<RequiredParseKind>(back.first);
  113. return back;
  114. }
  115. if constexpr (RequiredIdKind == IdKind::ClassId) {
  116. auto back = PopWithParseNode<SemIR::ClassId>();
  117. RequireParseKind<RequiredParseKind>(back.first);
  118. return back;
  119. }
  120. if constexpr (RequiredIdKind == IdKind::NameId) {
  121. auto back = PopWithParseNode<SemIR::NameId>();
  122. RequireParseKind<RequiredParseKind>(back.first);
  123. return back;
  124. }
  125. if constexpr (RequiredIdKind == IdKind::TypeId) {
  126. auto back = PopWithParseNode<SemIR::TypeId>();
  127. RequireParseKind<RequiredParseKind>(back.first);
  128. return back;
  129. }
  130. CARBON_FATAL() << "Unpoppable IdKind for parse kind: "
  131. << Parse::NodeKind::Create(RequiredParseKind)
  132. << "; see value in ParseNodeKindToIdKind";
  133. }
  134. // Pops an expression from the top of the stack and returns the ID.
  135. // Expressions map multiple Parse::NodeKinds to SemIR::InstId always.
  136. auto PopExpr() -> SemIR::InstId { return PopExprWithParseNode().second; }
  137. // Pops the top of the stack and returns the ID.
  138. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  139. auto Pop() -> auto {
  140. return PopWithParseNode<RequiredParseKind>().second;
  141. }
  142. // Pops the top of the stack if it has the given kind, and returns the ID.
  143. // Otherwise returns std::nullopt.
  144. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  145. auto PopIf() -> std::optional<decltype(Pop<RequiredParseKind>())> {
  146. if (PeekIs<RequiredParseKind>()) {
  147. return Pop<RequiredParseKind>();
  148. }
  149. return std::nullopt;
  150. }
  151. // Peeks at the parse node of the top of the name stack.
  152. auto PeekParseNode() const -> Parse::Node { return stack_.back().parse_node; }
  153. // Peeks at the ID associated with the top of the name stack.
  154. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  155. auto Peek() const -> auto {
  156. Entry back = stack_.back();
  157. RequireParseKind<RequiredParseKind>(back.parse_node);
  158. constexpr IdKind RequiredIdKind =
  159. ParseNodeKindToIdKind(Parse::NodeKind::Create(RequiredParseKind));
  160. if constexpr (RequiredIdKind == IdKind::InstId) {
  161. return back.id<SemIR::InstId>();
  162. }
  163. if constexpr (RequiredIdKind == IdKind::InstBlockId) {
  164. return back.id<SemIR::InstBlockId>();
  165. }
  166. if constexpr (RequiredIdKind == IdKind::FunctionId) {
  167. return back.id<SemIR::FunctionId>();
  168. }
  169. if constexpr (RequiredIdKind == IdKind::ClassId) {
  170. return back.id<SemIR::ClassId>();
  171. }
  172. if constexpr (RequiredIdKind == IdKind::NameId) {
  173. return back.id<SemIR::NameId>();
  174. }
  175. if constexpr (RequiredIdKind == IdKind::TypeId) {
  176. return back.id<SemIR::TypeId>();
  177. }
  178. CARBON_FATAL() << "Unpeekable IdKind for parse kind: "
  179. << Parse::NodeKind::Create(RequiredParseKind)
  180. << "; see value in ParseNodeKindToIdKind";
  181. }
  182. // Prints the stack for a stack dump.
  183. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  184. auto empty() const -> bool { return stack_.empty(); }
  185. auto size() const -> size_t { return stack_.size(); }
  186. private:
  187. // Possible associated ID types.
  188. enum class IdKind : int8_t {
  189. InstId,
  190. InstBlockId,
  191. FunctionId,
  192. ClassId,
  193. NameId,
  194. TypeId,
  195. // No associated ID type.
  196. SoloParseNode,
  197. // Not expected in the node stack.
  198. Unused,
  199. };
  200. // An entry in stack_.
  201. struct Entry {
  202. explicit Entry(Parse::Node parse_node, SemIR::InstId inst_id)
  203. : parse_node(parse_node), inst_id(inst_id) {}
  204. explicit Entry(Parse::Node parse_node, SemIR::InstBlockId inst_block_id)
  205. : parse_node(parse_node), inst_block_id(inst_block_id) {}
  206. explicit Entry(Parse::Node parse_node, SemIR::FunctionId function_id)
  207. : parse_node(parse_node), function_id(function_id) {}
  208. explicit Entry(Parse::Node parse_node, SemIR::ClassId class_id)
  209. : parse_node(parse_node), class_id(class_id) {}
  210. explicit Entry(Parse::Node parse_node, SemIR::NameId name_id)
  211. : parse_node(parse_node), name_id(name_id) {}
  212. explicit Entry(Parse::Node parse_node, SemIR::TypeId type_id)
  213. : parse_node(parse_node), type_id(type_id) {}
  214. // Returns the appropriate ID basaed on type.
  215. template <typename T>
  216. auto id() -> T& {
  217. if constexpr (std::is_same<T, SemIR::InstId>()) {
  218. return inst_id;
  219. }
  220. if constexpr (std::is_same<T, SemIR::InstBlockId>()) {
  221. return inst_block_id;
  222. }
  223. if constexpr (std::is_same<T, SemIR::FunctionId>()) {
  224. return function_id;
  225. }
  226. if constexpr (std::is_same<T, SemIR::ClassId>()) {
  227. return class_id;
  228. }
  229. if constexpr (std::is_same<T, SemIR::NameId>()) {
  230. return name_id;
  231. }
  232. if constexpr (std::is_same<T, SemIR::TypeId>()) {
  233. return type_id;
  234. }
  235. }
  236. // The parse node associated with the stack entry.
  237. Parse::Node parse_node;
  238. // The entries will evaluate as invalid if and only if they're a solo
  239. // parse_node. Invalid is used instead of optional to save space.
  240. //
  241. // A discriminator isn't needed because the caller can determine which field
  242. // is used based on the Parse::NodeKind.
  243. union {
  244. SemIR::InstId inst_id;
  245. SemIR::InstBlockId inst_block_id;
  246. SemIR::FunctionId function_id;
  247. SemIR::ClassId class_id;
  248. SemIR::NameId name_id;
  249. SemIR::TypeId type_id;
  250. };
  251. };
  252. static_assert(sizeof(Entry) == 8, "Unexpected Entry size");
  253. // Translate a parse node kind to the enum ID kind it should always provide.
  254. static constexpr auto ParseNodeKindToIdKind(Parse::NodeKind kind) -> IdKind {
  255. switch (kind) {
  256. case Parse::NodeKind::ArrayExpr:
  257. case Parse::NodeKind::CallExpr:
  258. case Parse::NodeKind::CallExprStart:
  259. case Parse::NodeKind::IfExprThen:
  260. case Parse::NodeKind::IfExprElse:
  261. case Parse::NodeKind::IndexExpr:
  262. case Parse::NodeKind::InfixOperator:
  263. case Parse::NodeKind::Literal:
  264. case Parse::NodeKind::MemberAccessExpr:
  265. case Parse::NodeKind::NameExpr:
  266. case Parse::NodeKind::ParenExpr:
  267. case Parse::NodeKind::PatternBinding:
  268. case Parse::NodeKind::PostfixOperator:
  269. case Parse::NodeKind::PrefixOperator:
  270. case Parse::NodeKind::ReturnType:
  271. case Parse::NodeKind::SelfTypeNameExpr:
  272. case Parse::NodeKind::SelfValueNameExpr:
  273. case Parse::NodeKind::ShortCircuitOperand:
  274. case Parse::NodeKind::StructFieldValue:
  275. case Parse::NodeKind::StructLiteral:
  276. case Parse::NodeKind::StructFieldType:
  277. case Parse::NodeKind::StructTypeLiteral:
  278. case Parse::NodeKind::TupleLiteral:
  279. return IdKind::InstId;
  280. case Parse::NodeKind::IfCondition:
  281. case Parse::NodeKind::IfExprIf:
  282. case Parse::NodeKind::ImplicitParameterList:
  283. case Parse::NodeKind::ParameterList:
  284. case Parse::NodeKind::WhileCondition:
  285. case Parse::NodeKind::WhileConditionStart:
  286. return IdKind::InstBlockId;
  287. case Parse::NodeKind::FunctionDefinitionStart:
  288. return IdKind::FunctionId;
  289. case Parse::NodeKind::ClassDefinitionStart:
  290. return IdKind::ClassId;
  291. case Parse::NodeKind::Name:
  292. return IdKind::NameId;
  293. case Parse::NodeKind::ArrayExprSemi:
  294. case Parse::NodeKind::ClassIntroducer:
  295. case Parse::NodeKind::CodeBlockStart:
  296. case Parse::NodeKind::FunctionIntroducer:
  297. case Parse::NodeKind::IfStatementElse:
  298. case Parse::NodeKind::ImplicitParameterListStart:
  299. case Parse::NodeKind::LetIntroducer:
  300. case Parse::NodeKind::ParameterListStart:
  301. case Parse::NodeKind::ParenExprOrTupleLiteralStart:
  302. case Parse::NodeKind::QualifiedDecl:
  303. case Parse::NodeKind::ReturnStatementStart:
  304. case Parse::NodeKind::SelfValueName:
  305. case Parse::NodeKind::StructLiteralOrStructTypeLiteralStart:
  306. case Parse::NodeKind::VariableInitializer:
  307. case Parse::NodeKind::VariableIntroducer:
  308. return IdKind::SoloParseNode;
  309. default:
  310. return IdKind::Unused;
  311. }
  312. }
  313. // Translates an ID type to the enum ID kind for comparison with
  314. // ParseNodeKindToIdKind.
  315. template <typename IdT>
  316. static constexpr auto IdTypeToIdKind() -> IdKind {
  317. if constexpr (std::is_same_v<IdT, SemIR::InstId>) {
  318. return IdKind::InstId;
  319. }
  320. if constexpr (std::is_same_v<IdT, SemIR::InstBlockId>) {
  321. return IdKind::InstBlockId;
  322. }
  323. if constexpr (std::is_same_v<IdT, SemIR::FunctionId>) {
  324. return IdKind::FunctionId;
  325. }
  326. if constexpr (std::is_same_v<IdT, SemIR::ClassId>) {
  327. return IdKind::ClassId;
  328. }
  329. if constexpr (std::is_same_v<IdT, SemIR::NameId>) {
  330. return IdKind::NameId;
  331. }
  332. if constexpr (std::is_same_v<IdT, SemIR::TypeId>) {
  333. return IdKind::TypeId;
  334. }
  335. }
  336. // Pops an entry.
  337. template <typename IdT>
  338. auto PopEntry() -> Entry {
  339. Entry back = stack_.pop_back_val();
  340. CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
  341. << parse_tree_->node_kind(back.parse_node) << " -> "
  342. << back.id<IdT>() << "\n";
  343. return back;
  344. }
  345. // Pops the top of the stack and returns the parse_node and the ID.
  346. template <typename IdT>
  347. auto PopWithParseNode() -> std::pair<Parse::Node, IdT> {
  348. Entry back = PopEntry<IdT>();
  349. RequireIdKind(parse_tree_->node_kind(back.parse_node),
  350. IdTypeToIdKind<IdT>());
  351. return {back.parse_node, back.id<IdT>()};
  352. }
  353. // Require a Parse::NodeKind be mapped to a particular IdKind.
  354. auto RequireIdKind(Parse::NodeKind parse_kind, IdKind id_kind) const -> void {
  355. CARBON_CHECK(ParseNodeKindToIdKind(parse_kind) == id_kind)
  356. << "Unexpected IdKind mapping for " << parse_kind;
  357. }
  358. // Require an entry to have the given Parse::NodeKind.
  359. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  360. auto RequireParseKind(Parse::Node parse_node) const -> void {
  361. auto actual_kind = parse_tree_->node_kind(parse_node);
  362. CARBON_CHECK(RequiredParseKind == actual_kind)
  363. << "Expected " << Parse::NodeKind::Create(RequiredParseKind)
  364. << ", found " << actual_kind;
  365. }
  366. // The file's parse tree.
  367. const Parse::Tree* parse_tree_;
  368. // Whether to print verbose output.
  369. llvm::raw_ostream* vlog_stream_;
  370. // The actual stack.
  371. // PushEntry and PopEntry control modification in order to centralize
  372. // vlogging.
  373. llvm::SmallVector<Entry> stack_;
  374. };
  375. } // namespace Carbon::Check
  376. #endif // CARBON_TOOLCHAIN_CHECK_NODE_STACK_H_