Files
carbon-lang/toolchain/semantics/semantics_handle_function.cpp
T
Jon Ross-Perkins e73207429f Adjust handling of values in calls and structs (#2824)
Previously, IR for arguments in calls and struct values was separated out. This merges it back in. Additionally, parameters for functions and struct types had their own IR; the block is still there, but there's a TODO to decide what to do with it.

In the LLVM IR, this has the consequence of emitting expressions that are inputs to a call or struct value within the scope of the function, which is pretty much where it should be. Importantly it happens before the call is encountered.

This change also tinkers with the int and real literal lowering. I'm pretty sure both are still wrong, but was having trouble figuring out a "better" way to do it, and this seems like it'll work for now.
2023-05-18 11:42:03 -07:00

75 lines
2.8 KiB
C++

// 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_context.h"
namespace Carbon {
auto SemanticsHandleFunctionDeclaration(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
return context.TODO(parse_node, "HandleFunctionDeclaration");
}
auto SemanticsHandleFunctionDefinition(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
// Merges code block children up under the FunctionDefinitionStart.
while (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
ParseNodeKind::FunctionDefinitionStart) {
context.node_stack().PopAndIgnore();
}
auto decl_id =
context.node_stack().PopForNodeId(ParseNodeKind::FunctionDefinitionStart);
context.return_scope_stack().pop_back();
context.PopScope();
auto block_id = context.node_block_stack().Pop();
context.AddNode(
SemanticsNode::FunctionDefinition::Make(parse_node, decl_id, block_id));
context.node_stack().Push(parse_node);
return true;
}
auto SemanticsHandleFunctionDefinitionStart(SemanticsContext& context,
ParseTree::Node parse_node)
-> bool {
SemanticsNodeId return_type_id = SemanticsNodeId::Invalid;
if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
ParseNodeKind::ReturnType) {
return_type_id =
context.node_stack().PopForNodeId(ParseNodeKind::ReturnType);
}
auto param_refs_id =
context.node_stack().PopForNodeBlockId(ParseNodeKind::ParameterList);
auto name_node =
context.node_stack().PopForSoloParseNode(ParseNodeKind::DeclaredName);
auto fn_node = context.node_stack().PopForSoloParseNode(
ParseNodeKind::FunctionIntroducer);
auto name_str = context.parse_tree().GetNodeText(name_node);
auto name_id = context.semantics().AddString(name_str);
auto callable_id = context.semantics().AddCallable(
{.param_refs_id = param_refs_id, .return_type_id = return_type_id});
auto decl_id = context.AddNode(
SemanticsNode::FunctionDeclaration::Make(fn_node, name_id, callable_id));
context.AddNameToLookup(name_node, name_id, decl_id);
context.node_block_stack().Push();
context.PushScope();
context.return_scope_stack().push_back(decl_id);
context.node_stack().Push(parse_node, decl_id);
return true;
}
auto SemanticsHandleFunctionIntroducer(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
// No action, just a bracketing node.
context.node_stack().Push(parse_node);
return true;
}
} // namespace Carbon