node_stack.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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/parse/typed_nodes.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. namespace Carbon::Check {
  14. // Wraps the stack of parse nodes for Context. Each parse node can have an
  15. // associated id of some kind (instruction, instruction block, function, class,
  16. // ...).
  17. //
  18. // All pushes and pops will be vlogged.
  19. //
  20. // Pop APIs will run basic verification:
  21. //
  22. // - If receiving a Parse::NodeKind, verify that the parse_node being popped has
  23. // that kind. Similarly, if receiving a Parse::NodeCategory, make sure the
  24. // of the popped parse_node overlaps that category.
  25. // - Validates the kind of id data in the node based on the kind or category of
  26. // the parse_node.
  27. //
  28. // These should be assumed API constraints unless otherwise mentioned on a
  29. // method. The main exception is PopAndIgnore, which doesn't do verification.
  30. class NodeStack {
  31. public:
  32. explicit NodeStack(const Parse::Tree& parse_tree,
  33. llvm::raw_ostream* vlog_stream)
  34. : parse_tree_(&parse_tree), vlog_stream_(vlog_stream) {}
  35. // Pushes a solo parse tree node onto the stack. Used when there is no
  36. // IR generated by the node.
  37. auto Push(Parse::NodeId parse_node) -> void {
  38. auto kind = parse_tree_->node_kind(parse_node);
  39. CARBON_CHECK(ParseNodeKindToIdKind(kind) == IdKind::SoloParseNode)
  40. << "Parse kind expects an Id: " << kind;
  41. CARBON_VLOG() << "Node Push " << stack_.size() << ": " << kind
  42. << " -> <none>\n";
  43. CARBON_CHECK(stack_.size() < (1 << 20))
  44. << "Excessive stack size: likely infinite loop";
  45. stack_.push_back(Entry(parse_node, SemIR::InstId::Invalid));
  46. }
  47. // Pushes a parse tree node onto the stack with an ID.
  48. template <typename IdT>
  49. auto Push(Parse::NodeId parse_node, IdT id) -> void {
  50. auto kind = parse_tree_->node_kind(parse_node);
  51. CARBON_CHECK(ParseNodeKindToIdKind(kind) == IdTypeToIdKind<IdT>())
  52. << "Parse kind expected a different IdT: " << kind << " -> " << id
  53. << "\n";
  54. CARBON_CHECK(id.is_valid()) << "Push called with invalid id: "
  55. << parse_tree_->node_kind(parse_node);
  56. CARBON_VLOG() << "Node Push " << stack_.size() << ": " << kind << " -> "
  57. << id << "\n";
  58. CARBON_CHECK(stack_.size() < (1 << 20))
  59. << "Excessive stack size: likely infinite loop";
  60. stack_.push_back(Entry(parse_node, id));
  61. }
  62. // Returns whether there is a node of the specified kind on top of the stack.
  63. auto PeekIs(Parse::NodeKind kind) const -> bool {
  64. return !stack_.empty() && PeekParseNodeKind() == kind;
  65. }
  66. // Returns whether there is a node of the specified kind on top of the stack.
  67. // Templated for consistency with other functions taking a parse node kind.
  68. template <const Parse::NodeKind& RequiredParseKind>
  69. auto PeekIs() const -> bool {
  70. return PeekIs(RequiredParseKind);
  71. }
  72. // Returns whether the node on the top of the stack has an overlapping
  73. // category.
  74. auto PeekIs(Parse::NodeCategory category) const -> bool {
  75. return !stack_.empty() && !!(PeekParseNodeKind().category() & category);
  76. }
  77. // Returns whether the node on the top of the stack has an overlapping
  78. // category. Templated for consistency with other functions taking a parse
  79. // node category.
  80. template <Parse::NodeCategory RequiredParseCategory>
  81. auto PeekIs() const -> bool {
  82. return PeekIs(RequiredParseCategory);
  83. }
  84. // Returns whether there is a name on top of the stack.
  85. auto PeekIsName() const -> bool {
  86. return !stack_.empty() &&
  87. ParseNodeKindToIdKind(PeekParseNodeKind()) == IdKind::NameId;
  88. }
  89. // Pops the top of the stack without any verification.
  90. auto PopAndIgnore() -> void {
  91. Entry back = stack_.pop_back_val();
  92. CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
  93. << parse_tree_->node_kind(back.parse_node)
  94. << " -> <ignored>\n";
  95. }
  96. // Pops the top of the stack and returns the parse_node.
  97. template <const Parse::NodeKind& RequiredParseKind>
  98. auto PopForSoloParseNode() -> Parse::NodeIdForKind<RequiredParseKind> {
  99. Entry back = PopEntry<SemIR::InstId>();
  100. RequireIdKind(RequiredParseKind, IdKind::SoloParseNode);
  101. RequireParseKind<RequiredParseKind>(back.parse_node);
  102. return Parse::NodeIdForKind<RequiredParseKind>(back.parse_node);
  103. }
  104. // Pops the top of the stack if it is the given kind, and returns the
  105. // parse_node. Otherwise, returns std::nullopt.
  106. template <const Parse::NodeKind& RequiredParseKind>
  107. auto PopForSoloParseNodeIf()
  108. -> std::optional<Parse::NodeIdForKind<RequiredParseKind>> {
  109. if (PeekIs<RequiredParseKind>()) {
  110. return PopForSoloParseNode<RequiredParseKind>();
  111. }
  112. return std::nullopt;
  113. }
  114. // Pops the top of the stack.
  115. template <const Parse::NodeKind& RequiredParseKind>
  116. auto PopAndDiscardSoloParseNode() -> void {
  117. PopForSoloParseNode<RequiredParseKind>();
  118. }
  119. // Pops the top of the stack if it is the given kind. Returns `true` if a node
  120. // was popped.
  121. template <const Parse::NodeKind& RequiredParseKind>
  122. auto PopAndDiscardSoloParseNodeIf() -> bool {
  123. if (!PeekIs<RequiredParseKind>()) {
  124. return false;
  125. }
  126. PopForSoloParseNode<RequiredParseKind>();
  127. return true;
  128. }
  129. // Pops an expression from the top of the stack and returns the parse_node and
  130. // the ID.
  131. auto PopExprWithParseNode() -> std::pair<Parse::AnyExprId, SemIR::InstId>;
  132. // Pops a pattern from the top of the stack and returns the parse_node and
  133. // the ID.
  134. auto PopPatternWithParseNode() -> std::pair<Parse::NodeId, SemIR::InstId> {
  135. return PopWithParseNode<SemIR::InstId>();
  136. }
  137. // Pops a name from the top of the stack and returns the parse_node and
  138. // the ID.
  139. auto PopNameWithParseNode() -> std::pair<Parse::NodeId, SemIR::NameId> {
  140. return PopWithParseNode<SemIR::NameId>();
  141. }
  142. // Pops the top of the stack and returns the parse_node and the ID.
  143. template <const Parse::NodeKind& RequiredParseKind>
  144. auto PopWithParseNode() -> auto {
  145. constexpr IdKind RequiredIdKind = ParseNodeKindToIdKind(RequiredParseKind);
  146. auto node_id_cast = [&](auto back) {
  147. using NodeIdT = Parse::NodeIdForKind<RequiredParseKind>;
  148. return std::pair<NodeIdT, decltype(back.second)>(back);
  149. };
  150. if constexpr (RequiredIdKind == IdKind::InstId) {
  151. auto back = PopWithParseNode<SemIR::InstId>();
  152. RequireParseKind<RequiredParseKind>(back.first);
  153. return node_id_cast(back);
  154. }
  155. if constexpr (RequiredIdKind == IdKind::InstBlockId) {
  156. auto back = PopWithParseNode<SemIR::InstBlockId>();
  157. RequireParseKind<RequiredParseKind>(back.first);
  158. return node_id_cast(back);
  159. }
  160. if constexpr (RequiredIdKind == IdKind::FunctionId) {
  161. auto back = PopWithParseNode<SemIR::FunctionId>();
  162. RequireParseKind<RequiredParseKind>(back.first);
  163. return node_id_cast(back);
  164. }
  165. if constexpr (RequiredIdKind == IdKind::ClassId) {
  166. auto back = PopWithParseNode<SemIR::ClassId>();
  167. RequireParseKind<RequiredParseKind>(back.first);
  168. return node_id_cast(back);
  169. }
  170. if constexpr (RequiredIdKind == IdKind::InterfaceId) {
  171. auto back = PopWithParseNode<SemIR::InterfaceId>();
  172. RequireParseKind<RequiredParseKind>(back.first);
  173. return node_id_cast(back);
  174. }
  175. if constexpr (RequiredIdKind == IdKind::NameId) {
  176. auto back = PopWithParseNode<SemIR::NameId>();
  177. RequireParseKind<RequiredParseKind>(back.first);
  178. return node_id_cast(back);
  179. }
  180. if constexpr (RequiredIdKind == IdKind::TypeId) {
  181. auto back = PopWithParseNode<SemIR::TypeId>();
  182. RequireParseKind<RequiredParseKind>(back.first);
  183. return node_id_cast(back);
  184. }
  185. CARBON_FATAL() << "Unpoppable IdKind for parse kind: " << RequiredParseKind
  186. << "; see value in ParseNodeKindToIdKind";
  187. }
  188. // Pops the top of the stack and returns the parse_node and the ID.
  189. template <Parse::NodeCategory RequiredParseCategory>
  190. auto PopWithParseNode() -> auto {
  191. auto id = Peek<RequiredParseCategory>();
  192. Parse::NodeIdInCategory<RequiredParseCategory> parse_node(
  193. stack_.pop_back_val().parse_node);
  194. return std::make_pair(parse_node, id);
  195. }
  196. // Pops the top of the stack and returns the parse_node and the ID if it is
  197. // of the specified kind.
  198. template <const Parse::NodeKind& RequiredParseKind>
  199. auto PopWithParseNodeIf()
  200. -> std::optional<decltype(PopWithParseNode<RequiredParseKind>())> {
  201. if (!PeekIs<RequiredParseKind>()) {
  202. return std::nullopt;
  203. }
  204. return PopWithParseNode<RequiredParseKind>();
  205. }
  206. // Pops the top of the stack and returns the parse_node and the ID if it is
  207. // of the specified category
  208. template <Parse::NodeCategory RequiredParseCategory>
  209. auto PopWithParseNodeIf()
  210. -> std::optional<decltype(PopWithParseNode<RequiredParseCategory>())> {
  211. if (!PeekIs<RequiredParseCategory>()) {
  212. return std::nullopt;
  213. }
  214. return PopWithParseNode<RequiredParseCategory>();
  215. }
  216. // Pops an expression from the top of the stack and returns the ID.
  217. // Expressions always map Parse::NodeCategory::Expr nodes to SemIR::InstId.
  218. auto PopExpr() -> SemIR::InstId { return PopExprWithParseNode().second; }
  219. // Pops a pattern from the top of the stack and returns the ID.
  220. // Patterns map multiple Parse::NodeKinds to SemIR::InstId always.
  221. auto PopPattern() -> SemIR::InstId {
  222. return PopPatternWithParseNode().second;
  223. }
  224. // Pops a name from the top of the stack and returns the ID.
  225. auto PopName() -> SemIR::NameId { return PopNameWithParseNode().second; }
  226. // Pops the top of the stack and returns the ID.
  227. template <const Parse::NodeKind& RequiredParseKind>
  228. auto Pop() -> auto {
  229. return PopWithParseNode<RequiredParseKind>().second;
  230. }
  231. // Pops the top of the stack and returns the ID.
  232. template <Parse::NodeCategory RequiredParseCategory>
  233. auto Pop() -> auto {
  234. return PopWithParseNode<RequiredParseCategory>().second;
  235. }
  236. // Pops the top of the stack and returns the ID.
  237. template <typename IdT>
  238. auto Pop() -> IdT {
  239. return PopWithParseNode<IdT>().second;
  240. }
  241. // Pops the top of the stack if it has the given kind, and returns the ID.
  242. // Otherwise returns std::nullopt.
  243. template <const Parse::NodeKind& RequiredParseKind>
  244. auto PopIf() -> std::optional<decltype(Pop<RequiredParseKind>())> {
  245. if (PeekIs<RequiredParseKind>()) {
  246. return Pop<RequiredParseKind>();
  247. }
  248. return std::nullopt;
  249. }
  250. // Pops the top of the stack if it has the given category, and returns the ID.
  251. // Otherwise returns std::nullopt.
  252. template <Parse::NodeCategory RequiredParseCategory>
  253. auto PopIf() -> std::optional<decltype(Pop<RequiredParseCategory>())> {
  254. if (PeekIs<RequiredParseCategory>()) {
  255. return Pop<RequiredParseCategory>();
  256. }
  257. return std::nullopt;
  258. }
  259. // Peeks at the parse node of the top of the node stack.
  260. auto PeekParseNode() const -> Parse::NodeId {
  261. return stack_.back().parse_node;
  262. }
  263. // Peeks at the kind of the parse node of the top of the node stack.
  264. auto PeekParseNodeKind() const -> Parse::NodeKind {
  265. return parse_tree_->node_kind(PeekParseNode());
  266. }
  267. // Peeks at the ID associated with the top of the name stack.
  268. template <const Parse::NodeKind& RequiredParseKind>
  269. auto Peek() const -> auto {
  270. Entry back = stack_.back();
  271. RequireParseKind<RequiredParseKind>(back.parse_node);
  272. constexpr IdKind RequiredIdKind = ParseNodeKindToIdKind(RequiredParseKind);
  273. if constexpr (RequiredIdKind == IdKind::InstId) {
  274. return back.id<SemIR::InstId>();
  275. }
  276. if constexpr (RequiredIdKind == IdKind::InstBlockId) {
  277. return back.id<SemIR::InstBlockId>();
  278. }
  279. if constexpr (RequiredIdKind == IdKind::FunctionId) {
  280. return back.id<SemIR::FunctionId>();
  281. }
  282. if constexpr (RequiredIdKind == IdKind::ClassId) {
  283. return back.id<SemIR::ClassId>();
  284. }
  285. if constexpr (RequiredIdKind == IdKind::InterfaceId) {
  286. return back.id<SemIR::InterfaceId>();
  287. }
  288. if constexpr (RequiredIdKind == IdKind::NameId) {
  289. return back.id<SemIR::NameId>();
  290. }
  291. if constexpr (RequiredIdKind == IdKind::TypeId) {
  292. return back.id<SemIR::TypeId>();
  293. }
  294. CARBON_FATAL() << "Unpeekable IdKind for parse kind: " << RequiredParseKind
  295. << "; see value in ParseNodeKindToIdKind";
  296. }
  297. // Peeks at the ID associated with the top of the name stack.
  298. template <Parse::NodeCategory RequiredParseCategory>
  299. auto Peek() const -> auto {
  300. Entry back = stack_.back();
  301. RequireParseCategory<RequiredParseCategory>(back.parse_node);
  302. constexpr std::optional<IdKind> RequiredIdKind =
  303. ParseNodeCategoryToIdKind(RequiredParseCategory);
  304. static_assert(RequiredIdKind.has_value());
  305. if constexpr (*RequiredIdKind == IdKind::InstId) {
  306. return back.id<SemIR::InstId>();
  307. } else {
  308. static_assert(*RequiredIdKind == IdKind::NameId,
  309. "Unpeekable IdKind for parse category");
  310. return back.id<SemIR::NameId>();
  311. }
  312. }
  313. // Prints the stack for a stack dump.
  314. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  315. auto empty() const -> bool { return stack_.empty(); }
  316. auto size() const -> size_t { return stack_.size(); }
  317. private:
  318. // Possible associated ID types.
  319. enum class IdKind : int8_t {
  320. InstId,
  321. InstBlockId,
  322. FunctionId,
  323. ClassId,
  324. InterfaceId,
  325. NameId,
  326. // NOTE: Currently unused.
  327. TypeId,
  328. // No associated ID type.
  329. SoloParseNode,
  330. // Not expected in the node stack.
  331. Unused,
  332. };
  333. // An entry in stack_.
  334. struct Entry {
  335. explicit Entry(Parse::NodeId parse_node, SemIR::InstId inst_id)
  336. : parse_node(parse_node), inst_id(inst_id) {}
  337. explicit Entry(Parse::NodeId parse_node, SemIR::InstBlockId inst_block_id)
  338. : parse_node(parse_node), inst_block_id(inst_block_id) {}
  339. explicit Entry(Parse::NodeId parse_node, SemIR::FunctionId function_id)
  340. : parse_node(parse_node), function_id(function_id) {}
  341. explicit Entry(Parse::NodeId parse_node, SemIR::ClassId class_id)
  342. : parse_node(parse_node), class_id(class_id) {}
  343. explicit Entry(Parse::NodeId parse_node, SemIR::InterfaceId interface_id)
  344. : parse_node(parse_node), interface_id(interface_id) {}
  345. explicit Entry(Parse::NodeId parse_node, SemIR::NameId name_id)
  346. : parse_node(parse_node), name_id(name_id) {}
  347. explicit Entry(Parse::NodeId parse_node, SemIR::TypeId type_id)
  348. : parse_node(parse_node), type_id(type_id) {}
  349. // Returns the appropriate ID basaed on type.
  350. template <typename T>
  351. auto id() -> T& {
  352. if constexpr (std::is_same<T, SemIR::InstId>()) {
  353. return inst_id;
  354. }
  355. if constexpr (std::is_same<T, SemIR::InstBlockId>()) {
  356. return inst_block_id;
  357. }
  358. if constexpr (std::is_same<T, SemIR::FunctionId>()) {
  359. return function_id;
  360. }
  361. if constexpr (std::is_same<T, SemIR::ClassId>()) {
  362. return class_id;
  363. }
  364. if constexpr (std::is_same<T, SemIR::InterfaceId>()) {
  365. return interface_id;
  366. }
  367. if constexpr (std::is_same<T, SemIR::NameId>()) {
  368. return name_id;
  369. }
  370. if constexpr (std::is_same<T, SemIR::TypeId>()) {
  371. return type_id;
  372. }
  373. }
  374. // The parse node associated with the stack entry.
  375. Parse::NodeId parse_node;
  376. // The entries will evaluate as invalid if and only if they're a solo
  377. // parse_node. Invalid is used instead of optional to save space.
  378. //
  379. // A discriminator isn't needed because the caller can determine which field
  380. // is used based on the Parse::NodeKind.
  381. union {
  382. SemIR::InstId inst_id;
  383. SemIR::InstBlockId inst_block_id;
  384. SemIR::FunctionId function_id;
  385. SemIR::ClassId class_id;
  386. SemIR::InterfaceId interface_id;
  387. SemIR::NameId name_id;
  388. SemIR::TypeId type_id;
  389. };
  390. };
  391. static_assert(sizeof(Entry) == 8, "Unexpected Entry size");
  392. // Translate a parse node category to the enum ID kind it should always
  393. // provide, if it is consistent.
  394. static constexpr auto ParseNodeCategoryToIdKind(Parse::NodeCategory category)
  395. -> std::optional<IdKind> {
  396. // TODO: Patterns should also produce an `InstId`, but currently
  397. // `TuplePattern` produces an `InstBlockId`.
  398. if (!!(category & Parse::NodeCategory::Expr)) {
  399. // Check for no consistent IdKind due to category with multiple bits set.
  400. if (!!(category & ~Parse::NodeCategory::Expr)) {
  401. return std::nullopt;
  402. }
  403. return IdKind::InstId;
  404. }
  405. if (!!(category & Parse::NodeCategory::MemberName)) {
  406. // Check for no consistent IdKind due to category with multiple bits set.
  407. if (!!(category & ~Parse::NodeCategory::MemberName)) {
  408. return std::nullopt;
  409. }
  410. return IdKind::NameId;
  411. }
  412. constexpr Parse::NodeCategory UnusedCategories =
  413. Parse::NodeCategory::Decl | Parse::NodeCategory::Statement |
  414. Parse::NodeCategory::Modifier;
  415. if (!!(category & UnusedCategories)) {
  416. // Check for no consistent IdKind due to category with multiple bits set.
  417. if (!!(category & ~UnusedCategories)) {
  418. return std::nullopt;
  419. }
  420. return IdKind::Unused;
  421. }
  422. return std::nullopt;
  423. }
  424. static constexpr auto ComputeIdKindTable()
  425. -> std::array<IdKind, Parse::NodeKind::ValidCount> {
  426. std::array<IdKind, Parse::NodeKind::ValidCount> table = {};
  427. auto to_id_kind =
  428. [](const Parse::NodeKind::Definition& node_kind) -> IdKind {
  429. if (auto from_category =
  430. NodeStack::ParseNodeCategoryToIdKind(node_kind.category())) {
  431. return *from_category;
  432. }
  433. switch (node_kind) {
  434. case Parse::NodeKind::Addr:
  435. case Parse::NodeKind::BindingPattern:
  436. case Parse::NodeKind::CallExprStart:
  437. case Parse::NodeKind::GenericBindingPattern:
  438. case Parse::NodeKind::IfExprThen:
  439. case Parse::NodeKind::ReturnType:
  440. case Parse::NodeKind::ShortCircuitOperandAnd:
  441. case Parse::NodeKind::ShortCircuitOperandOr:
  442. case Parse::NodeKind::StructFieldValue:
  443. case Parse::NodeKind::StructFieldType:
  444. case Parse::NodeKind::VariableInitializer:
  445. return IdKind::InstId;
  446. case Parse::NodeKind::IfCondition:
  447. case Parse::NodeKind::IfExprIf:
  448. case Parse::NodeKind::ImplForall:
  449. case Parse::NodeKind::ImplicitParamList:
  450. case Parse::NodeKind::TuplePattern:
  451. case Parse::NodeKind::WhileCondition:
  452. case Parse::NodeKind::WhileConditionStart:
  453. return IdKind::InstBlockId;
  454. case Parse::NodeKind::FunctionDefinitionStart:
  455. return IdKind::FunctionId;
  456. case Parse::NodeKind::ClassDefinitionStart:
  457. return IdKind::ClassId;
  458. case Parse::NodeKind::InterfaceDefinitionStart:
  459. return IdKind::InterfaceId;
  460. case Parse::NodeKind::IdentifierName:
  461. case Parse::NodeKind::SelfValueName:
  462. return IdKind::NameId;
  463. case Parse::NodeKind::ArrayExprSemi:
  464. case Parse::NodeKind::ClassIntroducer:
  465. case Parse::NodeKind::CodeBlockStart:
  466. case Parse::NodeKind::ExprOpenParen:
  467. case Parse::NodeKind::FunctionIntroducer:
  468. case Parse::NodeKind::IfStatementElse:
  469. case Parse::NodeKind::ImplicitParamListStart:
  470. case Parse::NodeKind::ImplIntroducer:
  471. case Parse::NodeKind::InterfaceIntroducer:
  472. case Parse::NodeKind::LetIntroducer:
  473. case Parse::NodeKind::QualifiedName:
  474. case Parse::NodeKind::ReturnedModifier:
  475. case Parse::NodeKind::ReturnStatementStart:
  476. case Parse::NodeKind::ReturnVarModifier:
  477. case Parse::NodeKind::StructLiteralOrStructTypeLiteralStart:
  478. case Parse::NodeKind::TuplePatternStart:
  479. case Parse::NodeKind::VariableIntroducer:
  480. return IdKind::SoloParseNode;
  481. default:
  482. return IdKind::Unused;
  483. }
  484. };
  485. #define CARBON_PARSE_NODE_KIND(Name) \
  486. table[Parse::Name::Kind.AsInt()] = to_id_kind(Parse::Name::Kind);
  487. #include "toolchain/parse/node_kind.def"
  488. return table;
  489. }
  490. // Lookup table to implement `ParseNodeKindToIdKind`. Initialized to the
  491. // return value of `ComputeIdKindTable()`.
  492. static const std::array<IdKind, Parse::NodeKind::ValidCount> IdKindTable;
  493. // Translate a parse node kind to the enum ID kind it should always provide.
  494. static constexpr auto ParseNodeKindToIdKind(Parse::NodeKind kind) -> IdKind {
  495. return IdKindTable[kind.AsInt()];
  496. }
  497. // Translates an ID type to the enum ID kind for comparison with
  498. // ParseNodeKindToIdKind.
  499. template <typename IdT>
  500. static constexpr auto IdTypeToIdKind() -> IdKind {
  501. if constexpr (std::is_same_v<IdT, SemIR::InstId>) {
  502. return IdKind::InstId;
  503. }
  504. if constexpr (std::is_same_v<IdT, SemIR::InstBlockId>) {
  505. return IdKind::InstBlockId;
  506. }
  507. if constexpr (std::is_same_v<IdT, SemIR::FunctionId>) {
  508. return IdKind::FunctionId;
  509. }
  510. if constexpr (std::is_same_v<IdT, SemIR::ClassId>) {
  511. return IdKind::ClassId;
  512. }
  513. if constexpr (std::is_same_v<IdT, SemIR::InterfaceId>) {
  514. return IdKind::InterfaceId;
  515. }
  516. if constexpr (std::is_same_v<IdT, SemIR::NameId>) {
  517. return IdKind::NameId;
  518. }
  519. if constexpr (std::is_same_v<IdT, SemIR::TypeId>) {
  520. return IdKind::TypeId;
  521. }
  522. }
  523. // Pops an entry.
  524. template <typename IdT>
  525. auto PopEntry() -> Entry {
  526. Entry back = stack_.pop_back_val();
  527. CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
  528. << parse_tree_->node_kind(back.parse_node) << " -> "
  529. << back.id<IdT>() << "\n";
  530. return back;
  531. }
  532. // Pops the top of the stack and returns the parse_node and the ID.
  533. template <typename IdT>
  534. auto PopWithParseNode() -> std::pair<Parse::NodeId, IdT> {
  535. Entry back = PopEntry<IdT>();
  536. RequireIdKind(parse_tree_->node_kind(back.parse_node),
  537. IdTypeToIdKind<IdT>());
  538. return {back.parse_node, back.id<IdT>()};
  539. }
  540. // Require a Parse::NodeKind be mapped to a particular IdKind.
  541. auto RequireIdKind(Parse::NodeKind parse_kind, IdKind id_kind) const -> void {
  542. CARBON_CHECK(ParseNodeKindToIdKind(parse_kind) == id_kind)
  543. << "Unexpected IdKind mapping for " << parse_kind;
  544. }
  545. // Require an entry to have the given Parse::NodeKind.
  546. template <const Parse::NodeKind& RequiredParseKind>
  547. auto RequireParseKind(Parse::NodeId parse_node) const -> void {
  548. auto actual_kind = parse_tree_->node_kind(parse_node);
  549. CARBON_CHECK(RequiredParseKind == actual_kind)
  550. << "Expected " << RequiredParseKind << ", found " << actual_kind;
  551. }
  552. // Require an entry to have the given Parse::NodeCategory.
  553. template <Parse::NodeCategory RequiredParseCategory>
  554. auto RequireParseCategory(Parse::NodeId parse_node) const -> void {
  555. auto kind = parse_tree_->node_kind(parse_node);
  556. CARBON_CHECK(!!(RequiredParseCategory & kind.category()))
  557. << "Expected " << RequiredParseCategory << ", found " << kind
  558. << " with category " << kind.category();
  559. }
  560. // The file's parse tree.
  561. const Parse::Tree* parse_tree_;
  562. // Whether to print verbose output.
  563. llvm::raw_ostream* vlog_stream_;
  564. // The actual stack.
  565. // PushEntry and PopEntry control modification in order to centralize
  566. // vlogging.
  567. llvm::SmallVector<Entry> stack_;
  568. };
  569. constexpr std::array<NodeStack::IdKind, Parse::NodeKind::ValidCount>
  570. NodeStack::IdKindTable = NodeStack::ComputeIdKindTable();
  571. inline auto NodeStack::PopExprWithParseNode()
  572. -> std::pair<Parse::AnyExprId, SemIR::InstId> {
  573. return PopWithParseNode<Parse::NodeCategory::Expr>();
  574. }
  575. } // namespace Carbon::Check
  576. #endif // CARBON_TOOLCHAIN_CHECK_NODE_STACK_H_