Skip to content

Commit dba60ff

Browse files
fix JitCall codegen for function with lambda as return value (#678)
1 parent 11c0183 commit dba60ff

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/CppInterOp/CppInterOp.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,9 +1966,24 @@ void collect_type_info(const FunctionDecl* FD, QualType& QT,
19661966
PrintingPolicy Policy(C.getPrintingPolicy());
19671967
Policy.SuppressElaboration = true;
19681968
refType = kNotReference;
1969-
if (QT->isRecordType() && forArgument) {
1970-
get_type_as_string(QT, type_name, C, Policy);
1971-
return;
1969+
if (QT->isRecordType()) {
1970+
if (forArgument) {
1971+
get_type_as_string(QT, type_name, C, Policy);
1972+
return;
1973+
}
1974+
if (auto* CXXRD = QT->getAsCXXRecordDecl()) {
1975+
if (CXXRD->isLambda()) {
1976+
std::string fn_name;
1977+
llvm::raw_string_ostream stream(fn_name);
1978+
Policy.FullyQualifiedName = true;
1979+
Policy.SuppressUnwrittenScope = true;
1980+
FD->getNameForDiagnostic(stream, Policy,
1981+
/*Qualified=*/false);
1982+
type_name = "__internal_CppInterOp::function<decltype(" + fn_name +
1983+
")>::result_type";
1984+
return;
1985+
}
1986+
}
19721987
}
19731988
if (QT->isFunctionPointerType()) {
19741989
std::string fp_typedef_name;
@@ -3223,6 +3238,17 @@ TInterp_t CreateInterpreter(const std::vector<const char*>& Args /*={}*/,
32233238
llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
32243239
}
32253240

3241+
I->declare(R"(
3242+
namespace __internal_CppInterOp {
3243+
template <typename Signature>
3244+
struct function;
3245+
template <typename Res, typename... ArgTypes>
3246+
struct function<Res(ArgTypes...)> {
3247+
typedef Res result_type;
3248+
};
3249+
} // namespace __internal_CppInterOp
3250+
)");
3251+
32263252
sInterpreters->emplace_back(I, /*Owned=*/true);
32273253

32283254
return I;

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,19 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
21542154
EXPECT_TRUE(obj);
21552155
Cpp::Destruct(obj, MyNameSpace_TemplatedEnum_instantiated);
21562156
obj = nullptr;
2157+
2158+
Cpp::Declare(R"(
2159+
auto get_fn(int x) { return [x](int y){ return x + y; }; }
2160+
)");
2161+
2162+
Cpp::TCppScope_t get_fn = Cpp::GetNamed("get_fn");
2163+
EXPECT_TRUE(get_fn);
2164+
2165+
auto get_fn_callable = Cpp::MakeFunctionCallable(get_fn);
2166+
EXPECT_EQ(get_fn_callable.getKind(), Cpp::JitCall::kGenericCall);
2167+
2168+
EXPECT_TRUE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(get_fn)));
2169+
EXPECT_FALSE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(bar)));
21572170
}
21582171

21592172
TEST(FunctionReflectionTest, IsConstMethod) {

0 commit comments

Comments
 (0)