C++ interop: support for default arguments. (#6108)

The general strategy here is to force use of a thunk when we want to use
default arguments, and have Clang generate uses of the default arguments
on its side of the thunk.

To support this, change the key type used in `clang_decls` from being
just a `Decl*` to being a pair of `Decl*` and number of parameters in
the case of function decls. Import distinct `SemIR::Function`s for each
number of parameters that's used, and corresponding distinct thunks.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
Richard Smith
2025-09-24 01:07:59 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent 82679e6689
commit 1e7b7e53ae
42 changed files with 2097 additions and 720 deletions
+15 -1
View File
@@ -437,6 +437,20 @@ auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id,
return nullptr;
}
// Don't lower C++ functions that use a thunk. We will never reference them
// directly, and their signatures would not be expected to match the
// corresponding C++ function anyway.
if (function.special_function_kind ==
SemIR::Function::SpecialFunctionKind::HasCppThunk) {
// Make sure Clang emits this function.
// TODO: This shouldn't be necessary: Clang should emit definitions of
// functions that it emits calls to. But this doesn't currently work.
auto clang_decl_id = sem_ir().functions().Get(function_id).clang_decl_id;
HandleReferencedCppFunction(cast<clang::FunctionDecl>(
sem_ir().clang_decls().Get(clang_decl_id).key.decl));
return nullptr;
}
// TODO: Consider tracking whether the function has been used, and only
// lowering it if it's needed.
@@ -467,7 +481,7 @@ auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id,
CARBON_CHECK(!specific_id.has_value(),
"Specific functions cannot have C++ definitions");
HandleReferencedCppFunction(
sem_ir().clang_decls().Get(clang_decl_id).decl->getAsFunction());
sem_ir().clang_decls().Get(clang_decl_id).key.decl->getAsFunction());
// TODO: Check that the signature and mangling generated by Clang and the
// one we generated are the same.
}