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