From ea731053138b31f5b246ede733513e2b6a93fdb5 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Sun, 8 Sep 2024 10:46:23 +0530 Subject: [PATCH] GetBinaryOperator returns vector of functions (#320) --- include/clang/Interpreter/CppInterOp.h | 5 ++- lib/Interpreter/CppInterOp.cpp | 36 ++++---------------- unittests/CppInterOp/ScopeReflectionTest.cpp | 25 +++++++------- 3 files changed, 20 insertions(+), 46 deletions(-) diff --git a/include/clang/Interpreter/CppInterOp.h b/include/clang/Interpreter/CppInterOp.h index e4a9969e..ef630de9 100644 --- a/include/clang/Interpreter/CppInterOp.h +++ b/include/clang/Interpreter/CppInterOp.h @@ -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& operators); /// Creates an instance of the interpreter we need for the various interop /// services. diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index a6f1710b..96aab802 100644 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -3161,25 +3161,13 @@ 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& operators) { Decl* D = static_cast(scope); - if (!D) - return nullptr; - - DeclContext* DC = nullptr; - if (llvm::isa_and_nonnull(D)) - DC = llvm::dyn_cast(D); - else - DC = D->getDeclContext(); - - if (!DC) - return nullptr; - + auto* DC = llvm::dyn_cast(D); Scope* S = getSema().getScopeForContext(DC); if (!S) - return nullptr; + return; clang::UnresolvedSet<8> lookup; @@ -3187,21 +3175,9 @@ namespace Cpp { lookup); for (NamedDecl* D : lookup) { - if (auto* FD = llvm::dyn_cast(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(D)) + operators.push_back(FD); } - return nullptr; } TCppObject_t Allocate(TCppScope_t scope) { diff --git a/unittests/CppInterOp/ScopeReflectionTest.cpp b/unittests/CppInterOp/ScopeReflectionTest.cpp index ee4ea6d9..d0da1c2f 100644 --- a/unittests/CppInterOp/ScopeReflectionTest.cpp +++ b/unittests/CppInterOp/ScopeReflectionTest.cpp @@ -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 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); }