Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1 | //===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective C @property and |
| 11 | // @synthesize declarations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Initialization.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseSet.h" |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | // Grammar actions. |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 27 | /// getImpliedARCOwnership - Given a set of property attributes and a |
| 28 | /// type, infer an expected lifetime. The type's ownership qualification |
| 29 | /// is not considered. |
| 30 | /// |
| 31 | /// Returns OCL_None if the attributes as stated do not imply an ownership. |
| 32 | /// Never returns OCL_Autoreleasing. |
| 33 | static Qualifiers::ObjCLifetime getImpliedARCOwnership( |
| 34 | ObjCPropertyDecl::PropertyAttributeKind attrs, |
| 35 | QualType type) { |
| 36 | // retain, strong, copy, weak, and unsafe_unretained are only legal |
| 37 | // on properties of retainable pointer type. |
| 38 | if (attrs & (ObjCPropertyDecl::OBJC_PR_retain | |
| 39 | ObjCPropertyDecl::OBJC_PR_strong | |
| 40 | ObjCPropertyDecl::OBJC_PR_copy)) { |
| 41 | return Qualifiers::OCL_Strong; |
| 42 | } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) { |
| 43 | return Qualifiers::OCL_Weak; |
| 44 | } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) { |
| 45 | return Qualifiers::OCL_ExplicitNone; |
| 46 | } |
| 47 | |
| 48 | // assign can appear on other types, so we have to check the |
| 49 | // property type. |
| 50 | if (attrs & ObjCPropertyDecl::OBJC_PR_assign && |
| 51 | type->isObjCRetainableType()) { |
| 52 | return Qualifiers::OCL_ExplicitNone; |
| 53 | } |
| 54 | |
| 55 | return Qualifiers::OCL_None; |
| 56 | } |
| 57 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 58 | /// Check the internal consistency of a property declaration. |
| 59 | static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) { |
| 60 | if (property->isInvalidDecl()) return; |
| 61 | |
| 62 | ObjCPropertyDecl::PropertyAttributeKind propertyKind |
| 63 | = property->getPropertyAttributes(); |
| 64 | Qualifiers::ObjCLifetime propertyLifetime |
| 65 | = property->getType().getObjCLifetime(); |
| 66 | |
| 67 | // Nothing to do if we don't have a lifetime. |
| 68 | if (propertyLifetime == Qualifiers::OCL_None) return; |
| 69 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 70 | Qualifiers::ObjCLifetime expectedLifetime |
| 71 | = getImpliedARCOwnership(propertyKind, property->getType()); |
| 72 | if (!expectedLifetime) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 73 | // We have a lifetime qualifier but no dominating property |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 74 | // attribute. That's okay, but restore reasonable invariants by |
| 75 | // setting the property attribute according to the lifetime |
| 76 | // qualifier. |
| 77 | ObjCPropertyDecl::PropertyAttributeKind attr; |
| 78 | if (propertyLifetime == Qualifiers::OCL_Strong) { |
| 79 | attr = ObjCPropertyDecl::OBJC_PR_strong; |
| 80 | } else if (propertyLifetime == Qualifiers::OCL_Weak) { |
| 81 | attr = ObjCPropertyDecl::OBJC_PR_weak; |
| 82 | } else { |
| 83 | assert(propertyLifetime == Qualifiers::OCL_ExplicitNone); |
| 84 | attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained; |
| 85 | } |
| 86 | property->setPropertyAttributes(attr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
| 90 | if (propertyLifetime == expectedLifetime) return; |
| 91 | |
| 92 | property->setInvalidDecl(); |
| 93 | S.Diag(property->getLocation(), |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 94 | diag::err_arc_inconsistent_property_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 95 | << property->getDeclName() |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 96 | << expectedLifetime |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 97 | << propertyLifetime; |
| 98 | } |
| 99 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 100 | Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
| 101 | FieldDeclarator &FD, |
| 102 | ObjCDeclSpec &ODS, |
| 103 | Selector GetterSel, |
| 104 | Selector SetterSel, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 105 | bool *isOverridingProperty, |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 106 | tok::ObjCKeywordKind MethodImplKind, |
| 107 | DeclContext *lexicalDC) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 108 | unsigned Attributes = ODS.getPropertyAttributes(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 109 | TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S); |
| 110 | QualType T = TSI->getType(); |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 111 | if ((getLangOptions().getGC() != LangOptions::NonGC && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 112 | T.isObjCGCWeak()) || |
| 113 | (getLangOptions().ObjCAutoRefCount && |
| 114 | T.getObjCLifetime() == Qualifiers::OCL_Weak)) |
| 115 | Attributes |= ObjCDeclSpec::DQ_PR_weak; |
| 116 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 117 | bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || |
| 118 | // default is readwrite! |
| 119 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); |
| 120 | // property is defaulted to 'assign' if it is readwrite and is |
| 121 | // not retain or copy |
| 122 | bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || |
| 123 | (isReadWrite && |
| 124 | !(Attributes & ObjCDeclSpec::DQ_PR_retain) && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 125 | !(Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 126 | !(Attributes & ObjCDeclSpec::DQ_PR_copy) && |
| 127 | !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) && |
| 128 | !(Attributes & ObjCDeclSpec::DQ_PR_weak))); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 130 | // Proceed with constructing the ObjCPropertDecls. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 131 | ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 133 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 134 | if (CDecl->IsClassExtension()) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 135 | Decl *Res = HandlePropertyInClassExtension(S, AtLoc, |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 136 | FD, GetterSel, SetterSel, |
| 137 | isAssign, isReadWrite, |
| 138 | Attributes, |
| 139 | isOverridingProperty, TSI, |
| 140 | MethodImplKind); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 141 | if (Res) { |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 142 | CheckObjCPropertyAttributes(Res, AtLoc, Attributes); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 143 | if (getLangOptions().ObjCAutoRefCount) |
| 144 | checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res)); |
| 145 | } |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 146 | return Res; |
| 147 | } |
| 148 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 149 | ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD, |
| 150 | GetterSel, SetterSel, |
| 151 | isAssign, isReadWrite, |
| 152 | Attributes, TSI, MethodImplKind); |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 153 | if (lexicalDC) |
| 154 | Res->setLexicalDeclContext(lexicalDC); |
| 155 | |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 156 | // Validate the attributes on the @property. |
| 157 | CheckObjCPropertyAttributes(Res, AtLoc, Attributes); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 158 | |
| 159 | if (getLangOptions().ObjCAutoRefCount) |
| 160 | checkARCPropertyDecl(*this, Res); |
| 161 | |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 162 | return Res; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 163 | } |
Ted Kremenek | 2d2f936 | 2010-03-12 00:49:00 +0000 | [diff] [blame] | 164 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 165 | Decl * |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 166 | Sema::HandlePropertyInClassExtension(Scope *S, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 167 | SourceLocation AtLoc, FieldDeclarator &FD, |
| 168 | Selector GetterSel, Selector SetterSel, |
| 169 | const bool isAssign, |
| 170 | const bool isReadWrite, |
| 171 | const unsigned Attributes, |
| 172 | bool *isOverridingProperty, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 173 | TypeSourceInfo *T, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 174 | tok::ObjCKeywordKind MethodImplKind) { |
Fariborz Jahanian | 58a7649 | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 175 | ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 176 | // Diagnose if this property is already in continuation class. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 177 | DeclContext *DC = CurContext; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 178 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
Fariborz Jahanian | 2a28914 | 2010-11-10 18:01:36 +0000 | [diff] [blame] | 179 | ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface(); |
| 180 | |
| 181 | if (CCPrimary) |
| 182 | // Check for duplicate declaration of this property in current and |
| 183 | // other class extensions. |
| 184 | for (const ObjCCategoryDecl *ClsExtDecl = |
| 185 | CCPrimary->getFirstClassExtension(); |
| 186 | ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) { |
| 187 | if (ObjCPropertyDecl *prevDecl = |
| 188 | ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) { |
| 189 | Diag(AtLoc, diag::err_duplicate_property); |
| 190 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
| 191 | return 0; |
| 192 | } |
| 193 | } |
| 194 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 195 | // Create a new ObjCPropertyDecl with the DeclContext being |
| 196 | // the class extension. |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 197 | // FIXME. We should really be using CreatePropertyDecl for this. |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 198 | ObjCPropertyDecl *PDecl = |
| 199 | ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(), |
| 200 | PropertyId, AtLoc, T); |
Fariborz Jahanian | 22f757b | 2010-03-22 23:25:52 +0000 | [diff] [blame] | 201 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
| 202 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
| 203 | if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) |
| 204 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 205 | // Set setter/getter selector name. Needed later. |
| 206 | PDecl->setGetterName(GetterSel); |
| 207 | PDecl->setSetterName(SetterSel); |
Douglas Gregor | 91ae6b4 | 2011-07-15 15:30:21 +0000 | [diff] [blame] | 208 | ProcessDeclAttributes(S, PDecl, FD.D); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 209 | DC->addDecl(PDecl); |
| 210 | |
| 211 | // We need to look in the @interface to see if the @property was |
| 212 | // already declared. |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 213 | if (!CCPrimary) { |
| 214 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
| 215 | *isOverridingProperty = true; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 216 | return 0; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Find the property in continuation class's primary class only. |
| 220 | ObjCPropertyDecl *PIDecl = |
| 221 | CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId); |
| 222 | |
| 223 | if (!PIDecl) { |
| 224 | // No matching property found in the primary class. Just fall thru |
| 225 | // and add property to continuation class's primary class. |
| 226 | ObjCPropertyDecl *PDecl = |
| 227 | CreatePropertyDecl(S, CCPrimary, AtLoc, |
| 228 | FD, GetterSel, SetterSel, isAssign, isReadWrite, |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 229 | Attributes, T, MethodImplKind, DC); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 230 | |
| 231 | // A case of continuation class adding a new property in the class. This |
| 232 | // is not what it was meant for. However, gcc supports it and so should we. |
| 233 | // Make sure setter/getters are declared here. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 234 | ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0, |
| 235 | /* lexicalDC = */ CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 236 | return PDecl; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 237 | } |
Fariborz Jahanian | a4b984d | 2011-09-24 00:56:59 +0000 | [diff] [blame] | 238 | if (PIDecl->getType().getCanonicalType() |
| 239 | != PDecl->getType().getCanonicalType()) { |
| 240 | Diag(AtLoc, |
| 241 | diag::error_type_mismatch_continuation_class) << PDecl->getType(); |
| 242 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 243 | } |
| 244 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 245 | // The property 'PIDecl's readonly attribute will be over-ridden |
| 246 | // with continuation class's readwrite property attribute! |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 247 | unsigned PIkind = PIDecl->getPropertyAttributesAsWritten(); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 248 | if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { |
| 249 | unsigned retainCopyNonatomic = |
| 250 | (ObjCPropertyDecl::OBJC_PR_retain | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 251 | ObjCPropertyDecl::OBJC_PR_strong | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 252 | ObjCPropertyDecl::OBJC_PR_copy | |
| 253 | ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 254 | if ((Attributes & retainCopyNonatomic) != |
| 255 | (PIkind & retainCopyNonatomic)) { |
| 256 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
| 257 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 258 | } |
Ted Kremenek | 9944c76 | 2010-03-18 01:22:36 +0000 | [diff] [blame] | 259 | DeclContext *DC = cast<DeclContext>(CCPrimary); |
| 260 | if (!ObjCPropertyDecl::findPropertyDecl(DC, |
| 261 | PIDecl->getDeclName().getAsIdentifierInfo())) { |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 262 | // Protocol is not in the primary class. Must build one for it. |
| 263 | ObjCDeclSpec ProtocolPropertyODS; |
| 264 | // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind |
| 265 | // and ObjCPropertyDecl::PropertyAttributeKind have identical |
| 266 | // values. Should consolidate both into one enum type. |
| 267 | ProtocolPropertyODS. |
| 268 | setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind) |
| 269 | PIkind); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 270 | // Must re-establish the context from class extension to primary |
| 271 | // class context. |
Fariborz Jahanian | 7939418 | 2011-08-22 20:15:24 +0000 | [diff] [blame] | 272 | ContextRAII SavedContext(*this, CCPrimary); |
| 273 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 274 | Decl *ProtocolPtrTy = |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 275 | ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS, |
| 276 | PIDecl->getGetterName(), |
| 277 | PIDecl->getSetterName(), |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 278 | isOverridingProperty, |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 279 | MethodImplKind, |
| 280 | /* lexicalDC = */ CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 281 | PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 282 | } |
| 283 | PIDecl->makeitReadWriteAttribute(); |
| 284 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 285 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 286 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
| 287 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 288 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 289 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 290 | PIDecl->setSetterName(SetterSel); |
| 291 | } else { |
Ted Kremenek | 788f489 | 2010-10-21 18:49:42 +0000 | [diff] [blame] | 292 | // Tailor the diagnostics for the common case where a readwrite |
| 293 | // property is declared both in the @interface and the continuation. |
| 294 | // This is a common error where the user often intended the original |
| 295 | // declaration to be readonly. |
| 296 | unsigned diag = |
| 297 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite) && |
| 298 | (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) |
| 299 | ? diag::err_use_continuation_class_redeclaration_readwrite |
| 300 | : diag::err_use_continuation_class; |
| 301 | Diag(AtLoc, diag) |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 302 | << CCPrimary->getDeclName(); |
| 303 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 304 | } |
| 305 | *isOverridingProperty = true; |
| 306 | // Make sure setter decl is synthesized, and added to primary class's list. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 307 | ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 308 | return 0; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S, |
| 312 | ObjCContainerDecl *CDecl, |
| 313 | SourceLocation AtLoc, |
| 314 | FieldDeclarator &FD, |
| 315 | Selector GetterSel, |
| 316 | Selector SetterSel, |
| 317 | const bool isAssign, |
| 318 | const bool isReadWrite, |
| 319 | const unsigned Attributes, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 320 | TypeSourceInfo *TInfo, |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 321 | tok::ObjCKeywordKind MethodImplKind, |
| 322 | DeclContext *lexicalDC){ |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 323 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 324 | QualType T = TInfo->getType(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 325 | |
| 326 | // Issue a warning if property is 'assign' as default and its object, which is |
| 327 | // gc'able conforms to NSCopying protocol |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 328 | if (getLangOptions().getGC() != LangOptions::NonGC && |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 329 | isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 330 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 331 | T->getAs<ObjCObjectPointerType>()) { |
| 332 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 333 | if (IDecl) |
| 334 | if (ObjCProtocolDecl* PNSCopying = |
| 335 | LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc)) |
| 336 | if (IDecl->ClassImplementsProtocol(PNSCopying, true)) |
| 337 | Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 338 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 339 | if (T->isObjCObjectType()) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 340 | Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object); |
| 341 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 342 | DeclContext *DC = cast<DeclContext>(CDecl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 343 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
| 344 | FD.D.getIdentifierLoc(), |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 345 | PropertyId, AtLoc, TInfo); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 347 | if (ObjCPropertyDecl *prevDecl = |
| 348 | ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 349 | Diag(PDecl->getLocation(), diag::err_duplicate_property); |
Ted Kremenek | 894ae6a | 2010-03-15 18:47:25 +0000 | [diff] [blame] | 350 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 351 | PDecl->setInvalidDecl(); |
| 352 | } |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 353 | else { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 354 | DC->addDecl(PDecl); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 355 | if (lexicalDC) |
| 356 | PDecl->setLexicalDeclContext(lexicalDC); |
| 357 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 358 | |
| 359 | if (T->isArrayType() || T->isFunctionType()) { |
| 360 | Diag(AtLoc, diag::err_property_type) << T; |
| 361 | PDecl->setInvalidDecl(); |
| 362 | } |
| 363 | |
| 364 | ProcessDeclAttributes(S, PDecl, FD.D); |
| 365 | |
| 366 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 367 | // selector names in anticipation of declaration of setter/getter methods. |
| 368 | PDecl->setGetterName(GetterSel); |
| 369 | PDecl->setSetterName(SetterSel); |
| 370 | |
Argyrios Kyrtzidis | 0a68dc7 | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 371 | unsigned attributesAsWritten = 0; |
| 372 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
| 373 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly; |
| 374 | if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) |
| 375 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite; |
| 376 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
| 377 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter; |
| 378 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
| 379 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter; |
| 380 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) |
| 381 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign; |
| 382 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 383 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain; |
| 384 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
| 385 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong; |
| 386 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) |
| 387 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak; |
| 388 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 389 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy; |
| 390 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
| 391 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained; |
| 392 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
| 393 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic; |
| 394 | if (Attributes & ObjCDeclSpec::DQ_PR_atomic) |
| 395 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic; |
| 396 | |
| 397 | PDecl->setPropertyAttributesAsWritten( |
| 398 | (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten); |
| 399 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 400 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
| 401 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
| 402 | |
| 403 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
| 404 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
| 405 | |
| 406 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
| 407 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
| 408 | |
| 409 | if (isReadWrite) |
| 410 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
| 411 | |
| 412 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 413 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 414 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 415 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
| 416 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 417 | |
| 418 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) |
| 419 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); |
| 420 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 421 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 422 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 423 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 424 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
| 425 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 426 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 427 | if (isAssign) |
| 428 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 429 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 430 | // In the semantic attributes, one of nonatomic or atomic is always set. |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 431 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
| 432 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 433 | else |
Fariborz Jahanian | 45937ae | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 434 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 435 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 436 | // 'unsafe_unretained' is alias for 'assign'. |
| 437 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
| 438 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 439 | if (isAssign) |
| 440 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 441 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 442 | if (MethodImplKind == tok::objc_required) |
| 443 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 444 | else if (MethodImplKind == tok::objc_optional) |
| 445 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 446 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 447 | return PDecl; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 448 | } |
| 449 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 450 | static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, |
| 451 | ObjCPropertyDecl *property, |
| 452 | ObjCIvarDecl *ivar) { |
| 453 | if (property->isInvalidDecl() || ivar->isInvalidDecl()) return; |
| 454 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 455 | QualType ivarType = ivar->getType(); |
| 456 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 457 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 458 | // The lifetime implied by the property's attributes. |
| 459 | Qualifiers::ObjCLifetime propertyLifetime = |
| 460 | getImpliedARCOwnership(property->getPropertyAttributes(), |
| 461 | property->getType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 462 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 463 | // We're fine if they match. |
| 464 | if (propertyLifetime == ivarLifetime) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 465 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 466 | // These aren't valid lifetimes for object ivars; don't diagnose twice. |
| 467 | if (ivarLifetime == Qualifiers::OCL_None || |
| 468 | ivarLifetime == Qualifiers::OCL_Autoreleasing) |
| 469 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 470 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 471 | switch (propertyLifetime) { |
| 472 | case Qualifiers::OCL_Strong: |
| 473 | S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership) |
| 474 | << property->getDeclName() |
| 475 | << ivar->getDeclName() |
| 476 | << ivarLifetime; |
| 477 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 478 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 479 | case Qualifiers::OCL_Weak: |
| 480 | S.Diag(propertyImplLoc, diag::error_weak_property) |
| 481 | << property->getDeclName() |
| 482 | << ivar->getDeclName(); |
| 483 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 484 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 485 | case Qualifiers::OCL_ExplicitNone: |
| 486 | S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership) |
| 487 | << property->getDeclName() |
| 488 | << ivar->getDeclName() |
| 489 | << ((property->getPropertyAttributesAsWritten() |
| 490 | & ObjCPropertyDecl::OBJC_PR_assign) != 0); |
| 491 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 492 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 493 | case Qualifiers::OCL_Autoreleasing: |
| 494 | llvm_unreachable("properties cannot be autoreleasing"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 495 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 496 | case Qualifiers::OCL_None: |
| 497 | // Any other property should be ignored. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 498 | return; |
| 499 | } |
| 500 | |
| 501 | S.Diag(property->getLocation(), diag::note_property_declare); |
| 502 | } |
| 503 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 504 | |
| 505 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 506 | /// builds the AST node for a property implementation declaration; declared |
| 507 | /// as @synthesize or @dynamic. |
| 508 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 509 | Decl *Sema::ActOnPropertyImplDecl(Scope *S, |
| 510 | SourceLocation AtLoc, |
| 511 | SourceLocation PropertyLoc, |
| 512 | bool Synthesize, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 513 | IdentifierInfo *PropertyId, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 514 | IdentifierInfo *PropertyIvar, |
| 515 | SourceLocation PropertyIvarLoc) { |
Ted Kremenek | e968657 | 2010-04-05 23:45:09 +0000 | [diff] [blame] | 516 | ObjCContainerDecl *ClassImpDecl = |
Fariborz Jahanian | 84e0ccf | 2011-09-19 16:32:32 +0000 | [diff] [blame] | 517 | dyn_cast<ObjCContainerDecl>(CurContext); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 518 | // Make sure we have a context for the property implementation declaration. |
| 519 | if (!ClassImpDecl) { |
| 520 | Diag(AtLoc, diag::error_missing_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 521 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 522 | } |
| 523 | ObjCPropertyDecl *property = 0; |
| 524 | ObjCInterfaceDecl* IDecl = 0; |
| 525 | // Find the class or category class where this property must have |
| 526 | // a declaration. |
| 527 | ObjCImplementationDecl *IC = 0; |
| 528 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 529 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
| 530 | IDecl = IC->getClassInterface(); |
| 531 | // We always synthesize an interface for an implementation |
| 532 | // without an interface decl. So, IDecl is always non-zero. |
| 533 | assert(IDecl && |
| 534 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 535 | |
| 536 | // Look for this property declaration in the @implementation's @interface |
| 537 | property = IDecl->FindPropertyDeclaration(PropertyId); |
| 538 | if (!property) { |
| 539 | Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 540 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 541 | } |
Fariborz Jahanian | dd4430e | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 542 | unsigned PIkind = property->getPropertyAttributesAsWritten(); |
Fariborz Jahanian | 45937ae | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 543 | if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic | |
| 544 | ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) { |
Fariborz Jahanian | dd4430e | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 545 | if (AtLoc.isValid()) |
| 546 | Diag(AtLoc, diag::warn_implicit_atomic_property); |
| 547 | else |
| 548 | Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property); |
| 549 | Diag(property->getLocation(), diag::note_property_declare); |
| 550 | } |
| 551 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 552 | if (const ObjCCategoryDecl *CD = |
| 553 | dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { |
| 554 | if (!CD->IsClassExtension()) { |
| 555 | Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); |
| 556 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 557 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
| 561 | if (Synthesize) { |
| 562 | Diag(AtLoc, diag::error_synthesize_category_decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 563 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 564 | } |
| 565 | IDecl = CatImplClass->getClassInterface(); |
| 566 | if (!IDecl) { |
| 567 | Diag(AtLoc, diag::error_missing_property_interface); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 568 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 569 | } |
| 570 | ObjCCategoryDecl *Category = |
| 571 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 572 | |
| 573 | // If category for this implementation not found, it is an error which |
| 574 | // has already been reported eralier. |
| 575 | if (!Category) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 576 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 577 | // Look for this property declaration in @implementation's category |
| 578 | property = Category->FindPropertyDeclaration(PropertyId); |
| 579 | if (!property) { |
| 580 | Diag(PropertyLoc, diag::error_bad_category_property_decl) |
| 581 | << Category->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 582 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 583 | } |
| 584 | } else { |
| 585 | Diag(AtLoc, diag::error_bad_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 586 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 587 | } |
| 588 | ObjCIvarDecl *Ivar = 0; |
| 589 | // Check that we have a valid, previously declared ivar for @synthesize |
| 590 | if (Synthesize) { |
| 591 | // @synthesize |
| 592 | if (!PropertyIvar) |
| 593 | PropertyIvar = PropertyId; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 594 | ObjCPropertyDecl::PropertyAttributeKind kind |
| 595 | = property->getPropertyAttributes(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 596 | QualType PropType = property->getType(); |
| 597 | |
| 598 | QualType PropertyIvarType = PropType.getNonReferenceType(); |
| 599 | |
| 600 | // Add GC __weak to the ivar type if the property is weak. |
| 601 | if ((kind & ObjCPropertyDecl::OBJC_PR_weak) && |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 602 | getLangOptions().getGC() != LangOptions::NonGC) { |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 603 | assert(!getLangOptions().ObjCAutoRefCount); |
| 604 | if (PropertyIvarType.isObjCGCStrong()) { |
| 605 | Diag(PropertyLoc, diag::err_gc_weak_property_strong_type); |
| 606 | Diag(property->getLocation(), diag::note_property_declare); |
| 607 | } else { |
| 608 | PropertyIvarType = |
| 609 | Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak); |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 610 | } |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 611 | } |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 612 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 613 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
| 614 | ObjCInterfaceDecl *ClassDeclared; |
| 615 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); |
| 616 | if (!Ivar) { |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 617 | // In ARC, give the ivar a lifetime qualifier based on the |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 618 | // property attributes. |
| 619 | if (getLangOptions().ObjCAutoRefCount && |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 620 | !PropertyIvarType.getObjCLifetime() && |
| 621 | PropertyIvarType->isObjCRetainableType()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 622 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 623 | // It's an error if we have to do this and the user didn't |
| 624 | // explicitly write an ownership attribute on the property. |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 625 | if (!property->hasWrittenStorageAttribute() && |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 626 | !(kind & ObjCPropertyDecl::OBJC_PR_strong)) { |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 627 | Diag(PropertyLoc, |
| 628 | diag::err_arc_objc_property_default_assign_on_object); |
| 629 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 630 | } else { |
| 631 | Qualifiers::ObjCLifetime lifetime = |
| 632 | getImpliedARCOwnership(kind, PropertyIvarType); |
| 633 | assert(lifetime && "no lifetime for property?"); |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 634 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 635 | if (lifetime == Qualifiers::OCL_Weak && |
| 636 | !getLangOptions().ObjCRuntimeHasWeak) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 637 | Diag(PropertyLoc, diag::err_arc_weak_no_runtime); |
| 638 | Diag(property->getLocation(), diag::note_property_declare); |
| 639 | } |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 640 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 641 | Qualifiers qs; |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 642 | qs.addObjCLifetime(lifetime); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 643 | PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs); |
| 644 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | if (kind & ObjCPropertyDecl::OBJC_PR_weak && |
| 648 | !getLangOptions().ObjCAutoRefCount && |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 649 | getLangOptions().getGC() == LangOptions::NonGC) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 650 | Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc); |
| 651 | Diag(property->getLocation(), diag::note_property_declare); |
| 652 | } |
| 653 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 654 | Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
| 655 | PropertyLoc, PropertyLoc, PropertyIvar, |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 656 | PropertyIvarType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 657 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 658 | (Expr *)0, true); |
Daniel Dunbar | 29fa69a | 2010-04-02 19:44:54 +0000 | [diff] [blame] | 659 | ClassImpDecl->addDecl(Ivar); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 660 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 661 | property->setPropertyIvarDecl(Ivar); |
| 662 | |
| 663 | if (!getLangOptions().ObjCNonFragileABI) |
| 664 | Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; |
| 665 | // Note! I deliberately want it to fall thru so, we have a |
| 666 | // a property implementation and to avoid future warnings. |
| 667 | } else if (getLangOptions().ObjCNonFragileABI && |
| 668 | ClassDeclared != IDecl) { |
| 669 | Diag(PropertyLoc, diag::error_ivar_in_superclass_use) |
| 670 | << property->getDeclName() << Ivar->getDeclName() |
| 671 | << ClassDeclared->getDeclName(); |
| 672 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 673 | << Ivar << Ivar->getName(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 674 | // Note! I deliberately want it to fall thru so more errors are caught. |
| 675 | } |
| 676 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
| 677 | |
| 678 | // Check that type of property and its ivar are type compatible. |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 679 | if (Context.getCanonicalType(PropertyIvarType) != IvarType) { |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 680 | bool compat = false; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 681 | if (isa<ObjCObjectPointerType>(PropertyIvarType) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 682 | && isa<ObjCObjectPointerType>(IvarType)) |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 683 | compat = |
| 684 | Context.canAssignObjCInterfaces( |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 685 | PropertyIvarType->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 686 | IvarType->getAs<ObjCObjectPointerType>()); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 687 | else { |
| 688 | SourceLocation Loc = PropertyIvarLoc; |
| 689 | if (Loc.isInvalid()) |
| 690 | Loc = PropertyLoc; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 691 | compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 692 | == Compatible); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 693 | } |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 694 | if (!compat) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 695 | Diag(PropertyLoc, diag::error_property_ivar_type) |
Ted Kremenek | f921a48 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 696 | << property->getDeclName() << PropType |
| 697 | << Ivar->getDeclName() << IvarType; |
| 698 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 699 | // Note! I deliberately want it to fall thru so, we have a |
| 700 | // a property implementation and to avoid future warnings. |
| 701 | } |
| 702 | |
| 703 | // FIXME! Rules for properties are somewhat different that those |
| 704 | // for assignments. Use a new routine to consolidate all cases; |
| 705 | // specifically for property redeclarations as well as for ivars. |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 706 | QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 707 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
| 708 | if (lhsType != rhsType && |
| 709 | lhsType->isArithmeticType()) { |
| 710 | Diag(PropertyLoc, diag::error_property_ivar_type) |
Ted Kremenek | f921a48 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 711 | << property->getDeclName() << PropType |
| 712 | << Ivar->getDeclName() << IvarType; |
| 713 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 714 | // Fall thru - see previous comment |
| 715 | } |
| 716 | // __weak is explicit. So it works on Canonical type. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 717 | if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 718 | getLangOptions().getGC() != LangOptions::NonGC)) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 719 | Diag(PropertyLoc, diag::error_weak_property) |
| 720 | << property->getDeclName() << Ivar->getDeclName(); |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 721 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 722 | // Fall thru - see previous comment |
| 723 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 724 | // Fall thru - see previous comment |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 725 | if ((property->getType()->isObjCObjectPointerType() || |
| 726 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 727 | getLangOptions().getGC() != LangOptions::NonGC) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 728 | Diag(PropertyLoc, diag::error_strong_property) |
| 729 | << property->getDeclName() << Ivar->getDeclName(); |
| 730 | // Fall thru - see previous comment |
| 731 | } |
| 732 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 733 | if (getLangOptions().ObjCAutoRefCount) |
| 734 | checkARCPropertyImpl(*this, PropertyLoc, property, Ivar); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 735 | } else if (PropertyIvar) |
| 736 | // @dynamic |
| 737 | Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 738 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 739 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
| 740 | ObjCPropertyImplDecl *PIDecl = |
| 741 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 742 | property, |
| 743 | (Synthesize ? |
| 744 | ObjCPropertyImplDecl::Synthesize |
| 745 | : ObjCPropertyImplDecl::Dynamic), |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 746 | Ivar, PropertyIvarLoc); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 747 | if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { |
| 748 | getterMethod->createImplicitParams(Context, IDecl); |
Fariborz Jahanian | 0313f44 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 749 | if (getLangOptions().CPlusPlus && Synthesize && |
| 750 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 751 | // For Objective-C++, need to synthesize the AST for the IVAR object to be |
| 752 | // returned by the getter as it must conform to C++'s copy-return rules. |
| 753 | // FIXME. Eventually we want to do this for Objective-C as well. |
| 754 | ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); |
| 755 | DeclRefExpr *SelfExpr = |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 756 | new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(), |
| 757 | VK_RValue, SourceLocation()); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 758 | Expr *IvarRefExpr = |
| 759 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, |
| 760 | SelfExpr, true, true); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 761 | ExprResult Res = |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 762 | PerformCopyInitialization(InitializedEntity::InitializeResult( |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 763 | SourceLocation(), |
| 764 | getterMethod->getResultType(), |
| 765 | /*NRVO=*/false), |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 766 | SourceLocation(), |
| 767 | Owned(IvarRefExpr)); |
| 768 | if (!Res.isInvalid()) { |
| 769 | Expr *ResExpr = Res.takeAs<Expr>(); |
| 770 | if (ResExpr) |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 771 | ResExpr = MaybeCreateExprWithCleanups(ResExpr); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 772 | PIDecl->setGetterCXXConstructor(ResExpr); |
| 773 | } |
| 774 | } |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 775 | if (property->hasAttr<NSReturnsNotRetainedAttr>() && |
| 776 | !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 777 | Diag(getterMethod->getLocation(), |
| 778 | diag::warn_property_getter_owning_mismatch); |
| 779 | Diag(property->getLocation(), diag::note_property_declare); |
| 780 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 781 | } |
| 782 | if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { |
| 783 | setterMethod->createImplicitParams(Context, IDecl); |
Fariborz Jahanian | 0313f44 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 784 | if (getLangOptions().CPlusPlus && Synthesize |
| 785 | && Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 786 | // FIXME. Eventually we want to do this for Objective-C as well. |
| 787 | ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); |
| 788 | DeclRefExpr *SelfExpr = |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 789 | new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(), |
| 790 | VK_RValue, SourceLocation()); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 791 | Expr *lhs = |
| 792 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, |
| 793 | SelfExpr, true, true); |
| 794 | ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); |
| 795 | ParmVarDecl *Param = (*P); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 796 | QualType T = Param->getType(); |
| 797 | if (T->isReferenceType()) |
Fariborz Jahanian | 61750f2 | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 798 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 799 | Expr *rhs = new (Context) DeclRefExpr(Param, T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 800 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | fa43239 | 2010-10-14 21:30:10 +0000 | [diff] [blame] | 801 | ExprResult Res = BuildBinOp(S, lhs->getLocEnd(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 802 | BO_Assign, lhs, rhs); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 803 | PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>()); |
| 804 | } |
| 805 | } |
| 806 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 807 | if (IC) { |
| 808 | if (Synthesize) |
| 809 | if (ObjCPropertyImplDecl *PPIDecl = |
| 810 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 811 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 812 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 813 | << PropertyIvar; |
| 814 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 815 | } |
| 816 | |
| 817 | if (ObjCPropertyImplDecl *PPIDecl |
| 818 | = IC->FindPropertyImplDecl(PropertyId)) { |
| 819 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 820 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 821 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 822 | } |
| 823 | IC->addPropertyImplementation(PIDecl); |
Fariborz Jahanian | e776f88 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 824 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 825 | getLangOptions().ObjCNonFragileABI2) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 826 | // Diagnose if an ivar was lazily synthesdized due to a previous |
| 827 | // use and if 1) property is @dynamic or 2) property is synthesized |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 828 | // but it requires an ivar of different name. |
Fariborz Jahanian | 411c25c | 2011-01-20 23:34:25 +0000 | [diff] [blame] | 829 | ObjCInterfaceDecl *ClassDeclared=0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 830 | ObjCIvarDecl *Ivar = 0; |
| 831 | if (!Synthesize) |
| 832 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 833 | else { |
| 834 | if (PropertyIvar && PropertyIvar != PropertyId) |
| 835 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 836 | } |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 837 | // Issue diagnostics only if Ivar belongs to current class. |
| 838 | if (Ivar && Ivar->getSynthesize() && |
| 839 | IC->getClassInterface() == ClassDeclared) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 840 | Diag(Ivar->getLocation(), diag::err_undeclared_var_use) |
| 841 | << PropertyId; |
| 842 | Ivar->setInvalidDecl(); |
| 843 | } |
| 844 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 845 | } else { |
| 846 | if (Synthesize) |
| 847 | if (ObjCPropertyImplDecl *PPIDecl = |
| 848 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 849 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 850 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 851 | << PropertyIvar; |
| 852 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 853 | } |
| 854 | |
| 855 | if (ObjCPropertyImplDecl *PPIDecl = |
| 856 | CatImplClass->FindPropertyImplDecl(PropertyId)) { |
| 857 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 858 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 859 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 860 | } |
| 861 | CatImplClass->addPropertyImplementation(PIDecl); |
| 862 | } |
| 863 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 864 | return PIDecl; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | //===----------------------------------------------------------------------===// |
| 868 | // Helper methods. |
| 869 | //===----------------------------------------------------------------------===// |
| 870 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 871 | /// DiagnosePropertyMismatch - Compares two properties for their |
| 872 | /// attributes and types and warns on a variety of inconsistencies. |
| 873 | /// |
| 874 | void |
| 875 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 876 | ObjCPropertyDecl *SuperProperty, |
| 877 | const IdentifierInfo *inheritedName) { |
| 878 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 879 | Property->getPropertyAttributes(); |
| 880 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
| 881 | SuperProperty->getPropertyAttributes(); |
| 882 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 883 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 884 | Diag(Property->getLocation(), diag::warn_readonly_property) |
| 885 | << Property->getDeclName() << inheritedName; |
| 886 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 887 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
| 888 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 889 | << Property->getDeclName() << "copy" << inheritedName; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 890 | else { |
| 891 | unsigned CAttrRetain = |
| 892 | (CAttr & |
| 893 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 894 | unsigned SAttrRetain = |
| 895 | (SAttr & |
| 896 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 897 | bool CStrong = (CAttrRetain != 0); |
| 898 | bool SStrong = (SAttrRetain != 0); |
| 899 | if (CStrong != SStrong) |
| 900 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 901 | << Property->getDeclName() << "retain (or strong)" << inheritedName; |
| 902 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 903 | |
| 904 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 905 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
| 906 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 907 | << Property->getDeclName() << "atomic" << inheritedName; |
| 908 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
| 909 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 910 | << Property->getDeclName() << "setter" << inheritedName; |
| 911 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
| 912 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 913 | << Property->getDeclName() << "getter" << inheritedName; |
| 914 | |
| 915 | QualType LHSType = |
| 916 | Context.getCanonicalType(SuperProperty->getType()); |
| 917 | QualType RHSType = |
| 918 | Context.getCanonicalType(Property->getType()); |
| 919 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 920 | if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
Fariborz Jahanian | 8beb6a2 | 2011-07-13 17:55:01 +0000 | [diff] [blame] | 921 | // Do cases not handled in above. |
| 922 | // FIXME. For future support of covariant property types, revisit this. |
| 923 | bool IncompatibleObjC = false; |
| 924 | QualType ConvertedType; |
| 925 | if (!isObjCPointerConversion(RHSType, LHSType, |
| 926 | ConvertedType, IncompatibleObjC) || |
| 927 | IncompatibleObjC) |
| 928 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) |
| 929 | << Property->getType() << SuperProperty->getType() << inheritedName; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
| 933 | bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, |
| 934 | ObjCMethodDecl *GetterMethod, |
| 935 | SourceLocation Loc) { |
| 936 | if (GetterMethod && |
| 937 | GetterMethod->getResultType() != property->getType()) { |
| 938 | AssignConvertType result = Incompatible; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 939 | if (property->getType()->isObjCObjectPointerType()) |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 940 | result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(), |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 941 | property->getType()); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 942 | if (result != Compatible) { |
| 943 | Diag(Loc, diag::warn_accessor_property_type_mismatch) |
| 944 | << property->getDeclName() |
| 945 | << GetterMethod->getSelector(); |
| 946 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 947 | return true; |
| 948 | } |
| 949 | } |
| 950 | return false; |
| 951 | } |
| 952 | |
| 953 | /// ComparePropertiesInBaseAndSuper - This routine compares property |
| 954 | /// declarations in base and its super class, if any, and issues |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 955 | /// diagnostics in a variety of inconsistent situations. |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 956 | /// |
| 957 | void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { |
| 958 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 959 | if (!SDecl) |
| 960 | return; |
| 961 | // FIXME: O(N^2) |
| 962 | for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(), |
| 963 | E = SDecl->prop_end(); S != E; ++S) { |
| 964 | ObjCPropertyDecl *SuperPDecl = (*S); |
| 965 | // Does property in super class has declaration in current class? |
| 966 | for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(), |
| 967 | E = IDecl->prop_end(); I != E; ++I) { |
| 968 | ObjCPropertyDecl *PDecl = (*I); |
| 969 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) |
| 970 | DiagnosePropertyMismatch(PDecl, SuperPDecl, |
| 971 | SDecl->getIdentifier()); |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /// MatchOneProtocolPropertiesInClass - This routine goes thru the list |
| 977 | /// of properties declared in a protocol and compares their attribute against |
| 978 | /// the same property declared in the class or category. |
| 979 | void |
| 980 | Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, |
| 981 | ObjCProtocolDecl *PDecl) { |
| 982 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 983 | if (!IDecl) { |
| 984 | // Category |
| 985 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 986 | assert (CatDecl && "MatchOneProtocolPropertiesInClass"); |
| 987 | if (!CatDecl->IsClassExtension()) |
| 988 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 989 | E = PDecl->prop_end(); P != E; ++P) { |
| 990 | ObjCPropertyDecl *Pr = (*P); |
| 991 | ObjCCategoryDecl::prop_iterator CP, CE; |
| 992 | // Is this property already in category's list of properties? |
Ted Kremenek | 2d2f936 | 2010-03-12 00:49:00 +0000 | [diff] [blame] | 993 | for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 994 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 995 | break; |
| 996 | if (CP != CE) |
| 997 | // Property protocol already exist in class. Diagnose any mismatch. |
| 998 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
| 999 | } |
| 1000 | return; |
| 1001 | } |
| 1002 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1003 | E = PDecl->prop_end(); P != E; ++P) { |
| 1004 | ObjCPropertyDecl *Pr = (*P); |
| 1005 | ObjCInterfaceDecl::prop_iterator CP, CE; |
| 1006 | // Is this property already in class's list of properties? |
| 1007 | for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP) |
| 1008 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 1009 | break; |
| 1010 | if (CP != CE) |
| 1011 | // Property protocol already exist in class. Diagnose any mismatch. |
| 1012 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | /// CompareProperties - This routine compares properties |
| 1017 | /// declared in 'ClassOrProtocol' objects (which can be a class or an |
| 1018 | /// inherited protocol with the list of properties for class/category 'CDecl' |
| 1019 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1020 | void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) { |
| 1021 | Decl *ClassDecl = ClassOrProtocol; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1022 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 1023 | |
| 1024 | if (!IDecl) { |
| 1025 | // Category |
| 1026 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 1027 | assert (CatDecl && "CompareProperties"); |
| 1028 | if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
| 1029 | for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 1030 | E = MDecl->protocol_end(); P != E; ++P) |
| 1031 | // Match properties of category with those of protocol (*P) |
| 1032 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
| 1033 | |
| 1034 | // Go thru the list of protocols for this category and recursively match |
| 1035 | // their properties with those in the category. |
| 1036 | for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), |
| 1037 | E = CatDecl->protocol_end(); P != E; ++P) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1038 | CompareProperties(CatDecl, *P); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1039 | } else { |
| 1040 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 1041 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 1042 | E = MD->protocol_end(); P != E; ++P) |
| 1043 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
| 1044 | } |
| 1045 | return; |
| 1046 | } |
| 1047 | |
| 1048 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1049 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1050 | P = MDecl->all_referenced_protocol_begin(), |
| 1051 | E = MDecl->all_referenced_protocol_end(); P != E; ++P) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1052 | // Match properties of class IDecl with those of protocol (*P). |
| 1053 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
| 1054 | |
| 1055 | // Go thru the list of protocols for this class and recursively match |
| 1056 | // their properties with those declared in the class. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1057 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1058 | P = IDecl->all_referenced_protocol_begin(), |
| 1059 | E = IDecl->all_referenced_protocol_end(); P != E; ++P) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1060 | CompareProperties(IDecl, *P); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1061 | } else { |
| 1062 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 1063 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 1064 | E = MD->protocol_end(); P != E; ++P) |
| 1065 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | /// isPropertyReadonly - Return true if property is readonly, by searching |
| 1070 | /// for the property in the class and in its categories and implementations |
| 1071 | /// |
| 1072 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, |
| 1073 | ObjCInterfaceDecl *IDecl) { |
| 1074 | // by far the most common case. |
| 1075 | if (!PDecl->isReadOnly()) |
| 1076 | return false; |
| 1077 | // Even if property is ready only, if interface has a user defined setter, |
| 1078 | // it is not considered read only. |
| 1079 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) |
| 1080 | return false; |
| 1081 | |
| 1082 | // Main class has the property as 'readonly'. Must search |
| 1083 | // through the category list to see if the property's |
| 1084 | // attribute has been over-ridden to 'readwrite'. |
| 1085 | for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); |
| 1086 | Category; Category = Category->getNextClassCategory()) { |
| 1087 | // Even if property is ready only, if a category has a user defined setter, |
| 1088 | // it is not considered read only. |
| 1089 | if (Category->getInstanceMethod(PDecl->getSetterName())) |
| 1090 | return false; |
| 1091 | ObjCPropertyDecl *P = |
| 1092 | Category->FindPropertyDeclaration(PDecl->getIdentifier()); |
| 1093 | if (P && !P->isReadOnly()) |
| 1094 | return false; |
| 1095 | } |
| 1096 | |
| 1097 | // Also, check for definition of a setter method in the implementation if |
| 1098 | // all else failed. |
| 1099 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { |
| 1100 | if (ObjCImplementationDecl *IMD = |
| 1101 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { |
| 1102 | if (IMD->getInstanceMethod(PDecl->getSetterName())) |
| 1103 | return false; |
| 1104 | } else if (ObjCCategoryImplDecl *CIMD = |
| 1105 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 1106 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) |
| 1107 | return false; |
| 1108 | } |
| 1109 | } |
| 1110 | // Lastly, look through the implementation (if one is in scope). |
| 1111 | if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) |
| 1112 | if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) |
| 1113 | return false; |
| 1114 | // If all fails, look at the super class. |
| 1115 | if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) |
| 1116 | return isPropertyReadonly(PDecl, SIDecl); |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
| 1120 | /// CollectImmediateProperties - This routine collects all properties in |
| 1121 | /// the class and its conforming protocols; but not those it its super class. |
| 1122 | void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1123 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap, |
| 1124 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1125 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1126 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1127 | E = IDecl->prop_end(); P != E; ++P) { |
| 1128 | ObjCPropertyDecl *Prop = (*P); |
| 1129 | PropMap[Prop->getIdentifier()] = Prop; |
| 1130 | } |
| 1131 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1132 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1133 | PI = IDecl->all_referenced_protocol_begin(), |
| 1134 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1135 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1136 | } |
| 1137 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 1138 | if (!CATDecl->IsClassExtension()) |
| 1139 | for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), |
| 1140 | E = CATDecl->prop_end(); P != E; ++P) { |
| 1141 | ObjCPropertyDecl *Prop = (*P); |
| 1142 | PropMap[Prop->getIdentifier()] = Prop; |
| 1143 | } |
| 1144 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1145 | for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1146 | E = CATDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1147 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1148 | } |
| 1149 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1150 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1151 | E = PDecl->prop_end(); P != E; ++P) { |
| 1152 | ObjCPropertyDecl *Prop = (*P); |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1153 | ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()]; |
| 1154 | // Exclude property for protocols which conform to class's super-class, |
| 1155 | // as super-class has to implement the property. |
Fariborz Jahanian | a929ec7 | 2011-09-27 00:23:52 +0000 | [diff] [blame] | 1156 | if (!PropertyFromSuper || |
| 1157 | PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) { |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1158 | ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; |
| 1159 | if (!PropEntry) |
| 1160 | PropEntry = Prop; |
| 1161 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1162 | } |
| 1163 | // scan through protocol's protocols. |
| 1164 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1165 | E = PDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1166 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1170 | /// CollectClassPropertyImplementations - This routine collects list of |
| 1171 | /// properties to be implemented in the class. This includes, class's |
| 1172 | /// and its conforming protocols' properties. |
| 1173 | static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl, |
| 1174 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { |
| 1175 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1176 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1177 | E = IDecl->prop_end(); P != E; ++P) { |
| 1178 | ObjCPropertyDecl *Prop = (*P); |
| 1179 | PropMap[Prop->getIdentifier()] = Prop; |
| 1180 | } |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1181 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1182 | PI = IDecl->all_referenced_protocol_begin(), |
| 1183 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1184 | CollectClassPropertyImplementations((*PI), PropMap); |
| 1185 | } |
| 1186 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1187 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1188 | E = PDecl->prop_end(); P != E; ++P) { |
| 1189 | ObjCPropertyDecl *Prop = (*P); |
| 1190 | PropMap[Prop->getIdentifier()] = Prop; |
| 1191 | } |
| 1192 | // scan through protocol's protocols. |
| 1193 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1194 | E = PDecl->protocol_end(); PI != E; ++PI) |
| 1195 | CollectClassPropertyImplementations((*PI), PropMap); |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | /// CollectSuperClassPropertyImplementations - This routine collects list of |
| 1200 | /// properties to be implemented in super class(s) and also coming from their |
| 1201 | /// conforming protocols. |
| 1202 | static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, |
| 1203 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { |
| 1204 | if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { |
| 1205 | while (SDecl) { |
| 1206 | CollectClassPropertyImplementations(SDecl, PropMap); |
| 1207 | SDecl = SDecl->getSuperClass(); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1212 | /// LookupPropertyDecl - Looks up a property in the current class and all |
| 1213 | /// its protocols. |
| 1214 | ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl, |
| 1215 | IdentifierInfo *II) { |
| 1216 | if (const ObjCInterfaceDecl *IDecl = |
| 1217 | dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1218 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1219 | E = IDecl->prop_end(); P != E; ++P) { |
| 1220 | ObjCPropertyDecl *Prop = (*P); |
| 1221 | if (Prop->getIdentifier() == II) |
| 1222 | return Prop; |
| 1223 | } |
| 1224 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1225 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1226 | PI = IDecl->all_referenced_protocol_begin(), |
| 1227 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1228 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 1229 | if (Prop) |
| 1230 | return Prop; |
| 1231 | } |
| 1232 | } |
| 1233 | else if (const ObjCProtocolDecl *PDecl = |
| 1234 | dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1235 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1236 | E = PDecl->prop_end(); P != E; ++P) { |
| 1237 | ObjCPropertyDecl *Prop = (*P); |
| 1238 | if (Prop->getIdentifier() == II) |
| 1239 | return Prop; |
| 1240 | } |
| 1241 | // scan through protocol's protocols. |
| 1242 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1243 | E = PDecl->protocol_end(); PI != E; ++PI) { |
| 1244 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 1245 | if (Prop) |
| 1246 | return Prop; |
| 1247 | } |
| 1248 | } |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame] | 1252 | static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop, |
| 1253 | ASTContext &Ctx) { |
| 1254 | llvm::SmallString<128> ivarName; |
| 1255 | { |
| 1256 | llvm::raw_svector_ostream os(ivarName); |
| 1257 | os << '_' << Prop->getIdentifier()->getName(); |
| 1258 | } |
| 1259 | return &Ctx.Idents.get(ivarName.str()); |
| 1260 | } |
| 1261 | |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1262 | /// DefaultSynthesizeProperties - This routine default synthesizes all |
| 1263 | /// properties which must be synthesized in class's @implementation. |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame] | 1264 | void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl, |
| 1265 | ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1266 | |
| 1267 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; |
| 1268 | CollectClassPropertyImplementations(IDecl, PropMap); |
| 1269 | if (PropMap.empty()) |
| 1270 | return; |
| 1271 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; |
| 1272 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
| 1273 | |
| 1274 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator |
| 1275 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1276 | ObjCPropertyDecl *Prop = P->second; |
| 1277 | // If property to be implemented in the super class, ignore. |
| 1278 | if (SuperPropMap[Prop->getIdentifier()]) |
| 1279 | continue; |
| 1280 | // Is there a matching propery synthesize/dynamic? |
| 1281 | if (Prop->isInvalidDecl() || |
| 1282 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
| 1283 | IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) |
| 1284 | continue; |
Fariborz Jahanian | d3635b9 | 2010-07-14 18:11:52 +0000 | [diff] [blame] | 1285 | // Property may have been synthesized by user. |
| 1286 | if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier())) |
| 1287 | continue; |
Fariborz Jahanian | 95f1b86 | 2010-08-25 00:31:58 +0000 | [diff] [blame] | 1288 | if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { |
| 1289 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1290 | continue; |
| 1291 | if (IMPDecl->getInstanceMethod(Prop->getSetterName())) |
| 1292 | continue; |
| 1293 | } |
Fariborz Jahanian | d3635b9 | 2010-07-14 18:11:52 +0000 | [diff] [blame] | 1294 | |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1295 | |
| 1296 | // We use invalid SourceLocations for the synthesized ivars since they |
| 1297 | // aren't really synthesized at a particular location; they just exist. |
| 1298 | // Saying that they are located at the @implementation isn't really going |
| 1299 | // to help users. |
| 1300 | ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1301 | true, |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame] | 1302 | /* property = */ Prop->getIdentifier(), |
| 1303 | /* ivar = */ getDefaultSynthIvarName(Prop, Context), |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1304 | SourceLocation()); |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1305 | } |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1306 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1307 | |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1308 | void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) { |
| 1309 | if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2) |
| 1310 | return; |
| 1311 | ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D); |
| 1312 | if (!IC) |
| 1313 | return; |
| 1314 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) |
| 1315 | DefaultSynthesizeProperties(S, IC, IDecl); |
| 1316 | } |
| 1317 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1318 | void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1319 | ObjCContainerDecl *CDecl, |
| 1320 | const llvm::DenseSet<Selector>& InsMap) { |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1321 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; |
| 1322 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
| 1323 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
| 1324 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1325 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1326 | CollectImmediateProperties(CDecl, PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1327 | if (PropMap.empty()) |
| 1328 | return; |
| 1329 | |
| 1330 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
| 1331 | for (ObjCImplDecl::propimpl_iterator |
| 1332 | I = IMPDecl->propimpl_begin(), |
| 1333 | EI = IMPDecl->propimpl_end(); I != EI; ++I) |
| 1334 | PropImplMap.insert((*I)->getPropertyDecl()); |
| 1335 | |
| 1336 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator |
| 1337 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1338 | ObjCPropertyDecl *Prop = P->second; |
| 1339 | // Is there a matching propery synthesize/dynamic? |
| 1340 | if (Prop->isInvalidDecl() || |
| 1341 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
Fariborz Jahanian | 327126e | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 1342 | PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>()) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1343 | continue; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1344 | if (!InsMap.count(Prop->getGetterName())) { |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1345 | Diag(IMPDecl->getLocation(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1346 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1347 | diag::warn_setter_getter_impl_required_in_category : |
| 1348 | diag::warn_setter_getter_impl_required) |
| 1349 | << Prop->getDeclName() << Prop->getGetterName(); |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1350 | Diag(Prop->getLocation(), |
| 1351 | diag::note_property_declare); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1355 | Diag(IMPDecl->getLocation(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1356 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1357 | diag::warn_setter_getter_impl_required_in_category : |
| 1358 | diag::warn_setter_getter_impl_required) |
| 1359 | << Prop->getDeclName() << Prop->getSetterName(); |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1360 | Diag(Prop->getLocation(), |
| 1361 | diag::note_property_declare); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | void |
| 1367 | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, |
| 1368 | ObjCContainerDecl* IDecl) { |
| 1369 | // Rules apply in non-GC mode only |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1370 | if (getLangOptions().getGC() != LangOptions::NonGC) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1371 | return; |
| 1372 | for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), |
| 1373 | E = IDecl->prop_end(); |
| 1374 | I != E; ++I) { |
| 1375 | ObjCPropertyDecl *Property = (*I); |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1376 | ObjCMethodDecl *GetterMethod = 0; |
| 1377 | ObjCMethodDecl *SetterMethod = 0; |
| 1378 | bool LookedUpGetterSetter = false; |
| 1379 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1380 | unsigned Attributes = Property->getPropertyAttributes(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1381 | unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1382 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1383 | if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) && |
| 1384 | !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) { |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1385 | GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1386 | SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1387 | LookedUpGetterSetter = true; |
| 1388 | if (GetterMethod) { |
| 1389 | Diag(GetterMethod->getLocation(), |
| 1390 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | 293a45e | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 1391 | << Property->getIdentifier() << 0; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1392 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1393 | } |
| 1394 | if (SetterMethod) { |
| 1395 | Diag(SetterMethod->getLocation(), |
| 1396 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | 293a45e | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 1397 | << Property->getIdentifier() << 1; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1398 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1399 | } |
| 1400 | } |
| 1401 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1402 | // We only care about readwrite atomic property. |
| 1403 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || |
| 1404 | !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 1405 | continue; |
| 1406 | if (const ObjCPropertyImplDecl *PIDecl |
| 1407 | = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { |
| 1408 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 1409 | continue; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1410 | if (!LookedUpGetterSetter) { |
| 1411 | GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1412 | SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1413 | LookedUpGetterSetter = true; |
| 1414 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1415 | if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { |
| 1416 | SourceLocation MethodLoc = |
| 1417 | (GetterMethod ? GetterMethod->getLocation() |
| 1418 | : SetterMethod->getLocation()); |
| 1419 | Diag(MethodLoc, diag::warn_atomic_property_rule) |
| 1420 | << Property->getIdentifier(); |
| 1421 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | } |
| 1426 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1427 | void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1428 | if (getLangOptions().getGC() == LangOptions::GCOnly) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1429 | return; |
| 1430 | |
| 1431 | for (ObjCImplementationDecl::propimpl_iterator |
| 1432 | i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { |
| 1433 | ObjCPropertyImplDecl *PID = *i; |
| 1434 | if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 1435 | continue; |
| 1436 | |
| 1437 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1438 | if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && |
| 1439 | !D->getInstanceMethod(PD->getGetterName())) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1440 | ObjCMethodDecl *method = PD->getGetterMethodDecl(); |
| 1441 | if (!method) |
| 1442 | continue; |
| 1443 | ObjCMethodFamily family = method->getMethodFamily(); |
| 1444 | if (family == OMF_alloc || family == OMF_copy || |
| 1445 | family == OMF_mutableCopy || family == OMF_new) { |
| 1446 | if (getLangOptions().ObjCAutoRefCount) |
| 1447 | Diag(PID->getLocation(), diag::err_ownin_getter_rule); |
| 1448 | else |
| 1449 | Diag(PID->getLocation(), diag::warn_ownin_getter_rule); |
| 1450 | Diag(PD->getLocation(), diag::note_property_declare); |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1456 | /// AddPropertyAttrs - Propagates attributes from a property to the |
| 1457 | /// implicitly-declared getter or setter for that property. |
| 1458 | static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, |
| 1459 | ObjCPropertyDecl *Property) { |
| 1460 | // Should we just clone all attributes over? |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1461 | for (Decl::attr_iterator A = Property->attr_begin(), |
| 1462 | AEnd = Property->attr_end(); |
| 1463 | A != AEnd; ++A) { |
| 1464 | if (isa<DeprecatedAttr>(*A) || |
| 1465 | isa<UnavailableAttr>(*A) || |
| 1466 | isa<AvailabilityAttr>(*A)) |
| 1467 | PropertyMethod->addAttr((*A)->clone(S.Context)); |
| 1468 | } |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1471 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
| 1472 | /// have the property type and issue diagnostics if they don't. |
| 1473 | /// Also synthesize a getter/setter method if none exist (and update the |
| 1474 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized |
| 1475 | /// methods is the "right" thing to do. |
| 1476 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1477 | ObjCContainerDecl *CD, |
| 1478 | ObjCPropertyDecl *redeclaredProperty, |
| 1479 | ObjCContainerDecl *lexicalDC) { |
| 1480 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1481 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
| 1482 | |
| 1483 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); |
| 1484 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); |
| 1485 | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
| 1486 | property->getLocation()); |
| 1487 | |
| 1488 | if (SetterMethod) { |
| 1489 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1490 | property->getPropertyAttributes(); |
| 1491 | if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && |
| 1492 | Context.getCanonicalType(SetterMethod->getResultType()) != |
| 1493 | Context.VoidTy) |
| 1494 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
| 1495 | if (SetterMethod->param_size() != 1 || |
Fariborz Jahanian | 2aac0c9 | 2011-09-26 22:59:09 +0000 | [diff] [blame] | 1496 | !Context.hasSameUnqualifiedType( |
| 1497 | (*SetterMethod->param_begin())->getType(), property->getType())) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1498 | Diag(property->getLocation(), |
| 1499 | diag::warn_accessor_property_type_mismatch) |
| 1500 | << property->getDeclName() |
| 1501 | << SetterMethod->getSelector(); |
| 1502 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | // Synthesize getter/setter methods if none exist. |
| 1507 | // Find the default getter and if one not found, add one. |
| 1508 | // FIXME: The synthesized property we set here is misleading. We almost always |
| 1509 | // synthesize these methods unless the user explicitly provided prototypes |
| 1510 | // (which is odd, but allowed). Sema should be typechecking that the |
| 1511 | // declarations jive in that situation (which it is not currently). |
| 1512 | if (!GetterMethod) { |
| 1513 | // No instance method of same name as property getter name was found. |
| 1514 | // Declare a getter method and add it to the list of methods |
| 1515 | // for this class. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1516 | SourceLocation Loc = redeclaredProperty ? |
| 1517 | redeclaredProperty->getLocation() : |
| 1518 | property->getLocation(); |
| 1519 | |
| 1520 | GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 1521 | ArrayRef<SourceLocation>(), |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1522 | property->getGetterName(), |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 1523 | property->getType(), 0, CD, /*isInstance=*/true, |
| 1524 | /*isVariadic=*/false, /*isSynthesized=*/true, |
| 1525 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1526 | (property->getPropertyImplementation() == |
| 1527 | ObjCPropertyDecl::Optional) ? |
| 1528 | ObjCMethodDecl::Optional : |
| 1529 | ObjCMethodDecl::Required); |
| 1530 | CD->addDecl(GetterMethod); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1531 | |
| 1532 | AddPropertyAttrs(*this, GetterMethod, property); |
| 1533 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1534 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 1535 | // and the real context should be the same. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1536 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1537 | GetterMethod->setLexicalDeclContext(lexicalDC); |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1538 | if (property->hasAttr<NSReturnsNotRetainedAttr>()) |
| 1539 | GetterMethod->addAttr( |
| 1540 | ::new (Context) NSReturnsNotRetainedAttr(Loc, Context)); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1541 | } else |
| 1542 | // A user declared getter will be synthesize when @synthesize of |
| 1543 | // the property with the same name is seen in the @implementation |
| 1544 | GetterMethod->setSynthesized(true); |
| 1545 | property->setGetterMethodDecl(GetterMethod); |
| 1546 | |
| 1547 | // Skip setter if property is read-only. |
| 1548 | if (!property->isReadOnly()) { |
| 1549 | // Find the default setter and if one not found, add one. |
| 1550 | if (!SetterMethod) { |
| 1551 | // No instance method of same name as property setter name was found. |
| 1552 | // Declare a setter method and add it to the list of methods |
| 1553 | // for this class. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1554 | SourceLocation Loc = redeclaredProperty ? |
| 1555 | redeclaredProperty->getLocation() : |
| 1556 | property->getLocation(); |
| 1557 | |
| 1558 | SetterMethod = |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 1559 | ObjCMethodDecl::Create(Context, Loc, Loc, ArrayRef<SourceLocation>(), |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1560 | property->getSetterName(), Context.VoidTy, 0, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 1561 | CD, /*isInstance=*/true, /*isVariadic=*/false, |
| 1562 | /*isSynthesized=*/true, |
| 1563 | /*isImplicitlyDeclared=*/true, |
| 1564 | /*isDefined=*/false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1565 | (property->getPropertyImplementation() == |
| 1566 | ObjCPropertyDecl::Optional) ? |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1567 | ObjCMethodDecl::Optional : |
| 1568 | ObjCMethodDecl::Required); |
| 1569 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1570 | // Invent the arguments for the setter. We don't bother making a |
| 1571 | // nice name for the argument. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1572 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
| 1573 | Loc, Loc, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1574 | property->getIdentifier(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1575 | property->getType().getUnqualifiedType(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1576 | /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1577 | SC_None, |
| 1578 | SC_None, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1579 | 0); |
Argyrios Kyrtzidis | da92a7f | 2011-10-03 06:36:29 +0000 | [diff] [blame] | 1580 | SetterMethod->setMethodParams(Context, &Argument, 1); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1581 | |
| 1582 | AddPropertyAttrs(*this, SetterMethod, property); |
| 1583 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1584 | CD->addDecl(SetterMethod); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1585 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 1586 | // and the real context should be the same. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1587 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1588 | SetterMethod->setLexicalDeclContext(lexicalDC); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1589 | } else |
| 1590 | // A user declared setter will be synthesize when @synthesize of |
| 1591 | // the property with the same name is seen in the @implementation |
| 1592 | SetterMethod->setSynthesized(true); |
| 1593 | property->setSetterMethodDecl(SetterMethod); |
| 1594 | } |
| 1595 | // Add any synthesized methods to the global pool. This allows us to |
| 1596 | // handle the following, which is supported by GCC (and part of the design). |
| 1597 | // |
| 1598 | // @interface Foo |
| 1599 | // @property double bar; |
| 1600 | // @end |
| 1601 | // |
| 1602 | // void thisIsUnfortunate() { |
| 1603 | // id foo; |
| 1604 | // double bar = [foo bar]; |
| 1605 | // } |
| 1606 | // |
| 1607 | if (GetterMethod) |
| 1608 | AddInstanceMethodToGlobalPool(GetterMethod); |
| 1609 | if (SetterMethod) |
| 1610 | AddInstanceMethodToGlobalPool(SetterMethod); |
| 1611 | } |
| 1612 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1613 | void Sema::CheckObjCPropertyAttributes(Decl *PDecl, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1614 | SourceLocation Loc, |
| 1615 | unsigned &Attributes) { |
| 1616 | // FIXME: Improve the reported location. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1617 | if (!PDecl || PDecl->isInvalidDecl()) |
Ted Kremenek | 5fcd52a | 2010-04-05 22:39:42 +0000 | [diff] [blame] | 1618 | return; |
| 1619 | |
| 1620 | ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1621 | QualType PropertyTy = PropertyDecl->getType(); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1622 | |
| 1623 | // readonly and readwrite/assign/retain/copy conflict. |
| 1624 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1625 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 1626 | ObjCDeclSpec::DQ_PR_assign | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1627 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1628 | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1629 | ObjCDeclSpec::DQ_PR_retain | |
| 1630 | ObjCDeclSpec::DQ_PR_strong))) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1631 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? |
| 1632 | "readwrite" : |
| 1633 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
| 1634 | "assign" : |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1635 | (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ? |
| 1636 | "unsafe_unretained" : |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1637 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
| 1638 | "copy" : "retain"; |
| 1639 | |
| 1640 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? |
| 1641 | diag::err_objc_property_attr_mutually_exclusive : |
| 1642 | diag::warn_objc_property_attr_mutually_exclusive) |
| 1643 | << "readonly" << which; |
| 1644 | } |
| 1645 | |
| 1646 | // Check for copy or retain on non-object types. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1647 | if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
| 1648 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) && |
| 1649 | !PropertyTy->isObjCRetainableType() && |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1650 | !PropertyDecl->getAttr<ObjCNSObjectAttr>()) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1651 | Diag(Loc, diag::err_objc_property_requires_object) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1652 | << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" : |
| 1653 | Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)"); |
| 1654 | Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
| 1655 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | // Check for more than one of { assign, copy, retain }. |
| 1659 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 1660 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1661 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1662 | << "assign" << "copy"; |
| 1663 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
| 1664 | } |
| 1665 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
| 1666 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1667 | << "assign" << "retain"; |
| 1668 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
| 1669 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1670 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
| 1671 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1672 | << "assign" << "strong"; |
| 1673 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
| 1674 | } |
| 1675 | if (getLangOptions().ObjCAutoRefCount && |
| 1676 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
| 1677 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1678 | << "assign" << "weak"; |
| 1679 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
| 1680 | } |
| 1681 | } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) { |
| 1682 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1683 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1684 | << "unsafe_unretained" << "copy"; |
| 1685 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
| 1686 | } |
| 1687 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
| 1688 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1689 | << "unsafe_unretained" << "retain"; |
| 1690 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
| 1691 | } |
| 1692 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
| 1693 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1694 | << "unsafe_unretained" << "strong"; |
| 1695 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
| 1696 | } |
| 1697 | if (getLangOptions().ObjCAutoRefCount && |
| 1698 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
| 1699 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1700 | << "unsafe_unretained" << "weak"; |
| 1701 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
| 1702 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1703 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1704 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
| 1705 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1706 | << "copy" << "retain"; |
| 1707 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
| 1708 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1709 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
| 1710 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1711 | << "copy" << "strong"; |
| 1712 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
| 1713 | } |
| 1714 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) { |
| 1715 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1716 | << "copy" << "weak"; |
| 1717 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
| 1718 | } |
| 1719 | } |
| 1720 | else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 1721 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
| 1722 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1723 | << "retain" << "weak"; |
Fariborz Jahanian | 528a499 | 2011-09-14 18:03:46 +0000 | [diff] [blame] | 1724 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1725 | } |
| 1726 | else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 1727 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
| 1728 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1729 | << "strong" << "weak"; |
| 1730 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | // Warn if user supplied no assignment attribute, property is |
| 1734 | // readwrite, and this is an object type. |
| 1735 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1736 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 1737 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong | |
| 1738 | ObjCDeclSpec::DQ_PR_weak)) && |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1739 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1740 | PropertyTy->isObjCObjectPointerType()) { |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 1741 | if (getLangOptions().ObjCAutoRefCount) |
| 1742 | // With arc, @property definitions should default to (strong) when |
| 1743 | // not specified |
| 1744 | PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 1745 | else { |
| 1746 | // Skip this warning in gc-only mode. |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1747 | if (getLangOptions().getGC() != LangOptions::GCOnly) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 1748 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1749 | |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 1750 | // If non-gc code warn that this is likely inappropriate. |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1751 | if (getLangOptions().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 1752 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
| 1753 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1754 | |
| 1755 | // FIXME: Implement warning dependent on NSCopying being |
| 1756 | // implemented. See also: |
| 1757 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 1758 | // (please trim this list while you are at it). |
| 1759 | } |
| 1760 | |
| 1761 | if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) |
Fariborz Jahanian | 2b77cb8 | 2011-01-05 23:00:04 +0000 | [diff] [blame] | 1762 | &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1763 | && getLangOptions().getGC() == LangOptions::GCOnly |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1764 | && PropertyTy->isBlockPointerType()) |
| 1765 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); |
Fariborz Jahanian | 528a499 | 2011-09-14 18:03:46 +0000 | [diff] [blame] | 1766 | else if (getLangOptions().ObjCAutoRefCount && |
| 1767 | (Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 1768 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1769 | !(Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 1770 | PropertyTy->isBlockPointerType()) |
| 1771 | Diag(Loc, diag::warn_objc_property_retain_of_block); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1772 | } |