@@ -2255,10 +2255,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
2255
2255
OptionalityOfReturn = OTK_ImplicitlyUnwrappedOptional;
2256
2256
}
2257
2257
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 ());
2262
2259
// In C interop mode, the return type of library builtin functions
2263
2260
// like 'memcpy' from headers like 'string.h' drops
2264
2261
// any nullability specifiers from their return type, and preserves it on the
@@ -2296,19 +2293,9 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
2296
2293
->isTemplateTypeParmType ())
2297
2294
OptionalityOfReturn = OTK_None;
2298
2295
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;
2312
2299
2313
2300
// Import the underlying result type.
2314
2301
if (clangDecl) {
@@ -2376,27 +2363,11 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
2376
2363
2377
2364
// Only eagerly import the return type if it's not too expensive (the current
2378
2365
// heuristic for that is if it's not a record type).
2379
- ImportedType importedType;
2380
2366
ImportDiagnosticAdder addDiag (*this , clangDecl,
2381
2367
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 );
2400
2371
2401
2372
if (auto templateType =
2402
2373
dyn_cast<clang::TemplateTypeParmType>(returnType)) {
@@ -2460,9 +2431,7 @@ ClangImporter::Implementation::importParameterType(
2460
2431
std::optional<unsigned > completionHandlerErrorParamIndex,
2461
2432
ArrayRef<GenericTypeParamDecl *> genericParams,
2462
2433
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 ());
2466
2435
2467
2436
ImportTypeKind importKind = paramIsCompletionHandler
2468
2437
? ImportTypeKind::CompletionHandlerParameter
@@ -2475,23 +2444,8 @@ ClangImporter::Implementation::importParameterType(
2475
2444
bool isConsuming = false ;
2476
2445
bool isParamTypeImplicitlyUnwrapped = false ;
2477
2446
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 ();
2495
2449
} else if (isa<clang::PointerType>(paramTy) &&
2496
2450
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType ())) {
2497
2451
auto pointeeType = paramTy->getPointeeType ();
@@ -3211,26 +3165,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
3211
3165
3212
3166
ImportDiagnosticAdder addImportDiag (*this , clangDecl,
3213
3167
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 );
3234
3170
if (!importedType)
3235
3171
importedType = importType (resultType, resultKind, addImportDiag,
3236
3172
allowNSUIntegerAsIntInResult, Bridgeability::Full,
@@ -3478,9 +3414,6 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
3478
3414
importedType.isImplicitlyUnwrapped ()};
3479
3415
}
3480
3416
3481
- ImportedType findOptionSetType (clang::QualType type,
3482
- ClangImporter::Implementation &Impl);
3483
-
3484
3417
ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType (
3485
3418
const DeclContext *dc, const clang::ObjCPropertyDecl *property,
3486
3419
const clang::ObjCMethodDecl *clangDecl, bool isFromSystemModule,
@@ -3506,11 +3439,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
3506
3439
if (!origDC)
3507
3440
return {Type (), false };
3508
3441
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 ());
3512
3445
// 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 );
3514
3447
if (!importedType)
3515
3448
importedType = importPropertyType (property, isFromSystemModule);
3516
3449
if (!importedType)
0 commit comments