Skip to content

Commit 41ef9b6

Browse files
committed
[ClangImporter] NFC: Convert CF{Un}retainedOutParaeter into ImportTypeAttr
1 parent 1e1f0a5 commit 41ef9b6

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,8 +1283,6 @@ static bool canBridgeTypes(ImportTypeKind importKind) {
12831283
case ImportTypeKind::AuditedResult:
12841284
case ImportTypeKind::Parameter:
12851285
case ImportTypeKind::CompletionHandlerResultParameter:
1286-
case ImportTypeKind::CFRetainedOutParameter:
1287-
case ImportTypeKind::CFUnretainedOutParameter:
12881286
case ImportTypeKind::Property:
12891287
case ImportTypeKind::PropertyWithReferenceSemantics:
12901288
case ImportTypeKind::ObjCCollectionElement:
@@ -1311,8 +1309,6 @@ static bool isCFAudited(ImportTypeKind importKind) {
13111309
case ImportTypeKind::AuditedResult:
13121310
case ImportTypeKind::Parameter:
13131311
case ImportTypeKind::CompletionHandlerResultParameter:
1314-
case ImportTypeKind::CFRetainedOutParameter:
1315-
case ImportTypeKind::CFUnretainedOutParameter:
13161312
case ImportTypeKind::Property:
13171313
case ImportTypeKind::PropertyWithReferenceSemantics:
13181314
return true;
@@ -1388,7 +1384,7 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl,
13881384

13891385
static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
13901386
Type importedType,
1391-
ImportTypeKind importKind) {
1387+
ImportTypeAttrs attrs) {
13921388
PointerTypeKind PTK;
13931389
auto elementType = importedType->getAnyPointerElementType(PTK);
13941390
if (!elementType || PTK != PTK_UnsafeMutablePointer)
@@ -1414,7 +1410,7 @@ static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
14141410
resultTy = OptionalType::get(resultTy);
14151411

14161412
PointerTypeKind pointerKind;
1417-
if (importKind == ImportTypeKind::CFRetainedOutParameter)
1413+
if (attrs.contains(ImportTypeAttr::CFRetainedOutParameter))
14181414
pointerKind = PTK_UnsafeMutablePointer;
14191415
else
14201416
pointerKind = PTK_AutoreleasingUnsafeMutablePointer;
@@ -1569,8 +1565,16 @@ static ImportedType adjustTypeForConcreteImport(
15691565
break;
15701566

15711567
case ImportHint::OtherPointer:
1572-
// Special-case AutoreleasingUnsafeMutablePointer<NSError?> parameters.
1573-
if (importKind == ImportTypeKind::Parameter) {
1568+
// Remove 'Unmanaged' from audited CF out-parameters.
1569+
if (attrs.contains(ImportTypeAttr::CFRetainedOutParameter) ||
1570+
attrs.contains(ImportTypeAttr::CFUnretainedOutParameter)) {
1571+
if (Type outParamTy =
1572+
maybeImportCFOutParameter(impl, importedType, attrs)) {
1573+
importedType = outParamTy;
1574+
break;
1575+
}
1576+
} else if (importKind == ImportTypeKind::Parameter) {
1577+
// Special-case AutoreleasingUnsafeMutablePointer<NSError?> parameters.
15741578
if (Type result = maybeImportNSErrorOutParameter(impl, importedType,
15751579
resugarNSErrorPointer)) {
15761580
importedType = result;
@@ -1579,16 +1583,6 @@ static ImportedType adjustTypeForConcreteImport(
15791583
}
15801584
}
15811585

1582-
// Remove 'Unmanaged' from audited CF out-parameters.
1583-
if (importKind == ImportTypeKind::CFRetainedOutParameter ||
1584-
importKind == ImportTypeKind::CFUnretainedOutParameter) {
1585-
if (Type outParamTy = maybeImportCFOutParameter(impl, importedType,
1586-
importKind)) {
1587-
importedType = outParamTy;
1588-
break;
1589-
}
1590-
}
1591-
15921586
break;
15931587
}
15941588

@@ -2022,6 +2016,16 @@ ImportTypeAttrs swift::getImportTypeAttrs(const clang::Decl *D, bool isParam,
20222016
continue;
20232017
}
20242018

2019+
if (isa<clang::CFReturnsRetainedAttr>(attr)) {
2020+
attrs |= ImportTypeAttr::CFRetainedOutParameter;
2021+
continue;
2022+
}
2023+
2024+
if (isa<clang::CFReturnsNotRetainedAttr>(attr)) {
2025+
attrs |= ImportTypeAttr::CFUnretainedOutParameter;
2026+
continue;
2027+
}
2028+
20252029
auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(attr);
20262030
if (!swiftAttr)
20272031
continue;
@@ -2297,13 +2301,7 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
22972301

22982302
static ImportTypeKind
22992303
getImportTypeKindForParam(const clang::ParmVarDecl *param) {
2300-
ImportTypeKind importKind = ImportTypeKind::Parameter;
2301-
if (param->hasAttr<clang::CFReturnsRetainedAttr>())
2302-
importKind = ImportTypeKind::CFRetainedOutParameter;
2303-
else if (param->hasAttr<clang::CFReturnsNotRetainedAttr>())
2304-
importKind = ImportTypeKind::CFUnretainedOutParameter;
2305-
2306-
return importKind;
2304+
return ImportTypeKind::Parameter;
23072305
}
23082306

23092307
llvm::Optional<ClangImporter::Implementation::ImportParameterTypeResult>

lib/ClangImporter/ImporterImpl.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,6 @@ enum class ImportTypeKind {
157157
/// * _Nullable_result is treated as _Nonnull rather than _Nullable_result.
158158
CompletionHandlerResultParameter,
159159

160-
/// Import the type of a parameter declared with
161-
/// \c CF_RETURNS_RETAINED.
162-
///
163-
/// This ensures that the parameter is not marked as Unmanaged.
164-
CFRetainedOutParameter,
165-
166-
/// Import the type of a parameter declared with
167-
/// \c CF_RETURNS_NON_RETAINED.
168-
///
169-
/// This ensures that the parameter is not marked as Unmanaged.
170-
CFUnretainedOutParameter,
171-
172160
/// Import the type of an ObjC property.
173161
///
174162
/// This enables the conversion of bridged types. Properties are always
@@ -210,7 +198,19 @@ enum class ImportTypeAttr : uint8_t {
210198
/// Type is in a declaration where it would be imported as Sendable by
211199
/// default. This comes directly from the parameters to
212200
/// \c getImportTypeAttrs() and merely affects diagnostics.
213-
DefaultsToSendable = 1 << 3
201+
DefaultsToSendable = 1 << 3,
202+
203+
/// Import the type of a parameter declared with
204+
/// \c CF_RETURNS_RETAINED.
205+
///
206+
/// This ensures that the parameter is not marked as Unmanaged.
207+
CFRetainedOutParameter = 1 << 4,
208+
209+
/// Import the type of a parameter declared with
210+
/// \c CF_RETURNS_NON_RETAINED.
211+
///
212+
/// This ensures that the parameter is not marked as Unmanaged.
213+
CFUnretainedOutParameter = 1 << 5,
214214
};
215215

216216
/// Attributes which were set on the declaration and affect how its type is

0 commit comments

Comments
 (0)