Files
carbon-lang/toolchain/sem_ir/clang_decl.cpp
T
Richard Smith 71ba07239f 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
2026-05-13 01:44:07 +00:00

110 lines
2.9 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/sem_ir/clang_decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/TextNodeDumper.h"
#include "common/ostream.h"
#include "common/raw_string_ostream.h"
namespace Carbon::SemIR {
auto ClangDeclSignature::Print(llvm::raw_ostream& out) const -> void {
out << "{kind: ";
switch (kind) {
case Normal:
out << "normal";
break;
case TuplePattern:
out << "tuple";
break;
}
out << ", num_params: " << num_params;
auto print_mode = [&](PassingMode mode) {
switch (mode) {
case PassingMode::ByValue:
out << "value";
break;
case PassingMode::ByVar:
out << "var";
break;
case PassingMode::ByRef:
out << "ref";
break;
}
};
if (!passing_modes.empty() && llvm::any_of(passing_modes, [](auto mode) {
return mode != PassingMode::ByVar;
})) {
out << ", modes: [";
llvm::ListSeparator sep;
for (auto mode : passing_modes) {
out << sep;
print_mode(mode);
}
out << "]";
}
if (self_passing_mode != PassingMode::ByRef) {
out << ", self_mode: ";
print_mode(self_passing_mode);
}
out << "}";
}
auto ClangDeclKey::Print(llvm::raw_ostream& out) const -> void {
RawStringOstream decl_stream;
auto policy = decl->getASTContext().getPrintingPolicy();
policy.TerseOutput = true;
if (isa<clang::TranslationUnitDecl>(decl)) {
decl_stream << "<translation unit>";
} else {
decl->print(decl_stream, policy);
}
out << "{decl: \"" << FormatEscaped(decl_stream.TakeStr()) << "\"";
if (signature_id != ClangDeclSignatureId::None) {
out << ", clang_decl_signature_id: " << signature_id;
}
out << "}";
}
auto ClangDecl::Print(llvm::raw_ostream& out) const -> void {
out << "{key: " << key << ", inst_id: " << inst_id << "}";
}
ClangDeclStore::ClangDeclStore(CheckIRId check_ir_id) : values_(check_ir_id) {}
auto ClangDeclStore::Add(ClangDecl value) -> ClangDeclId {
auto id = values_.Add(value);
inst_id_to_clang_decl_id_.Insert(value.inst_id, id);
return id;
}
auto ClangDeclStore::Lookup(ClangDeclKey key) const -> ClangDeclId {
return values_.Lookup(key);
}
auto ClangDeclStore::Lookup(InstId inst_id) const -> ClangDeclId {
if (auto result = inst_id_to_clang_decl_id_.Lookup(inst_id)) {
return result.value();
}
return ClangDeclId::None;
}
auto ClangDeclStore::OutputYaml() const -> Yaml::OutputMapping {
return values_.OutputYaml();
}
auto ClangDeclStore::CollectMemUsage(MemUsage& mem_usage,
llvm::StringRef label) const -> void {
values_.CollectMemUsage(mem_usage, label);
mem_usage.Collect(MemUsage::ConcatLabel(label, "inst_id_to_clang_decl_id_"),
inst_id_to_clang_decl_id_);
}
} // namespace Carbon::SemIR