Skip to content

[cxx-interop] Support bool-based enums. #34365

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
Oct 26, 2020
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
18 changes: 13 additions & 5 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8558,11 +8558,19 @@ ClangImporter::Implementation::createConstant(Identifier name, DeclContext *dc,
// Create the expression node.
StringRef printedValueCopy(context.AllocateCopy(printedValue));
if (value.getKind() == clang::APValue::Int) {
if (type->getCanonicalType()->isBool()) {
auto *boolExpr =
new (context) BooleanLiteralExpr(value.getInt().getBoolValue(),
SourceLoc(),
/**Implicit=*/true);
bool isBool = type->getCanonicalType()->isBool();
// Check if "type" is a C++ enum with an underlying type of "bool".
if (!isBool && type->getStructOrBoundGenericStruct() &&
type->getStructOrBoundGenericStruct()->getClangDecl()) {
if (auto enumDecl = dyn_cast<clang::EnumDecl>(
type->getStructOrBoundGenericStruct()->getClangDecl())) {
Comment on lines +8564 to +8566
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could simplify this a little bit by using dyn_cast_or_null.

isBool = enumDecl->getIntegerType()->isBooleanType();
}
}
if (isBool) {
auto *boolExpr = new (context)
BooleanLiteralExpr(value.getInt().getBoolValue(), SourceLoc(),
/*Implicit=*/true);

boolExpr->setBuiltinInitializer(
context.getBoolBuiltinInitDecl());
Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/enum/Inputs/bool-enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum Maybe : bool { No, Yes };
enum BinaryNumbers : bool { One = 1, Zero = 0 };
enum class EnumClass : bool { Foo, Bar };
struct WrapperStruct {
enum InnerBoolEnum : bool { A, B };
};
3 changes: 3 additions & 0 deletions test/Interop/Cxx/enum/Inputs/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module BoolEnums {
header "bool-enums.h"
}
40 changes: 40 additions & 0 deletions test/Interop/Cxx/enum/bool-enums-module-interface.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=BoolEnums -I %S/Inputs -source-filename=x -enable-cxx-interop | %FileCheck %s

// TODO: these should be enums eventually (especially the enum class).

// CHECK: struct Maybe : Equatable, RawRepresentable {
// CHECK-NEXT: init(_ rawValue: Bool)
// CHECK-NEXT: init(rawValue: Bool)
// CHECK-NEXT: var rawValue: Bool
// CHECK-NEXT: typealias RawValue = Bool
// CHECK-NEXT: }

// CHECK: var No: Maybe { get }
// CHECK: var Yes: Maybe { get }

// CHECK: struct BinaryNumbers : Equatable, RawRepresentable {
// CHECK-NEXT: init(_ rawValue: Bool)
// CHECK-NEXT: init(rawValue: Bool)
// CHECK-NEXT: var rawValue: Bool
// CHECK-NEXT: typealias RawValue = Bool
// CHECK-NEXT: }

// CHECK: var One: BinaryNumbers { get }
// CHECK: var Zero: BinaryNumbers { get }
// CHECK: struct EnumClass : Equatable, RawRepresentable {
// CHECK-NEXT: init(_ rawValue: Bool)
// CHECK-NEXT: init(rawValue: Bool)
// CHECK-NEXT: var rawValue: Bool
// CHECK-NEXT: typealias RawValue = Bool
// CHECK-NEXT: }

// CHECK: struct WrapperStruct {
// TODO: where is "A" and "B"? They should be member variables.
// CHECK-NEXT: struct InnerBoolEnum : Equatable, RawRepresentable {
// CHECK-NEXT: init(_ rawValue: Bool)
// CHECK-NEXT: init(rawValue: Bool)
// CHECK-NEXT: var rawValue: Bool
// CHECK-NEXT: typealias RawValue = Bool
// CHECK-NEXT: }
// CHECK-NEXT: init()
// CHECK-NEXT: }