Skip to content

[cxx-interop] Allow instantiated operator methods to serve as protocol conformance witnesses #62764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2023
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
24 changes: 24 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4021,6 +4021,30 @@ SwiftLookupTable *ClangImporter::Implementation::findLookupTable(
return known->second.get();
}

SwiftLookupTable *
ClangImporter::Implementation::findLookupTable(const clang::Decl *decl) {
// Contents of a C++ namespace are added to the __ObjC module.
bool isWithinNamespace = false;
auto declContext = decl->getDeclContext();
while (!declContext->isTranslationUnit()) {
if (declContext->isNamespace()) {
isWithinNamespace = true;
break;
}
declContext = declContext->getParent();
}

clang::Module *owningModule = nullptr;
if (!isWithinNamespace) {
// Members of class template specializations don't have an owning module.
if (auto spec = dyn_cast<clang::ClassTemplateSpecializationDecl>(decl))
owningModule = spec->getSpecializedTemplate()->getOwningModule();
else
owningModule = decl->getOwningModule();
}
return findLookupTable(owningModule);
}

bool ClangImporter::Implementation::forEachLookupTable(
llvm::function_ref<bool(SwiftLookupTable &table)> fn) {
// Visit the bridging header's lookup table.
Expand Down
13 changes: 4 additions & 9 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2118,15 +2118,7 @@ namespace {
if (friendDecl->getFriendDecl()) {
m = friendDecl->getFriendDecl();

// Find the owning module of the class template. Members of class
// template specializations don't have an owning module.
clang::Module *owningModule = nullptr;
if (auto spec = dyn_cast<clang::ClassTemplateSpecializationDecl>(decl))
owningModule = spec->getSpecializedTemplate()->getOwningModule();
else
owningModule = decl->getOwningModule();

auto lookupTable = Impl.findLookupTable(owningModule);
auto lookupTable = Impl.findLookupTable(decl);
addEntryToLookupTable(*lookupTable, friendDecl->getFriendDecl(),
Impl.getNameImporter());
}
Expand Down Expand Up @@ -2243,6 +2235,9 @@ namespace {

// Make sure the synthesized decl can be found by lookupDirect.
result->addMemberToLookupTable(opFuncDecl);

addEntryToLookupTable(*Impl.findLookupTable(decl), cxxMethod,
Impl.getNameImporter());
}
}
methods.push_back(MD);
Expand Down
3 changes: 3 additions & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
/// about the directly-parsed headers.
SwiftLookupTable *findLookupTable(const clang::Module *clangModule);

/// Find the lookup table that should contain the given Clang declaration.
SwiftLookupTable *findLookupTable(const clang::Decl *decl);

/// Visit each of the lookup tables in some deterministic order.
///
/// \param fn Invoke the given visitor for each table. If the
Expand Down
12 changes: 12 additions & 0 deletions test/Interop/Cxx/class/Inputs/protocol-conformance.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ struct HasOperatorEqualEqual {
}
};

template <typename T>
struct HasOperatorPlusEqual {
T value;

HasOperatorPlusEqual &operator+=(int x) {
value += x;
return *this;
}
};

using HasOperatorPlusEqualInt = HasOperatorPlusEqual<int>;

#endif // TEST_INTEROP_CXX_CLASS_INPUTS_PROTOCOL_CONFORMANCE_H
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ protocol Invertable {
extension HasOperatorExclaim: Invertable {}

extension HasOperatorEqualEqual: Equatable {}


protocol HasOperatorPlusEqualProtocol {
static func +=(lhs: inout Self, x: Int32)
}

extension HasOperatorPlusEqualInt : HasOperatorPlusEqualProtocol {}