blob: 98c1bb27f9070a61e8a1e8ce81e392d223c466ad [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 =
Richard Smitha8eaf002012-08-23 06:16:52 +0000889 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
890 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
891 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000892 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000893 Diag(property->getLocation(), diag::note_property_declare);
894 err = true;
895 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000896 }
John McCall0a7dd782012-08-21 02:47:43 +0000897 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000898 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000899 Diag(property->getLocation(), diag::note_property_declare);
900 }
John McCallf85e1932011-06-15 23:02:42 +0000901 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000902
John McCallf85e1932011-06-15 23:02:42 +0000903 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000904 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000905 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
906 }
John McCallf85e1932011-06-15 23:02:42 +0000907 }
908
909 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000910 !getLangOpts().ObjCAutoRefCount &&
911 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000912 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000913 Diag(property->getLocation(), diag::note_property_declare);
914 }
915
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000916 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000917 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000918 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000919 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000920 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000921 if (CompleteTypeErr)
922 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000923 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000924 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000925 property->setPropertyIvarDecl(Ivar);
926
John McCall260611a2012-06-20 06:18:46 +0000927 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000928 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
929 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000930 // Note! I deliberately want it to fall thru so, we have a
931 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000932 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000933 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000934 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000935 << property->getDeclName() << Ivar->getDeclName()
936 << ClassDeclared->getDeclName();
937 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000938 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000939 // Note! I deliberately want it to fall thru so more errors are caught.
940 }
941 QualType IvarType = Context.getCanonicalType(Ivar->getType());
942
943 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000944 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
945 compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000946 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000947 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000948 compat =
949 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000950 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000951 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000952 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000953 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
954 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000955 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000956 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000957 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000958 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000959 << property->getDeclName() << PropType
960 << Ivar->getDeclName() << IvarType;
961 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000962 // Note! I deliberately want it to fall thru so, we have a
963 // a property implementation and to avoid future warnings.
964 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000965 else {
966 // FIXME! Rules for properties are somewhat different that those
967 // for assignments. Use a new routine to consolidate all cases;
968 // specifically for property redeclarations as well as for ivars.
969 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
970 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
971 if (lhsType != rhsType &&
972 lhsType->isArithmeticType()) {
973 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
974 << property->getDeclName() << PropType
975 << Ivar->getDeclName() << IvarType;
976 Diag(Ivar->getLocation(), diag::note_ivar_decl);
977 // Fall thru - see previous comment
978 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000979 }
980 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000981 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000982 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000983 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000984 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000985 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000986 // Fall thru - see previous comment
987 }
John McCallf85e1932011-06-15 23:02:42 +0000988 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000989 if ((property->getType()->isObjCObjectPointerType() ||
990 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000991 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000992 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000993 << property->getDeclName() << Ivar->getDeclName();
994 // Fall thru - see previous comment
995 }
996 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000997 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000998 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000999 } else if (PropertyIvar)
1000 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001001 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001002
Ted Kremenek28685ab2010-03-12 00:46:40 +00001003 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1004 ObjCPropertyImplDecl *PIDecl =
1005 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1006 property,
1007 (Synthesize ?
1008 ObjCPropertyImplDecl::Synthesize
1009 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001010 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001011
Fariborz Jahanian74414712012-05-15 18:12:51 +00001012 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001013 PIDecl->setInvalidDecl();
1014
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001015 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1016 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001017 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001018 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001019 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1020 // returned by the getter as it must conform to C++'s copy-return rules.
1021 // FIXME. Eventually we want to do this for Objective-C as well.
1022 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1023 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001024 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001025 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001026 Expr *IvarRefExpr =
1027 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1028 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001029 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001030 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001031 SourceLocation(),
1032 getterMethod->getResultType(),
1033 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001034 SourceLocation(),
1035 Owned(IvarRefExpr));
1036 if (!Res.isInvalid()) {
1037 Expr *ResExpr = Res.takeAs<Expr>();
1038 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001039 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001040 PIDecl->setGetterCXXConstructor(ResExpr);
1041 }
1042 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001043 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1044 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1045 Diag(getterMethod->getLocation(),
1046 diag::warn_property_getter_owning_mismatch);
1047 Diag(property->getLocation(), diag::note_property_declare);
1048 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001049 }
1050 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1051 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001052 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1053 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001054 // FIXME. Eventually we want to do this for Objective-C as well.
1055 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1056 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001057 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001058 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001059 Expr *lhs =
1060 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1061 SelfExpr, true, true);
1062 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1063 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001064 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +00001065 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +00001066 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +00001067 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +00001068 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001069 if (property->getPropertyAttributes() &
1070 ObjCPropertyDecl::OBJC_PR_atomic) {
1071 Expr *callExpr = Res.takeAs<Expr>();
1072 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001073 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1074 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001075 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001076 if (property->getType()->isReferenceType()) {
1077 Diag(PropertyLoc,
1078 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001079 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001080 Diag(FuncDecl->getLocStart(),
1081 diag::note_callee_decl) << FuncDecl;
1082 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001083 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001084 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1085 }
1086 }
1087
Ted Kremenek28685ab2010-03-12 00:46:40 +00001088 if (IC) {
1089 if (Synthesize)
1090 if (ObjCPropertyImplDecl *PPIDecl =
1091 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1092 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1093 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1094 << PropertyIvar;
1095 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1096 }
1097
1098 if (ObjCPropertyImplDecl *PPIDecl
1099 = IC->FindPropertyImplDecl(PropertyId)) {
1100 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1101 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001102 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001103 }
1104 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001105 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001106 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001107 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001108 // Diagnose if an ivar was lazily synthesdized due to a previous
1109 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001110 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001111 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001112 ObjCIvarDecl *Ivar = 0;
1113 if (!Synthesize)
1114 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1115 else {
1116 if (PropertyIvar && PropertyIvar != PropertyId)
1117 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1118 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001119 // Issue diagnostics only if Ivar belongs to current class.
1120 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001121 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001122 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1123 << PropertyId;
1124 Ivar->setInvalidDecl();
1125 }
1126 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001127 } else {
1128 if (Synthesize)
1129 if (ObjCPropertyImplDecl *PPIDecl =
1130 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001131 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001132 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1133 << PropertyIvar;
1134 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1135 }
1136
1137 if (ObjCPropertyImplDecl *PPIDecl =
1138 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001139 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001140 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001141 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001142 }
1143 CatImplClass->addPropertyImplementation(PIDecl);
1144 }
1145
John McCalld226f652010-08-21 09:40:31 +00001146 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001147}
1148
1149//===----------------------------------------------------------------------===//
1150// Helper methods.
1151//===----------------------------------------------------------------------===//
1152
Ted Kremenek9d64c152010-03-12 00:38:38 +00001153/// DiagnosePropertyMismatch - Compares two properties for their
1154/// attributes and types and warns on a variety of inconsistencies.
1155///
1156void
1157Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1158 ObjCPropertyDecl *SuperProperty,
1159 const IdentifierInfo *inheritedName) {
1160 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1161 Property->getPropertyAttributes();
1162 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1163 SuperProperty->getPropertyAttributes();
1164 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1165 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1166 Diag(Property->getLocation(), diag::warn_readonly_property)
1167 << Property->getDeclName() << inheritedName;
1168 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1169 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1170 Diag(Property->getLocation(), diag::warn_property_attribute)
1171 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001172 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001173 unsigned CAttrRetain =
1174 (CAttr &
1175 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1176 unsigned SAttrRetain =
1177 (SAttr &
1178 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1179 bool CStrong = (CAttrRetain != 0);
1180 bool SStrong = (SAttrRetain != 0);
1181 if (CStrong != SStrong)
1182 Diag(Property->getLocation(), diag::warn_property_attribute)
1183 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1184 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001185
1186 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1187 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1188 Diag(Property->getLocation(), diag::warn_property_attribute)
1189 << Property->getDeclName() << "atomic" << inheritedName;
1190 if (Property->getSetterName() != SuperProperty->getSetterName())
1191 Diag(Property->getLocation(), diag::warn_property_attribute)
1192 << Property->getDeclName() << "setter" << inheritedName;
1193 if (Property->getGetterName() != SuperProperty->getGetterName())
1194 Diag(Property->getLocation(), diag::warn_property_attribute)
1195 << Property->getDeclName() << "getter" << inheritedName;
1196
1197 QualType LHSType =
1198 Context.getCanonicalType(SuperProperty->getType());
1199 QualType RHSType =
1200 Context.getCanonicalType(Property->getType());
1201
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001202 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001203 // Do cases not handled in above.
1204 // FIXME. For future support of covariant property types, revisit this.
1205 bool IncompatibleObjC = false;
1206 QualType ConvertedType;
1207 if (!isObjCPointerConversion(RHSType, LHSType,
1208 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001209 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001210 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1211 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001212 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1213 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001214 }
1215}
1216
1217bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1218 ObjCMethodDecl *GetterMethod,
1219 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001220 if (!GetterMethod)
1221 return false;
1222 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1223 QualType PropertyIvarType = property->getType().getNonReferenceType();
1224 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1225 if (!compat) {
1226 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1227 isa<ObjCObjectPointerType>(GetterType))
1228 compat =
1229 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001230 GetterType->getAs<ObjCObjectPointerType>(),
1231 PropertyIvarType->getAs<ObjCObjectPointerType>());
1232 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001233 != Compatible) {
1234 Diag(Loc, diag::error_property_accessor_type)
1235 << property->getDeclName() << PropertyIvarType
1236 << GetterMethod->getSelector() << GetterType;
1237 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1238 return true;
1239 } else {
1240 compat = true;
1241 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1242 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1243 if (lhsType != rhsType && lhsType->isArithmeticType())
1244 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001245 }
1246 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001247
1248 if (!compat) {
1249 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1250 << property->getDeclName()
1251 << GetterMethod->getSelector();
1252 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1253 return true;
1254 }
1255
Ted Kremenek9d64c152010-03-12 00:38:38 +00001256 return false;
1257}
1258
1259/// ComparePropertiesInBaseAndSuper - This routine compares property
1260/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001261/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001262///
1263void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1264 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1265 if (!SDecl)
1266 return;
1267 // FIXME: O(N^2)
1268 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1269 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001270 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001271 // Does property in super class has declaration in current class?
1272 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1273 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001274 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001275 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1276 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1277 SDecl->getIdentifier());
1278 }
1279 }
1280}
1281
1282/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1283/// of properties declared in a protocol and compares their attribute against
1284/// the same property declared in the class or category.
1285void
1286Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1287 ObjCProtocolDecl *PDecl) {
1288 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1289 if (!IDecl) {
1290 // Category
1291 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1292 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1293 if (!CatDecl->IsClassExtension())
1294 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1295 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001296 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001297 ObjCCategoryDecl::prop_iterator CP, CE;
1298 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001299 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001300 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001301 break;
1302 if (CP != CE)
1303 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001304 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001305 }
1306 return;
1307 }
1308 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1309 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001310 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001311 ObjCInterfaceDecl::prop_iterator CP, CE;
1312 // Is this property already in class's list of properties?
1313 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001314 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001315 break;
1316 if (CP != CE)
1317 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001318 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001319 }
1320}
1321
1322/// CompareProperties - This routine compares properties
1323/// declared in 'ClassOrProtocol' objects (which can be a class or an
1324/// inherited protocol with the list of properties for class/category 'CDecl'
1325///
John McCalld226f652010-08-21 09:40:31 +00001326void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1327 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001328 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1329
1330 if (!IDecl) {
1331 // Category
1332 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1333 assert (CatDecl && "CompareProperties");
1334 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1335 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1336 E = MDecl->protocol_end(); P != E; ++P)
1337 // Match properties of category with those of protocol (*P)
1338 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1339
1340 // Go thru the list of protocols for this category and recursively match
1341 // their properties with those in the category.
1342 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1343 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001344 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001345 } else {
1346 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1347 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1348 E = MD->protocol_end(); P != E; ++P)
1349 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1350 }
1351 return;
1352 }
1353
1354 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001355 for (ObjCInterfaceDecl::all_protocol_iterator
1356 P = MDecl->all_referenced_protocol_begin(),
1357 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001358 // Match properties of class IDecl with those of protocol (*P).
1359 MatchOneProtocolPropertiesInClass(IDecl, *P);
1360
1361 // Go thru the list of protocols for this class and recursively match
1362 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001363 for (ObjCInterfaceDecl::all_protocol_iterator
1364 P = IDecl->all_referenced_protocol_begin(),
1365 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001366 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001367 } else {
1368 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1369 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1370 E = MD->protocol_end(); P != E; ++P)
1371 MatchOneProtocolPropertiesInClass(IDecl, *P);
1372 }
1373}
1374
1375/// isPropertyReadonly - Return true if property is readonly, by searching
1376/// for the property in the class and in its categories and implementations
1377///
1378bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1379 ObjCInterfaceDecl *IDecl) {
1380 // by far the most common case.
1381 if (!PDecl->isReadOnly())
1382 return false;
1383 // Even if property is ready only, if interface has a user defined setter,
1384 // it is not considered read only.
1385 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1386 return false;
1387
1388 // Main class has the property as 'readonly'. Must search
1389 // through the category list to see if the property's
1390 // attribute has been over-ridden to 'readwrite'.
1391 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1392 Category; Category = Category->getNextClassCategory()) {
1393 // Even if property is ready only, if a category has a user defined setter,
1394 // it is not considered read only.
1395 if (Category->getInstanceMethod(PDecl->getSetterName()))
1396 return false;
1397 ObjCPropertyDecl *P =
1398 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1399 if (P && !P->isReadOnly())
1400 return false;
1401 }
1402
1403 // Also, check for definition of a setter method in the implementation if
1404 // all else failed.
1405 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1406 if (ObjCImplementationDecl *IMD =
1407 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1408 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1409 return false;
1410 } else if (ObjCCategoryImplDecl *CIMD =
1411 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1412 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1413 return false;
1414 }
1415 }
1416 // Lastly, look through the implementation (if one is in scope).
1417 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1418 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1419 return false;
1420 // If all fails, look at the super class.
1421 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1422 return isPropertyReadonly(PDecl, SIDecl);
1423 return true;
1424}
1425
1426/// CollectImmediateProperties - This routine collects all properties in
1427/// the class and its conforming protocols; but not those it its super class.
1428void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001429 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1430 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001431 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1432 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1433 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001434 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001435 PropMap[Prop->getIdentifier()] = Prop;
1436 }
1437 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001438 for (ObjCInterfaceDecl::all_protocol_iterator
1439 PI = IDecl->all_referenced_protocol_begin(),
1440 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001441 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001442 }
1443 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1444 if (!CATDecl->IsClassExtension())
1445 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1446 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001447 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001448 PropMap[Prop->getIdentifier()] = Prop;
1449 }
1450 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001451 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001452 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001453 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001454 }
1455 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1456 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1457 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001458 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001459 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1460 // Exclude property for protocols which conform to class's super-class,
1461 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001462 if (!PropertyFromSuper ||
1463 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001464 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1465 if (!PropEntry)
1466 PropEntry = Prop;
1467 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001468 }
1469 // scan through protocol's protocols.
1470 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1471 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001472 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001473 }
1474}
1475
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001476/// CollectClassPropertyImplementations - This routine collects list of
1477/// properties to be implemented in the class. This includes, class's
1478/// and its conforming protocols' properties.
1479static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1480 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1481 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1482 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1483 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001484 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001485 PropMap[Prop->getIdentifier()] = Prop;
1486 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001487 for (ObjCInterfaceDecl::all_protocol_iterator
1488 PI = IDecl->all_referenced_protocol_begin(),
1489 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001490 CollectClassPropertyImplementations((*PI), PropMap);
1491 }
1492 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1493 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1494 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001495 ObjCPropertyDecl *Prop = *P;
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00001496 // Insert into PropMap if not there already.
1497 PropMap.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001498 }
1499 // scan through protocol's protocols.
1500 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1501 E = PDecl->protocol_end(); PI != E; ++PI)
1502 CollectClassPropertyImplementations((*PI), PropMap);
1503 }
1504}
1505
1506/// CollectSuperClassPropertyImplementations - This routine collects list of
1507/// properties to be implemented in super class(s) and also coming from their
1508/// conforming protocols.
1509static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1510 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1511 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1512 while (SDecl) {
1513 CollectClassPropertyImplementations(SDecl, PropMap);
1514 SDecl = SDecl->getSuperClass();
1515 }
1516 }
1517}
1518
Ted Kremenek9d64c152010-03-12 00:38:38 +00001519/// LookupPropertyDecl - Looks up a property in the current class and all
1520/// its protocols.
1521ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1522 IdentifierInfo *II) {
1523 if (const ObjCInterfaceDecl *IDecl =
1524 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1525 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1526 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001527 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001528 if (Prop->getIdentifier() == II)
1529 return Prop;
1530 }
1531 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001532 for (ObjCInterfaceDecl::all_protocol_iterator
1533 PI = IDecl->all_referenced_protocol_begin(),
1534 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001535 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1536 if (Prop)
1537 return Prop;
1538 }
1539 }
1540 else if (const ObjCProtocolDecl *PDecl =
1541 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1542 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1543 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001544 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001545 if (Prop->getIdentifier() == II)
1546 return Prop;
1547 }
1548 // scan through protocol's protocols.
1549 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1550 E = PDecl->protocol_end(); PI != E; ++PI) {
1551 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1552 if (Prop)
1553 return Prop;
1554 }
1555 }
1556 return 0;
1557}
1558
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001559static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1560 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001561 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001562 {
1563 llvm::raw_svector_ostream os(ivarName);
1564 os << '_' << Prop->getIdentifier()->getName();
1565 }
1566 return &Ctx.Idents.get(ivarName.str());
1567}
1568
James Dennett699c9042012-06-15 07:13:21 +00001569/// \brief Default synthesizes all properties which must be synthesized
1570/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001571void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1572 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001573
1574 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1575 CollectClassPropertyImplementations(IDecl, PropMap);
1576 if (PropMap.empty())
1577 return;
1578 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1579 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1580
1581 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1582 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1583 ObjCPropertyDecl *Prop = P->second;
1584 // If property to be implemented in the super class, ignore.
1585 if (SuperPropMap[Prop->getIdentifier()])
1586 continue;
1587 // Is there a matching propery synthesize/dynamic?
1588 if (Prop->isInvalidDecl() ||
1589 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1590 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1591 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001592 // Property may have been synthesized by user.
1593 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1594 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001595 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1596 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1597 continue;
1598 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1599 continue;
1600 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001601 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1602 // We won't auto-synthesize properties declared in protocols.
1603 Diag(IMPDecl->getLocation(),
1604 diag::warn_auto_synthesizing_protocol_property);
1605 Diag(Prop->getLocation(), diag::note_property_declare);
1606 continue;
1607 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001608
1609 // We use invalid SourceLocations for the synthesized ivars since they
1610 // aren't really synthesized at a particular location; they just exist.
1611 // Saying that they are located at the @implementation isn't really going
1612 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001613 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1614 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1615 true,
1616 /* property = */ Prop->getIdentifier(),
1617 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001618 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001619 if (PIDecl) {
1620 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001621 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001622 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001623 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001624}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001625
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001626void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001627 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001628 return;
1629 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1630 if (!IC)
1631 return;
1632 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001633 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001634 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001635}
1636
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001637void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001638 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001639 const SelectorSet &InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001640 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1641 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1642 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1643
Ted Kremenek9d64c152010-03-12 00:38:38 +00001644 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001645 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001646 if (PropMap.empty())
1647 return;
1648
1649 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1650 for (ObjCImplDecl::propimpl_iterator
1651 I = IMPDecl->propimpl_begin(),
1652 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001653 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001654
1655 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1656 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1657 ObjCPropertyDecl *Prop = P->second;
1658 // Is there a matching propery synthesize/dynamic?
1659 if (Prop->isInvalidDecl() ||
1660 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001661 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001662 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001663 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001664 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001665 isa<ObjCCategoryDecl>(CDecl) ?
1666 diag::warn_setter_getter_impl_required_in_category :
1667 diag::warn_setter_getter_impl_required)
1668 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001669 Diag(Prop->getLocation(),
1670 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001671 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001672 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001673 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001674 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1675
Ted Kremenek9d64c152010-03-12 00:38:38 +00001676 }
1677
1678 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001679 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001680 isa<ObjCCategoryDecl>(CDecl) ?
1681 diag::warn_setter_getter_impl_required_in_category :
1682 diag::warn_setter_getter_impl_required)
1683 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001684 Diag(Prop->getLocation(),
1685 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001686 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001687 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001688 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001689 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001690 }
1691 }
1692}
1693
1694void
1695Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1696 ObjCContainerDecl* IDecl) {
1697 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001698 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001699 return;
1700 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1701 E = IDecl->prop_end();
1702 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001703 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001704 ObjCMethodDecl *GetterMethod = 0;
1705 ObjCMethodDecl *SetterMethod = 0;
1706 bool LookedUpGetterSetter = false;
1707
Ted Kremenek9d64c152010-03-12 00:38:38 +00001708 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001709 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001710
John McCall265941b2011-09-13 18:31:23 +00001711 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1712 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001713 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1714 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1715 LookedUpGetterSetter = true;
1716 if (GetterMethod) {
1717 Diag(GetterMethod->getLocation(),
1718 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001719 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001720 Diag(Property->getLocation(), diag::note_property_declare);
1721 }
1722 if (SetterMethod) {
1723 Diag(SetterMethod->getLocation(),
1724 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001725 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001726 Diag(Property->getLocation(), diag::note_property_declare);
1727 }
1728 }
1729
Ted Kremenek9d64c152010-03-12 00:38:38 +00001730 // We only care about readwrite atomic property.
1731 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1732 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1733 continue;
1734 if (const ObjCPropertyImplDecl *PIDecl
1735 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1736 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1737 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001738 if (!LookedUpGetterSetter) {
1739 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1740 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1741 LookedUpGetterSetter = true;
1742 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001743 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1744 SourceLocation MethodLoc =
1745 (GetterMethod ? GetterMethod->getLocation()
1746 : SetterMethod->getLocation());
1747 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001748 << Property->getIdentifier() << (GetterMethod != 0)
1749 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001750 // fixit stuff.
1751 if (!AttributesAsWritten) {
1752 if (Property->getLParenLoc().isValid()) {
1753 // @property () ... case.
1754 SourceRange PropSourceRange(Property->getAtLoc(),
1755 Property->getLParenLoc());
1756 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1757 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1758 }
1759 else {
1760 //@property id etc.
1761 SourceLocation endLoc =
1762 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1763 endLoc = endLoc.getLocWithOffset(-1);
1764 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1765 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1766 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1767 }
1768 }
1769 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1770 // @property () ... case.
1771 SourceLocation endLoc = Property->getLParenLoc();
1772 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1773 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1774 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1775 }
1776 else
1777 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001778 Diag(Property->getLocation(), diag::note_property_declare);
1779 }
1780 }
1781 }
1782}
1783
John McCallf85e1932011-06-15 23:02:42 +00001784void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001785 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001786 return;
1787
1788 for (ObjCImplementationDecl::propimpl_iterator
1789 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001790 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001791 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1792 continue;
1793
1794 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001795 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1796 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001797 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1798 if (!method)
1799 continue;
1800 ObjCMethodFamily family = method->getMethodFamily();
1801 if (family == OMF_alloc || family == OMF_copy ||
1802 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001803 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001804 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1805 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001806 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001807 Diag(PD->getLocation(), diag::note_property_declare);
1808 }
1809 }
1810 }
1811}
1812
John McCall5de74d12010-11-10 07:01:40 +00001813/// AddPropertyAttrs - Propagates attributes from a property to the
1814/// implicitly-declared getter or setter for that property.
1815static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1816 ObjCPropertyDecl *Property) {
1817 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001818 for (Decl::attr_iterator A = Property->attr_begin(),
1819 AEnd = Property->attr_end();
1820 A != AEnd; ++A) {
1821 if (isa<DeprecatedAttr>(*A) ||
1822 isa<UnavailableAttr>(*A) ||
1823 isa<AvailabilityAttr>(*A))
1824 PropertyMethod->addAttr((*A)->clone(S.Context));
1825 }
John McCall5de74d12010-11-10 07:01:40 +00001826}
1827
Ted Kremenek9d64c152010-03-12 00:38:38 +00001828/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1829/// have the property type and issue diagnostics if they don't.
1830/// Also synthesize a getter/setter method if none exist (and update the
1831/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1832/// methods is the "right" thing to do.
1833void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001834 ObjCContainerDecl *CD,
1835 ObjCPropertyDecl *redeclaredProperty,
1836 ObjCContainerDecl *lexicalDC) {
1837
Ted Kremenek9d64c152010-03-12 00:38:38 +00001838 ObjCMethodDecl *GetterMethod, *SetterMethod;
1839
1840 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1841 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1842 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1843 property->getLocation());
1844
1845 if (SetterMethod) {
1846 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1847 property->getPropertyAttributes();
1848 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1849 Context.getCanonicalType(SetterMethod->getResultType()) !=
1850 Context.VoidTy)
1851 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1852 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001853 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001854 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1855 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001856 Diag(property->getLocation(),
1857 diag::warn_accessor_property_type_mismatch)
1858 << property->getDeclName()
1859 << SetterMethod->getSelector();
1860 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1861 }
1862 }
1863
1864 // Synthesize getter/setter methods if none exist.
1865 // Find the default getter and if one not found, add one.
1866 // FIXME: The synthesized property we set here is misleading. We almost always
1867 // synthesize these methods unless the user explicitly provided prototypes
1868 // (which is odd, but allowed). Sema should be typechecking that the
1869 // declarations jive in that situation (which it is not currently).
1870 if (!GetterMethod) {
1871 // No instance method of same name as property getter name was found.
1872 // Declare a getter method and add it to the list of methods
1873 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001874 SourceLocation Loc = redeclaredProperty ?
1875 redeclaredProperty->getLocation() :
1876 property->getLocation();
1877
1878 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1879 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001880 property->getType(), 0, CD, /*isInstance=*/true,
1881 /*isVariadic=*/false, /*isSynthesized=*/true,
1882 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001883 (property->getPropertyImplementation() ==
1884 ObjCPropertyDecl::Optional) ?
1885 ObjCMethodDecl::Optional :
1886 ObjCMethodDecl::Required);
1887 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001888
1889 AddPropertyAttrs(*this, GetterMethod, property);
1890
Ted Kremenek23173d72010-05-18 21:09:07 +00001891 // FIXME: Eventually this shouldn't be needed, as the lexical context
1892 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001893 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001894 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001895 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1896 GetterMethod->addAttr(
1897 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001898 } else
1899 // A user declared getter will be synthesize when @synthesize of
1900 // the property with the same name is seen in the @implementation
1901 GetterMethod->setSynthesized(true);
1902 property->setGetterMethodDecl(GetterMethod);
1903
1904 // Skip setter if property is read-only.
1905 if (!property->isReadOnly()) {
1906 // Find the default setter and if one not found, add one.
1907 if (!SetterMethod) {
1908 // No instance method of same name as property setter name was found.
1909 // Declare a setter method and add it to the list of methods
1910 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001911 SourceLocation Loc = redeclaredProperty ?
1912 redeclaredProperty->getLocation() :
1913 property->getLocation();
1914
1915 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001916 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001917 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001918 CD, /*isInstance=*/true, /*isVariadic=*/false,
1919 /*isSynthesized=*/true,
1920 /*isImplicitlyDeclared=*/true,
1921 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001922 (property->getPropertyImplementation() ==
1923 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001924 ObjCMethodDecl::Optional :
1925 ObjCMethodDecl::Required);
1926
Ted Kremenek9d64c152010-03-12 00:38:38 +00001927 // Invent the arguments for the setter. We don't bother making a
1928 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001929 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1930 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001931 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001932 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001933 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001934 SC_None,
1935 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001936 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001937 SetterMethod->setMethodParams(Context, Argument,
1938 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001939
1940 AddPropertyAttrs(*this, SetterMethod, property);
1941
Ted Kremenek9d64c152010-03-12 00:38:38 +00001942 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001943 // FIXME: Eventually this shouldn't be needed, as the lexical context
1944 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001945 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001946 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001947 } else
1948 // A user declared setter will be synthesize when @synthesize of
1949 // the property with the same name is seen in the @implementation
1950 SetterMethod->setSynthesized(true);
1951 property->setSetterMethodDecl(SetterMethod);
1952 }
1953 // Add any synthesized methods to the global pool. This allows us to
1954 // handle the following, which is supported by GCC (and part of the design).
1955 //
1956 // @interface Foo
1957 // @property double bar;
1958 // @end
1959 //
1960 // void thisIsUnfortunate() {
1961 // id foo;
1962 // double bar = [foo bar];
1963 // }
1964 //
1965 if (GetterMethod)
1966 AddInstanceMethodToGlobalPool(GetterMethod);
1967 if (SetterMethod)
1968 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001969
1970 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1971 if (!CurrentClass) {
1972 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1973 CurrentClass = Cat->getClassInterface();
1974 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1975 CurrentClass = Impl->getClassInterface();
1976 }
1977 if (GetterMethod)
1978 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1979 if (SetterMethod)
1980 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001981}
1982
John McCalld226f652010-08-21 09:40:31 +00001983void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001984 SourceLocation Loc,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001985 unsigned &Attributes,
1986 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001987 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001988 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001989 return;
1990
1991 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001992 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001993
David Blaikie4e4d0842012-03-11 07:00:24 +00001994 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001995 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1996 PropertyTy->isObjCRetainableType()) {
1997 // 'readonly' property with no obvious lifetime.
1998 // its life time will be determined by its backing ivar.
1999 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
2000 ObjCDeclSpec::DQ_PR_copy |
2001 ObjCDeclSpec::DQ_PR_retain |
2002 ObjCDeclSpec::DQ_PR_strong |
2003 ObjCDeclSpec::DQ_PR_weak |
2004 ObjCDeclSpec::DQ_PR_assign);
2005 if ((Attributes & rel) == 0)
2006 return;
2007 }
2008
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002009 if (propertyInPrimaryClass) {
2010 // we postpone most property diagnosis until class's implementation
2011 // because, its readonly attribute may be overridden in its class
2012 // extensions making other attributes, which make no sense, to make sense.
2013 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2014 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2015 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2016 << "readonly" << "readwrite";
2017 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002018 // readonly and readwrite/assign/retain/copy conflict.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002019 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2020 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002021 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002022 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002023 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002024 ObjCDeclSpec::DQ_PR_retain |
2025 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002026 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2027 "readwrite" :
2028 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2029 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00002030 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
2031 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00002032 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2033 "copy" : "retain";
2034
2035 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
2036 diag::err_objc_property_attr_mutually_exclusive :
2037 diag::warn_objc_property_attr_mutually_exclusive)
2038 << "readonly" << which;
2039 }
2040
2041 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00002042 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2043 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2044 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002045 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002046 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00002047 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2048 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2049 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2050 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002051 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002052 }
2053
2054 // Check for more than one of { assign, copy, retain }.
2055 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2056 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2057 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2058 << "assign" << "copy";
2059 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2060 }
2061 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2062 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2063 << "assign" << "retain";
2064 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2065 }
John McCallf85e1932011-06-15 23:02:42 +00002066 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2067 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2068 << "assign" << "strong";
2069 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2070 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002071 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002072 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2073 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2074 << "assign" << "weak";
2075 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2076 }
2077 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2078 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2079 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2080 << "unsafe_unretained" << "copy";
2081 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2082 }
2083 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2084 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2085 << "unsafe_unretained" << "retain";
2086 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2087 }
2088 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2089 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2090 << "unsafe_unretained" << "strong";
2091 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2092 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002093 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002094 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2095 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2096 << "unsafe_unretained" << "weak";
2097 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2098 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002099 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2100 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2101 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2102 << "copy" << "retain";
2103 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2104 }
John McCallf85e1932011-06-15 23:02:42 +00002105 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2106 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2107 << "copy" << "strong";
2108 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2109 }
2110 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2111 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2112 << "copy" << "weak";
2113 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2114 }
2115 }
2116 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2117 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2118 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2119 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002120 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002121 }
2122 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2123 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2124 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2125 << "strong" << "weak";
2126 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002127 }
2128
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002129 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2130 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2131 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2132 << "atomic" << "nonatomic";
2133 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2134 }
2135
Ted Kremenek9d64c152010-03-12 00:38:38 +00002136 // Warn if user supplied no assignment attribute, property is
2137 // readwrite, and this is an object type.
2138 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002139 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2140 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2141 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002142 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002143 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002144 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002145 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002146 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002147 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002148 bool isAnyClassTy =
2149 (PropertyTy->isObjCClassType() ||
2150 PropertyTy->isObjCQualifiedClassType());
2151 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2152 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002153 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002154 ;
2155 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002156 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002157 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002158 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002159
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002160 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002161 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002162 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002163 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002164 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002165
2166 // FIXME: Implement warning dependent on NSCopying being
2167 // implemented. See also:
2168 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2169 // (please trim this list while you are at it).
2170 }
2171
2172 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002173 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002174 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002175 && PropertyTy->isBlockPointerType())
2176 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian7c16d582012-06-27 20:52:46 +00002177 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002178 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2179 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2180 PropertyTy->isBlockPointerType())
2181 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002182
2183 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2184 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2185 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2186
Ted Kremenek9d64c152010-03-12 00:38:38 +00002187}