Skip to content

Commit f59f2cf

Browse files
committed
Improve template instantiation for GetMethodTemplate
1 parent 1c35b41 commit f59f2cf

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/Interpreter/CppInterOp.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,8 +1004,9 @@ namespace Cpp {
10041004
continue;
10051005
#endif
10061006

1007-
// FIXME : first score based on the type similarity before forcing
1007+
// TODO(aaronj0) : first score based on the type similarity before forcing
10081008
// instantiation.
1009+
10091010
TCppFunction_t instantiated =
10101011
InstantiateTemplate(candidate, arg_types.data(), arg_types.size());
10111012
if (instantiated)
@@ -1018,6 +1019,19 @@ namespace Cpp {
10181019
explicit_types.size());
10191020
if (instantiated)
10201021
return instantiated;
1022+
1023+
// join explicit and arg_types
1024+
std::vector<TemplateArgInfo> total_arg_set;
1025+
total_arg_set.reserve(explicit_types.size() + arg_types.size());
1026+
total_arg_set.insert(total_arg_set.end(), explicit_types.begin(),
1027+
explicit_types.end());
1028+
total_arg_set.insert(total_arg_set.end(), arg_types.begin(),
1029+
arg_types.end());
1030+
1031+
instantiated = InstantiateTemplate(candidate, total_arg_set.data(),
1032+
total_arg_set.size());
1033+
if (instantiated)
1034+
return instantiated;
10211035
}
10221036
return nullptr;
10231037
}

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ TEST(FunctionReflectionTest, BestTemplateFunctionMatch) {
597597
template<class A> long get_size(A&);
598598
template<class A> long get_size();
599599
template<class A, class B> long get_size(A a, B b);
600+
template<class A> long add_size(float a);
600601
};
601602
602603
template<class A>
@@ -609,6 +610,11 @@ TEST(FunctionReflectionTest, BestTemplateFunctionMatch) {
609610
return sizeof(A) + 1;
610611
}
611612
613+
template<class A>
614+
long MyTemplatedMethodClass::add_size(float a) {
615+
return sizeof(A) + long(a);
616+
}
617+
612618
template<class A, class B>
613619
long MyTemplatedMethodClass::get_size(A a, B b) {
614620
return sizeof(A) + sizeof(B);
@@ -626,21 +632,24 @@ TEST(FunctionReflectionTest, BestTemplateFunctionMatch) {
626632
std::vector<Cpp::TemplateArgInfo> args0;
627633
std::vector<Cpp::TemplateArgInfo> args1 = {C.IntTy.getAsOpaquePtr()};
628634
std::vector<Cpp::TemplateArgInfo> args2 = {C.CharTy.getAsOpaquePtr(), C.FloatTy.getAsOpaquePtr()};
635+
std::vector<Cpp::TemplateArgInfo> args3 = {C.FloatTy.getAsOpaquePtr()};
629636

630637
std::vector<Cpp::TemplateArgInfo> explicit_args0;
631638
std::vector<Cpp::TemplateArgInfo> explicit_args1 = {C.IntTy.getAsOpaquePtr()};
632-
633639

634640
Cpp::TCppFunction_t func1 = Cpp::BestTemplateFunctionMatch(candidates, explicit_args0, args1);
635641
Cpp::TCppFunction_t func2 = Cpp::BestTemplateFunctionMatch(candidates, explicit_args1, args0);
636642
Cpp::TCppFunction_t func3 = Cpp::BestTemplateFunctionMatch(candidates, explicit_args0, args2);
643+
Cpp::TCppFunction_t func4 = Cpp::BestTemplateFunctionMatch(candidates, explicit_args1, args3);
637644

638645
EXPECT_EQ(Cpp::GetFunctionSignature(func1),
639646
"template<> long MyTemplatedMethodClass::get_size<int>(int &)");
640647
EXPECT_EQ(Cpp::GetFunctionSignature(func2),
641648
"template<> long MyTemplatedMethodClass::get_size<int>()");
642649
EXPECT_EQ(Cpp::GetFunctionSignature(func3),
643650
"template<> long MyTemplatedMethodClass::get_size<char, float>(char a, float b)");
651+
EXPECT_EQ(Cpp::GetFunctionSignature(func4),
652+
"template<> long MyTemplatedMethodClass::get_size<float>(float &)");
644653
}
645654

646655
TEST(FunctionReflectionTest, IsPublicMethod) {

0 commit comments

Comments
 (0)