Support pass-by-move when calling a C++ function taking by value. (#7135)

Previously, we picked a single Carbon parameter pattern for each C++
parameter pattern. This doesn't work well in cases where the Carbon
semantics and the C++ semantics are not perfectly aligned. In
particular, when a parameter is passed by value in C++, that might mean
either pass-by-move (which in Carbon would best be modeled by a `var`
pattern, as no other form of parameter would perform a move) or
pass-by-copy (which in Carbon would best be modeled by a value
parameter, as a `var` parameter would force an extra copy).

After this change, we compute a passing mode for each parameter based on
the implicit conversion sequence from the argument to the parameter as
determined by C++ overload resolution, and use that to determine the
Carbon pattern corresponding to each C++ parameter. This results in
potentially generating multiple different thunks for the same C++
function if it's called in different ways, but we already did that to
handle default arguments and list-initialization. The passing modes are
included in the thunk mangling.

Add a new value store for clang decl signatures, which capture the
information about parameter passing mode as well as the other existing
information about different ways that a C++ function might be imported
to Carbon.

Most of the rules for computing passing modes are the same as before:
const references use pass by value, non-const lvalue references use
pass-by-ref, non-const rvalue references use pass-by-var. But for C++
non-reference parameters, pick between pass-by-value and pass-by-var
based on whether the implicit conversion sequence was effectively
performing a copy. Prefer pass-by-value if either would work and they'd
do the same thing. We still use pass-by-value for const references, even
when the argument is an lvalue and we could pass a reference; we may
want to change this in future.

For virtual functions, we try to pick a worst-case passing mode, as we
can only pick a single signature for what goes in the vtable. Calls to
virtual functions will still use a thunk to C++, allowing variance in
the calling convention at call sites. We don't allow variance in the
overriders as we don't implement support for thunks for virtual
functions yet. We currently use pass-by-value for const reference
parameters here, but that should probably change at some point.

Assisted-by: Gemini via Antigravity
This commit is contained in:
Richard Smith
2026-05-13 01:44:07 +00:00
committed by GitHub
parent 10b2b71847
commit 71ba07239f
50 changed files with 1654 additions and 524 deletions
+62 -32
View File
@@ -19,6 +19,7 @@
#include "toolchain/check/type.h"
#include "toolchain/check/type_completion.h"
#include "toolchain/sem_ir/builtin_function_kind.h"
#include "toolchain/sem_ir/clang_decl.h"
#include "toolchain/sem_ir/cpp_initializer_list.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/inst.h"
@@ -285,44 +286,67 @@ static auto MakeCppStdInitializerListMake(Context& context, SemIR::LocId loc_id,
static auto GetConversionSignatureToImport(
Context& context, SemIR::InstId source_id,
clang::InitializationSequence::StepKind step_kind,
clang::FunctionDecl* function_decl) -> SemIR::ClangDeclKey::Signature {
clang::FunctionDecl* function_decl, clang::DeclAccessPair found_decl,
clang::Expr* arg_expr) -> SemIR::ClangDeclSignatureId {
auto signature_kind = SemIR::ClangDeclSignature::Normal;
clang::Expr* self_expr = nullptr;
llvm::ArrayRef<clang::Expr*> arg_exprs(arg_expr);
// If we're performing a constructor initialization from a list, form a
// function signature that takes a single tuple or struct pattern
// instead of a function signature with one parameter per C++ parameter.
if (step_kind ==
clang::InitializationSequence::SK_ConstructorInitializationFromList) {
// Initialization from a tuple `(a, b, c)` results in a constructor
// function that takes a tuple pattern:
//
// fn Class.Class((a: A, b: B, c: C)) -> Class;
//
// The source type should always be a tuple type, because we don't support
// C++ initialization from struct types.
auto tuple_type = context.types().TryGetAs<SemIR::TupleType>(
context.insts().Get(source_id).type_id());
CARBON_CHECK(tuple_type, "List initialization from non-tuple type");
// Initialization from a tuple `(a, b, c)` results in a constructor
// function that takes a tuple pattern:
//
// fn Class.Class((a: A, b: B, c: C)) -> Class;
return {
.kind = SemIR::ClangDeclKey::Signature::Kind::TuplePattern,
.num_params = static_cast<int32_t>(
context.inst_blocks().Get(tuple_type->type_elements_id).size())};
arg_exprs = cast<clang::InitListExpr>(arg_expr)->inits();
signature_kind = SemIR::ClangDeclSignature::TuplePattern;
}
// Any other initialization using a constructor is calling a converting
// constructor:
//
// fn Class.Class(a: A) -> Class;
// In order to determine how to map the parameters, we need to build the
// conversion sequence(s) again. Clang already threw them away. The only way
// to do this is to "redo" overload resolution with our single candidate.
clang::OverloadCandidateSet candidates(
function_decl->getLocation(),
clang::OverloadCandidateSet::CSK_InitByUserDefinedConversion);
if (isa<clang::CXXConstructorDecl>(function_decl)) {
return {.kind = SemIR::ClangDeclKey::Signature::Kind::Normal,
.num_params = 1};
// This is either tuple list initialization as described above or a
// constructor call:
//
// fn Class.Class(a: A) -> Class;
context.clang_sema().AddOverloadCandidate(function_decl, found_decl,
arg_exprs, candidates);
} else {
// Otherwise, the initialization is calling a conversion function
// `Source::operator Dest`:
//
// fn Source.<conversion function>[self: Source]() -> Dest;
auto* conversion_decl = cast<clang::CXXConversionDecl>(function_decl);
self_expr = arg_expr;
arg_exprs = {};
context.clang_sema().AddMethodCandidate(
conversion_decl, found_decl, conversion_decl->getParent(),
self_expr->getType(), self_expr->Classify(context.ast_context()),
arg_exprs, candidates);
}
// Otherwise, the initialization is calling a conversion function
// `Source::operator Dest`:
//
// fn Source.<conversion function>[self: Source]() -> Dest;
CARBON_CHECK(isa<clang::CXXConversionDecl>(function_decl));
return {.kind = SemIR::ClangDeclKey::Signature::Kind::Normal,
.num_params = 0};
clang::OverloadCandidateSet::iterator best;
auto result = candidates.BestViableFunction(
context.clang_sema(), function_decl->getLocation(), best);
CARBON_CHECK(result == clang::OverloadingResult::OR_Success ||
result == clang::OverloadingResult::OR_Deleted);
return ComputeClangDeclSignatureFromBestViableFunction(
context, best, self_expr, arg_exprs, signature_kind);
}
static auto LookupCppConversion(Context& context, SemIR::LocId loc_id,
@@ -402,10 +426,12 @@ static auto LookupCppConversion(Context& context, SemIR::LocId loc_id,
sema.MarkFunctionReferenced(loc, step.Function.Function);
auto signature = GetConversionSignatureToImport(
context, source_id, step.Kind, step.Function.Function);
SemIR::ClangDeclSignatureId signature_id =
GetConversionSignatureToImport(context, source_id, step.Kind,
step.Function.Function,
step.Function.FoundDecl, arg_expr);
auto result_id = ImportCppFunctionDecl(
context, loc_id, step.Function.Function, signature);
context, loc_id, step.Function.Function, signature_id);
if (auto fn_decl = context.insts().TryGetAsWithId<SemIR::FunctionDecl>(
result_id)) {
CheckCppOverloadAccess(context, loc_id, step.Function.FoundDecl,
@@ -637,14 +663,18 @@ static auto FindClangOperator(Context& context, SemIR::LocId loc_id,
sema.MarkFunctionReferenced(loc, best_viable_fn->Function);
// If this is an operator method, the first arg will be used as self.
int32_t num_params = arg_exprs.size();
if (isa<clang::CXXMethodDecl>(best_viable_fn->Function)) {
--num_params;
clang::Expr* self_expr = nullptr;
auto arg_exprs_for_signature = arg_exprs;
if (IsObjectMemberFunction(*best_viable_fn->Function)) {
self_expr = arg_exprs_for_signature.consume_front();
}
auto result_id =
ImportCppFunctionDecl(context, loc_id, best_viable_fn->Function,
{.num_params = num_params});
SemIR::ClangDeclSignatureId signature_id =
ComputeClangDeclSignatureFromBestViableFunction(
context, best_viable_fn, self_expr, arg_exprs_for_signature);
auto result_id = ImportCppFunctionDecl(
context, loc_id, best_viable_fn->Function, signature_id);
if (result_id != SemIR::ErrorInst::InstId) {
CheckCppOverloadAccess(
context, loc_id, best_viable_fn->FoundDecl,