Skip to content

Commit 816474a

Browse files
fix JitCall codegen for function with lambda as return value
1 parent eafe658 commit 816474a

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
@@ -1942,9 +1942,24 @@ void collect_type_info(const FunctionDecl* FD, QualType& QT,
19421942
PrintingPolicy Policy(C.getPrintingPolicy());
19431943
Policy.SuppressElaboration = true;
19441944
refType = kNotReference;
1945-
if (QT->isRecordType() && forArgument) {
1946-
get_type_as_string(QT, type_name, C, Policy);
1947-
return;
1945+
if (QT->isRecordType()) {
1946+
if (forArgument) {
1947+
get_type_as_string(QT, type_name, C, Policy);
1948+
return;
1949+
}
1950+
if (auto* CXXRD = QT->getAsCXXRecordDecl()) {
1951+
if (CXXRD->isLambda()) {
1952+
std::string fn_name;
1953+
llvm::raw_string_ostream stream(fn_name);
1954+
Policy.FullyQualifiedName = true;
1955+
Policy.SuppressUnwrittenScope = true;
1956+
FD->getNameForDiagnostic(stream, Policy,
1957+
/*Qualified=*/false);
1958+
type_name = "__internal_CppInterOp::function<decltype(" + fn_name +
1959+
")>::result_type";
1960+
return;
1961+
}
1962+
}
19481963
}
19491964
if (QT->isFunctionPointerType()) {
19501965
std::string fp_typedef_name;
@@ -3199,6 +3214,17 @@ TInterp_t CreateInterpreter(const std::vector<const char*>& Args /*={}*/,
31993214
llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
32003215
}
32013216

3217+
I->declare(R"(
3218+
namespace __internal_CppInterOp {
3219+
template <typename Signature>
3220+
struct function;
3221+
template <typename Res, typename... ArgTypes>
3222+
struct function<Res(ArgTypes...)> {
3223+
typedef Res result_type;
3224+
};
3225+
} // namespace __internal_CppInterOp
3226+
)");
3227+
32023228
sInterpreters->emplace_back(I, /*Owned=*/true);
32033229

32043230
return I;

unittests/CppInterOp/FunctionReflectionTest.cpp

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

20692069
auto op_callable = Cpp::MakeFunctionCallable(op);
20702070
EXPECT_EQ(op_callable.getKind(), Cpp::JitCall::kGenericCall);
2071+
2072+
Cpp::Declare(R"(
2073+
auto get_fn(int x) { return [x](int y){ return x + y; }; }
2074+
)");
2075+
2076+
Cpp::TCppScope_t get_fn = Cpp::GetNamed("get_fn");
2077+
EXPECT_TRUE(get_fn);
2078+
2079+
auto get_fn_callable = Cpp::MakeFunctionCallable(get_fn);
2080+
EXPECT_EQ(get_fn_callable.getKind(), Cpp::JitCall::kGenericCall);
2081+
2082+
EXPECT_TRUE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(get_fn)));
2083+
EXPECT_FALSE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(bar)));
20712084
}
20722085

20732086
TEST(FunctionReflectionTest, IsConstMethod) {

0 commit comments

Comments
 (0)