Skip to content

Commit c6e89b9

Browse files
authored
[Cxx-Interop] Enable Record Friend functions (#59801)
* Update ImportDecl to handle Friend functions inside of recrods * Add tests and update comment * Undo unnecessary lines, format * Clean up and fix tests * New approach * Only import valid friend functions for now * Re-add == func to get Equatable conformance * Remove requirement that friend is a function
1 parent 5246372 commit c6e89b9

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,15 @@ namespace {
21162116
continue;
21172117
}
21182118

2119+
if (auto friendDecl = dyn_cast<clang::FriendDecl>(m)) {
2120+
if (friendDecl->getFriendDecl()) {
2121+
m = friendDecl->getFriendDecl();
2122+
auto lookupTable = Impl.findLookupTable(m->getOwningModule());
2123+
addEntryToLookupTable(*lookupTable, friendDecl->getFriendDecl(),
2124+
Impl.getNameImporter());
2125+
}
2126+
}
2127+
21192128
auto nd = dyn_cast<clang::NamedDecl>(m);
21202129
if (!nd) {
21212130
// We couldn't import the member, so we can't reference it in Swift.
@@ -2145,6 +2154,7 @@ namespace {
21452154
}
21462155

21472156
Decl *member = Impl.importDecl(nd, getActiveSwiftVersion());
2157+
21482158
if (!member) {
21492159
if (!isa<clang::TypeDecl>(nd) && !isa<clang::FunctionDecl>(nd)) {
21502160
// We don't know what this member is.
@@ -4764,12 +4774,6 @@ namespace {
47644774
return nullptr;
47654775
}
47664776

4767-
Decl *VisitFriendDecl(const clang::FriendDecl *decl) {
4768-
// Friends are not imported; Swift has a different access control
4769-
// mechanism.
4770-
return nullptr;
4771-
}
4772-
47734777
Decl *VisitFriendTemplateDecl(const clang::FriendTemplateDecl *decl) {
47744778
// Friends are not imported; Swift has a different access control
47754779
// mechanism.

test/Interop/Cxx/operators/Inputs/member-inline.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ struct LoadableIntWrapper {
2121
value++;
2222
return *this;
2323
}
24+
25+
// Friend functions
26+
friend bool operator==(const LoadableIntWrapper lhs,
27+
const LoadableIntWrapper &rhs) {
28+
return lhs.value == rhs.value;
29+
}
30+
31+
friend LoadableIntWrapper operator-(const LoadableIntWrapper& obj) {
32+
return LoadableIntWrapper{.value = -obj.value};
33+
}
2434
};
2535

2636
struct LoadableBoolWrapper {

test/Interop/Cxx/operators/member-inline.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ OperatorsTestSuite.test("AddressOnlyIntWrapper.minus") {
3030
}
3131
#endif
3232

33+
OperatorsTestSuite.test("LoadableIntWrapper.equal (inline)") {
34+
let lhs = LoadableIntWrapper(value: 42)
35+
let rhs = LoadableIntWrapper(value: 42)
36+
37+
let result = lhs == rhs
38+
39+
expectTrue(result)
40+
}
41+
42+
OperatorsTestSuite.test("LoadableIntWrapper.unaryMinus (inline)") {
43+
let lhs = LoadableIntWrapper(value: 42)
44+
let inverseLhs = -lhs;
45+
expectEqual(-42, inverseLhs.value)
46+
}
47+
3348
OperatorsTestSuite.test("LoadableIntWrapper.call (inline)") {
3449
var wrapper = LoadableIntWrapper(value: 42)
3550

0 commit comments

Comments
 (0)