blob: 8a8927d362f183a6511280c93ed721f22fbc83a0 [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)) {
John McCalld64c2eb2012-08-20 23:36:59 +000046 return Qualifiers::OCL_Strong;
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 Kremenek28685ab2010-03-12 00:46:40 +0000138 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000139 if (CDecl->IsClassExtension()) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000140 Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000141 FD, GetterSel, SetterSel,
142 isAssign, isReadWrite,
143 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000144 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000145 isOverridingProperty, TSI,
146 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000147 if (Res) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000148 CheckObjCPropertyAttributes(Res, AtLoc, Attributes, false);
David Blaikie4e4d0842012-03-11 07:00:24 +0000149 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000150 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
151 }
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000152 ActOnDocumentableDecl(Res);
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.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000166 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
167 (isa<ObjCInterfaceDecl>(ClassDecl) ||
168 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000169
David Blaikie4e4d0842012-03-11 07:00:24 +0000170 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000171 checkARCPropertyDecl(*this, Res);
172
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000173 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000174 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000175}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000176
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000177static ObjCPropertyDecl::PropertyAttributeKind
178makePropertyAttributesAsWritten(unsigned Attributes) {
179 unsigned attributesAsWritten = 0;
180 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
181 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
182 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
183 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
184 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
185 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
186 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
187 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
188 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
189 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
190 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
191 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
192 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
193 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
194 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
195 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
196 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
197 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
198 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
199 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
200 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
201 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
202 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
203 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
204
205 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
206}
207
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000208static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000209 SourceLocation LParenLoc, SourceLocation &Loc) {
210 if (LParenLoc.isMacroID())
211 return false;
212
213 SourceManager &SM = Context.getSourceManager();
214 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
215 // Try to load the file buffer.
216 bool invalidTemp = false;
217 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
218 if (invalidTemp)
219 return false;
220 const char *tokenBegin = file.data() + locInfo.second;
221
222 // Lex from the start of the given location.
223 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
224 Context.getLangOpts(),
225 file.begin(), tokenBegin, file.end());
226 Token Tok;
227 do {
228 lexer.LexFromRawLexer(Tok);
229 if (Tok.is(tok::raw_identifier) &&
230 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
231 Loc = Tok.getLocation();
232 return true;
233 }
234 } while (Tok.isNot(tok::r_paren));
235 return false;
236
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000237}
238
John McCalld226f652010-08-21 09:40:31 +0000239Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000240Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000241 SourceLocation AtLoc,
242 SourceLocation LParenLoc,
243 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000244 Selector GetterSel, Selector SetterSel,
245 const bool isAssign,
246 const bool isReadWrite,
247 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000248 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000249 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000250 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000251 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000252 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000253 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000254 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000255 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000256 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
257
258 if (CCPrimary)
259 // Check for duplicate declaration of this property in current and
260 // other class extensions.
261 for (const ObjCCategoryDecl *ClsExtDecl =
262 CCPrimary->getFirstClassExtension();
263 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
264 if (ObjCPropertyDecl *prevDecl =
265 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
266 Diag(AtLoc, diag::err_duplicate_property);
267 Diag(prevDecl->getLocation(), diag::note_property_declare);
268 return 0;
269 }
270 }
271
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000272 // Create a new ObjCPropertyDecl with the DeclContext being
273 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000274 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000275 ObjCPropertyDecl *PDecl =
276 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000277 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000278 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000279 makePropertyAttributesAsWritten(AttributesAsWritten));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000280 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
281 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
282 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
283 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000284 // Set setter/getter selector name. Needed later.
285 PDecl->setGetterName(GetterSel);
286 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000287 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000288 DC->addDecl(PDecl);
289
290 // We need to look in the @interface to see if the @property was
291 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000292 if (!CCPrimary) {
293 Diag(CDecl->getLocation(), diag::err_continuation_class);
294 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000295 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000296 }
297
298 // Find the property in continuation class's primary class only.
299 ObjCPropertyDecl *PIDecl =
300 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
301
302 if (!PIDecl) {
303 // No matching property found in the primary class. Just fall thru
304 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000305 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000306 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000307 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000308 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000309
310 // A case of continuation class adding a new property in the class. This
311 // is not what it was meant for. However, gcc supports it and so should we.
312 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000313 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000314 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000315 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
316 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000317 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000318 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
319 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000320 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000321 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
322 bool IncompatibleObjC = false;
323 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000324 // Relax the strict type matching for property type in continuation class.
325 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000326 // as it narrows the object type in its primary class property. Note that
327 // this conversion is safe only because the wider type is for a 'readonly'
328 // property in primary class and 'narrowed' type for a 'readwrite' property
329 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000330 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
331 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
332 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
333 ConvertedType, IncompatibleObjC))
334 || IncompatibleObjC) {
335 Diag(AtLoc,
336 diag::err_type_mismatch_continuation_class) << PDecl->getType();
337 Diag(PIDecl->getLocation(), diag::note_property_declare);
338 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000339 }
340
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000341 // The property 'PIDecl's readonly attribute will be over-ridden
342 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000343 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000344 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
345 unsigned retainCopyNonatomic =
346 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000347 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000348 ObjCPropertyDecl::OBJC_PR_copy |
349 ObjCPropertyDecl::OBJC_PR_nonatomic);
350 if ((Attributes & retainCopyNonatomic) !=
351 (PIkind & retainCopyNonatomic)) {
352 Diag(AtLoc, diag::warn_property_attr_mismatch);
353 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000354 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000355 DeclContext *DC = cast<DeclContext>(CCPrimary);
356 if (!ObjCPropertyDecl::findPropertyDecl(DC,
357 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000358 // Protocol is not in the primary class. Must build one for it.
359 ObjCDeclSpec ProtocolPropertyODS;
360 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
361 // and ObjCPropertyDecl::PropertyAttributeKind have identical
362 // values. Should consolidate both into one enum type.
363 ProtocolPropertyODS.
364 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
365 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000366 // Must re-establish the context from class extension to primary
367 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000368 ContextRAII SavedContext(*this, CCPrimary);
369
John McCalld226f652010-08-21 09:40:31 +0000370 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000371 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000372 PIDecl->getGetterName(),
373 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000374 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000375 MethodImplKind,
376 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000377 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000378 }
379 PIDecl->makeitReadWriteAttribute();
380 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
381 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000382 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
383 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000384 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
385 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
386 PIDecl->setSetterName(SetterSel);
387 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000388 // Tailor the diagnostics for the common case where a readwrite
389 // property is declared both in the @interface and the continuation.
390 // This is a common error where the user often intended the original
391 // declaration to be readonly.
392 unsigned diag =
393 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
394 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
395 ? diag::err_use_continuation_class_redeclaration_readwrite
396 : diag::err_use_continuation_class;
397 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000398 << CCPrimary->getDeclName();
399 Diag(PIDecl->getLocation(), diag::note_property_declare);
400 }
401 *isOverridingProperty = true;
402 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000403 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000404 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
405 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000406 if (ASTMutationListener *L = Context.getASTMutationListener())
407 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000408 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000409}
410
411ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
412 ObjCContainerDecl *CDecl,
413 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000414 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000415 FieldDeclarator &FD,
416 Selector GetterSel,
417 Selector SetterSel,
418 const bool isAssign,
419 const bool isReadWrite,
420 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000421 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000422 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000423 tok::ObjCKeywordKind MethodImplKind,
424 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000425 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000426 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000427
428 // Issue a warning if property is 'assign' as default and its object, which is
429 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000430 if (getLangOpts().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000431 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000432 if (const ObjCObjectPointerType *ObjPtrTy =
433 T->getAs<ObjCObjectPointerType>()) {
434 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
435 if (IDecl)
436 if (ObjCProtocolDecl* PNSCopying =
437 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
438 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
439 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000440 }
John McCallc12c5bb2010-05-15 11:32:37 +0000441 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000442 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
443
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000444 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000445 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
446 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000447 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000448
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000449 if (ObjCPropertyDecl *prevDecl =
450 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000451 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000452 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000453 PDecl->setInvalidDecl();
454 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000455 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000456 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000457 if (lexicalDC)
458 PDecl->setLexicalDeclContext(lexicalDC);
459 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000460
461 if (T->isArrayType() || T->isFunctionType()) {
462 Diag(AtLoc, diag::err_property_type) << T;
463 PDecl->setInvalidDecl();
464 }
465
466 ProcessDeclAttributes(S, PDecl, FD.D);
467
468 // Regardless of setter/getter attribute, we save the default getter/setter
469 // selector names in anticipation of declaration of setter/getter methods.
470 PDecl->setGetterName(GetterSel);
471 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000472 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000473 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000474
Ted Kremenek28685ab2010-03-12 00:46:40 +0000475 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
476 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
477
478 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
479 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
480
481 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
482 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
483
484 if (isReadWrite)
485 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
486
487 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
488 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
489
John McCallf85e1932011-06-15 23:02:42 +0000490 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
491 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
492
493 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
494 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
495
Ted Kremenek28685ab2010-03-12 00:46:40 +0000496 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
497 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
498
John McCallf85e1932011-06-15 23:02:42 +0000499 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
500 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
501
Ted Kremenek28685ab2010-03-12 00:46:40 +0000502 if (isAssign)
503 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
504
John McCall265941b2011-09-13 18:31:23 +0000505 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000506 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
507 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000508 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000509 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000510
John McCallf85e1932011-06-15 23:02:42 +0000511 // 'unsafe_unretained' is alias for 'assign'.
512 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
513 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
514 if (isAssign)
515 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
516
Ted Kremenek28685ab2010-03-12 00:46:40 +0000517 if (MethodImplKind == tok::objc_required)
518 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
519 else if (MethodImplKind == tok::objc_optional)
520 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000521
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000522 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000523}
524
John McCallf85e1932011-06-15 23:02:42 +0000525static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
526 ObjCPropertyDecl *property,
527 ObjCIvarDecl *ivar) {
528 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
529
John McCallf85e1932011-06-15 23:02:42 +0000530 QualType ivarType = ivar->getType();
531 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000532
John McCall265941b2011-09-13 18:31:23 +0000533 // The lifetime implied by the property's attributes.
534 Qualifiers::ObjCLifetime propertyLifetime =
535 getImpliedARCOwnership(property->getPropertyAttributes(),
536 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000537
John McCall265941b2011-09-13 18:31:23 +0000538 // We're fine if they match.
539 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000540
John McCall265941b2011-09-13 18:31:23 +0000541 // These aren't valid lifetimes for object ivars; don't diagnose twice.
542 if (ivarLifetime == Qualifiers::OCL_None ||
543 ivarLifetime == Qualifiers::OCL_Autoreleasing)
544 return;
John McCallf85e1932011-06-15 23:02:42 +0000545
John McCalld64c2eb2012-08-20 23:36:59 +0000546 // If the ivar is private, and it's implicitly __unsafe_unretained
547 // becaues of its type, then pretend it was actually implicitly
548 // __strong. This is only sound because we're processing the
549 // property implementation before parsing any method bodies.
550 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
551 propertyLifetime == Qualifiers::OCL_Strong &&
552 ivar->getAccessControl() == ObjCIvarDecl::Private) {
553 SplitQualType split = ivarType.split();
554 if (split.Quals.hasObjCLifetime()) {
555 assert(ivarType->isObjCARCImplicitlyUnretainedType());
556 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
557 ivarType = S.Context.getQualifiedType(split);
558 ivar->setType(ivarType);
559 return;
560 }
561 }
562
John McCall265941b2011-09-13 18:31:23 +0000563 switch (propertyLifetime) {
564 case Qualifiers::OCL_Strong:
565 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
566 << property->getDeclName()
567 << ivar->getDeclName()
568 << ivarLifetime;
569 break;
John McCallf85e1932011-06-15 23:02:42 +0000570
John McCall265941b2011-09-13 18:31:23 +0000571 case Qualifiers::OCL_Weak:
572 S.Diag(propertyImplLoc, diag::error_weak_property)
573 << property->getDeclName()
574 << ivar->getDeclName();
575 break;
John McCallf85e1932011-06-15 23:02:42 +0000576
John McCall265941b2011-09-13 18:31:23 +0000577 case Qualifiers::OCL_ExplicitNone:
578 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
579 << property->getDeclName()
580 << ivar->getDeclName()
581 << ((property->getPropertyAttributesAsWritten()
582 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
583 break;
John McCallf85e1932011-06-15 23:02:42 +0000584
John McCall265941b2011-09-13 18:31:23 +0000585 case Qualifiers::OCL_Autoreleasing:
586 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000587
John McCall265941b2011-09-13 18:31:23 +0000588 case Qualifiers::OCL_None:
589 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000590 return;
591 }
592
593 S.Diag(property->getLocation(), diag::note_property_declare);
594}
595
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000596/// setImpliedPropertyAttributeForReadOnlyProperty -
597/// This routine evaludates life-time attributes for a 'readonly'
598/// property with no known lifetime of its own, using backing
599/// 'ivar's attribute, if any. If no backing 'ivar', property's
600/// life-time is assumed 'strong'.
601static void setImpliedPropertyAttributeForReadOnlyProperty(
602 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
603 Qualifiers::ObjCLifetime propertyLifetime =
604 getImpliedARCOwnership(property->getPropertyAttributes(),
605 property->getType());
606 if (propertyLifetime != Qualifiers::OCL_None)
607 return;
608
609 if (!ivar) {
610 // if no backing ivar, make property 'strong'.
611 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
612 return;
613 }
614 // property assumes owenership of backing ivar.
615 QualType ivarType = ivar->getType();
616 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
617 if (ivarLifetime == Qualifiers::OCL_Strong)
618 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
619 else if (ivarLifetime == Qualifiers::OCL_Weak)
620 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
621 return;
622}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000623
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000624/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
625/// attribute declared in primary class and attributes overridden in any of its
626/// class extensions.
627static void
628DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
629 ObjCPropertyDecl *property) {
630 unsigned Attributes = property->getPropertyAttributesAsWritten();
631 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
632 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
633 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000634 ObjCPropertyDecl *ClassExtProperty = 0;
635 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
636 E = CDecl->prop_end(); P != E; ++P) {
637 if ((*P)->getIdentifier() == property->getIdentifier()) {
638 ClassExtProperty = *P;
639 break;
640 }
641 }
642 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000643 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000644 unsigned classExtPropertyAttr =
645 ClassExtProperty->getPropertyAttributesAsWritten();
646 // We are issuing the warning that we postponed because class extensions
647 // can override readonly->readwrite and 'setter' attributes originally
648 // placed on class's property declaration now make sense in the overridden
649 // property.
650 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
651 if (!classExtPropertyAttr ||
652 (classExtPropertyAttr & ObjCDeclSpec::DQ_PR_readwrite))
653 continue;
654 warn = true;
655 break;
656 }
657 }
658 }
659 if (warn) {
660 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
661 ObjCDeclSpec::DQ_PR_unsafe_unretained |
662 ObjCDeclSpec::DQ_PR_copy |
663 ObjCDeclSpec::DQ_PR_retain |
664 ObjCDeclSpec::DQ_PR_strong);
665 if (Attributes & setterAttrs) {
666 const char * which =
667 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
668 "assign" :
669 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
670 "unsafe_unretained" :
671 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
672 "copy" :
673 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
674 "retain" : "strong";
675
676 S.Diag(property->getLocation(),
677 diag::warn_objc_property_attr_mutually_exclusive)
678 << "readonly" << which;
679 }
680 }
681
682
683}
684
Ted Kremenek28685ab2010-03-12 00:46:40 +0000685/// ActOnPropertyImplDecl - This routine performs semantic checks and
686/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000687/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000688///
John McCalld226f652010-08-21 09:40:31 +0000689Decl *Sema::ActOnPropertyImplDecl(Scope *S,
690 SourceLocation AtLoc,
691 SourceLocation PropertyLoc,
692 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000693 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000694 IdentifierInfo *PropertyIvar,
695 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000696 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000697 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000698 // Make sure we have a context for the property implementation declaration.
699 if (!ClassImpDecl) {
700 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000701 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000702 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000703 if (PropertyIvarLoc.isInvalid())
704 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000705 SourceLocation PropertyDiagLoc = PropertyLoc;
706 if (PropertyDiagLoc.isInvalid())
707 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000708 ObjCPropertyDecl *property = 0;
709 ObjCInterfaceDecl* IDecl = 0;
710 // Find the class or category class where this property must have
711 // a declaration.
712 ObjCImplementationDecl *IC = 0;
713 ObjCCategoryImplDecl* CatImplClass = 0;
714 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
715 IDecl = IC->getClassInterface();
716 // We always synthesize an interface for an implementation
717 // without an interface decl. So, IDecl is always non-zero.
718 assert(IDecl &&
719 "ActOnPropertyImplDecl - @implementation without @interface");
720
721 // Look for this property declaration in the @implementation's @interface
722 property = IDecl->FindPropertyDeclaration(PropertyId);
723 if (!property) {
724 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000725 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000726 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000727 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000728 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
729 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000730 if (AtLoc.isValid())
731 Diag(AtLoc, diag::warn_implicit_atomic_property);
732 else
733 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
734 Diag(property->getLocation(), diag::note_property_declare);
735 }
736
Ted Kremenek28685ab2010-03-12 00:46:40 +0000737 if (const ObjCCategoryDecl *CD =
738 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
739 if (!CD->IsClassExtension()) {
740 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
741 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000742 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000743 }
744 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000745
746 if (Synthesize&&
747 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
748 property->hasAttr<IBOutletAttr>() &&
749 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000750 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
751 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000752 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000753 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000754 property->getLParenLoc(), readonlyLoc)) {
755 SourceLocation endLoc =
756 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
757 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
758 Diag(property->getLocation(),
759 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
760 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
761 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000762 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000763
764 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000765
Ted Kremenek28685ab2010-03-12 00:46:40 +0000766 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
767 if (Synthesize) {
768 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000769 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000770 }
771 IDecl = CatImplClass->getClassInterface();
772 if (!IDecl) {
773 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000774 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000775 }
776 ObjCCategoryDecl *Category =
777 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
778
779 // If category for this implementation not found, it is an error which
780 // has already been reported eralier.
781 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000782 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000783 // Look for this property declaration in @implementation's category
784 property = Category->FindPropertyDeclaration(PropertyId);
785 if (!property) {
786 Diag(PropertyLoc, diag::error_bad_category_property_decl)
787 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000788 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000789 }
790 } else {
791 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000792 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000793 }
794 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000795 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000796 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000797 // Check that we have a valid, previously declared ivar for @synthesize
798 if (Synthesize) {
799 // @synthesize
800 if (!PropertyIvar)
801 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000802 // Check that this is a previously declared 'ivar' in 'IDecl' interface
803 ObjCInterfaceDecl *ClassDeclared;
804 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
805 QualType PropType = property->getType();
806 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000807
808 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000809 diag::err_incomplete_synthesized_property,
810 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000811 Diag(property->getLocation(), diag::note_property_declare);
812 CompleteTypeErr = true;
813 }
814
David Blaikie4e4d0842012-03-11 07:00:24 +0000815 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000816 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000817 ObjCPropertyDecl::OBJC_PR_readonly) &&
818 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000819 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
820 }
821
John McCallf85e1932011-06-15 23:02:42 +0000822 ObjCPropertyDecl::PropertyAttributeKind kind
823 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000824
825 // Add GC __weak to the ivar type if the property is weak.
826 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000827 getLangOpts().getGC() != LangOptions::NonGC) {
828 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000829 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000830 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000831 Diag(property->getLocation(), diag::note_property_declare);
832 } else {
833 PropertyIvarType =
834 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000835 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000836 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000837 if (AtLoc.isInvalid()) {
838 // Check when default synthesizing a property that there is
839 // an ivar matching property name and issue warning; since this
840 // is the most common case of not using an ivar used for backing
841 // property in non-default synthesis case.
842 ObjCInterfaceDecl *ClassDeclared=0;
843 ObjCIvarDecl *originalIvar =
844 IDecl->lookupInstanceVariable(property->getIdentifier(),
845 ClassDeclared);
846 if (originalIvar) {
847 Diag(PropertyDiagLoc,
848 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000849 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000850 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000851 Diag(property->getLocation(), diag::note_property_declare);
852 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000853 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000854 }
855
856 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000857 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000858 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000859 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000860 !PropertyIvarType.getObjCLifetime() &&
861 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000862
John McCall265941b2011-09-13 18:31:23 +0000863 // It's an error if we have to do this and the user didn't
864 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000865 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000866 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000867 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000868 diag::err_arc_objc_property_default_assign_on_object);
869 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000870 } else {
871 Qualifiers::ObjCLifetime lifetime =
872 getImpliedARCOwnership(kind, PropertyIvarType);
873 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000874 if (lifetime == Qualifiers::OCL_Weak) {
875 bool err = false;
876 if (const ObjCObjectPointerType *ObjT =
877 PropertyIvarType->getAs<ObjCObjectPointerType>())
878 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000879 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000880 Diag(property->getLocation(), diag::note_property_declare);
881 err = true;
882 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000883 if (!err && !getLangOpts().ObjCRuntimeHasWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000884 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000885 Diag(property->getLocation(), diag::note_property_declare);
886 }
John McCallf85e1932011-06-15 23:02:42 +0000887 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000888
John McCallf85e1932011-06-15 23:02:42 +0000889 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000890 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000891 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
892 }
John McCallf85e1932011-06-15 23:02:42 +0000893 }
894
895 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000896 !getLangOpts().ObjCAutoRefCount &&
897 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000898 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000899 Diag(property->getLocation(), diag::note_property_declare);
900 }
901
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000902 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000903 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000904 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000905 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000906 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000907 if (CompleteTypeErr)
908 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000909 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000910 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000911 property->setPropertyIvarDecl(Ivar);
912
John McCall260611a2012-06-20 06:18:46 +0000913 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000914 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
915 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000916 // Note! I deliberately want it to fall thru so, we have a
917 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000918 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000919 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000920 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000921 << property->getDeclName() << Ivar->getDeclName()
922 << ClassDeclared->getDeclName();
923 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000924 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000925 // Note! I deliberately want it to fall thru so more errors are caught.
926 }
927 QualType IvarType = Context.getCanonicalType(Ivar->getType());
928
929 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000930 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
931 compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000932 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000933 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000934 compat =
935 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000936 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000937 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000938 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000939 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
940 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000941 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000942 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000943 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000944 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000945 << property->getDeclName() << PropType
946 << Ivar->getDeclName() << IvarType;
947 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000948 // Note! I deliberately want it to fall thru so, we have a
949 // a property implementation and to avoid future warnings.
950 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000951 else {
952 // FIXME! Rules for properties are somewhat different that those
953 // for assignments. Use a new routine to consolidate all cases;
954 // specifically for property redeclarations as well as for ivars.
955 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
956 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
957 if (lhsType != rhsType &&
958 lhsType->isArithmeticType()) {
959 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
960 << property->getDeclName() << PropType
961 << Ivar->getDeclName() << IvarType;
962 Diag(Ivar->getLocation(), diag::note_ivar_decl);
963 // Fall thru - see previous comment
964 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000965 }
966 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000967 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000968 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000969 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000970 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000971 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000972 // Fall thru - see previous comment
973 }
John McCallf85e1932011-06-15 23:02:42 +0000974 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000975 if ((property->getType()->isObjCObjectPointerType() ||
976 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000977 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000978 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000979 << property->getDeclName() << Ivar->getDeclName();
980 // Fall thru - see previous comment
981 }
982 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000983 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000984 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000985 } else if (PropertyIvar)
986 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +0000987 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000988
Ted Kremenek28685ab2010-03-12 00:46:40 +0000989 assert (property && "ActOnPropertyImplDecl - property declaration missing");
990 ObjCPropertyImplDecl *PIDecl =
991 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
992 property,
993 (Synthesize ?
994 ObjCPropertyImplDecl::Synthesize
995 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000996 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000997
Fariborz Jahanian74414712012-05-15 18:12:51 +0000998 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +0000999 PIDecl->setInvalidDecl();
1000
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001001 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1002 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001003 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001004 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001005 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1006 // returned by the getter as it must conform to C++'s copy-return rules.
1007 // FIXME. Eventually we want to do this for Objective-C as well.
1008 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1009 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001010 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001011 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001012 Expr *IvarRefExpr =
1013 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1014 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001015 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001016 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001017 SourceLocation(),
1018 getterMethod->getResultType(),
1019 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001020 SourceLocation(),
1021 Owned(IvarRefExpr));
1022 if (!Res.isInvalid()) {
1023 Expr *ResExpr = Res.takeAs<Expr>();
1024 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001025 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001026 PIDecl->setGetterCXXConstructor(ResExpr);
1027 }
1028 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001029 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1030 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1031 Diag(getterMethod->getLocation(),
1032 diag::warn_property_getter_owning_mismatch);
1033 Diag(property->getLocation(), diag::note_property_declare);
1034 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001035 }
1036 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1037 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001038 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1039 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001040 // FIXME. Eventually we want to do this for Objective-C as well.
1041 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1042 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001043 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001044 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001045 Expr *lhs =
1046 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1047 SelfExpr, true, true);
1048 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1049 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001050 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +00001051 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +00001052 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +00001053 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +00001054 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001055 if (property->getPropertyAttributes() &
1056 ObjCPropertyDecl::OBJC_PR_atomic) {
1057 Expr *callExpr = Res.takeAs<Expr>();
1058 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001059 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1060 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001061 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001062 if (property->getType()->isReferenceType()) {
1063 Diag(PropertyLoc,
1064 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001065 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001066 Diag(FuncDecl->getLocStart(),
1067 diag::note_callee_decl) << FuncDecl;
1068 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001069 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001070 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1071 }
1072 }
1073
Ted Kremenek28685ab2010-03-12 00:46:40 +00001074 if (IC) {
1075 if (Synthesize)
1076 if (ObjCPropertyImplDecl *PPIDecl =
1077 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1078 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1079 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1080 << PropertyIvar;
1081 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1082 }
1083
1084 if (ObjCPropertyImplDecl *PPIDecl
1085 = IC->FindPropertyImplDecl(PropertyId)) {
1086 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1087 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001088 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001089 }
1090 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001091 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001092 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001093 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001094 // Diagnose if an ivar was lazily synthesdized due to a previous
1095 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001096 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001097 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001098 ObjCIvarDecl *Ivar = 0;
1099 if (!Synthesize)
1100 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1101 else {
1102 if (PropertyIvar && PropertyIvar != PropertyId)
1103 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1104 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001105 // Issue diagnostics only if Ivar belongs to current class.
1106 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001107 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001108 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1109 << PropertyId;
1110 Ivar->setInvalidDecl();
1111 }
1112 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001113 } else {
1114 if (Synthesize)
1115 if (ObjCPropertyImplDecl *PPIDecl =
1116 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001117 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001118 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1119 << PropertyIvar;
1120 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1121 }
1122
1123 if (ObjCPropertyImplDecl *PPIDecl =
1124 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001125 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001126 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001127 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001128 }
1129 CatImplClass->addPropertyImplementation(PIDecl);
1130 }
1131
John McCalld226f652010-08-21 09:40:31 +00001132 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001133}
1134
1135//===----------------------------------------------------------------------===//
1136// Helper methods.
1137//===----------------------------------------------------------------------===//
1138
Ted Kremenek9d64c152010-03-12 00:38:38 +00001139/// DiagnosePropertyMismatch - Compares two properties for their
1140/// attributes and types and warns on a variety of inconsistencies.
1141///
1142void
1143Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1144 ObjCPropertyDecl *SuperProperty,
1145 const IdentifierInfo *inheritedName) {
1146 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1147 Property->getPropertyAttributes();
1148 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1149 SuperProperty->getPropertyAttributes();
1150 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1151 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1152 Diag(Property->getLocation(), diag::warn_readonly_property)
1153 << Property->getDeclName() << inheritedName;
1154 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1155 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1156 Diag(Property->getLocation(), diag::warn_property_attribute)
1157 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001158 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001159 unsigned CAttrRetain =
1160 (CAttr &
1161 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1162 unsigned SAttrRetain =
1163 (SAttr &
1164 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1165 bool CStrong = (CAttrRetain != 0);
1166 bool SStrong = (SAttrRetain != 0);
1167 if (CStrong != SStrong)
1168 Diag(Property->getLocation(), diag::warn_property_attribute)
1169 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1170 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001171
1172 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1173 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1174 Diag(Property->getLocation(), diag::warn_property_attribute)
1175 << Property->getDeclName() << "atomic" << inheritedName;
1176 if (Property->getSetterName() != SuperProperty->getSetterName())
1177 Diag(Property->getLocation(), diag::warn_property_attribute)
1178 << Property->getDeclName() << "setter" << inheritedName;
1179 if (Property->getGetterName() != SuperProperty->getGetterName())
1180 Diag(Property->getLocation(), diag::warn_property_attribute)
1181 << Property->getDeclName() << "getter" << inheritedName;
1182
1183 QualType LHSType =
1184 Context.getCanonicalType(SuperProperty->getType());
1185 QualType RHSType =
1186 Context.getCanonicalType(Property->getType());
1187
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001188 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001189 // Do cases not handled in above.
1190 // FIXME. For future support of covariant property types, revisit this.
1191 bool IncompatibleObjC = false;
1192 QualType ConvertedType;
1193 if (!isObjCPointerConversion(RHSType, LHSType,
1194 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001195 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001196 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1197 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001198 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1199 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001200 }
1201}
1202
1203bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1204 ObjCMethodDecl *GetterMethod,
1205 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001206 if (!GetterMethod)
1207 return false;
1208 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1209 QualType PropertyIvarType = property->getType().getNonReferenceType();
1210 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1211 if (!compat) {
1212 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1213 isa<ObjCObjectPointerType>(GetterType))
1214 compat =
1215 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001216 GetterType->getAs<ObjCObjectPointerType>(),
1217 PropertyIvarType->getAs<ObjCObjectPointerType>());
1218 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001219 != Compatible) {
1220 Diag(Loc, diag::error_property_accessor_type)
1221 << property->getDeclName() << PropertyIvarType
1222 << GetterMethod->getSelector() << GetterType;
1223 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1224 return true;
1225 } else {
1226 compat = true;
1227 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1228 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1229 if (lhsType != rhsType && lhsType->isArithmeticType())
1230 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001231 }
1232 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001233
1234 if (!compat) {
1235 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1236 << property->getDeclName()
1237 << GetterMethod->getSelector();
1238 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1239 return true;
1240 }
1241
Ted Kremenek9d64c152010-03-12 00:38:38 +00001242 return false;
1243}
1244
1245/// ComparePropertiesInBaseAndSuper - This routine compares property
1246/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001247/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001248///
1249void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1250 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1251 if (!SDecl)
1252 return;
1253 // FIXME: O(N^2)
1254 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1255 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001256 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001257 // Does property in super class has declaration in current class?
1258 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1259 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001260 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001261 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1262 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1263 SDecl->getIdentifier());
1264 }
1265 }
1266}
1267
1268/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1269/// of properties declared in a protocol and compares their attribute against
1270/// the same property declared in the class or category.
1271void
1272Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1273 ObjCProtocolDecl *PDecl) {
1274 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1275 if (!IDecl) {
1276 // Category
1277 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1278 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1279 if (!CatDecl->IsClassExtension())
1280 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1281 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001282 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001283 ObjCCategoryDecl::prop_iterator CP, CE;
1284 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001285 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001286 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001287 break;
1288 if (CP != CE)
1289 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001290 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001291 }
1292 return;
1293 }
1294 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1295 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001296 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001297 ObjCInterfaceDecl::prop_iterator CP, CE;
1298 // Is this property already in class's list of properties?
1299 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001300 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001301 break;
1302 if (CP != CE)
1303 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001304 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001305 }
1306}
1307
1308/// CompareProperties - This routine compares properties
1309/// declared in 'ClassOrProtocol' objects (which can be a class or an
1310/// inherited protocol with the list of properties for class/category 'CDecl'
1311///
John McCalld226f652010-08-21 09:40:31 +00001312void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1313 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001314 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1315
1316 if (!IDecl) {
1317 // Category
1318 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1319 assert (CatDecl && "CompareProperties");
1320 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1321 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1322 E = MDecl->protocol_end(); P != E; ++P)
1323 // Match properties of category with those of protocol (*P)
1324 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1325
1326 // Go thru the list of protocols for this category and recursively match
1327 // their properties with those in the category.
1328 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1329 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001330 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001331 } else {
1332 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1333 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1334 E = MD->protocol_end(); P != E; ++P)
1335 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1336 }
1337 return;
1338 }
1339
1340 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001341 for (ObjCInterfaceDecl::all_protocol_iterator
1342 P = MDecl->all_referenced_protocol_begin(),
1343 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001344 // Match properties of class IDecl with those of protocol (*P).
1345 MatchOneProtocolPropertiesInClass(IDecl, *P);
1346
1347 // Go thru the list of protocols for this class and recursively match
1348 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001349 for (ObjCInterfaceDecl::all_protocol_iterator
1350 P = IDecl->all_referenced_protocol_begin(),
1351 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001352 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001353 } else {
1354 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1355 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1356 E = MD->protocol_end(); P != E; ++P)
1357 MatchOneProtocolPropertiesInClass(IDecl, *P);
1358 }
1359}
1360
1361/// isPropertyReadonly - Return true if property is readonly, by searching
1362/// for the property in the class and in its categories and implementations
1363///
1364bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1365 ObjCInterfaceDecl *IDecl) {
1366 // by far the most common case.
1367 if (!PDecl->isReadOnly())
1368 return false;
1369 // Even if property is ready only, if interface has a user defined setter,
1370 // it is not considered read only.
1371 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1372 return false;
1373
1374 // Main class has the property as 'readonly'. Must search
1375 // through the category list to see if the property's
1376 // attribute has been over-ridden to 'readwrite'.
1377 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1378 Category; Category = Category->getNextClassCategory()) {
1379 // Even if property is ready only, if a category has a user defined setter,
1380 // it is not considered read only.
1381 if (Category->getInstanceMethod(PDecl->getSetterName()))
1382 return false;
1383 ObjCPropertyDecl *P =
1384 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1385 if (P && !P->isReadOnly())
1386 return false;
1387 }
1388
1389 // Also, check for definition of a setter method in the implementation if
1390 // all else failed.
1391 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1392 if (ObjCImplementationDecl *IMD =
1393 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1394 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1395 return false;
1396 } else if (ObjCCategoryImplDecl *CIMD =
1397 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1398 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1399 return false;
1400 }
1401 }
1402 // Lastly, look through the implementation (if one is in scope).
1403 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1404 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1405 return false;
1406 // If all fails, look at the super class.
1407 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1408 return isPropertyReadonly(PDecl, SIDecl);
1409 return true;
1410}
1411
1412/// CollectImmediateProperties - This routine collects all properties in
1413/// the class and its conforming protocols; but not those it its super class.
1414void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001415 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1416 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001417 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1418 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1419 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001420 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001421 PropMap[Prop->getIdentifier()] = Prop;
1422 }
1423 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001424 for (ObjCInterfaceDecl::all_protocol_iterator
1425 PI = IDecl->all_referenced_protocol_begin(),
1426 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001427 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001428 }
1429 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1430 if (!CATDecl->IsClassExtension())
1431 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1432 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001433 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001434 PropMap[Prop->getIdentifier()] = Prop;
1435 }
1436 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001437 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001438 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001439 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001440 }
1441 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1442 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1443 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001444 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001445 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1446 // Exclude property for protocols which conform to class's super-class,
1447 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001448 if (!PropertyFromSuper ||
1449 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001450 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1451 if (!PropEntry)
1452 PropEntry = Prop;
1453 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001454 }
1455 // scan through protocol's protocols.
1456 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1457 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001458 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001459 }
1460}
1461
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001462/// CollectClassPropertyImplementations - This routine collects list of
1463/// properties to be implemented in the class. This includes, class's
1464/// and its conforming protocols' properties.
1465static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1466 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1467 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1468 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1469 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001470 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001471 PropMap[Prop->getIdentifier()] = Prop;
1472 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001473 for (ObjCInterfaceDecl::all_protocol_iterator
1474 PI = IDecl->all_referenced_protocol_begin(),
1475 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001476 CollectClassPropertyImplementations((*PI), PropMap);
1477 }
1478 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1479 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1480 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001481 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanianac371502012-02-23 18:21:25 +00001482 if (!PropMap.count(Prop->getIdentifier()))
1483 PropMap[Prop->getIdentifier()] = Prop;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001484 }
1485 // scan through protocol's protocols.
1486 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1487 E = PDecl->protocol_end(); PI != E; ++PI)
1488 CollectClassPropertyImplementations((*PI), PropMap);
1489 }
1490}
1491
1492/// CollectSuperClassPropertyImplementations - This routine collects list of
1493/// properties to be implemented in super class(s) and also coming from their
1494/// conforming protocols.
1495static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1496 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1497 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1498 while (SDecl) {
1499 CollectClassPropertyImplementations(SDecl, PropMap);
1500 SDecl = SDecl->getSuperClass();
1501 }
1502 }
1503}
1504
Ted Kremenek9d64c152010-03-12 00:38:38 +00001505/// LookupPropertyDecl - Looks up a property in the current class and all
1506/// its protocols.
1507ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1508 IdentifierInfo *II) {
1509 if (const ObjCInterfaceDecl *IDecl =
1510 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1511 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1512 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001513 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001514 if (Prop->getIdentifier() == II)
1515 return Prop;
1516 }
1517 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001518 for (ObjCInterfaceDecl::all_protocol_iterator
1519 PI = IDecl->all_referenced_protocol_begin(),
1520 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001521 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1522 if (Prop)
1523 return Prop;
1524 }
1525 }
1526 else if (const ObjCProtocolDecl *PDecl =
1527 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1528 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1529 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001530 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001531 if (Prop->getIdentifier() == II)
1532 return Prop;
1533 }
1534 // scan through protocol's protocols.
1535 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1536 E = PDecl->protocol_end(); PI != E; ++PI) {
1537 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1538 if (Prop)
1539 return Prop;
1540 }
1541 }
1542 return 0;
1543}
1544
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001545static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1546 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001547 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001548 {
1549 llvm::raw_svector_ostream os(ivarName);
1550 os << '_' << Prop->getIdentifier()->getName();
1551 }
1552 return &Ctx.Idents.get(ivarName.str());
1553}
1554
James Dennett699c9042012-06-15 07:13:21 +00001555/// \brief Default synthesizes all properties which must be synthesized
1556/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001557void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1558 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001559
1560 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1561 CollectClassPropertyImplementations(IDecl, PropMap);
1562 if (PropMap.empty())
1563 return;
1564 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1565 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1566
1567 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1568 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1569 ObjCPropertyDecl *Prop = P->second;
1570 // If property to be implemented in the super class, ignore.
1571 if (SuperPropMap[Prop->getIdentifier()])
1572 continue;
1573 // Is there a matching propery synthesize/dynamic?
1574 if (Prop->isInvalidDecl() ||
1575 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1576 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1577 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001578 // Property may have been synthesized by user.
1579 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1580 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001581 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1582 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1583 continue;
1584 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1585 continue;
1586 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001587 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1588 // We won't auto-synthesize properties declared in protocols.
1589 Diag(IMPDecl->getLocation(),
1590 diag::warn_auto_synthesizing_protocol_property);
1591 Diag(Prop->getLocation(), diag::note_property_declare);
1592 continue;
1593 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001594
1595 // We use invalid SourceLocations for the synthesized ivars since they
1596 // aren't really synthesized at a particular location; they just exist.
1597 // Saying that they are located at the @implementation isn't really going
1598 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001599 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1600 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1601 true,
1602 /* property = */ Prop->getIdentifier(),
1603 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001604 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001605 if (PIDecl) {
1606 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001607 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001608 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001609 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001610}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001611
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001612void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001613 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001614 return;
1615 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1616 if (!IC)
1617 return;
1618 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001619 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001620 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001621}
1622
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001623void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001624 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001625 const SelectorSet &InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001626 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1627 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1628 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1629
Ted Kremenek9d64c152010-03-12 00:38:38 +00001630 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001631 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001632 if (PropMap.empty())
1633 return;
1634
1635 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1636 for (ObjCImplDecl::propimpl_iterator
1637 I = IMPDecl->propimpl_begin(),
1638 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001639 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001640
1641 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1642 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1643 ObjCPropertyDecl *Prop = P->second;
1644 // Is there a matching propery synthesize/dynamic?
1645 if (Prop->isInvalidDecl() ||
1646 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001647 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001648 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001649 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001650 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001651 isa<ObjCCategoryDecl>(CDecl) ?
1652 diag::warn_setter_getter_impl_required_in_category :
1653 diag::warn_setter_getter_impl_required)
1654 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001655 Diag(Prop->getLocation(),
1656 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001657 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001658 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001659 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001660 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1661
Ted Kremenek9d64c152010-03-12 00:38:38 +00001662 }
1663
1664 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001665 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001666 isa<ObjCCategoryDecl>(CDecl) ?
1667 diag::warn_setter_getter_impl_required_in_category :
1668 diag::warn_setter_getter_impl_required)
1669 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001670 Diag(Prop->getLocation(),
1671 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001672 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001673 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001674 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001675 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001676 }
1677 }
1678}
1679
1680void
1681Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1682 ObjCContainerDecl* IDecl) {
1683 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001684 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001685 return;
1686 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1687 E = IDecl->prop_end();
1688 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001689 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001690 ObjCMethodDecl *GetterMethod = 0;
1691 ObjCMethodDecl *SetterMethod = 0;
1692 bool LookedUpGetterSetter = false;
1693
Ted Kremenek9d64c152010-03-12 00:38:38 +00001694 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001695 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001696
John McCall265941b2011-09-13 18:31:23 +00001697 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1698 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001699 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1700 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1701 LookedUpGetterSetter = true;
1702 if (GetterMethod) {
1703 Diag(GetterMethod->getLocation(),
1704 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001705 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001706 Diag(Property->getLocation(), diag::note_property_declare);
1707 }
1708 if (SetterMethod) {
1709 Diag(SetterMethod->getLocation(),
1710 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001711 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001712 Diag(Property->getLocation(), diag::note_property_declare);
1713 }
1714 }
1715
Ted Kremenek9d64c152010-03-12 00:38:38 +00001716 // We only care about readwrite atomic property.
1717 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1718 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1719 continue;
1720 if (const ObjCPropertyImplDecl *PIDecl
1721 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1722 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1723 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001724 if (!LookedUpGetterSetter) {
1725 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1726 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1727 LookedUpGetterSetter = true;
1728 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001729 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1730 SourceLocation MethodLoc =
1731 (GetterMethod ? GetterMethod->getLocation()
1732 : SetterMethod->getLocation());
1733 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001734 << Property->getIdentifier() << (GetterMethod != 0)
1735 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001736 // fixit stuff.
1737 if (!AttributesAsWritten) {
1738 if (Property->getLParenLoc().isValid()) {
1739 // @property () ... case.
1740 SourceRange PropSourceRange(Property->getAtLoc(),
1741 Property->getLParenLoc());
1742 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1743 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1744 }
1745 else {
1746 //@property id etc.
1747 SourceLocation endLoc =
1748 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1749 endLoc = endLoc.getLocWithOffset(-1);
1750 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1751 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1752 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1753 }
1754 }
1755 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1756 // @property () ... case.
1757 SourceLocation endLoc = Property->getLParenLoc();
1758 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1759 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1760 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1761 }
1762 else
1763 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001764 Diag(Property->getLocation(), diag::note_property_declare);
1765 }
1766 }
1767 }
1768}
1769
John McCallf85e1932011-06-15 23:02:42 +00001770void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001771 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001772 return;
1773
1774 for (ObjCImplementationDecl::propimpl_iterator
1775 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001776 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001777 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1778 continue;
1779
1780 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001781 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1782 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001783 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1784 if (!method)
1785 continue;
1786 ObjCMethodFamily family = method->getMethodFamily();
1787 if (family == OMF_alloc || family == OMF_copy ||
1788 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001789 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001790 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1791 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001792 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001793 Diag(PD->getLocation(), diag::note_property_declare);
1794 }
1795 }
1796 }
1797}
1798
John McCall5de74d12010-11-10 07:01:40 +00001799/// AddPropertyAttrs - Propagates attributes from a property to the
1800/// implicitly-declared getter or setter for that property.
1801static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1802 ObjCPropertyDecl *Property) {
1803 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001804 for (Decl::attr_iterator A = Property->attr_begin(),
1805 AEnd = Property->attr_end();
1806 A != AEnd; ++A) {
1807 if (isa<DeprecatedAttr>(*A) ||
1808 isa<UnavailableAttr>(*A) ||
1809 isa<AvailabilityAttr>(*A))
1810 PropertyMethod->addAttr((*A)->clone(S.Context));
1811 }
John McCall5de74d12010-11-10 07:01:40 +00001812}
1813
Ted Kremenek9d64c152010-03-12 00:38:38 +00001814/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1815/// have the property type and issue diagnostics if they don't.
1816/// Also synthesize a getter/setter method if none exist (and update the
1817/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1818/// methods is the "right" thing to do.
1819void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001820 ObjCContainerDecl *CD,
1821 ObjCPropertyDecl *redeclaredProperty,
1822 ObjCContainerDecl *lexicalDC) {
1823
Ted Kremenek9d64c152010-03-12 00:38:38 +00001824 ObjCMethodDecl *GetterMethod, *SetterMethod;
1825
1826 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1827 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1828 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1829 property->getLocation());
1830
1831 if (SetterMethod) {
1832 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1833 property->getPropertyAttributes();
1834 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1835 Context.getCanonicalType(SetterMethod->getResultType()) !=
1836 Context.VoidTy)
1837 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1838 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001839 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001840 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1841 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001842 Diag(property->getLocation(),
1843 diag::warn_accessor_property_type_mismatch)
1844 << property->getDeclName()
1845 << SetterMethod->getSelector();
1846 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1847 }
1848 }
1849
1850 // Synthesize getter/setter methods if none exist.
1851 // Find the default getter and if one not found, add one.
1852 // FIXME: The synthesized property we set here is misleading. We almost always
1853 // synthesize these methods unless the user explicitly provided prototypes
1854 // (which is odd, but allowed). Sema should be typechecking that the
1855 // declarations jive in that situation (which it is not currently).
1856 if (!GetterMethod) {
1857 // No instance method of same name as property getter name was found.
1858 // Declare a getter method and add it to the list of methods
1859 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001860 SourceLocation Loc = redeclaredProperty ?
1861 redeclaredProperty->getLocation() :
1862 property->getLocation();
1863
1864 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1865 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001866 property->getType(), 0, CD, /*isInstance=*/true,
1867 /*isVariadic=*/false, /*isSynthesized=*/true,
1868 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001869 (property->getPropertyImplementation() ==
1870 ObjCPropertyDecl::Optional) ?
1871 ObjCMethodDecl::Optional :
1872 ObjCMethodDecl::Required);
1873 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001874
1875 AddPropertyAttrs(*this, GetterMethod, property);
1876
Ted Kremenek23173d72010-05-18 21:09:07 +00001877 // FIXME: Eventually this shouldn't be needed, as the lexical context
1878 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001879 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001880 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001881 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1882 GetterMethod->addAttr(
1883 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001884 } else
1885 // A user declared getter will be synthesize when @synthesize of
1886 // the property with the same name is seen in the @implementation
1887 GetterMethod->setSynthesized(true);
1888 property->setGetterMethodDecl(GetterMethod);
1889
1890 // Skip setter if property is read-only.
1891 if (!property->isReadOnly()) {
1892 // Find the default setter and if one not found, add one.
1893 if (!SetterMethod) {
1894 // No instance method of same name as property setter name was found.
1895 // Declare a setter method and add it to the list of methods
1896 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001897 SourceLocation Loc = redeclaredProperty ?
1898 redeclaredProperty->getLocation() :
1899 property->getLocation();
1900
1901 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001902 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001903 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001904 CD, /*isInstance=*/true, /*isVariadic=*/false,
1905 /*isSynthesized=*/true,
1906 /*isImplicitlyDeclared=*/true,
1907 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001908 (property->getPropertyImplementation() ==
1909 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001910 ObjCMethodDecl::Optional :
1911 ObjCMethodDecl::Required);
1912
Ted Kremenek9d64c152010-03-12 00:38:38 +00001913 // Invent the arguments for the setter. We don't bother making a
1914 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001915 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1916 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001917 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001918 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001919 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001920 SC_None,
1921 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001922 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001923 SetterMethod->setMethodParams(Context, Argument,
1924 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001925
1926 AddPropertyAttrs(*this, SetterMethod, property);
1927
Ted Kremenek9d64c152010-03-12 00:38:38 +00001928 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001929 // FIXME: Eventually this shouldn't be needed, as the lexical context
1930 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001931 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001932 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001933 } else
1934 // A user declared setter will be synthesize when @synthesize of
1935 // the property with the same name is seen in the @implementation
1936 SetterMethod->setSynthesized(true);
1937 property->setSetterMethodDecl(SetterMethod);
1938 }
1939 // Add any synthesized methods to the global pool. This allows us to
1940 // handle the following, which is supported by GCC (and part of the design).
1941 //
1942 // @interface Foo
1943 // @property double bar;
1944 // @end
1945 //
1946 // void thisIsUnfortunate() {
1947 // id foo;
1948 // double bar = [foo bar];
1949 // }
1950 //
1951 if (GetterMethod)
1952 AddInstanceMethodToGlobalPool(GetterMethod);
1953 if (SetterMethod)
1954 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001955
1956 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1957 if (!CurrentClass) {
1958 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1959 CurrentClass = Cat->getClassInterface();
1960 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1961 CurrentClass = Impl->getClassInterface();
1962 }
1963 if (GetterMethod)
1964 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1965 if (SetterMethod)
1966 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001967}
1968
John McCalld226f652010-08-21 09:40:31 +00001969void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001970 SourceLocation Loc,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001971 unsigned &Attributes,
1972 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001973 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001974 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001975 return;
1976
1977 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001978 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001979
David Blaikie4e4d0842012-03-11 07:00:24 +00001980 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001981 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1982 PropertyTy->isObjCRetainableType()) {
1983 // 'readonly' property with no obvious lifetime.
1984 // its life time will be determined by its backing ivar.
1985 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1986 ObjCDeclSpec::DQ_PR_copy |
1987 ObjCDeclSpec::DQ_PR_retain |
1988 ObjCDeclSpec::DQ_PR_strong |
1989 ObjCDeclSpec::DQ_PR_weak |
1990 ObjCDeclSpec::DQ_PR_assign);
1991 if ((Attributes & rel) == 0)
1992 return;
1993 }
1994
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001995 if (propertyInPrimaryClass) {
1996 // we postpone most property diagnosis until class's implementation
1997 // because, its readonly attribute may be overridden in its class
1998 // extensions making other attributes, which make no sense, to make sense.
1999 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2000 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2001 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2002 << "readonly" << "readwrite";
2003 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002004 // readonly and readwrite/assign/retain/copy conflict.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002005 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2006 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002007 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002008 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002009 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002010 ObjCDeclSpec::DQ_PR_retain |
2011 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002012 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2013 "readwrite" :
2014 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2015 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00002016 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
2017 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00002018 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2019 "copy" : "retain";
2020
2021 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
2022 diag::err_objc_property_attr_mutually_exclusive :
2023 diag::warn_objc_property_attr_mutually_exclusive)
2024 << "readonly" << which;
2025 }
2026
2027 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00002028 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2029 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2030 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002031 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002032 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00002033 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2034 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2035 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2036 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002037 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002038 }
2039
2040 // Check for more than one of { assign, copy, retain }.
2041 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2042 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2043 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2044 << "assign" << "copy";
2045 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2046 }
2047 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2048 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2049 << "assign" << "retain";
2050 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2051 }
John McCallf85e1932011-06-15 23:02:42 +00002052 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2053 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2054 << "assign" << "strong";
2055 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2056 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002057 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002058 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2059 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2060 << "assign" << "weak";
2061 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2062 }
2063 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2064 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2065 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2066 << "unsafe_unretained" << "copy";
2067 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2068 }
2069 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2070 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2071 << "unsafe_unretained" << "retain";
2072 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2073 }
2074 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2075 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2076 << "unsafe_unretained" << "strong";
2077 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2078 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002079 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002080 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2081 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2082 << "unsafe_unretained" << "weak";
2083 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2084 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002085 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2086 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2087 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2088 << "copy" << "retain";
2089 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2090 }
John McCallf85e1932011-06-15 23:02:42 +00002091 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2092 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2093 << "copy" << "strong";
2094 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2095 }
2096 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2097 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2098 << "copy" << "weak";
2099 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2100 }
2101 }
2102 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2103 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2104 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2105 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002106 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002107 }
2108 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2109 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2110 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2111 << "strong" << "weak";
2112 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002113 }
2114
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002115 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2116 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2117 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2118 << "atomic" << "nonatomic";
2119 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2120 }
2121
Ted Kremenek9d64c152010-03-12 00:38:38 +00002122 // Warn if user supplied no assignment attribute, property is
2123 // readwrite, and this is an object type.
2124 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002125 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2126 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2127 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002128 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002129 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002130 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002131 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002132 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002133 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002134 bool isAnyClassTy =
2135 (PropertyTy->isObjCClassType() ||
2136 PropertyTy->isObjCQualifiedClassType());
2137 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2138 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002139 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002140 ;
2141 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002142 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002143 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002144 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002145
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002146 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002147 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002148 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002149 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002150 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002151
2152 // FIXME: Implement warning dependent on NSCopying being
2153 // implemented. See also:
2154 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2155 // (please trim this list while you are at it).
2156 }
2157
2158 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002159 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002160 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002161 && PropertyTy->isBlockPointerType())
2162 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian7c16d582012-06-27 20:52:46 +00002163 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002164 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2165 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2166 PropertyTy->isBlockPointerType())
2167 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002168
2169 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2170 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2171 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2172
Ted Kremenek9d64c152010-03-12 00:38:38 +00002173}