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