15
15
// ===----------------------------------------------------------------------===//
16
16
17
17
#include " CFTypeInfo.h"
18
+ #include " ImportEnumInfo.h"
18
19
#include " ImporterImpl.h"
19
20
#include " SwiftDeclSynthesizer.h"
20
21
#include " swift/ABI/MetadataValues.h"
@@ -2282,10 +2283,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
2282
2283
OptionalityOfReturn = OTK_ImplicitlyUnwrappedOptional;
2283
2284
}
2284
2285
2285
- clang::QualType returnType = clangDecl->getReturnType ();
2286
- if (auto elaborated =
2287
- dyn_cast<clang::ElaboratedType>(returnType))
2288
- returnType = elaborated->desugar ();
2286
+ clang::QualType returnType = desugarIfElaborated (clangDecl->getReturnType ());
2289
2287
// In C interop mode, the return type of library builtin functions
2290
2288
// like 'memcpy' from headers like 'string.h' drops
2291
2289
// any nullability specifiers from their return type, and preserves it on the
@@ -2323,19 +2321,9 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
2323
2321
->isTemplateTypeParmType ())
2324
2322
OptionalityOfReturn = OTK_None;
2325
2323
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
- }
2324
+ ImportedType optionSetEnum = importer::findOptionSetEnum (returnType, *this );
2325
+ if (optionSetEnum)
2326
+ return optionSetEnum;
2339
2327
2340
2328
// Import the underlying result type.
2341
2329
if (clangDecl) {
@@ -2399,27 +2387,11 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
2399
2387
2400
2388
// Only eagerly import the return type if it's not too expensive (the current
2401
2389
// heuristic for that is if it's not a record type).
2402
- ImportedType importedType;
2403
2390
ImportDiagnosticAdder addDiag (*this , clangDecl,
2404
2391
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
- }
2392
+ clang::QualType returnType = desugarIfElaborated (clangDecl->getReturnType ());
2393
+
2394
+ ImportedType importedType = importer::findOptionSetEnum (returnType, *this );
2423
2395
2424
2396
if (auto templateType =
2425
2397
dyn_cast<clang::TemplateTypeParmType>(returnType)) {
@@ -2483,9 +2455,7 @@ ClangImporter::Implementation::importParameterType(
2483
2455
std::optional<unsigned > completionHandlerErrorParamIndex,
2484
2456
ArrayRef<GenericTypeParamDecl *> genericParams,
2485
2457
llvm::function_ref<void (Diagnostic &&)> addImportDiagnosticFn) {
2486
- auto paramTy = param->getType ();
2487
- if (auto elaborated = dyn_cast<clang::ElaboratedType>(paramTy))
2488
- paramTy = elaborated->desugar ();
2458
+ auto paramTy = desugarIfElaborated (param->getType ());
2489
2459
2490
2460
ImportTypeKind importKind = paramIsCompletionHandler
2491
2461
? ImportTypeKind::CompletionHandlerParameter
@@ -2498,23 +2468,8 @@ ClangImporter::Implementation::importParameterType(
2498
2468
bool isConsuming = false ;
2499
2469
bool isParamTypeImplicitlyUnwrapped = false ;
2500
2470
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
- }
2471
+ if (auto optionSetEnum = importer::findOptionSetEnum (paramTy, *this )) {
2472
+ swiftParamTy = optionSetEnum.getType ();
2518
2473
} else if (isa<clang::PointerType>(paramTy) &&
2519
2474
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType ())) {
2520
2475
auto pointeeType = paramTy->getPointeeType ();
@@ -2955,13 +2910,8 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
2955
2910
return argumentAttrs;
2956
2911
}
2957
2912
}
2958
- auto loc = typedefDecl->getEndLoc ();
2959
- if (loc.isMacroID ()) {
2960
- StringRef macroName =
2961
- nameImporter.getClangPreprocessor ().getImmediateMacroName (loc);
2962
- if (isCFOptionsMacro (macroName))
2963
- return argumentAttrs;
2964
- }
2913
+ if (isCFOptionsMacro (typedefDecl, nameImporter.getClangPreprocessor ()))
2914
+ return argumentAttrs;
2965
2915
}
2966
2916
}
2967
2917
@@ -3234,26 +3184,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
3234
3184
3235
3185
ImportDiagnosticAdder addImportDiag (*this , clangDecl,
3236
3186
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
-
3187
+ clang::QualType resultType = desugarIfElaborated (clangDecl->getReturnType ());
3188
+ ImportedType importedType = importer::findOptionSetEnum (resultType, *this );
3257
3189
if (!importedType)
3258
3190
importedType = importType (resultType, resultKind, addImportDiag,
3259
3191
allowNSUIntegerAsIntInResult, Bridgeability::Full,
@@ -3501,9 +3433,6 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
3501
3433
importedType.isImplicitlyUnwrapped ()};
3502
3434
}
3503
3435
3504
- ImportedType findOptionSetType (clang::QualType type,
3505
- ClangImporter::Implementation &Impl);
3506
-
3507
3436
ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType (
3508
3437
const DeclContext *dc, const clang::ObjCPropertyDecl *property,
3509
3438
const clang::ObjCMethodDecl *clangDecl, bool isFromSystemModule,
@@ -3529,11 +3458,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
3529
3458
if (!origDC)
3530
3459
return {Type (), false };
3531
3460
3532
- auto fieldType = isGetter ? clangDecl-> getReturnType ()
3533
- : clangDecl->getParamDecl ( 0 )-> getType ();
3534
-
3461
+ auto fieldType =
3462
+ desugarIfElaborated (isGetter ? clangDecl->getReturnType ()
3463
+ : clangDecl-> getParamDecl ( 0 )-> getType ());
3535
3464
// Import the property type, independent of what kind of accessor this is.
3536
- ImportedType importedType = findOptionSetType (fieldType, *this );
3465
+ ImportedType importedType = importer::findOptionSetEnum (fieldType, *this );
3537
3466
if (!importedType)
3538
3467
importedType = importPropertyType (property, isFromSystemModule);
3539
3468
if (!importedType)
0 commit comments