Skip to content

[cxx-interop] Import non-member operators as global functions #62711

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
Dec 21, 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
8 changes: 7 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8241,13 +8241,19 @@ ClangImporter::Implementation::importDeclContextOf(
if (dc->getDeclKind() == clang::Decl::LinkageSpec)
dc = dc->getParent();

// Treat friend decls like top-level decls.
if (auto functionDecl = dyn_cast<clang::FunctionDecl>(decl)) {
// Treat friend decls like top-level decls.
if (functionDecl->getFriendObjectKind()) {
// Find the top-level decl context.
while (isa<clang::NamedDecl>(dc))
dc = dc->getParent();
}

// If this is a non-member operator, import it as a top-level function.
if (functionDecl->isOverloadedOperator()) {
while (dc->isNamespace())
dc = dc->getParent();
}
}

if (dc->isTranslationUnit()) {
Expand Down
9 changes: 9 additions & 0 deletions test/Interop/Cxx/namespace/Inputs/free-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ inline const char *sameNameInSibling() {
}
} // namespace FunctionsNS4

namespace FunctionsNS1 {
namespace FunctionsNS2 {
namespace FunctionsNS3 {
struct Y {};
inline bool operator==(Y, Y) { return true; }
} // namespace FunctionsNS3
} // namespace FunctionsNS2
} // namespace FunctionsNS1

#endif // TEST_INTEROP_CXX_NAMESPACE_INPUTS_FREE_FUNCTION_H
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,34 @@
// CHECK-NEXT: struct X {
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: static func + (_: FunctionsNS1.X, _: FunctionsNS1.X) -> UnsafePointer<CChar>!
// CHECK-NEXT: static func sameNameInChild() -> UnsafePointer<CChar>!
// CHECK-NEXT: static func sameNameInSibling() -> UnsafePointer<CChar>!

// FIXME: this seems wrong, the operator shouldn't be printed twice (https://github.com/apple/swift/issues/62727).
// CHECK-NEXT: func + (_: FunctionsNS1.X, _: FunctionsNS1.X) -> UnsafePointer<CChar>!

// CHECK-NEXT: enum FunctionsNS2 {
// CHECK-NEXT: static func basicFunctionSecondLevel() -> UnsafePointer<CChar>!
// CHECK-NEXT: static func sameNameInChild() -> UnsafePointer<CChar>!
// CHECK-NEXT: enum FunctionsNS3 {
// CHECK-NEXT: struct Y {
// CHECK-NEXT: init()
// CHECK-NEXT: }

// FIXME: this seems wrong, the operator shouldn't be printed twice (https://github.com/apple/swift/issues/62727).
// CHECK-NEXT: func == (_: FunctionsNS1.FunctionsNS2.FunctionsNS3.Y, _: FunctionsNS1.FunctionsNS2.FunctionsNS3.Y) -> Bool

// CHECK-NEXT: static func basicFunctionLowestLevel() -> UnsafePointer<CChar>!
// CHECK-NEXT: }
// CHECK-NEXT: static func sameNameInChild() -> UnsafePointer<CChar>!
// CHECK-NEXT: static func basicFunctionSecondLevel() -> UnsafePointer<CChar>!
// CHECK-NEXT: }

// CHECK-NEXT: static func sameNameInChild() -> UnsafePointer<CChar>!
// CHECK-NEXT: static func sameNameInSibling() -> UnsafePointer<CChar>!
// CHECK-NEXT: static func definedInDefs() -> UnsafePointer<CChar>!
// CHECK-NEXT: }

// CHECK-NEXT: static func + (_: FunctionsNS1.X, _: FunctionsNS1.X) -> UnsafePointer<CChar>!
// CHECK-NEXT: func + (_: FunctionsNS1.X, _: FunctionsNS1.X) -> UnsafePointer<CChar>!

// CHECK-NEXT: enum FunctionsNS4 {
// CHECK-NEXT: static func sameNameInSibling() -> UnsafePointer<CChar>!
// CHECK-NEXT: }

// CHECK-NEXT: func == (_: FunctionsNS1.FunctionsNS2.FunctionsNS3.Y, _: FunctionsNS1.FunctionsNS2.FunctionsNS3.Y) -> Bool