Skip to content

fix JitCall codegen for function with lambda as return value #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/CppInterOp/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ void GetEnumConstantDatamembers(TCppScope_t scope,
CPPINTEROP_API TCppScope_t LookupDatamember(const std::string& name,
TCppScope_t parent);

/// Check if the given type is a lamda class
bool IsLambdaClass(TCppType_t type);

/// Gets the type of the variable that is passed as a parameter.
CPPINTEROP_API TCppType_t GetVariableType(TCppScope_t var);

Expand Down
40 changes: 37 additions & 3 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,14 @@ TCppScope_t LookupDatamember(const std::string& name, TCppScope_t parent) {
return 0;
}

bool IsLambdaClass(TCppType_t type) {
QualType QT = QualType::getFromOpaquePtr(type);
if (auto* CXXRD = QT->getAsCXXRecordDecl()) {
return CXXRD->isLambda();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI isLambdaCallOperator in clang.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isLambdaCallOperator takes a CXXMethodDecl, but we are using QualType to check here.
Also, we are not checking if the function is the lambda call operator; instead, we are checking if the given class is a lambda class.

}
return false;
}

TCppType_t GetVariableType(TCppScope_t var) {
auto* D = static_cast<Decl*>(var);

Expand Down Expand Up @@ -1934,9 +1942,24 @@ void collect_type_info(const FunctionDecl* FD, QualType& QT,
PrintingPolicy Policy(C.getPrintingPolicy());
Policy.SuppressElaboration = true;
refType = kNotReference;
if (QT->isRecordType() && forArgument) {
get_type_as_string(QT, type_name, C, Policy);
return;
if (QT->isRecordType()) {
if (forArgument) {
get_type_as_string(QT, type_name, C, Policy);
return;
}
if (auto* CXXRD = QT->getAsCXXRecordDecl()) {
if (CXXRD->isLambda()) {
std::string fn_name;
llvm::raw_string_ostream stream(fn_name);
Policy.FullyQualifiedName = true;
Policy.SuppressUnwrittenScope = true;
FD->getNameForDiagnostic(stream, Policy,
/*Qualified=*/false);
type_name = "__internal_CppInterOp::function<decltype(" + fn_name +
")>::result_type";
return;
}
}
}
if (QT->isFunctionPointerType()) {
std::string fp_typedef_name;
Expand Down Expand Up @@ -3191,6 +3214,17 @@ TInterp_t CreateInterpreter(const std::vector<const char*>& Args /*={}*/,
llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
}

I->declare(R"(
namespace __internal_CppInterOp {
template <typename Signature>
struct function;
template <typename Res, typename... ArgTypes>
struct function<Res(ArgTypes...)> {
typedef Res result_type;
};
} // namespace __internal_CppInterOp
)");

sInterpreters->emplace_back(I, /*Owned=*/true);

return I;
Expand Down
13 changes: 13 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,19 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {

auto op_callable = Cpp::MakeFunctionCallable(op);
EXPECT_EQ(op_callable.getKind(), Cpp::JitCall::kGenericCall);

Cpp::Declare(R"(
auto get_fn(int x) { return [x](int y){ return x + y; }; }
)");

Cpp::TCppScope_t get_fn = Cpp::GetNamed("get_fn");
EXPECT_TRUE(get_fn);

auto get_fn_callable = Cpp::MakeFunctionCallable(get_fn);
EXPECT_EQ(get_fn_callable.getKind(), Cpp::JitCall::kGenericCall);

EXPECT_TRUE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(get_fn)));
EXPECT_FALSE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(bar)));
}

TEST(FunctionReflectionTest, IsConstMethod) {
Expand Down
Loading