node_block_stack.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // A stack of node blocks that are currently being constructed in a Context. The
  11. // contents of the node blocks are stored here until the node block is popped
  12. // from the stack, at which point they are transferred into the SemIR::File for
  13. // long-term storage.
  14. //
  15. // All pushes and pops will be vlogged.
  16. class NodeBlockStack {
  17. public:
  18. explicit NodeBlockStack(llvm::StringLiteral name, SemIR::File& semantics_ir,
  19. llvm::raw_ostream* vlog_stream)
  20. : name_(name), semantics_ir_(&semantics_ir), vlog_stream_(vlog_stream) {}
  21. // Pushes an existing node block.
  22. auto Push(SemIR::NodeBlockId id) -> void;
  23. // Pushes a new node block. It will be invalid unless PeekOrAdd is called in
  24. // order to support lazy allocation.
  25. auto Push() -> void { Push(SemIR::NodeBlockId::Invalid); }
  26. // Pushes a new unreachable code block.
  27. auto PushUnreachable() -> void { Push(SemIR::NodeBlockId::Unreachable); }
  28. // Returns the ID of the top node block, allocating one if necessary. If
  29. // `depth` is specified, returns the node at `depth` levels from the top of
  30. // the stack instead of the top block, where the top block is at depth 0.
  31. auto PeekOrAdd(int depth = 0) -> SemIR::NodeBlockId;
  32. // Pops the top node block. This will always return a valid node block;
  33. // SemIR::NodeBlockId::Empty is returned if one wasn't allocated.
  34. auto Pop() -> SemIR::NodeBlockId;
  35. // Adds the given node to the block at the top of the stack and returns its
  36. // ID.
  37. auto AddNode(SemIR::Node node) -> SemIR::NodeId {
  38. auto node_id = semantics_ir_->AddNodeInNoBlock(node);
  39. AddNodeId(node_id);
  40. return node_id;
  41. }
  42. // Adds the given node ID to the block at the top of the stack.
  43. auto AddNodeId(SemIR::NodeId node_id) -> void {
  44. CARBON_CHECK(!empty()) << "no current block";
  45. stack_[size_ - 1].content.push_back(node_id);
  46. }
  47. // Returns whether the current block is statically reachable.
  48. auto is_current_block_reachable() -> bool {
  49. return size_ != 0 &&
  50. stack_[size_ - 1].id != SemIR::NodeBlockId::Unreachable;
  51. }
  52. // Returns a view of the contents of the top node block on the stack.
  53. auto PeekCurrentBlockContents() -> llvm::ArrayRef<SemIR::NodeId> {
  54. CARBON_CHECK(!empty()) << "no current block";
  55. return stack_[size_ - 1].content;
  56. }
  57. // Prints the stack for a stack dump.
  58. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  59. auto empty() const -> bool { return size() == 0; }
  60. auto size() const -> int { return size_; }
  61. private:
  62. struct StackEntry {
  63. // Preallocate an arbitrary size for the stack entries.
  64. // TODO: Perform measurements to pick a good starting size to avoid
  65. // reallocation.
  66. StackEntry() { content.reserve(32); }
  67. auto Reset(SemIR::NodeBlockId new_id) {
  68. id = new_id;
  69. content.clear();
  70. }
  71. // The block ID, if one has been allocated, Invalid if no block has been
  72. // allocated, or Unreachable if this block is known to be unreachable.
  73. SemIR::NodeBlockId id = SemIR::NodeBlockId::Invalid;
  74. // The content of the block. Stored as a vector rather than as a SmallVector
  75. // to reduce the cost of resizing `stack_` and performing swaps.
  76. std::vector<SemIR::NodeId> content;
  77. };
  78. // A name for debugging.
  79. llvm::StringLiteral name_;
  80. // The underlying SemIR::File instance. Always non-null.
  81. SemIR::File* semantics_ir_;
  82. // Whether to print verbose output.
  83. llvm::raw_ostream* vlog_stream_;
  84. // The actual stack.
  85. llvm::SmallVector<StackEntry> stack_;
  86. // The size of the stack. Entries after this in `stack_` are kept around so
  87. // that we can reuse the allocated buffer for their content.
  88. int size_ = 0;
  89. };
  90. } // namespace Carbon::Check
  91. #endif // CARBON_TOOLCHAIN_CHECK_NODE_BLOCK_STACK_H_