Skip to content
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,
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