Support for passing C++ templates as arguments to template template parameters. (#6475)

Allow passing a C++ template as a template template argument to another
C++ template. Does not allow passing a Carbon generic as an argument.

Depends on #6474.
This commit is contained in:
Richard Smith
2025-12-16 19:00:49 +00:00
committed by GitHub
parent 3ae6f96141
commit 2e65d28a16
2 changed files with 104 additions and 21 deletions
+40 -12
View File
@@ -48,6 +48,29 @@ auto PerformCallToCppFunction(Context& context, SemIR::LocId loc_id,
}
}
// Synthesize a placeholder `void{}` template argument, that will never be a
// valid argument for any template parameter. This is used in order to get Clang
// to diagnose invalid template argument errors for us. The location of the
// Carbon expression is used as the location of the C++ expression, so
// Clang's diagnostics will point into the Carbon code.
//
// TODO: If Clang ever tries to print the type of the expression or to
// pretty-print the expression itself, it would print the wrong thing. Currently
// this doesn't appear to happen, but in principle it could. Ideally we'd add an
// extension point to Clang to represent a "foreign expression" and use it here
// instead of creating a bogus placeholder expression.
static auto MakePlaceholderTemplateArg(Context& context, SemIR::InstId arg_id)
-> clang::TemplateArgumentLoc {
auto arg_loc = GetCppLocation(context, SemIR::LocId(arg_id));
auto void_type = context.ast_context().VoidTy;
auto* arg = new (context.ast_context()) clang::CXXScalarValueInitExpr(
void_type,
context.ast_context().getTrivialTypeSourceInfo(void_type, arg_loc),
arg_loc);
return clang::TemplateArgumentLoc(
clang::TemplateArgument(arg, /*IsCanonical=*/false), arg);
}
// Converts an argument in a call to a C++ template name into a corresponding
// clang template argument, given the template parameter it will be matched
// against.
@@ -72,10 +95,22 @@ static auto ConvertArgToTemplateArg(Context& context,
}
if (isa<clang::TemplateTemplateParmDecl>(param_decl)) {
// TODO: Check the type of the argument `CppTemplateNameType` and
// convert it to a `clang::TemplateName`.
context.TODO(arg_id, "argument for template template parameter");
return std::nullopt;
auto inst = context.sem_ir().insts().Get(arg_id);
if (auto template_name_type =
context.types().TryGetAs<SemIR::CppTemplateNameType>(
inst.type_id())) {
clang::TemplateName name(cast<clang::TemplateDecl>(
context.clang_decls().Get(template_name_type->decl_id).key.decl));
return clang::TemplateArgumentLoc(
context.ast_context(), clang::TemplateArgument(name),
/*TemplateKWLoc=*/clang::SourceLocation(),
clang::NestedNameSpecifierLoc(),
GetCppLocation(context, SemIR::LocId(arg_id)));
}
// TODO: Eventually we should also support passing Carbon generics as
// template template arguments.
return MakePlaceholderTemplateArg(context, arg_id);
}
if (isa<clang::NonTypeTemplateParmDecl>(param_decl)) {
@@ -120,14 +155,7 @@ static auto ConvertArgsToTemplateArgs(Context& context,
// placeholder template arguments so that Clang will diagnose it for us.
for (auto arg_id : arg_ids) {
// Synthesize a placeholder `void{}` template argument.
auto arg_loc = GetCppLocation(context, SemIR::LocId(arg_id));
auto void_type = context.ast_context().VoidTy;
auto* arg = new (context.ast_context()) clang::CXXScalarValueInitExpr(
void_type,
context.ast_context().getTrivialTypeSourceInfo(void_type, arg_loc),
arg_loc);
arg_list.addArgument(clang::TemplateArgumentLoc(
clang::TemplateArgument(arg, /*IsCanonical=*/false), arg));
arg_list.addArgument(MakePlaceholderTemplateArg(context, arg_id));
}
return true;