Skip to content

Commit a4341b4

Browse files
committed
[NFC] Extract getImportTypeKindForParam helper
The same piece of code was repeated in two different methods: `importFunctionParameterList` and `importMethodParamsAndReturnType`. To avoid repetition and simplify the later refactoring extract the code into a helper. The code in the two methods was happening in slightly different places, but the modified code should still be equivalent and should not introduce functional differences.
1 parent 77502b5 commit a4341b4

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,17 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
22302230
return {swiftResultTy, importedType.isImplicitlyUnwrapped()};
22312231
}
22322232

2233+
static ImportTypeKind
2234+
getImportTypeKindForParam(const clang::ParmVarDecl *param) {
2235+
ImportTypeKind importKind = ImportTypeKind::Parameter;
2236+
if (param->hasAttr<clang::CFReturnsRetainedAttr>())
2237+
importKind = ImportTypeKind::CFRetainedOutParameter;
2238+
else if (param->hasAttr<clang::CFReturnsNotRetainedAttr>())
2239+
importKind = ImportTypeKind::CFUnretainedOutParameter;
2240+
2241+
return importKind;
2242+
}
2243+
22332244
ParameterList *ClangImporter::Implementation::importFunctionParameterList(
22342245
DeclContext *dc, const clang::FunctionDecl *clangDecl,
22352246
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
@@ -2254,11 +2265,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
22542265
// Check nullability of the parameter.
22552266
OptionalTypeKind OptionalityOfParam = getParamOptionality(param, knownNonNull);
22562267

2257-
ImportTypeKind importKind = ImportTypeKind::Parameter;
2258-
if (param->hasAttr<clang::CFReturnsRetainedAttr>())
2259-
importKind = ImportTypeKind::CFRetainedOutParameter;
2260-
else if (param->hasAttr<clang::CFReturnsNotRetainedAttr>())
2261-
importKind = ImportTypeKind::CFUnretainedOutParameter;
2268+
ImportTypeKind importKind = getImportTypeKindForParam(param);
22622269

22632270
// Import the parameter type into Swift.
22642271
ImportDiagnosticAdder paramAddDiag(*this, clangDecl, param->getLocation());
@@ -2904,7 +2911,7 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
29042911
}
29052912

29062913
// Special case for NSDictionary's subscript.
2907-
ImportTypeKind importKind = ImportTypeKind::Parameter;
2914+
ImportTypeKind importKind = getImportTypeKindForParam(param);
29082915
ImportDiagnosticAdder paramAddDiag(*this, clangDecl, param->getLocation());
29092916
Type swiftParamTy;
29102917
bool paramIsIUO = false;
@@ -2934,11 +2941,6 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
29342941

29352942
paramIsIUO = optionalityOfParam == OTK_ImplicitlyUnwrappedOptional;
29362943
} else if (!swiftParamTy) {
2937-
if (param->hasAttr<clang::CFReturnsRetainedAttr>())
2938-
importKind = ImportTypeKind::CFRetainedOutParameter;
2939-
else if (param->hasAttr<clang::CFReturnsNotRetainedAttr>())
2940-
importKind = ImportTypeKind::CFUnretainedOutParameter;
2941-
29422944
// Figure out if this is a completion handler parameter whose error
29432945
// parameter is used to indicate throwing.
29442946
Optional<unsigned> completionHandlerErrorParamIndex;

0 commit comments

Comments
 (0)