@@ -2282,10 +2282,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
22822282 OptionalityOfReturn = OTK_ImplicitlyUnwrappedOptional;
22832283 }
22842284
2285- clang::QualType returnType = clangDecl->getReturnType ();
2286- if (auto elaborated =
2287- dyn_cast<clang::ElaboratedType>(returnType))
2288- returnType = elaborated->desugar ();
2285+ clang::QualType returnType = desugarIfElaborated (clangDecl->getReturnType ());
22892286 // In C interop mode, the return type of library builtin functions
22902287 // like 'memcpy' from headers like 'string.h' drops
22912288 // any nullability specifiers from their return type, and preserves it on the
@@ -2323,19 +2320,9 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
23232320 ->isTemplateTypeParmType ())
23242321 OptionalityOfReturn = OTK_None;
23252322
2326- if (auto typedefType = dyn_cast<clang::TypedefType>(returnType)) {
2327- if (isUnavailableInSwift (typedefType->getDecl ())) {
2328- if (auto clangEnum = findAnonymousEnumForTypedef (SwiftContext, typedefType)) {
2329- // If this fails, it means that we need a stronger predicate for
2330- // determining the relationship between an enum and typedef.
2331- assert (clangEnum.value ()->getIntegerType ()->getCanonicalTypeInternal () ==
2332- typedefType->getCanonicalTypeInternal ());
2333- if (auto swiftEnum = importDecl (*clangEnum, CurrentVersion)) {
2334- return {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType (), false };
2335- }
2336- }
2337- }
2338- }
2323+ ImportedType optionSetEnum = importer::findOptionSetEnum (returnType, *this );
2324+ if (optionSetEnum)
2325+ return optionSetEnum;
23392326
23402327 // Import the underlying result type.
23412328 if (clangDecl) {
@@ -2399,27 +2386,11 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23992386
24002387 // Only eagerly import the return type if it's not too expensive (the current
24012388 // heuristic for that is if it's not a record type).
2402- ImportedType importedType;
24032389 ImportDiagnosticAdder addDiag (*this , clangDecl,
24042390 clangDecl->getSourceRange ().getBegin ());
2405- clang::QualType returnType = clangDecl->getReturnType ();
2406- if (auto elaborated = dyn_cast<clang::ElaboratedType>(returnType))
2407- returnType = elaborated->desugar ();
2408-
2409- if (auto typedefType = dyn_cast<clang::TypedefType>(returnType)) {
2410- if (isUnavailableInSwift (typedefType->getDecl ())) {
2411- if (auto clangEnum = findAnonymousEnumForTypedef (SwiftContext, typedefType)) {
2412- // If this fails, it means that we need a stronger predicate for
2413- // determining the relationship between an enum and typedef.
2414- assert (clangEnum.value ()->getIntegerType ()->getCanonicalTypeInternal () ==
2415- typedefType->getCanonicalTypeInternal ());
2416- if (auto swiftEnum = importDecl (*clangEnum, CurrentVersion)) {
2417- importedType = {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType (),
2418- false };
2419- }
2420- }
2421- }
2422- }
2391+ clang::QualType returnType = desugarIfElaborated (clangDecl->getReturnType ());
2392+
2393+ ImportedType importedType = importer::findOptionSetEnum (returnType, *this );
24232394
24242395 if (auto templateType =
24252396 dyn_cast<clang::TemplateTypeParmType>(returnType)) {
@@ -2483,9 +2454,7 @@ ClangImporter::Implementation::importParameterType(
24832454 std::optional<unsigned > completionHandlerErrorParamIndex,
24842455 ArrayRef<GenericTypeParamDecl *> genericParams,
24852456 llvm::function_ref<void (Diagnostic &&)> addImportDiagnosticFn) {
2486- auto paramTy = param->getType ();
2487- if (auto elaborated = dyn_cast<clang::ElaboratedType>(paramTy))
2488- paramTy = elaborated->desugar ();
2457+ auto paramTy = desugarIfElaborated (param->getType ());
24892458
24902459 ImportTypeKind importKind = paramIsCompletionHandler
24912460 ? ImportTypeKind::CompletionHandlerParameter
@@ -2498,23 +2467,8 @@ ClangImporter::Implementation::importParameterType(
24982467 bool isConsuming = false ;
24992468 bool isParamTypeImplicitlyUnwrapped = false ;
25002469
2501- // Sometimes we import unavailable typedefs as enums. If that's the case,
2502- // use the enum, not the typedef here.
2503- if (auto typedefType = dyn_cast<clang::TypedefType>(paramTy.getTypePtr ())) {
2504- if (isUnavailableInSwift (typedefType->getDecl ())) {
2505- if (auto clangEnum =
2506- findAnonymousEnumForTypedef (SwiftContext, typedefType)) {
2507- // If this fails, it means that we need a stronger predicate for
2508- // determining the relationship between an enum and typedef.
2509- assert (clangEnum.value ()
2510- ->getIntegerType ()
2511- ->getCanonicalTypeInternal () ==
2512- typedefType->getCanonicalTypeInternal ());
2513- if (auto swiftEnum = importDecl (*clangEnum, CurrentVersion)) {
2514- swiftParamTy = cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType ();
2515- }
2516- }
2517- }
2470+ if (auto optionSetEnum = importer::findOptionSetEnum (paramTy, *this )) {
2471+ swiftParamTy = optionSetEnum.getType ();
25182472 } else if (isa<clang::PointerType>(paramTy) &&
25192473 isa<clang::TemplateTypeParmType>(paramTy->getPointeeType ())) {
25202474 auto pointeeType = paramTy->getPointeeType ();
@@ -3234,26 +3188,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
32343188
32353189 ImportDiagnosticAdder addImportDiag (*this , clangDecl,
32363190 clangDecl->getLocation ());
3237- clang::QualType resultType = clangDecl->getReturnType ();
3238- if (auto elaborated = dyn_cast<clang::ElaboratedType>(resultType))
3239- resultType = elaborated->desugar ();
3240-
3241- ImportedType importedType;
3242- if (auto typedefType = dyn_cast<clang::TypedefType>(resultType.getTypePtr ())) {
3243- if (isUnavailableInSwift (typedefType->getDecl ())) {
3244- if (auto clangEnum = findAnonymousEnumForTypedef (SwiftContext, typedefType)) {
3245- // If this fails, it means that we need a stronger predicate for
3246- // determining the relationship between an enum and typedef.
3247- assert (clangEnum.value ()->getIntegerType ()->getCanonicalTypeInternal () ==
3248- typedefType->getCanonicalTypeInternal ());
3249- if (auto swiftEnum = importDecl (*clangEnum, CurrentVersion)) {
3250- importedType = {cast<TypeDecl>(swiftEnum)->getDeclaredInterfaceType (),
3251- false };
3252- }
3253- }
3254- }
3255- }
3256-
3191+ clang::QualType resultType = desugarIfElaborated (clangDecl->getReturnType ());
3192+ ImportedType importedType = importer::findOptionSetEnum (resultType, *this );
32573193 if (!importedType)
32583194 importedType = importType (resultType, resultKind, addImportDiag,
32593195 allowNSUIntegerAsIntInResult, Bridgeability::Full,
@@ -3501,9 +3437,6 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
35013437 importedType.isImplicitlyUnwrapped ()};
35023438}
35033439
3504- ImportedType findOptionSetType (clang::QualType type,
3505- ClangImporter::Implementation &Impl);
3506-
35073440ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType (
35083441 const DeclContext *dc, const clang::ObjCPropertyDecl *property,
35093442 const clang::ObjCMethodDecl *clangDecl, bool isFromSystemModule,
@@ -3529,11 +3462,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
35293462 if (!origDC)
35303463 return {Type (), false };
35313464
3532- auto fieldType = isGetter ? clangDecl-> getReturnType ()
3533- : clangDecl->getParamDecl ( 0 )-> getType ();
3534-
3465+ auto fieldType =
3466+ desugarIfElaborated (isGetter ? clangDecl->getReturnType ()
3467+ : clangDecl-> getParamDecl ( 0 )-> getType ());
35353468 // Import the property type, independent of what kind of accessor this is.
3536- ImportedType importedType = findOptionSetType (fieldType, *this );
3469+ ImportedType importedType = importer::findOptionSetEnum (fieldType, *this );
35373470 if (!importedType)
35383471 importedType = importPropertyType (property, isFromSystemModule);
35393472 if (!importedType)
0 commit comments