node_stack.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 "common/vlog.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "toolchain/parse/node_ids.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/id_kind.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. namespace Carbon::Check {
  15. // A non-discriminated union of ID types.
  16. class IdUnion {
  17. public:
  18. // The default constructor forms an invalid ID.
  19. explicit constexpr IdUnion() : index(IdBase::InvalidIndex) {}
  20. template <typename IdT>
  21. requires SemIR::IdKind::Contains<IdT>
  22. explicit constexpr IdUnion(IdT id) : index(id.index) {}
  23. using Kind = SemIR::IdKind::RawEnumType;
  24. // Returns the ID given its type.
  25. template <typename IdT>
  26. requires SemIR::IdKind::Contains<IdT>
  27. constexpr auto As() const -> IdT {
  28. return IdT(index);
  29. }
  30. // Returns the ID given its kind.
  31. template <SemIR::IdKind::RawEnumType K>
  32. constexpr auto As() const -> SemIR::IdKind::TypeFor<K> {
  33. return As<SemIR::IdKind::TypeFor<K>>();
  34. }
  35. // Translates an ID type to the enum ID kind. Returns Invalid if `IdT` isn't
  36. // a type that can be stored in this union.
  37. template <typename IdT>
  38. static constexpr auto KindFor() -> Kind {
  39. return SemIR::IdKind::For<IdT>;
  40. }
  41. private:
  42. decltype(IdBase::index) index;
  43. };
  44. // The stack of parse nodes representing the current state of a Check::Context.
  45. // Each parse node can have an associated id of some kind (instruction,
  46. // instruction block, function, class, ...).
  47. //
  48. // All pushes and pops will be vlogged.
  49. //
  50. // Pop APIs will run basic verification:
  51. //
  52. // - If receiving a Parse::NodeKind, verify that the node_id being popped has
  53. // that kind. Similarly, if receiving a Parse::NodeCategory, make sure the
  54. // of the popped node_id overlaps that category.
  55. // - Validates the kind of id data in the node based on the kind or category of
  56. // the node_id.
  57. //
  58. // These should be assumed API constraints unless otherwise mentioned on a
  59. // method. The main exception is PopAndIgnore, which doesn't do verification.
  60. class NodeStack {
  61. public:
  62. explicit NodeStack(const Parse::Tree& parse_tree,
  63. llvm::raw_ostream* vlog_stream)
  64. : parse_tree_(&parse_tree), vlog_stream_(vlog_stream) {}
  65. // Pushes a solo parse tree node onto the stack. Used when there is no
  66. // IR generated by the node.
  67. auto Push(Parse::NodeId node_id) -> void {
  68. auto kind = parse_tree_->node_kind(node_id);
  69. CARBON_CHECK(NodeKindToIdKind(kind) == Id::Kind::None)
  70. << "Parse kind expects an Id: " << kind;
  71. CARBON_VLOG() << "Node Push " << stack_.size() << ": " << kind
  72. << " -> <none>\n";
  73. CARBON_CHECK(stack_.size() < (1 << 20))
  74. << "Excessive stack size: likely infinite loop";
  75. stack_.push_back(Entry{node_id, Id()});
  76. }
  77. // Pushes a parse tree node onto the stack with an ID.
  78. template <typename IdT>
  79. auto Push(Parse::NodeId node_id, IdT id) -> void {
  80. auto kind = parse_tree_->node_kind(node_id);
  81. CARBON_CHECK(NodeKindToIdKind(kind) == Id::KindFor<IdT>())
  82. << "Parse kind expected a different IdT: " << kind << " -> " << id
  83. << "\n";
  84. CARBON_CHECK(id.is_valid())
  85. << "Push called with invalid id: " << parse_tree_->node_kind(node_id);
  86. CARBON_VLOG() << "Node Push " << stack_.size() << ": " << kind << " -> "
  87. << id << "\n";
  88. CARBON_CHECK(stack_.size() < (1 << 20))
  89. << "Excessive stack size: likely infinite loop";
  90. stack_.push_back(Entry{node_id, Id(id)});
  91. }
  92. // Returns whether there is a node of the specified kind on top of the stack.
  93. auto PeekIs(Parse::NodeKind kind) const -> bool {
  94. return !stack_.empty() && PeekNodeKind() == kind;
  95. }
  96. // Returns whether there is a node of the specified kind on top of the stack.
  97. // Templated for consistency with other functions taking a parse node kind.
  98. template <const Parse::NodeKind& RequiredParseKind>
  99. auto PeekIs() const -> bool {
  100. return PeekIs(RequiredParseKind);
  101. }
  102. // Returns whether the node on the top of the stack has an overlapping
  103. // category.
  104. auto PeekIs(Parse::NodeCategory category) const -> bool {
  105. return !stack_.empty() && !!(PeekNodeKind().category() & category);
  106. }
  107. // Returns whether the node on the top of the stack has an overlapping
  108. // category. Templated for consistency with other functions taking a parse
  109. // node category.
  110. template <Parse::NodeCategory RequiredParseCategory>
  111. auto PeekIs() const -> bool {
  112. return PeekIs(RequiredParseCategory);
  113. }
  114. // Returns whether there is a name on top of the stack.
  115. auto PeekIsName() const -> bool {
  116. return !stack_.empty() &&
  117. NodeKindToIdKind(PeekNodeKind()) == Id::KindFor<SemIR::NameId>();
  118. }
  119. // Returns whether the *next* node on the stack is a given kind. This doesn't
  120. // have the breadth of support versus other Peek functions because it's
  121. // expected to be used in narrow circumstances when determining how to treat
  122. // the *current* top of the stack.
  123. template <const Parse::NodeKind& RequiredParseKind>
  124. auto PeekNextIs() const -> bool {
  125. CARBON_CHECK(stack_.size() >= 2);
  126. return parse_tree_->node_kind(stack_[stack_.size() - 2].node_id) ==
  127. RequiredParseKind;
  128. }
  129. // Pops the top of the stack without any verification.
  130. auto PopAndIgnore() -> void {
  131. Entry back = stack_.pop_back_val();
  132. CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
  133. << parse_tree_->node_kind(back.node_id) << " -> <ignored>\n";
  134. }
  135. // Pops the top of the stack and returns the node_id.
  136. template <const Parse::NodeKind& RequiredParseKind>
  137. auto PopForSoloNodeId() -> Parse::NodeIdForKind<RequiredParseKind> {
  138. Entry back = PopEntry<SemIR::InstId>();
  139. RequireIdKind(RequiredParseKind, Id::Kind::None);
  140. RequireParseKind<RequiredParseKind>(back.node_id);
  141. return Parse::NodeIdForKind<RequiredParseKind>(back.node_id);
  142. }
  143. // Pops the top of the stack if it is the given kind, and returns the
  144. // node_id. Otherwise, returns std::nullopt.
  145. template <const Parse::NodeKind& RequiredParseKind>
  146. auto PopForSoloNodeIdIf()
  147. -> std::optional<Parse::NodeIdForKind<RequiredParseKind>> {
  148. if (PeekIs<RequiredParseKind>()) {
  149. return PopForSoloNodeId<RequiredParseKind>();
  150. }
  151. return std::nullopt;
  152. }
  153. // Pops the top of the stack.
  154. template <const Parse::NodeKind& RequiredParseKind>
  155. auto PopAndDiscardSoloNodeId() -> void {
  156. PopForSoloNodeId<RequiredParseKind>();
  157. }
  158. // Pops the top of the stack if it is the given kind. Returns `true` if a node
  159. // was popped.
  160. template <const Parse::NodeKind& RequiredParseKind>
  161. auto PopAndDiscardSoloNodeIdIf() -> bool {
  162. if (!PeekIs<RequiredParseKind>()) {
  163. return false;
  164. }
  165. PopForSoloNodeId<RequiredParseKind>();
  166. return true;
  167. }
  168. // Pops an expression from the top of the stack and returns the node_id and
  169. // the ID.
  170. auto PopExprWithNodeId() -> std::pair<Parse::AnyExprId, SemIR::InstId>;
  171. // Pops a pattern from the top of the stack and returns the node_id and
  172. // the ID.
  173. auto PopPatternWithNodeId() -> std::pair<Parse::NodeId, SemIR::InstId> {
  174. return PopWithNodeId<SemIR::InstId>();
  175. }
  176. // Pops a name from the top of the stack and returns the node_id and
  177. // the ID.
  178. auto PopNameWithNodeId() -> std::pair<Parse::NodeId, SemIR::NameId> {
  179. return PopWithNodeId<SemIR::NameId>();
  180. }
  181. // Pops the top of the stack and returns the node_id and the ID.
  182. template <const Parse::NodeKind& RequiredParseKind>
  183. auto PopWithNodeId() -> auto {
  184. auto id = Peek<RequiredParseKind>();
  185. Parse::NodeIdForKind<RequiredParseKind> node_id(
  186. stack_.pop_back_val().node_id);
  187. return std::make_pair(node_id, id);
  188. }
  189. // Pops the top of the stack and returns the node_id and the ID.
  190. template <Parse::NodeCategory RequiredParseCategory>
  191. auto PopWithNodeId() -> auto {
  192. auto id = Peek<RequiredParseCategory>();
  193. Parse::NodeIdInCategory<RequiredParseCategory> node_id(
  194. stack_.pop_back_val().node_id);
  195. return std::make_pair(node_id, id);
  196. }
  197. // Pops an expression from the top of the stack and returns the ID.
  198. // Expressions always map Parse::NodeCategory::Expr nodes to SemIR::InstId.
  199. auto PopExpr() -> SemIR::InstId { return PopExprWithNodeId().second; }
  200. // Pops a pattern from the top of the stack and returns the ID.
  201. // Patterns map multiple Parse::NodeKinds to SemIR::InstId always.
  202. auto PopPattern() -> SemIR::InstId { return PopPatternWithNodeId().second; }
  203. // Pops a name from the top of the stack and returns the ID.
  204. auto PopName() -> SemIR::NameId { return PopNameWithNodeId().second; }
  205. // Pops the top of the stack and returns the ID.
  206. template <const Parse::NodeKind& RequiredParseKind>
  207. auto Pop() -> auto {
  208. return PopWithNodeId<RequiredParseKind>().second;
  209. }
  210. // Pops the top of the stack and returns the ID.
  211. template <Parse::NodeCategory RequiredParseCategory>
  212. auto Pop() -> auto {
  213. return PopWithNodeId<RequiredParseCategory>().second;
  214. }
  215. // Pops the top of the stack and returns the ID.
  216. template <typename IdT>
  217. auto Pop() -> IdT {
  218. return PopWithNodeId<IdT>().second;
  219. }
  220. // Pops the top of the stack if it has the given kind, and returns the ID.
  221. // Otherwise returns std::nullopt.
  222. template <const Parse::NodeKind& RequiredParseKind>
  223. auto PopIf() -> std::optional<decltype(Pop<RequiredParseKind>())> {
  224. if (PeekIs<RequiredParseKind>()) {
  225. return Pop<RequiredParseKind>();
  226. }
  227. return std::nullopt;
  228. }
  229. // Pops the top of the stack if it has the given category, and returns the ID.
  230. // Otherwise returns std::nullopt.
  231. template <Parse::NodeCategory RequiredParseCategory>
  232. auto PopIf() -> std::optional<decltype(Pop<RequiredParseCategory>())> {
  233. if (PeekIs<RequiredParseCategory>()) {
  234. return Pop<RequiredParseCategory>();
  235. }
  236. return std::nullopt;
  237. }
  238. // Pops the top of the stack and returns the node_id and the ID if it is
  239. // of the specified kind.
  240. template <const Parse::NodeKind& RequiredParseKind>
  241. auto PopWithNodeIdIf() -> std::pair<Parse::NodeIdForKind<RequiredParseKind>,
  242. decltype(PopIf<RequiredParseKind>())> {
  243. if (!PeekIs<RequiredParseKind>()) {
  244. return {Parse::NodeId::Invalid, std::nullopt};
  245. }
  246. return PopWithNodeId<RequiredParseKind>();
  247. }
  248. // Pops the top of the stack and returns the node_id and the ID if it is
  249. // of the specified category.
  250. template <Parse::NodeCategory RequiredParseCategory>
  251. auto PopWithNodeIdIf()
  252. -> std::pair<Parse::NodeIdInCategory<RequiredParseCategory>,
  253. decltype(PopIf<RequiredParseCategory>())> {
  254. if (!PeekIs<RequiredParseCategory>()) {
  255. return {Parse::NodeId::Invalid, std::nullopt};
  256. }
  257. return PopWithNodeId<RequiredParseCategory>();
  258. }
  259. // Peeks at the parse node of the top of the node stack.
  260. auto PeekNodeId() const -> Parse::NodeId { return stack_.back().node_id; }
  261. // Peeks at the kind of the parse node of the top of the node stack.
  262. auto PeekNodeKind() const -> Parse::NodeKind {
  263. return parse_tree_->node_kind(PeekNodeId());
  264. }
  265. // Peeks at the ID associated with the top of the name stack.
  266. template <const Parse::NodeKind& RequiredParseKind>
  267. auto Peek() const -> auto {
  268. Entry back = stack_.back();
  269. RequireParseKind<RequiredParseKind>(back.node_id);
  270. constexpr Id::Kind RequiredIdKind = NodeKindToIdKind(RequiredParseKind);
  271. return Peek<RequiredIdKind>();
  272. }
  273. // Peeks at the ID associated with the top of the name stack.
  274. template <Parse::NodeCategory RequiredParseCategory>
  275. auto Peek() const -> auto {
  276. Entry back = stack_.back();
  277. RequireParseCategory<RequiredParseCategory>(back.node_id);
  278. constexpr std::optional<Id::Kind> RequiredIdKind =
  279. NodeCategoryToIdKind(RequiredParseCategory, false);
  280. static_assert(RequiredIdKind.has_value());
  281. return Peek<*RequiredIdKind>();
  282. }
  283. // Prints the stack for a stack dump.
  284. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  285. auto empty() const -> bool { return stack_.empty(); }
  286. auto size() const -> size_t { return stack_.size(); }
  287. protected:
  288. // An ID that can be associated with a parse node.
  289. //
  290. // Each parse node kind has a corresponding Id::Kind indicating which kind of
  291. // ID is stored, computed by NodeKindToIdKind. Id::Kind::None indicates
  292. // that the parse node has no associated ID, in which case the *SoloNodeId
  293. // functions should be used to push and pop it. Id::Kind::Invalid indicates
  294. // that the parse node should not appear in the node stack at all.
  295. using Id = IdUnion;
  296. // An entry in stack_.
  297. struct Entry {
  298. // The parse node associated with the stack entry.
  299. Parse::NodeId node_id;
  300. // The ID associated with this parse node. The kind of ID is determined by
  301. // the kind of the parse node, so a separate discriminiator is not needed.
  302. Id id;
  303. };
  304. static_assert(sizeof(Entry) == 8, "Unexpected Entry size");
  305. // Translate a parse node category to the enum ID kind it should always
  306. // provide, if it is consistent.
  307. static constexpr auto NodeCategoryToIdKind(Parse::NodeCategory category,
  308. bool for_node_kind)
  309. -> std::optional<Id::Kind> {
  310. std::optional<Id::Kind> result;
  311. auto set_id_if_category_is = [&](Parse::NodeCategory cat, Id::Kind kind) {
  312. if (!!(category & cat)) {
  313. // Check for no consistent Id::Kind due to category with multiple bits
  314. // set. When computing the Id::Kind for a node kind, a partial category
  315. // match is OK, so long as we don't match two inconsistent categories.
  316. // When computing the Id::Kind for a category query, the query can't
  317. // have any extra bits set or we could be popping a node that is not in
  318. // this category.
  319. if (for_node_kind ? result.has_value() : !!(category & ~cat)) {
  320. result = Id::Kind::Invalid;
  321. } else {
  322. result = kind;
  323. }
  324. }
  325. };
  326. // TODO: Patterns should also produce an `InstId`, but currently
  327. // `TuplePattern` produces an `InstBlockId`.
  328. set_id_if_category_is(Parse::NodeCategory::Expr,
  329. Id::KindFor<SemIR::InstId>());
  330. set_id_if_category_is(Parse::NodeCategory::MemberName,
  331. Id::KindFor<SemIR::NameId>());
  332. set_id_if_category_is(Parse::NodeCategory::ImplAs,
  333. Id::KindFor<SemIR::TypeId>());
  334. set_id_if_category_is(Parse::NodeCategory::Decl |
  335. Parse::NodeCategory::Statement |
  336. Parse::NodeCategory::Modifier,
  337. Id::Kind::None);
  338. return result;
  339. }
  340. using IdKindTableType = std::array<Id::Kind, Parse::NodeKind::ValidCount>;
  341. // Lookup table to implement `NodeKindToIdKind`. Initialized to the
  342. // return value of `ComputeIdKindTable()`.
  343. static const IdKindTableType IdKindTable;
  344. static constexpr auto ComputeIdKindTable() -> IdKindTableType {
  345. IdKindTableType table = {};
  346. auto to_id_kind =
  347. [](const Parse::NodeKind::Definition& node_kind) -> Id::Kind {
  348. if (auto from_category =
  349. NodeCategoryToIdKind(node_kind.category(), true)) {
  350. return *from_category;
  351. }
  352. switch (node_kind) {
  353. case Parse::NodeKind::Addr:
  354. case Parse::NodeKind::BindingPattern:
  355. case Parse::NodeKind::CallExprStart:
  356. case Parse::NodeKind::CompileTimeBindingPattern:
  357. case Parse::NodeKind::IfExprThen:
  358. case Parse::NodeKind::ReturnType:
  359. case Parse::NodeKind::ShortCircuitOperandAnd:
  360. case Parse::NodeKind::ShortCircuitOperandOr:
  361. case Parse::NodeKind::StructField:
  362. case Parse::NodeKind::StructTypeField:
  363. return Id::KindFor<SemIR::InstId>();
  364. case Parse::NodeKind::IfCondition:
  365. case Parse::NodeKind::IfExprIf:
  366. case Parse::NodeKind::ImplForall:
  367. case Parse::NodeKind::ImplicitParamList:
  368. case Parse::NodeKind::TuplePattern:
  369. case Parse::NodeKind::WhileCondition:
  370. case Parse::NodeKind::WhileConditionStart:
  371. return Id::KindFor<SemIR::InstBlockId>();
  372. case Parse::NodeKind::FunctionDefinitionStart:
  373. case Parse::NodeKind::BuiltinFunctionDefinitionStart:
  374. return Id::KindFor<SemIR::FunctionId>();
  375. case Parse::NodeKind::ClassDefinitionStart:
  376. return Id::KindFor<SemIR::ClassId>();
  377. case Parse::NodeKind::InterfaceDefinitionStart:
  378. return Id::KindFor<SemIR::InterfaceId>();
  379. case Parse::NodeKind::ImplDefinitionStart:
  380. return Id::KindFor<SemIR::ImplId>();
  381. case Parse::NodeKind::SelfValueName:
  382. return Id::KindFor<SemIR::NameId>();
  383. case Parse::NodeKind::ArrayExprSemi:
  384. case Parse::NodeKind::BuiltinName:
  385. case Parse::NodeKind::ClassIntroducer:
  386. case Parse::NodeKind::CodeBlockStart:
  387. case Parse::NodeKind::FunctionIntroducer:
  388. case Parse::NodeKind::IfStatementElse:
  389. case Parse::NodeKind::ImplicitParamListStart:
  390. case Parse::NodeKind::ImplIntroducer:
  391. case Parse::NodeKind::InterfaceIntroducer:
  392. case Parse::NodeKind::LetInitializer:
  393. case Parse::NodeKind::LetIntroducer:
  394. case Parse::NodeKind::QualifiedName:
  395. case Parse::NodeKind::ReturnedModifier:
  396. case Parse::NodeKind::ReturnStatementStart:
  397. case Parse::NodeKind::ReturnVarModifier:
  398. case Parse::NodeKind::StructLiteralStart:
  399. case Parse::NodeKind::StructTypeLiteralStart:
  400. case Parse::NodeKind::TupleLiteralStart:
  401. case Parse::NodeKind::TuplePatternStart:
  402. case Parse::NodeKind::VariableInitializer:
  403. case Parse::NodeKind::VariableIntroducer:
  404. return Id::Kind::None;
  405. default:
  406. return Id::Kind::Invalid;
  407. }
  408. };
  409. #define CARBON_PARSE_NODE_KIND(Name) \
  410. table[Parse::Name::Kind.AsInt()] = to_id_kind(Parse::Name::Kind);
  411. #include "toolchain/parse/node_kind.def"
  412. return table;
  413. }
  414. // Translate a parse node kind to the enum ID kind it should always provide.
  415. static constexpr auto NodeKindToIdKind(Parse::NodeKind kind) -> Id::Kind {
  416. return IdKindTable[kind.AsInt()];
  417. }
  418. // Peeks at the ID associated with the top of the name stack.
  419. template <Id::Kind RequiredIdKind>
  420. auto Peek() const -> auto {
  421. Id id = stack_.back().id;
  422. return id.As<RequiredIdKind>();
  423. }
  424. // Pops an entry.
  425. template <typename IdT>
  426. auto PopEntry() -> Entry {
  427. Entry back = stack_.pop_back_val();
  428. CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
  429. << parse_tree_->node_kind(back.node_id) << " -> "
  430. << back.id.template As<IdT>() << "\n";
  431. return back;
  432. }
  433. // Pops the top of the stack and returns the node_id and the ID.
  434. template <typename IdT>
  435. auto PopWithNodeId() -> std::pair<Parse::NodeId, IdT> {
  436. Entry back = PopEntry<IdT>();
  437. RequireIdKind(parse_tree_->node_kind(back.node_id), Id::KindFor<IdT>());
  438. return {back.node_id, back.id.template As<IdT>()};
  439. }
  440. // Require a Parse::NodeKind be mapped to a particular Id::Kind.
  441. auto RequireIdKind(Parse::NodeKind parse_kind, Id::Kind id_kind) const
  442. -> void {
  443. CARBON_CHECK(NodeKindToIdKind(parse_kind) == id_kind)
  444. << "Unexpected Id::Kind mapping for " << parse_kind;
  445. }
  446. // Require an entry to have the given Parse::NodeKind.
  447. template <const Parse::NodeKind& RequiredParseKind>
  448. auto RequireParseKind(Parse::NodeId node_id) const -> void {
  449. auto actual_kind = parse_tree_->node_kind(node_id);
  450. CARBON_CHECK(RequiredParseKind == actual_kind)
  451. << "Expected " << RequiredParseKind << ", found " << actual_kind;
  452. }
  453. // Require an entry to have the given Parse::NodeCategory.
  454. template <Parse::NodeCategory RequiredParseCategory>
  455. auto RequireParseCategory(Parse::NodeId node_id) const -> void {
  456. auto kind = parse_tree_->node_kind(node_id);
  457. CARBON_CHECK(!!(RequiredParseCategory & kind.category()))
  458. << "Expected " << RequiredParseCategory << ", found " << kind
  459. << " with category " << kind.category();
  460. }
  461. // The file's parse tree.
  462. const Parse::Tree* parse_tree_;
  463. // Whether to print verbose output.
  464. llvm::raw_ostream* vlog_stream_;
  465. // The actual stack.
  466. // PushEntry and PopEntry control modification in order to centralize
  467. // vlogging.
  468. llvm::SmallVector<Entry> stack_;
  469. };
  470. constexpr NodeStack::IdKindTableType NodeStack::IdKindTable =
  471. ComputeIdKindTable();
  472. inline auto NodeStack::PopExprWithNodeId()
  473. -> std::pair<Parse::AnyExprId, SemIR::InstId> {
  474. return PopWithNodeId<Parse::NodeCategory::Expr>();
  475. }
  476. } // namespace Carbon::Check
  477. #endif // CARBON_TOOLCHAIN_CHECK_NODE_STACK_H_