Skip to content

Commit e309dbf

Browse files
committed
[c++-interop] Adding options set struct to go with the typedef's enum bit set.
This does not work yet, but it is a proof of concept for now.
1 parent e977a21 commit e309dbf

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,14 +2856,46 @@ namespace {
28562856
return nullptr;
28572857

28582858
auto Loc = Impl.importSourceLoc(Decl->getLocation());
2859-
auto Result = Impl.createDeclWithClangNode<TypeAliasDecl>(Decl,
2860-
AccessLevel::Public,
2861-
Impl.importSourceLoc(Decl->getBeginLoc()),
2862-
SourceLoc(), Name,
2863-
Loc,
2864-
/*genericparams*/nullptr, DC);
2865-
2866-
Result->setUnderlyingType(SwiftType);
2859+
2860+
swift::Decl *Result = nullptr;
2861+
if (importer::isUnavailableInSwift(Decl, nullptr, true) &&
2862+
Impl.SwiftContext.LangOpts.EnableCXXInterop) {
2863+
ASTContext &ctx = Impl.SwiftContext;
2864+
2865+
// Compute the underlying type.
2866+
clang::QualType ClangType = Decl->getUnderlyingType();
2867+
auto underlyingType = Impl.importTypeIgnoreIUO(
2868+
ClangType, ImportTypeKind::Enum,
2869+
ImportDiagnosticAdder(Impl, Decl, Decl->getLocation()),
2870+
isInSystemModule(DC), Bridgeability::None, ImportTypeAttrs());
2871+
if (!underlyingType)
2872+
return nullptr;
2873+
2874+
auto structDecl = Impl.createDeclWithClangNode<StructDecl>(
2875+
Decl, AccessLevel::Public, Loc, Name, Loc, None, nullptr, DC);
2876+
2877+
addSynthesizedProtocolAttrs(Impl, structDecl,
2878+
{KnownProtocolKind::Sendable},
2879+
/*isUnchecked=*/true);
2880+
2881+
makeStructRawValued(Impl, structDecl, underlyingType,
2882+
{KnownProtocolKind::OptionSet});
2883+
auto selfType = structDecl->getDeclaredInterfaceType();
2884+
addSynthesizedTypealias(structDecl, ctx.Id_Element, selfType);
2885+
addSynthesizedTypealias(structDecl, ctx.Id_ArrayLiteralElement,
2886+
selfType);
2887+
Impl.DeclsWithSuperfluousTypedefs.insert(Decl);
2888+
2889+
Result = structDecl;
2890+
} else {
2891+
auto TypeAliasResult = Impl.createDeclWithClangNode<TypeAliasDecl>(
2892+
Decl, AccessLevel::Public,
2893+
Impl.importSourceLoc(Decl->getBeginLoc()), SourceLoc(), Name, Loc,
2894+
/*genericparams*/ nullptr, DC);
2895+
2896+
TypeAliasResult->setUnderlyingType(SwiftType);
2897+
Result = TypeAliasResult;
2898+
}
28672899

28682900
// Make Objective-C's 'id' unavailable.
28692901
if (Impl.SwiftContext.LangOpts.EnableObjCInterop && isObjCId(Decl)) {

test/Interop/Cxx/enum/c-enums-withOptions-omit.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
import CenumsWithOptionsOmit
55

6+
// CHECK: struct NSEnumerationOptions : OptionSet {
7+
// CHECK-NEXT: init(rawValue: Int32)
8+
// CHECK-NEXT: let rawValue: Int32
9+
// CHECK-NEXT: typealias RawValue = Int32
10+
// CHECK-NEXT: typealias Element = NSEnumerationOptions
11+
// CHECK-NEXT: typealias ArrayLiteralElement = NSEnumerationOptions
12+
613
// CHECK: class NSSet {
714
// CHECK-NEXT: class func enumerateObjects(options
815
// CHECK-NEXT: func enumerateObjects(options

0 commit comments

Comments
 (0)