Skip to content

Commit c25757a

Browse files
Vipul-Cariappamcbarton
authored andcommitted
fix JitCall codegen for function with lambda as return value
1 parent 42b7e18 commit c25757a

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
@@ -1964,9 +1964,24 @@ void collect_type_info(const FunctionDecl* FD, QualType& QT,
19641964
PrintingPolicy Policy(C.getPrintingPolicy());
19651965
Policy.SuppressElaboration = true;
19661966
refType = kNotReference;
1967-
if (QT->isRecordType() && forArgument) {
1968-
get_type_as_string(QT, type_name, C, Policy);
1969-
return;
1967+
if (QT->isRecordType()) {
1968+
if (forArgument) {
1969+
get_type_as_string(QT, type_name, C, Policy);
1970+
return;
1971+
}
1972+
if (auto* CXXRD = QT->getAsCXXRecordDecl()) {
1973+
if (CXXRD->isLambda()) {
1974+
std::string fn_name;
1975+
llvm::raw_string_ostream stream(fn_name);
1976+
Policy.FullyQualifiedName = true;
1977+
Policy.SuppressUnwrittenScope = true;
1978+
FD->getNameForDiagnostic(stream, Policy,
1979+
/*Qualified=*/false);
1980+
type_name = "__internal_CppInterOp::function<decltype(" + fn_name +
1981+
")>::result_type";
1982+
return;
1983+
}
1984+
}
19701985
}
19711986
if (QT->isFunctionPointerType()) {
19721987
std::string fp_typedef_name;
@@ -3221,6 +3236,17 @@ TInterp_t CreateInterpreter(const std::vector<const char*>& Args /*={}*/,
32213236
llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
32223237
}
32233238

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

32263252
return I;

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,19 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
21112111

21122112
auto op_callable = Cpp::MakeFunctionCallable(op);
21132113
EXPECT_EQ(op_callable.getKind(), Cpp::JitCall::kGenericCall);
2114+
2115+
Cpp::Declare(R"(
2116+
auto get_fn(int x) { return [x](int y){ return x + y; }; }
2117+
)");
2118+
2119+
Cpp::TCppScope_t get_fn = Cpp::GetNamed("get_fn");
2120+
EXPECT_TRUE(get_fn);
2121+
2122+
auto get_fn_callable = Cpp::MakeFunctionCallable(get_fn);
2123+
EXPECT_EQ(get_fn_callable.getKind(), Cpp::JitCall::kGenericCall);
2124+
2125+
EXPECT_TRUE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(get_fn)));
2126+
EXPECT_FALSE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(bar)));
21142127
}
21152128

21162129
TEST(FunctionReflectionTest, IsConstMethod) {

0 commit comments

Comments
 (0)