mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 14:31:04 +01:00
This removes the builtin FunctionType, replacing it with a FunctionType instruction. The constant for a FunctionDecl is now a StructValue with type of FunctionType. Note this means a function declaration produces _both_ a type, and a value of the type. This has some consequences in terms of circularity, and makes the importing of function declarations a little more complex. It'll get particularly peculiar for imports because of the behavior of the reference, but that's a known issue due to other things such as `alias`. The impact will hopefully be contained to ResolvePrevInstForMerge (and ImportRefs). To note a small formatting change in diagnostics: ``` - // CHECK:STDERR: fail_member_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated <function> in Interface>` is not callable. + // CHECK:STDERR: fail_member_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated F in Interface>` is not callable. - // CHECK:STDERR: fail_todo_facet_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated <function> in Interface>` is not callable. + // CHECK:STDERR: fail_todo_facet_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated F in Interface>` is not callable. ``` --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
78 lines
3.0 KiB
C++
78 lines
3.0 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/call.h"
|
|
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/check/convert.h"
|
|
#include "toolchain/check/function.h"
|
|
#include "toolchain/sem_ir/inst.h"
|
|
#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto PerformCall(Context& context, Parse::NodeId node_id,
|
|
SemIR::InstId callee_id, llvm::ArrayRef<SemIR::InstId> arg_ids)
|
|
-> SemIR::InstId {
|
|
// Identify the function we're calling.
|
|
auto callee_function = GetCalleeFunction(context.sem_ir(), callee_id);
|
|
if (!callee_function.function_id.is_valid()) {
|
|
if (!callee_function.is_error) {
|
|
CARBON_DIAGNOSTIC(CallToNonCallable, Error,
|
|
"Value of type `{0}` is not callable.", SemIR::TypeId);
|
|
context.emitter().Emit(node_id, CallToNonCallable,
|
|
context.insts().Get(callee_id).type_id());
|
|
}
|
|
return SemIR::InstId::BuiltinError;
|
|
}
|
|
auto& callable = context.functions().Get(callee_function.function_id);
|
|
|
|
// 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.GetTupleType({});
|
|
}
|
|
|
|
// If there is a return slot, build storage for the result.
|
|
SemIR::InstId return_storage_id = SemIR::InstId::Invalid;
|
|
{
|
|
DiagnosticAnnotationScope annotate_diagnostics(
|
|
&context.emitter(), [&](auto& builder) {
|
|
CARBON_DIAGNOSTIC(IncompleteReturnTypeHere, Note,
|
|
"Return type declared here.");
|
|
builder.Note(callable.return_storage_id, IncompleteReturnTypeHere);
|
|
});
|
|
CheckFunctionReturnType(context, callee_id, callable);
|
|
}
|
|
switch (callable.return_slot) {
|
|
case SemIR::Function::ReturnSlot::Present:
|
|
// Tentatively put storage for a temporary in the function's return slot.
|
|
// This will be replaced if necessary when we perform initialization.
|
|
return_storage_id = context.AddInst(
|
|
{node_id, SemIR::TemporaryStorage{callable.return_type_id}});
|
|
break;
|
|
case SemIR::Function::ReturnSlot::Absent:
|
|
break;
|
|
case SemIR::Function::ReturnSlot::Error:
|
|
// Don't form an initializing expression with an incomplete type.
|
|
type_id = SemIR::TypeId::Error;
|
|
break;
|
|
case SemIR::Function::ReturnSlot::NotComputed:
|
|
CARBON_FATAL() << "Missing return slot category in call to " << callable;
|
|
}
|
|
|
|
// Convert the arguments to match the parameters.
|
|
auto converted_args_id =
|
|
ConvertCallArgs(context, node_id, callee_function.self_id, arg_ids,
|
|
return_storage_id, callable.decl_id,
|
|
callable.implicit_param_refs_id, callable.param_refs_id);
|
|
auto call_inst_id = context.AddInst(
|
|
{node_id, SemIR::Call{type_id, callee_id, converted_args_id}});
|
|
|
|
return call_inst_id;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|