semantics_node_block_stack.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // Pushes a new node block. It will be invalid unless PeekForAdd is called in
  21. // order to support lazy allocation.
  22. auto Push() -> void;
  23. // Pushes a new node block.
  24. // TODO: Try to remove this in favor of the lazy alloc in Push.
  25. auto PushWithUnconditionalAlloc() -> SemanticsNodeBlockId;
  26. // Peeks at the top node block. This does not trigger lazy allocation, so the
  27. // returned node block may be invalid.
  28. auto Peek() -> SemanticsNodeBlockId { return stack_.back(); }
  29. // Returns the top node block, allocating one if it's still invalid.
  30. auto PeekForAdd() -> SemanticsNodeBlockId;
  31. // Pops the top node block. This will always return a valid node block;
  32. // SemanticsNodeBlockId::Empty is returned if one wasn't allocated.
  33. auto Pop() -> SemanticsNodeBlockId;
  34. // Prints the stack for a stack dump.
  35. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  36. auto empty() const -> bool { return stack_.empty(); }
  37. auto size() const -> size_t { return stack_.size(); }
  38. private:
  39. // The underlying node block storage on SemanticsIR. Always non-null.
  40. llvm::SmallVector<llvm::SmallVector<SemanticsNodeId>>* const node_blocks_;
  41. // Whether to print verbose output.
  42. llvm::raw_ostream* vlog_stream_;
  43. // The actual stack.
  44. // PushEntry and PopEntry control modification in order to centralize
  45. // vlogging.
  46. llvm::SmallVector<SemanticsNodeBlockId> stack_;
  47. };
  48. } // namespace Carbon
  49. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_BLOCK_STACK_H_