node_stack.h 21 KB

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