Support list initialization of C++ classes that is performed via a constructor call. (#6660)

The general strategy here is to import the constructor with a signature
that directly matches the argument. The intent is that the imported
function will eventually be usable directly as the `ImplicitAs.Convert`
function in a generated `impl`.

For initialization from a tuple, for example `(1, 2)`, we import the
selected constructor with a signature that takes a tuple pattern:

  `fn Class.Class((a: i32, b: i32)) -> Class;`

In order to support that, this PR also adds support in general for tuple
patterns in function signatures. It turns out the implementation was
already very close to allowing this.

Assisted-by: Gemini 3 Pro via Antigravity
This commit is contained in:
Richard Smith
2026-01-27 22:04:21 +00:00
committed by GitHub
parent 9f6e84cc02
commit e69c3fd978
18 changed files with 2151 additions and 1118 deletions
+5 -4
View File
@@ -53,7 +53,7 @@ namespace {
// See `GetDeclForCoreInterface`.
struct DeclInfo {
clang::NamedDecl* decl;
int num_params;
SemIR::ClangDeclKey::Signature signature;
};
} // namespace
@@ -68,9 +68,10 @@ auto GetDeclForCoreInterface(clang::Sema& clang_sema,
case CoreInterface::Copy:
return {.decl = clang_sema.LookupCopyingConstructor(
class_decl, clang::Qualifiers::Const),
.num_params = 1};
.signature = {.num_params = 1}};
case CoreInterface::Destroy:
return {.decl = clang_sema.LookupDestructor(class_decl), .num_params = 0};
return {.decl = clang_sema.LookupDestructor(class_decl),
.signature = {.num_params = 0}};
case CoreInterface::Unknown:
CARBON_FATAL("shouldn't be called with `Unknown`");
}
@@ -102,7 +103,7 @@ auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
}
auto fn_id =
ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info.num_params);
ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info.signature);
if (fn_id == SemIR::ErrorInst::InstId) {
return SemIR::ErrorInst::InstId;
}