Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variadic support for templates #324

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3081,10 +3081,17 @@
ArrayRef<TemplateArgument> TemplateArgs, 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(),

Check warning on line 3087 in lib/Interpreter/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CppInterOp.cpp#L3086-L3087

Added lines #L3086 - L3087 were not covered by tests
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) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we include the example of #305?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current test mimics the same variadic template, I am extending it to initialize using a template pack which should cover the other branch for codecov

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still worth adding the test from the issue and adding code to support it.

)";

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