@@ -1027,17 +1027,40 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
1027
1027
return true ;
1028
1028
}
1029
1029
1030
+ // / Returns whether \p ty is the C type \c CFTypeRef, or some typealias
1031
+ // / thereof.
1030
1032
bool isCFTypeRef (Type ty) {
1031
- if (ID_CFTypeRef.empty ())
1032
- ID_CFTypeRef = M.getASTContext ().getIdentifier (" CFTypeRef" );
1033
-
1034
1033
const TypeAliasDecl *TAD = nullptr ;
1035
1034
while (auto aliasTy = dyn_cast<NameAliasType>(ty.getPointer ())) {
1036
1035
TAD = aliasTy->getDecl ();
1037
1036
ty = aliasTy->getSinglyDesugaredType ();
1038
1037
}
1039
1038
1040
- return TAD && TAD->getName () == ID_CFTypeRef && TAD->hasClangNode ();
1039
+ if (!TAD || !TAD->hasClangNode ())
1040
+ return false ;
1041
+
1042
+ if (ID_CFTypeRef.empty ())
1043
+ ID_CFTypeRef = M.getASTContext ().getIdentifier (" CFTypeRef" );
1044
+ return TAD->getName () == ID_CFTypeRef;
1045
+ }
1046
+
1047
+ // / Returns true if \p ty can be used with Objective-C reference-counting
1048
+ // / annotations like \c strong and \c weak.
1049
+ bool isObjCReferenceCountableObjectType (Type ty) {
1050
+ if (auto classDecl = ty->getClassOrBoundGenericClass ()) {
1051
+ switch (classDecl->getForeignClassKind ()) {
1052
+ case ClassDecl::ForeignKind::Normal:
1053
+ case ClassDecl::ForeignKind::RuntimeOnly:
1054
+ return true ;
1055
+ case ClassDecl::ForeignKind::CFType:
1056
+ return false ;
1057
+ }
1058
+ }
1059
+
1060
+ if (ty->isObjCExistentialType () && !isCFTypeRef (ty))
1061
+ return true ;
1062
+
1063
+ return false ;
1041
1064
}
1042
1065
1043
1066
void visitVarDecl (VarDecl *VD) {
@@ -1066,20 +1089,26 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
1066
1089
1067
1090
// Print the ownership semantics, if relevant.
1068
1091
Type ty = VD->getInterfaceType ();
1069
- if (auto weakTy = ty->getAs <WeakStorageType>()) {
1070
- auto innerTy = weakTy->getReferentType ()->getOptionalObjectType ();
1071
- auto innerClass = innerTy->getClassOrBoundGenericClass ();
1072
- if ((innerClass &&
1073
- innerClass->getForeignClassKind ()!=ClassDecl::ForeignKind::CFType) ||
1074
- (innerTy->isObjCExistentialType () && !isCFTypeRef (innerTy))) {
1075
- os << " , weak" ;
1092
+ if (auto referenceStorageTy = ty->getAs <ReferenceStorageType>()) {
1093
+ switch (referenceStorageTy->getOwnership ()) {
1094
+ case ReferenceOwnership::Strong:
1095
+ llvm_unreachable (" not represented with a ReferenceStorageType" );
1096
+ case ReferenceOwnership::Weak: {
1097
+ auto innerTy =
1098
+ referenceStorageTy->getReferentType ()->getOptionalObjectType ();
1099
+ if (isObjCReferenceCountableObjectType (innerTy))
1100
+ os << " , weak" ;
1101
+ break ;
1102
+ }
1103
+ case ReferenceOwnership::Unowned:
1104
+ case ReferenceOwnership::Unmanaged:
1105
+ // We treat "unowned" as "unsafe_unretained" (even though it's more
1106
+ // like "safe_unretained") because we want people to think twice about
1107
+ // allowing that object to disappear. "unowned(unsafe)" (and
1108
+ // Swift.Unmanaged, handled below) really are "unsafe_unretained".
1109
+ os << " , unsafe_unretained" ;
1110
+ break ;
1076
1111
}
1077
- } else if (ty->is <UnownedStorageType>()|| ty->is <UnmanagedStorageType>()) {
1078
- // We treat "unowned" as "unsafe_unretained" (even though it's more like
1079
- // "safe_unretained") because we want people to think twice about
1080
- // allowing that object to disappear. "unowned(unsafe)" (and Unmanaged,
1081
- // handled below) really are "unsafe_unretained".
1082
- os << " , unsafe_unretained" ;
1083
1112
} else {
1084
1113
Type copyTy = ty;
1085
1114
bool isOptional = false ;
@@ -1116,10 +1145,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
1116
1145
case FunctionTypeRepresentation::CFunctionPointer:
1117
1146
break ;
1118
1147
}
1119
- } else if ((nominal && isa<ClassDecl>(nominal) &&
1120
- cast<ClassDecl>(nominal)->getForeignClassKind () !=
1121
- ClassDecl::ForeignKind::CFType) ||
1122
- (copyTy->isObjCExistentialType () && !isCFTypeRef (copyTy))) {
1148
+ } else if (isObjCReferenceCountableObjectType (copyTy)) {
1123
1149
os << " , strong" ;
1124
1150
}
1125
1151
}
0 commit comments