semantics_node_block_stack.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "llvm/ADT/SmallVector.h"
  7. #include "toolchain/semantics/semantics_ir.h"
  8. #include "toolchain/semantics/semantics_node.h"
  9. namespace Carbon::Check {
  10. // Wraps the stack of node blocks for Context.
  11. //
  12. // All pushes and pops will be vlogged.
  13. class NodeBlockStack {
  14. public:
  15. explicit NodeBlockStack(llvm::StringLiteral name, SemIR::File& semantics_ir,
  16. llvm::raw_ostream* vlog_stream)
  17. : name_(name), semantics_ir_(&semantics_ir), vlog_stream_(vlog_stream) {}
  18. // Pushes an existing node block.
  19. auto Push(SemIR::NodeBlockId id) -> void;
  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 { Push(SemIR::NodeBlockId::Invalid); }
  23. // Pushes a new unreachable code block.
  24. auto PushUnreachable() -> void { Push(SemIR::NodeBlockId::Unreachable); }
  25. // Allocates and pushes a new node block.
  26. auto PushForAdd() -> SemIR::NodeBlockId {
  27. Push();
  28. return PeekForAdd();
  29. }
  30. // Peeks at the top node block. This does not trigger lazy allocation, so the
  31. // returned node block may be invalid.
  32. auto Peek() -> SemIR::NodeBlockId {
  33. CARBON_CHECK(!stack_.empty()) << "no current block";
  34. return stack_.back();
  35. }
  36. // Returns the top node block, allocating one if it's still invalid.
  37. auto PeekForAdd() -> SemIR::NodeBlockId;
  38. // Pops the top node block. This will always return a valid node block;
  39. // SemIR::NodeBlockId::Empty is returned if one wasn't allocated.
  40. auto Pop() -> SemIR::NodeBlockId;
  41. // Pops the top node block, ensuring that it is lazily allocated if it's
  42. // empty. For use when more nodes will be added to the block later.
  43. auto PopForAdd() -> SemIR::NodeBlockId {
  44. PeekForAdd();
  45. return Pop();
  46. }
  47. // Returns whether the current block is statically reachable.
  48. auto is_current_block_reachable() -> bool {
  49. return Peek() != SemIR::NodeBlockId::Unreachable;
  50. }
  51. // Prints the stack for a stack dump.
  52. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  53. auto empty() const -> bool { return stack_.empty(); }
  54. auto size() const -> size_t { return stack_.size(); }
  55. private:
  56. // A name for debugging.
  57. llvm::StringLiteral name_;
  58. // The underlying SemIR::File instance. Always non-null.
  59. SemIR::File* semantics_ir_;
  60. // Whether to print verbose output.
  61. llvm::raw_ostream* vlog_stream_;
  62. // The actual stack.
  63. // PushEntry and PopEntry control modification in order to centralize
  64. // vlogging.
  65. llvm::SmallVector<SemIR::NodeBlockId> stack_;
  66. };
  67. } // namespace Carbon::Check
  68. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_BLOCK_STACK_H_