@@ -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) {
@@ -1065,22 +1088,27 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
1065
1088
os << " , readonly" ;
1066
1089
1067
1090
// Print the ownership semantics, if relevant.
1068
- // We treat "unowned" as "assign" (even though it's more like
1069
- // "safe_unretained") because we want people to think twice about
1070
- // allowing that object to disappear.
1071
1091
Type ty = VD->getInterfaceType ();
1072
- if (auto weakTy = ty->getAs <WeakStorageType>()) {
1073
- auto innerTy = weakTy->getReferentType ()->getOptionalObjectType ();
1074
- auto innerClass = innerTy->getClassOrBoundGenericClass ();
1075
- if ((innerClass &&
1076
- innerClass->getForeignClassKind ()!=ClassDecl::ForeignKind::CFType) ||
1077
- (innerTy->isObjCExistentialType () && !isCFTypeRef (innerTy))) {
1078
- 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 ;
1079
1111
}
1080
- } else if (ty->is <UnownedStorageType>()) {
1081
- os << " , assign" ;
1082
- } else if (ty->is <UnmanagedStorageType>()) {
1083
- os << " , unsafe_unretained" ;
1084
1112
} else {
1085
1113
Type copyTy = ty;
1086
1114
bool isOptional = false ;
@@ -1117,10 +1145,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
1117
1145
case FunctionTypeRepresentation::CFunctionPointer:
1118
1146
break ;
1119
1147
}
1120
- } else if ((nominal && isa<ClassDecl>(nominal) &&
1121
- cast<ClassDecl>(nominal)->getForeignClassKind () !=
1122
- ClassDecl::ForeignKind::CFType) ||
1123
- (copyTy->isObjCExistentialType () && !isCFTypeRef (copyTy))) {
1148
+ } else if (isObjCReferenceCountableObjectType (copyTy)) {
1124
1149
os << " , strong" ;
1125
1150
}
1126
1151
}
0 commit comments