Skip to content

[Interop][SwiftToCxx] Emit helper cases class in C++ for Swift enum #59669

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 3 commits into from
Jun 25, 2022
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
16 changes: 14 additions & 2 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,20 @@ class DeclAndTypePrinter::Implementation
ClangValueTypePrinter printer(os, owningPrinter.prologueOS,
owningPrinter.typeMapping,
owningPrinter.interopContext);
printer.printValueTypeDecl(
ED, /*bodyPrinter=*/[&]() {}); // TODO: (tongjie) print cases
printer.printValueTypeDecl(ED, /*bodyPrinter=*/[&]() {
ClangSyntaxPrinter syntaxPrinter(os);
os << " enum class cases {";
llvm::interleaveComma(
ED->getAllCases(), os, [&](const EnumCaseDecl *caseDecl) {
llvm::interleaveComma(caseDecl->getElements(), os,
[&](const EnumElementDecl *elementDecl) {
os << "\n ";
syntaxPrinter.printIdentifier(
elementDecl->getNameStr());
});
});
os << "\n };\n";
});
os << outOfLineDefinitions;
outOfLineDefinitions.clear();
return;
Expand Down
47 changes: 47 additions & 0 deletions test/Interop/SwiftToCxx/enums/swift-enum-cases-in-cxx.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name Enums -clang-header-expose-public-decls -emit-clang-header-path %t/enums.h
// RUN: %FileCheck %s < %t/enums.h

// RUN: %check-interop-cxx-header-in-clang(%t/enums.h -Wno-unused-private-field -Wno-unused-function)

public enum EnumMultipleElementsInSingleCase { case first, second }

public enum EnumSingleElementInSingleCase {
case first
case second
}

public enum EnumCaseIsSwiftKeyword {
case first(a: Int)
case `protocol`(b: Double)
}

public enum EnumCaseIsCxxKeyword {
case first, second(x: Float)
case const
}

// CHECK: class EnumCaseIsCxxKeyword final {
// CHECK: enum class cases {
// CHECK-NEXT: first,
// CHECK-NEXT: second,
// CHECK-NEXT: const_
// CHECK-NEXT: };

// CHECK: class EnumCaseIsSwiftKeyword final {
// CHECK: enum class cases {
// CHECK-NEXT: first,
// CHECK-NEXT: protocol
// CHECK-NEXT: };

// CHECK: class EnumMultipleElementsInSingleCase final {
// CHECK: enum class cases {
// CHECK-NEXT: first,
// CHECK-NEXT: second
// CHECK-NEXT: };

// CHECK: class EnumSingleElementInSingleCase final {
// CHECK: enum class cases {
// CHECK-NEXT: first,
// CHECK-NEXT: second
// CHECK-NEXT: };