Skip to content

[cxx-interop] Fix nested structs for non-opaque types #80495

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
Apr 4, 2025
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
10 changes: 6 additions & 4 deletions lib/PrintAsClang/ClangSyntaxPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "clang/AST/Decl.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/NestedNameSpecifier.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"

using namespace swift;
Expand Down Expand Up @@ -127,8 +128,8 @@ void ClangSyntaxPrinter::printClangTypeReference(const clang::Decl *typeDecl) {
}
}

bool ClangSyntaxPrinter::printNestedTypeNamespaceQualifiers(
const ValueDecl *D) const {
bool ClangSyntaxPrinter::printNestedTypeNamespaceQualifiers(const ValueDecl *D,
bool forC) const {
bool first = true;
while (auto parent = dyn_cast_or_null<NominalTypeDecl>(
D->getDeclContext()->getAsDecl())) {
Expand All @@ -137,9 +138,10 @@ bool ClangSyntaxPrinter::printNestedTypeNamespaceQualifiers(
isa<clang::NamespaceDecl>(parent->getClangNode().getAsDecl()))
break;
if (!first)
os << "::";
os << (forC ? "_" : "::");
first = false;
os << "__";
if (!forC)
os << "__";
printBaseName(parent);
os << "Nested";
D = parent;
Expand Down
3 changes: 2 additions & 1 deletion lib/PrintAsClang/ClangSyntaxPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class ClangSyntaxPrinter {
const NominalTypeDecl *typeDecl);

// Returns true when no qualifiers were printed.
bool printNestedTypeNamespaceQualifiers(const ValueDecl *D) const;
bool printNestedTypeNamespaceQualifiers(const ValueDecl *D,
bool forC = false) const;

/// Print out the C++ class access qualifier for the given Swift type
/// declaration.
Expand Down
5 changes: 4 additions & 1 deletion lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,10 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
os << "void **vtable_ = *selfPtr_;\n";
os << "#endif\n";
os << "struct FTypeAddress {\n";
os << "decltype(" << cxx_synthesis::getCxxImplNamespaceName()
os << "decltype(";
ClangSyntaxPrinter(moduleContext->getASTContext(), os)
.printBaseName(moduleContext);
os << "::" << cxx_synthesis::getCxxImplNamespaceName()
<< "::" << swiftSymbolName << ") *";
if (auto ptrAuthDisc = dispatchInfo->getPointerAuthDiscriminator())
os << " __ptrauth_swift_class_method_pointer(" << ptrAuthDisc->value
Expand Down
5 changes: 3 additions & 2 deletions lib/PrintAsClang/PrintClangValueType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ using namespace swift;
static void printCTypeName(raw_ostream &os, const NominalTypeDecl *type) {
ClangSyntaxPrinter printer(type->getASTContext(), os);
printer.printModuleNameCPrefix(*type->getParentModule());
if (!ClangSyntaxPrinter(type->getASTContext(), os).printNestedTypeNamespaceQualifiers(type))
os << "::";
if (!ClangSyntaxPrinter(type->getASTContext(), os)
.printNestedTypeNamespaceQualifiers(type, /*forC=*/true))
os << "_";
printer.printBaseName(type);
}

Expand Down
3 changes: 3 additions & 0 deletions test/Interop/SwiftToCxx/class/nested-classes-in-cxx.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -enable-library-evolution -typecheck -module-name Classes -clang-header-expose-decls=all-public -emit-clang-header-path %t/classes.h
// RUN: %check-interop-cxx-header-in-clang(%t/classes.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -std=c++17)

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name Classes -clang-header-expose-decls=all-public -emit-clang-header-path %t/classes.h
// RUN: %check-interop-cxx-header-in-clang(%t/classes.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -std=c++17)

public class RecordConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class04BaseA0C13virtualMethodyyF) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class04BaseA0C13virtualMethodyyF) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM1:]] / sizeof(void *));
// CHECK-NEXT: (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -138,7 +138,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class04BaseA0C016virtualMethodIntE0yS2iF) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class04BaseA0C016virtualMethodIntE0yS2iF) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM2:]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(x, ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -156,7 +156,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class04BaseA0C19virtualComputedPropSivg) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class04BaseA0C19virtualComputedPropSivg) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM3:]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -170,7 +170,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class04BaseA0C21virtualComputedGetSets5Int64Vvg) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class04BaseA0C21virtualComputedGetSets5Int64Vvg) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM4:]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -184,7 +184,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class04BaseA0C21virtualComputedGetSets5Int64Vvs) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class04BaseA0C21virtualComputedGetSets5Int64Vvs) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM5:]] / sizeof(void *));
// CHECK-NEXT: (* fptrptr_->func)(newValue, ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -198,7 +198,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class04BaseA0C10storedPropSivg) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class04BaseA0C10storedPropSivg) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM7:]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -212,7 +212,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class04BaseA0C10storedPropSivs) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class04BaseA0C10storedPropSivs) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM8:]] / sizeof(void *));
// CHECK-NEXT: (* fptrptr_->func)(value, ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -226,7 +226,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class04BaseA0CyS2icig) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class04BaseA0CyS2icig) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM10:]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(i, ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -240,7 +240,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class07DerivedA0C13virtualMethodyyF) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class07DerivedA0C13virtualMethodyyF) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM1]] / sizeof(void *));
// CHECK-NEXT: (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -254,7 +254,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class07DerivedA0C016virtualMethodIntE0yS2iF) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class07DerivedA0C016virtualMethodIntE0yS2iF) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM2]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(x, ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -268,7 +268,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class07DerivedA0C015virtualMethodInB0yAA04BaseA0CAFF) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class07DerivedA0C015virtualMethodInB0yAA04BaseA0CAFF) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM11:]] / sizeof(void *));
// CHECK-NEXT: return _impl::_impl_BaseClass::makeRetained((* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(x), ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this)));
Expand All @@ -282,7 +282,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class07DerivedA0C19virtualComputedPropSivg) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class07DerivedA0C19virtualComputedPropSivg) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM3]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -296,7 +296,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class07DerivedA0C21virtualComputedGetSets5Int64Vvg) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class07DerivedA0C21virtualComputedGetSets5Int64Vvg) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM4]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -310,7 +310,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class07DerivedA0C21virtualComputedGetSets5Int64Vvs) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class07DerivedA0C21virtualComputedGetSets5Int64Vvs) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM5]] / sizeof(void *));
// CHECK-NEXT: (* fptrptr_->func)(newValue, ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -324,7 +324,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class07DerivedA0C10storedPropSivg) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class07DerivedA0C10storedPropSivg) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM7]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -338,7 +338,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class07DerivedA0C10storedPropSivs) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class07DerivedA0C10storedPropSivs) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM8]] / sizeof(void *));
// CHECK-NEXT: (* fptrptr_->func)(newValue, ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand All @@ -352,7 +352,7 @@ public func returnDerivedDerivedClass() -> DerivedDerivedClass {
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class07DerivedA0CyS2icig) * func;
// CHECK-NEXT: decltype(Class::_impl::$s5Class07DerivedA0CyS2icig) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM10]] / sizeof(void *));
// CHECK-NEXT: return (* fptrptr_->func)(i, ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public func createCrossModuleDerivedDerivedClass() -> CrossModuleDerivedDerivedC
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s8UseClass018CrossModuleDerivedB0C015virtualMethodInE0yyF) * func;
// CHECK-NEXT: decltype(UseClass::_impl::$s8UseClass018CrossModuleDerivedB0C015virtualMethodInE0yyF) * func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + (_impl::$s8UseClass018CrossModuleDerivedB0CMo + 0) / sizeof(void *));
// CHECK-NEXT: (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -enable-library-evolution -module-name Structs -clang-header-expose-decls=all-public -typecheck -verify -emit-clang-header-path %t/structs.h
// RN: %FileCheck %s < %t/structs.h
// RUN: %check-interop-cxx-header-in-clang(%t/structs.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -std=c++17)

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -module-name Structs -clang-header-expose-decls=all-public -typecheck -verify -emit-clang-header-path %t/structs.h
// RUN: %check-interop-cxx-header-in-clang(%t/structs.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -std=c++17)

public enum AudioFileType {
Expand Down