Skip to content

[Cxx-Interop] Enable Record Friend functions #59801

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 8 commits into from
Jul 6, 2022
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
16 changes: 10 additions & 6 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,15 @@ namespace {
continue;
}

if (auto friendDecl = dyn_cast<clang::FriendDecl>(m)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto friendDecl = dyn_cast<clang::FriendDecl>(m)) {
if (auto friendDecl = dyn_cast<clang::FriendDecl>(m)) {
m = friendDecl->getFriend();

if (friendDecl->getFriendDecl()) {
m = friendDecl->getFriendDecl();
auto lookupTable = Impl.findLookupTable(m->getOwningModule());
addEntryToLookupTable(*lookupTable, friendDecl->getFriendDecl(),
Impl.getNameImporter());
}
}

auto nd = dyn_cast<clang::NamedDecl>(m);
if (!nd) {
// We couldn't import the member, so we can't reference it in Swift.
Expand Down Expand Up @@ -2145,6 +2154,7 @@ namespace {
}

Decl *member = Impl.importDecl(nd, getActiveSwiftVersion());

if (!member) {
if (!isa<clang::TypeDecl>(nd) && !isa<clang::FunctionDecl>(nd)) {
// We don't know what this member is.
Expand Down Expand Up @@ -4756,12 +4766,6 @@ namespace {
return nullptr;
}

Decl *VisitFriendDecl(const clang::FriendDecl *decl) {
// Friends are not imported; Swift has a different access control
// mechanism.
return nullptr;
}

Decl *VisitFriendTemplateDecl(const clang::FriendTemplateDecl *decl) {
// Friends are not imported; Swift has a different access control
// mechanism.
Expand Down
10 changes: 10 additions & 0 deletions test/Interop/Cxx/operators/Inputs/member-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ struct LoadableIntWrapper {
value++;
return *this;
}

// Friend functions
friend bool operator==(const LoadableIntWrapper lhs,
const LoadableIntWrapper &rhs) {
return lhs.value == rhs.value;
}

friend LoadableIntWrapper operator-(const LoadableIntWrapper& obj) {
return LoadableIntWrapper{.value = -obj.value};
}
};

struct LoadableBoolWrapper {
Expand Down
15 changes: 15 additions & 0 deletions test/Interop/Cxx/operators/member-inline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ OperatorsTestSuite.test("AddressOnlyIntWrapper.minus") {
}
#endif

OperatorsTestSuite.test("LoadableIntWrapper.equal (inline)") {
let lhs = LoadableIntWrapper(value: 42)
let rhs = LoadableIntWrapper(value: 42)

let result = lhs == rhs

expectTrue(result)
}

OperatorsTestSuite.test("LoadableIntWrapper.unaryMinus (inline)") {
let lhs = LoadableIntWrapper(value: 42)
let inverseLhs = -lhs;
expectEqual(-42, inverseLhs.value)
}

OperatorsTestSuite.test("LoadableIntWrapper.call (inline)") {
var wrapper = LoadableIntWrapper(value: 42)

Expand Down