Make function definitions allocate the body more lazily. (#2557)

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.
This commit is contained in:
Jon Ross-Perkins
2023-01-27 11:46:12 -08:00
committed by GitHub
parent 2ffbe72384
commit 94cbb9d917
23 changed files with 78 additions and 117 deletions
@@ -306,10 +306,12 @@ auto SemanticsParseTreeHandler::HandleFunctionDefinition(
ParseNodeKind::FunctionDefinitionStart) {
node_stack_.PopAndIgnore();
}
node_stack_.PopAndIgnore();
auto decl_id =
node_stack_.PopForNodeId(ParseNodeKind::FunctionDefinitionStart);
PopScope();
node_block_stack_.Pop();
auto block_id = node_block_stack_.Pop();
AddNode(SemanticsNode::MakeFunctionDefinition(parse_node, decl_id, block_id));
node_stack_.Push(parse_node);
return true;
@@ -332,16 +334,9 @@ auto SemanticsParseTreeHandler::HandleFunctionDefinitionStart(
// TODO: Propagate the type of the function.
BindName(name_node, SemanticsNodeId::MakeInvalid(), decl_id);
// TODO: Consider approaches that allow lazy creation of the definition block.
auto outer_block = node_block_stack_.PeekForAdd();
auto def_block = node_block_stack_.PushWithUnconditionalAlloc();
auto node =
SemanticsNode::MakeFunctionDefinition(parse_node, decl_id, def_block);
CARBON_VLOG() << "AddNode " << outer_block << ": " << node << "\n";
semantics_->AddNode(outer_block, node);
node_block_stack_.Push();
PushScope();
node_stack_.Push(parse_node);
node_stack_.Push(parse_node, decl_id);
return true;
}