1515// ===----------------------------------------------------------------------===//
1616
1717#include " CFTypeInfo.h"
18+ #include " ImportEnumInfo.h"
1819#include " ImporterImpl.h"
1920#include " SwiftDeclSynthesizer.h"
2021#include " swift/ABI/MetadataValues.h"
@@ -2282,10 +2283,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
22822283 OptionalityOfReturn = OTK_ImplicitlyUnwrappedOptional;
22832284 }
22842285
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 ());
22892287 // In C interop mode, the return type of library builtin functions
22902288 // like 'memcpy' from headers like 'string.h' drops
22912289 // any nullability specifiers from their return type, and preserves it on the
@@ -2323,19 +2321,9 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
23232321 ->isTemplateTypeParmType ())
23242322 OptionalityOfReturn = OTK_None;
23252323
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;
23392327
23402328 // Import the underlying result type.
23412329 if (clangDecl) {
@@ -2399,27 +2387,11 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23992387
24002388 // Only eagerly import the return type if it's not too expensive (the current
24012389 // heuristic for that is if it's not a record type).
2402- ImportedType importedType;
24032390 ImportDiagnosticAdder addDiag (*this , clangDecl,
24042391 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 );
24232395
24242396 if (auto templateType =
24252397 dyn_cast<clang::TemplateTypeParmType>(returnType)) {
@@ -2483,9 +2455,7 @@ ClangImporter::Implementation::importParameterType(
24832455 std::optional<unsigned > completionHandlerErrorParamIndex,
24842456 ArrayRef<GenericTypeParamDecl *> genericParams,
24852457 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 ());
24892459
24902460 ImportTypeKind importKind = paramIsCompletionHandler
24912461 ? ImportTypeKind::CompletionHandlerParameter
@@ -2498,23 +2468,8 @@ ClangImporter::Implementation::importParameterType(
24982468 bool isConsuming = false ;
24992469 bool isParamTypeImplicitlyUnwrapped = false ;
25002470
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 ();
25182473 } else if (isa<clang::PointerType>(paramTy) &&
25192474 isa<clang::TemplateTypeParmType>(paramTy->getPointeeType ())) {
25202475 auto pointeeType = paramTy->getPointeeType ();
@@ -2955,13 +2910,8 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
29552910 return argumentAttrs;
29562911 }
29572912 }
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;
29652915 }
29662916 }
29672917
@@ -3234,26 +3184,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
32343184
32353185 ImportDiagnosticAdder addImportDiag (*this , clangDecl,
32363186 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 );
32573189 if (!importedType)
32583190 importedType = importType (resultType, resultKind, addImportDiag,
32593191 allowNSUIntegerAsIntInResult, Bridgeability::Full,
@@ -3501,9 +3433,6 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
35013433 importedType.isImplicitlyUnwrapped ()};
35023434}
35033435
3504- ImportedType findOptionSetType (clang::QualType type,
3505- ClangImporter::Implementation &Impl);
3506-
35073436ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType (
35083437 const DeclContext *dc, const clang::ObjCPropertyDecl *property,
35093438 const clang::ObjCMethodDecl *clangDecl, bool isFromSystemModule,
@@ -3529,11 +3458,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
35293458 if (!origDC)
35303459 return {Type (), false };
35313460
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 ());
35353464 // 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 );
35373466 if (!importedType)
35383467 importedType = importPropertyType (property, isFromSystemModule);
35393468 if (!importedType)
0 commit comments