blob: 95fbde39af0cbbabae6b4d4a74d145bcf19fc21b [file] [log] [blame]
Ted Kremenek9d64c152010-03-12 00:38:38 +00001//===--- 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 McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
Fariborz Jahanian17cb3262010-05-05 21:52:17 +000018#include "clang/AST/ExprObjC.h"
Fariborz Jahanian57e264e2011-10-06 18:38:18 +000019#include "clang/AST/ExprCXX.h"
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +000020#include "clang/AST/ASTMutationListener.h"
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Basic/SourceManager.h"
John McCall50df6ae2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000025
26using namespace clang;
27
Ted Kremenek28685ab2010-03-12 00:46:40 +000028//===----------------------------------------------------------------------===//
29// Grammar actions.
30//===----------------------------------------------------------------------===//
31
John McCall265941b2011-09-13 18:31:23 +000032/// getImpliedARCOwnership - Given a set of property attributes and a
33/// type, infer an expected lifetime. The type's ownership qualification
34/// is not considered.
35///
36/// Returns OCL_None if the attributes as stated do not imply an ownership.
37/// Never returns OCL_Autoreleasing.
38static Qualifiers::ObjCLifetime getImpliedARCOwnership(
39 ObjCPropertyDecl::PropertyAttributeKind attrs,
40 QualType type) {
41 // retain, strong, copy, weak, and unsafe_unretained are only legal
42 // on properties of retainable pointer type.
43 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
44 ObjCPropertyDecl::OBJC_PR_strong |
45 ObjCPropertyDecl::OBJC_PR_copy)) {
Fariborz Jahanian5fa065b2011-10-13 23:45:45 +000046 return type->getObjCARCImplicitLifetime();
John McCall265941b2011-09-13 18:31:23 +000047 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
48 return Qualifiers::OCL_Weak;
49 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
50 return Qualifiers::OCL_ExplicitNone;
51 }
52
53 // assign can appear on other types, so we have to check the
54 // property type.
55 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
56 type->isObjCRetainableType()) {
57 return Qualifiers::OCL_ExplicitNone;
58 }
59
60 return Qualifiers::OCL_None;
61}
62
John McCallf85e1932011-06-15 23:02:42 +000063/// Check the internal consistency of a property declaration.
64static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
65 if (property->isInvalidDecl()) return;
66
67 ObjCPropertyDecl::PropertyAttributeKind propertyKind
68 = property->getPropertyAttributes();
69 Qualifiers::ObjCLifetime propertyLifetime
70 = property->getType().getObjCLifetime();
71
72 // Nothing to do if we don't have a lifetime.
73 if (propertyLifetime == Qualifiers::OCL_None) return;
74
John McCall265941b2011-09-13 18:31:23 +000075 Qualifiers::ObjCLifetime expectedLifetime
76 = getImpliedARCOwnership(propertyKind, property->getType());
77 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000078 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000079 // attribute. That's okay, but restore reasonable invariants by
80 // setting the property attribute according to the lifetime
81 // qualifier.
82 ObjCPropertyDecl::PropertyAttributeKind attr;
83 if (propertyLifetime == Qualifiers::OCL_Strong) {
84 attr = ObjCPropertyDecl::OBJC_PR_strong;
85 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
86 attr = ObjCPropertyDecl::OBJC_PR_weak;
87 } else {
88 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
89 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
90 }
91 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000092 return;
93 }
94
95 if (propertyLifetime == expectedLifetime) return;
96
97 property->setInvalidDecl();
98 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000099 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000100 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +0000101 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +0000102 << propertyLifetime;
103}
104
John McCalld226f652010-08-21 09:40:31 +0000105Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000106 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000107 FieldDeclarator &FD,
108 ObjCDeclSpec &ODS,
109 Selector GetterSel,
110 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000111 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000112 tok::ObjCKeywordKind MethodImplKind,
113 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000114 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000115 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
116 QualType T = TSI->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +0000117 if ((getLangOpts().getGC() != LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +0000118 T.isObjCGCWeak()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +0000119 (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +0000120 T.getObjCLifetime() == Qualifiers::OCL_Weak))
121 Attributes |= ObjCDeclSpec::DQ_PR_weak;
122
Ted Kremenek28685ab2010-03-12 00:46:40 +0000123 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
124 // default is readwrite!
125 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
126 // property is defaulted to 'assign' if it is readwrite and is
127 // not retain or copy
128 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
129 (isReadWrite &&
130 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +0000131 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
132 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
133 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
134 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000135
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000136 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000137 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000138
Ted Kremenek28685ab2010-03-12 00:46:40 +0000139 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000140 if (CDecl->IsClassExtension()) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000141 Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000142 FD, GetterSel, SetterSel,
143 isAssign, isReadWrite,
144 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000145 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000146 isOverridingProperty, TSI,
147 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000148 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000149 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
David Blaikie4e4d0842012-03-11 07:00:24 +0000150 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000151 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
152 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000153 return Res;
154 }
155
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000156 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
John McCallf85e1932011-06-15 23:02:42 +0000157 GetterSel, SetterSel,
158 isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000159 Attributes,
160 ODS.getPropertyAttributes(),
161 TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000162 if (lexicalDC)
163 Res->setLexicalDeclContext(lexicalDC);
164
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000165 // Validate the attributes on the @property.
166 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000167
David Blaikie4e4d0842012-03-11 07:00:24 +0000168 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000169 checkARCPropertyDecl(*this, Res);
170
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000171 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000172}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000173
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000174static ObjCPropertyDecl::PropertyAttributeKind
175makePropertyAttributesAsWritten(unsigned Attributes) {
176 unsigned attributesAsWritten = 0;
177 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
178 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
179 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
180 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
181 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
182 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
183 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
184 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
185 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
186 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
187 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
188 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
189 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
190 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
191 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
192 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
193 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
194 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
195 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
196 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
197 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
198 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
199 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
200 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
201
202 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
203}
204
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000205static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000206 SourceLocation LParenLoc, SourceLocation &Loc) {
207 if (LParenLoc.isMacroID())
208 return false;
209
210 SourceManager &SM = Context.getSourceManager();
211 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
212 // Try to load the file buffer.
213 bool invalidTemp = false;
214 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
215 if (invalidTemp)
216 return false;
217 const char *tokenBegin = file.data() + locInfo.second;
218
219 // Lex from the start of the given location.
220 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
221 Context.getLangOpts(),
222 file.begin(), tokenBegin, file.end());
223 Token Tok;
224 do {
225 lexer.LexFromRawLexer(Tok);
226 if (Tok.is(tok::raw_identifier) &&
227 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
228 Loc = Tok.getLocation();
229 return true;
230 }
231 } while (Tok.isNot(tok::r_paren));
232 return false;
233
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000234}
235
John McCalld226f652010-08-21 09:40:31 +0000236Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000237Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000238 SourceLocation AtLoc,
239 SourceLocation LParenLoc,
240 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000241 Selector GetterSel, Selector SetterSel,
242 const bool isAssign,
243 const bool isReadWrite,
244 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000245 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000246 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000247 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000248 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000249 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000250 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000251 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000252 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000253 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
254
255 if (CCPrimary)
256 // Check for duplicate declaration of this property in current and
257 // other class extensions.
258 for (const ObjCCategoryDecl *ClsExtDecl =
259 CCPrimary->getFirstClassExtension();
260 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
261 if (ObjCPropertyDecl *prevDecl =
262 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
263 Diag(AtLoc, diag::err_duplicate_property);
264 Diag(prevDecl->getLocation(), diag::note_property_declare);
265 return 0;
266 }
267 }
268
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000269 // Create a new ObjCPropertyDecl with the DeclContext being
270 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000271 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000272 ObjCPropertyDecl *PDecl =
273 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000274 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000275 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000276 makePropertyAttributesAsWritten(AttributesAsWritten));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000277 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
278 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
279 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
280 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000281 // Set setter/getter selector name. Needed later.
282 PDecl->setGetterName(GetterSel);
283 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000284 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000285 DC->addDecl(PDecl);
286
287 // We need to look in the @interface to see if the @property was
288 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000289 if (!CCPrimary) {
290 Diag(CDecl->getLocation(), diag::err_continuation_class);
291 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000292 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000293 }
294
295 // Find the property in continuation class's primary class only.
296 ObjCPropertyDecl *PIDecl =
297 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
298
299 if (!PIDecl) {
300 // No matching property found in the primary class. Just fall thru
301 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000302 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000303 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000304 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000305 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000306
307 // A case of continuation class adding a new property in the class. This
308 // is not what it was meant for. However, gcc supports it and so should we.
309 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000310 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000311 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000312 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
313 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000314 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000315 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
316 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000317 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000318 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
319 bool IncompatibleObjC = false;
320 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000321 // Relax the strict type matching for property type in continuation class.
322 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000323 // as it narrows the object type in its primary class property. Note that
324 // this conversion is safe only because the wider type is for a 'readonly'
325 // property in primary class and 'narrowed' type for a 'readwrite' property
326 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000327 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
328 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
329 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
330 ConvertedType, IncompatibleObjC))
331 || IncompatibleObjC) {
332 Diag(AtLoc,
333 diag::err_type_mismatch_continuation_class) << PDecl->getType();
334 Diag(PIDecl->getLocation(), diag::note_property_declare);
335 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000336 }
337
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000338 // The property 'PIDecl's readonly attribute will be over-ridden
339 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000340 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000341 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
342 unsigned retainCopyNonatomic =
343 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000344 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000345 ObjCPropertyDecl::OBJC_PR_copy |
346 ObjCPropertyDecl::OBJC_PR_nonatomic);
347 if ((Attributes & retainCopyNonatomic) !=
348 (PIkind & retainCopyNonatomic)) {
349 Diag(AtLoc, diag::warn_property_attr_mismatch);
350 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000351 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000352 DeclContext *DC = cast<DeclContext>(CCPrimary);
353 if (!ObjCPropertyDecl::findPropertyDecl(DC,
354 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000355 // Protocol is not in the primary class. Must build one for it.
356 ObjCDeclSpec ProtocolPropertyODS;
357 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
358 // and ObjCPropertyDecl::PropertyAttributeKind have identical
359 // values. Should consolidate both into one enum type.
360 ProtocolPropertyODS.
361 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
362 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000363 // Must re-establish the context from class extension to primary
364 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000365 ContextRAII SavedContext(*this, CCPrimary);
366
John McCalld226f652010-08-21 09:40:31 +0000367 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000368 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000369 PIDecl->getGetterName(),
370 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000371 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000372 MethodImplKind,
373 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000374 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000375 }
376 PIDecl->makeitReadWriteAttribute();
377 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
378 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000379 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
380 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000381 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
382 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
383 PIDecl->setSetterName(SetterSel);
384 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000385 // Tailor the diagnostics for the common case where a readwrite
386 // property is declared both in the @interface and the continuation.
387 // This is a common error where the user often intended the original
388 // declaration to be readonly.
389 unsigned diag =
390 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
391 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
392 ? diag::err_use_continuation_class_redeclaration_readwrite
393 : diag::err_use_continuation_class;
394 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000395 << CCPrimary->getDeclName();
396 Diag(PIDecl->getLocation(), diag::note_property_declare);
397 }
398 *isOverridingProperty = true;
399 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000400 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000401 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
402 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000403 if (ASTMutationListener *L = Context.getASTMutationListener())
404 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000405 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000406}
407
408ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
409 ObjCContainerDecl *CDecl,
410 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000411 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000412 FieldDeclarator &FD,
413 Selector GetterSel,
414 Selector SetterSel,
415 const bool isAssign,
416 const bool isReadWrite,
417 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000418 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000419 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000420 tok::ObjCKeywordKind MethodImplKind,
421 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000422 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000423 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000424
425 // Issue a warning if property is 'assign' as default and its object, which is
426 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000427 if (getLangOpts().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000428 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000429 if (const ObjCObjectPointerType *ObjPtrTy =
430 T->getAs<ObjCObjectPointerType>()) {
431 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
432 if (IDecl)
433 if (ObjCProtocolDecl* PNSCopying =
434 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
435 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
436 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000437 }
John McCallc12c5bb2010-05-15 11:32:37 +0000438 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000439 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
440
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000441 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000442 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
443 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000444 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000445
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000446 if (ObjCPropertyDecl *prevDecl =
447 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000448 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000449 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000450 PDecl->setInvalidDecl();
451 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000452 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000453 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000454 if (lexicalDC)
455 PDecl->setLexicalDeclContext(lexicalDC);
456 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000457
458 if (T->isArrayType() || T->isFunctionType()) {
459 Diag(AtLoc, diag::err_property_type) << T;
460 PDecl->setInvalidDecl();
461 }
462
463 ProcessDeclAttributes(S, PDecl, FD.D);
464
465 // Regardless of setter/getter attribute, we save the default getter/setter
466 // selector names in anticipation of declaration of setter/getter methods.
467 PDecl->setGetterName(GetterSel);
468 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000469 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000470 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000471
Ted Kremenek28685ab2010-03-12 00:46:40 +0000472 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
473 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
474
475 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
476 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
477
478 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
479 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
480
481 if (isReadWrite)
482 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
483
484 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
485 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
486
John McCallf85e1932011-06-15 23:02:42 +0000487 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
488 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
489
490 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
491 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
492
Ted Kremenek28685ab2010-03-12 00:46:40 +0000493 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
494 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
495
John McCallf85e1932011-06-15 23:02:42 +0000496 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
497 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
498
Ted Kremenek28685ab2010-03-12 00:46:40 +0000499 if (isAssign)
500 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
501
John McCall265941b2011-09-13 18:31:23 +0000502 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000503 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
504 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000505 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000506 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000507
John McCallf85e1932011-06-15 23:02:42 +0000508 // 'unsafe_unretained' is alias for 'assign'.
509 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
510 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
511 if (isAssign)
512 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
513
Ted Kremenek28685ab2010-03-12 00:46:40 +0000514 if (MethodImplKind == tok::objc_required)
515 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
516 else if (MethodImplKind == tok::objc_optional)
517 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000518
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000519 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000520}
521
John McCallf85e1932011-06-15 23:02:42 +0000522static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
523 ObjCPropertyDecl *property,
524 ObjCIvarDecl *ivar) {
525 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
526
John McCallf85e1932011-06-15 23:02:42 +0000527 QualType ivarType = ivar->getType();
528 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000529
John McCall265941b2011-09-13 18:31:23 +0000530 // The lifetime implied by the property's attributes.
531 Qualifiers::ObjCLifetime propertyLifetime =
532 getImpliedARCOwnership(property->getPropertyAttributes(),
533 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000534
John McCall265941b2011-09-13 18:31:23 +0000535 // We're fine if they match.
536 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000537
John McCall265941b2011-09-13 18:31:23 +0000538 // These aren't valid lifetimes for object ivars; don't diagnose twice.
539 if (ivarLifetime == Qualifiers::OCL_None ||
540 ivarLifetime == Qualifiers::OCL_Autoreleasing)
541 return;
John McCallf85e1932011-06-15 23:02:42 +0000542
John McCall265941b2011-09-13 18:31:23 +0000543 switch (propertyLifetime) {
544 case Qualifiers::OCL_Strong:
545 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
546 << property->getDeclName()
547 << ivar->getDeclName()
548 << ivarLifetime;
549 break;
John McCallf85e1932011-06-15 23:02:42 +0000550
John McCall265941b2011-09-13 18:31:23 +0000551 case Qualifiers::OCL_Weak:
552 S.Diag(propertyImplLoc, diag::error_weak_property)
553 << property->getDeclName()
554 << ivar->getDeclName();
555 break;
John McCallf85e1932011-06-15 23:02:42 +0000556
John McCall265941b2011-09-13 18:31:23 +0000557 case Qualifiers::OCL_ExplicitNone:
558 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
559 << property->getDeclName()
560 << ivar->getDeclName()
561 << ((property->getPropertyAttributesAsWritten()
562 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
563 break;
John McCallf85e1932011-06-15 23:02:42 +0000564
John McCall265941b2011-09-13 18:31:23 +0000565 case Qualifiers::OCL_Autoreleasing:
566 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000567
John McCall265941b2011-09-13 18:31:23 +0000568 case Qualifiers::OCL_None:
569 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000570 return;
571 }
572
573 S.Diag(property->getLocation(), diag::note_property_declare);
574}
575
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000576/// setImpliedPropertyAttributeForReadOnlyProperty -
577/// This routine evaludates life-time attributes for a 'readonly'
578/// property with no known lifetime of its own, using backing
579/// 'ivar's attribute, if any. If no backing 'ivar', property's
580/// life-time is assumed 'strong'.
581static void setImpliedPropertyAttributeForReadOnlyProperty(
582 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
583 Qualifiers::ObjCLifetime propertyLifetime =
584 getImpliedARCOwnership(property->getPropertyAttributes(),
585 property->getType());
586 if (propertyLifetime != Qualifiers::OCL_None)
587 return;
588
589 if (!ivar) {
590 // if no backing ivar, make property 'strong'.
591 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
592 return;
593 }
594 // property assumes owenership of backing ivar.
595 QualType ivarType = ivar->getType();
596 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
597 if (ivarLifetime == Qualifiers::OCL_Strong)
598 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
599 else if (ivarLifetime == Qualifiers::OCL_Weak)
600 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
601 return;
602}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000603
604/// ActOnPropertyImplDecl - This routine performs semantic checks and
605/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000606/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000607///
John McCalld226f652010-08-21 09:40:31 +0000608Decl *Sema::ActOnPropertyImplDecl(Scope *S,
609 SourceLocation AtLoc,
610 SourceLocation PropertyLoc,
611 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000612 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000613 IdentifierInfo *PropertyIvar,
614 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000615 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000616 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000617 // Make sure we have a context for the property implementation declaration.
618 if (!ClassImpDecl) {
619 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000620 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000621 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000622 if (PropertyIvarLoc.isInvalid())
623 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000624 SourceLocation PropertyDiagLoc = PropertyLoc;
625 if (PropertyDiagLoc.isInvalid())
626 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000627 ObjCPropertyDecl *property = 0;
628 ObjCInterfaceDecl* IDecl = 0;
629 // Find the class or category class where this property must have
630 // a declaration.
631 ObjCImplementationDecl *IC = 0;
632 ObjCCategoryImplDecl* CatImplClass = 0;
633 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
634 IDecl = IC->getClassInterface();
635 // We always synthesize an interface for an implementation
636 // without an interface decl. So, IDecl is always non-zero.
637 assert(IDecl &&
638 "ActOnPropertyImplDecl - @implementation without @interface");
639
640 // Look for this property declaration in the @implementation's @interface
641 property = IDecl->FindPropertyDeclaration(PropertyId);
642 if (!property) {
643 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000644 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000645 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000646 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000647 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
648 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000649 if (AtLoc.isValid())
650 Diag(AtLoc, diag::warn_implicit_atomic_property);
651 else
652 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
653 Diag(property->getLocation(), diag::note_property_declare);
654 }
655
Ted Kremenek28685ab2010-03-12 00:46:40 +0000656 if (const ObjCCategoryDecl *CD =
657 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
658 if (!CD->IsClassExtension()) {
659 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
660 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000661 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000662 }
663 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000664
665 if (Synthesize&&
666 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
667 property->hasAttr<IBOutletAttr>() &&
668 !AtLoc.isValid()) {
669 unsigned rwPIKind = (PIkind | ObjCPropertyDecl::OBJC_PR_readwrite);
670 rwPIKind &= (~ObjCPropertyDecl::OBJC_PR_readonly);
671 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
672 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000673 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000674 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000675 property->getLParenLoc(), readonlyLoc)) {
676 SourceLocation endLoc =
677 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
678 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
679 Diag(property->getLocation(),
680 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
681 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
682 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000683 }
684
Ted Kremenek28685ab2010-03-12 00:46:40 +0000685 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
686 if (Synthesize) {
687 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000688 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000689 }
690 IDecl = CatImplClass->getClassInterface();
691 if (!IDecl) {
692 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000693 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000694 }
695 ObjCCategoryDecl *Category =
696 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
697
698 // If category for this implementation not found, it is an error which
699 // has already been reported eralier.
700 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000701 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000702 // Look for this property declaration in @implementation's category
703 property = Category->FindPropertyDeclaration(PropertyId);
704 if (!property) {
705 Diag(PropertyLoc, diag::error_bad_category_property_decl)
706 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000707 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000708 }
709 } else {
710 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000711 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000712 }
713 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000714 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000715 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000716 // Check that we have a valid, previously declared ivar for @synthesize
717 if (Synthesize) {
718 // @synthesize
719 if (!PropertyIvar)
720 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000721 // Check that this is a previously declared 'ivar' in 'IDecl' interface
722 ObjCInterfaceDecl *ClassDeclared;
723 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
724 QualType PropType = property->getType();
725 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000726
727 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000728 diag::err_incomplete_synthesized_property,
729 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000730 Diag(property->getLocation(), diag::note_property_declare);
731 CompleteTypeErr = true;
732 }
733
David Blaikie4e4d0842012-03-11 07:00:24 +0000734 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000735 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000736 ObjCPropertyDecl::OBJC_PR_readonly) &&
737 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000738 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
739 }
740
John McCallf85e1932011-06-15 23:02:42 +0000741 ObjCPropertyDecl::PropertyAttributeKind kind
742 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000743
744 // Add GC __weak to the ivar type if the property is weak.
745 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000746 getLangOpts().getGC() != LangOptions::NonGC) {
747 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000748 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000749 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000750 Diag(property->getLocation(), diag::note_property_declare);
751 } else {
752 PropertyIvarType =
753 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000754 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000755 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000756 if (AtLoc.isInvalid()) {
757 // Check when default synthesizing a property that there is
758 // an ivar matching property name and issue warning; since this
759 // is the most common case of not using an ivar used for backing
760 // property in non-default synthesis case.
761 ObjCInterfaceDecl *ClassDeclared=0;
762 ObjCIvarDecl *originalIvar =
763 IDecl->lookupInstanceVariable(property->getIdentifier(),
764 ClassDeclared);
765 if (originalIvar) {
766 Diag(PropertyDiagLoc,
767 diag::warn_autosynthesis_property_ivar_match)
768 << property->getName() << (Ivar == 0) << PropertyIvar->getName()
769 << originalIvar->getName();
770 Diag(property->getLocation(), diag::note_property_declare);
771 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000772 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000773 }
774
775 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000776 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000777 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000778 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000779 !PropertyIvarType.getObjCLifetime() &&
780 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000781
John McCall265941b2011-09-13 18:31:23 +0000782 // It's an error if we have to do this and the user didn't
783 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000784 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000785 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000786 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000787 diag::err_arc_objc_property_default_assign_on_object);
788 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000789 } else {
790 Qualifiers::ObjCLifetime lifetime =
791 getImpliedARCOwnership(kind, PropertyIvarType);
792 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000793 if (lifetime == Qualifiers::OCL_Weak) {
794 bool err = false;
795 if (const ObjCObjectPointerType *ObjT =
796 PropertyIvarType->getAs<ObjCObjectPointerType>())
797 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000798 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000799 Diag(property->getLocation(), diag::note_property_declare);
800 err = true;
801 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000802 if (!err && !getLangOpts().ObjCRuntimeHasWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000803 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000804 Diag(property->getLocation(), diag::note_property_declare);
805 }
John McCallf85e1932011-06-15 23:02:42 +0000806 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000807
John McCallf85e1932011-06-15 23:02:42 +0000808 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000809 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000810 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
811 }
John McCallf85e1932011-06-15 23:02:42 +0000812 }
813
814 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000815 !getLangOpts().ObjCAutoRefCount &&
816 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000817 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000818 Diag(property->getLocation(), diag::note_property_declare);
819 }
820
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000821 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000822 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000823 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000824 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000825 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000826 if (CompleteTypeErr)
827 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000828 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000829 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000830 property->setPropertyIvarDecl(Ivar);
831
John McCall260611a2012-06-20 06:18:46 +0000832 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000833 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
834 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000835 // Note! I deliberately want it to fall thru so, we have a
836 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000837 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000838 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000839 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000840 << property->getDeclName() << Ivar->getDeclName()
841 << ClassDeclared->getDeclName();
842 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000843 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000844 // Note! I deliberately want it to fall thru so more errors are caught.
845 }
846 QualType IvarType = Context.getCanonicalType(Ivar->getType());
847
848 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000849 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
850 compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000851 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000852 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000853 compat =
854 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000855 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000856 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000857 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000858 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
859 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000860 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000861 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000862 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000863 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000864 << property->getDeclName() << PropType
865 << Ivar->getDeclName() << IvarType;
866 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000867 // Note! I deliberately want it to fall thru so, we have a
868 // a property implementation and to avoid future warnings.
869 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000870 else {
871 // FIXME! Rules for properties are somewhat different that those
872 // for assignments. Use a new routine to consolidate all cases;
873 // specifically for property redeclarations as well as for ivars.
874 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
875 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
876 if (lhsType != rhsType &&
877 lhsType->isArithmeticType()) {
878 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
879 << property->getDeclName() << PropType
880 << Ivar->getDeclName() << IvarType;
881 Diag(Ivar->getLocation(), diag::note_ivar_decl);
882 // Fall thru - see previous comment
883 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000884 }
885 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000886 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000887 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000888 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000889 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000890 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000891 // Fall thru - see previous comment
892 }
John McCallf85e1932011-06-15 23:02:42 +0000893 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000894 if ((property->getType()->isObjCObjectPointerType() ||
895 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000896 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000897 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000898 << property->getDeclName() << Ivar->getDeclName();
899 // Fall thru - see previous comment
900 }
901 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000902 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000903 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000904 } else if (PropertyIvar)
905 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +0000906 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000907
Ted Kremenek28685ab2010-03-12 00:46:40 +0000908 assert (property && "ActOnPropertyImplDecl - property declaration missing");
909 ObjCPropertyImplDecl *PIDecl =
910 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
911 property,
912 (Synthesize ?
913 ObjCPropertyImplDecl::Synthesize
914 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000915 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000916
Fariborz Jahanian74414712012-05-15 18:12:51 +0000917 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +0000918 PIDecl->setInvalidDecl();
919
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000920 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
921 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000922 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000923 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000924 // For Objective-C++, need to synthesize the AST for the IVAR object to be
925 // returned by the getter as it must conform to C++'s copy-return rules.
926 // FIXME. Eventually we want to do this for Objective-C as well.
927 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
928 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +0000929 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000930 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000931 Expr *IvarRefExpr =
932 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
933 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000934 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000935 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000936 SourceLocation(),
937 getterMethod->getResultType(),
938 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000939 SourceLocation(),
940 Owned(IvarRefExpr));
941 if (!Res.isInvalid()) {
942 Expr *ResExpr = Res.takeAs<Expr>();
943 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000944 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000945 PIDecl->setGetterCXXConstructor(ResExpr);
946 }
947 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000948 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
949 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
950 Diag(getterMethod->getLocation(),
951 diag::warn_property_getter_owning_mismatch);
952 Diag(property->getLocation(), diag::note_property_declare);
953 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000954 }
955 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
956 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000957 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
958 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000959 // FIXME. Eventually we want to do this for Objective-C as well.
960 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
961 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +0000962 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000963 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000964 Expr *lhs =
965 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
966 SelfExpr, true, true);
967 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
968 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +0000969 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +0000970 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +0000971 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000972 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000973 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000974 if (property->getPropertyAttributes() &
975 ObjCPropertyDecl::OBJC_PR_atomic) {
976 Expr *callExpr = Res.takeAs<Expr>();
977 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000978 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
979 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000980 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000981 if (property->getType()->isReferenceType()) {
982 Diag(PropertyLoc,
983 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000984 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000985 Diag(FuncDecl->getLocStart(),
986 diag::note_callee_decl) << FuncDecl;
987 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000988 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000989 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
990 }
991 }
992
Ted Kremenek28685ab2010-03-12 00:46:40 +0000993 if (IC) {
994 if (Synthesize)
995 if (ObjCPropertyImplDecl *PPIDecl =
996 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
997 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
998 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
999 << PropertyIvar;
1000 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1001 }
1002
1003 if (ObjCPropertyImplDecl *PPIDecl
1004 = IC->FindPropertyImplDecl(PropertyId)) {
1005 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1006 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001007 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001008 }
1009 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001010 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001011 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001012 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001013 // Diagnose if an ivar was lazily synthesdized due to a previous
1014 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001015 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001016 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001017 ObjCIvarDecl *Ivar = 0;
1018 if (!Synthesize)
1019 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1020 else {
1021 if (PropertyIvar && PropertyIvar != PropertyId)
1022 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1023 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001024 // Issue diagnostics only if Ivar belongs to current class.
1025 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001026 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001027 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1028 << PropertyId;
1029 Ivar->setInvalidDecl();
1030 }
1031 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001032 } else {
1033 if (Synthesize)
1034 if (ObjCPropertyImplDecl *PPIDecl =
1035 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001036 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001037 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1038 << PropertyIvar;
1039 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1040 }
1041
1042 if (ObjCPropertyImplDecl *PPIDecl =
1043 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001044 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001045 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001046 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001047 }
1048 CatImplClass->addPropertyImplementation(PIDecl);
1049 }
1050
John McCalld226f652010-08-21 09:40:31 +00001051 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001052}
1053
1054//===----------------------------------------------------------------------===//
1055// Helper methods.
1056//===----------------------------------------------------------------------===//
1057
Ted Kremenek9d64c152010-03-12 00:38:38 +00001058/// DiagnosePropertyMismatch - Compares two properties for their
1059/// attributes and types and warns on a variety of inconsistencies.
1060///
1061void
1062Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1063 ObjCPropertyDecl *SuperProperty,
1064 const IdentifierInfo *inheritedName) {
1065 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1066 Property->getPropertyAttributes();
1067 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1068 SuperProperty->getPropertyAttributes();
1069 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1070 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1071 Diag(Property->getLocation(), diag::warn_readonly_property)
1072 << Property->getDeclName() << inheritedName;
1073 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1074 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1075 Diag(Property->getLocation(), diag::warn_property_attribute)
1076 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001077 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001078 unsigned CAttrRetain =
1079 (CAttr &
1080 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1081 unsigned SAttrRetain =
1082 (SAttr &
1083 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1084 bool CStrong = (CAttrRetain != 0);
1085 bool SStrong = (SAttrRetain != 0);
1086 if (CStrong != SStrong)
1087 Diag(Property->getLocation(), diag::warn_property_attribute)
1088 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1089 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001090
1091 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1092 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1093 Diag(Property->getLocation(), diag::warn_property_attribute)
1094 << Property->getDeclName() << "atomic" << inheritedName;
1095 if (Property->getSetterName() != SuperProperty->getSetterName())
1096 Diag(Property->getLocation(), diag::warn_property_attribute)
1097 << Property->getDeclName() << "setter" << inheritedName;
1098 if (Property->getGetterName() != SuperProperty->getGetterName())
1099 Diag(Property->getLocation(), diag::warn_property_attribute)
1100 << Property->getDeclName() << "getter" << inheritedName;
1101
1102 QualType LHSType =
1103 Context.getCanonicalType(SuperProperty->getType());
1104 QualType RHSType =
1105 Context.getCanonicalType(Property->getType());
1106
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001107 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001108 // Do cases not handled in above.
1109 // FIXME. For future support of covariant property types, revisit this.
1110 bool IncompatibleObjC = false;
1111 QualType ConvertedType;
1112 if (!isObjCPointerConversion(RHSType, LHSType,
1113 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001114 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001115 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1116 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001117 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1118 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001119 }
1120}
1121
1122bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1123 ObjCMethodDecl *GetterMethod,
1124 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001125 if (!GetterMethod)
1126 return false;
1127 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1128 QualType PropertyIvarType = property->getType().getNonReferenceType();
1129 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1130 if (!compat) {
1131 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1132 isa<ObjCObjectPointerType>(GetterType))
1133 compat =
1134 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001135 GetterType->getAs<ObjCObjectPointerType>(),
1136 PropertyIvarType->getAs<ObjCObjectPointerType>());
1137 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001138 != Compatible) {
1139 Diag(Loc, diag::error_property_accessor_type)
1140 << property->getDeclName() << PropertyIvarType
1141 << GetterMethod->getSelector() << GetterType;
1142 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1143 return true;
1144 } else {
1145 compat = true;
1146 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1147 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1148 if (lhsType != rhsType && lhsType->isArithmeticType())
1149 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001150 }
1151 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001152
1153 if (!compat) {
1154 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1155 << property->getDeclName()
1156 << GetterMethod->getSelector();
1157 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1158 return true;
1159 }
1160
Ted Kremenek9d64c152010-03-12 00:38:38 +00001161 return false;
1162}
1163
1164/// ComparePropertiesInBaseAndSuper - This routine compares property
1165/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001166/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001167///
1168void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1169 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1170 if (!SDecl)
1171 return;
1172 // FIXME: O(N^2)
1173 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1174 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001175 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001176 // Does property in super class has declaration in current class?
1177 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1178 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001179 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001180 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1181 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1182 SDecl->getIdentifier());
1183 }
1184 }
1185}
1186
1187/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1188/// of properties declared in a protocol and compares their attribute against
1189/// the same property declared in the class or category.
1190void
1191Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1192 ObjCProtocolDecl *PDecl) {
1193 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1194 if (!IDecl) {
1195 // Category
1196 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1197 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1198 if (!CatDecl->IsClassExtension())
1199 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1200 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001201 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001202 ObjCCategoryDecl::prop_iterator CP, CE;
1203 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001204 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001205 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001206 break;
1207 if (CP != CE)
1208 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001209 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001210 }
1211 return;
1212 }
1213 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1214 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001215 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001216 ObjCInterfaceDecl::prop_iterator CP, CE;
1217 // Is this property already in class's list of properties?
1218 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001219 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001220 break;
1221 if (CP != CE)
1222 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001223 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001224 }
1225}
1226
1227/// CompareProperties - This routine compares properties
1228/// declared in 'ClassOrProtocol' objects (which can be a class or an
1229/// inherited protocol with the list of properties for class/category 'CDecl'
1230///
John McCalld226f652010-08-21 09:40:31 +00001231void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1232 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001233 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1234
1235 if (!IDecl) {
1236 // Category
1237 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1238 assert (CatDecl && "CompareProperties");
1239 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1240 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1241 E = MDecl->protocol_end(); P != E; ++P)
1242 // Match properties of category with those of protocol (*P)
1243 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1244
1245 // Go thru the list of protocols for this category and recursively match
1246 // their properties with those in the category.
1247 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1248 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001249 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001250 } else {
1251 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1252 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1253 E = MD->protocol_end(); P != E; ++P)
1254 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1255 }
1256 return;
1257 }
1258
1259 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001260 for (ObjCInterfaceDecl::all_protocol_iterator
1261 P = MDecl->all_referenced_protocol_begin(),
1262 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001263 // Match properties of class IDecl with those of protocol (*P).
1264 MatchOneProtocolPropertiesInClass(IDecl, *P);
1265
1266 // Go thru the list of protocols for this class and recursively match
1267 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001268 for (ObjCInterfaceDecl::all_protocol_iterator
1269 P = IDecl->all_referenced_protocol_begin(),
1270 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001271 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001272 } else {
1273 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1274 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1275 E = MD->protocol_end(); P != E; ++P)
1276 MatchOneProtocolPropertiesInClass(IDecl, *P);
1277 }
1278}
1279
1280/// isPropertyReadonly - Return true if property is readonly, by searching
1281/// for the property in the class and in its categories and implementations
1282///
1283bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1284 ObjCInterfaceDecl *IDecl) {
1285 // by far the most common case.
1286 if (!PDecl->isReadOnly())
1287 return false;
1288 // Even if property is ready only, if interface has a user defined setter,
1289 // it is not considered read only.
1290 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1291 return false;
1292
1293 // Main class has the property as 'readonly'. Must search
1294 // through the category list to see if the property's
1295 // attribute has been over-ridden to 'readwrite'.
1296 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1297 Category; Category = Category->getNextClassCategory()) {
1298 // Even if property is ready only, if a category has a user defined setter,
1299 // it is not considered read only.
1300 if (Category->getInstanceMethod(PDecl->getSetterName()))
1301 return false;
1302 ObjCPropertyDecl *P =
1303 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1304 if (P && !P->isReadOnly())
1305 return false;
1306 }
1307
1308 // Also, check for definition of a setter method in the implementation if
1309 // all else failed.
1310 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1311 if (ObjCImplementationDecl *IMD =
1312 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1313 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1314 return false;
1315 } else if (ObjCCategoryImplDecl *CIMD =
1316 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1317 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1318 return false;
1319 }
1320 }
1321 // Lastly, look through the implementation (if one is in scope).
1322 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1323 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1324 return false;
1325 // If all fails, look at the super class.
1326 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1327 return isPropertyReadonly(PDecl, SIDecl);
1328 return true;
1329}
1330
1331/// CollectImmediateProperties - This routine collects all properties in
1332/// the class and its conforming protocols; but not those it its super class.
1333void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001334 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1335 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001336 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1337 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1338 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001339 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001340 PropMap[Prop->getIdentifier()] = Prop;
1341 }
1342 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001343 for (ObjCInterfaceDecl::all_protocol_iterator
1344 PI = IDecl->all_referenced_protocol_begin(),
1345 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001346 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001347 }
1348 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1349 if (!CATDecl->IsClassExtension())
1350 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1351 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001352 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001353 PropMap[Prop->getIdentifier()] = Prop;
1354 }
1355 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001356 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001357 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001358 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001359 }
1360 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1361 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1362 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001363 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001364 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1365 // Exclude property for protocols which conform to class's super-class,
1366 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001367 if (!PropertyFromSuper ||
1368 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001369 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1370 if (!PropEntry)
1371 PropEntry = Prop;
1372 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001373 }
1374 // scan through protocol's protocols.
1375 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1376 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001377 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001378 }
1379}
1380
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001381/// CollectClassPropertyImplementations - This routine collects list of
1382/// properties to be implemented in the class. This includes, class's
1383/// and its conforming protocols' properties.
1384static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1385 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1386 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1387 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1388 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001389 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001390 PropMap[Prop->getIdentifier()] = Prop;
1391 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001392 for (ObjCInterfaceDecl::all_protocol_iterator
1393 PI = IDecl->all_referenced_protocol_begin(),
1394 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001395 CollectClassPropertyImplementations((*PI), PropMap);
1396 }
1397 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1398 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1399 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001400 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanianac371502012-02-23 18:21:25 +00001401 if (!PropMap.count(Prop->getIdentifier()))
1402 PropMap[Prop->getIdentifier()] = Prop;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001403 }
1404 // scan through protocol's protocols.
1405 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1406 E = PDecl->protocol_end(); PI != E; ++PI)
1407 CollectClassPropertyImplementations((*PI), PropMap);
1408 }
1409}
1410
1411/// CollectSuperClassPropertyImplementations - This routine collects list of
1412/// properties to be implemented in super class(s) and also coming from their
1413/// conforming protocols.
1414static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1415 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1416 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1417 while (SDecl) {
1418 CollectClassPropertyImplementations(SDecl, PropMap);
1419 SDecl = SDecl->getSuperClass();
1420 }
1421 }
1422}
1423
Ted Kremenek9d64c152010-03-12 00:38:38 +00001424/// LookupPropertyDecl - Looks up a property in the current class and all
1425/// its protocols.
1426ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1427 IdentifierInfo *II) {
1428 if (const ObjCInterfaceDecl *IDecl =
1429 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1430 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1431 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001432 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001433 if (Prop->getIdentifier() == II)
1434 return Prop;
1435 }
1436 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001437 for (ObjCInterfaceDecl::all_protocol_iterator
1438 PI = IDecl->all_referenced_protocol_begin(),
1439 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001440 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1441 if (Prop)
1442 return Prop;
1443 }
1444 }
1445 else if (const ObjCProtocolDecl *PDecl =
1446 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1447 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1448 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001449 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001450 if (Prop->getIdentifier() == II)
1451 return Prop;
1452 }
1453 // scan through protocol's protocols.
1454 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1455 E = PDecl->protocol_end(); PI != E; ++PI) {
1456 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1457 if (Prop)
1458 return Prop;
1459 }
1460 }
1461 return 0;
1462}
1463
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001464static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1465 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001466 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001467 {
1468 llvm::raw_svector_ostream os(ivarName);
1469 os << '_' << Prop->getIdentifier()->getName();
1470 }
1471 return &Ctx.Idents.get(ivarName.str());
1472}
1473
James Dennett699c9042012-06-15 07:13:21 +00001474/// \brief Default synthesizes all properties which must be synthesized
1475/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001476void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1477 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001478
1479 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1480 CollectClassPropertyImplementations(IDecl, PropMap);
1481 if (PropMap.empty())
1482 return;
1483 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1484 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1485
1486 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1487 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1488 ObjCPropertyDecl *Prop = P->second;
1489 // If property to be implemented in the super class, ignore.
1490 if (SuperPropMap[Prop->getIdentifier()])
1491 continue;
1492 // Is there a matching propery synthesize/dynamic?
1493 if (Prop->isInvalidDecl() ||
1494 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1495 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1496 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001497 // Property may have been synthesized by user.
1498 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1499 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001500 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1501 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1502 continue;
1503 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1504 continue;
1505 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001506 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1507 // We won't auto-synthesize properties declared in protocols.
1508 Diag(IMPDecl->getLocation(),
1509 diag::warn_auto_synthesizing_protocol_property);
1510 Diag(Prop->getLocation(), diag::note_property_declare);
1511 continue;
1512 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001513
1514 // We use invalid SourceLocations for the synthesized ivars since they
1515 // aren't really synthesized at a particular location; they just exist.
1516 // Saying that they are located at the @implementation isn't really going
1517 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001518 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1519 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1520 true,
1521 /* property = */ Prop->getIdentifier(),
1522 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001523 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001524 if (PIDecl) {
1525 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001526 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001527 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001528 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001529}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001530
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001531void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001532 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001533 return;
1534 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1535 if (!IC)
1536 return;
1537 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001538 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001539 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001540}
1541
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001542void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001543 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001544 const SelectorSet &InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001545 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1546 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1547 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1548
Ted Kremenek9d64c152010-03-12 00:38:38 +00001549 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001550 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001551 if (PropMap.empty())
1552 return;
1553
1554 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1555 for (ObjCImplDecl::propimpl_iterator
1556 I = IMPDecl->propimpl_begin(),
1557 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001558 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001559
1560 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1561 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1562 ObjCPropertyDecl *Prop = P->second;
1563 // Is there a matching propery synthesize/dynamic?
1564 if (Prop->isInvalidDecl() ||
1565 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001566 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001567 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001568 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001569 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001570 isa<ObjCCategoryDecl>(CDecl) ?
1571 diag::warn_setter_getter_impl_required_in_category :
1572 diag::warn_setter_getter_impl_required)
1573 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001574 Diag(Prop->getLocation(),
1575 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001576 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001577 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001578 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001579 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1580
Ted Kremenek9d64c152010-03-12 00:38:38 +00001581 }
1582
1583 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001584 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001585 isa<ObjCCategoryDecl>(CDecl) ?
1586 diag::warn_setter_getter_impl_required_in_category :
1587 diag::warn_setter_getter_impl_required)
1588 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001589 Diag(Prop->getLocation(),
1590 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001591 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001592 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001593 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001594 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001595 }
1596 }
1597}
1598
1599void
1600Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1601 ObjCContainerDecl* IDecl) {
1602 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001603 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001604 return;
1605 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1606 E = IDecl->prop_end();
1607 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001608 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001609 ObjCMethodDecl *GetterMethod = 0;
1610 ObjCMethodDecl *SetterMethod = 0;
1611 bool LookedUpGetterSetter = false;
1612
Ted Kremenek9d64c152010-03-12 00:38:38 +00001613 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001614 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001615
John McCall265941b2011-09-13 18:31:23 +00001616 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1617 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001618 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1619 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1620 LookedUpGetterSetter = true;
1621 if (GetterMethod) {
1622 Diag(GetterMethod->getLocation(),
1623 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001624 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001625 Diag(Property->getLocation(), diag::note_property_declare);
1626 }
1627 if (SetterMethod) {
1628 Diag(SetterMethod->getLocation(),
1629 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001630 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001631 Diag(Property->getLocation(), diag::note_property_declare);
1632 }
1633 }
1634
Ted Kremenek9d64c152010-03-12 00:38:38 +00001635 // We only care about readwrite atomic property.
1636 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1637 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1638 continue;
1639 if (const ObjCPropertyImplDecl *PIDecl
1640 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1641 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1642 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001643 if (!LookedUpGetterSetter) {
1644 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1645 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1646 LookedUpGetterSetter = true;
1647 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001648 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1649 SourceLocation MethodLoc =
1650 (GetterMethod ? GetterMethod->getLocation()
1651 : SetterMethod->getLocation());
1652 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001653 << Property->getIdentifier() << (GetterMethod != 0)
1654 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001655 // fixit stuff.
1656 if (!AttributesAsWritten) {
1657 if (Property->getLParenLoc().isValid()) {
1658 // @property () ... case.
1659 SourceRange PropSourceRange(Property->getAtLoc(),
1660 Property->getLParenLoc());
1661 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1662 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1663 }
1664 else {
1665 //@property id etc.
1666 SourceLocation endLoc =
1667 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1668 endLoc = endLoc.getLocWithOffset(-1);
1669 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1670 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1671 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1672 }
1673 }
1674 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1675 // @property () ... case.
1676 SourceLocation endLoc = Property->getLParenLoc();
1677 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1678 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1679 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1680 }
1681 else
1682 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001683 Diag(Property->getLocation(), diag::note_property_declare);
1684 }
1685 }
1686 }
1687}
1688
John McCallf85e1932011-06-15 23:02:42 +00001689void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001690 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001691 return;
1692
1693 for (ObjCImplementationDecl::propimpl_iterator
1694 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001695 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001696 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1697 continue;
1698
1699 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001700 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1701 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001702 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1703 if (!method)
1704 continue;
1705 ObjCMethodFamily family = method->getMethodFamily();
1706 if (family == OMF_alloc || family == OMF_copy ||
1707 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001708 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001709 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1710 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001711 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001712 Diag(PD->getLocation(), diag::note_property_declare);
1713 }
1714 }
1715 }
1716}
1717
John McCall5de74d12010-11-10 07:01:40 +00001718/// AddPropertyAttrs - Propagates attributes from a property to the
1719/// implicitly-declared getter or setter for that property.
1720static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1721 ObjCPropertyDecl *Property) {
1722 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001723 for (Decl::attr_iterator A = Property->attr_begin(),
1724 AEnd = Property->attr_end();
1725 A != AEnd; ++A) {
1726 if (isa<DeprecatedAttr>(*A) ||
1727 isa<UnavailableAttr>(*A) ||
1728 isa<AvailabilityAttr>(*A))
1729 PropertyMethod->addAttr((*A)->clone(S.Context));
1730 }
John McCall5de74d12010-11-10 07:01:40 +00001731}
1732
Ted Kremenek9d64c152010-03-12 00:38:38 +00001733/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1734/// have the property type and issue diagnostics if they don't.
1735/// Also synthesize a getter/setter method if none exist (and update the
1736/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1737/// methods is the "right" thing to do.
1738void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001739 ObjCContainerDecl *CD,
1740 ObjCPropertyDecl *redeclaredProperty,
1741 ObjCContainerDecl *lexicalDC) {
1742
Ted Kremenek9d64c152010-03-12 00:38:38 +00001743 ObjCMethodDecl *GetterMethod, *SetterMethod;
1744
1745 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1746 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1747 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1748 property->getLocation());
1749
1750 if (SetterMethod) {
1751 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1752 property->getPropertyAttributes();
1753 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1754 Context.getCanonicalType(SetterMethod->getResultType()) !=
1755 Context.VoidTy)
1756 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1757 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001758 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001759 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1760 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001761 Diag(property->getLocation(),
1762 diag::warn_accessor_property_type_mismatch)
1763 << property->getDeclName()
1764 << SetterMethod->getSelector();
1765 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1766 }
1767 }
1768
1769 // Synthesize getter/setter methods if none exist.
1770 // Find the default getter and if one not found, add one.
1771 // FIXME: The synthesized property we set here is misleading. We almost always
1772 // synthesize these methods unless the user explicitly provided prototypes
1773 // (which is odd, but allowed). Sema should be typechecking that the
1774 // declarations jive in that situation (which it is not currently).
1775 if (!GetterMethod) {
1776 // No instance method of same name as property getter name was found.
1777 // Declare a getter method and add it to the list of methods
1778 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001779 SourceLocation Loc = redeclaredProperty ?
1780 redeclaredProperty->getLocation() :
1781 property->getLocation();
1782
1783 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1784 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001785 property->getType(), 0, CD, /*isInstance=*/true,
1786 /*isVariadic=*/false, /*isSynthesized=*/true,
1787 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001788 (property->getPropertyImplementation() ==
1789 ObjCPropertyDecl::Optional) ?
1790 ObjCMethodDecl::Optional :
1791 ObjCMethodDecl::Required);
1792 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001793
1794 AddPropertyAttrs(*this, GetterMethod, property);
1795
Ted Kremenek23173d72010-05-18 21:09:07 +00001796 // FIXME: Eventually this shouldn't be needed, as the lexical context
1797 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001798 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001799 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001800 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1801 GetterMethod->addAttr(
1802 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001803 } else
1804 // A user declared getter will be synthesize when @synthesize of
1805 // the property with the same name is seen in the @implementation
1806 GetterMethod->setSynthesized(true);
1807 property->setGetterMethodDecl(GetterMethod);
1808
1809 // Skip setter if property is read-only.
1810 if (!property->isReadOnly()) {
1811 // Find the default setter and if one not found, add one.
1812 if (!SetterMethod) {
1813 // No instance method of same name as property setter name was found.
1814 // Declare a setter method and add it to the list of methods
1815 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001816 SourceLocation Loc = redeclaredProperty ?
1817 redeclaredProperty->getLocation() :
1818 property->getLocation();
1819
1820 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001821 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001822 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001823 CD, /*isInstance=*/true, /*isVariadic=*/false,
1824 /*isSynthesized=*/true,
1825 /*isImplicitlyDeclared=*/true,
1826 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001827 (property->getPropertyImplementation() ==
1828 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001829 ObjCMethodDecl::Optional :
1830 ObjCMethodDecl::Required);
1831
Ted Kremenek9d64c152010-03-12 00:38:38 +00001832 // Invent the arguments for the setter. We don't bother making a
1833 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001834 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1835 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001836 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001837 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001838 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001839 SC_None,
1840 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001841 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001842 SetterMethod->setMethodParams(Context, Argument,
1843 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001844
1845 AddPropertyAttrs(*this, SetterMethod, property);
1846
Ted Kremenek9d64c152010-03-12 00:38:38 +00001847 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001848 // FIXME: Eventually this shouldn't be needed, as the lexical context
1849 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001850 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001851 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001852 } else
1853 // A user declared setter will be synthesize when @synthesize of
1854 // the property with the same name is seen in the @implementation
1855 SetterMethod->setSynthesized(true);
1856 property->setSetterMethodDecl(SetterMethod);
1857 }
1858 // Add any synthesized methods to the global pool. This allows us to
1859 // handle the following, which is supported by GCC (and part of the design).
1860 //
1861 // @interface Foo
1862 // @property double bar;
1863 // @end
1864 //
1865 // void thisIsUnfortunate() {
1866 // id foo;
1867 // double bar = [foo bar];
1868 // }
1869 //
1870 if (GetterMethod)
1871 AddInstanceMethodToGlobalPool(GetterMethod);
1872 if (SetterMethod)
1873 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001874
1875 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1876 if (!CurrentClass) {
1877 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1878 CurrentClass = Cat->getClassInterface();
1879 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1880 CurrentClass = Impl->getClassInterface();
1881 }
1882 if (GetterMethod)
1883 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1884 if (SetterMethod)
1885 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001886}
1887
John McCalld226f652010-08-21 09:40:31 +00001888void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001889 SourceLocation Loc,
1890 unsigned &Attributes) {
1891 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001892 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001893 return;
1894
1895 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001896 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001897
David Blaikie4e4d0842012-03-11 07:00:24 +00001898 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001899 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1900 PropertyTy->isObjCRetainableType()) {
1901 // 'readonly' property with no obvious lifetime.
1902 // its life time will be determined by its backing ivar.
1903 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1904 ObjCDeclSpec::DQ_PR_copy |
1905 ObjCDeclSpec::DQ_PR_retain |
1906 ObjCDeclSpec::DQ_PR_strong |
1907 ObjCDeclSpec::DQ_PR_weak |
1908 ObjCDeclSpec::DQ_PR_assign);
1909 if ((Attributes & rel) == 0)
1910 return;
1911 }
1912
Ted Kremenek9d64c152010-03-12 00:38:38 +00001913 // readonly and readwrite/assign/retain/copy conflict.
1914 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1915 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1916 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001917 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001918 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001919 ObjCDeclSpec::DQ_PR_retain |
1920 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001921 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1922 "readwrite" :
1923 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1924 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001925 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1926 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001927 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1928 "copy" : "retain";
1929
1930 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1931 diag::err_objc_property_attr_mutually_exclusive :
1932 diag::warn_objc_property_attr_mutually_exclusive)
1933 << "readonly" << which;
1934 }
1935
1936 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001937 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1938 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1939 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001940 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001941 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001942 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1943 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1944 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1945 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00001946 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001947 }
1948
1949 // Check for more than one of { assign, copy, retain }.
1950 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1951 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1952 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1953 << "assign" << "copy";
1954 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1955 }
1956 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1957 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1958 << "assign" << "retain";
1959 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1960 }
John McCallf85e1932011-06-15 23:02:42 +00001961 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1962 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1963 << "assign" << "strong";
1964 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1965 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001966 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001967 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1968 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1969 << "assign" << "weak";
1970 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1971 }
1972 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1973 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1974 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1975 << "unsafe_unretained" << "copy";
1976 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1977 }
1978 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1979 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1980 << "unsafe_unretained" << "retain";
1981 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1982 }
1983 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1984 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1985 << "unsafe_unretained" << "strong";
1986 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1987 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001988 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001989 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1990 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1991 << "unsafe_unretained" << "weak";
1992 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1993 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001994 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1995 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1996 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1997 << "copy" << "retain";
1998 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1999 }
John McCallf85e1932011-06-15 23:02:42 +00002000 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2001 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2002 << "copy" << "strong";
2003 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2004 }
2005 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2006 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2007 << "copy" << "weak";
2008 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2009 }
2010 }
2011 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2012 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2013 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2014 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002015 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002016 }
2017 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2018 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2019 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2020 << "strong" << "weak";
2021 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002022 }
2023
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002024 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2025 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2026 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2027 << "atomic" << "nonatomic";
2028 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2029 }
2030
Ted Kremenek9d64c152010-03-12 00:38:38 +00002031 // Warn if user supplied no assignment attribute, property is
2032 // readwrite, and this is an object type.
2033 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002034 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2035 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2036 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002037 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002038 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002039 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002040 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002041 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002042 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002043 bool isAnyClassTy =
2044 (PropertyTy->isObjCClassType() ||
2045 PropertyTy->isObjCQualifiedClassType());
2046 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2047 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002048 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002049 ;
2050 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002051 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002052 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002053 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002054
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002055 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002056 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002057 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002058 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002059 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002060
2061 // FIXME: Implement warning dependent on NSCopying being
2062 // implemented. See also:
2063 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2064 // (please trim this list while you are at it).
2065 }
2066
2067 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002068 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002069 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002070 && PropertyTy->isBlockPointerType())
2071 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
David Blaikie4e4d0842012-03-11 07:00:24 +00002072 else if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002073 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2074 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2075 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2076 PropertyTy->isBlockPointerType())
2077 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002078
2079 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2080 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2081 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2082
Ted Kremenek9d64c152010-03-12 00:38:38 +00002083}