Skip to content

Commit

Permalink
GetBinaryOperator returns vector of functions (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa authored Sep 8, 2024
1 parent 9bd4678 commit ea73105
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 46 deletions.
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);
}

0 comments on commit ea73105

Please sign in to comment.