mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-01 22:02:45 +01:00
As proposed in [Carbon: C++ interop for overloaded functions and function templates](https://docs.google.com/document/d/1KUxumZtNe3mY3TsjW2s_ZADOlAaFlrtsLKHVILtqIaM/edit?tab=t.0), Clang is used to perform the overload resolution using C++ rules, when an overloaded C++ set is called from Carbon. Once a function is selected, it's converted into a Carbon function and called using the Carbon rules including argument conversions. A single non-templated function is treated the same way as an overload set and the same rules apply for its call. Template functions are not supported yet. Demo: a) Non-templated function calls: ```c++ // --- overloads.h auto foo(int a, short b) -> void; auto foo(double a) -> void; auto foo(int a) -> void; ``` ```c++ // overloads.cpp #include "overloads.h" #include <cstdio> auto foo(int a, short b) -> void { printf("hello from foo_int_short(%d, %d) \n", a, b); } auto foo(double a) -> void { printf("hello from foo_double(%f) \n", a); } auto foo(int a) -> void { printf("hello from foo_int(%d) \n", a); } ``` ```c++ library "Main"; import Cpp library "overloads.h"; fn Run() -> i32 { Cpp.foo(1.1 as f64); return 0; } ``` ``` $ clang -c overloads.cpp $ bazel-bin/toolchain/carbon compile main.carbon $ bazel-bin/toolchain/carbon link overloads.o main.o --output=demo $ ./demo hello from foo_double(1.100000) ``` b) Constructors: ```c++ // --- constructor_overloads.h class C { public: C(); C(int a, int b); }; ``` ```c++ // constructor_overloads.cpp #include "constructor_overloads.h" #include <cstdio> C::C() { printf("hello from C() \n"); } C::C(int a, int b) { printf("hello from C(%d, %d) \n", a, b); } ``` ```c++ library "Main"; import Cpp library "constructor_overloads.h"; fn Run() -> i32 { let c1: Cpp.C = Cpp.C.C(); let c2: Cpp.C = Cpp.C.C(1, 2); return 0; } ``` ``` $ clang -c constructor_overloads.cpp $ bazel-bin/toolchain/carbon compile main.carbon $ bazel-bin/toolchain/carbon link constructor_overloads.o main.o \--output=demo $ ./demo hello from C() hello from C(1, 2) ``` Follow-ups: - `Cpp.foo({})` - proper handling of struct literals as call args. - Fix access for overloaded sets. - Fix tests: - Method calls: `error: missing object argument in method call [MissingObjectInMethodCall]` in tests. - Fix `toolchain/check/testdata/interop/cpp/import.carbon` test. - Fix `enums` support. - Fix `str` -> `std::string_view` mapping. Part of #5915
271 lines
10 KiB
C++
271 lines
10 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/type.h"
|
|
|
|
#include "toolchain/check/eval.h"
|
|
#include "toolchain/check/facet_type.h"
|
|
#include "toolchain/check/type_completion.h"
|
|
#include "toolchain/sem_ir/facet_type_info.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto ValidateIntType(Context& context, SemIR::LocId loc_id,
|
|
SemIR::IntType result) -> bool {
|
|
auto bit_width =
|
|
context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
|
|
if (!bit_width) {
|
|
// Symbolic bit width.
|
|
return true;
|
|
}
|
|
const auto& bit_width_val = context.ints().Get(bit_width->int_id);
|
|
if (bit_width_val.isZero() ||
|
|
(context.types().IsSignedInt(bit_width->type_id) &&
|
|
bit_width_val.isNegative())) {
|
|
CARBON_DIAGNOSTIC(IntWidthNotPositive, Error,
|
|
"integer type width of {0} is not positive", TypedInt);
|
|
context.emitter().Emit(
|
|
loc_id, IntWidthNotPositive,
|
|
{.type = bit_width->type_id, .value = bit_width_val});
|
|
return false;
|
|
}
|
|
if (bit_width_val.ugt(IntStore::MaxIntWidth)) {
|
|
CARBON_DIAGNOSTIC(IntWidthTooLarge, Error,
|
|
"integer type width of {0} is greater than the "
|
|
"maximum supported width of {1}",
|
|
TypedInt, int);
|
|
context.emitter().Emit(loc_id, IntWidthTooLarge,
|
|
{.type = bit_width->type_id, .value = bit_width_val},
|
|
IntStore::MaxIntWidth);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
auto ValidateFloatTypeAndSetKind(Context& context, SemIR::LocId loc_id,
|
|
SemIR::FloatType& result) -> bool {
|
|
// Get the bit width value.
|
|
auto bit_width_inst =
|
|
context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
|
|
if (!bit_width_inst) {
|
|
// Symbolic bit width. Defer checking until we have a concrete value.
|
|
return true;
|
|
}
|
|
auto bit_width = context.ints().Get(bit_width_inst->int_id);
|
|
|
|
// If no kind is specified, infer kind from width.
|
|
if (!result.float_kind.has_value()) {
|
|
switch (bit_width.getLimitedValue()) {
|
|
case 16:
|
|
result.float_kind = SemIR::FloatKind::Binary16;
|
|
break;
|
|
case 32:
|
|
result.float_kind = SemIR::FloatKind::Binary32;
|
|
break;
|
|
case 64:
|
|
result.float_kind = SemIR::FloatKind::Binary64;
|
|
break;
|
|
case 128:
|
|
result.float_kind = SemIR::FloatKind::Binary128;
|
|
break;
|
|
default:
|
|
CARBON_DIAGNOSTIC(CompileTimeFloatBitWidth, Error,
|
|
"unsupported floating-point bit width {0}", TypedInt);
|
|
context.emitter().Emit(loc_id, CompileTimeFloatBitWidth,
|
|
TypedInt(bit_width_inst->type_id, bit_width));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (llvm::APFloat::semanticsSizeInBits(result.float_kind.Semantics()) !=
|
|
bit_width) {
|
|
// This can't currently happen because we don't provide any way to set the
|
|
// float kind other than through the bit width.
|
|
// TODO: Add a float_type.make builtin that takes a float kind, and add a
|
|
// diagnostic here if the size is wrong.
|
|
context.TODO(loc_id, "wrong size for float type");
|
|
return false;
|
|
}
|
|
|
|
// TODO: Diagnose if the floating-point type is not supported on this target?
|
|
|
|
return true;
|
|
}
|
|
|
|
// Gets or forms a type_id for a type, given the instruction kind and arguments.
|
|
template <typename InstT, typename... EachArgT>
|
|
static auto GetTypeImpl(Context& context, EachArgT... each_arg)
|
|
-> SemIR::TypeId {
|
|
InstT inst = {SemIR::TypeType::TypeId, each_arg...};
|
|
return context.types().GetTypeIdForTypeConstantId(TryEvalInst(context, inst));
|
|
}
|
|
|
|
// Gets or forms a type_id for a type, given the instruction kind and arguments,
|
|
// and completes the type. This should only be used when type completion cannot
|
|
// fail.
|
|
template <typename InstT, typename... EachArgT>
|
|
static auto GetCompleteTypeImpl(Context& context, EachArgT... each_arg)
|
|
-> SemIR::TypeId {
|
|
auto type_id = GetTypeImpl<InstT>(context, each_arg...);
|
|
CompleteTypeOrCheckFail(context, type_id);
|
|
return type_id;
|
|
}
|
|
|
|
auto GetStructType(Context& context, SemIR::StructTypeFieldsId fields_id)
|
|
-> SemIR::TypeId {
|
|
return GetTypeImpl<SemIR::StructType>(context, fields_id);
|
|
}
|
|
|
|
auto GetTupleType(Context& context, llvm::ArrayRef<SemIR::InstId> type_inst_ids)
|
|
-> SemIR::TypeId {
|
|
return GetTypeImpl<SemIR::TupleType>(
|
|
context, context.inst_blocks().AddCanonical(type_inst_ids));
|
|
}
|
|
|
|
auto GetAssociatedEntityType(Context& context, SemIR::InterfaceId interface_id,
|
|
SemIR::SpecificId interface_specific_id)
|
|
-> SemIR::TypeId {
|
|
return GetTypeImpl<SemIR::AssociatedEntityType>(context, interface_id,
|
|
interface_specific_id);
|
|
}
|
|
|
|
auto GetConstType(Context& context, SemIR::TypeInstId inner_type_id)
|
|
-> SemIR::TypeId {
|
|
return GetTypeImpl<SemIR::ConstType>(context, inner_type_id);
|
|
}
|
|
|
|
auto GetQualifiedType(Context& context, SemIR::TypeId type_id,
|
|
SemIR::TypeQualifiers quals) -> SemIR::TypeId {
|
|
if (quals.HasAnyOf(SemIR::TypeQualifiers::Const)) {
|
|
type_id = GetConstType(context, context.types().GetInstId(type_id));
|
|
quals.Remove(SemIR::TypeQualifiers::Const);
|
|
}
|
|
if (quals.HasAnyOf(SemIR::TypeQualifiers::MaybeUnformed)) {
|
|
type_id = GetTypeImpl<SemIR::MaybeUnformedType>(
|
|
context, context.types().GetInstId(type_id));
|
|
quals.Remove(SemIR::TypeQualifiers::MaybeUnformed);
|
|
}
|
|
if (quals.HasAnyOf(SemIR::TypeQualifiers::Partial)) {
|
|
type_id = GetTypeImpl<SemIR::PartialType>(
|
|
context, context.types().GetInstId(type_id));
|
|
quals.Remove(SemIR::TypeQualifiers::Partial);
|
|
}
|
|
CARBON_CHECK(quals == SemIR::TypeQualifiers::None);
|
|
return type_id;
|
|
}
|
|
|
|
auto GetSingletonType(Context& context, SemIR::TypeInstId singleton_id)
|
|
-> SemIR::TypeId {
|
|
CARBON_CHECK(SemIR::IsSingletonInstId(singleton_id));
|
|
auto type_id = context.types().GetTypeIdForTypeInstId(singleton_id);
|
|
// To keep client code simpler, complete builtin types before returning them.
|
|
CompleteTypeOrCheckFail(context, type_id);
|
|
return type_id;
|
|
}
|
|
|
|
auto GetClassType(Context& context, SemIR::ClassId class_id,
|
|
SemIR::SpecificId specific_id) -> SemIR::TypeId {
|
|
return GetTypeImpl<SemIR::ClassType>(context, class_id, specific_id);
|
|
}
|
|
|
|
auto GetCppOverloadSetType(Context& context,
|
|
SemIR::CppOverloadSetId overload_set_id,
|
|
SemIR::SpecificId specific_id) -> SemIR::TypeId {
|
|
return GetCompleteTypeImpl<SemIR::CppOverloadSetType>(
|
|
context, overload_set_id, specific_id);
|
|
}
|
|
|
|
auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
|
|
SemIR::SpecificId specific_id) -> SemIR::TypeId {
|
|
return GetCompleteTypeImpl<SemIR::FunctionType>(context, fn_id, specific_id);
|
|
}
|
|
|
|
auto GetFunctionTypeWithSelfType(Context& context,
|
|
SemIR::TypeInstId interface_function_type_id,
|
|
SemIR::InstId self_id) -> SemIR::TypeId {
|
|
return GetCompleteTypeImpl<SemIR::FunctionTypeWithSelfType>(
|
|
context, interface_function_type_id, self_id);
|
|
}
|
|
|
|
auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
|
|
SemIR::SpecificId enclosing_specific_id)
|
|
-> SemIR::TypeId {
|
|
return GetCompleteTypeImpl<SemIR::GenericClassType>(context, class_id,
|
|
enclosing_specific_id);
|
|
}
|
|
|
|
auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
|
|
SemIR::SpecificId enclosing_specific_id)
|
|
-> SemIR::TypeId {
|
|
return GetCompleteTypeImpl<SemIR::GenericInterfaceType>(
|
|
context, interface_id, enclosing_specific_id);
|
|
}
|
|
|
|
auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
|
|
SemIR::SpecificId specific_id) -> SemIR::TypeId {
|
|
return GetTypeImpl<SemIR::FacetType>(
|
|
context,
|
|
FacetTypeFromInterface(context, interface_id, specific_id).facet_type_id);
|
|
}
|
|
|
|
auto GetFacetType(Context& context, const SemIR::FacetTypeInfo& info)
|
|
-> SemIR::TypeId {
|
|
return GetTypeImpl<SemIR::FacetType>(context,
|
|
context.facet_types().Add(info));
|
|
}
|
|
|
|
auto GetPointerType(Context& context, SemIR::TypeInstId pointee_type_id)
|
|
-> SemIR::TypeId {
|
|
return GetTypeImpl<SemIR::PointerType>(context, pointee_type_id);
|
|
}
|
|
|
|
auto GetPatternType(Context& context, SemIR::TypeId scrutinee_type_id)
|
|
-> SemIR::TypeId {
|
|
CARBON_CHECK(!context.types().Is<SemIR::PatternType>(scrutinee_type_id),
|
|
"Type is already a pattern type");
|
|
if (scrutinee_type_id == SemIR::ErrorInst::TypeId) {
|
|
return SemIR::ErrorInst::TypeId;
|
|
}
|
|
return GetTypeImpl<SemIR::PatternType>(
|
|
context, context.types().GetInstId(scrutinee_type_id));
|
|
}
|
|
|
|
auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
|
|
SemIR::TypeInstId element_type_id) -> SemIR::TypeId {
|
|
return GetTypeImpl<SemIR::UnboundElementType>(context, class_type_id,
|
|
element_type_id);
|
|
}
|
|
|
|
auto GetCanonicalizedFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
|
|
-> SemIR::InstId {
|
|
// We can have FacetAccessType of a FacetValue, and a FacetValue of a
|
|
// FacetAccessType, but they don't nest indefinitely.
|
|
if (auto access = context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) {
|
|
inst_id = access->facet_value_inst_id;
|
|
}
|
|
if (auto value = context.insts().TryGetAs<SemIR::FacetValue>(inst_id)) {
|
|
inst_id = value->type_inst_id;
|
|
|
|
if (auto access =
|
|
context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) {
|
|
inst_id = access->facet_value_inst_id;
|
|
}
|
|
}
|
|
|
|
CARBON_CHECK(!context.insts().Is<SemIR::FacetAccessType>(inst_id));
|
|
CARBON_CHECK(!context.insts().Is<SemIR::FacetValue>(inst_id));
|
|
|
|
return context.constant_values().GetConstantInstId(inst_id);
|
|
}
|
|
|
|
auto GetCanonicalizedFacetOrTypeValue(Context& context,
|
|
SemIR::ConstantId const_id)
|
|
-> SemIR::ConstantId {
|
|
return context.constant_values().Get(GetCanonicalizedFacetOrTypeValue(
|
|
context, context.constant_values().GetInstId(const_id)));
|
|
}
|
|
|
|
} // namespace Carbon::Check
|