| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- // 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/base/kind_switch.h"
- #include "toolchain/check/context.h"
- #include "toolchain/check/convert.h"
- #include "toolchain/check/function.h"
- #include "toolchain/check/generic.h"
- #include "toolchain/sem_ir/ids.h"
- #include "toolchain/sem_ir/inst.h"
- #include "toolchain/sem_ir/typed_insts.h"
- namespace Carbon::Check {
- // Performs a call where the callee is the name of a generic class, such as
- // `Vector(i32)`.
- static auto PerformCallToGenericClass(Context& context, Parse::NodeId node_id,
- SemIR::InstId callee_id,
- SemIR::ClassId class_id,
- llvm::ArrayRef<SemIR::InstId> arg_ids)
- -> SemIR::InstId {
- auto& class_info = context.classes().Get(class_id);
- // TODO: Pass in information about the specific in which the generic class
- // name was found.
- // TODO: Perform argument deduction.
- auto specific_id = SemIR::GenericInstanceId::Invalid;
- // Convert the arguments to match the parameters.
- auto converted_args_id = ConvertCallArgs(
- context, node_id, /*self_id=*/SemIR::InstId::Invalid, arg_ids,
- /*return_storage_id=*/SemIR::InstId::Invalid, class_info, specific_id);
- return context.AddInst<SemIR::Call>(node_id,
- {.type_id = SemIR::TypeId::TypeType,
- .callee_id = callee_id,
- .args_id = converted_args_id});
- }
- // Performs a call where the callee is the name of a generic interface, such as
- // `AddWith(i32)`.
- // TODO: Refactor with PerformCallToGenericClass.
- static auto PerformCallToGenericInterface(Context& context,
- Parse::NodeId node_id,
- SemIR::InstId callee_id,
- SemIR::InterfaceId interface_id,
- llvm::ArrayRef<SemIR::InstId> arg_ids)
- -> SemIR::InstId {
- auto& interface_info = context.interfaces().Get(interface_id);
- // TODO: Pass in information about the specific in which the generic interface
- // name was found.
- // TODO: Perform argument deduction.
- auto specific_id = SemIR::GenericInstanceId::Invalid;
- // Convert the arguments to match the parameters.
- auto converted_args_id = ConvertCallArgs(
- context, node_id, /*self_id=*/SemIR::InstId::Invalid, arg_ids,
- /*return_storage_id=*/SemIR::InstId::Invalid, interface_info,
- specific_id);
- return context.AddInst<SemIR::Call>(node_id,
- {.type_id = SemIR::TypeId::TypeType,
- .callee_id = callee_id,
- .args_id = converted_args_id});
- }
- 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()) {
- auto type_inst =
- context.types().GetAsInst(context.insts().Get(callee_id).type_id());
- CARBON_KIND_SWITCH(type_inst) {
- case CARBON_KIND(SemIR::GenericClassType generic_class): {
- return PerformCallToGenericClass(context, node_id, callee_id,
- generic_class.class_id, arg_ids);
- }
- case CARBON_KIND(SemIR::GenericInterfaceType generic_interface): {
- return PerformCallToGenericInterface(context, node_id, callee_id,
- generic_interface.interface_id,
- arg_ids);
- }
- default: {
- 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);
- // TODO: Properly determine the generic argument values for the call. For now,
- // we do so only if the function introduces no generic parameters beyond those
- // of the enclosing context.
- auto specific_id = SemIR::GenericInstanceId::Invalid;
- if (callee_function.instance_id.is_valid()) {
- auto enclosing_args_id =
- context.generic_instances().Get(callee_function.instance_id).args_id;
- auto fn_params_id = context.generics().Get(callable.generic_id).bindings_id;
- if (context.inst_blocks().Get(fn_params_id).size() ==
- context.inst_blocks().Get(enclosing_args_id).size()) {
- specific_id =
- MakeGenericInstance(context, callable.generic_id, enclosing_args_id);
- }
- }
- // For functions with an implicit return type, the return type is the empty
- // tuple type.
- SemIR::TypeId type_id =
- callable.GetDeclaredReturnType(context.sem_ir(), specific_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;
- SemIR::Function::ReturnSlot return_slot;
- {
- DiagnosticAnnotationScope annotate_diagnostics(
- &context.emitter(), [&](auto& builder) {
- CARBON_DIAGNOSTIC(IncompleteReturnTypeHere, Note,
- "Return type declared here.");
- builder.Note(callable.return_storage_id, IncompleteReturnTypeHere);
- });
- return_slot =
- CheckFunctionReturnType(context, callee_id, callable, specific_id);
- }
- switch (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<SemIR::TemporaryStorage>(
- node_id, {.type_id = 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, specific_id);
- auto call_inst_id =
- context.AddInst<SemIR::Call>(node_id, {.type_id = type_id,
- .callee_id = callee_id,
- .args_id = converted_args_id});
- return call_inst_id;
- }
- } // namespace Carbon::Check
|