pending_block.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_PENDING_BLOCK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_PENDING_BLOCK_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "toolchain/check/context.h"
  8. namespace Carbon::Check {
  9. // A block of code that contains pending instructions that might be needed but
  10. // that haven't been inserted yet.
  11. class PendingBlock {
  12. public:
  13. PendingBlock(Context& context) : context_(context) {}
  14. PendingBlock(const PendingBlock&) = delete;
  15. PendingBlock& operator=(const PendingBlock&) = delete;
  16. // A scope in which we will tentatively add nodes to a pending block. If we
  17. // leave the scope without inserting or merging the block, nodes added after
  18. // this point will be removed again.
  19. class DiscardUnusedNodesScope {
  20. public:
  21. // If `block` is not null, enters the scope. If `block` is null, this object
  22. // has no effect.
  23. DiscardUnusedNodesScope(PendingBlock* block)
  24. : block_(block), size_(block ? block->nodes_.size() : 0) {}
  25. ~DiscardUnusedNodesScope() {
  26. if (block_ && block_->nodes_.size() > size_) {
  27. block_->nodes_.truncate(size_);
  28. }
  29. }
  30. private:
  31. PendingBlock* block_;
  32. size_t size_;
  33. };
  34. auto AddNode(SemIR::Node node) -> SemIR::NodeId {
  35. auto node_id = context_.semantics_ir().AddNodeInNoBlock(node);
  36. nodes_.push_back(node_id);
  37. return node_id;
  38. }
  39. // Insert the pending block of code at the current position.
  40. auto InsertHere() -> void {
  41. for (auto id : nodes_) {
  42. context_.node_block_stack().AddNodeId(id);
  43. }
  44. nodes_.clear();
  45. }
  46. // Replace the node at target_id with the nodes in this block. The new value
  47. // for target_id should be value_id.
  48. auto MergeReplacing(SemIR::NodeId target_id, SemIR::NodeId value_id) -> void {
  49. auto value = context_.semantics_ir().GetNode(value_id);
  50. // There are three cases here:
  51. if (nodes_.empty()) {
  52. // 1) The block is empty. Replace `target_id` with an empty splice
  53. // pointing at `value_id`.
  54. context_.semantics_ir().ReplaceNode(
  55. target_id,
  56. SemIR::Node::SpliceBlock::Make(value.parse_node(), value.type_id(),
  57. SemIR::NodeBlockId::Empty, value_id));
  58. } else if (nodes_.size() == 1 && nodes_[0] == value_id) {
  59. // 2) The block is {value_id}. Replace `target_id` with the node referred
  60. // to by `value_id`. This is intended to be the common case.
  61. context_.semantics_ir().ReplaceNode(target_id, value);
  62. } else {
  63. // 3) Anything else: splice it into the IR, replacing `target_id`.
  64. context_.semantics_ir().ReplaceNode(
  65. target_id,
  66. SemIR::Node::SpliceBlock::Make(
  67. value.parse_node(), value.type_id(),
  68. context_.semantics_ir().AddNodeBlock(nodes_), value_id));
  69. }
  70. // Prepare to stash more pending instructions.
  71. nodes_.clear();
  72. }
  73. private:
  74. Context& context_;
  75. llvm::SmallVector<SemIR::NodeId> nodes_;
  76. };
  77. } // namespace Carbon::Check
  78. #endif // CARBON_TOOLCHAIN_CHECK_PENDING_BLOCK_H_