Skip to content

Commit 88492f2

Browse files
fix operator resolution to check base classes (#586)
1 parent 192da21 commit 88492f2

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/Interpreter/CppInterOp.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3564,7 +3564,21 @@ namespace Cpp {
35643564
void GetOperator(TCppScope_t scope, Operator op,
35653565
std::vector<TCppFunction_t>& operators, OperatorArity kind) {
35663566
Decl* D = static_cast<Decl*>(scope);
3567-
if (auto* DC = llvm::dyn_cast_or_null<DeclContext>(D)) {
3567+
if (auto* CXXRD = llvm::dyn_cast_or_null<CXXRecordDecl>(D)) {
3568+
auto fn = [&operators, kind, op](const RecordDecl* RD) {
3569+
ASTContext& C = RD->getASTContext();
3570+
DeclContextLookupResult Result =
3571+
RD->lookup(C.DeclarationNames.getCXXOperatorName(
3572+
(clang::OverloadedOperatorKind)op));
3573+
for (auto* i : Result) {
3574+
if (kind & GetOperatorArity(i))
3575+
operators.push_back(i);
3576+
}
3577+
return true;
3578+
};
3579+
fn(CXXRD);
3580+
CXXRD->forallBases(fn);
3581+
} else if (auto* DC = llvm::dyn_cast_or_null<DeclContext>(D)) {
35683582
ASTContext& C = getSema().getASTContext();
35693583
DeclContextLookupResult Result =
35703584
DC->lookup(C.DeclarationNames.getCXXOperatorName(

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,4 +1107,30 @@ TEST(ScopeReflectionTest, GetOperator) {
11071107
Cpp::GetOperator(Cpp::GetScope("extra_ops"), Cpp::Operator::OP_Tilde, ops,
11081108
Cpp::OperatorArity::kBinary);
11091109
EXPECT_EQ(ops.size(), 0);
1110+
1111+
std::string inheritance_code = R"(
1112+
struct Parent {
1113+
int x;
1114+
Parent(int x) : x(x) {}
1115+
Parent operator+(const Parent& other) {
1116+
return Parent(x + other.x);
1117+
}
1118+
};
1119+
1120+
struct Child : Parent {
1121+
Child(int x) : Parent(x) {}
1122+
Child operator-(const Child& other) {
1123+
return Child(x - other.x);
1124+
}
1125+
};
1126+
)";
1127+
Cpp::Declare(inheritance_code.c_str());
1128+
1129+
ops.clear();
1130+
Cpp::GetOperator(Cpp::GetScope("Child"), Cpp::Operator::OP_Plus, ops);
1131+
EXPECT_EQ(ops.size(), 1);
1132+
1133+
ops.clear();
1134+
Cpp::GetOperator(Cpp::GetScope("Child"), Cpp::Operator::OP_Minus, ops);
1135+
EXPECT_EQ(ops.size(), 1);
11101136
}

0 commit comments

Comments
 (0)