Overload resolution support for more kinds of candidate. (#6071)

* Add support for template candidates by calling the suitable
`AddCandidate` function for them.
* Add support for overloading on `*this` qualifiers by calling
`AddMethodCandidate` when appropriate.
* Make mapping from Carbon arguments to Clang arguments a little more
faithful by mapping the Carbon expression category into the Clang value
kind.
This commit is contained in:
Richard Smith
2025-09-16 21:57:32 +00:00
committed by GitHub
parent bac828d244
commit b054e3d2b0
11 changed files with 420 additions and 234 deletions
+216 -5
View File
@@ -67,10 +67,12 @@ fn Value(v: Cpp.HasQualifiers) {
// CHECK:STDERR:
v.volatile_this();
// CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+5]]:3: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef]
// CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+7]]:3: error: no matching function for call to `ref_this` [CppOverloadingNoViableFunctionFound]
// CHECK:STDERR: v.ref_this();
// CHECK:STDERR: ^
// CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon: note: initializing function parameter [InCallToFunctionParam]
// CHECK:STDERR: ^~~~~~~~~~~~
// CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
// CHECK:STDERR: v.ref_this();
// CHECK:STDERR: ^~~~~~~~~~~~
// CHECK:STDERR:
v.ref_this();
@@ -118,7 +120,7 @@ library "[[@TEST_NAME]]";
import Cpp library "object_param_qualifiers.h";
fn Ref(p: Cpp.HasQualifiers*) {
// CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: object parameter type: struct HasQualifiers &&` [SemanticsTodo]
// CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+7]]:3: error: no matching function for call to `ref_ref_this` [CppOverloadingNoViableFunctionFound]
// CHECK:STDERR: p->ref_ref_this();
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
@@ -127,7 +129,7 @@ fn Ref(p: Cpp.HasQualifiers*) {
// CHECK:STDERR:
p->ref_ref_this();
// CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: object parameter type: const struct HasQualifiers &&` [SemanticsTodo]
// CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+7]]:3: error: no matching function for call to `const_ref_ref_this` [CppOverloadingNoViableFunctionFound]
// CHECK:STDERR: p->const_ref_ref_this();
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
@@ -137,6 +139,27 @@ fn Ref(p: Cpp.HasQualifiers*) {
p->const_ref_ref_this();
}
// --- object_param_qualifiers_overloaded.h
struct HasQualifiers {
int* _Nonnull F() &;
int F() const &;
void F() volatile &;
};
// --- use_object_param_qualifiers_overloaded.carbon
library "[[@TEST_NAME]]";
import Cpp library "object_param_qualifiers_overloaded.h";
fn CallF(v: Cpp.HasQualifiers, p: Cpp.HasQualifiers*) {
//@dump-sem-ir-begin
var a: i32 = v.F();
var b: i32* = p->F();
//@dump-sem-ir-end
}
// --- explicit_object_param.h
struct Another {
@@ -162,6 +185,31 @@ fn Call(e: Cpp.ExplicitObjectParam, n: i32, a: Cpp.Another) {
//@dump-sem-ir-end
}
// --- explicit_object_param_overloaded.h
struct Another {
};
struct ExplicitObjectParam {
void F(this ExplicitObjectParam);
void F(this int);
void F(this Another);
};
// --- call_explicit_object_param_overloaded.carbon
library "[[@TEST_NAME]]";
import Cpp library "explicit_object_param_overloaded.h";
fn Call(e: Cpp.ExplicitObjectParam, n: i32, a: Cpp.Another) {
//@dump-sem-ir-begin
e.F();
n.(Cpp.ExplicitObjectParam.F)();
a.(Cpp.ExplicitObjectParam.F)();
//@dump-sem-ir-end
}
// CHECK:STDOUT: --- use_object_param_qualifiers.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
@@ -268,6 +316,98 @@ fn Call(e: Cpp.ExplicitObjectParam, n: i32, a: Cpp.Another) {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- use_object_param_qualifiers_overloaded.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %HasQualifiers: type = class_type @HasQualifiers [concrete]
// CHECK:STDOUT: %ptr.ec3: type = ptr_type %HasQualifiers [concrete]
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
// CHECK:STDOUT: %.4ab: type = cpp_overload_set_type @HasQualifiers.F.1 [concrete]
// CHECK:STDOUT: %empty_struct: %.4ab = struct_value () [concrete]
// CHECK:STDOUT: %const: type = const_type %HasQualifiers [concrete]
// CHECK:STDOUT: %ptr.2cb: type = ptr_type %const [concrete]
// CHECK:STDOUT: %F__carbon_thunk.type: type = fn_type @F__carbon_thunk [concrete]
// CHECK:STDOUT: %F__carbon_thunk: %F__carbon_thunk.type = struct_value () [concrete]
// CHECK:STDOUT: %ptr.235: type = ptr_type %i32 [concrete]
// CHECK:STDOUT: %pattern_type.fe8: type = pattern_type %ptr.235 [concrete]
// CHECK:STDOUT: %HasQualifiers.F.type.d208f0.2: type = fn_type @HasQualifiers.F.2 [concrete]
// CHECK:STDOUT: %HasQualifiers.F.efd4e4.2: %HasQualifiers.F.type.d208f0.2 = struct_value () [concrete]
// CHECK:STDOUT: %T.as.Destroy.impl.Op.type.f3e: type = fn_type @T.as.Destroy.impl.Op, @T.as.Destroy.impl(%ptr.235) [concrete]
// CHECK:STDOUT: %T.as.Destroy.impl.Op.a18: %T.as.Destroy.impl.Op.type.f3e = struct_value () [concrete]
// CHECK:STDOUT: %ptr.5d5: type = ptr_type %ptr.235 [concrete]
// CHECK:STDOUT: %Int.as.Destroy.impl.Op.type.4f9: type = fn_type @Int.as.Destroy.impl.Op, @Int.as.Destroy.impl(%int_32) [concrete]
// CHECK:STDOUT: %Int.as.Destroy.impl.Op.796: %Int.as.Destroy.impl.Op.type.4f9 = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %.570: %.4ab = cpp_overload_set_value @HasQualifiers.F.1 [concrete = constants.%empty_struct]
// CHECK:STDOUT: %F__carbon_thunk.decl: %F__carbon_thunk.type = fn_decl @F__carbon_thunk [concrete = constants.%F__carbon_thunk] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: %HasQualifiers.F.decl.f862ea.2: %HasQualifiers.F.type.d208f0.2 = fn_decl @HasQualifiers.F.2 [concrete = constants.%HasQualifiers.F.efd4e4.2] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @CallF(%v.param: %HasQualifiers, %p.param: %ptr.ec3) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %a.patt: %pattern_type.7ce = binding_pattern a [concrete]
// CHECK:STDOUT: %a.var_patt: %pattern_type.7ce = var_pattern %a.patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %a.var: ref %i32 = var %a.var_patt
// CHECK:STDOUT: %v.ref: %HasQualifiers = name_ref v, %v
// CHECK:STDOUT: %F.ref.loc8: %.4ab = name_ref F, imports.%.570 [concrete = constants.%empty_struct]
// CHECK:STDOUT: %bound_method.loc8_17: <bound method> = bound_method %v.ref, %F.ref.loc8
// CHECK:STDOUT: %.loc8_16: ref %HasQualifiers = value_as_ref %v.ref
// CHECK:STDOUT: %addr.loc8_20: %ptr.ec3 = addr_of %.loc8_16
// CHECK:STDOUT: %.loc8_20.1: %ptr.2cb = as_compatible %addr.loc8_20
// CHECK:STDOUT: %.loc8_20.2: %ptr.2cb = converted %addr.loc8_20, %.loc8_20.1
// CHECK:STDOUT: %F__carbon_thunk.call: init %i32 = call imports.%F__carbon_thunk.decl(%.loc8_20.2)
// CHECK:STDOUT: assign %a.var, %F__carbon_thunk.call
// CHECK:STDOUT: %.loc8_10: type = splice_block %i32.loc8 [concrete = constants.%i32] {
// CHECK:STDOUT: %int_32.loc8: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
// CHECK:STDOUT: %i32.loc8: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %a: ref %i32 = bind_name a, %a.var
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %b.patt: %pattern_type.fe8 = binding_pattern b [concrete]
// CHECK:STDOUT: %b.var_patt: %pattern_type.fe8 = var_pattern %b.patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %b.var: ref %ptr.235 = var %b.var_patt
// CHECK:STDOUT: %p.ref: %ptr.ec3 = name_ref p, %p
// CHECK:STDOUT: %.loc9_18: ref %HasQualifiers = deref %p.ref
// CHECK:STDOUT: %F.ref.loc9: %.4ab = name_ref F, imports.%.570 [concrete = constants.%empty_struct]
// CHECK:STDOUT: %bound_method.loc9_18: <bound method> = bound_method %.loc9_18, %F.ref.loc9
// CHECK:STDOUT: %addr.loc9_18: %ptr.ec3 = addr_of %.loc9_18
// CHECK:STDOUT: %HasQualifiers.F.call: init %ptr.235 = call imports.%HasQualifiers.F.decl.f862ea.2(%addr.loc9_18)
// CHECK:STDOUT: assign %b.var, %HasQualifiers.F.call
// CHECK:STDOUT: %.loc9_13: type = splice_block %ptr.loc9 [concrete = constants.%ptr.235] {
// CHECK:STDOUT: %int_32.loc9: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
// CHECK:STDOUT: %i32.loc9: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
// CHECK:STDOUT: %ptr.loc9: type = ptr_type %i32.loc9 [concrete = constants.%ptr.235]
// CHECK:STDOUT: }
// CHECK:STDOUT: %b: ref %ptr.235 = bind_name b, %b.var
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: <bound method> = bound_method %b.var, constants.%T.as.Destroy.impl.Op.a18
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %bound_method.loc9_3: <bound method> = bound_method %b.var, %T.as.Destroy.impl.Op.specific_fn
// CHECK:STDOUT: %addr.loc9_3: %ptr.5d5 = addr_of %b.var
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc9_3(%addr.loc9_3)
// CHECK:STDOUT: %Int.as.Destroy.impl.Op.bound: <bound method> = bound_method %a.var, constants.%Int.as.Destroy.impl.Op.796
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %bound_method.loc8_3: <bound method> = bound_method %a.var, %Int.as.Destroy.impl.Op.specific_fn
// CHECK:STDOUT: %addr.loc8_3: %ptr.235 = addr_of %a.var
// CHECK:STDOUT: %Int.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc8_3(%addr.loc8_3)
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- call_explicit_object_param.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
@@ -345,3 +485,74 @@ fn Call(e: Cpp.ExplicitObjectParam, n: i32, a: Cpp.Another) {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- call_explicit_object_param_overloaded.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %ExplicitObjectParam: type = class_type @ExplicitObjectParam [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %Another: type = class_type @Another [concrete]
// CHECK:STDOUT: %.3e2: type = cpp_overload_set_type @ExplicitObjectParam.F.1 [concrete]
// CHECK:STDOUT: %empty_struct: %.3e2 = struct_value () [concrete]
// CHECK:STDOUT: %ptr.7f5: type = ptr_type %ExplicitObjectParam [concrete]
// CHECK:STDOUT: %F__carbon_thunk.type.eda1ac.1: type = fn_type @F__carbon_thunk.1 [concrete]
// CHECK:STDOUT: %F__carbon_thunk.0cd6a8.1: %F__carbon_thunk.type.eda1ac.1 = struct_value () [concrete]
// CHECK:STDOUT: %ExplicitObjectParam.F.type.5d25a8.2: type = fn_type @ExplicitObjectParam.F.2 [concrete]
// CHECK:STDOUT: %ExplicitObjectParam.F.28cf2e.2: %ExplicitObjectParam.F.type.5d25a8.2 = struct_value () [concrete]
// CHECK:STDOUT: %ptr.289: type = ptr_type %Another [concrete]
// CHECK:STDOUT: %F__carbon_thunk.type.eda1ac.2: type = fn_type @F__carbon_thunk.2 [concrete]
// CHECK:STDOUT: %F__carbon_thunk.0cd6a8.2: %F__carbon_thunk.type.eda1ac.2 = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
// CHECK:STDOUT: .ExplicitObjectParam = %ExplicitObjectParam.decl
// CHECK:STDOUT: .Another = %Another.decl
// CHECK:STDOUT: import Cpp//...
// CHECK:STDOUT: }
// CHECK:STDOUT: %ExplicitObjectParam.decl: type = class_decl @ExplicitObjectParam [concrete = constants.%ExplicitObjectParam] {} {}
// CHECK:STDOUT: %Another.decl: type = class_decl @Another [concrete = constants.%Another] {} {}
// CHECK:STDOUT: %.037: %.3e2 = cpp_overload_set_value @ExplicitObjectParam.F.1 [concrete = constants.%empty_struct]
// CHECK:STDOUT: %F__carbon_thunk.decl.e1b8ec.1: %F__carbon_thunk.type.eda1ac.1 = fn_decl @F__carbon_thunk.1 [concrete = constants.%F__carbon_thunk.0cd6a8.1] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: %ExplicitObjectParam.F.decl.28f5af.2: %ExplicitObjectParam.F.type.5d25a8.2 = fn_decl @ExplicitObjectParam.F.2 [concrete = constants.%ExplicitObjectParam.F.28cf2e.2] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: %F__carbon_thunk.decl.e1b8ec.2: %F__carbon_thunk.type.eda1ac.2 = fn_decl @F__carbon_thunk.2 [concrete = constants.%F__carbon_thunk.0cd6a8.2] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Call(%e.param: %ExplicitObjectParam, %n.param: %i32, %a.param: %Another) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %e.ref: %ExplicitObjectParam = name_ref e, %e
// CHECK:STDOUT: %F.ref.loc8: %.3e2 = name_ref F, imports.%.037 [concrete = constants.%empty_struct]
// CHECK:STDOUT: %bound_method.loc8: <bound method> = bound_method %e.ref, %F.ref.loc8
// CHECK:STDOUT: %.loc8: ref %ExplicitObjectParam = value_as_ref %e.ref
// CHECK:STDOUT: %addr.loc8: %ptr.7f5 = addr_of %.loc8
// CHECK:STDOUT: %F__carbon_thunk.call.loc8: init %empty_tuple.type = call imports.%F__carbon_thunk.decl.e1b8ec.1(%addr.loc8)
// CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
// CHECK:STDOUT: %Cpp.ref.loc9: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %ExplicitObjectParam.ref.loc9: type = name_ref ExplicitObjectParam, imports.%ExplicitObjectParam.decl [concrete = constants.%ExplicitObjectParam]
// CHECK:STDOUT: %F.ref.loc9: %.3e2 = name_ref F, imports.%.037 [concrete = constants.%empty_struct]
// CHECK:STDOUT: %bound_method.loc9: <bound method> = bound_method %n.ref, %F.ref.loc9
// CHECK:STDOUT: %ExplicitObjectParam.F.call: init %empty_tuple.type = call imports.%ExplicitObjectParam.F.decl.28f5af.2(%n.ref)
// CHECK:STDOUT: %a.ref: %Another = name_ref a, %a
// CHECK:STDOUT: %Cpp.ref.loc10: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %ExplicitObjectParam.ref.loc10: type = name_ref ExplicitObjectParam, imports.%ExplicitObjectParam.decl [concrete = constants.%ExplicitObjectParam]
// CHECK:STDOUT: %F.ref.loc10: %.3e2 = name_ref F, imports.%.037 [concrete = constants.%empty_struct]
// CHECK:STDOUT: %bound_method.loc10: <bound method> = bound_method %a.ref, %F.ref.loc10
// CHECK:STDOUT: %.loc10: ref %Another = value_as_ref %a.ref
// CHECK:STDOUT: %addr.loc10: %ptr.289 = addr_of %.loc10
// CHECK:STDOUT: %F__carbon_thunk.call.loc10: init %empty_tuple.type = call imports.%F__carbon_thunk.decl.e1b8ec.2(%addr.loc10)
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: