mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-18 04:00:28 +00:00
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:
1 parent
bac828d244
commit
b054e3d2b0
11 files changed
+420
-234
No files matched your search
@@ -8,13 +8,117 @@
|
||||
#include "clang/Sema/Sema.h"
|
||||
#include "toolchain/check/cpp_import.h"
|
||||
#include "toolchain/check/cpp_type_mapping.h"
|
||||
#include "toolchain/sem_ir/expr_info.h"
|
||||
#include "toolchain/sem_ir/typed_insts.h"
|
||||
|
||||
namespace Carbon::Check {
|
||||
|
||||
// Invents a Clang argument expression to use in overload resolution to
|
||||
// represent the given Carbon argument instruction.
|
||||
static auto InventClangArg(Context& context, SemIR::InstId arg_id)
|
||||
-> clang::Expr* {
|
||||
clang::ExprValueKind value_kind;
|
||||
switch (SemIR::GetExprCategory(context.sem_ir(), arg_id)) {
|
||||
case SemIR::ExprCategory::NotExpr:
|
||||
CARBON_FATAL("Should not see these here");
|
||||
|
||||
case SemIR::ExprCategory::Error:
|
||||
return nullptr;
|
||||
|
||||
case SemIR::ExprCategory::DurableRef:
|
||||
value_kind = clang::ExprValueKind::VK_LValue;
|
||||
break;
|
||||
|
||||
case SemIR::ExprCategory::EphemeralRef:
|
||||
value_kind = clang::ExprValueKind::VK_XValue;
|
||||
break;
|
||||
|
||||
case SemIR::ExprCategory::Value:
|
||||
case SemIR::ExprCategory::Initializing:
|
||||
value_kind = clang::ExprValueKind::VK_PRValue;
|
||||
break;
|
||||
|
||||
case SemIR::ExprCategory::Mixed:
|
||||
// TODO: Handle this by creating an InitListExpr.
|
||||
value_kind = clang::ExprValueKind::VK_PRValue;
|
||||
break;
|
||||
}
|
||||
|
||||
if (context.insts().Get(arg_id).type_id() == SemIR::ErrorInst::TypeId) {
|
||||
// The argument error has already been diagnosed.
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
clang::QualType arg_cpp_type = MapToCppType(context, arg_id);
|
||||
if (arg_cpp_type.isNull()) {
|
||||
CARBON_DIAGNOSTIC(CppCallArgTypeNotSupported, Error,
|
||||
"call argument of type {0} is not supported",
|
||||
TypeOfInstId);
|
||||
context.emitter().Emit(arg_id, CppCallArgTypeNotSupported, arg_id);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// TODO: Avoid heap allocating more of these on every call. Either cache them
|
||||
// somewhere or put them on the stack.
|
||||
return new (context.ast_context()) clang::OpaqueValueExpr(
|
||||
// TODO: Add location accordingly.
|
||||
clang::SourceLocation(), arg_cpp_type.getNonReferenceType(), value_kind);
|
||||
}
|
||||
|
||||
// Adds the given overload candidates to the candidate set.
|
||||
static auto AddOverloadCandidataes(clang::Sema& sema,
|
||||
clang::OverloadCandidateSet& candidate_set,
|
||||
const clang::UnresolvedSetImpl& functions,
|
||||
clang::Expr* self_arg,
|
||||
llvm::ArrayRef<clang::Expr*> args) -> void {
|
||||
constexpr bool SuppressUserConversions = false;
|
||||
constexpr bool PartialOverloading = false;
|
||||
constexpr clang::TemplateArgumentListInfo* ExplicitTemplateArgs = nullptr;
|
||||
|
||||
for (auto found_decl : functions.pairs()) {
|
||||
auto* decl = found_decl.getDecl()->getUnderlyingDecl();
|
||||
auto* template_decl = dyn_cast<clang::FunctionTemplateDecl>(decl);
|
||||
auto* fn_decl = template_decl ? template_decl->getTemplatedDecl()
|
||||
: cast<clang::FunctionDecl>(decl);
|
||||
auto* method_decl = dyn_cast<clang::CXXMethodDecl>(fn_decl);
|
||||
if (method_decl && !method_decl->isStatic() &&
|
||||
!isa<clang::CXXConstructorDecl>(fn_decl)) {
|
||||
clang::QualType self_type;
|
||||
clang::Expr::Classification self_classification;
|
||||
if (self_arg) {
|
||||
self_type = self_arg->getType();
|
||||
self_classification = self_arg->Classify(sema.Context);
|
||||
}
|
||||
if (template_decl) {
|
||||
sema.AddMethodTemplateCandidate(
|
||||
template_decl, found_decl,
|
||||
cast<clang::CXXRecordDecl>(template_decl->getDeclContext()),
|
||||
ExplicitTemplateArgs, self_type, self_classification, args,
|
||||
candidate_set, SuppressUserConversions, PartialOverloading);
|
||||
} else {
|
||||
sema.AddMethodCandidate(method_decl, found_decl,
|
||||
method_decl->getParent(), self_type,
|
||||
self_classification, args, candidate_set,
|
||||
SuppressUserConversions, PartialOverloading);
|
||||
}
|
||||
} else {
|
||||
if (template_decl) {
|
||||
sema.AddTemplateOverloadCandidate(
|
||||
template_decl, found_decl, ExplicitTemplateArgs, args,
|
||||
candidate_set, SuppressUserConversions, PartialOverloading);
|
||||
} else {
|
||||
sema.AddOverloadCandidate(fn_decl, found_decl, args, candidate_set,
|
||||
SuppressUserConversions, PartialOverloading);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto PerformCppOverloadResolution(Context& context, SemIR::LocId loc_id,
|
||||
SemIR::CppOverloadSetId overload_set_id,
|
||||
SemIR::InstId self_id,
|
||||
llvm::ArrayRef<SemIR::InstId> arg_ids)
|
||||
-> std::optional<SemIR::InstId> {
|
||||
-> SemIR::InstId {
|
||||
Diagnostics::AnnotationScope annotate_diagnostics(
|
||||
&context.emitter(), [&](auto& builder) {
|
||||
CARBON_DIAGNOSTIC(InCallToCppFunction, Note,
|
||||
@@ -23,22 +127,21 @@ auto PerformCppOverloadResolution(Context& context, SemIR::LocId loc_id,
|
||||
});
|
||||
|
||||
// Map Carbon call argument types to C++ types.
|
||||
clang::Expr* self_expr = nullptr;
|
||||
if (self_id.has_value()) {
|
||||
self_expr = InventClangArg(context, self_id);
|
||||
if (!self_expr) {
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
}
|
||||
llvm::SmallVector<clang::Expr*> arg_exprs;
|
||||
arg_exprs.reserve(arg_ids.size());
|
||||
for (SemIR::InstId arg_id : arg_ids) {
|
||||
clang::QualType arg_cpp_type = MapToCppType(context, arg_id);
|
||||
if (arg_cpp_type.isNull()) {
|
||||
CARBON_DIAGNOSTIC(CppCallArgTypeNotSupported, Error,
|
||||
"call argument of type {0} is not supported",
|
||||
TypeOfInstId);
|
||||
context.emitter().Emit(loc_id, CppCallArgTypeNotSupported, arg_id);
|
||||
return std::nullopt;
|
||||
auto* arg_expr = InventClangArg(context, arg_id);
|
||||
if (!arg_expr) {
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
// TODO: Allocate these on the stack.
|
||||
arg_exprs.emplace_back(new (context.ast_context()) clang::OpaqueValueExpr(
|
||||
// TODO: Add location accordingly.
|
||||
clang::SourceLocation(), arg_cpp_type.getNonReferenceType(),
|
||||
clang::ExprValueKind::VK_LValue));
|
||||
arg_exprs.push_back(arg_expr);
|
||||
}
|
||||
|
||||
const SemIR::CppOverloadSet& overload_set =
|
||||
@@ -54,20 +157,8 @@ auto PerformCppOverloadResolution(Context& context, SemIR::LocId loc_id,
|
||||
CARBON_CHECK(ast);
|
||||
clang::Sema& sema = ast->getSema();
|
||||
|
||||
// TODO: Add support for method calls.
|
||||
for (clang::NamedDecl* candidate : overload_set.candidate_functions) {
|
||||
if (auto* fn_decl = dyn_cast<clang::FunctionDecl>(candidate)) {
|
||||
sema.AddOverloadCandidate(
|
||||
fn_decl, clang::DeclAccessPair::make(fn_decl, candidate->getAccess()),
|
||||
arg_exprs, candidate_set);
|
||||
} else if (isa<clang::FunctionTemplateDecl>(candidate)) {
|
||||
CARBON_DIAGNOSTIC(CppTemplateFunctionNotSupported, Error,
|
||||
"template function is not supported");
|
||||
context.emitter().Emit(loc_id, CppTemplateFunctionNotSupported);
|
||||
return std::nullopt;
|
||||
}
|
||||
// TODO: Diagnose if it's neither of these types.
|
||||
}
|
||||
AddOverloadCandidataes(sema, candidate_set, overload_set.candidate_functions,
|
||||
self_expr, arg_exprs);
|
||||
|
||||
// Find best viable function among the candidates.
|
||||
clang::OverloadCandidateSet::iterator best_viable_fn;
|
||||
@@ -91,7 +182,7 @@ auto PerformCppOverloadResolution(Context& context, SemIR::LocId loc_id,
|
||||
SemIR::NameId);
|
||||
context.emitter().Emit(loc_id, CppOverloadingNoViableFunctionFound,
|
||||
overload_set.name_id);
|
||||
return std::nullopt;
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
case clang::OverloadingResult::OR_Ambiguous: {
|
||||
// TODO: Add notes with the candidates.
|
||||
@@ -99,7 +190,7 @@ auto PerformCppOverloadResolution(Context& context, SemIR::LocId loc_id,
|
||||
"call to `{0}` is ambiguous", SemIR::NameId);
|
||||
context.emitter().Emit(loc_id, CppOverloadingAmbiguousCandidatesFound,
|
||||
overload_set.name_id);
|
||||
return std::nullopt;
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
case clang::OverloadingResult::OR_Deleted: {
|
||||
// TODO: Add notes with the candidates.
|
||||
@@ -107,7 +198,7 @@ auto PerformCppOverloadResolution(Context& context, SemIR::LocId loc_id,
|
||||
"call to deleted function `{0}`", SemIR::NameId);
|
||||
context.emitter().Emit(loc_id, CppOverloadingDeletedFunctionFound,
|
||||
overload_set.name_id);
|
||||
return std::nullopt;
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user