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
@@ -11,6 +11,7 @@
#include "toolchain/parser/parse_tree.h"
#include "toolchain/semantics/semantics_ir.h"
#include "toolchain/semantics/semantics_node.h"
#include "toolchain/semantics/semantics_node_block_stack.h"
#include "toolchain/semantics/semantics_node_stack.h"
namespace Carbon {
@@ -28,7 +29,9 @@ class SemanticsParseTreeHandler {
parse_tree_(&parse_tree),
semantics_(&semantics),
vlog_stream_(vlog_stream),
node_stack_(parse_tree, vlog_stream) {}
node_stack_(parse_tree, vlog_stream),
node_block_stack_(semantics.node_blocks_, vlog_stream),
params_stack_(semantics.node_blocks_, vlog_stream) {}
// Outputs the ParseTree information into SemanticsIR.
auto Build() -> void;
@@ -98,15 +101,15 @@ class SemanticsParseTreeHandler {
SemanticsNodeId rhs_id, bool can_convert_lhs)
-> SemanticsNodeId;
// Saves a parameter from the top block in node_stack_ to the top block in
// params_stack_. Returns false if nothing is copied.
auto SaveParam() -> bool;
// Parse node handlers.
#define CARBON_PARSE_NODE_KIND(Name) \
auto Handle##Name(ParseTree::Node parse_node)->void;
#include "toolchain/parser/parse_node_kind.def"
auto current_block_id() -> SemanticsNodeBlockId {
return node_block_stack_.back();
}
auto current_scope() -> ScopeStackEntry& { return scope_stack_.back(); }
// Tokens for getting data on literals.
@@ -127,9 +130,14 @@ class SemanticsParseTreeHandler {
// The stack during Build. Will contain file-level parse nodes on return.
SemanticsNodeStack node_stack_;
// The stack of node blocks during build. Only updated on ParseTree nodes that
// affect the stack.
llvm::SmallVector<SemanticsNodeBlockId> node_block_stack_;
// The stack of node blocks being used for general IR generation.
SemanticsNodeBlockStack node_block_stack_;
// The stack of node blocks being used for parameters.
SemanticsNodeBlockStack params_stack_;
llvm::SmallVector<std::pair<SemanticsNodeBlockId, SemanticsNodeBlockId>>
finished_params_stack_;
// A stack for scope context.
llvm::SmallVector<ScopeStackEntry> scope_stack_;