node_stack.h 21 KB

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