Skip to content

Commit a7e19f3

Browse files
authored
Merge pull request #82712 from Xazax-hun/types-nested-in-extensions
[cxx-interop] Support types nested in extensions
2 parents 0442c7c + c5b18a0 commit a7e19f3

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

lib/AST/SwiftNameTranslation.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,6 @@ swift::cxx_translation::getNameForCxx(const ValueDecl *VD,
231231
auto r = ctx.getIdentifier(os.str());
232232
return r.str();
233233
}
234-
235-
// FIXME: String.Index should be exposed as String::Index, not
236-
// _String_Index.
237-
if (VD->getBaseIdentifier().str() == "Index") {
238-
return "String_Index";
239-
}
240234
}
241235

242236
return VD->getBaseIdentifier().str();

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/Decl.h"
1818
#include "swift/AST/Module.h"
1919
#include "swift/AST/SwiftNameTranslation.h"
20+
#include "swift/AST/Type.h"
2021
#include "swift/AST/TypeCheckRequests.h"
2122
#include "swift/Basic/Assertions.h"
2223
#include "clang/AST/ASTContext.h"
@@ -131,20 +132,25 @@ void ClangSyntaxPrinter::printClangTypeReference(const clang::Decl *typeDecl) {
131132
bool ClangSyntaxPrinter::printNestedTypeNamespaceQualifiers(const ValueDecl *D,
132133
bool forC) const {
133134
bool first = true;
134-
while (auto parent = dyn_cast_or_null<NominalTypeDecl>(
135-
D->getDeclContext()->getAsDecl())) {
135+
while (auto parent = D->getDeclContext()->getAsDecl()) {
136+
const auto *parentNTD = dyn_cast<NominalTypeDecl>(parent);
137+
if (!parentNTD)
138+
if (const auto *ED = dyn_cast<ExtensionDecl>(parent))
139+
parentNTD = ED->getExtendedNominal();
140+
if (!parentNTD)
141+
continue;
136142
// C++ namespaces are imported as enums.
137-
if (parent->hasClangNode() &&
138-
isa<clang::NamespaceDecl>(parent->getClangNode().getAsDecl()))
143+
if (parentNTD->hasClangNode() &&
144+
isa<clang::NamespaceDecl>(parentNTD->getClangNode().getAsDecl()))
139145
break;
140146
if (!first)
141147
os << (forC ? "_" : "::");
142148
first = false;
143149
if (!forC)
144150
os << "__";
145-
printBaseName(parent);
151+
printBaseName(parentNTD);
146152
os << "Nested";
147-
D = parent;
153+
D = parentNTD;
148154
}
149155
return first;
150156
}
@@ -207,7 +213,8 @@ void ClangSyntaxPrinter::printNamespace(
207213
void ClangSyntaxPrinter::printParentNamespaceForNestedTypes(
208214
const ValueDecl *D, llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
209215
NamespaceTrivia trivia) const {
210-
if (!isa_and_nonnull<NominalTypeDecl>(D->getDeclContext()->getAsDecl()) ||
216+
if ((!isa_and_nonnull<NominalTypeDecl>(D->getDeclContext()->getAsDecl()) &&
217+
!isa_and_nonnull<ExtensionDecl>(D->getDeclContext()->getAsDecl())) ||
211218
importer::isClangNamespace(D->getDeclContext())) {
212219
bodyPrinter(os);
213220
return;

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,10 @@ class ModuleWriter {
767767
return false;
768768

769769
(void)forwardDeclareMemberTypes(CD->getAllMembers(), CD);
770+
for (const auto *ed :
771+
printer.getInteropContext().getExtensionsForNominalType(CD)) {
772+
(void)forwardDeclareMemberTypes(ed->getAllMembers(), CD);
773+
}
770774
auto [it, inserted] =
771775
seenTypes.try_emplace(CD, EmissionState::NotYetDefined, false);
772776
if (outputLangMode == OutputLanguageMode::Cxx &&
@@ -868,6 +872,10 @@ class ModuleWriter {
868872

869873
if (outputLangMode == OutputLanguageMode::Cxx) {
870874
forwardDeclareMemberTypes(ED->getAllMembers(), ED);
875+
for (const auto *ed :
876+
printer.getInteropContext().getExtensionsForNominalType(ED)) {
877+
(void)forwardDeclareMemberTypes(ed->getAllMembers(), ED);
878+
}
871879
forwardDeclareCxxValueTypeIfNeeded(ED);
872880
}
873881

test/Interop/SwiftToCxx/stdlib/swift-stdlib-in-cxx.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686
// CHECK: }
8787
// CHECK-NEXT: static SWIFT_INLINE_THUNK String init() SWIFT_SYMBOL({{.*}});
8888
// CHECK: SWIFT_INLINE_THUNK void append(const String& other)
89-
// CHECK: SWIFT_INLINE_THUNK UTF8View getUtf8() const SWIFT_SYMBOL({{.*}});
90-
// CHECK-NEXT: SWIFT_INLINE_THUNK void setUtf8(const UTF8View& newValue) SWIFT_SYMBOL({{.*}});
89+
// CHECK: SWIFT_INLINE_THUNK __StringNested::UTF8View getUtf8() const SWIFT_SYMBOL({{.*}});
90+
// CHECK-NEXT: SWIFT_INLINE_THUNK void setUtf8(const __StringNested::UTF8View& newValue) SWIFT_SYMBOL({{.*}});
9191
// CHECK: SWIFT_INLINE_THUNK operator NSString * _Nonnull () const noexcept {
9292
// CHECK-NEXT: return (__bridge_transfer NSString *)(_impl::$sSS10FoundationE19_bridgeToObjectiveCSo8NSStringCyF(_impl::swift_interop_passDirect_Swift_String(_getOpaquePointer())));
9393
// CHECK-NEXT: }
@@ -120,11 +120,11 @@
120120
// CHECK: class SWIFT_SYMBOL({{.*}}) UTF8View final {
121121
// CHECK: SWIFT_INLINE_PRIVATE_HELPER UTF8View(UTF8View &&) noexcept {
122122
// CHECK: }
123-
// CHECK-NEXT: SWIFT_INLINE_THUNK String_Index getStartIndex() const SWIFT_SYMBOL({{.*}});
124-
// CHECK-NEXT: SWIFT_INLINE_THUNK String_Index getEndIndex() const SWIFT_SYMBOL({{.*}});
125-
// CHECK: SWIFT_INLINE_THUNK swift::Optional<String_Index> indexOffsetByLimitedBy(const String_Index& i, swift::Int n, const String_Index& limit) const SWIFT_SYMBOL({{.*}});
126-
// CHECK: SWIFT_INLINE_THUNK swift::Int distanceFromTo(const String_Index& i, const String_Index& j) const SWIFT_SYMBOL({{.*}});
127-
// CHECK: SWIFT_INLINE_THUNK uint8_t operator [](const String_Index& i) const SWIFT_SYMBOL({{.*}});
123+
// CHECK-NEXT: SWIFT_INLINE_THUNK __StringNested::Index getStartIndex() const SWIFT_SYMBOL({{.*}});
124+
// CHECK-NEXT: SWIFT_INLINE_THUNK __StringNested::Index getEndIndex() const SWIFT_SYMBOL({{.*}});
125+
// CHECK: SWIFT_INLINE_THUNK swift::Optional<__StringNested::Index> indexOffsetByLimitedBy(const __StringNested::Index& i, swift::Int n, const __StringNested::Index& limit) const SWIFT_SYMBOL({{.*}});
126+
// CHECK: SWIFT_INLINE_THUNK swift::Int distanceFromTo(const __StringNested::Index& i, const __StringNested::Index& j) const SWIFT_SYMBOL({{.*}});
127+
// CHECK: SWIFT_INLINE_THUNK uint8_t operator [](const __StringNested::Index& i) const SWIFT_SYMBOL({{.*}});
128128
// CHECK: SWIFT_INLINE_THUNK String getDescription() const SWIFT_SYMBOL({{.*}});
129129
// CHECK: SWIFT_INLINE_THUNK swift::Int getCount() const SWIFT_SYMBOL({{.*}});
130130
// CHECK-NEXT: private:

test/Interop/SwiftToCxx/structs/nested-structs-in-cxx.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ extension RecordConfig.File {
7373
public func getFileExtension() -> String { ".wav" }
7474
}
7575

76+
extension RecordConfig {
77+
public struct NestedInExtension {
78+
public var foo: Int
79+
}
80+
}
81+
82+
extension AudioFileType {
83+
public struct NestedInExtension {
84+
public var foo: Int
85+
}
86+
}
87+
88+
7689
public func getFiles() -> [RecordConfig.File] {
7790
[]
7891
}

0 commit comments

Comments
 (0)