@@ -2255,10 +2255,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
22552255 OptionalityOfReturn = OTK_ImplicitlyUnwrappedOptional;
22562256 }
22572257
2258- clang::QualType returnType = clangDecl->getReturnType ();
2259- if (auto elaborated =
2260- dyn_cast<clang::ElaboratedType>(returnType))
2261- returnType = elaborated->desugar ();
2258+ clang::QualType returnType = desugarIfElaborated (clangDecl->getReturnType ());
22622259 // In C interop mode, the return type of library builtin functions
22632260 // like 'memcpy' from headers like 'string.h' drops
22642261 // any nullability specifiers from their return type, and preserves it on the
@@ -2296,19 +2293,9 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
22962293 ->isTemplateTypeParmType ())
22972294 OptionalityOfReturn = OTK_None;
22982295
2299- if (auto typedefType = dyn_cast<clang::TypedefType>(returnType)) {
2300- if (isUnavailableInSwift (typedefType->getDecl ())) {
2301- if (auto clangEnum = findAnonymousEnumForTypedef (SwiftContext, typedefType)) {
2302- // If this fails, it means that we need a stronger predicate for
2303- // determining the relationship between an enum and typedef.
2304- assert (clangEnum.value ()->getIntegerType ()->getCanonicalTypeInternal () ==
2305- typedefType->getCanonicalTypeInternal ());
2306- if (auto swiftEnum = importDecl (*clangEnum, CurrentVersion)) {
2307- return {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType (), false };
2308- }
2309- }
2310- }
2311- }
2296+ ImportedType optionSetEnum = importer::findOptionSetEnum (returnType, *this );
2297+ if (optionSetEnum)
2298+ return optionSetEnum;
23122299
23132300 // Import the underlying result type.
23142301 if (clangDecl) {
@@ -2376,27 +2363,11 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23762363
23772364 // Only eagerly import the return type if it's not too expensive (the current
23782365 // heuristic for that is if it's not a record type).
2379- ImportedType importedType;
23802366 ImportDiagnosticAdder addDiag (*this , clangDecl,
23812367 clangDecl->getSourceRange ().getBegin ());
2382- clang::QualType returnType = clangDecl->getReturnType ();
2383- if (auto elaborated = dyn_cast<clang::ElaboratedType>(returnType))
2384- returnType = elaborated->desugar ();
2385-
2386- if (auto typedefType = dyn_cast<clang::TypedefType>(returnType)) {
2387- if (isUnavailableInSwift (typedefType->getDecl ())) {
2388- if (auto clangEnum = findAnonymousEnumForTypedef (SwiftContext, typedefType)) {
2389- // If this fails, it means that we need a stronger predicate for
2390- // determining the relationship between an enum and typedef.
2391- assert (clangEnum.value ()->getIntegerType ()->getCanonicalTypeInternal () ==
2392- typedefType->getCanonicalTypeInternal ());
2393- if (auto swiftEnum = importDecl (*clangEnum, CurrentVersion)) {
2394- importedType = {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType (),
2395- false };
2396- }
2397- }
2398- }
2399- }
2368+ clang::QualType returnType = desugarIfElaborated (clangDecl->getReturnType ());
2369+
2370+ ImportedType importedType = importer::findOptionSetEnum (returnType, *this );
24002371
24012372 if (auto templateType =
24022373 dyn_cast<clang::TemplateTypeParmType>(returnType)) {
@@ -2460,9 +2431,7 @@ ClangImporter::Implementation::importParameterType(
24602431 std::optional<unsigned > completionHandlerErrorParamIndex,
24612432 ArrayRef<GenericTypeParamDecl *> genericParams,
24622433 llvm::function_ref<void (Diagnostic &&)> addImportDiagnosticFn) {
2463- auto paramTy = param->getType ();
2464- if (auto elaborated = dyn_cast<clang::ElaboratedType>(paramTy))
2465- paramTy = elaborated->desugar ();
2434+ auto paramTy = desugarIfElaborated (param->getType ());
24662435
24672436 ImportTypeKind importKind = paramIsCompletionHandler
24682437 ? ImportTypeKind::CompletionHandlerParameter
@@ -2475,23 +2444,8 @@ ClangImporter::Implementation::importParameterType(
24752444 bool isConsuming = false ;
24762445 bool isParamTypeImplicitlyUnwrapped = false ;
24772446
2478- // Sometimes we import unavailable typedefs as enums. If that's the case,
2479- // use the enum, not the typedef here.
2480- if (auto typedefType = dyn_cast<clang::TypedefType>(paramTy.getTypePtr ())) {
2481- if (isUnavailableInSwift (typedefType->getDecl ())) {
2482- if (auto clangEnum =
2483- findAnonymousEnumForTypedef (SwiftContext, typedefType)) {
2484- // If this fails, it means that we need a stronger predicate for
2485- // determining the relationship between an enum and typedef.
2486- assert (clangEnum.value ()
2487- ->getIntegerType ()
2488- ->getCanonicalTypeInternal () ==
2489- typedefType->getCanonicalTypeInternal ());
2490- if (auto swiftEnum = importDecl (*clangEnum, CurrentVersion)) {
2491- swiftParamTy = cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType ();
2492- }
2493- }
2494- }
2447+ if (auto optionSetEnum = importer::findOptionSetEnum (paramTy, *this )) {
2448+ swiftParamTy = optionSetEnum.getType ();
24952449 } else if (isa<clang::PointerType>(paramTy) &&
24962450 isa<clang::TemplateTypeParmType>(paramTy->getPointeeType ())) {
24972451 auto pointeeType = paramTy->getPointeeType ();
@@ -3211,26 +3165,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
32113165
32123166 ImportDiagnosticAdder addImportDiag (*this , clangDecl,
32133167 clangDecl->getLocation ());
3214- clang::QualType resultType = clangDecl->getReturnType ();
3215- if (auto elaborated = dyn_cast<clang::ElaboratedType>(resultType))
3216- resultType = elaborated->desugar ();
3217-
3218- ImportedType importedType;
3219- if (auto typedefType = dyn_cast<clang::TypedefType>(resultType.getTypePtr ())) {
3220- if (isUnavailableInSwift (typedefType->getDecl ())) {
3221- if (auto clangEnum = findAnonymousEnumForTypedef (SwiftContext, typedefType)) {
3222- // If this fails, it means that we need a stronger predicate for
3223- // determining the relationship between an enum and typedef.
3224- assert (clangEnum.value ()->getIntegerType ()->getCanonicalTypeInternal () ==
3225- typedefType->getCanonicalTypeInternal ());
3226- if (auto swiftEnum = importDecl (*clangEnum, CurrentVersion)) {
3227- importedType = {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType (),
3228- false };
3229- }
3230- }
3231- }
3232- }
3233-
3168+ clang::QualType resultType = desugarIfElaborated (clangDecl->getReturnType ());
3169+ ImportedType importedType = importer::findOptionSetEnum (resultType, *this );
32343170 if (!importedType)
32353171 importedType = importType (resultType, resultKind, addImportDiag,
32363172 allowNSUIntegerAsIntInResult, Bridgeability::Full,
@@ -3478,9 +3414,6 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
34783414 importedType.isImplicitlyUnwrapped ()};
34793415}
34803416
3481- ImportedType findOptionSetType (clang::QualType type,
3482- ClangImporter::Implementation &Impl);
3483-
34843417ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType (
34853418 const DeclContext *dc, const clang::ObjCPropertyDecl *property,
34863419 const clang::ObjCMethodDecl *clangDecl, bool isFromSystemModule,
@@ -3506,11 +3439,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
35063439 if (!origDC)
35073440 return {Type (), false };
35083441
3509- auto fieldType = isGetter ? clangDecl-> getReturnType ()
3510- : clangDecl->getParamDecl ( 0 )-> getType ();
3511-
3442+ auto fieldType =
3443+ desugarIfElaborated (isGetter ? clangDecl->getReturnType ()
3444+ : clangDecl-> getParamDecl ( 0 )-> getType ());
35123445 // Import the property type, independent of what kind of accessor this is.
3513- ImportedType importedType = findOptionSetType (fieldType, *this );
3446+ ImportedType importedType = importer::findOptionSetEnum (fieldType, *this );
35143447 if (!importedType)
35153448 importedType = importPropertyType (property, isFromSystemModule);
35163449 if (!importedType)
0 commit comments