diff --git a/include/CppInterOp/CppInterOp.h b/include/CppInterOp/CppInterOp.h index 2f3d330db..5b6e7398c 100644 --- a/include/CppInterOp/CppInterOp.h +++ b/include/CppInterOp/CppInterOp.h @@ -426,6 +426,15 @@ CPPINTEROP_API void GetFunctionTemplatedDecls(TCppScope_t klass, std::vector& 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& methods); + ///\returns if a class has a default constructor. CPPINTEROP_API bool HasDefaultConstructor(TCppScope_t scope); diff --git a/lib/CppInterOp/CppInterOp.cpp b/lib/CppInterOp/CppInterOp.cpp index b7fe67392..4e9e2d658 100755 --- a/lib/CppInterOp/CppInterOp.cpp +++ b/lib/CppInterOp/CppInterOp.cpp @@ -863,6 +863,21 @@ void GetFunctionTemplatedDecls(TCppScope_t klass, GetClassDecls(klass, methods); } +void GetTemplateInstantiatedFunctions(const std::string& name, + TCppScope_t scope, + std::vector& methods) { + std::vector tmpl_funcs; + GetClassTemplatedMethods(name, scope, tmpl_funcs); + for (TCppFunction_t i : tmpl_funcs) { + auto* D = static_cast(i); + if (auto* FTD = llvm::dyn_cast(D)) { + for (FunctionDecl* FD : FTD->specializations()) { + methods.push_back(FD); + } + } + } +} + bool HasDefaultConstructor(TCppScope_t scope) { auto* D = (clang::Decl*)scope; diff --git a/unittests/CppInterOp/FunctionReflectionTest.cpp b/unittests/CppInterOp/FunctionReflectionTest.cpp index 4252c86b0..0c26ce9bf 100644 --- a/unittests/CppInterOp/FunctionReflectionTest.cpp +++ b/unittests/CppInterOp/FunctionReflectionTest.cpp @@ -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 + T my_templated_function(T t) { return t; } + + template char my_templated_function(char); + template double my_templated_function(double); + )"); + + std::vector 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 t)"); + EXPECT_EQ(Cpp::GetFunctionSignature(template_methods[1]), + "template<> double my_templated_function(double t)"); +} + TEST(FunctionReflectionTest, GetFunctionReturnType) { std::vector Decls, SubDecls, TemplateSubDecls; std::string code = R"(