Skip to content

Commit 2c983d9

Browse files
committed
[cxx-interop] Apply typedef -> enum patch to method param types as well.
Basically just applying #42431 to ObjC method param tyeps.
1 parent e6af2d9 commit 2c983d9

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
@@ -2734,10 +2734,28 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
27342734
ImportDiagnosticAdder addImportDiag(*this, clangDecl,
27352735
clangDecl->getLocation());
27362736
clang::QualType resultType = clangDecl->getReturnType();
2737-
auto importedType =
2738-
importType(resultType, resultKind, addImportDiag,
2739-
allowNSUIntegerAsIntInResult, Bridgeability::Full,
2740-
getImportTypeAttrs(clangDecl), OptionalityOfReturn);
2737+
2738+
2739+
ImportedType importedType;
2740+
if (auto typedefType = dyn_cast<clang::TypedefType>(resultType.getTypePtr())) {
2741+
if (isUnavailableInSwift(typedefType->getDecl())) {
2742+
if (auto clangEnum = findAnonymousEnumForTypedef(SwiftContext, typedefType)) {
2743+
// If this fails, it means that we need a stronger predicate for
2744+
// determining the relationship between an enum and typedef.
2745+
assert(clangEnum.getValue()->getIntegerType()->getCanonicalTypeInternal() ==
2746+
typedefType->getCanonicalTypeInternal());
2747+
if (auto swiftEnum = importDecl(*clangEnum, CurrentVersion)) {
2748+
importedType = {cast<NominalTypeDecl>(swiftEnum)->getDeclaredType(), false};
2749+
}
2750+
}
2751+
}
2752+
}
2753+
2754+
if (!importedType)
2755+
importedType = importType(resultType, resultKind, addImportDiag,
2756+
allowNSUIntegerAsIntInResult, Bridgeability::Full,
2757+
getImportTypeAttrs(clangDecl),
2758+
OptionalityOfReturn);
27412759

27422760
// Adjust the result type for a throwing function.
27432761
if (importedType.getType() && errorInfo) {
@@ -2847,7 +2865,21 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
28472865
ImportTypeKind importKind = ImportTypeKind::Parameter;
28482866
ImportDiagnosticAdder paramAddDiag(*this, clangDecl, param->getLocation());
28492867
Type swiftParamTy;
2850-
bool paramIsIUO;
2868+
bool paramIsIUO = false;
2869+
if (auto typedefType = dyn_cast<clang::TypedefType>(paramTy.getTypePtr())) {
2870+
if (isUnavailableInSwift(typedefType->getDecl())) {
2871+
if (auto clangEnum = findAnonymousEnumForTypedef(SwiftContext, typedefType)) {
2872+
// If this fails, it means that we need a stronger predicate for
2873+
// determining the relationship between an enum and typedef.
2874+
assert(clangEnum.getValue()->getIntegerType()->getCanonicalTypeInternal() ==
2875+
typedefType->getCanonicalTypeInternal());
2876+
if (auto swiftEnum = importDecl(*clangEnum, CurrentVersion)) {
2877+
swiftParamTy = cast<NominalTypeDecl>(swiftEnum)->getDeclaredType();
2878+
}
2879+
}
2880+
}
2881+
}
2882+
28512883
if (kind == SpecialMethodKind::NSDictionarySubscriptGetter &&
28522884
paramTy->isObjCIdType()) {
28532885
// Not using `getImportTypeAttrs()` is unprincipled but OK for this hack.
@@ -2859,7 +2891,7 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
28592891
swiftParamTy = OptionalType::get(swiftParamTy);
28602892

28612893
paramIsIUO = optionalityOfParam == OTK_ImplicitlyUnwrappedOptional;
2862-
} else {
2894+
} else if (!swiftParamTy) {
28632895
if (param->hasAttr<clang::CFReturnsRetainedAttr>())
28642896
importKind = ImportTypeKind::CFRetainedOutParameter;
28652897
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)