mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Add tracking of function parameters (#2552)
For parameters (and in the future, arguments too; generally comma-separated lists) track two node blocks: 1. param_ir: The complete IR. 2. param_refs: Nodes within the IR that are the "root" parameter. param_refs should allow quick counting of the # of parameters, and more efficient comparison of call args with function parameters. param_ir should be necessary to generate the actual signature. In order to construct this, this refactors the node_block_stack into its own class, which is reused in params_stack. These carry references to the underlying SmallVector for lazy modification in order to avoid a dependency cycle with SemanticsIR (also see notes on empty node blocks below). When finalized, the block pair is pushed onto finished_params_stack. That's because node_stack only has space for one thing, and this is two things -- so I'm essentially choosing a trade-off of adding another stack in order to avoid consuming more space in the expectation that most parse nodes have 0 or 1 things to return, and 2 will be very rare. As factored, this currently consolidates most empty node blocks into a single canonical empty node block. This is because I think empty blocks, i.e. `()`, will be very common. In order to achieve this, SemanticsNodeBlockStack does lazy creation. An alternative approach would have been to use 1 node block per parameter. We decided against this in order to reduce the number of vectors being created.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
// 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::PushWithUnconditionalAlloc()
|
||||
-> SemanticsNodeBlockId {
|
||||
SemanticsNodeBlockId block_id(node_blocks_->size());
|
||||
CARBON_VLOG() << "NodeBlock Push " << stack_.size() << ": " << block_id
|
||||
<< "\n";
|
||||
CARBON_CHECK(stack_.size() < (1 << 20))
|
||||
<< "Excessive stack size: likely infinite loop";
|
||||
node_blocks_->resize(block_id.index + 1);
|
||||
stack_.push_back(block_id);
|
||||
return block_id;
|
||||
}
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user