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