blob: 84b609622a30edbce9354896bed7b2c929ad1332 [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
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000105static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
106 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
107 T.isObjCGCWeak()) ||
108 (S.getLangOpts().ObjCAutoRefCount &&
109 T.getObjCLifetime() == Qualifiers::OCL_Weak))
110 return ObjCDeclSpec::DQ_PR_weak;
111 return 0;
112}
113
John McCalld226f652010-08-21 09:40:31 +0000114Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000115 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000116 FieldDeclarator &FD,
117 ObjCDeclSpec &ODS,
118 Selector GetterSel,
119 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000120 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000121 tok::ObjCKeywordKind MethodImplKind,
122 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000123 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000124 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
125 QualType T = TSI->getType();
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000126 Attributes |= deduceWeakPropertyFromType(*this, T);
127
Ted Kremenek28685ab2010-03-12 00:46:40 +0000128 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
129 // default is readwrite!
130 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
131 // property is defaulted to 'assign' if it is readwrite and is
132 // not retain or copy
133 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
134 (isReadWrite &&
135 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +0000136 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
137 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
138 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
139 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000140
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000141 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000142 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000143 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000144 if (CDecl->IsClassExtension()) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000145 Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000146 FD, GetterSel, SetterSel,
147 isAssign, isReadWrite,
148 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000149 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000150 isOverridingProperty, TSI,
151 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000152 if (Res) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000153 CheckObjCPropertyAttributes(Res, AtLoc, Attributes, false);
David Blaikie4e4d0842012-03-11 07:00:24 +0000154 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000155 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
156 }
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000157 ActOnDocumentableDecl(Res);
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000158 return Res;
159 }
160
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000161 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
John McCallf85e1932011-06-15 23:02:42 +0000162 GetterSel, SetterSel,
163 isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000164 Attributes,
165 ODS.getPropertyAttributes(),
166 TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000167 if (lexicalDC)
168 Res->setLexicalDeclContext(lexicalDC);
169
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000170 // Validate the attributes on the @property.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000171 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
172 (isa<ObjCInterfaceDecl>(ClassDecl) ||
173 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000174
David Blaikie4e4d0842012-03-11 07:00:24 +0000175 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000176 checkARCPropertyDecl(*this, Res);
177
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000178 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000179 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000180}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000181
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000182static ObjCPropertyDecl::PropertyAttributeKind
183makePropertyAttributesAsWritten(unsigned Attributes) {
184 unsigned attributesAsWritten = 0;
185 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
186 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
187 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
188 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
189 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
190 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
191 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
192 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
193 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
194 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
195 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
196 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
197 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
198 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
199 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
200 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
201 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
202 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
203 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
204 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
205 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
206 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
207 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
208 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
209
210 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
211}
212
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000213static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000214 SourceLocation LParenLoc, SourceLocation &Loc) {
215 if (LParenLoc.isMacroID())
216 return false;
217
218 SourceManager &SM = Context.getSourceManager();
219 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
220 // Try to load the file buffer.
221 bool invalidTemp = false;
222 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
223 if (invalidTemp)
224 return false;
225 const char *tokenBegin = file.data() + locInfo.second;
226
227 // Lex from the start of the given location.
228 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
229 Context.getLangOpts(),
230 file.begin(), tokenBegin, file.end());
231 Token Tok;
232 do {
233 lexer.LexFromRawLexer(Tok);
234 if (Tok.is(tok::raw_identifier) &&
235 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
236 Loc = Tok.getLocation();
237 return true;
238 }
239 } while (Tok.isNot(tok::r_paren));
240 return false;
241
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000242}
243
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000244static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000245 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
246 ObjCPropertyDecl::OBJC_PR_retain |
247 ObjCPropertyDecl::OBJC_PR_copy |
248 ObjCPropertyDecl::OBJC_PR_weak |
249 ObjCPropertyDecl::OBJC_PR_strong |
250 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
251}
252
John McCalld226f652010-08-21 09:40:31 +0000253Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000254Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000255 SourceLocation AtLoc,
256 SourceLocation LParenLoc,
257 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000258 Selector GetterSel, Selector SetterSel,
259 const bool isAssign,
260 const bool isReadWrite,
261 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000262 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000263 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000264 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000265 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000266 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000267 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000268 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000269 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000270 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
271
272 if (CCPrimary)
273 // Check for duplicate declaration of this property in current and
274 // other class extensions.
275 for (const ObjCCategoryDecl *ClsExtDecl =
276 CCPrimary->getFirstClassExtension();
277 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
278 if (ObjCPropertyDecl *prevDecl =
279 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
280 Diag(AtLoc, diag::err_duplicate_property);
281 Diag(prevDecl->getLocation(), diag::note_property_declare);
282 return 0;
283 }
284 }
285
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000286 // Create a new ObjCPropertyDecl with the DeclContext being
287 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000288 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000289 ObjCPropertyDecl *PDecl =
290 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000291 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000292 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000293 makePropertyAttributesAsWritten(AttributesAsWritten));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000294 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
295 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
296 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
297 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000298 // Set setter/getter selector name. Needed later.
299 PDecl->setGetterName(GetterSel);
300 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000301 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000302 DC->addDecl(PDecl);
303
304 // We need to look in the @interface to see if the @property was
305 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000306 if (!CCPrimary) {
307 Diag(CDecl->getLocation(), diag::err_continuation_class);
308 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000309 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000310 }
311
312 // Find the property in continuation class's primary class only.
313 ObjCPropertyDecl *PIDecl =
314 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
315
316 if (!PIDecl) {
317 // No matching property found in the primary class. Just fall thru
318 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000319 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000320 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000321 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000322 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000323
324 // A case of continuation class adding a new property in the class. This
325 // is not what it was meant for. However, gcc supports it and so should we.
326 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000327 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000328 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000329 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
330 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000331 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000332 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
333 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000334 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000335 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
336 bool IncompatibleObjC = false;
337 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000338 // Relax the strict type matching for property type in continuation class.
339 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000340 // as it narrows the object type in its primary class property. Note that
341 // this conversion is safe only because the wider type is for a 'readonly'
342 // property in primary class and 'narrowed' type for a 'readwrite' property
343 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000344 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
345 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
346 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
347 ConvertedType, IncompatibleObjC))
348 || IncompatibleObjC) {
349 Diag(AtLoc,
350 diag::err_type_mismatch_continuation_class) << PDecl->getType();
351 Diag(PIDecl->getLocation(), diag::note_property_declare);
352 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000353 }
354
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000355 // The property 'PIDecl's readonly attribute will be over-ridden
356 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000357 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000358 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000359 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000360 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
361 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000362 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
363 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000364 Diag(AtLoc, diag::warn_property_attr_mismatch);
365 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000366 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000367 DeclContext *DC = cast<DeclContext>(CCPrimary);
368 if (!ObjCPropertyDecl::findPropertyDecl(DC,
369 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000370 // Protocol is not in the primary class. Must build one for it.
371 ObjCDeclSpec ProtocolPropertyODS;
372 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
373 // and ObjCPropertyDecl::PropertyAttributeKind have identical
374 // values. Should consolidate both into one enum type.
375 ProtocolPropertyODS.
376 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
377 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000378 // Must re-establish the context from class extension to primary
379 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000380 ContextRAII SavedContext(*this, CCPrimary);
381
John McCalld226f652010-08-21 09:40:31 +0000382 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000383 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000384 PIDecl->getGetterName(),
385 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000386 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000387 MethodImplKind,
388 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000389 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000390 }
391 PIDecl->makeitReadWriteAttribute();
392 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
393 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000394 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
395 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000396 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
397 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
398 PIDecl->setSetterName(SetterSel);
399 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000400 // Tailor the diagnostics for the common case where a readwrite
401 // property is declared both in the @interface and the continuation.
402 // This is a common error where the user often intended the original
403 // declaration to be readonly.
404 unsigned diag =
405 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
406 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
407 ? diag::err_use_continuation_class_redeclaration_readwrite
408 : diag::err_use_continuation_class;
409 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000410 << CCPrimary->getDeclName();
411 Diag(PIDecl->getLocation(), diag::note_property_declare);
412 }
413 *isOverridingProperty = true;
414 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000415 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000416 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
417 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000418 if (ASTMutationListener *L = Context.getASTMutationListener())
419 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000420 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000421}
422
423ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
424 ObjCContainerDecl *CDecl,
425 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000426 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000427 FieldDeclarator &FD,
428 Selector GetterSel,
429 Selector SetterSel,
430 const bool isAssign,
431 const bool isReadWrite,
432 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000433 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000434 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000435 tok::ObjCKeywordKind MethodImplKind,
436 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000437 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000438 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000439
440 // Issue a warning if property is 'assign' as default and its object, which is
441 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000442 if (getLangOpts().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000443 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000444 if (const ObjCObjectPointerType *ObjPtrTy =
445 T->getAs<ObjCObjectPointerType>()) {
446 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
447 if (IDecl)
448 if (ObjCProtocolDecl* PNSCopying =
449 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
450 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
451 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000452 }
John McCallc12c5bb2010-05-15 11:32:37 +0000453 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000454 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
455
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000456 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000457 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
458 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000459 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000460
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000461 if (ObjCPropertyDecl *prevDecl =
462 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000463 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000464 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000465 PDecl->setInvalidDecl();
466 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000467 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000468 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000469 if (lexicalDC)
470 PDecl->setLexicalDeclContext(lexicalDC);
471 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000472
473 if (T->isArrayType() || T->isFunctionType()) {
474 Diag(AtLoc, diag::err_property_type) << T;
475 PDecl->setInvalidDecl();
476 }
477
478 ProcessDeclAttributes(S, PDecl, FD.D);
479
480 // Regardless of setter/getter attribute, we save the default getter/setter
481 // selector names in anticipation of declaration of setter/getter methods.
482 PDecl->setGetterName(GetterSel);
483 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000484 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000485 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000486
Ted Kremenek28685ab2010-03-12 00:46:40 +0000487 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
488 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
489
490 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
491 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
492
493 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
494 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
495
496 if (isReadWrite)
497 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
498
499 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
500 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
501
John McCallf85e1932011-06-15 23:02:42 +0000502 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
503 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
504
505 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
506 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
507
Ted Kremenek28685ab2010-03-12 00:46:40 +0000508 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
509 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
510
John McCallf85e1932011-06-15 23:02:42 +0000511 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
512 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
513
Ted Kremenek28685ab2010-03-12 00:46:40 +0000514 if (isAssign)
515 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
516
John McCall265941b2011-09-13 18:31:23 +0000517 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000518 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
519 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000520 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000521 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000522
John McCallf85e1932011-06-15 23:02:42 +0000523 // 'unsafe_unretained' is alias for 'assign'.
524 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
525 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
526 if (isAssign)
527 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
528
Ted Kremenek28685ab2010-03-12 00:46:40 +0000529 if (MethodImplKind == tok::objc_required)
530 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
531 else if (MethodImplKind == tok::objc_optional)
532 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000533
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000534 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000535}
536
John McCallf85e1932011-06-15 23:02:42 +0000537static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
538 ObjCPropertyDecl *property,
539 ObjCIvarDecl *ivar) {
540 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
541
John McCallf85e1932011-06-15 23:02:42 +0000542 QualType ivarType = ivar->getType();
543 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000544
John McCall265941b2011-09-13 18:31:23 +0000545 // The lifetime implied by the property's attributes.
546 Qualifiers::ObjCLifetime propertyLifetime =
547 getImpliedARCOwnership(property->getPropertyAttributes(),
548 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000549
John McCall265941b2011-09-13 18:31:23 +0000550 // We're fine if they match.
551 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000552
John McCall265941b2011-09-13 18:31:23 +0000553 // These aren't valid lifetimes for object ivars; don't diagnose twice.
554 if (ivarLifetime == Qualifiers::OCL_None ||
555 ivarLifetime == Qualifiers::OCL_Autoreleasing)
556 return;
John McCallf85e1932011-06-15 23:02:42 +0000557
John McCalld64c2eb2012-08-20 23:36:59 +0000558 // If the ivar is private, and it's implicitly __unsafe_unretained
559 // becaues of its type, then pretend it was actually implicitly
560 // __strong. This is only sound because we're processing the
561 // property implementation before parsing any method bodies.
562 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
563 propertyLifetime == Qualifiers::OCL_Strong &&
564 ivar->getAccessControl() == ObjCIvarDecl::Private) {
565 SplitQualType split = ivarType.split();
566 if (split.Quals.hasObjCLifetime()) {
567 assert(ivarType->isObjCARCImplicitlyUnretainedType());
568 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
569 ivarType = S.Context.getQualifiedType(split);
570 ivar->setType(ivarType);
571 return;
572 }
573 }
574
John McCall265941b2011-09-13 18:31:23 +0000575 switch (propertyLifetime) {
576 case Qualifiers::OCL_Strong:
577 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
578 << property->getDeclName()
579 << ivar->getDeclName()
580 << ivarLifetime;
581 break;
John McCallf85e1932011-06-15 23:02:42 +0000582
John McCall265941b2011-09-13 18:31:23 +0000583 case Qualifiers::OCL_Weak:
584 S.Diag(propertyImplLoc, diag::error_weak_property)
585 << property->getDeclName()
586 << ivar->getDeclName();
587 break;
John McCallf85e1932011-06-15 23:02:42 +0000588
John McCall265941b2011-09-13 18:31:23 +0000589 case Qualifiers::OCL_ExplicitNone:
590 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
591 << property->getDeclName()
592 << ivar->getDeclName()
593 << ((property->getPropertyAttributesAsWritten()
594 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
595 break;
John McCallf85e1932011-06-15 23:02:42 +0000596
John McCall265941b2011-09-13 18:31:23 +0000597 case Qualifiers::OCL_Autoreleasing:
598 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000599
John McCall265941b2011-09-13 18:31:23 +0000600 case Qualifiers::OCL_None:
601 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000602 return;
603 }
604
605 S.Diag(property->getLocation(), diag::note_property_declare);
606}
607
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000608/// setImpliedPropertyAttributeForReadOnlyProperty -
609/// This routine evaludates life-time attributes for a 'readonly'
610/// property with no known lifetime of its own, using backing
611/// 'ivar's attribute, if any. If no backing 'ivar', property's
612/// life-time is assumed 'strong'.
613static void setImpliedPropertyAttributeForReadOnlyProperty(
614 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
615 Qualifiers::ObjCLifetime propertyLifetime =
616 getImpliedARCOwnership(property->getPropertyAttributes(),
617 property->getType());
618 if (propertyLifetime != Qualifiers::OCL_None)
619 return;
620
621 if (!ivar) {
622 // if no backing ivar, make property 'strong'.
623 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
624 return;
625 }
626 // property assumes owenership of backing ivar.
627 QualType ivarType = ivar->getType();
628 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
629 if (ivarLifetime == Qualifiers::OCL_Strong)
630 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
631 else if (ivarLifetime == Qualifiers::OCL_Weak)
632 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
633 return;
634}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000635
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000636/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
637/// attribute declared in primary class and attributes overridden in any of its
638/// class extensions.
639static void
640DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
641 ObjCPropertyDecl *property) {
642 unsigned Attributes = property->getPropertyAttributesAsWritten();
643 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
644 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
645 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000646 ObjCPropertyDecl *ClassExtProperty = 0;
647 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
648 E = CDecl->prop_end(); P != E; ++P) {
649 if ((*P)->getIdentifier() == property->getIdentifier()) {
650 ClassExtProperty = *P;
651 break;
652 }
653 }
654 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000655 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000656 unsigned classExtPropertyAttr =
657 ClassExtProperty->getPropertyAttributesAsWritten();
658 // We are issuing the warning that we postponed because class extensions
659 // can override readonly->readwrite and 'setter' attributes originally
660 // placed on class's property declaration now make sense in the overridden
661 // property.
662 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
663 if (!classExtPropertyAttr ||
664 (classExtPropertyAttr & ObjCDeclSpec::DQ_PR_readwrite))
665 continue;
666 warn = true;
667 break;
668 }
669 }
670 }
671 if (warn) {
672 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
673 ObjCDeclSpec::DQ_PR_unsafe_unretained |
674 ObjCDeclSpec::DQ_PR_copy |
675 ObjCDeclSpec::DQ_PR_retain |
676 ObjCDeclSpec::DQ_PR_strong);
677 if (Attributes & setterAttrs) {
678 const char * which =
679 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
680 "assign" :
681 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
682 "unsafe_unretained" :
683 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
684 "copy" :
685 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
686 "retain" : "strong";
687
688 S.Diag(property->getLocation(),
689 diag::warn_objc_property_attr_mutually_exclusive)
690 << "readonly" << which;
691 }
692 }
693
694
695}
696
Ted Kremenek28685ab2010-03-12 00:46:40 +0000697/// ActOnPropertyImplDecl - This routine performs semantic checks and
698/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000699/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000700///
John McCalld226f652010-08-21 09:40:31 +0000701Decl *Sema::ActOnPropertyImplDecl(Scope *S,
702 SourceLocation AtLoc,
703 SourceLocation PropertyLoc,
704 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000705 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000706 IdentifierInfo *PropertyIvar,
707 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000708 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000709 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000710 // Make sure we have a context for the property implementation declaration.
711 if (!ClassImpDecl) {
712 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000713 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000714 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000715 if (PropertyIvarLoc.isInvalid())
716 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000717 SourceLocation PropertyDiagLoc = PropertyLoc;
718 if (PropertyDiagLoc.isInvalid())
719 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000720 ObjCPropertyDecl *property = 0;
721 ObjCInterfaceDecl* IDecl = 0;
722 // Find the class or category class where this property must have
723 // a declaration.
724 ObjCImplementationDecl *IC = 0;
725 ObjCCategoryImplDecl* CatImplClass = 0;
726 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
727 IDecl = IC->getClassInterface();
728 // We always synthesize an interface for an implementation
729 // without an interface decl. So, IDecl is always non-zero.
730 assert(IDecl &&
731 "ActOnPropertyImplDecl - @implementation without @interface");
732
733 // Look for this property declaration in the @implementation's @interface
734 property = IDecl->FindPropertyDeclaration(PropertyId);
735 if (!property) {
736 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000737 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000738 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000739 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000740 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
741 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000742 if (AtLoc.isValid())
743 Diag(AtLoc, diag::warn_implicit_atomic_property);
744 else
745 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
746 Diag(property->getLocation(), diag::note_property_declare);
747 }
748
Ted Kremenek28685ab2010-03-12 00:46:40 +0000749 if (const ObjCCategoryDecl *CD =
750 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
751 if (!CD->IsClassExtension()) {
752 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
753 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000754 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000755 }
756 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000757
758 if (Synthesize&&
759 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
760 property->hasAttr<IBOutletAttr>() &&
761 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000762 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
763 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000764 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000765 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000766 property->getLParenLoc(), readonlyLoc)) {
767 SourceLocation endLoc =
768 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
769 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
770 Diag(property->getLocation(),
771 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
772 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
773 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000774 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000775
776 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000777
Ted Kremenek28685ab2010-03-12 00:46:40 +0000778 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
779 if (Synthesize) {
780 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000781 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000782 }
783 IDecl = CatImplClass->getClassInterface();
784 if (!IDecl) {
785 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000786 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000787 }
788 ObjCCategoryDecl *Category =
789 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
790
791 // If category for this implementation not found, it is an error which
792 // has already been reported eralier.
793 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000794 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000795 // Look for this property declaration in @implementation's category
796 property = Category->FindPropertyDeclaration(PropertyId);
797 if (!property) {
798 Diag(PropertyLoc, diag::error_bad_category_property_decl)
799 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000800 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000801 }
802 } else {
803 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000804 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000805 }
806 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000807 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000808 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000809 // Check that we have a valid, previously declared ivar for @synthesize
810 if (Synthesize) {
811 // @synthesize
812 if (!PropertyIvar)
813 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000814 // Check that this is a previously declared 'ivar' in 'IDecl' interface
815 ObjCInterfaceDecl *ClassDeclared;
816 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
817 QualType PropType = property->getType();
818 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000819
820 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000821 diag::err_incomplete_synthesized_property,
822 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000823 Diag(property->getLocation(), diag::note_property_declare);
824 CompleteTypeErr = true;
825 }
826
David Blaikie4e4d0842012-03-11 07:00:24 +0000827 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000828 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000829 ObjCPropertyDecl::OBJC_PR_readonly) &&
830 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000831 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
832 }
833
John McCallf85e1932011-06-15 23:02:42 +0000834 ObjCPropertyDecl::PropertyAttributeKind kind
835 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000836
837 // Add GC __weak to the ivar type if the property is weak.
838 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000839 getLangOpts().getGC() != LangOptions::NonGC) {
840 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000841 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000842 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000843 Diag(property->getLocation(), diag::note_property_declare);
844 } else {
845 PropertyIvarType =
846 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000847 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000848 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000849 if (AtLoc.isInvalid()) {
850 // Check when default synthesizing a property that there is
851 // an ivar matching property name and issue warning; since this
852 // is the most common case of not using an ivar used for backing
853 // property in non-default synthesis case.
854 ObjCInterfaceDecl *ClassDeclared=0;
855 ObjCIvarDecl *originalIvar =
856 IDecl->lookupInstanceVariable(property->getIdentifier(),
857 ClassDeclared);
858 if (originalIvar) {
859 Diag(PropertyDiagLoc,
860 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000861 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000862 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000863 Diag(property->getLocation(), diag::note_property_declare);
864 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000865 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000866 }
867
868 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000869 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000870 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000871 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000872 !PropertyIvarType.getObjCLifetime() &&
873 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000874
John McCall265941b2011-09-13 18:31:23 +0000875 // It's an error if we have to do this and the user didn't
876 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000877 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000878 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000879 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000880 diag::err_arc_objc_property_default_assign_on_object);
881 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000882 } else {
883 Qualifiers::ObjCLifetime lifetime =
884 getImpliedARCOwnership(kind, PropertyIvarType);
885 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000886 if (lifetime == Qualifiers::OCL_Weak) {
887 bool err = false;
888 if (const ObjCObjectPointerType *ObjT =
889 PropertyIvarType->getAs<ObjCObjectPointerType>())
890 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000891 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000892 Diag(property->getLocation(), diag::note_property_declare);
893 err = true;
894 }
John McCall0a7dd782012-08-21 02:47:43 +0000895 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000896 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000897 Diag(property->getLocation(), diag::note_property_declare);
898 }
John McCallf85e1932011-06-15 23:02:42 +0000899 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000900
John McCallf85e1932011-06-15 23:02:42 +0000901 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000902 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000903 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
904 }
John McCallf85e1932011-06-15 23:02:42 +0000905 }
906
907 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000908 !getLangOpts().ObjCAutoRefCount &&
909 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000910 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000911 Diag(property->getLocation(), diag::note_property_declare);
912 }
913
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000914 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000915 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000916 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000917 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000918 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000919 if (CompleteTypeErr)
920 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000921 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000922 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000923 property->setPropertyIvarDecl(Ivar);
924
John McCall260611a2012-06-20 06:18:46 +0000925 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000926 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
927 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000928 // Note! I deliberately want it to fall thru so, we have a
929 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000930 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000931 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000932 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000933 << property->getDeclName() << Ivar->getDeclName()
934 << ClassDeclared->getDeclName();
935 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000936 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000937 // Note! I deliberately want it to fall thru so more errors are caught.
938 }
939 QualType IvarType = Context.getCanonicalType(Ivar->getType());
940
941 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000942 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
943 compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000944 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000945 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000946 compat =
947 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000948 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000949 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000950 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000951 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
952 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000953 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000954 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000955 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000956 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000957 << property->getDeclName() << PropType
958 << Ivar->getDeclName() << IvarType;
959 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000960 // Note! I deliberately want it to fall thru so, we have a
961 // a property implementation and to avoid future warnings.
962 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000963 else {
964 // FIXME! Rules for properties are somewhat different that those
965 // for assignments. Use a new routine to consolidate all cases;
966 // specifically for property redeclarations as well as for ivars.
967 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
968 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
969 if (lhsType != rhsType &&
970 lhsType->isArithmeticType()) {
971 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
972 << property->getDeclName() << PropType
973 << Ivar->getDeclName() << IvarType;
974 Diag(Ivar->getLocation(), diag::note_ivar_decl);
975 // Fall thru - see previous comment
976 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000977 }
978 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000979 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000980 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000981 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000982 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000983 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000984 // Fall thru - see previous comment
985 }
John McCallf85e1932011-06-15 23:02:42 +0000986 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000987 if ((property->getType()->isObjCObjectPointerType() ||
988 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000989 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000990 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000991 << property->getDeclName() << Ivar->getDeclName();
992 // Fall thru - see previous comment
993 }
994 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000995 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000996 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000997 } else if (PropertyIvar)
998 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +0000999 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001000
Ted Kremenek28685ab2010-03-12 00:46:40 +00001001 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1002 ObjCPropertyImplDecl *PIDecl =
1003 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1004 property,
1005 (Synthesize ?
1006 ObjCPropertyImplDecl::Synthesize
1007 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001008 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001009
Fariborz Jahanian74414712012-05-15 18:12:51 +00001010 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001011 PIDecl->setInvalidDecl();
1012
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001013 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1014 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001015 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001016 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001017 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1018 // returned by the getter as it must conform to C++'s copy-return rules.
1019 // FIXME. Eventually we want to do this for Objective-C as well.
1020 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1021 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001022 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001023 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001024 Expr *IvarRefExpr =
1025 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1026 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001027 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001028 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001029 SourceLocation(),
1030 getterMethod->getResultType(),
1031 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001032 SourceLocation(),
1033 Owned(IvarRefExpr));
1034 if (!Res.isInvalid()) {
1035 Expr *ResExpr = Res.takeAs<Expr>();
1036 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001037 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001038 PIDecl->setGetterCXXConstructor(ResExpr);
1039 }
1040 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001041 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1042 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1043 Diag(getterMethod->getLocation(),
1044 diag::warn_property_getter_owning_mismatch);
1045 Diag(property->getLocation(), diag::note_property_declare);
1046 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001047 }
1048 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1049 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001050 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1051 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001052 // FIXME. Eventually we want to do this for Objective-C as well.
1053 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1054 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001055 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001056 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001057 Expr *lhs =
1058 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1059 SelfExpr, true, true);
1060 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1061 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001062 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +00001063 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +00001064 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +00001065 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +00001066 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001067 if (property->getPropertyAttributes() &
1068 ObjCPropertyDecl::OBJC_PR_atomic) {
1069 Expr *callExpr = Res.takeAs<Expr>();
1070 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001071 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1072 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001073 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001074 if (property->getType()->isReferenceType()) {
1075 Diag(PropertyLoc,
1076 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001077 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001078 Diag(FuncDecl->getLocStart(),
1079 diag::note_callee_decl) << FuncDecl;
1080 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001081 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001082 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1083 }
1084 }
1085
Ted Kremenek28685ab2010-03-12 00:46:40 +00001086 if (IC) {
1087 if (Synthesize)
1088 if (ObjCPropertyImplDecl *PPIDecl =
1089 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1090 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1091 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1092 << PropertyIvar;
1093 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1094 }
1095
1096 if (ObjCPropertyImplDecl *PPIDecl
1097 = IC->FindPropertyImplDecl(PropertyId)) {
1098 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1099 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001100 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001101 }
1102 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001103 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001104 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001105 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001106 // Diagnose if an ivar was lazily synthesdized due to a previous
1107 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001108 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001109 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001110 ObjCIvarDecl *Ivar = 0;
1111 if (!Synthesize)
1112 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1113 else {
1114 if (PropertyIvar && PropertyIvar != PropertyId)
1115 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1116 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001117 // Issue diagnostics only if Ivar belongs to current class.
1118 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001119 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001120 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1121 << PropertyId;
1122 Ivar->setInvalidDecl();
1123 }
1124 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001125 } else {
1126 if (Synthesize)
1127 if (ObjCPropertyImplDecl *PPIDecl =
1128 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001129 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001130 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1131 << PropertyIvar;
1132 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1133 }
1134
1135 if (ObjCPropertyImplDecl *PPIDecl =
1136 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001137 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001138 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001139 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001140 }
1141 CatImplClass->addPropertyImplementation(PIDecl);
1142 }
1143
John McCalld226f652010-08-21 09:40:31 +00001144 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001145}
1146
1147//===----------------------------------------------------------------------===//
1148// Helper methods.
1149//===----------------------------------------------------------------------===//
1150
Ted Kremenek9d64c152010-03-12 00:38:38 +00001151/// DiagnosePropertyMismatch - Compares two properties for their
1152/// attributes and types and warns on a variety of inconsistencies.
1153///
1154void
1155Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1156 ObjCPropertyDecl *SuperProperty,
1157 const IdentifierInfo *inheritedName) {
1158 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1159 Property->getPropertyAttributes();
1160 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1161 SuperProperty->getPropertyAttributes();
1162 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1163 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1164 Diag(Property->getLocation(), diag::warn_readonly_property)
1165 << Property->getDeclName() << inheritedName;
1166 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1167 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1168 Diag(Property->getLocation(), diag::warn_property_attribute)
1169 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001170 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001171 unsigned CAttrRetain =
1172 (CAttr &
1173 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1174 unsigned SAttrRetain =
1175 (SAttr &
1176 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1177 bool CStrong = (CAttrRetain != 0);
1178 bool SStrong = (SAttrRetain != 0);
1179 if (CStrong != SStrong)
1180 Diag(Property->getLocation(), diag::warn_property_attribute)
1181 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1182 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001183
1184 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1185 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1186 Diag(Property->getLocation(), diag::warn_property_attribute)
1187 << Property->getDeclName() << "atomic" << inheritedName;
1188 if (Property->getSetterName() != SuperProperty->getSetterName())
1189 Diag(Property->getLocation(), diag::warn_property_attribute)
1190 << Property->getDeclName() << "setter" << inheritedName;
1191 if (Property->getGetterName() != SuperProperty->getGetterName())
1192 Diag(Property->getLocation(), diag::warn_property_attribute)
1193 << Property->getDeclName() << "getter" << inheritedName;
1194
1195 QualType LHSType =
1196 Context.getCanonicalType(SuperProperty->getType());
1197 QualType RHSType =
1198 Context.getCanonicalType(Property->getType());
1199
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001200 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001201 // Do cases not handled in above.
1202 // FIXME. For future support of covariant property types, revisit this.
1203 bool IncompatibleObjC = false;
1204 QualType ConvertedType;
1205 if (!isObjCPointerConversion(RHSType, LHSType,
1206 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001207 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001208 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1209 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001210 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1211 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001212 }
1213}
1214
1215bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1216 ObjCMethodDecl *GetterMethod,
1217 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001218 if (!GetterMethod)
1219 return false;
1220 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1221 QualType PropertyIvarType = property->getType().getNonReferenceType();
1222 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1223 if (!compat) {
1224 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1225 isa<ObjCObjectPointerType>(GetterType))
1226 compat =
1227 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001228 GetterType->getAs<ObjCObjectPointerType>(),
1229 PropertyIvarType->getAs<ObjCObjectPointerType>());
1230 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001231 != Compatible) {
1232 Diag(Loc, diag::error_property_accessor_type)
1233 << property->getDeclName() << PropertyIvarType
1234 << GetterMethod->getSelector() << GetterType;
1235 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1236 return true;
1237 } else {
1238 compat = true;
1239 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1240 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1241 if (lhsType != rhsType && lhsType->isArithmeticType())
1242 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001243 }
1244 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001245
1246 if (!compat) {
1247 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1248 << property->getDeclName()
1249 << GetterMethod->getSelector();
1250 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1251 return true;
1252 }
1253
Ted Kremenek9d64c152010-03-12 00:38:38 +00001254 return false;
1255}
1256
1257/// ComparePropertiesInBaseAndSuper - This routine compares property
1258/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001259/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001260///
1261void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1262 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1263 if (!SDecl)
1264 return;
1265 // FIXME: O(N^2)
1266 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1267 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001268 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001269 // Does property in super class has declaration in current class?
1270 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1271 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001272 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001273 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1274 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1275 SDecl->getIdentifier());
1276 }
1277 }
1278}
1279
1280/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1281/// of properties declared in a protocol and compares their attribute against
1282/// the same property declared in the class or category.
1283void
1284Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1285 ObjCProtocolDecl *PDecl) {
1286 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1287 if (!IDecl) {
1288 // Category
1289 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1290 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1291 if (!CatDecl->IsClassExtension())
1292 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1293 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001294 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001295 ObjCCategoryDecl::prop_iterator CP, CE;
1296 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001297 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001298 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001299 break;
1300 if (CP != CE)
1301 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001302 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001303 }
1304 return;
1305 }
1306 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1307 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001308 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001309 ObjCInterfaceDecl::prop_iterator CP, CE;
1310 // Is this property already in class's list of properties?
1311 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001312 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001313 break;
1314 if (CP != CE)
1315 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001316 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001317 }
1318}
1319
1320/// CompareProperties - This routine compares properties
1321/// declared in 'ClassOrProtocol' objects (which can be a class or an
1322/// inherited protocol with the list of properties for class/category 'CDecl'
1323///
John McCalld226f652010-08-21 09:40:31 +00001324void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1325 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001326 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1327
1328 if (!IDecl) {
1329 // Category
1330 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1331 assert (CatDecl && "CompareProperties");
1332 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1333 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1334 E = MDecl->protocol_end(); P != E; ++P)
1335 // Match properties of category with those of protocol (*P)
1336 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1337
1338 // Go thru the list of protocols for this category and recursively match
1339 // their properties with those in the category.
1340 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1341 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001342 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001343 } else {
1344 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1345 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1346 E = MD->protocol_end(); P != E; ++P)
1347 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1348 }
1349 return;
1350 }
1351
1352 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001353 for (ObjCInterfaceDecl::all_protocol_iterator
1354 P = MDecl->all_referenced_protocol_begin(),
1355 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001356 // Match properties of class IDecl with those of protocol (*P).
1357 MatchOneProtocolPropertiesInClass(IDecl, *P);
1358
1359 // Go thru the list of protocols for this class and recursively match
1360 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001361 for (ObjCInterfaceDecl::all_protocol_iterator
1362 P = IDecl->all_referenced_protocol_begin(),
1363 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001364 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001365 } else {
1366 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1367 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1368 E = MD->protocol_end(); P != E; ++P)
1369 MatchOneProtocolPropertiesInClass(IDecl, *P);
1370 }
1371}
1372
1373/// isPropertyReadonly - Return true if property is readonly, by searching
1374/// for the property in the class and in its categories and implementations
1375///
1376bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1377 ObjCInterfaceDecl *IDecl) {
1378 // by far the most common case.
1379 if (!PDecl->isReadOnly())
1380 return false;
1381 // Even if property is ready only, if interface has a user defined setter,
1382 // it is not considered read only.
1383 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1384 return false;
1385
1386 // Main class has the property as 'readonly'. Must search
1387 // through the category list to see if the property's
1388 // attribute has been over-ridden to 'readwrite'.
1389 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1390 Category; Category = Category->getNextClassCategory()) {
1391 // Even if property is ready only, if a category has a user defined setter,
1392 // it is not considered read only.
1393 if (Category->getInstanceMethod(PDecl->getSetterName()))
1394 return false;
1395 ObjCPropertyDecl *P =
1396 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1397 if (P && !P->isReadOnly())
1398 return false;
1399 }
1400
1401 // Also, check for definition of a setter method in the implementation if
1402 // all else failed.
1403 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1404 if (ObjCImplementationDecl *IMD =
1405 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1406 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1407 return false;
1408 } else if (ObjCCategoryImplDecl *CIMD =
1409 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1410 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1411 return false;
1412 }
1413 }
1414 // Lastly, look through the implementation (if one is in scope).
1415 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1416 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1417 return false;
1418 // If all fails, look at the super class.
1419 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1420 return isPropertyReadonly(PDecl, SIDecl);
1421 return true;
1422}
1423
1424/// CollectImmediateProperties - This routine collects all properties in
1425/// the class and its conforming protocols; but not those it its super class.
1426void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001427 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1428 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001429 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1430 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1431 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001432 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001433 PropMap[Prop->getIdentifier()] = Prop;
1434 }
1435 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001436 for (ObjCInterfaceDecl::all_protocol_iterator
1437 PI = IDecl->all_referenced_protocol_begin(),
1438 E = IDecl->all_referenced_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 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1442 if (!CATDecl->IsClassExtension())
1443 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1444 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001445 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001446 PropMap[Prop->getIdentifier()] = Prop;
1447 }
1448 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001449 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001450 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001451 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001452 }
1453 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1454 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1455 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001456 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001457 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1458 // Exclude property for protocols which conform to class's super-class,
1459 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001460 if (!PropertyFromSuper ||
1461 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001462 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1463 if (!PropEntry)
1464 PropEntry = Prop;
1465 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001466 }
1467 // scan through protocol's protocols.
1468 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1469 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001470 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001471 }
1472}
1473
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001474/// CollectClassPropertyImplementations - This routine collects list of
1475/// properties to be implemented in the class. This includes, class's
1476/// and its conforming protocols' properties.
1477static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1478 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1479 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1480 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1481 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001482 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001483 PropMap[Prop->getIdentifier()] = Prop;
1484 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001485 for (ObjCInterfaceDecl::all_protocol_iterator
1486 PI = IDecl->all_referenced_protocol_begin(),
1487 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001488 CollectClassPropertyImplementations((*PI), PropMap);
1489 }
1490 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1491 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1492 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001493 ObjCPropertyDecl *Prop = *P;
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00001494 // Insert into PropMap if not there already.
1495 PropMap.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001496 }
1497 // scan through protocol's protocols.
1498 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1499 E = PDecl->protocol_end(); PI != E; ++PI)
1500 CollectClassPropertyImplementations((*PI), PropMap);
1501 }
1502}
1503
1504/// CollectSuperClassPropertyImplementations - This routine collects list of
1505/// properties to be implemented in super class(s) and also coming from their
1506/// conforming protocols.
1507static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1508 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1509 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1510 while (SDecl) {
1511 CollectClassPropertyImplementations(SDecl, PropMap);
1512 SDecl = SDecl->getSuperClass();
1513 }
1514 }
1515}
1516
Ted Kremenek9d64c152010-03-12 00:38:38 +00001517/// LookupPropertyDecl - Looks up a property in the current class and all
1518/// its protocols.
1519ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1520 IdentifierInfo *II) {
1521 if (const ObjCInterfaceDecl *IDecl =
1522 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1523 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1524 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001525 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001526 if (Prop->getIdentifier() == II)
1527 return Prop;
1528 }
1529 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001530 for (ObjCInterfaceDecl::all_protocol_iterator
1531 PI = IDecl->all_referenced_protocol_begin(),
1532 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001533 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1534 if (Prop)
1535 return Prop;
1536 }
1537 }
1538 else if (const ObjCProtocolDecl *PDecl =
1539 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1540 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1541 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001542 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001543 if (Prop->getIdentifier() == II)
1544 return Prop;
1545 }
1546 // scan through protocol's protocols.
1547 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1548 E = PDecl->protocol_end(); PI != E; ++PI) {
1549 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1550 if (Prop)
1551 return Prop;
1552 }
1553 }
1554 return 0;
1555}
1556
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001557static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1558 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001559 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001560 {
1561 llvm::raw_svector_ostream os(ivarName);
1562 os << '_' << Prop->getIdentifier()->getName();
1563 }
1564 return &Ctx.Idents.get(ivarName.str());
1565}
1566
James Dennett699c9042012-06-15 07:13:21 +00001567/// \brief Default synthesizes all properties which must be synthesized
1568/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001569void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1570 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001571
1572 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1573 CollectClassPropertyImplementations(IDecl, PropMap);
1574 if (PropMap.empty())
1575 return;
1576 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1577 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1578
1579 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1580 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1581 ObjCPropertyDecl *Prop = P->second;
1582 // If property to be implemented in the super class, ignore.
1583 if (SuperPropMap[Prop->getIdentifier()])
1584 continue;
1585 // Is there a matching propery synthesize/dynamic?
1586 if (Prop->isInvalidDecl() ||
1587 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1588 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1589 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001590 // Property may have been synthesized by user.
1591 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1592 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001593 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1594 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1595 continue;
1596 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1597 continue;
1598 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001599 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1600 // We won't auto-synthesize properties declared in protocols.
1601 Diag(IMPDecl->getLocation(),
1602 diag::warn_auto_synthesizing_protocol_property);
1603 Diag(Prop->getLocation(), diag::note_property_declare);
1604 continue;
1605 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001606
1607 // We use invalid SourceLocations for the synthesized ivars since they
1608 // aren't really synthesized at a particular location; they just exist.
1609 // Saying that they are located at the @implementation isn't really going
1610 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001611 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1612 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1613 true,
1614 /* property = */ Prop->getIdentifier(),
1615 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001616 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001617 if (PIDecl) {
1618 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001619 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001620 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001621 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001622}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001623
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001624void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001625 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001626 return;
1627 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1628 if (!IC)
1629 return;
1630 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001631 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001632 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001633}
1634
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001635void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001636 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001637 const SelectorSet &InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001638 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1639 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1640 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1641
Ted Kremenek9d64c152010-03-12 00:38:38 +00001642 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001643 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001644 if (PropMap.empty())
1645 return;
1646
1647 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1648 for (ObjCImplDecl::propimpl_iterator
1649 I = IMPDecl->propimpl_begin(),
1650 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001651 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001652
1653 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1654 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1655 ObjCPropertyDecl *Prop = P->second;
1656 // Is there a matching propery synthesize/dynamic?
1657 if (Prop->isInvalidDecl() ||
1658 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001659 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001660 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001661 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001662 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001663 isa<ObjCCategoryDecl>(CDecl) ?
1664 diag::warn_setter_getter_impl_required_in_category :
1665 diag::warn_setter_getter_impl_required)
1666 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001667 Diag(Prop->getLocation(),
1668 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001669 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001670 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001671 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001672 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1673
Ted Kremenek9d64c152010-03-12 00:38:38 +00001674 }
1675
1676 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001677 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001678 isa<ObjCCategoryDecl>(CDecl) ?
1679 diag::warn_setter_getter_impl_required_in_category :
1680 diag::warn_setter_getter_impl_required)
1681 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001682 Diag(Prop->getLocation(),
1683 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001684 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001685 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001686 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001687 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001688 }
1689 }
1690}
1691
1692void
1693Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1694 ObjCContainerDecl* IDecl) {
1695 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001696 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001697 return;
1698 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1699 E = IDecl->prop_end();
1700 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001701 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001702 ObjCMethodDecl *GetterMethod = 0;
1703 ObjCMethodDecl *SetterMethod = 0;
1704 bool LookedUpGetterSetter = false;
1705
Ted Kremenek9d64c152010-03-12 00:38:38 +00001706 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001707 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001708
John McCall265941b2011-09-13 18:31:23 +00001709 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1710 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001711 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1712 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1713 LookedUpGetterSetter = true;
1714 if (GetterMethod) {
1715 Diag(GetterMethod->getLocation(),
1716 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001717 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001718 Diag(Property->getLocation(), diag::note_property_declare);
1719 }
1720 if (SetterMethod) {
1721 Diag(SetterMethod->getLocation(),
1722 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001723 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001724 Diag(Property->getLocation(), diag::note_property_declare);
1725 }
1726 }
1727
Ted Kremenek9d64c152010-03-12 00:38:38 +00001728 // We only care about readwrite atomic property.
1729 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1730 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1731 continue;
1732 if (const ObjCPropertyImplDecl *PIDecl
1733 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1734 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1735 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001736 if (!LookedUpGetterSetter) {
1737 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1738 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1739 LookedUpGetterSetter = true;
1740 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001741 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1742 SourceLocation MethodLoc =
1743 (GetterMethod ? GetterMethod->getLocation()
1744 : SetterMethod->getLocation());
1745 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001746 << Property->getIdentifier() << (GetterMethod != 0)
1747 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001748 // fixit stuff.
1749 if (!AttributesAsWritten) {
1750 if (Property->getLParenLoc().isValid()) {
1751 // @property () ... case.
1752 SourceRange PropSourceRange(Property->getAtLoc(),
1753 Property->getLParenLoc());
1754 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1755 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1756 }
1757 else {
1758 //@property id etc.
1759 SourceLocation endLoc =
1760 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1761 endLoc = endLoc.getLocWithOffset(-1);
1762 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1763 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1764 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1765 }
1766 }
1767 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1768 // @property () ... case.
1769 SourceLocation endLoc = Property->getLParenLoc();
1770 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1771 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1772 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1773 }
1774 else
1775 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001776 Diag(Property->getLocation(), diag::note_property_declare);
1777 }
1778 }
1779 }
1780}
1781
John McCallf85e1932011-06-15 23:02:42 +00001782void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001783 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001784 return;
1785
1786 for (ObjCImplementationDecl::propimpl_iterator
1787 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001788 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001789 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1790 continue;
1791
1792 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001793 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1794 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001795 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1796 if (!method)
1797 continue;
1798 ObjCMethodFamily family = method->getMethodFamily();
1799 if (family == OMF_alloc || family == OMF_copy ||
1800 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001801 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001802 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1803 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001804 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001805 Diag(PD->getLocation(), diag::note_property_declare);
1806 }
1807 }
1808 }
1809}
1810
John McCall5de74d12010-11-10 07:01:40 +00001811/// AddPropertyAttrs - Propagates attributes from a property to the
1812/// implicitly-declared getter or setter for that property.
1813static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1814 ObjCPropertyDecl *Property) {
1815 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001816 for (Decl::attr_iterator A = Property->attr_begin(),
1817 AEnd = Property->attr_end();
1818 A != AEnd; ++A) {
1819 if (isa<DeprecatedAttr>(*A) ||
1820 isa<UnavailableAttr>(*A) ||
1821 isa<AvailabilityAttr>(*A))
1822 PropertyMethod->addAttr((*A)->clone(S.Context));
1823 }
John McCall5de74d12010-11-10 07:01:40 +00001824}
1825
Ted Kremenek9d64c152010-03-12 00:38:38 +00001826/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1827/// have the property type and issue diagnostics if they don't.
1828/// Also synthesize a getter/setter method if none exist (and update the
1829/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1830/// methods is the "right" thing to do.
1831void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001832 ObjCContainerDecl *CD,
1833 ObjCPropertyDecl *redeclaredProperty,
1834 ObjCContainerDecl *lexicalDC) {
1835
Ted Kremenek9d64c152010-03-12 00:38:38 +00001836 ObjCMethodDecl *GetterMethod, *SetterMethod;
1837
1838 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1839 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1840 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1841 property->getLocation());
1842
1843 if (SetterMethod) {
1844 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1845 property->getPropertyAttributes();
1846 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1847 Context.getCanonicalType(SetterMethod->getResultType()) !=
1848 Context.VoidTy)
1849 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1850 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001851 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001852 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1853 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001854 Diag(property->getLocation(),
1855 diag::warn_accessor_property_type_mismatch)
1856 << property->getDeclName()
1857 << SetterMethod->getSelector();
1858 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1859 }
1860 }
1861
1862 // Synthesize getter/setter methods if none exist.
1863 // Find the default getter and if one not found, add one.
1864 // FIXME: The synthesized property we set here is misleading. We almost always
1865 // synthesize these methods unless the user explicitly provided prototypes
1866 // (which is odd, but allowed). Sema should be typechecking that the
1867 // declarations jive in that situation (which it is not currently).
1868 if (!GetterMethod) {
1869 // No instance method of same name as property getter name was found.
1870 // Declare a getter method and add it to the list of methods
1871 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001872 SourceLocation Loc = redeclaredProperty ?
1873 redeclaredProperty->getLocation() :
1874 property->getLocation();
1875
1876 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1877 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001878 property->getType(), 0, CD, /*isInstance=*/true,
1879 /*isVariadic=*/false, /*isSynthesized=*/true,
1880 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001881 (property->getPropertyImplementation() ==
1882 ObjCPropertyDecl::Optional) ?
1883 ObjCMethodDecl::Optional :
1884 ObjCMethodDecl::Required);
1885 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001886
1887 AddPropertyAttrs(*this, GetterMethod, property);
1888
Ted Kremenek23173d72010-05-18 21:09:07 +00001889 // FIXME: Eventually this shouldn't be needed, as the lexical context
1890 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001891 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001892 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001893 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1894 GetterMethod->addAttr(
1895 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001896 } else
1897 // A user declared getter will be synthesize when @synthesize of
1898 // the property with the same name is seen in the @implementation
1899 GetterMethod->setSynthesized(true);
1900 property->setGetterMethodDecl(GetterMethod);
1901
1902 // Skip setter if property is read-only.
1903 if (!property->isReadOnly()) {
1904 // Find the default setter and if one not found, add one.
1905 if (!SetterMethod) {
1906 // No instance method of same name as property setter name was found.
1907 // Declare a setter method and add it to the list of methods
1908 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001909 SourceLocation Loc = redeclaredProperty ?
1910 redeclaredProperty->getLocation() :
1911 property->getLocation();
1912
1913 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001914 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001915 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001916 CD, /*isInstance=*/true, /*isVariadic=*/false,
1917 /*isSynthesized=*/true,
1918 /*isImplicitlyDeclared=*/true,
1919 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001920 (property->getPropertyImplementation() ==
1921 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001922 ObjCMethodDecl::Optional :
1923 ObjCMethodDecl::Required);
1924
Ted Kremenek9d64c152010-03-12 00:38:38 +00001925 // Invent the arguments for the setter. We don't bother making a
1926 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001927 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1928 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001929 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001930 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001931 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001932 SC_None,
1933 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001934 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001935 SetterMethod->setMethodParams(Context, Argument,
1936 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001937
1938 AddPropertyAttrs(*this, SetterMethod, property);
1939
Ted Kremenek9d64c152010-03-12 00:38:38 +00001940 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001941 // FIXME: Eventually this shouldn't be needed, as the lexical context
1942 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001943 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001944 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001945 } else
1946 // A user declared setter will be synthesize when @synthesize of
1947 // the property with the same name is seen in the @implementation
1948 SetterMethod->setSynthesized(true);
1949 property->setSetterMethodDecl(SetterMethod);
1950 }
1951 // Add any synthesized methods to the global pool. This allows us to
1952 // handle the following, which is supported by GCC (and part of the design).
1953 //
1954 // @interface Foo
1955 // @property double bar;
1956 // @end
1957 //
1958 // void thisIsUnfortunate() {
1959 // id foo;
1960 // double bar = [foo bar];
1961 // }
1962 //
1963 if (GetterMethod)
1964 AddInstanceMethodToGlobalPool(GetterMethod);
1965 if (SetterMethod)
1966 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001967
1968 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1969 if (!CurrentClass) {
1970 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1971 CurrentClass = Cat->getClassInterface();
1972 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1973 CurrentClass = Impl->getClassInterface();
1974 }
1975 if (GetterMethod)
1976 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1977 if (SetterMethod)
1978 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001979}
1980
John McCalld226f652010-08-21 09:40:31 +00001981void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001982 SourceLocation Loc,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001983 unsigned &Attributes,
1984 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001985 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001986 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001987 return;
1988
1989 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001990 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001991
David Blaikie4e4d0842012-03-11 07:00:24 +00001992 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001993 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1994 PropertyTy->isObjCRetainableType()) {
1995 // 'readonly' property with no obvious lifetime.
1996 // its life time will be determined by its backing ivar.
1997 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1998 ObjCDeclSpec::DQ_PR_copy |
1999 ObjCDeclSpec::DQ_PR_retain |
2000 ObjCDeclSpec::DQ_PR_strong |
2001 ObjCDeclSpec::DQ_PR_weak |
2002 ObjCDeclSpec::DQ_PR_assign);
2003 if ((Attributes & rel) == 0)
2004 return;
2005 }
2006
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002007 if (propertyInPrimaryClass) {
2008 // we postpone most property diagnosis until class's implementation
2009 // because, its readonly attribute may be overridden in its class
2010 // extensions making other attributes, which make no sense, to make sense.
2011 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2012 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2013 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2014 << "readonly" << "readwrite";
2015 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002016 // readonly and readwrite/assign/retain/copy conflict.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002017 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2018 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002019 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002020 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002021 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002022 ObjCDeclSpec::DQ_PR_retain |
2023 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002024 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2025 "readwrite" :
2026 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2027 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00002028 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
2029 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00002030 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2031 "copy" : "retain";
2032
2033 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
2034 diag::err_objc_property_attr_mutually_exclusive :
2035 diag::warn_objc_property_attr_mutually_exclusive)
2036 << "readonly" << which;
2037 }
2038
2039 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00002040 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2041 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2042 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002043 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002044 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00002045 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2046 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2047 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2048 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002049 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002050 }
2051
2052 // Check for more than one of { assign, copy, retain }.
2053 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2054 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2055 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2056 << "assign" << "copy";
2057 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2058 }
2059 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2060 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2061 << "assign" << "retain";
2062 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2063 }
John McCallf85e1932011-06-15 23:02:42 +00002064 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2065 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2066 << "assign" << "strong";
2067 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2068 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002069 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002070 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2071 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2072 << "assign" << "weak";
2073 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2074 }
2075 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2076 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2077 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2078 << "unsafe_unretained" << "copy";
2079 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2080 }
2081 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2082 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2083 << "unsafe_unretained" << "retain";
2084 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2085 }
2086 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2087 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2088 << "unsafe_unretained" << "strong";
2089 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2090 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002091 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002092 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2093 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2094 << "unsafe_unretained" << "weak";
2095 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2096 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002097 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2098 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2099 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2100 << "copy" << "retain";
2101 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2102 }
John McCallf85e1932011-06-15 23:02:42 +00002103 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2104 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2105 << "copy" << "strong";
2106 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2107 }
2108 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2109 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2110 << "copy" << "weak";
2111 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2112 }
2113 }
2114 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2115 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2116 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2117 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002118 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002119 }
2120 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2121 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2122 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2123 << "strong" << "weak";
2124 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002125 }
2126
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002127 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2128 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2129 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2130 << "atomic" << "nonatomic";
2131 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2132 }
2133
Ted Kremenek9d64c152010-03-12 00:38:38 +00002134 // Warn if user supplied no assignment attribute, property is
2135 // readwrite, and this is an object type.
2136 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002137 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2138 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2139 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002140 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002141 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002142 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002143 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002144 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002145 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002146 bool isAnyClassTy =
2147 (PropertyTy->isObjCClassType() ||
2148 PropertyTy->isObjCQualifiedClassType());
2149 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2150 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002151 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002152 ;
2153 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002154 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002155 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002156 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002157
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002158 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002159 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002160 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002161 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002162 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002163
2164 // FIXME: Implement warning dependent on NSCopying being
2165 // implemented. See also:
2166 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2167 // (please trim this list while you are at it).
2168 }
2169
2170 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002171 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002172 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002173 && PropertyTy->isBlockPointerType())
2174 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian7c16d582012-06-27 20:52:46 +00002175 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002176 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2177 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2178 PropertyTy->isBlockPointerType())
2179 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002180
2181 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2182 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2183 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2184
Ted Kremenek9d64c152010-03-12 00:38:38 +00002185}