Skip to content

Commit ea60a2b

Browse files
committed
[Clang importer] Factor out Clang parameter attributes mapping to types.
1 parent 81af954 commit ea60a2b

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,17 +1693,28 @@ ImportedType ClangImporter::Implementation::importPropertyType(
16931693
Bridgeability::Full, optionality);
16941694
}
16951695

1696-
/// Apply the @noescape attribute
1697-
static Type applyNoEscape(Type type) {
1696+
/// Apply an attribute to a function type.
1697+
static Type applyToFunctionType(
1698+
Type type, llvm::function_ref<Type(FunctionType *)> transform) {
16981699
// Recurse into optional types.
16991700
if (Type objectType = type->getOptionalObjectType()) {
1700-
return OptionalType::get(applyNoEscape(objectType));
1701+
return OptionalType::get(applyToFunctionType(objectType, transform));
17011702
}
17021703

17031704
// Apply @noescape to function types.
1704-
if (auto funcType = type->getAs<FunctionType>()) {
1705-
return FunctionType::get(funcType->getParams(), funcType->getResult(),
1706-
funcType->getExtInfo().withNoEscape());
1705+
if (auto funcType = type->getAs<FunctionType>())
1706+
return transform(funcType);
1707+
1708+
return type;
1709+
}
1710+
1711+
static Type applyParamAttributes(const clang::ParmVarDecl *param, Type type) {
1712+
// Map __attribute__((noescape)) to @noescape.
1713+
if (param->hasAttr<clang::NoEscapeAttr>()) {
1714+
type = applyToFunctionType(type, [](FunctionType *funcType) {
1715+
return FunctionType::get(funcType->getParams(), funcType->getResult(),
1716+
funcType->getExtInfo().withNoEscape());
1717+
});
17071718
}
17081719

17091720
return type;
@@ -1883,13 +1894,8 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
18831894
swiftParamTy = importedType.getType();
18841895
}
18851896

1886-
// Map __attribute__((noescape)) to @noescape.
1887-
if (param->hasAttr<clang::NoEscapeAttr>()) {
1888-
Type newParamTy = applyNoEscape(swiftParamTy);
1889-
if (newParamTy.getPointer() != swiftParamTy.getPointer()) {
1890-
swiftParamTy = newParamTy;
1891-
}
1892-
}
1897+
// Apply attributes to the type.
1898+
swiftParamTy = applyParamAttributes(param, swiftParamTy);
18931899

18941900
// Figure out the name for this parameter.
18951901
Identifier bodyName = importFullName(param, CurrentVersion)
@@ -2384,15 +2390,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
23842390
llvm_unreachable("async info computed incorrectly?");
23852391
}
23862392

2387-
// Map __attribute__((noescape)) to @noescape.
2388-
bool addNoEscapeAttr = false;
2389-
if (param->hasAttr<clang::NoEscapeAttr>()) {
2390-
Type newParamTy = applyNoEscape(swiftParamTy);
2391-
if (newParamTy.getPointer() != swiftParamTy.getPointer()) {
2392-
swiftParamTy = newParamTy;
2393-
addNoEscapeAttr = true;
2394-
}
2395-
}
2393+
// Apply Clang attributes to the parameter type.
2394+
swiftParamTy = applyParamAttributes(param, swiftParamTy);
23962395

23972396
// Figure out the name for this parameter.
23982397
Identifier bodyName = importFullName(param, CurrentVersion)

0 commit comments

Comments
 (0)