Skip to content

Commit

Permalink
Add variadic support for templates
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronj0 committed Sep 19, 2024
1 parent ea73105 commit 9412530
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2927,10 +2927,17 @@ namespace Cpp {
Sema& S) {
// Create a list of template arguments.
TemplateArgumentListInfo TLI{};
for (auto TA : TemplateArgs)
TLI.addArgument(S.getTrivialTemplateArgumentLoc(TA,QualType(),
SourceLocation()));

for (auto TA : TemplateArgs) {
if (TA.getKind() == TemplateArgument::ArgKind::Pack) {
for (auto PTA : TA.getPackAsArray()) {
TLI.addArgument(S.getTrivialTemplateArgumentLoc(PTA, QualType(),
SourceLocation()));
}
} else {
TLI.addArgument(
S.getTrivialTemplateArgumentLoc(TA, QualType(), SourceLocation()));
}
}
return InstantiateTemplate(TemplateD, TLI, S);
}

Expand Down
28 changes: 28 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,34 @@ TEST(FunctionReflectionTest, InstantiateTemplateMethod) {
EXPECT_TRUE(TA1.getAsType()->isIntegerType());
}

TEST(FunctionReflectionTest, InstantiateVariadicFunctionTemplate) {
std::vector<Decl*> Decls;
std::string code = R"(
template<typename... Args> void VariadicFnTemplate(Args... args) {}
)";

GetAllTopLevelDecls(code, Decls);
ASTContext& C = Interp->getCI()->getASTContext();

// Example instantiation with int and double
std::vector<Cpp::TemplateArgInfo> args1 = {C.IntTy.getAsOpaquePtr(),
C.DoubleTy.getAsOpaquePtr()};
auto Instance1 = Cpp::InstantiateTemplate(Decls[0], args1.data(),
/*type_size*/ args1.size());
// EXPECT_TRUE(isa<FunctionTemplateDecl>((Decl*)Instance1));
FunctionDecl* FD = cast<FunctionDecl>((Decl*)Instance1);
FunctionDecl* FnTD1 = FD->getTemplateInstantiationPattern();
EXPECT_TRUE(FnTD1->isThisDeclarationADefinition());
// TemplateArgument TA1 = FD->getTemplateSpecializationArgs()->get(0);

// // Validate the first argument is of integer type
// EXPECT_TRUE(TA1.getAsType()->isIntegerType());

// // Validate the second argument is of double type
// TemplateArgument TA2 = FD->getTemplateSpecializationArgs()->get(1);
// EXPECT_TRUE(TA2.getAsType()->isFloatingType());
}

TEST(FunctionReflectionTest, BestTemplateFunctionMatch) {
std::vector<Decl*> Decls;
std::string code = R"(
Expand Down

0 comments on commit 9412530

Please sign in to comment.