mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 09:34:42 +01:00
Support for type-checking and lowering method calls. (#3343)
Adds a `BoundMethod` SemIR node to represent an `x.F` bound method, with a new builtin type `BoundMethodType`. Reorganized conversion of call expression arguments to also check and convert a `self` parameter in the implicit parameters list. In passing, improved diagnostics and error recovery for bad call expressions. We now build a `call` node with the appropriate type and value category, but with invalid arguments, if the argument conversion failed, and diagnose calls to non-callable expressions. `addr self` methods don't work properly yet; the `addr` is ignored for now. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
c3e5721886
commit
57f3c553b8
@@ -2,6 +2,7 @@
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#include "llvm/ADT/ScopeExit.h"
|
||||
#include "toolchain/check/context.h"
|
||||
#include "toolchain/check/convert.h"
|
||||
#include "toolchain/sem_ir/node.h"
|
||||
@@ -9,26 +10,50 @@
|
||||
namespace Carbon::Check {
|
||||
|
||||
auto HandleCallExpression(Context& context, Parse::Node parse_node) -> bool {
|
||||
// Process the final explicit call argument, but leave the arguments block on
|
||||
// the stack until we add the return slot argument.
|
||||
// Process the final explicit call argument now, but leave the arguments
|
||||
// block on the stack until the end of this function.
|
||||
context.ParamOrArgEndNoPop(Parse::NodeKind::CallExpressionStart);
|
||||
auto discard_args_block = llvm::make_scope_exit(
|
||||
[&] { context.params_or_args_stack().PopAndDiscard(); });
|
||||
|
||||
// TODO: Convert to call expression.
|
||||
auto [call_expr_parse_node, callee_id] =
|
||||
context.node_stack()
|
||||
.PopWithParseNode<Parse::NodeKind::CallExpressionStart>();
|
||||
auto callee_node =
|
||||
context.nodes().Get(context.FollowNameReferences(callee_id));
|
||||
auto function_name = callee_node.TryAs<SemIR::FunctionDeclaration>();
|
||||
if (!function_name) {
|
||||
// TODO: Work on error.
|
||||
context.TODO(parse_node, "Not a callable name");
|
||||
|
||||
auto diagnose_not_callable = [&, call_expr_parse_node = call_expr_parse_node,
|
||||
callee_id = callee_id] {
|
||||
auto callee_type_id = context.nodes().Get(callee_id).type_id();
|
||||
if (callee_type_id != SemIR::TypeId::Error) {
|
||||
CARBON_DIAGNOSTIC(CallToNonCallable, Error,
|
||||
"Value of type `{0}` is not callable.", std::string);
|
||||
context.emitter().Emit(
|
||||
call_expr_parse_node, CallToNonCallable,
|
||||
context.sem_ir().StringifyType(callee_type_id, true));
|
||||
}
|
||||
context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
|
||||
context.ParamOrArgPop();
|
||||
return true;
|
||||
};
|
||||
|
||||
// For a method call, pick out the `self` value.
|
||||
auto function_callee_id = callee_id;
|
||||
SemIR::NodeId self_id = SemIR::NodeId::Invalid;
|
||||
if (auto bound_method =
|
||||
context.nodes().Get(callee_id).TryAs<SemIR::BoundMethod>()) {
|
||||
self_id = bound_method->object_id;
|
||||
function_callee_id = bound_method->function_id;
|
||||
}
|
||||
|
||||
auto function_id = function_name->function_id;
|
||||
// Identify the function we're calling.
|
||||
auto function_decl_id = context.GetConstantValue(function_callee_id);
|
||||
if (!function_decl_id.is_valid()) {
|
||||
return diagnose_not_callable();
|
||||
}
|
||||
auto function_decl =
|
||||
context.nodes().Get(function_decl_id).TryAs<SemIR::FunctionDeclaration>();
|
||||
if (!function_decl) {
|
||||
return diagnose_not_callable();
|
||||
}
|
||||
auto function_id = function_decl->function_id;
|
||||
const auto& callable = context.functions().Get(function_id);
|
||||
|
||||
// For functions with an implicit return type, the return type is the empty
|
||||
@@ -38,40 +63,23 @@ auto HandleCallExpression(Context& context, Parse::Node parse_node) -> bool {
|
||||
type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
|
||||
}
|
||||
|
||||
// If there is a return slot, add a corresponding argument.
|
||||
// If there is a return slot, build storage for the result.
|
||||
SemIR::NodeId return_storage_id = SemIR::NodeId::Invalid;
|
||||
if (callable.return_slot_id.is_valid()) {
|
||||
// Tentatively put storage for a temporary in the function's return slot.
|
||||
// This will be replaced if necessary when we perform initialization.
|
||||
auto temp_id = context.AddNode(
|
||||
return_storage_id = context.AddNode(
|
||||
SemIR::TemporaryStorage{call_expr_parse_node, callable.return_type_id});
|
||||
context.ParamOrArgSave(temp_id);
|
||||
}
|
||||
|
||||
for (auto implicit_param_id :
|
||||
context.node_blocks().Get(callable.implicit_param_refs_id)) {
|
||||
auto param = context.nodes().Get(implicit_param_id);
|
||||
if (auto self_param = param.TryAs<SemIR::SelfParameter>()) {
|
||||
// TODO: Handle `self` parameter.
|
||||
}
|
||||
|
||||
// TODO: Form argument values for implicit parameters.
|
||||
context.TODO(parse_node, "Call with implicit parameters");
|
||||
context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
|
||||
context.ParamOrArgPop();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Convert the arguments to match the parameters.
|
||||
auto refs_id = context.ParamOrArgPop();
|
||||
if (!ConvertCallArgs(context, call_expr_parse_node, refs_id,
|
||||
callee_node.parse_node(), callable.param_refs_id,
|
||||
callable.return_slot_id.is_valid())) {
|
||||
context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto converted_args_id =
|
||||
ConvertCallArgs(context, call_expr_parse_node, self_id,
|
||||
context.params_or_args_stack().PeekCurrentBlockContents(),
|
||||
return_storage_id, function_decl->parse_node,
|
||||
callable.implicit_param_refs_id, callable.param_refs_id);
|
||||
auto call_node_id = context.AddNode(
|
||||
SemIR::Call{call_expr_parse_node, type_id, callee_id, refs_id});
|
||||
SemIR::Call{call_expr_parse_node, type_id, callee_id, converted_args_id});
|
||||
|
||||
context.node_stack().Push(parse_node, call_node_id);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user