Skip to content

Commit 0f3f460

Browse files
committed
[NFC] Extract getParameterInfo helper
The methods `importFunctionParameterList` and `importMethodParamsAndReturnType` share a similar piece of code to create the parameter information from the results of finding the parameter type. In order to simplify the later refactor and to make the changes clearer, extract the common code into a helper function and call it from both methods, replacing the duplicated code.
1 parent a4341b4 commit 0f3f460

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,34 @@ getImportTypeKindForParam(const clang::ParmVarDecl *param) {
22412241
return importKind;
22422242
}
22432243

2244+
static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
2245+
const clang::ParmVarDecl *param,
2246+
const Identifier &name,
2247+
const swift::Type &swiftParamTy,
2248+
const bool isInOut,
2249+
const bool isParamTypeImplicitlyUnwrapped) {
2250+
// Figure out the name for this parameter.
2251+
Identifier bodyName = impl->importFullName(param, impl->CurrentVersion)
2252+
.getDeclName()
2253+
.getBaseIdentifier();
2254+
2255+
// It doesn't actually matter which DeclContext we use, so just use the
2256+
// imported header unit.
2257+
auto paramInfo = impl->createDeclWithClangNode<ParamDecl>(
2258+
param, AccessLevel::Private, SourceLoc(), SourceLoc(), name,
2259+
impl->importSourceLoc(param->getLocation()), bodyName,
2260+
impl->ImportedHeaderUnit);
2261+
// Foreign references are already references so they don't need to be passed
2262+
// as inout.
2263+
paramInfo->setSpecifier(isInOut && !swiftParamTy->isForeignReferenceType()
2264+
? ParamSpecifier::InOut
2265+
: ParamSpecifier::Default);
2266+
paramInfo->setInterfaceType(swiftParamTy);
2267+
impl->recordImplicitUnwrapForDecl(paramInfo, isParamTypeImplicitlyUnwrapped);
2268+
2269+
return paramInfo;
2270+
}
2271+
22442272
ParameterList *ClangImporter::Implementation::importFunctionParameterList(
22452273
DeclContext *dc, const clang::FunctionDecl *clangDecl,
22462274
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
@@ -2344,29 +2372,13 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
23442372
swiftParamTy = importedType.getType();
23452373
}
23462374

2347-
// Figure out the name for this parameter.
2348-
Identifier bodyName = importFullName(param, CurrentVersion)
2349-
.getDeclName()
2350-
.getBaseIdentifier();
2351-
23522375
// Retrieve the argument name.
23532376
Identifier name;
23542377
if (index < argNames.size())
23552378
name = argNames[index];
23562379

2357-
// It doesn't actually matter which DeclContext we use, so just use the
2358-
// imported header unit.
2359-
auto paramInfo = createDeclWithClangNode<ParamDecl>(
2360-
param, AccessLevel::Private, SourceLoc(), SourceLoc(), name,
2361-
importSourceLoc(param->getLocation()), bodyName,
2362-
ImportedHeaderUnit);
2363-
// Foreign references are already references so they don't need to be passed
2364-
// as inout.
2365-
paramInfo->setSpecifier(isInOut && !swiftParamTy->isForeignReferenceType()
2366-
? ParamSpecifier::InOut
2367-
: ParamSpecifier::Default);
2368-
paramInfo->setInterfaceType(swiftParamTy);
2369-
recordImplicitUnwrapForDecl(paramInfo, isParamTypeImplicitlyUnwrapped);
2380+
auto paramInfo = getParameterInfo(this, param, name, swiftParamTy, isInOut,
2381+
isParamTypeImplicitlyUnwrapped);
23702382
parameters.push_back(paramInfo);
23712383
++index;
23722384
}
@@ -3013,28 +3025,16 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
30133025
llvm_unreachable("async info computed incorrectly?");
30143026
}
30153027

3016-
// Figure out the name for this parameter.
3017-
Identifier bodyName = importFullName(param, CurrentVersion)
3018-
.getDeclName()
3019-
.getBaseIdentifier();
3020-
30213028
// Figure out the name for this argument, which comes from the method name.
30223029
Identifier name;
30233030
if (nameIndex < argNames.size()) {
30243031
name = argNames[nameIndex];
30253032
}
30263033
++nameIndex;
30273034

3028-
// Set up the parameter info.
3029-
auto paramInfo
3030-
= createDeclWithClangNode<ParamDecl>(param, AccessLevel::Private,
3031-
SourceLoc(), SourceLoc(), name,
3032-
importSourceLoc(param->getLocation()),
3033-
bodyName,
3034-
ImportedHeaderUnit);
3035-
paramInfo->setSpecifier(ParamSpecifier::Default);
3036-
paramInfo->setInterfaceType(swiftParamTy);
3037-
recordImplicitUnwrapForDecl(paramInfo, paramIsIUO);
3035+
// Set up the parameter info
3036+
auto paramInfo = getParameterInfo(this, param, name, swiftParamTy,
3037+
/*isInOut=*/false, paramIsIUO);
30383038

30393039
// Determine whether we have a default argument.
30403040
if (kind == SpecialMethodKind::Regular ||

0 commit comments

Comments
 (0)