[ClangImporter] NFC: Convert CF{Un}retainedOutParaeter into ImportTypeAttr

This commit is contained in:
Pavel Yaskevich
2024-01-02 14:48:17 -08:00
parent 1e1f0a54ae
commit 41ef9b6f38
2 changed files with 36 additions and 38 deletions

View File

@@ -1283,8 +1283,6 @@ static bool canBridgeTypes(ImportTypeKind importKind) {
case ImportTypeKind::AuditedResult:
case ImportTypeKind::Parameter:
case ImportTypeKind::CompletionHandlerResultParameter:
case ImportTypeKind::CFRetainedOutParameter:
case ImportTypeKind::CFUnretainedOutParameter:
case ImportTypeKind::Property:
case ImportTypeKind::PropertyWithReferenceSemantics:
case ImportTypeKind::ObjCCollectionElement:
@@ -1311,8 +1309,6 @@ static bool isCFAudited(ImportTypeKind importKind) {
case ImportTypeKind::AuditedResult:
case ImportTypeKind::Parameter:
case ImportTypeKind::CompletionHandlerResultParameter:
case ImportTypeKind::CFRetainedOutParameter:
case ImportTypeKind::CFUnretainedOutParameter:
case ImportTypeKind::Property:
case ImportTypeKind::PropertyWithReferenceSemantics:
return true;
@@ -1388,7 +1384,7 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl,
static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
Type importedType,
ImportTypeKind importKind) {
ImportTypeAttrs attrs) {
PointerTypeKind PTK;
auto elementType = importedType->getAnyPointerElementType(PTK);
if (!elementType || PTK != PTK_UnsafeMutablePointer)
@@ -1414,7 +1410,7 @@ static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
resultTy = OptionalType::get(resultTy);
PointerTypeKind pointerKind;
if (importKind == ImportTypeKind::CFRetainedOutParameter)
if (attrs.contains(ImportTypeAttr::CFRetainedOutParameter))
pointerKind = PTK_UnsafeMutablePointer;
else
pointerKind = PTK_AutoreleasingUnsafeMutablePointer;
@@ -1569,8 +1565,16 @@ static ImportedType adjustTypeForConcreteImport(
break;
case ImportHint::OtherPointer:
// Special-case AutoreleasingUnsafeMutablePointer<NSError?> parameters.
if (importKind == ImportTypeKind::Parameter) {
// Remove 'Unmanaged' from audited CF out-parameters.
if (attrs.contains(ImportTypeAttr::CFRetainedOutParameter) ||
attrs.contains(ImportTypeAttr::CFUnretainedOutParameter)) {
if (Type outParamTy =
maybeImportCFOutParameter(impl, importedType, attrs)) {
importedType = outParamTy;
break;
}
} else if (importKind == ImportTypeKind::Parameter) {
// Special-case AutoreleasingUnsafeMutablePointer<NSError?> parameters.
if (Type result = maybeImportNSErrorOutParameter(impl, importedType,
resugarNSErrorPointer)) {
importedType = result;
@@ -1579,16 +1583,6 @@ static ImportedType adjustTypeForConcreteImport(
}
}
// Remove 'Unmanaged' from audited CF out-parameters.
if (importKind == ImportTypeKind::CFRetainedOutParameter ||
importKind == ImportTypeKind::CFUnretainedOutParameter) {
if (Type outParamTy = maybeImportCFOutParameter(impl, importedType,
importKind)) {
importedType = outParamTy;
break;
}
}
break;
}
@@ -2022,6 +2016,16 @@ ImportTypeAttrs swift::getImportTypeAttrs(const clang::Decl *D, bool isParam,
continue;
}
if (isa<clang::CFReturnsRetainedAttr>(attr)) {
attrs |= ImportTypeAttr::CFRetainedOutParameter;
continue;
}
if (isa<clang::CFReturnsNotRetainedAttr>(attr)) {
attrs |= ImportTypeAttr::CFUnretainedOutParameter;
continue;
}
auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(attr);
if (!swiftAttr)
continue;
@@ -2297,13 +2301,7 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
static ImportTypeKind
getImportTypeKindForParam(const clang::ParmVarDecl *param) {
ImportTypeKind importKind = ImportTypeKind::Parameter;
if (param->hasAttr<clang::CFReturnsRetainedAttr>())
importKind = ImportTypeKind::CFRetainedOutParameter;
else if (param->hasAttr<clang::CFReturnsNotRetainedAttr>())
importKind = ImportTypeKind::CFUnretainedOutParameter;
return importKind;
return ImportTypeKind::Parameter;
}
llvm::Optional<ClangImporter::Implementation::ImportParameterTypeResult>

View File

@@ -157,18 +157,6 @@ enum class ImportTypeKind {
/// * _Nullable_result is treated as _Nonnull rather than _Nullable_result.
CompletionHandlerResultParameter,
/// Import the type of a parameter declared with
/// \c CF_RETURNS_RETAINED.
///
/// This ensures that the parameter is not marked as Unmanaged.
CFRetainedOutParameter,
/// Import the type of a parameter declared with
/// \c CF_RETURNS_NON_RETAINED.
///
/// This ensures that the parameter is not marked as Unmanaged.
CFUnretainedOutParameter,
/// Import the type of an ObjC property.
///
/// This enables the conversion of bridged types. Properties are always
@@ -210,7 +198,19 @@ enum class ImportTypeAttr : uint8_t {
/// Type is in a declaration where it would be imported as Sendable by
/// default. This comes directly from the parameters to
/// \c getImportTypeAttrs() and merely affects diagnostics.
DefaultsToSendable = 1 << 3
DefaultsToSendable = 1 << 3,
/// Import the type of a parameter declared with
/// \c CF_RETURNS_RETAINED.
///
/// This ensures that the parameter is not marked as Unmanaged.
CFRetainedOutParameter = 1 << 4,
/// Import the type of a parameter declared with
/// \c CF_RETURNS_NON_RETAINED.
///
/// This ensures that the parameter is not marked as Unmanaged.
CFUnretainedOutParameter = 1 << 5,
};
/// Attributes which were set on the declaration and affect how its type is