Skip to content

Commit db6872a

Browse files
authored
Merge pull request #68853 from hyp/eng/revert-ns-options-x2
revert "Revert "Revert "[cxx-interop] Import custom NS_OPTIONS correctly"""
2 parents a744d3e + 1f6373e commit db6872a

File tree

9 files changed

+75
-104
lines changed

9 files changed

+75
-104
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ class ClangModuleLoader : public ModuleLoader {
306306
virtual EffectiveClangContext getEffectiveClangContext(
307307
const NominalTypeDecl *nominal) = 0;
308308

309+
virtual const clang::TypedefType *
310+
getTypeDefForCXXCFOptionsDefinition(const clang::Decl *candidateDecl) = 0;
311+
309312
virtual SourceLoc importSourceLocation(clang::SourceLocation loc) = 0;
310313
};
311314

include/swift/ClangImporter/ClangImporter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ class ClangImporter final : public ClangModuleLoader {
609609
/// Enable the symbolic import experimental feature for the given callback.
610610
void withSymbolicFeatureEnabled(llvm::function_ref<void(void)> callback);
611611

612-
static const clang::TypedefType *getTypedefForCXXCFOptionsDefinition(
613-
const clang::Decl *candidateDecl, const ASTContext &ctx);
612+
const clang::TypedefType *getTypeDefForCXXCFOptionsDefinition(
613+
const clang::Decl *candidateDecl) override;
614614

615615
SourceLoc importSourceLocation(clang::SourceLocation loc) override;
616616
};

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,8 +2548,8 @@ ASTMangler::getTypeDefForCXXCFOptionsDefinition(const ValueDecl *decl) {
25482548
if (!clangDecl)
25492549
return nullptr;
25502550

2551-
auto &ctx = decl->getASTContext();
2552-
return ClangImporter::getTypedefForCXXCFOptionsDefinition(clangDecl, ctx);
2551+
const auto &clangModuleLoader = decl->getASTContext().getClangModuleLoader();
2552+
return clangModuleLoader->getTypeDefForCXXCFOptionsDefinition(clangDecl);
25532553
}
25542554

25552555
const clang::NamedDecl *

lib/ClangImporter/ClangImporter.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7023,23 +7023,26 @@ void ClangImporter::withSymbolicFeatureEnabled(
70237023
oldImportSymbolicCXXDecls.get());
70247024
}
70257025

