Skip to content

[cxx-interop] Export empty enums as namespaces #83616

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion lib/AST/SwiftNameTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ swift::cxx_translation::getDeclRepresentation(
return {Unsupported, UnrepresentableGeneric};
genericSignature = typeDecl->getGenericSignature();
}
if (!isa<ClassDecl>(typeDecl) && isZeroSized && (*isZeroSized)(typeDecl))
if (!isa<ClassDecl>(typeDecl) && isZeroSized && (*isZeroSized)(typeDecl) &&
// Empty enums are exported as namespaces.
!isa<EnumDecl>(typeDecl))
return {Unsupported, UnrepresentableZeroSizedValueType};
}
if (const auto *varDecl = dyn_cast<VarDecl>(VD)) {
Expand Down
34 changes: 32 additions & 2 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ class DeclAndTypePrinter::Implementation
const ModuleDecl *moduleContext) {
if (TD->isImplicit() || TD->isSynthesized())
return;
// Empty enums are exported as namespaces.
if (owningPrinter.isEmptyEnum(TD))
return;
os << " using ";
ClangSyntaxPrinter(getASTContext(), os).printBaseName(TD);
os << "=";
Expand Down Expand Up @@ -490,10 +493,31 @@ class DeclAndTypePrinter::Implementation

void visitEnumDeclCxx(EnumDecl *ED) {
assert(owningPrinter.outputLang == OutputLanguageMode::Cxx);
ClangSyntaxPrinter syntaxPrinter(ED->getASTContext(), os);

if (owningPrinter.isEmptyEnum(ED)) {
syntaxPrinter.printParentNamespaceForNestedTypes(
ED, [&](raw_ostream &os) {
syntaxPrinter.printNamespace(
cxx_translation::getNameForCxx(ED),
[ED, this](llvm::raw_ostream &) {
printMembers(ED->getAllMembers());
for (const auto *ext :
owningPrinter.interopContext.getExtensionsForNominalType(
ED)) {
if (!cxx_translation::isExposableToCxx(
ext->getGenericSignature()))
continue;

printMembers(ext->getAllMembers());
}
});
});
return;
}

ClangValueTypePrinter valueTypePrinter(os, owningPrinter.prologueOS,
owningPrinter.interopContext);
ClangSyntaxPrinter syntaxPrinter(ED->getASTContext(), os);
DeclAndTypeClangFunctionPrinter clangFuncPrinter(
os, owningPrinter.prologueOS, owningPrinter.typeMapping,
owningPrinter.interopContext, owningPrinter);
Expand Down Expand Up @@ -1890,7 +1914,9 @@ class DeclAndTypePrinter::Implementation
void visitFuncDecl(FuncDecl *FD) {
if (outputLang == OutputLanguageMode::Cxx) {
if (FD->getDeclContext()->isTypeContext())
return printAbstractFunctionAsMethod(FD, FD->isStatic());
return printAbstractFunctionAsMethod(
FD, FD->isStatic() && !owningPrinter.isEmptyEnum(
FD->getDeclContext()->getAsDecl()));

// Emit the underlying C signature that matches the Swift ABI
// in the generated C++ implementation prologue for the module.
Expand Down Expand Up @@ -3080,6 +3106,10 @@ bool DeclAndTypePrinter::isZeroSized(const NominalTypeDecl *decl) {
return false;
}

bool DeclAndTypePrinter::isEmptyEnum(const Decl *decl) {
return isa<EnumDecl>(decl) && isZeroSized(cast<EnumDecl>(decl));
}

bool DeclAndTypePrinter::isVisible(const ValueDecl *vd) const {
return outputLang == OutputLanguageMode::Cxx
? cxx_translation::isVisibleToCxx(vd, minRequiredAccess)
Expand Down
1 change: 1 addition & 0 deletions lib/PrintAsClang/DeclAndTypePrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class DeclAndTypePrinter {
bool shouldInclude(const ValueDecl *VD);

bool isZeroSized(const NominalTypeDecl *decl);
bool isEmptyEnum(const Decl *decl);

/// Returns true if \p vd is visible given the current access level and thus
/// can be included in the generated header.
Expand Down
4 changes: 4 additions & 0 deletions lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,10 @@ class ModuleWriter {
}

void forwardDeclareCxxValueTypeIfNeeded(const NominalTypeDecl *NTD) {
// Zero sized enums are exported as namespaces, no forward declaration is
// required.
if (printer.isZeroSized(NTD) && isa<EnumDecl>(NTD))
return;
forwardDeclare(NTD, [&]() {
ClangValueTypePrinter::forwardDeclType(os, NTD, printer);
});
Expand Down
3 changes: 2 additions & 1 deletion lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,8 @@ void DeclAndTypeClangFunctionPrinter::printCxxMethod(
bool isMutating =
isa<FuncDecl>(FD) ? cast<FuncDecl>(FD)->isMutating() : false;
modifiers.isConst = !isa<ClassDecl>(typeDeclContext) && !isMutating &&
!isConstructor && !isStatic;
!isConstructor && !isStatic &&
!declAndTypePrinter.isEmptyEnum(typeDeclContext);
modifiers.hasSymbolUSR = !isDefinition;
auto result = printFunctionSignature(
FD, signature, cxx_translation::getNameForCxx(FD), resultTy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ int main() {

auto xx = makeAudioFileType();
AudioFileType::SubType yy = xx.getCAF();
auto xxx = Empty::getInt();
Empty::NestedInEmpty *ptr = nullptr;
auto xxxxx = AuxConfig::Empty::getInt();
}
11 changes: 11 additions & 0 deletions test/Interop/SwiftToCxx/structs/nested-structs-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public class AuxConfig {
}

public var directory = AuxDirectory()

public enum Empty {
public static func getInt() -> Int { 55 }
}
}

public func makeRecordConfig() -> RecordConfig {
Expand Down Expand Up @@ -85,6 +89,13 @@ extension AudioFileType {
}
}

public enum Empty {
public static func getInt() -> Int { 42 }

public struct NestedInEmpty {
public var x : Int
}
}

public func getFiles() -> [RecordConfig.File] {
[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public func f() -> ZeroSizedStruct {
public func g(x: ZeroSizedStruct) {
}

// CHECK: class ZeroSizedEnum { } SWIFT_UNAVAILABLE_MSG("'ZeroSizedEnum' is a zero sized value type, it cannot be exposed to C++ yet");
// CHECK: namespace ZeroSizedEnum

// CHECK: class ZeroSizedEnum2 { } SWIFT_UNAVAILABLE_MSG("'ZeroSizedEnum2' is a zero sized value type, it cannot be exposed to C++ yet");
// CHECK: namespace ZeroSizedEnum2

// CHECK: class ZeroSizedEnum3 { } SWIFT_UNAVAILABLE_MSG("'ZeroSizedEnum3' is a zero sized value type, it cannot be exposed to C++ yet");
// CHECK: class ZeroSizedEnum3 { } SWIFT_UNAVAILABLE_MSG("enum 'ZeroSizedEnum3' can not yet be represented in C++ as one of its cases has multiple associated values");

// CHECK: class ZeroSizedStruct { } SWIFT_UNAVAILABLE_MSG("'ZeroSizedStruct' is a zero sized value type, it cannot be exposed to C++ yet");

Expand Down