node_stack.h 21 KB

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