Skip to content

perform type inference on template arguments #352

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

Closed
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
71 changes: 71 additions & 0 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,77 @@ namespace Cpp {
if (instantiated)
return instantiated;

if (func->getMinRequiredArguments() == 1 && arg_types.size() == 1) {
/*
reconstruct TemplateArgs for cases such as
template<class T1, class T2>
void fn(MyClass<T1, T2> arg);
by decomposing the argument's template

transforms
template_args = {MyClass<T1, T2>}
to
template_args = {T1, T2}
then performs the instantiation
*/
QualType fn_arg_type = func->getParamDecl(0)->getType();
QualType arg_type = QualType::getFromOpaquePtr(arg_types[0].m_Type);

// dereference
if (fn_arg_type->isReferenceType())
fn_arg_type = fn_arg_type.getNonReferenceType();
if (arg_type->isReferenceType())
arg_type = arg_type.getNonReferenceType();

// matching parameter and argument types
// resolving parameter
if (const auto* ET = fn_arg_type->getAs<clang::ElaboratedType>()) {
if (const auto* TST =
ET->getNamedType()
->getAs<clang::TemplateSpecializationType>()) {
if (const auto* TD = TST->getTemplateName().getAsTemplateDecl()) {
// resolving argument
if (const auto* RT = arg_type->getAs<clang::RecordType>()) {
if (auto* CTSD =
llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
RT->getDecl())) {
if (CTSD->getSpecializedTemplate()->getCanonicalDecl() ==
TD->getCanonicalDecl()) {
// parameter type matches argument type
std::vector<TemplateArgInfo> total_arg_set;

const TemplateArgumentList& TAL = CTSD->getTemplateArgs();

total_arg_set.insert(total_arg_set.end(),
explicit_types.begin(),
explicit_types.end());

for (size_t i = 0; i < TAL.size(); i++) {
// FIXME: handle the case where TemplateArgument is
// Integral value
if (TAL[i].getKind() == clang::TemplateArgument::Pack) {
for (auto i : TAL[i].pack_elements()) {
total_arg_set.emplace_back(
i.getAsType().getAsOpaquePtr());
}
} else if (TAL[i].getKind() ==
clang::TemplateArgument::Type) {
QualType TA = TAL[i].getAsType();
total_arg_set.emplace_back(TA.getAsOpaquePtr());
}
}
instantiated = InstantiateTemplate(
candidate, total_arg_set.data(), total_arg_set.size());
if (instantiated)
return instantiated;
}
}
}
}
}
}
}

// join explicit and arg_types
std::vector<TemplateArgInfo> total_arg_set;
total_arg_set.reserve(explicit_types.size() + arg_types.size());
Expand Down
61 changes: 61 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "gtest/gtest.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: 'gtest/gtest.h' file not found [clang-diagnostic-error]

#include "gtest/gtest.h"
         ^


#include <string>
#include <tuple>

using namespace TestUtils;
using namespace llvm;
Expand Down Expand Up @@ -1154,3 +1155,63 @@ TEST(FunctionReflectionTest, Destruct) {
output = testing::internal::GetCapturedStdout();
EXPECT_EQ(output, "Destructor Executed");
}

TEST(FunctionReflectionTest, NestedTemplate) {
if (llvm::sys::RunningOnValgrind())
GTEST_SKIP() << "XFAIL due to Valgrind report";

Cpp::CreateInterpreter();

Interp->declare(R"(
#include <tuple>
#include <string>
)");

ASTContext& C = Interp->getCI()->getASTContext();

std::vector<Cpp::TCppFunction_t> make_tuple_candidate_methods;
Cpp::GetClassTemplatedMethods("make_tuple", Cpp::GetScope("std"),
make_tuple_candidate_methods);
EXPECT_GE(make_tuple_candidate_methods.size(), 1);

std::vector<Cpp::TemplateArgInfo> make_tuple_arg_types = {
{C.IntTy.getAsOpaquePtr()},
{C.DoubleTy.getAsOpaquePtr()},
};
std::vector<Cpp::TemplateArgInfo> make_tuple_templ_params = {};
Cpp::TCppFunction_t make_tuple_scope = Cpp::BestTemplateFunctionMatch(
make_tuple_candidate_methods, make_tuple_templ_params,
make_tuple_arg_types);
EXPECT_TRUE(make_tuple_scope);

auto make_tuple = Cpp::MakeFunctionCallable(make_tuple_scope);
EXPECT_TRUE(make_tuple);

int x = 2;
double y = 4.0;
void* args0[2] = {(void*)&x, (void*)&y};
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: do not declare C-style arrays, use std::array<> instead [cppcoreguidelines-avoid-c-arrays]

  void* args0[2] = {(void*)&x, (void*)&y};
  ^

void* tuple = new std::tuple<int, double>;
make_tuple.Invoke(tuple, {args0, 2});
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay]

  make_tuple.Invoke(tuple, {args0, 2});
                            ^


std::vector<Cpp::TCppFunction_t> get_candidate_methods;
Cpp::GetClassTemplatedMethods("get", Cpp::GetScope("std"),
get_candidate_methods);
EXPECT_GE(get_candidate_methods.size(), 1);

std::vector<Cpp::TemplateArgInfo> get_arg_types = {
{Cpp::GetFunctionReturnType(make_tuple_scope)},
};
std::vector<Cpp::TemplateArgInfo> get_templ_params = {
{C.IntTy.getAsOpaquePtr(), "0"}};
Cpp::TCppFunction_t get_scope = Cpp::BestTemplateFunctionMatch(
get_candidate_methods, get_templ_params, get_arg_types);
EXPECT_TRUE(get_scope);

auto get = Cpp::MakeFunctionCallable(get_scope);
EXPECT_TRUE(get);

// int get0_result = 0;
// void *args1[1] = { (void *) &tuple };
// get.Invoke(&get0_result, {args1, 1});
// EXPECT_EQ(get0_result, 2);
}
Loading