Skip to content

[cxx-interop] Fix generated declaration order #74384

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
Jun 13, 2024
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
14 changes: 12 additions & 2 deletions lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "PrintSwiftToClangCoreScaffold.h"
#include "SwiftToClangInteropContext.h"

#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/Module.h"
Expand Down Expand Up @@ -671,6 +672,15 @@ class ModuleWriter {
llvm_unreachable("unknown top-level ObjC decl");
};

// When we visit a function, we might also generate a thunk that calls into the
// implementation of structs/enums to get the opaque pointers. To avoid
// referencing these methods before we see the definition for the generated
// classes, we want to visit function definitions last.
if (isa<AbstractFunctionDecl>(*rhs) && isa<NominalTypeDecl>(*lhs))
return Descending;
if (isa<AbstractFunctionDecl>(*lhs) && isa<NominalTypeDecl>(*rhs))
return Ascending;

// Sort by names.
int result = getSortName(*rhs).compare(getSortName(*lhs));
if (result != 0)
Expand Down Expand Up @@ -700,9 +710,9 @@ class ModuleWriter {
// even when the variable might not actually be emitted by the emitter.
// In that case, order the function before the variable.
if (isa<AbstractFunctionDecl>(*rhs) && isa<VarDecl>(*lhs))
return 1;
return Descending;
if (isa<AbstractFunctionDecl>(*lhs) && isa<VarDecl>(*rhs))
return -1;
return Ascending;

// Prefer value decls to extensions.
assert(!(isa<ValueDecl>(*lhs) && isa<ValueDecl>(*rhs)));
Expand Down
8 changes: 4 additions & 4 deletions test/Interop/SwiftToCxx/class/swift-class-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ public final class ClassWithIntField {
// CHECK-EMPTY:
// CHECK-NEXT: namespace Class SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Class") {

// CHECK: SWIFT_INLINE_THUNK ClassWithIntField passThroughClassWithIntField(const ClassWithIntField& x) noexcept SWIFT_SYMBOL("s:5Class011passThroughA12WithIntFieldyAA0adeF0CADF") SWIFT_WARN_UNUSED_RESULT {
// CHECK-NEXT: return _impl::_impl_ClassWithIntField::makeRetained(_impl::$s5Class011passThroughA12WithIntFieldyAA0adeF0CADF(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(x)));
// CHECK-NEXT: }

public final class register { }

// CHECK: class SWIFT_SYMBOL("s:5Class8registerC") register_ final : public swift::_impl::RefCountedClass {

// CHECK: SWIFT_INLINE_THUNK ClassWithIntField passThroughClassWithIntField(const ClassWithIntField& x) noexcept SWIFT_SYMBOL("s:5Class011passThroughA12WithIntFieldyAA0adeF0CADF") SWIFT_WARN_UNUSED_RESULT {
// CHECK-NEXT: return _impl::_impl_ClassWithIntField::makeRetained(_impl::$s5Class011passThroughA12WithIntFieldyAA0adeF0CADF(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(x)));
// CHECK-NEXT: }

public func returnClassWithIntField() -> ClassWithIntField {
return ClassWithIntField()
}
Expand Down
4 changes: 1 addition & 3 deletions test/Interop/SwiftToCxx/functions/swift-operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// RUN: %target-swift-frontend %s -typecheck -module-name Operators -clang-header-expose-decls=all-public -emit-clang-header-path %t/operators.h
// RUN: %FileCheck %s < %t/operators.h

// TODO: %check-interop-cxx-header-in-clang(%t/operators.h)
// unfortunately the header still triggers an error:
// error: no member named '_impl_IntBox' in namespace 'Operators::_impl'
// RUN: %check-interop-cxx-header-in-clang(%t/operators.h)

// CHECK-LABEL: namespace Operators SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Operators") {

Expand Down