Skip to content
Merged
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
5 changes: 2 additions & 3 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,8 @@ namespace Cpp {
TCppIndex_t param_index);

///\returns function that performs operation op on lc and rc
TCppFunction_t GetBinaryOperator(TCppScope_t scope, enum BinaryOperator op,
const std::string& lc,
const std::string& rc);
void GetBinaryOperator(TCppScope_t scope, enum BinaryOperator op,
std::vector<TCppFunction_t>& operators);

/// Creates an instance of the interpreter we need for the various interop
/// services.
Expand Down
36 changes: 6 additions & 30 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3161,47 +3161,23 @@ namespace Cpp {
return PI->getNameAsString();
}

TCppFunction_t GetBinaryOperator(TCppScope_t scope, enum BinaryOperator op,
const std::string& lc,
const std::string& rc) {
void GetBinaryOperator(TCppScope_t scope, enum BinaryOperator op,
std::vector<TCppFunction_t>& operators) {
Decl* D = static_cast<Decl*>(scope);
if (!D)
return nullptr;

DeclContext* DC = nullptr;
if (llvm::isa_and_nonnull<TranslationUnitDecl>(D))
DC = llvm::dyn_cast<DeclContext>(D);
else
DC = D->getDeclContext();

if (!DC)
return nullptr;

auto* DC = llvm::dyn_cast<DeclContext>(D);
Scope* S = getSema().getScopeForContext(DC);
if (!S)
return nullptr;
return;

clang::UnresolvedSet<8> lookup;

getSema().LookupBinOp(S, SourceLocation(), (clang::BinaryOperatorKind)op,
lookup);

for (NamedDecl* D : lookup) {
if (auto* FD = llvm::dyn_cast<Decl>(D)) {
assert(GetFunctionNumArgs(FD) == 2 &&
"LookupBinOp returned function without 2 arguments");

std::string arg_type = GetTypeAsString(GetFunctionArgType(FD, 0));
if (arg_type != lc)
continue;
arg_type = GetTypeAsString(GetFunctionArgType(FD, 1));
if (arg_type != rc)
continue;

return FD;
}
if (auto* FD = llvm::dyn_cast<Decl>(D))
operators.push_back(FD);
}
return nullptr;
}

TCppObject_t Allocate(TCppScope_t scope) {
Expand Down
25 changes: 12 additions & 13 deletions unittests/CppInterOp/ScopeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -985,17 +985,16 @@ TEST(ScopeReflectionTest, GetBinaryOperator) {

Cpp::Declare(code.c_str());

EXPECT_TRUE(Cpp::GetBinaryOperator(
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "MyClass", "MyClass"));
EXPECT_TRUE(Cpp::GetBinaryOperator(
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Sub, "MyClass", "MyClass"));
EXPECT_TRUE(Cpp::GetBinaryOperator(
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "MyClass", "int"));
EXPECT_TRUE(Cpp::GetBinaryOperator(
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "int", "MyClass"));

EXPECT_FALSE(Cpp::GetBinaryOperator(
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "float", "MyClass"));
EXPECT_FALSE(Cpp::GetBinaryOperator(
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "MyClass", "float"));
std::vector<Cpp::TCppFunction_t> ops;

Cpp::GetBinaryOperator(Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, ops);
EXPECT_EQ(ops.size(), 3);
ops.clear();

Cpp::GetBinaryOperator(Cpp::GetGlobalScope(), Cpp::BinaryOperator::Sub, ops);
EXPECT_EQ(ops.size(), 1);
ops.clear();

Cpp::GetBinaryOperator(Cpp::GetGlobalScope(), Cpp::BinaryOperator::Mul, ops);
EXPECT_EQ(ops.size(), 0);
Copy link
Contributor

@vgvassilev vgvassilev Sep 7, 2024

Choose a reason for hiding this comment

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

Do we have an example involving argument dependent lookup?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No. I have not implemented the argument dependent lookup with this PR.

I have made changes to take std::vector& as an argument and populate it with the operators. Removing the previous string comparisons to resolve the overloaded operator.
We discussed this after the monthly meeting the day before yesterday.

}