semantics_node_block_stack.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_BLOCK_STACK_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_BLOCK_STACK_H_
  6. #include <type_traits>
  7. #include "common/check.h"
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "toolchain/semantics/semantics_node.h"
  10. namespace Carbon {
  11. // Wraps the stack of node blocks for SemanticsParseTreeHandler.
  12. //
  13. // All pushes and pops will be vlogged.
  14. class SemanticsNodeBlockStack {
  15. public:
  16. explicit SemanticsNodeBlockStack(
  17. llvm::SmallVector<llvm::SmallVector<SemanticsNodeId>>& node_blocks,
  18. llvm::raw_ostream* vlog_stream)
  19. : node_blocks_(&node_blocks), vlog_stream_(vlog_stream) {}
  20. ~SemanticsNodeBlockStack() { CARBON_CHECK(stack_.empty()) << stack_.size(); }
  21. // Pushes a new node block. It will be invalid unless PeekForAdd is called in
  22. // order to support lazy allocation.
  23. auto Push() -> void;
  24. // Pushes a new node block.
  25. // TODO: Try to remove this in favor of the lazy alloc in Push.
  26. auto PushWithUnconditionalAlloc() -> SemanticsNodeBlockId;
  27. // Peeks at the top node block. This does not trigger lazy allocation, so the
  28. // returned node block may be invalid.
  29. auto Peek() -> SemanticsNodeBlockId { return stack_.back(); }
  30. // Returns the top node block, allocating one if it's still invalid.
  31. auto PeekForAdd() -> SemanticsNodeBlockId;
  32. // Pops the top node block. This will always return a valid node block;
  33. // SemanticsNodeBlockId::Empty is returned if one wasn't allocated.
  34. auto Pop() -> SemanticsNodeBlockId;
  35. // Prints the stack for a stack dump.
  36. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  37. private:
  38. // The underlying node block storage on SemanticsIR. Always non-null.
  39. llvm::SmallVector<llvm::SmallVector<SemanticsNodeId>>* const node_blocks_;
  40. // Whether to print verbose output.
  41. llvm::raw_ostream* vlog_stream_;
  42. // The actual stack.
  43. // PushEntry and PopEntry control modification in order to centralize
  44. // vlogging.
  45. llvm::SmallVector<SemanticsNodeBlockId> stack_;
  46. };
  47. } // namespace Carbon
  48. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_BLOCK_STACK_H_