@@ -2282,10 +2282,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
2282
2282
OptionalityOfReturn = OTK_ImplicitlyUnwrappedOptional;
2283
2283
}
2284
2284
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 ());
2289
2286
// In C interop mode, the return type of library builtin functions
2290
2287
// like 'memcpy' from headers like 'string.h' drops
2291
2288
// any nullability specifiers from their return type, and preserves it on the
@@ -2323,19 +2320,9 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
2323
2320
->isTemplateTypeParmType ())
2324
2321
OptionalityOfReturn = OTK_None;
2325
2322
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;
2339
2326
2340
2327
// Import the underlying result type.
2341
2328
if (clangDecl) {
@@ -2399,27 +2386,11 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
2399
2386
2400
2387
// Only eagerly import the return type if it's not too expensive (the current
2401
2388
// heuristic for that is if it's not a record type).
2402
- ImportedType importedType;
2403
2389
ImportDiagnosticAdder addDiag (*this , clangDecl,
2404
2390
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 );
2423
2394
2424
2395
if (auto templateType =
2425
2396
dyn_cast<clang::TemplateTypeParmType>(returnType)) {
@@ -2483,9 +2454,7 @@ ClangImporter::Implementation::importParameterType(
2483
2454
std::optional<unsigned > completionHandlerErrorParamIndex,
2484
2455
ArrayRef<GenericTypeParamDecl *> genericParams,
2485
2456
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 ());
2489
2458
2490
2459
ImportTypeKind importKind = paramIsCompletionHandler
2491
2460
? ImportTypeKind::CompletionHandlerParameter
@@ -2498,23 +2467,8 @@ ClangImporter::Implementation::importParameterType(
2498
2467
bool isConsuming = false ;
2499
2468
bool isParamTypeImplicitlyUnwrapped = false ;
2500
2469
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 ();
2518
2472
} else if (isa<clang::PointerType>(paramTy) &&
2519
2473
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType ())) {
2520
2474
auto pointeeType = paramTy->getPointeeType ();
@@ -3234,26 +3188,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
3234
3188
3235
3189
ImportDiagnosticAdder addImportDiag (*this , clangDecl,
3236
3190
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 );
3257
3193
if (!importedType)
3258
3194
importedType = importType (resultType, resultKind, addImportDiag,
3259
3195
allowNSUIntegerAsIntInResult, Bridgeability::Full,
@@ -3501,9 +3437,6 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
3501
3437
importedType.isImplicitlyUnwrapped ()};
3502
3438
}
3503
3439
3504
- ImportedType findOptionSetType (clang::QualType type,
3505
- ClangImporter::Implementation &Impl);
3506
-
3507
3440
ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType (
3508
3441
const DeclContext *dc, const clang::ObjCPropertyDecl *property,
3509
3442
const clang::ObjCMethodDecl *clangDecl, bool isFromSystemModule,
@@ -3529,11 +3462,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
3529
3462
if (!origDC)
3530
3463
return {Type (), false };
3531
3464
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 ());
3535
3468
// 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 );
3537
3470
if (!importedType)
3538
3471
importedType = importPropertyType (property, isFromSystemModule);
3539
3472
if (!importedType)
0 commit comments