semantics_node_stack.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "llvm/ADT/SmallVector.h"
  7. #include "toolchain/parser/parse_node_kind.h"
  8. #include "toolchain/parser/parse_tree.h"
  9. #include "toolchain/semantics/semantics_node.h"
  10. namespace Carbon {
  11. // Wraps the stack of nodes for SemanticsParseTreeHandler.
  12. //
  13. // All pushes and pops will be vlogged.
  14. //
  15. // Pop APIs will run basic verification:
  16. //
  17. // - If receiving a pop_parse_kind, verify that the parse_node being popped is
  18. // of pop_parse_kind.
  19. // - Validates presence of node_id based on whether it's a solo
  20. // parse_node.
  21. //
  22. // These should be assumed API constraints unless otherwise mentioned on a
  23. // method. The main exception is PopAndIgnore, which doesn't do verification.
  24. class SemanticsNodeStack {
  25. public:
  26. SemanticsNodeStack(const ParseTree& parse_tree,
  27. llvm::raw_ostream* vlog_stream)
  28. : parse_tree_(&parse_tree), vlog_stream_(vlog_stream) {}
  29. // Pushes a solo parse tree node onto the stack. Used when there is no
  30. // IR generated by the node.
  31. auto Push(ParseTree::Node parse_node) -> void {
  32. PushEntry(
  33. {.parse_node = parse_node, .node_id = SemanticsNodeId::MakeInvalid()},
  34. /*is_node_id=*/true);
  35. }
  36. // Pushes a parse tree node onto the stack.
  37. auto Push(ParseTree::Node parse_node, SemanticsNodeId node_id) -> void {
  38. PushEntry({.parse_node = parse_node, .node_id = node_id},
  39. /*is_node_id=*/true);
  40. }
  41. // Pushes a PatternBinding parse tree node onto the stack with its name.
  42. auto Push(ParseTree::Node parse_node, SemanticsStringId name_id) -> void {
  43. CARBON_CHECK(parse_tree_->node_kind(parse_node) ==
  44. ParseNodeKind::PatternBinding);
  45. PushEntry({.parse_node = parse_node, .name_id = name_id},
  46. /*is_node_id=*/false);
  47. }
  48. // Pops the top of the stack without any verification.
  49. auto PopAndIgnore() -> void { PopEntry(); }
  50. // Pops the top of the stack.
  51. auto PopAndDiscardSoloParseNode(ParseNodeKind pop_parse_kind) -> void;
  52. // Pops the top of the stack and returns the parse_node.
  53. auto PopForSoloParseNode() -> ParseTree::Node;
  54. // Pops the top of the stack and returns the parse_node.
  55. auto PopForSoloParseNode(ParseNodeKind pop_parse_kind) -> ParseTree::Node;
  56. // Pops the top of the stack and returns the node_id.
  57. auto PopForNodeId(ParseNodeKind pop_parse_kind) -> SemanticsNodeId;
  58. // Pops the top of the stack and returns the parse_node and node_id.
  59. auto PopForParseNodeAndNodeId()
  60. -> std::pair<ParseTree::Node, SemanticsNodeId>;
  61. // Pops the top of the stack and returns the parse_node and node_id.
  62. auto PopForParseNodeAndNodeId(ParseNodeKind pop_parse_kind)
  63. -> std::pair<ParseTree::Node, SemanticsNodeId>;
  64. // Pops the top of the stack and returns the node_id.
  65. auto PopForNodeId() -> SemanticsNodeId;
  66. // Pops the top of the stack and returns the parse_node and name_id.
  67. // Verifies that the parse_node is a PatternBinding.
  68. auto PopForParseNodeAndNameId()
  69. -> std::pair<ParseTree::Node, SemanticsStringId>;
  70. // Peeks at the parse_node of the top of the stack.
  71. auto PeekParseNode() -> ParseTree::Node { return stack_.back().parse_node; }
  72. // Peeks at the name_id of the top of the stack.
  73. // Verifies that the parse_node is a PatternBinding.
  74. auto PeekForNameId() -> SemanticsStringId;
  75. // Prints the stack for a stack dump.
  76. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  77. private:
  78. // An entry in node_stack_.
  79. struct Entry {
  80. ParseTree::Node parse_node;
  81. union {
  82. // The node_id will be invalid if and only if it's a solo parse_node.
  83. SemanticsNodeId node_id;
  84. // The name_id is provided for PatternBindings.
  85. SemanticsStringId name_id;
  86. };
  87. };
  88. static_assert(sizeof(Entry) == 8, "Unexpected Entry size");
  89. // Pushes an entry onto the stack. is_node_id is provided for debug output
  90. // only.
  91. auto PushEntry(Entry entry, bool is_node_id) -> void;
  92. // Pops an entry.
  93. auto PopEntry() -> Entry;
  94. // Pops an entry, requiring the specific kind.
  95. auto PopEntry(ParseNodeKind pop_parse_kind) -> Entry;
  96. // Require an entry to have the given ParseNodeKind.
  97. auto RequireParseKind(Entry entry, ParseNodeKind require_kind) -> void;
  98. // Requires an entry to have a invalid node_id.
  99. // Also works with name_id in the union due to type compatibility.
  100. auto RequireSoloParseNode(Entry entry) -> void;
  101. // Requires an entry to have a valid node_id.
  102. // Also works with name_id in the union due to type compatibility.
  103. auto RequireNodeId(Entry entry) -> void;
  104. // The file's parse tree.
  105. const ParseTree* parse_tree_;
  106. // Whether to print verbose output.
  107. llvm::raw_ostream* vlog_stream_;
  108. // The actual stack.
  109. // PushEntry and PopEntry control modification in order to centralize
  110. // vlogging.
  111. llvm::SmallVector<Entry> stack_;
  112. };
  113. } // namespace Carbon
  114. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_STACK_H_