Skip to content

Commit ea73105

Browse files
GetBinaryOperator returns vector of functions (#320)
1 parent 9bd4678 commit ea73105

File tree

3 files changed

+20
-46
lines changed

3 files changed

+20
-46
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,8 @@ namespace Cpp {
502502
TCppIndex_t param_index);
503503

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

509508
/// Creates an instance of the interpreter we need for the various interop
510509
/// services.

lib/Interpreter/CppInterOp.cpp

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3161,47 +3161,23 @@ namespace Cpp {
31613161
return PI->getNameAsString();
31623162
}
31633163

3164-
TCppFunction_t GetBinaryOperator(TCppScope_t scope, enum BinaryOperator op,
3165-
const std::string& lc,
3166-
const std::string& rc) {
3164+
void GetBinaryOperator(TCppScope_t scope, enum BinaryOperator op,
3165+
std::vector<TCppFunction_t>& operators) {
31673166
Decl* D = static_cast<Decl*>(scope);
3168-
if (!D)
3169-
return nullptr;
3170-
3171-
DeclContext* DC = nullptr;
3172-
if (llvm::isa_and_nonnull<TranslationUnitDecl>(D))
3173-
DC = llvm::dyn_cast<DeclContext>(D);
3174-
else
3175-
DC = D->getDeclContext();
3176-
3177-
if (!DC)
3178-
return nullptr;
3179-
3167+
auto* DC = llvm::dyn_cast<DeclContext>(D);
31803168
Scope* S = getSema().getScopeForContext(DC);
31813169
if (!S)
3182-
return nullptr;
3170+
return;
31833171

31843172
clang::UnresolvedSet<8> lookup;
31853173

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

31893177
for (NamedDecl* D : lookup) {
3190-
if (auto* FD = llvm::dyn_cast<Decl>(D)) {
3191-
assert(GetFunctionNumArgs(FD) == 2 &&
3192-
"LookupBinOp returned function without 2 arguments");
3193-
3194-
std::string arg_type = GetTypeAsString(GetFunctionArgType(FD, 0));
3195-
if (arg_type != lc)
3196-
continue;
3197-
arg_type = GetTypeAsString(GetFunctionArgType(FD, 1));
3198-
if (arg_type != rc)
3199-
continue;
3200-
3201-
return FD;
3202-
}
3178+
if (auto* FD = llvm::dyn_cast<Decl>(D))
3179+
operators.push_back(FD);
32033180
}
3204-
return nullptr;
32053181
}
32063182

32073183
TCppObject_t Allocate(TCppScope_t scope) {

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -985,17 +985,16 @@ TEST(ScopeReflectionTest, GetBinaryOperator) {
985985

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

988-
EXPECT_TRUE(Cpp::GetBinaryOperator(
989-
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "MyClass", "MyClass"));
990-
EXPECT_TRUE(Cpp::GetBinaryOperator(
991-
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Sub, "MyClass", "MyClass"));
992-
EXPECT_TRUE(Cpp::GetBinaryOperator(
993-
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "MyClass", "int"));
994-
EXPECT_TRUE(Cpp::GetBinaryOperator(
995-
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "int", "MyClass"));
996-
997-
EXPECT_FALSE(Cpp::GetBinaryOperator(
998-
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "float", "MyClass"));
999-
EXPECT_FALSE(Cpp::GetBinaryOperator(
1000-
Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, "MyClass", "float"));
988+
std::vector<Cpp::TCppFunction_t> ops;
989+
990+
Cpp::GetBinaryOperator(Cpp::GetGlobalScope(), Cpp::BinaryOperator::Add, ops);
991+
EXPECT_EQ(ops.size(), 3);
992+
ops.clear();
993+
994+
Cpp::GetBinaryOperator(Cpp::GetGlobalScope(), Cpp::BinaryOperator::Sub, ops);
995+
EXPECT_EQ(ops.size(), 1);
996+
ops.clear();
997+
998+
Cpp::GetBinaryOperator(Cpp::GetGlobalScope(), Cpp::BinaryOperator::Mul, ops);
999+
EXPECT_EQ(ops.size(), 0);
10011000
}

0 commit comments

Comments
 (0)