Skip to content

[Generated header] Emit members of enum extensions into C++ class #72105

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
Mar 6, 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: 11 additions & 3 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,14 @@ class DeclAndTypePrinter::Implementation
os << "\n";

printMembers(ED->getMembers());

for (const auto *ext :
owningPrinter.interopContext.getExtensionsForNominalType(ED)) {
if (!cxx_translation::isExposableToCxx(ext->getGenericSignature()))
continue;

printMembers(ext->getMembers());
}
},
owningPrinter);
recordEmittedDeclInCurrentCxxLexicalScope(ED);
Expand Down Expand Up @@ -2769,13 +2777,13 @@ static bool isStringNestedType(const ValueDecl *VD, StringRef Typename) {
VD->getASTContext().getStringDecl();
}

static bool hasExposeAttr(const ValueDecl *VD, bool isExtension = false) {
static bool hasExposeAttr(const ValueDecl *VD) {
if (isa<NominalTypeDecl>(VD) && VD->getModuleContext()->isStdlibModule()) {
if (VD == VD->getASTContext().getStringDecl())
return true;
if (VD == VD->getASTContext().getArrayDecl())
return true;
if (VD == VD->getASTContext().getOptionalDecl() && !isExtension)
if (VD == VD->getASTContext().getOptionalDecl())
return true;
if (isStringNestedType(VD, "UTF8View") || isStringNestedType(VD, "Index"))
return true;
Expand Down Expand Up @@ -2812,7 +2820,7 @@ static bool hasExposeAttr(const ValueDecl *VD, bool isExtension = false) {
return false;
}

return hasExposeAttr(ED->getExtendedNominal(), /*isExtension=*/true);
return hasExposeAttr(ED->getExtendedNominal());
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ class ModuleWriter {
for (const Decl *D : declsToWrite) {
if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
const auto *type = ED->getExtendedNominal();
if (isa<StructDecl>(type))
if (isa<StructDecl>(type) || isa<EnumDecl>(type))
printer.getInteropContext().recordExtensions(type, ED);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ public enum E {
case i(Int)
}

extension E {
public func matchesIntValue(_ value: Int) -> Bool {
switch self {
case .c:
return false

case .i(let mine):
return mine == value
}
}
}

// CHECK: SWIFT_INLINE_THUNK E E::_impl_c::operator()(const C& val) const {
// CHECK-NEXT: auto result = E::_make();
// CHECK-NEXT: auto op = swift::_impl::_impl_RefCountedClass::copyOpaquePointer(val);
Expand All @@ -29,3 +41,6 @@ public enum E {
// CHECK-NEXT: char * _Nonnull payloadFromDestruction = thisCopy->_destructiveProjectEnumData();
// CHECK-NEXT: return swift::_impl::implClassFor<C>::type::makeRetained(*reinterpret_cast<void **>(payloadFromDestruction));
// CHECK-NEXT: }

// CHECK: SWIFT_INLINE_THUNK bool E::matchesIntValue(swift::Int value) const {
// CHECK-NEXT: return _impl::$s5Enums1EO15matchesIntValueySbSiF(value, _impl::swift_interop_passDirect_Enums_uint64_t_0_8_uint8_t_8_9(_getOpaquePointer()));
3 changes: 2 additions & 1 deletion test/Interop/SwiftToCxx/stdlib/swift-stdlib-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
// CHECK-NEXT: };
// CHECK: SWIFT_INLINE_THUNK bool isSome() const;
// CHECK: SWIFT_INLINE_THUNK bool isNone() const;
// CHECK: SWIFT_INLINE_THUNK T_0_0 getUnsafelyUnwrapped() const SWIFT_SYMBOL({{.*}});
// CHECK-DAG: SWIFT_INLINE_THUNK T_0_0 getUnsafelyUnwrapped() const SWIFT_SYMBOL({{.*}});
// CHECK-DAG: SWIFT_INLINE_THUNK String getDebugDescription() const SWIFT_SYMBOL("s:Sq16debugDescriptionSSvp");

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