Skip to content

Commit 2484e20

Browse files
authored
Merge branch 'main' into wip]-add-automated-wasm-tests
2 parents 906de42 + 2b184ce commit 2484e20

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/Interpreter/CppInterOp.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1862,10 +1862,22 @@ namespace Cpp {
18621862
{
18631863
std::string name;
18641864
{
1865-
llvm::raw_string_ostream stream(name);
1865+
std::string complete_name;
1866+
llvm::raw_string_ostream stream(complete_name);
18661867
FD->getNameForDiagnostic(stream,
18671868
FD->getASTContext().getPrintingPolicy(),
18681869
/*Qualified=*/false);
1870+
1871+
// insert space between template argument list and the function name
1872+
// this is require if the function is `operator<`
1873+
// `operator<<type1, type2, ...>` is invalid syntax
1874+
// whereas `operator< <type1, type2, ...>` is valid
1875+
std::string simple_name = FD->getNameAsString();
1876+
size_t idx = complete_name.find(simple_name, 0) + simple_name.size();
1877+
std::string name_without_template_args = complete_name.substr(0, idx);
1878+
std::string template_args = complete_name.substr(idx);
1879+
name = name_without_template_args +
1880+
(template_args.empty() ? "" : " " + template_args);
18691881
}
18701882
callbuf << name;
18711883
}

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,34 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
10551055
FCI_f.Invoke(&res, {nullptr, 0});
10561056
EXPECT_TRUE(res);
10571057
#endif
1058+
1059+
// templated operators
1060+
Interp->process(R"(
1061+
class TOperator{
1062+
public:
1063+
template<typename T>
1064+
bool operator<(T t) { return true; }
1065+
};
1066+
)");
1067+
Cpp::TCppScope_t TOperator = Cpp::GetNamed("TOperator");
1068+
1069+
auto* TOperatorCtor = Cpp::GetDefaultConstructor(TOperator);
1070+
auto FCI_TOperatorCtor = Cpp::MakeFunctionCallable(TOperatorCtor);
1071+
void* toperator = nullptr;
1072+
FCI_TOperatorCtor.Invoke((void*)&toperator);
1073+
1074+
EXPECT_TRUE(toperator);
1075+
std::vector<Cpp::TCppScope_t> operators;
1076+
Cpp::GetOperator(TOperator, Cpp::OP_Less, operators);
1077+
EXPECT_EQ(operators.size(), 1);
1078+
1079+
Cpp::TCppScope_t op_templated = operators[0];
1080+
auto TAI = Cpp::TemplateArgInfo(Cpp::GetType("int"));
1081+
Cpp::TCppScope_t op = Cpp::InstantiateTemplate(op_templated, &TAI, 1);
1082+
auto FCI_op = Cpp::MakeFunctionCallable(op);
1083+
bool boolean = false;
1084+
FCI_op.Invoke((void*)&boolean, {args, /*args_size=*/1}, object);
1085+
EXPECT_TRUE(boolean);
10581086
}
10591087

10601088
TEST(FunctionReflectionTest, IsConstMethod) {

0 commit comments

Comments
 (0)