Files
carbon-lang/toolchain/check/handle_call_expression.cpp
T
Richard Smith 3eb8c9a5f2 Remove two-phase implicit conversion of function calls. (#3208)
In order to maintain diagnostic quality, add a mechanism to add notes to
any diagnostics that are produced as part of initialization of function
parameters. As suggested in review of #3205.

In passing, fix the only caller of `ImplicitAsRequired` outside of the
implementation of `Check::Context` to instead use
`ConvertToValueExpressionOfType`. This causes some missing
`value_binding` nodes to be added to the produced SemIR. Also fixed a
matching bug in lowering where a bogus load was being added, that
resulted in assertion failures when the checker bug was fixed.

The warning `-Wnon-virtual-dtor` starts producing false-positive
warnings after this change. Replace it with the fixed version,
`-Wdelete-non-virtual-dtor`.
2023-09-12 00:05:27 +00:00

76 lines
2.7 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/check/context.h"
#include "toolchain/sem_ir/node.h"
namespace Carbon::Check {
auto HandleCallExpression(Context& context, Parse::Node parse_node) -> bool {
auto refs_id = context.ParamOrArgEnd(
/*for_args=*/true, Parse::NodeKind::CallExpressionStart);
// TODO: Convert to call expression.
auto [call_expr_parse_node, name_id] =
context.node_stack()
.PopWithParseNode<Parse::NodeKind::CallExpressionStart>();
auto name_node = context.semantics_ir().GetNode(name_id);
if (name_node.kind() != SemIR::NodeKind::FunctionDeclaration) {
// TODO: Work on error.
context.TODO(parse_node, "Not a callable name");
context.node_stack().Push(parse_node, name_id);
return true;
}
auto function_id = name_node.GetAsFunctionDeclaration();
const auto& callable = context.semantics_ir().GetFunction(function_id);
if (!context.ImplicitAsForArgs(call_expr_parse_node, refs_id,
name_node.parse_node(),
callable.param_refs_id)) {
context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
return true;
}
// For functions with an implicit return type, the return type is the empty
// tuple type.
SemIR::TypeId type_id = callable.return_type_id;
if (!type_id.is_valid()) {
type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
}
// If there is a return slot, add a corresponding argument.
if (callable.return_slot_id.is_valid()) {
if (refs_id == SemIR::NodeBlockId::Empty) {
refs_id = context.semantics_ir().AddNodeBlock();
}
// Tentatively put storage for a temporary in the function's return slot.
// This will be replaced if necessary when we perform initialization.
auto return_slot_id = context.AddNode(SemIR::Node::TemporaryStorage::Make(
call_expr_parse_node, callable.return_type_id));
context.semantics_ir().GetNodeBlock(refs_id).push_back(return_slot_id);
}
auto call_node_id = context.AddNode(SemIR::Node::Call::Make(
call_expr_parse_node, type_id, refs_id, function_id));
context.node_stack().Push(parse_node, call_node_id);
return true;
}
auto HandleCallExpressionComma(Context& context, Parse::Node /*parse_node*/)
-> bool {
context.ParamOrArgComma(/*for_args=*/true);
return true;
}
auto HandleCallExpressionStart(Context& context, Parse::Node parse_node)
-> bool {
auto name_id = context.node_stack().PopExpression();
context.node_stack().Push(parse_node, name_id);
context.ParamOrArgStart();
return true;
}
} // namespace Carbon::Check