semantics_node_stack.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_SEMANTICS_SEMANTICS_NODE_STACK_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_STACK_H_
  6. #include <type_traits>
  7. #include "common/vlog.h"
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "toolchain/parser/parse_node_kind.h"
  10. #include "toolchain/parser/parse_tree.h"
  11. #include "toolchain/semantics/semantics_node.h"
  12. namespace Carbon::Check {
  13. // Wraps the stack of nodes for Context.
  14. //
  15. // All pushes and pops will be vlogged.
  16. //
  17. // Pop APIs will run basic verification:
  18. //
  19. // - If receiving a pop_parse_kind, verify that the parse_node being popped is
  20. // of pop_parse_kind.
  21. // - Validates presence of node_id based on whether it's a solo
  22. // parse_node.
  23. //
  24. // These should be assumed API constraints unless otherwise mentioned on a
  25. // method. The main exception is PopAndIgnore, which doesn't do verification.
  26. class NodeStack {
  27. public:
  28. explicit NodeStack(const Parse::Tree& parse_tree,
  29. llvm::raw_ostream* vlog_stream)
  30. : parse_tree_(&parse_tree), vlog_stream_(vlog_stream) {}
  31. // Pushes a solo parse tree node onto the stack. Used when there is no
  32. // IR generated by the node.
  33. auto Push(Parse::Node parse_node) -> void {
  34. CARBON_CHECK(ParseNodeKindToIdKind(parse_tree_->node_kind(parse_node)) ==
  35. IdKind::SoloParseNode)
  36. << "Parse kind expects an Id: " << parse_tree_->node_kind(parse_node);
  37. CARBON_VLOG() << "Node Push " << stack_.size() << ": "
  38. << parse_tree_->node_kind(parse_node) << " -> <none>\n";
  39. CARBON_CHECK(stack_.size() < (1 << 20))
  40. << "Excessive stack size: likely infinite loop";
  41. stack_.push_back(Entry(parse_node, SemIR::NodeId::Invalid));
  42. }
  43. // Pushes a parse tree node onto the stack with an ID.
  44. template <typename IdT>
  45. auto Push(Parse::Node parse_node, IdT id) -> void {
  46. CARBON_CHECK(ParseNodeKindToIdKind(parse_tree_->node_kind(parse_node)) ==
  47. IdTypeToIdKind<IdT>())
  48. << "Parse kind expected a different IdT: "
  49. << parse_tree_->node_kind(parse_node) << " -> " << id << "\n";
  50. CARBON_CHECK(id.is_valid()) << "Push called with invalid id: "
  51. << parse_tree_->node_kind(parse_node);
  52. CARBON_VLOG() << "Node Push " << stack_.size() << ": "
  53. << parse_tree_->node_kind(parse_node) << " -> " << id << "\n";
  54. CARBON_CHECK(stack_.size() < (1 << 20))
  55. << "Excessive stack size: likely infinite loop";
  56. stack_.push_back(Entry(parse_node, id));
  57. }
  58. // Pops the top of the stack without any verification.
  59. auto PopAndIgnore() -> void { PopEntry<SemIR::NodeId>(); }
  60. // Pops the top of the stack and returns the parse_node.
  61. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  62. auto PopForSoloParseNode() -> Parse::Node {
  63. Entry back = PopEntry<SemIR::NodeId>();
  64. RequireIdKind(Parse::NodeKind::Create(RequiredParseKind),
  65. IdKind::SoloParseNode);
  66. RequireParseKind<RequiredParseKind>(back.parse_node);
  67. return back.parse_node;
  68. }
  69. // Pops the top of the stack.
  70. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  71. auto PopAndDiscardSoloParseNode() -> void {
  72. PopForSoloParseNode<RequiredParseKind>();
  73. }
  74. // Pops an expression from the top of the stack and returns the parse_node and
  75. // the ID.
  76. auto PopExpressionWithParseNode() -> std::pair<Parse::Node, SemIR::NodeId> {
  77. return PopWithParseNode<SemIR::NodeId>();
  78. }
  79. // Pops the top of the stack and returns the parse_node and the ID.
  80. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  81. auto PopWithParseNode() -> auto {
  82. constexpr IdKind RequiredIdKind =
  83. ParseNodeKindToIdKind(Parse::NodeKind::Create(RequiredParseKind));
  84. if constexpr (RequiredIdKind == IdKind::NodeId) {
  85. auto back = PopWithParseNode<SemIR::NodeId>();
  86. RequireParseKind<RequiredParseKind>(back.first);
  87. return back;
  88. }
  89. if constexpr (RequiredIdKind == IdKind::NodeBlockId) {
  90. auto back = PopWithParseNode<SemIR::NodeBlockId>();
  91. RequireParseKind<RequiredParseKind>(back.first);
  92. return back;
  93. }
  94. if constexpr (RequiredIdKind == IdKind::FunctionId) {
  95. auto back = PopWithParseNode<SemIR::FunctionId>();
  96. RequireParseKind<RequiredParseKind>(back.first);
  97. return back;
  98. }
  99. if constexpr (RequiredIdKind == IdKind::StringId) {
  100. auto back = PopWithParseNode<SemIR::StringId>();
  101. RequireParseKind<RequiredParseKind>(back.first);
  102. return back;
  103. }
  104. if constexpr (RequiredIdKind == IdKind::TypeId) {
  105. auto back = PopWithParseNode<SemIR::TypeId>();
  106. RequireParseKind<RequiredParseKind>(back.first);
  107. return back;
  108. }
  109. CARBON_FATAL() << "Unpoppable IdKind for parse kind: "
  110. << Parse::NodeKind::Create(RequiredParseKind)
  111. << "; see value in ParseNodeKindToIdKind";
  112. }
  113. // Pops an expression from the top of the stack and returns the ID.
  114. // Expressions map multiple Parse::NodeKinds to SemIR::NodeId always.
  115. auto PopExpression() -> SemIR::NodeId {
  116. return PopExpressionWithParseNode().second;
  117. }
  118. // Pops the top of the stack and returns the ID.
  119. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  120. auto Pop() -> auto {
  121. return PopWithParseNode<RequiredParseKind>().second;
  122. }
  123. // Peeks at the parse_node of the top of the stack.
  124. auto PeekParseNode() -> Parse::Node { return stack_.back().parse_node; }
  125. // Peeks at the ID of the top of the stack.
  126. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  127. auto Peek() -> auto {
  128. Entry back = stack_.back();
  129. RequireParseKind<RequiredParseKind>(back.parse_node);
  130. constexpr IdKind RequiredIdKind =
  131. ParseNodeKindToIdKind(Parse::NodeKind::Create(RequiredParseKind));
  132. if constexpr (RequiredIdKind == IdKind::NodeId) {
  133. return back.id<SemIR::NodeId>();
  134. }
  135. if constexpr (RequiredIdKind == IdKind::NodeBlockId) {
  136. return back.id<SemIR::NodeBlockId>();
  137. }
  138. if constexpr (RequiredIdKind == IdKind::FunctionId) {
  139. return back.id<SemIR::FunctionId>();
  140. }
  141. if constexpr (RequiredIdKind == IdKind::StringId) {
  142. return back.id<SemIR::StringId>();
  143. }
  144. if constexpr (RequiredIdKind == IdKind::TypeId) {
  145. return back.id<SemIR::TypeId>();
  146. }
  147. CARBON_FATAL() << "Unpeekable IdKind for parse kind: "
  148. << Parse::NodeKind::Create(RequiredParseKind)
  149. << "; see value in ParseNodeKindToIdKind";
  150. }
  151. // Prints the stack for a stack dump.
  152. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  153. auto empty() const -> bool { return stack_.empty(); }
  154. auto size() const -> size_t { return stack_.size(); }
  155. private:
  156. // Possible associated ID types.
  157. enum class IdKind : int8_t {
  158. NodeId,
  159. NodeBlockId,
  160. FunctionId,
  161. StringId,
  162. TypeId,
  163. // No associated ID type.
  164. SoloParseNode,
  165. // Not expected in the node stack.
  166. Unused,
  167. };
  168. // An entry in stack_.
  169. struct Entry {
  170. explicit Entry(Parse::Node parse_node, SemIR::NodeId node_id)
  171. : parse_node(parse_node), node_id(node_id) {}
  172. explicit Entry(Parse::Node parse_node, SemIR::NodeBlockId node_block_id)
  173. : parse_node(parse_node), node_block_id(node_block_id) {}
  174. explicit Entry(Parse::Node parse_node, SemIR::FunctionId function_id)
  175. : parse_node(parse_node), function_id(function_id) {}
  176. explicit Entry(Parse::Node parse_node, SemIR::StringId name_id)
  177. : parse_node(parse_node), name_id(name_id) {}
  178. explicit Entry(Parse::Node parse_node, SemIR::TypeId type_id)
  179. : parse_node(parse_node), type_id(type_id) {}
  180. // Returns the appropriate ID basaed on type.
  181. template <typename T>
  182. auto id() -> T& {
  183. if constexpr (std::is_same<T, SemIR::NodeId>()) {
  184. return node_id;
  185. }
  186. if constexpr (std::is_same<T, SemIR::NodeBlockId>()) {
  187. return node_block_id;
  188. }
  189. if constexpr (std::is_same<T, SemIR::FunctionId>()) {
  190. return function_id;
  191. }
  192. if constexpr (std::is_same<T, SemIR::StringId>()) {
  193. return name_id;
  194. }
  195. if constexpr (std::is_same<T, SemIR::TypeId>()) {
  196. return type_id;
  197. }
  198. }
  199. // The node associated with the stack entry.
  200. Parse::Node parse_node;
  201. // The entries will evaluate as invalid if and only if they're a solo
  202. // parse_node. Invalid is used instead of optional to save space.
  203. //
  204. // A discriminator isn't needed because the caller can determine which field
  205. // is used based on the Parse::NodeKind.
  206. union {
  207. SemIR::NodeId node_id;
  208. SemIR::NodeBlockId node_block_id;
  209. SemIR::FunctionId function_id;
  210. SemIR::StringId name_id;
  211. SemIR::TypeId type_id;
  212. };
  213. };
  214. static_assert(sizeof(Entry) == 8, "Unexpected Entry size");
  215. // Translate a parse node kind to the enum ID kind it should always provide.
  216. static constexpr auto ParseNodeKindToIdKind(Parse::NodeKind kind) -> IdKind {
  217. switch (kind) {
  218. case Parse::NodeKind::ArrayExpression:
  219. case Parse::NodeKind::CallExpression:
  220. case Parse::NodeKind::CallExpressionStart:
  221. case Parse::NodeKind::IfExpressionElse:
  222. case Parse::NodeKind::IndexExpression:
  223. case Parse::NodeKind::InfixOperator:
  224. case Parse::NodeKind::Literal:
  225. case Parse::NodeKind::MemberAccessExpression:
  226. case Parse::NodeKind::NameExpression:
  227. case Parse::NodeKind::ParenExpression:
  228. case Parse::NodeKind::PatternBinding:
  229. case Parse::NodeKind::PostfixOperator:
  230. case Parse::NodeKind::PrefixOperator:
  231. case Parse::NodeKind::ReturnType:
  232. case Parse::NodeKind::ShortCircuitOperand:
  233. case Parse::NodeKind::StructFieldValue:
  234. case Parse::NodeKind::StructLiteral:
  235. case Parse::NodeKind::StructFieldType:
  236. case Parse::NodeKind::StructTypeLiteral:
  237. case Parse::NodeKind::TupleLiteral:
  238. return IdKind::NodeId;
  239. case Parse::NodeKind::IfExpressionThen:
  240. case Parse::NodeKind::IfStatementElse:
  241. case Parse::NodeKind::ParameterList:
  242. return IdKind::NodeBlockId;
  243. case Parse::NodeKind::FunctionDefinitionStart:
  244. return IdKind::FunctionId;
  245. case Parse::NodeKind::Name:
  246. return IdKind::StringId;
  247. case Parse::NodeKind::ArrayExpressionSemi:
  248. case Parse::NodeKind::CodeBlockStart:
  249. case Parse::NodeKind::FunctionIntroducer:
  250. case Parse::NodeKind::IfCondition:
  251. case Parse::NodeKind::IfExpressionIf:
  252. case Parse::NodeKind::ParameterListStart:
  253. case Parse::NodeKind::ParenExpressionOrTupleLiteralStart:
  254. case Parse::NodeKind::QualifiedDeclaration:
  255. case Parse::NodeKind::ReturnStatementStart:
  256. case Parse::NodeKind::StructLiteralOrStructTypeLiteralStart:
  257. case Parse::NodeKind::VariableInitializer:
  258. case Parse::NodeKind::VariableIntroducer:
  259. return IdKind::SoloParseNode;
  260. default:
  261. return IdKind::Unused;
  262. }
  263. }
  264. // Translates an ID type to the enum ID kind for comparison with
  265. // ParseNodeKindToIdKind.
  266. template <typename IdT>
  267. static constexpr auto IdTypeToIdKind() -> IdKind {
  268. if constexpr (std::is_same_v<IdT, SemIR::NodeId>) {
  269. return IdKind::NodeId;
  270. }
  271. if constexpr (std::is_same_v<IdT, SemIR::NodeBlockId>) {
  272. return IdKind::NodeBlockId;
  273. }
  274. if constexpr (std::is_same_v<IdT, SemIR::FunctionId>) {
  275. return IdKind::FunctionId;
  276. }
  277. if constexpr (std::is_same_v<IdT, SemIR::StringId>) {
  278. return IdKind::StringId;
  279. }
  280. if constexpr (std::is_same_v<IdT, SemIR::TypeId>) {
  281. return IdKind::TypeId;
  282. }
  283. }
  284. // Pops an entry.
  285. template <typename IdT>
  286. auto PopEntry() -> Entry {
  287. Entry back = stack_.pop_back_val();
  288. CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
  289. << parse_tree_->node_kind(back.parse_node) << " -> "
  290. << back.id<IdT>() << "\n";
  291. return back;
  292. }
  293. // Pops the top of the stack and returns the parse_node and the ID.
  294. template <typename IdT>
  295. auto PopWithParseNode() -> std::pair<Parse::Node, IdT> {
  296. Entry back = PopEntry<IdT>();
  297. RequireIdKind(parse_tree_->node_kind(back.parse_node),
  298. IdTypeToIdKind<IdT>());
  299. return {back.parse_node, back.id<IdT>()};
  300. }
  301. // Require a Parse::NodeKind be mapped to a particular IdKind.
  302. auto RequireIdKind(Parse::NodeKind parse_kind, IdKind id_kind) -> void {
  303. CARBON_CHECK(ParseNodeKindToIdKind(parse_kind) == id_kind)
  304. << "Unexpected IdKind mapping for " << parse_kind;
  305. }
  306. // Require an entry to have the given Parse::NodeKind.
  307. template <Parse::NodeKind::RawEnumType RequiredParseKind>
  308. auto RequireParseKind(Parse::Node parse_node) -> void {
  309. auto actual_kind = parse_tree_->node_kind(parse_node);
  310. CARBON_CHECK(RequiredParseKind == actual_kind)
  311. << "Expected " << Parse::NodeKind::Create(RequiredParseKind)
  312. << ", found " << actual_kind;
  313. }
  314. // The file's parse tree.
  315. const Parse::Tree* parse_tree_;
  316. // Whether to print verbose output.
  317. llvm::raw_ostream* vlog_stream_;
  318. // The actual stack.
  319. // PushEntry and PopEntry control modification in order to centralize
  320. // vlogging.
  321. llvm::SmallVector<Entry> stack_;
  322. };
  323. } // namespace Carbon::Check
  324. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_STACK_H_