Skip to content

Commit 5be506e

Browse files
authored
Merge pull request #42452 from zoecarver/method-param-types-use-enum-not-typedef
[cxx-interop] Apply typedef -> enum patch to method param types as well.
2 parents 255f965 + 2c983d9 commit 5be506e

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,10 +2747,28 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
27472747
ImportDiagnosticAdder addImportDiag(*this, clangDecl,
27482748
clangDecl->getLocation());
27492749
clang::QualType resultType = clangDecl->getReturnType();
2750-
auto importedType =
2751-
importType(resultType, resultKind, addImportDiag,
2752-
allowNSUIntegerAsIntInResult, Bridgeability::Full,
2753-
getImportTypeAttrs(clangDecl), OptionalityOfReturn);
2750+
2751+
2752+
ImportedType importedType;
2753+
if (auto typedefType = dyn_cast<clang::TypedefType>(resultType.getTypePtr())) {
2754+
if (isUnavailableInSwift(typedefType->getDecl())) {
2755+
if (auto clangEnum = findAnonymousEnumForTypedef(SwiftContext, typedefType)) {
2756+
// If this fails, it means that we need a stronger predicate for
2757+
// determining the relationship between an enum and typedef.
2758+
assert(clangEnum.getValue()->getIntegerType()->getCanonicalTypeInternal() ==
2759+
typedefType->getCanonicalTypeInternal());
2760+
if (auto swiftEnum = importDecl(*clangEnum, CurrentVersion)) {
2761+
importedType = {cast<NominalTypeDecl>(swiftEnum)->getDeclaredType(), false};
2762+
}
2763+
}
2764+
}
2765+
}
2766+
2767+
if (!importedType)
2768+
importedType = importType(resultType, resultKind, addImportDiag,
2769+
allowNSUIntegerAsIntInResult, Bridgeability::Full,
2770+
getImportTypeAttrs(clangDecl),
2771+
OptionalityOfReturn);
27542772

27552773
// Adjust the result type for a throwing function.
27562774
if (importedType.getType() && errorInfo) {
@@ -2860,7 +2878,21 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
28602878
ImportTypeKind importKind = ImportTypeKind::Parameter;
28612879
ImportDiagnosticAdder paramAddDiag(*this, clangDecl, param->getLocation());
28622880
Type swiftParamTy;
2863-
bool paramIsIUO;
2881+
bool paramIsIUO = false;
2882+
if (auto typedefType = dyn_cast<clang::TypedefType>(paramTy.getTypePtr())) {
2883+
if (isUnavailableInSwift(typedefType->getDecl())) {
2884+
if (auto clangEnum = findAnonymousEnumForTypedef(SwiftContext, typedefType)) {
2885+
// If this fails, it means that we need a stronger predicate for
2886+
// determining the relationship between an enum and typedef.
2887+
assert(clangEnum.getValue()->getIntegerType()->getCanonicalTypeInternal() ==
2888+
typedefType->getCanonicalTypeInternal());
2889+
if (auto swiftEnum = importDecl(*clangEnum, CurrentVersion)) {
2890+
swiftParamTy = cast<NominalTypeDecl>(swiftEnum)->getDeclaredType();
2891+
}
2892+
}
2893+
}
2894+
}
2895+
28642896
if (kind == SpecialMethodKind::NSDictionarySubscriptGetter &&
28652897
paramTy->isObjCIdType()) {
28662898
// Not using `getImportTypeAttrs()` is unprincipled but OK for this hack.
@@ -2872,7 +2904,7 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
28722904
swiftParamTy = OptionalType::get(swiftParamTy);
28732905

28742906
paramIsIUO = optionalityOfParam == OTK_ImplicitlyUnwrappedOptional;
2875-
} else {
2907+
} else if (!swiftParamTy) {
28762908
if (param->hasAttr<clang::CFReturnsRetainedAttr>())
28772909
importKind = ImportTypeKind::CFRetainedOutParameter;
28782910
else if (param->hasAttr<clang::CFReturnsNotRetainedAttr>())

test/Interop/Cxx/enum/Inputs/anonymous-with-swift-name.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ typedef CF_OPTIONS(unsigned, CFColorMask) {
2222
inline SOColorMask useSOColorMask(SOColorMask mask) { return mask; }
2323
inline CFColorMask useCFColorMask(CFColorMask mask) { return mask; }
2424

25+
#if __OBJC__
26+
@interface ColorMaker
27+
- (void)makeColorWithOptions:(SOColorMask)opts;
28+
- (void)makeOtherColorWithInt:(int) x withOptions:(CFColorMask)opts;
29+
@end
30+
#endif // SWIFT_OBJC_INTEROP
31+
2532
#endif // TEST_INTEROP_CXX_ENUM_INPUTS_ANONYMOUS_WITH_SWIFT_NAME_H
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=AnonymousWithSwiftName -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
2+
//
3+
// REQUIRES: objc_interop
4+
5+
// CHECK: class ColorMaker {
6+
// CHECK: class func makeColor(withOptions opts: SOColorMask)
7+
// CHECK: func makeColor(withOptions opts: SOColorMask)
8+
// CHECK: @available(swift, obsoleted: 3, renamed: "makeColor(withOptions:)")
9+
// CHECK: class func makeColorWithOptions(_ opts: SOColorMask)
10+
// CHECK: @available(swift, obsoleted: 3, renamed: "makeColor(withOptions:)")
11+
// CHECK: func makeColorWithOptions(_ opts: SOColorMask)
12+
// CHECK: class func makeOtherColor(with x: Int32, withOptions opts: CFColorMask)
13+
// CHECK: func makeOtherColor(with x: Int32, withOptions opts: CFColorMask)
14+
// CHECK: @available(swift, obsoleted: 3, renamed: "makeOtherColor(with:withOptions:)")
15+
// CHECK: class func makeOtherColorWithInt(_ x: Int32, withOptions opts: CFColorMask)
16+
// CHECK: @available(swift, obsoleted: 3, renamed: "makeOtherColor(with:withOptions:)")
17+
// CHECK: func makeOtherColorWithInt(_ x: Int32, withOptions opts: CFColorMask)
18+
// CHECK: }

0 commit comments

Comments
 (0)