7026-
const clang::TypedefType *ClangImporter::getTypedefForCXXCFOptionsDefinition(
7027-
const clang::Decl *candidateDecl, const ASTContext &ctx) {
7028-
if (!ctx.LangOpts.EnableCXXInterop)
7026+
const clang::TypedefType *ClangImporter::getTypeDefForCXXCFOptionsDefinition(
7027+
const clang::Decl *candidateDecl) {
7028+
7029+
if (!Impl.SwiftContext.LangOpts.EnableCXXInterop)
70297030
return nullptr;
70307031

70317032
auto enumDecl = dyn_cast<clang::EnumDecl>(candidateDecl);
70327033
if (!enumDecl)
70337034
return nullptr;
7035+
70347036
if (!enumDecl->getDeclName().isEmpty())
70357037
return nullptr;
70367038

70377039
const clang::ElaboratedType *elaboratedType =
7038-
enumDecl->getIntegerType()->getAs<clang::ElaboratedType>();
7040+
dyn_cast<clang::ElaboratedType>(enumDecl->getIntegerType().getTypePtr());
70397041
if (auto typedefType =
70407042
elaboratedType
70417043
? dyn_cast<clang::TypedefType>(elaboratedType->desugar())
7042-
: enumDecl->getIntegerType()->getAs<clang::TypedefType>()) {
7044+
: dyn_cast<clang::TypedefType>(
7045+
enumDecl->getIntegerType().getTypePtr())) {
70437046
auto enumExtensibilityAttr =
70447047
elaboratedType
70457048
? enumDecl->getAttr<clang::EnumExtensibilityAttr>()
@@ -7052,13 +7055,8 @@ const clang::TypedefType *ClangImporter::getTypedefForCXXCFOptionsDefinition(
70527055
enumExtensibilityAttr->getExtensibility() ==
70537056
clang::EnumExtensibilityAttr::Open &&
70547057
hasFlagEnumAttr) {
7055-
// Make sure the typedef is marked as unavailable in Swift.
7056-
auto typedefDecl = typedefType->getDecl();
7057-
for (auto *attr :
7058-
typedefDecl->specific_attrs<clang::AvailabilityAttr>()) {
7059-
if (attr->getPlatform()->getName() == "swift")
7060-
return typedefType;
7061-
}
7058+
return Impl.isUnavailableInSwift(typedefType->getDecl()) ? typedefType
7059+
: nullptr;
70627060
}
70637061
}
70647062

lib/ClangImporter/ImportType.cpp

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,30 +2646,44 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
26462646
}
26472647
} else if (const clang::TypedefType *typedefType =
26482648
type->getAs<clang::TypedefType>()) {
2649-
clang::TypedefNameDecl *typedefDecl = typedefType->getDecl();
2650-
// Find the next decl in the same context. If this typedef is a part of an
2651-
// NS/CF_OPTIONS declaration, the next decl will be an enum.
2652-
auto declsInContext = typedefDecl->getDeclContext()->decls();
2653-
auto declIter = llvm::find(declsInContext, typedefDecl);
2654-
if (declIter != declsInContext.end())
2655-
declIter++;
2656-
if (declIter != declsInContext.end()) {
2657-
if (auto enumDecl = dyn_cast<clang::EnumDecl>(*declIter)) {
2658-
if (auto cfOptionsTy =
2659-
ClangImporter::getTypedefForCXXCFOptionsDefinition(
2660-
enumDecl, nameImporter.getContext())) {
2661-
if (cfOptionsTy->getDecl() == typedefDecl) {
2662-
auto enumName = typedefDecl->getName();
2663-
ArgumentAttrs argumentAttrs(DefaultArgumentKind::None, true,
2664-
enumName);
2665-
for (auto word : llvm::reverse(camel_case::getWords(enumName))) {
2666-
if (camel_case::sameWordIgnoreFirstCase(word, "options")) {
2667-
argumentAttrs.argumentKind = DefaultArgumentKind::EmptyArray;
2668-
}
2669-
}
2670-
return argumentAttrs;
2671-
}
2649+
// Get the AvailabilityAttr that would be set from CF/NS_OPTIONS
2650+
if (importer::isUnavailableInSwift(typedefType->getDecl(), nullptr, true)) {
2651+
// If we've taken this branch it means we have an enum type, and it is
2652+
// likely an integer or NSInteger that is being used by NS/CF_OPTIONS to
2653+
// behave like a C enum in the presence of C++.
2654+
auto enumName = typedefType->getDecl()->getName();
2655+
ArgumentAttrs argumentAttrs(DefaultArgumentKind::None, true, enumName);
2656+
auto camelCaseWords = camel_case::getWords(enumName);
2657+
for (auto it = camelCaseWords.rbegin(); it != camelCaseWords.rend();
2658+
++it) {
2659+
auto word = *it;
2660+
auto next = std::next(it);
2661+
if (camel_case::sameWordIgnoreFirstCase(word, "options")) {
2662+
argumentAttrs.argumentKind = DefaultArgumentKind::EmptyArray;
2663+
return argumentAttrs;
26722664
}
2665+
if (camel_case::sameWordIgnoreFirstCase(word, "units"))
2666+
return argumentAttrs;
2667+
if (camel_case::sameWordIgnoreFirstCase(word, "domain"))
2668+
return argumentAttrs;
2669+
if (camel_case::sameWordIgnoreFirstCase(word, "action"))
2670+
return argumentAttrs;
2671+
if (camel_case::sameWordIgnoreFirstCase(word, "event"))
2672+
return argumentAttrs;
2673+
if (camel_case::sameWordIgnoreFirstCase(word, "events") &&
2674+
next != camelCaseWords.rend() &&
2675+
camel_case::sameWordIgnoreFirstCase(*next, "control"))
2676+
return argumentAttrs;
2677+
if (camel_case::sameWordIgnoreFirstCase(word, "state"))
2678+
return argumentAttrs;
2679+
if (camel_case::sameWordIgnoreFirstCase(word, "unit"))
2680+
return argumentAttrs;
2681+
if (camel_case::sameWordIgnoreFirstCase(word, "position") &&
2682+
next != camelCaseWords.rend() &&
2683+
camel_case::sameWordIgnoreFirstCase(*next, "scroll"))
2684+
return argumentAttrs;
2685+
if (camel_case::sameWordIgnoreFirstCase(word, "edge"))
2686+
return argumentAttrs;
26732687
}
26742688
}
26752689
}

test/Interop/Cxx/enum/Inputs/c-enums-withOptions-omit.h

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,39 @@
1-
typedef unsigned NSUInteger;
2-
3-
#define __CF_OPTIONS_ATTRIBUTES __attribute__((flag_enum,enum_extensibility(open)))
4-
#if (__cplusplus)
5-
#define CF_OPTIONS(_type, _name) __attribute__((availability(swift,unavailable))) _type _name; enum __CF_OPTIONS_ATTRIBUTES : _name
6-
#else
7-
#define CF_OPTIONS(_type, _name) enum __CF_OPTIONS_ATTRIBUTES _name : _type _name; enum _name : _type
8-
#endif
9-
10-
typedef CF_OPTIONS(NSUInteger, NSEnumerationOptions) {
11-
NSEnumerationConcurrent = (1UL << 0),
12-
NSEnumerationReverse = (1UL << 1),
13-
};
1+
// Enum usage that is bitwise-able and assignable in C++, aka how CF_OPTIONS
2+
// does things.
3+
typedef int __attribute__((availability(swift, unavailable))) NSEnumerationOptions;
4+
enum : NSEnumerationOptions { NSEnumerationConcurrent, NSEnumerationReverse };
145

156
@interface NSSet
167
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts ;
178
@end
189

19-
typedef CF_OPTIONS(NSUInteger, NSOrderedCollectionDifferenceCalculationOptions) {
10+
typedef int __attribute__((availability(swift, unavailable))) NSOrderedCollectionDifferenceCalculationOptions;
11+
enum : NSOrderedCollectionDifferenceCalculationOptions {
2012
NSOrderedCollectionDifferenceCalculationOptions1,
2113
NSOrderedCollectionDifferenceCalculationOptions2
2214
};
2315

24-
typedef CF_OPTIONS(NSUInteger, NSCalendarUnit) {
25-
NSCalendarUnit1,
26-
NSCalendarUnit2
27-
};
16+
typedef int __attribute__((availability(swift, unavailable))) NSCalendarUnit;
17+
enum : NSCalendarUnit { NSCalendarUnit1, NSCalendarUnit2 };
2818

29-
typedef CF_OPTIONS(NSUInteger, NSSearchPathDomainMask) {
30-
NSSearchPathDomainMask1,
31-
NSSearchPathDomainMask2
32-
};
19+
typedef int __attribute__((availability(swift, unavailable))) NSSearchPathDomainMask;
20+
enum : NSSearchPathDomainMask { NSSearchPathDomainMask1, NSSearchPathDomainMask2 };
3321

34-
typedef CF_OPTIONS(NSUInteger, NSControlCharacterAction) {
35-
NSControlCharacterAction1,
36-
NSControlCharacterAction2
37-
};
22+
typedef int __attribute__((availability(swift, unavailable))) NSControlCharacterAction;
23+
enum : NSControlCharacterAction { NSControlCharacterAction1, NSControlCharacterAction2 };
3824

39-
typedef CF_OPTIONS(NSUInteger, UIControlState) {
40-
UIControlState1,
41-
UIControlState2
42-
};
25+
typedef int __attribute__((availability(swift, unavailable))) UIControlState;
26+
enum : UIControlState { UIControlState1, UIControlState2 };
4327

44-
typedef CF_OPTIONS(NSUInteger, UITableViewCellStateMask) {
45-
UITableViewCellStateMask1,
46-
UITableViewCellStateMask2
47-
};
28+
typedef int __attribute__((availability(swift, unavailable))) UITableViewCellStateMask;
29+
enum : UITableViewCellStateMask { UITableViewCellStateMask1, UITableViewCellStateMask2 };
4830

49-
typedef CF_OPTIONS(NSUInteger, UIControlEvents) {
50-
UIControlEvents1,
51-
UIControlEvents2
52-
};
31+
typedef int __attribute__((availability(swift, unavailable))) UIControlEvents;
32+
enum : UIControlEvents { UIControlEvents1, UIControlEvents2 };
5333

54-
typedef CF_OPTIONS(NSUInteger, UITableViewScrollPosition) {
34+
typedef int __attribute__((availability(swift, unavailable)))
35+
UITableViewScrollPosition;
36+
enum : UITableViewScrollPosition {
5537
UITableViewScrollPosition1,
5638
UITableViewScrollPosition2
5739
};

test/Interop/Cxx/objc-correctness/Inputs/customNSOptions.h

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/Interop/Cxx/objc-correctness/Inputs/module.modulemap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ module CxxClassWithNSStringInit {
99
requires cplusplus
1010
}
1111

12-
module CustomNSOptions {
13-
header "customNSOptions.h"
14-
}
15-
1612
module NSOptionsMangling {
1713
header "NSOptionsMangling.h"
1814
}

test/Interop/Cxx/objc-correctness/custom-nsoptions.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)