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:
Jon Ross-Perkins
2023-01-26 14:30:38 -08:00
committed by GitHub
parent 6fde5cfab0
commit 6feed2ae33
37 changed files with 749 additions and 150 deletions
+9 -2
View File
@@ -38,8 +38,8 @@ auto SemanticsIR::MakeBuiltinIR() -> SemanticsIR {
semantics.AddNode(block_id, SemanticsNode::MakeBuiltin(
SemanticsBuiltinKind::RealType, type_type));
CARBON_CHECK(semantics.node_blocks_.size() == 1)
<< "BuildBuiltins should only produce 1 block, actual: "
CARBON_CHECK(semantics.node_blocks_.size() == 2)
<< "BuildBuiltins should produce 2 blocks, actual: "
<< semantics.node_blocks_.size();
return semantics;
}
@@ -77,6 +77,13 @@ auto SemanticsIR::Print(llvm::raw_ostream& out) const -> void {
out << "cross_reference_irs_size: " << cross_reference_irs_.size() << "\n";
out << "callables: [\n";
for (auto callable : callables_) {
out.indent(Indent);
out << callable << "\n";
}
out << "]\n";
out << "integer_literals: [\n";
for (const auto& integer_literal : integer_literals_) {
out.indent(Indent);