Skip to content

Commit ad55091

Browse files
committed
Add variadic support for templates
1 parent b810c6e commit ad55091

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/Interpreter/CppInterOp.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -3081,10 +3081,17 @@ namespace Cpp {
30813081
ArrayRef<TemplateArgument> TemplateArgs, Sema& S) {
30823082
// Create a list of template arguments.
30833083
TemplateArgumentListInfo TLI{};
3084-
for (auto TA : TemplateArgs)
3085-
TLI.addArgument(S.getTrivialTemplateArgumentLoc(TA,QualType(),
3086-
SourceLocation()));
3087-
3084+
for (auto TA : TemplateArgs) {
3085+
if (TA.getKind() == TemplateArgument::ArgKind::Pack) {
3086+
for (auto PTA : TA.getPackAsArray()) {
3087+
TLI.addArgument(S.getTrivialTemplateArgumentLoc(PTA, QualType(),
3088+
SourceLocation()));
3089+
}
3090+
} else {
3091+
TLI.addArgument(
3092+
S.getTrivialTemplateArgumentLoc(TA, QualType(), SourceLocation()));
3093+
}
3094+
}
30883095
return InstantiateTemplate(TemplateD, TLI, S);
30893096
}
30903097

unittests/CppInterOp/FunctionReflectionTest.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,34 @@ TEST(FunctionReflectionTest, InstantiateTemplateMethod) {
589589
EXPECT_TRUE(TA1.getAsType()->isIntegerType());
590590
}
591591

592+
TEST(FunctionReflectionTest, InstantiateVariadicFunctionTemplate) {
593+
std::vector<Decl*> Decls;
594+
std::string code = R"(
595+
template<typename... Args> void VariadicFnTemplate(Args... args) {}
596+
)";
597+
598+
GetAllTopLevelDecls(code, Decls);
599+
ASTContext& C = Interp->getCI()->getASTContext();
600+
601+
// Example instantiation with int and double
602+
std::vector<Cpp::TemplateArgInfo> args1 = {C.IntTy.getAsOpaquePtr(),
603+
C.DoubleTy.getAsOpaquePtr()};
604+
auto Instance1 = Cpp::InstantiateTemplate(Decls[0], args1.data(),
605+
/*type_size*/ args1.size());
606+
// EXPECT_TRUE(isa<FunctionTemplateDecl>((Decl*)Instance1));
607+
FunctionDecl* FD = cast<FunctionDecl>((Decl*)Instance1);
608+
FunctionDecl* FnTD1 = FD->getTemplateInstantiationPattern();
609+
EXPECT_TRUE(FnTD1->isThisDeclarationADefinition());
610+
// TemplateArgument TA1 = FD->getTemplateSpecializationArgs()->get(0);
611+
612+
// // Validate the first argument is of integer type
613+
// EXPECT_TRUE(TA1.getAsType()->isIntegerType());
614+
615+
// // Validate the second argument is of double type
616+
// TemplateArgument TA2 = FD->getTemplateSpecializationArgs()->get(1);
617+
// EXPECT_TRUE(TA2.getAsType()->isFloatingType());
618+
}
619+
592620
TEST(FunctionReflectionTest, BestTemplateFunctionMatch) {
593621
std::vector<Decl*> Decls;
594622
std::string code = R"(

0 commit comments

Comments
 (0)