mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 13:40:12 +01:00
Lazy allocation means that we can use a single node block for _all_ empty node blocks. The change in timing for when the definition node is emitted shouldn't affect semantic correctness; the signature is already present for recursive calls.
49 lines
1.5 KiB
C++
49 lines
1.5 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/semantics/semantics_node_block_stack.h"
|
|
|
|
#include "common/vlog.h"
|
|
#include "toolchain/semantics/semantics_node.h"
|
|
|
|
namespace Carbon {
|
|
|
|
auto SemanticsNodeBlockStack::Push() -> void {
|
|
CARBON_VLOG() << "NodeBlock Push " << stack_.size() << "\n";
|
|
CARBON_CHECK(stack_.size() < (1 << 20))
|
|
<< "Excessive stack size: likely infinite loop";
|
|
stack_.push_back(SemanticsNodeBlockId::Invalid);
|
|
}
|
|
|
|
auto SemanticsNodeBlockStack::PeekForAdd() -> SemanticsNodeBlockId {
|
|
auto& back = stack_.back();
|
|
if (!back.is_valid()) {
|
|
SemanticsNodeBlockId block_id(node_blocks_->size());
|
|
node_blocks_->resize(block_id.index + 1);
|
|
back = block_id;
|
|
CARBON_VLOG() << "NodeBlock Add " << stack_.size() - 1 << ": " << back
|
|
<< "\n";
|
|
}
|
|
return back;
|
|
}
|
|
|
|
auto SemanticsNodeBlockStack::Pop() -> SemanticsNodeBlockId {
|
|
auto back = stack_.pop_back_val();
|
|
CARBON_VLOG() << "NodeBlock Pop " << stack_.size() << ": " << back << "\n";
|
|
if (!back.is_valid()) {
|
|
return SemanticsNodeBlockId::Empty;
|
|
}
|
|
return back;
|
|
}
|
|
|
|
auto SemanticsNodeBlockStack::PrintForStackDump(llvm::raw_ostream& output) const
|
|
-> void {
|
|
output << "SemanticsNodeBlockStack:\n";
|
|
for (int i = 0; i < static_cast<int>(stack_.size()); ++i) {
|
|
output << "\t" << i << ".\t" << stack_[i] << "\n";
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|