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