Skip to content

Commit 26df3a3

Browse files
authored
Merge pull request swiftlang#72105 from DougGregor/print-cxx-enum-extensions
[Generated header] Emit members of enum extensions into C++ class
2 parents d1ec089 + b5fc2fc commit 26df3a3

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,14 @@ class DeclAndTypePrinter::Implementation
869869
os << "\n";
870870

871871
printMembers(ED->getMembers());
872+
873+
for (const auto *ext :
874+
owningPrinter.interopContext.getExtensionsForNominalType(ED)) {
875+
if (!cxx_translation::isExposableToCxx(ext->getGenericSignature()))
876+
continue;
877+
878+
printMembers(ext->getMembers());
879+
}
872880
},
873881
owningPrinter);
874882
recordEmittedDeclInCurrentCxxLexicalScope(ED);
@@ -2769,13 +2777,13 @@ static bool isStringNestedType(const ValueDecl *VD, StringRef Typename) {
27692777
VD->getASTContext().getStringDecl();
27702778
}
27712779

2772-
static bool hasExposeAttr(const ValueDecl *VD, bool isExtension = false) {
2780+
static bool hasExposeAttr(const ValueDecl *VD) {
27732781
if (isa<NominalTypeDecl>(VD) && VD->getModuleContext()->isStdlibModule()) {
27742782
if (VD == VD->getASTContext().getStringDecl())
27752783
return true;
27762784
if (VD == VD->getASTContext().getArrayDecl())
27772785
return true;
2778-
if (VD == VD->getASTContext().getOptionalDecl() && !isExtension)
2786+
if (VD == VD->getASTContext().getOptionalDecl())
27792787
return true;
27802788
if (isStringNestedType(VD, "UTF8View") || isStringNestedType(VD, "Index"))
27812789
return true;
@@ -2812,7 +2820,7 @@ static bool hasExposeAttr(const ValueDecl *VD, bool isExtension = false) {
28122820
return false;
28132821
}
28142822

2815-
return hasExposeAttr(ED->getExtendedNominal(), /*isExtension=*/true);
2823+
return hasExposeAttr(ED->getExtendedNominal());
28162824
}
28172825
return false;
28182826
}

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ class ModuleWriter {
750750
for (const Decl *D : declsToWrite) {
751751
if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
752752
const auto *type = ED->getExtendedNominal();
753-
if (isa<StructDecl>(type))
753+
if (isa<StructDecl>(type) || isa<EnumDecl>(type))
754754
printer.getInteropContext().recordExtensions(type, ED);
755755
}
756756
}

test/Interop/SwiftToCxx/enums/enum-associated-value-class-type-cxx.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ public enum E {
1414
case i(Int)
1515
}
1616

17+
extension E {
18+
public func matchesIntValue(_ value: Int) -> Bool {
19+
switch self {
20+
case .c:
21+
return false
22+
23+
case .i(let mine):
24+
return mine == value
25+
}
26+
}
27+
}
28+
1729
// CHECK: SWIFT_INLINE_THUNK E E::_impl_c::operator()(const C& val) const {
1830
// CHECK-NEXT: auto result = E::_make();
1931
// CHECK-NEXT: auto op = swift::_impl::_impl_RefCountedClass::copyOpaquePointer(val);
@@ -29,3 +41,6 @@ public enum E {
2941
// CHECK-NEXT: char * _Nonnull payloadFromDestruction = thisCopy->_destructiveProjectEnumData();
3042
// CHECK-NEXT: return swift::_impl::implClassFor<C>::type::makeRetained(*reinterpret_cast<void **>(payloadFromDestruction));
3143
// CHECK-NEXT: }
44+
45+
// CHECK: SWIFT_INLINE_THUNK bool E::matchesIntValue(swift::Int value) const {
46+
// CHECK-NEXT: return _impl::$s5Enums1EO15matchesIntValueySbSiF(value, _impl::swift_interop_passDirect_Enums_uint64_t_0_8_uint8_t_8_9(_getOpaquePointer()));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
// CHECK-NEXT: };
6767
// CHECK: SWIFT_INLINE_THUNK bool isSome() const;
6868
// CHECK: SWIFT_INLINE_THUNK bool isNone() const;
69-
// CHECK: SWIFT_INLINE_THUNK T_0_0 getUnsafelyUnwrapped() const SWIFT_SYMBOL({{.*}});
69+
// CHECK-DAG: SWIFT_INLINE_THUNK T_0_0 getUnsafelyUnwrapped() const SWIFT_SYMBOL({{.*}});
70+
// CHECK-DAG: SWIFT_INLINE_THUNK String getDebugDescription() const SWIFT_SYMBOL("s:Sq16debugDescriptionSSvp");
7071

7172
// CHECK: class SWIFT_SYMBOL({{.*}}) String final {
7273
// CHECK-NEXT: public:

0 commit comments

Comments
 (0)