Skip to content

add GetTemplateInstantiatedFunctions #642

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

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
9 changes: 9 additions & 0 deletions include/CppInterOp/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ CPPINTEROP_API void
GetFunctionTemplatedDecls(TCppScope_t klass,
std::vector<TCppFunction_t>& methods);

/// Returns instantiated functions with the given templated function name
///\param[in] name - name of the templated function
///\param[in] scope - Pointer to the scope/class under which the functions have
/// to be retrieved
///\param[out] methods - Vector of methods in the class
CPPINTEROP_API void
GetTemplateInstantiatedFunctions(const std::string& name, TCppScope_t scope,
std::vector<TCppFunction_t>& methods);

///\returns if a class has a default constructor.
CPPINTEROP_API bool HasDefaultConstructor(TCppScope_t scope);

Expand Down
15 changes: 15 additions & 0 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,21 @@ void GetFunctionTemplatedDecls(TCppScope_t klass,
GetClassDecls<FunctionTemplateDecl>(klass, methods);
}

void GetTemplateInstantiatedFunctions(const std::string& name,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why would we need all possible instantiations? That encourages iteration other expensive operations.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Required here.

In cppyy, if there is an instantiation of a templated function, then it is treated as an overloaded function. This also prevents instantiation because we have already extracted the instantiated functions.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should avoid iteration. Perhaps we can use some level of FindInstantiation or similar, which does not push everything onto a vector…

TCppScope_t scope,
std::vector<TCppFunction_t>& methods) {
std::vector<TCppFunction_t> tmpl_funcs;
GetClassTemplatedMethods(name, scope, tmpl_funcs);
for (TCppFunction_t i : tmpl_funcs) {
auto* D = static_cast<Decl*>(i);
if (auto* FTD = llvm::dyn_cast<FunctionTemplateDecl>(D)) {
for (FunctionDecl* FD : FTD->specializations()) {
methods.push_back(FD);
}
}
}
}

bool HasDefaultConstructor(TCppScope_t scope) {
auto* D = (clang::Decl*)scope;

Expand Down
20 changes: 20 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,26 @@ TEST(FunctionReflectionTest, GetFunctionTemplatedDecls) {
EXPECT_EQ(Cpp::GetName(template_methods[3]), Cpp::GetName(SubDecls[6]));
}

TEST(FunctionReflectionTest, GetTemplateInstantiatedFunctions) {
Cpp::Declare(R"(
template<class T>
T my_templated_function(T t) { return t; }

template char my_templated_function<char>(char);
template double my_templated_function<double>(double);
)");

std::vector<Cpp::TCppFunction_t> template_methods;
Cpp::GetTemplateInstantiatedFunctions(
"my_templated_function", Cpp::GetGlobalScope(), template_methods);

EXPECT_EQ(template_methods.size(), 2);
EXPECT_EQ(Cpp::GetFunctionSignature(template_methods[0]),
"template<> char my_templated_function<char>(char t)");
EXPECT_EQ(Cpp::GetFunctionSignature(template_methods[1]),
"template<> double my_templated_function<double>(double t)");
}

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