Skip to content

Commit 4ff0941

Browse files
add GetTemplateInstantiatedFunctions
lookup instantated templated functions
1 parent bc728d3 commit 4ff0941

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,15 @@ CPPINTEROP_API void
426426
GetFunctionTemplatedDecls(TCppScope_t klass,
427427
std::vector<TCppFunction_t>& methods);
428428

429+
/// Returns instantiated functions with the given templated function name
430+
///\param[in] name - name of the templated function
431+
///\param[in] scope - Pointer to the scope/class under which the functions have
432+
/// to be retrieved
433+
///\param[out] methods - Vector of methods in the class
434+
CPPINTEROP_API void
435+
GetTemplateInstantiatedFunctions(const std::string& name, TCppScope_t scope,
436+
std::vector<TCppFunction_t>& methods);
437+
429438
///\returns if a class has a default constructor.
430439
CPPINTEROP_API bool HasDefaultConstructor(TCppScope_t scope);
431440

lib/CppInterOp/CppInterOp.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,21 @@ void GetFunctionTemplatedDecls(TCppScope_t klass,
863863
GetClassDecls<FunctionTemplateDecl>(klass, methods);
864864
}
865865

866+
void GetTemplateInstantiatedFunctions(const std::string& name,
867+
TCppScope_t scope,
868+
std::vector<TCppFunction_t>& methods) {
869+
std::vector<TCppFunction_t> tmpl_funcs;
870+
GetClassTemplatedMethods(name, scope, tmpl_funcs);
871+
for (TCppFunction_t i : tmpl_funcs) {
872+
auto* D = static_cast<Decl*>(i);
873+
if (auto* FTD = llvm::dyn_cast<FunctionTemplateDecl>(D)) {
874+
for (FunctionDecl* FD : FTD->specializations()) {
875+
methods.push_back(FD);
876+
}
877+
}
878+
}
879+
}
880+
866881
bool HasDefaultConstructor(TCppScope_t scope) {
867882
auto* D = (clang::Decl*)scope;
868883

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,26 @@ TEST(FunctionReflectionTest, GetFunctionTemplatedDecls) {
345345
EXPECT_EQ(Cpp::GetName(template_methods[3]), Cpp::GetName(SubDecls[6]));
346346
}
347347

348+
TEST(FunctionReflectionTest, GetTemplateInstantiatedFunctions) {
349+
Cpp::Declare(R"(
350+
template<class T>
351+
T my_templated_function(T t) { return t; }
352+
353+
template char my_templated_function<char>(char);
354+
template double my_templated_function<double>(double);
355+
)");
356+
357+
std::vector<Cpp::TCppFunction_t> template_methods;
358+
Cpp::GetTemplateInstantiatedFunctions(
359+
"my_templated_function", Cpp::GetGlobalScope(), template_methods);
360+
361+
EXPECT_EQ(template_methods.size(), 2);
362+
EXPECT_EQ(Cpp::GetFunctionSignature(template_methods[0]),
363+
"template<> char my_templated_function<char>(char t)");
364+
EXPECT_EQ(Cpp::GetFunctionSignature(template_methods[1]),
365+
"template<> double my_templated_function<double>(double t)");
366+
}
367+
348368
TEST(FunctionReflectionTest, GetFunctionReturnType) {
349369
std::vector<Decl*> Decls, SubDecls, TemplateSubDecls;
350370
std::string code = R"(

0 commit comments

Comments
 (0)