node_block_stack.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_CHECK_NODE_BLOCK_STACK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_NODE_BLOCK_STACK_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "toolchain/sem_ir/file.h"
  8. #include "toolchain/sem_ir/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(!empty()) << "no current block";
  34. return stack_[size() - 1].id;
  35. }
  36. // Returns the top node block, allocating one if it's still invalid. If
  37. // `depth` is specified, returns the node at `depth` levels from the top of
  38. // the stack, where the top block is at depth 0.
  39. auto PeekForAdd(int depth = 0) -> SemIR::NodeBlockId;
  40. // Pops the top node block. This will always return a valid node block;
  41. // SemIR::NodeBlockId::Empty is returned if one wasn't allocated.
  42. auto Pop() -> SemIR::NodeBlockId;
  43. // Adds the given node ID to the block at the top of the stack.
  44. auto AddNodeId(SemIR::NodeId node_id) -> void {
  45. CARBON_CHECK(!empty()) << "no current block";
  46. stack_[size_ - 1].content.push_back(node_id);
  47. }
  48. // Returns whether the current block is statically reachable.
  49. auto is_current_block_reachable() -> bool {
  50. return Peek() != SemIR::NodeBlockId::Unreachable;
  51. }
  52. // Prints the stack for a stack dump.
  53. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  54. auto empty() const -> bool { return size() == 0; }
  55. auto size() const -> int { return size_; }
  56. private:
  57. struct StackEntry {
  58. // Preallocate an arbitrary size for the stack entries.
  59. // TODO: Perform measurements to pick a good starting size to avoid
  60. // reallocation.
  61. StackEntry() { content.reserve(32); }
  62. auto Reset(SemIR::NodeBlockId new_id) {
  63. id = new_id;
  64. content.clear();
  65. }
  66. // The block ID, if one has been allocated, Invalid if no block has been
  67. // allocated, or Unreachable if this block is known to be unreachable.
  68. SemIR::NodeBlockId id = SemIR::NodeBlockId::Invalid;
  69. // The content of the block. Stored as a vector rather than as a SmallVector
  70. // to reduce the cost of resizing `stack_` and performing swaps.
  71. std::vector<SemIR::NodeId> content;
  72. };
  73. // A name for debugging.
  74. llvm::StringLiteral name_;
  75. // The underlying SemIR::File instance. Always non-null.
  76. SemIR::File* semantics_ir_;
  77. // Whether to print verbose output.
  78. llvm::raw_ostream* vlog_stream_;
  79. // The actual stack.
  80. llvm::SmallVector<StackEntry> stack_;
  81. // The size of the stack. Entries after this in `stack_` are kept around so
  82. // that we can reuse the allocated buffer for their content.
  83. int size_ = 0;
  84. };
  85. } // namespace Carbon::Check
  86. #endif // CARBON_TOOLCHAIN_CHECK_NODE_BLOCK_STACK_H_