Basic support for non-defining declarations of functions. (#2977)

This doesn't support redeclaration, so there's no way to provide a
definition in Carbon for a forward-declared function yet.
This commit is contained in:
Richard Smith
2023-07-07 01:08:02 +00:00
committed by GitHub
parent 239cdcc457
commit 123662f5b1
3 changed files with 114 additions and 37 deletions
@@ -6,9 +6,46 @@
namespace Carbon {
// Build a FunctionDeclaration describing the signature of a function. This
// handles the common logic shared by function declaration syntax and function
// definition syntax.
static auto BuildFunctionDeclaration(SemanticsContext& context)
-> std::pair<SemanticsFunctionId, SemanticsNodeId> {
SemanticsTypeId return_type_id = SemanticsTypeId::Invalid;
if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
ParseNodeKind::ReturnType) {
return_type_id =
context.node_stack().Pop<SemanticsTypeId>(ParseNodeKind::ReturnType);
} else {
// Canonicalize the empty tuple for the implicit return.
context.CanonicalizeType(SemanticsNodeId::BuiltinEmptyTupleType);
}
auto param_refs_id = context.node_stack().Pop<SemanticsNodeBlockId>(
ParseNodeKind::ParameterList);
auto name_context = context.PopDeclarationName();
auto fn_node = context.node_stack().PopForSoloParseNode(
ParseNodeKind::FunctionIntroducer);
// TODO: Support out-of-line definitions, which will have a resolved
// name_context. Right now, those become errors in AddNameToLookup.
// Add the callable.
auto function_id = context.semantics_ir().AddFunction(
{.name_id = name_context.unresolved_name_id,
.param_refs_id = param_refs_id,
.return_type_id = return_type_id,
.body_block_ids = {}});
auto decl_id = context.AddNode(
SemanticsNode::FunctionDeclaration::Make(fn_node, function_id));
context.AddNameToLookup(name_context, decl_id);
return {function_id, decl_id};
}
auto SemanticsHandleFunctionDeclaration(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
return context.TODO(parse_node, "HandleFunctionDeclaration");
ParseTree::Node /*parse_node*/)
-> bool {
BuildFunctionDeclaration(context);
return true;
}
auto SemanticsHandleFunctionDefinition(SemanticsContext& context,
@@ -31,57 +68,34 @@ auto SemanticsHandleFunctionDefinition(SemanticsContext& context,
}
}
context.return_scope_stack().pop_back();
context.PopScope();
context.node_block_stack().Pop();
context.return_scope_stack().pop_back();
return true;
}
auto SemanticsHandleFunctionDefinitionStart(SemanticsContext& context,
ParseTree::Node parse_node)
-> bool {
SemanticsTypeId return_type_id = SemanticsTypeId::Invalid;
if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
ParseNodeKind::ReturnType) {
return_type_id =
context.node_stack().Pop<SemanticsTypeId>(ParseNodeKind::ReturnType);
} else {
// Canonicalize the empty tuple for the implicit return.
context.CanonicalizeType(SemanticsNodeId::BuiltinEmptyTupleType);
}
auto param_refs_id = context.node_stack().Pop<SemanticsNodeBlockId>(
ParseNodeKind::ParameterList);
auto name_context = context.PopDeclarationName();
auto fn_node = context.node_stack().PopForSoloParseNode(
ParseNodeKind::FunctionIntroducer);
// Process the declaration portion of the function.
auto [function_id, decl_id] = BuildFunctionDeclaration(context);
const auto& function = context.semantics_ir().GetFunction(function_id);
// Create the entry block.
auto outer_block = context.node_block_stack().PeekForAdd();
// Create the function scope and the entry block.
context.return_scope_stack().push_back(decl_id);
context.node_block_stack().Push();
// TODO: Support out-of-line definitions, which will have a resolved
// name_context. Right now, those become errors in AddNameToLookup.
// Add the callable.
auto function_id = context.semantics_ir().AddFunction(
{.name_id = name_context.unresolved_name_id,
.param_refs_id = param_refs_id,
.return_type_id = return_type_id,
.body_block_ids = {context.node_block_stack().PeekForAdd()}});
auto decl_id = context.AddNodeToBlock(
outer_block,
SemanticsNode::FunctionDeclaration::Make(fn_node, function_id));
context.AddNameToLookup(name_context, decl_id);
context.PushScope();
for (auto ref_id : context.semantics_ir().GetNodeBlock(param_refs_id)) {
context.AddCurrentCodeBlockToFunction();
// Bring the parameters into scope.
for (auto ref_id :
context.semantics_ir().GetNodeBlock(function.param_refs_id)) {
auto ref = context.semantics_ir().GetNode(ref_id);
auto [name_id, target_id] = ref.GetAsBindName();
context.AddNameToLookup(ref.parse_node(), name_id, target_id);
}
context.return_scope_stack().push_back(decl_id);
context.node_stack().Push(parse_node, function_id);
context.node_stack().Push(parse_node, function_id);
return true;
}