blob: 4d2fb777e2164ea1ceaaf7e4295ee699fade0018 [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 ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000664 (classExtPropertyAttr &
665 (ObjCDeclSpec::DQ_PR_readwrite|
666 ObjCDeclSpec::DQ_PR_assign |
667 ObjCDeclSpec::DQ_PR_unsafe_unretained |
668 ObjCDeclSpec::DQ_PR_copy |
669 ObjCDeclSpec::DQ_PR_retain |
670 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000671 continue;
672 warn = true;
673 break;
674 }
675 }
676 }
677 if (warn) {
678 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
679 ObjCDeclSpec::DQ_PR_unsafe_unretained |
680 ObjCDeclSpec::DQ_PR_copy |
681 ObjCDeclSpec::DQ_PR_retain |
682 ObjCDeclSpec::DQ_PR_strong);
683 if (Attributes & setterAttrs) {
684 const char * which =
685 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
686 "assign" :
687 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
688 "unsafe_unretained" :
689 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
690 "copy" :
691 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
692 "retain" : "strong";
693
694 S.Diag(property->getLocation(),
695 diag::warn_objc_property_attr_mutually_exclusive)
696 << "readonly" << which;
697 }
698 }
699
700
701}
702
Ted Kremenek28685ab2010-03-12 00:46:40 +0000703/// ActOnPropertyImplDecl - This routine performs semantic checks and
704/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000705/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000706///
John McCalld226f652010-08-21 09:40:31 +0000707Decl *Sema::ActOnPropertyImplDecl(Scope *S,
708 SourceLocation AtLoc,
709 SourceLocation PropertyLoc,
710 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000711 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000712 IdentifierInfo *PropertyIvar,
713 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000714 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000715 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000716 // Make sure we have a context for the property implementation declaration.
717 if (!ClassImpDecl) {
718 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000719 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000720 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000721 if (PropertyIvarLoc.isInvalid())
722 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000723 SourceLocation PropertyDiagLoc = PropertyLoc;
724 if (PropertyDiagLoc.isInvalid())
725 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000726 ObjCPropertyDecl *property = 0;
727 ObjCInterfaceDecl* IDecl = 0;
728 // Find the class or category class where this property must have
729 // a declaration.
730 ObjCImplementationDecl *IC = 0;
731 ObjCCategoryImplDecl* CatImplClass = 0;
732 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
733 IDecl = IC->getClassInterface();
734 // We always synthesize an interface for an implementation
735 // without an interface decl. So, IDecl is always non-zero.
736 assert(IDecl &&
737 "ActOnPropertyImplDecl - @implementation without @interface");
738
739 // Look for this property declaration in the @implementation's @interface
740 property = IDecl->FindPropertyDeclaration(PropertyId);
741 if (!property) {
742 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000743 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000744 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000745 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000746 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
747 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000748 if (AtLoc.isValid())
749 Diag(AtLoc, diag::warn_implicit_atomic_property);
750 else
751 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
752 Diag(property->getLocation(), diag::note_property_declare);
753 }
754
Ted Kremenek28685ab2010-03-12 00:46:40 +0000755 if (const ObjCCategoryDecl *CD =
756 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
757 if (!CD->IsClassExtension()) {
758 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
759 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000760 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000761 }
762 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000763
764 if (Synthesize&&
765 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
766 property->hasAttr<IBOutletAttr>() &&
767 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000768 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
769 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000770 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000771 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000772 property->getLParenLoc(), readonlyLoc)) {
773 SourceLocation endLoc =
774 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
775 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
776 Diag(property->getLocation(),
777 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
778 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
779 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000780 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000781
782 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000783
Ted Kremenek28685ab2010-03-12 00:46:40 +0000784 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
785 if (Synthesize) {
786 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000787 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000788 }
789 IDecl = CatImplClass->getClassInterface();
790 if (!IDecl) {
791 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000792 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000793 }
794 ObjCCategoryDecl *Category =
795 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
796
797 // If category for this implementation not found, it is an error which
798 // has already been reported eralier.
799 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000800 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000801 // Look for this property declaration in @implementation's category
802 property = Category->FindPropertyDeclaration(PropertyId);
803 if (!property) {
804 Diag(PropertyLoc, diag::error_bad_category_property_decl)
805 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000806 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000807 }
808 } else {
809 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000810 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000811 }
812 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000813 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000814 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000815 // Check that we have a valid, previously declared ivar for @synthesize
816 if (Synthesize) {
817 // @synthesize
818 if (!PropertyIvar)
819 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000820 // Check that this is a previously declared 'ivar' in 'IDecl' interface
821 ObjCInterfaceDecl *ClassDeclared;
822 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
823 QualType PropType = property->getType();
824 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000825
826 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000827 diag::err_incomplete_synthesized_property,
828 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000829 Diag(property->getLocation(), diag::note_property_declare);
830 CompleteTypeErr = true;
831 }
832
David Blaikie4e4d0842012-03-11 07:00:24 +0000833 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000834 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000835 ObjCPropertyDecl::OBJC_PR_readonly) &&
836 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000837 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
838 }
839
John McCallf85e1932011-06-15 23:02:42 +0000840 ObjCPropertyDecl::PropertyAttributeKind kind
841 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000842
843 // Add GC __weak to the ivar type if the property is weak.
844 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000845 getLangOpts().getGC() != LangOptions::NonGC) {
846 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000847 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000848 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000849 Diag(property->getLocation(), diag::note_property_declare);
850 } else {
851 PropertyIvarType =
852 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000853 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000854 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000855 if (AtLoc.isInvalid()) {
856 // Check when default synthesizing a property that there is
857 // an ivar matching property name and issue warning; since this
858 // is the most common case of not using an ivar used for backing
859 // property in non-default synthesis case.
860 ObjCInterfaceDecl *ClassDeclared=0;
861 ObjCIvarDecl *originalIvar =
862 IDecl->lookupInstanceVariable(property->getIdentifier(),
863 ClassDeclared);
864 if (originalIvar) {
865 Diag(PropertyDiagLoc,
866 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000867 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000868 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000869 Diag(property->getLocation(), diag::note_property_declare);
870 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000871 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000872 }
873
874 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000875 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000876 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000877 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000878 !PropertyIvarType.getObjCLifetime() &&
879 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000880
John McCall265941b2011-09-13 18:31:23 +0000881 // It's an error if we have to do this and the user didn't
882 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000883 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000884 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000885 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000886 diag::err_arc_objc_property_default_assign_on_object);
887 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000888 } else {
889 Qualifiers::ObjCLifetime lifetime =
890 getImpliedARCOwnership(kind, PropertyIvarType);
891 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000892 if (lifetime == Qualifiers::OCL_Weak) {
893 bool err = false;
894 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000895 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
896 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
897 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000898 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000899 Diag(property->getLocation(), diag::note_property_declare);
900 err = true;
901 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000902 }
John McCall0a7dd782012-08-21 02:47:43 +0000903 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000904 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000905 Diag(property->getLocation(), diag::note_property_declare);
906 }
John McCallf85e1932011-06-15 23:02:42 +0000907 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000908
John McCallf85e1932011-06-15 23:02:42 +0000909 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000910 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000911 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
912 }
John McCallf85e1932011-06-15 23:02:42 +0000913 }
914
915 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000916 !getLangOpts().ObjCAutoRefCount &&
917 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000918 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000919 Diag(property->getLocation(), diag::note_property_declare);
920 }
921
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000922 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000923 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000924 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000925 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000926 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000927 if (CompleteTypeErr)
928 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000929 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000930 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000931 property->setPropertyIvarDecl(Ivar);
932
John McCall260611a2012-06-20 06:18:46 +0000933 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000934 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
935 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000936 // Note! I deliberately want it to fall thru so, we have a
937 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000938 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000939 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000940 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000941 << property->getDeclName() << Ivar->getDeclName()
942 << ClassDeclared->getDeclName();
943 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000944 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000945 // Note! I deliberately want it to fall thru so more errors are caught.
946 }
947 QualType IvarType = Context.getCanonicalType(Ivar->getType());
948
949 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000950 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +0000951 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000952 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +0000953 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000954 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000955 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000956 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000957 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000958 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
959 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000960 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000961 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000962 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000963 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000964 << property->getDeclName() << PropType
965 << Ivar->getDeclName() << IvarType;
966 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000967 // Note! I deliberately want it to fall thru so, we have a
968 // a property implementation and to avoid future warnings.
969 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000970 else {
971 // FIXME! Rules for properties are somewhat different that those
972 // for assignments. Use a new routine to consolidate all cases;
973 // specifically for property redeclarations as well as for ivars.
974 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
975 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
976 if (lhsType != rhsType &&
977 lhsType->isArithmeticType()) {
978 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
979 << property->getDeclName() << PropType
980 << Ivar->getDeclName() << IvarType;
981 Diag(Ivar->getLocation(), diag::note_ivar_decl);
982 // Fall thru - see previous comment
983 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000984 }
985 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000986 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000987 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000988 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000989 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000990 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000991 // Fall thru - see previous comment
992 }
John McCallf85e1932011-06-15 23:02:42 +0000993 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000994 if ((property->getType()->isObjCObjectPointerType() ||
995 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000996 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000997 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000998 << property->getDeclName() << Ivar->getDeclName();
999 // Fall thru - see previous comment
1000 }
1001 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001002 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001003 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001004 } else if (PropertyIvar)
1005 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001006 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001007
Ted Kremenek28685ab2010-03-12 00:46:40 +00001008 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1009 ObjCPropertyImplDecl *PIDecl =
1010 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1011 property,
1012 (Synthesize ?
1013 ObjCPropertyImplDecl::Synthesize
1014 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001015 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001016
Fariborz Jahanian74414712012-05-15 18:12:51 +00001017 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001018 PIDecl->setInvalidDecl();
1019
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001020 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1021 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001022 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001023 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001024 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1025 // returned by the getter as it must conform to C++'s copy-return rules.
1026 // FIXME. Eventually we want to do this for Objective-C as well.
1027 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1028 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001029 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001030 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001031 Expr *IvarRefExpr =
1032 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1033 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001034 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001035 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001036 SourceLocation(),
1037 getterMethod->getResultType(),
1038 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001039 SourceLocation(),
1040 Owned(IvarRefExpr));
1041 if (!Res.isInvalid()) {
1042 Expr *ResExpr = Res.takeAs<Expr>();
1043 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001044 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001045 PIDecl->setGetterCXXConstructor(ResExpr);
1046 }
1047 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001048 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1049 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1050 Diag(getterMethod->getLocation(),
1051 diag::warn_property_getter_owning_mismatch);
1052 Diag(property->getLocation(), diag::note_property_declare);
1053 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001054 }
1055 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1056 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001057 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1058 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001059 // FIXME. Eventually we want to do this for Objective-C as well.
1060 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1061 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001062 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001063 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001064 Expr *lhs =
1065 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1066 SelfExpr, true, true);
1067 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1068 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001069 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +00001070 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +00001071 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +00001072 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +00001073 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001074 if (property->getPropertyAttributes() &
1075 ObjCPropertyDecl::OBJC_PR_atomic) {
1076 Expr *callExpr = Res.takeAs<Expr>();
1077 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001078 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1079 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001080 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001081 if (property->getType()->isReferenceType()) {
1082 Diag(PropertyLoc,
1083 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001084 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001085 Diag(FuncDecl->getLocStart(),
1086 diag::note_callee_decl) << FuncDecl;
1087 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001088 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001089 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1090 }
1091 }
1092
Ted Kremenek28685ab2010-03-12 00:46:40 +00001093 if (IC) {
1094 if (Synthesize)
1095 if (ObjCPropertyImplDecl *PPIDecl =
1096 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1097 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1098 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1099 << PropertyIvar;
1100 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1101 }
1102
1103 if (ObjCPropertyImplDecl *PPIDecl
1104 = IC->FindPropertyImplDecl(PropertyId)) {
1105 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1106 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001107 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001108 }
1109 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001110 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001111 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001112 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001113 // Diagnose if an ivar was lazily synthesdized due to a previous
1114 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001115 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001116 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001117 ObjCIvarDecl *Ivar = 0;
1118 if (!Synthesize)
1119 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1120 else {
1121 if (PropertyIvar && PropertyIvar != PropertyId)
1122 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1123 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001124 // Issue diagnostics only if Ivar belongs to current class.
1125 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001126 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001127 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1128 << PropertyId;
1129 Ivar->setInvalidDecl();
1130 }
1131 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001132 } else {
1133 if (Synthesize)
1134 if (ObjCPropertyImplDecl *PPIDecl =
1135 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001136 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001137 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1138 << PropertyIvar;
1139 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1140 }
1141
1142 if (ObjCPropertyImplDecl *PPIDecl =
1143 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001144 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001145 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001146 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001147 }
1148 CatImplClass->addPropertyImplementation(PIDecl);
1149 }
1150
John McCalld226f652010-08-21 09:40:31 +00001151 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001152}
1153
1154//===----------------------------------------------------------------------===//
1155// Helper methods.
1156//===----------------------------------------------------------------------===//
1157
Ted Kremenek9d64c152010-03-12 00:38:38 +00001158/// DiagnosePropertyMismatch - Compares two properties for their
1159/// attributes and types and warns on a variety of inconsistencies.
1160///
1161void
1162Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1163 ObjCPropertyDecl *SuperProperty,
1164 const IdentifierInfo *inheritedName) {
1165 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1166 Property->getPropertyAttributes();
1167 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1168 SuperProperty->getPropertyAttributes();
1169 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1170 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1171 Diag(Property->getLocation(), diag::warn_readonly_property)
1172 << Property->getDeclName() << inheritedName;
1173 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1174 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1175 Diag(Property->getLocation(), diag::warn_property_attribute)
1176 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001177 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001178 unsigned CAttrRetain =
1179 (CAttr &
1180 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1181 unsigned SAttrRetain =
1182 (SAttr &
1183 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1184 bool CStrong = (CAttrRetain != 0);
1185 bool SStrong = (SAttrRetain != 0);
1186 if (CStrong != SStrong)
1187 Diag(Property->getLocation(), diag::warn_property_attribute)
1188 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1189 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001190
1191 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1192 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1193 Diag(Property->getLocation(), diag::warn_property_attribute)
1194 << Property->getDeclName() << "atomic" << inheritedName;
1195 if (Property->getSetterName() != SuperProperty->getSetterName())
1196 Diag(Property->getLocation(), diag::warn_property_attribute)
1197 << Property->getDeclName() << "setter" << inheritedName;
1198 if (Property->getGetterName() != SuperProperty->getGetterName())
1199 Diag(Property->getLocation(), diag::warn_property_attribute)
1200 << Property->getDeclName() << "getter" << inheritedName;
1201
1202 QualType LHSType =
1203 Context.getCanonicalType(SuperProperty->getType());
1204 QualType RHSType =
1205 Context.getCanonicalType(Property->getType());
1206
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001207 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001208 // Do cases not handled in above.
1209 // FIXME. For future support of covariant property types, revisit this.
1210 bool IncompatibleObjC = false;
1211 QualType ConvertedType;
1212 if (!isObjCPointerConversion(RHSType, LHSType,
1213 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001214 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001215 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1216 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001217 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1218 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001219 }
1220}
1221
1222bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1223 ObjCMethodDecl *GetterMethod,
1224 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001225 if (!GetterMethod)
1226 return false;
1227 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1228 QualType PropertyIvarType = property->getType().getNonReferenceType();
1229 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1230 if (!compat) {
1231 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1232 isa<ObjCObjectPointerType>(GetterType))
1233 compat =
1234 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001235 GetterType->getAs<ObjCObjectPointerType>(),
1236 PropertyIvarType->getAs<ObjCObjectPointerType>());
1237 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001238 != Compatible) {
1239 Diag(Loc, diag::error_property_accessor_type)
1240 << property->getDeclName() << PropertyIvarType
1241 << GetterMethod->getSelector() << GetterType;
1242 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1243 return true;
1244 } else {
1245 compat = true;
1246 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1247 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1248 if (lhsType != rhsType && lhsType->isArithmeticType())
1249 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001250 }
1251 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001252
1253 if (!compat) {
1254 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1255 << property->getDeclName()
1256 << GetterMethod->getSelector();
1257 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1258 return true;
1259 }
1260
Ted Kremenek9d64c152010-03-12 00:38:38 +00001261 return false;
1262}
1263
1264/// ComparePropertiesInBaseAndSuper - This routine compares property
1265/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001266/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001267///
1268void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1269 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1270 if (!SDecl)
1271 return;
1272 // FIXME: O(N^2)
1273 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1274 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001275 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001276 // Does property in super class has declaration in current class?
1277 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1278 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001279 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001280 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1281 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1282 SDecl->getIdentifier());
1283 }
1284 }
1285}
1286
1287/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1288/// of properties declared in a protocol and compares their attribute against
1289/// the same property declared in the class or category.
1290void
1291Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1292 ObjCProtocolDecl *PDecl) {
1293 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1294 if (!IDecl) {
1295 // Category
1296 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1297 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1298 if (!CatDecl->IsClassExtension())
1299 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1300 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001301 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001302 ObjCCategoryDecl::prop_iterator CP, CE;
1303 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001304 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001305 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001306 break;
1307 if (CP != CE)
1308 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001309 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001310 }
1311 return;
1312 }
1313 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1314 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001315 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001316 ObjCInterfaceDecl::prop_iterator CP, CE;
1317 // Is this property already in class's list of properties?
1318 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001319 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001320 break;
1321 if (CP != CE)
1322 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001323 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001324 }
1325}
1326
1327/// CompareProperties - This routine compares properties
1328/// declared in 'ClassOrProtocol' objects (which can be a class or an
1329/// inherited protocol with the list of properties for class/category 'CDecl'
1330///
John McCalld226f652010-08-21 09:40:31 +00001331void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1332 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001333 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1334
1335 if (!IDecl) {
1336 // Category
1337 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1338 assert (CatDecl && "CompareProperties");
1339 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1340 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1341 E = MDecl->protocol_end(); P != E; ++P)
1342 // Match properties of category with those of protocol (*P)
1343 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1344
1345 // Go thru the list of protocols for this category and recursively match
1346 // their properties with those in the category.
1347 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1348 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001349 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001350 } else {
1351 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1352 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1353 E = MD->protocol_end(); P != E; ++P)
1354 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1355 }
1356 return;
1357 }
1358
1359 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001360 for (ObjCInterfaceDecl::all_protocol_iterator
1361 P = MDecl->all_referenced_protocol_begin(),
1362 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001363 // Match properties of class IDecl with those of protocol (*P).
1364 MatchOneProtocolPropertiesInClass(IDecl, *P);
1365
1366 // Go thru the list of protocols for this class and recursively match
1367 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001368 for (ObjCInterfaceDecl::all_protocol_iterator
1369 P = IDecl->all_referenced_protocol_begin(),
1370 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001371 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001372 } else {
1373 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1374 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1375 E = MD->protocol_end(); P != E; ++P)
1376 MatchOneProtocolPropertiesInClass(IDecl, *P);
1377 }
1378}
1379
1380/// isPropertyReadonly - Return true if property is readonly, by searching
1381/// for the property in the class and in its categories and implementations
1382///
1383bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1384 ObjCInterfaceDecl *IDecl) {
1385 // by far the most common case.
1386 if (!PDecl->isReadOnly())
1387 return false;
1388 // Even if property is ready only, if interface has a user defined setter,
1389 // it is not considered read only.
1390 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1391 return false;
1392
1393 // Main class has the property as 'readonly'. Must search
1394 // through the category list to see if the property's
1395 // attribute has been over-ridden to 'readwrite'.
1396 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1397 Category; Category = Category->getNextClassCategory()) {
1398 // Even if property is ready only, if a category has a user defined setter,
1399 // it is not considered read only.
1400 if (Category->getInstanceMethod(PDecl->getSetterName()))
1401 return false;
1402 ObjCPropertyDecl *P =
1403 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1404 if (P && !P->isReadOnly())
1405 return false;
1406 }
1407
1408 // Also, check for definition of a setter method in the implementation if
1409 // all else failed.
1410 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1411 if (ObjCImplementationDecl *IMD =
1412 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1413 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1414 return false;
1415 } else if (ObjCCategoryImplDecl *CIMD =
1416 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1417 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1418 return false;
1419 }
1420 }
1421 // Lastly, look through the implementation (if one is in scope).
1422 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1423 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1424 return false;
1425 // If all fails, look at the super class.
1426 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1427 return isPropertyReadonly(PDecl, SIDecl);
1428 return true;
1429}
1430
1431/// CollectImmediateProperties - This routine collects all properties in
1432/// the class and its conforming protocols; but not those it its super class.
1433void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001434 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1435 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001436 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1437 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1438 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001439 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001440 PropMap[Prop->getIdentifier()] = Prop;
1441 }
1442 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001443 for (ObjCInterfaceDecl::all_protocol_iterator
1444 PI = IDecl->all_referenced_protocol_begin(),
1445 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001446 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001447 }
1448 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1449 if (!CATDecl->IsClassExtension())
1450 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1451 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001452 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001453 PropMap[Prop->getIdentifier()] = Prop;
1454 }
1455 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001456 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001457 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001458 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001459 }
1460 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1461 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1462 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001463 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001464 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1465 // Exclude property for protocols which conform to class's super-class,
1466 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001467 if (!PropertyFromSuper ||
1468 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001469 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1470 if (!PropEntry)
1471 PropEntry = Prop;
1472 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001473 }
1474 // scan through protocol's protocols.
1475 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1476 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001477 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001478 }
1479}
1480
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001481/// CollectClassPropertyImplementations - This routine collects list of
1482/// properties to be implemented in the class. This includes, class's
1483/// and its conforming protocols' properties.
1484static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1485 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1486 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1487 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1488 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001489 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001490 PropMap[Prop->getIdentifier()] = Prop;
1491 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001492 for (ObjCInterfaceDecl::all_protocol_iterator
1493 PI = IDecl->all_referenced_protocol_begin(),
1494 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001495 CollectClassPropertyImplementations((*PI), PropMap);
1496 }
1497 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1498 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1499 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001500 ObjCPropertyDecl *Prop = *P;
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00001501 // Insert into PropMap if not there already.
1502 PropMap.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001503 }
1504 // scan through protocol's protocols.
1505 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1506 E = PDecl->protocol_end(); PI != E; ++PI)
1507 CollectClassPropertyImplementations((*PI), PropMap);
1508 }
1509}
1510
1511/// CollectSuperClassPropertyImplementations - This routine collects list of
1512/// properties to be implemented in super class(s) and also coming from their
1513/// conforming protocols.
1514static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1515 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1516 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1517 while (SDecl) {
1518 CollectClassPropertyImplementations(SDecl, PropMap);
1519 SDecl = SDecl->getSuperClass();
1520 }
1521 }
1522}
1523
Ted Kremenek9d64c152010-03-12 00:38:38 +00001524/// LookupPropertyDecl - Looks up a property in the current class and all
1525/// its protocols.
1526ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1527 IdentifierInfo *II) {
1528 if (const ObjCInterfaceDecl *IDecl =
1529 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1530 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1531 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001532 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001533 if (Prop->getIdentifier() == II)
1534 return Prop;
1535 }
1536 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001537 for (ObjCInterfaceDecl::all_protocol_iterator
1538 PI = IDecl->all_referenced_protocol_begin(),
1539 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001540 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1541 if (Prop)
1542 return Prop;
1543 }
1544 }
1545 else if (const ObjCProtocolDecl *PDecl =
1546 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1547 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1548 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001549 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001550 if (Prop->getIdentifier() == II)
1551 return Prop;
1552 }
1553 // scan through protocol's protocols.
1554 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1555 E = PDecl->protocol_end(); PI != E; ++PI) {
1556 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1557 if (Prop)
1558 return Prop;
1559 }
1560 }
1561 return 0;
1562}
1563
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001564static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1565 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001566 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001567 {
1568 llvm::raw_svector_ostream os(ivarName);
1569 os << '_' << Prop->getIdentifier()->getName();
1570 }
1571 return &Ctx.Idents.get(ivarName.str());
1572}
1573
James Dennett699c9042012-06-15 07:13:21 +00001574/// \brief Default synthesizes all properties which must be synthesized
1575/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001576void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1577 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001578
1579 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1580 CollectClassPropertyImplementations(IDecl, PropMap);
1581 if (PropMap.empty())
1582 return;
1583 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1584 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1585
1586 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1587 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1588 ObjCPropertyDecl *Prop = P->second;
1589 // If property to be implemented in the super class, ignore.
1590 if (SuperPropMap[Prop->getIdentifier()])
1591 continue;
1592 // Is there a matching propery synthesize/dynamic?
1593 if (Prop->isInvalidDecl() ||
1594 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1595 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1596 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001597 // Property may have been synthesized by user.
1598 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1599 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001600 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1601 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1602 continue;
1603 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1604 continue;
1605 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001606 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1607 // We won't auto-synthesize properties declared in protocols.
1608 Diag(IMPDecl->getLocation(),
1609 diag::warn_auto_synthesizing_protocol_property);
1610 Diag(Prop->getLocation(), diag::note_property_declare);
1611 continue;
1612 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001613
1614 // We use invalid SourceLocations for the synthesized ivars since they
1615 // aren't really synthesized at a particular location; they just exist.
1616 // Saying that they are located at the @implementation isn't really going
1617 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001618 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1619 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1620 true,
1621 /* property = */ Prop->getIdentifier(),
1622 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001623 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001624 if (PIDecl) {
1625 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001626 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001627 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001628 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001629}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001630
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001631void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001632 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001633 return;
1634 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1635 if (!IC)
1636 return;
1637 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001638 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001639 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001640}
1641
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001642void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001643 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001644 const SelectorSet &InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001645 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1646 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1647 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1648
Ted Kremenek9d64c152010-03-12 00:38:38 +00001649 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001650 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001651 if (PropMap.empty())
1652 return;
1653
1654 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1655 for (ObjCImplDecl::propimpl_iterator
1656 I = IMPDecl->propimpl_begin(),
1657 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001658 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001659
1660 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1661 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1662 ObjCPropertyDecl *Prop = P->second;
1663 // Is there a matching propery synthesize/dynamic?
1664 if (Prop->isInvalidDecl() ||
1665 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001666 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001667 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001668 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001669 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001670 isa<ObjCCategoryDecl>(CDecl) ?
1671 diag::warn_setter_getter_impl_required_in_category :
1672 diag::warn_setter_getter_impl_required)
1673 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001674 Diag(Prop->getLocation(),
1675 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001676 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001677 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001678 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001679 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1680
Ted Kremenek9d64c152010-03-12 00:38:38 +00001681 }
1682
1683 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001684 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001685 isa<ObjCCategoryDecl>(CDecl) ?
1686 diag::warn_setter_getter_impl_required_in_category :
1687 diag::warn_setter_getter_impl_required)
1688 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001689 Diag(Prop->getLocation(),
1690 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001691 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001692 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001693 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001694 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001695 }
1696 }
1697}
1698
1699void
1700Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1701 ObjCContainerDecl* IDecl) {
1702 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001703 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001704 return;
1705 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1706 E = IDecl->prop_end();
1707 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001708 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001709 ObjCMethodDecl *GetterMethod = 0;
1710 ObjCMethodDecl *SetterMethod = 0;
1711 bool LookedUpGetterSetter = false;
1712
Ted Kremenek9d64c152010-03-12 00:38:38 +00001713 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001714 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001715
John McCall265941b2011-09-13 18:31:23 +00001716 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1717 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001718 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1719 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1720 LookedUpGetterSetter = true;
1721 if (GetterMethod) {
1722 Diag(GetterMethod->getLocation(),
1723 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001724 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001725 Diag(Property->getLocation(), diag::note_property_declare);
1726 }
1727 if (SetterMethod) {
1728 Diag(SetterMethod->getLocation(),
1729 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001730 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001731 Diag(Property->getLocation(), diag::note_property_declare);
1732 }
1733 }
1734
Ted Kremenek9d64c152010-03-12 00:38:38 +00001735 // We only care about readwrite atomic property.
1736 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1737 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1738 continue;
1739 if (const ObjCPropertyImplDecl *PIDecl
1740 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1741 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1742 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001743 if (!LookedUpGetterSetter) {
1744 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1745 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1746 LookedUpGetterSetter = true;
1747 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001748 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1749 SourceLocation MethodLoc =
1750 (GetterMethod ? GetterMethod->getLocation()
1751 : SetterMethod->getLocation());
1752 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001753 << Property->getIdentifier() << (GetterMethod != 0)
1754 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001755 // fixit stuff.
1756 if (!AttributesAsWritten) {
1757 if (Property->getLParenLoc().isValid()) {
1758 // @property () ... case.
1759 SourceRange PropSourceRange(Property->getAtLoc(),
1760 Property->getLParenLoc());
1761 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1762 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1763 }
1764 else {
1765 //@property id etc.
1766 SourceLocation endLoc =
1767 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1768 endLoc = endLoc.getLocWithOffset(-1);
1769 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1770 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1771 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1772 }
1773 }
1774 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1775 // @property () ... case.
1776 SourceLocation endLoc = Property->getLParenLoc();
1777 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1778 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1779 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1780 }
1781 else
1782 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001783 Diag(Property->getLocation(), diag::note_property_declare);
1784 }
1785 }
1786 }
1787}
1788
John McCallf85e1932011-06-15 23:02:42 +00001789void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001790 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001791 return;
1792
1793 for (ObjCImplementationDecl::propimpl_iterator
1794 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001795 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001796 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1797 continue;
1798
1799 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001800 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1801 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001802 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1803 if (!method)
1804 continue;
1805 ObjCMethodFamily family = method->getMethodFamily();
1806 if (family == OMF_alloc || family == OMF_copy ||
1807 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001808 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001809 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1810 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001811 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001812 Diag(PD->getLocation(), diag::note_property_declare);
1813 }
1814 }
1815 }
1816}
1817
John McCall5de74d12010-11-10 07:01:40 +00001818/// AddPropertyAttrs - Propagates attributes from a property to the
1819/// implicitly-declared getter or setter for that property.
1820static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1821 ObjCPropertyDecl *Property) {
1822 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001823 for (Decl::attr_iterator A = Property->attr_begin(),
1824 AEnd = Property->attr_end();
1825 A != AEnd; ++A) {
1826 if (isa<DeprecatedAttr>(*A) ||
1827 isa<UnavailableAttr>(*A) ||
1828 isa<AvailabilityAttr>(*A))
1829 PropertyMethod->addAttr((*A)->clone(S.Context));
1830 }
John McCall5de74d12010-11-10 07:01:40 +00001831}
1832
Ted Kremenek9d64c152010-03-12 00:38:38 +00001833/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1834/// have the property type and issue diagnostics if they don't.
1835/// Also synthesize a getter/setter method if none exist (and update the
1836/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1837/// methods is the "right" thing to do.
1838void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001839 ObjCContainerDecl *CD,
1840 ObjCPropertyDecl *redeclaredProperty,
1841 ObjCContainerDecl *lexicalDC) {
1842
Ted Kremenek9d64c152010-03-12 00:38:38 +00001843 ObjCMethodDecl *GetterMethod, *SetterMethod;
1844
1845 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1846 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1847 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1848 property->getLocation());
1849
1850 if (SetterMethod) {
1851 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1852 property->getPropertyAttributes();
1853 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1854 Context.getCanonicalType(SetterMethod->getResultType()) !=
1855 Context.VoidTy)
1856 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1857 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001858 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001859 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1860 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001861 Diag(property->getLocation(),
1862 diag::warn_accessor_property_type_mismatch)
1863 << property->getDeclName()
1864 << SetterMethod->getSelector();
1865 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1866 }
1867 }
1868
1869 // Synthesize getter/setter methods if none exist.
1870 // Find the default getter and if one not found, add one.
1871 // FIXME: The synthesized property we set here is misleading. We almost always
1872 // synthesize these methods unless the user explicitly provided prototypes
1873 // (which is odd, but allowed). Sema should be typechecking that the
1874 // declarations jive in that situation (which it is not currently).
1875 if (!GetterMethod) {
1876 // No instance method of same name as property getter name was found.
1877 // Declare a getter method and add it to the list of methods
1878 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001879 SourceLocation Loc = redeclaredProperty ?
1880 redeclaredProperty->getLocation() :
1881 property->getLocation();
1882
1883 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1884 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001885 property->getType(), 0, CD, /*isInstance=*/true,
1886 /*isVariadic=*/false, /*isSynthesized=*/true,
1887 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001888 (property->getPropertyImplementation() ==
1889 ObjCPropertyDecl::Optional) ?
1890 ObjCMethodDecl::Optional :
1891 ObjCMethodDecl::Required);
1892 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001893
1894 AddPropertyAttrs(*this, GetterMethod, property);
1895
Ted Kremenek23173d72010-05-18 21:09:07 +00001896 // FIXME: Eventually this shouldn't be needed, as the lexical context
1897 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001898 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001899 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001900 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1901 GetterMethod->addAttr(
1902 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001903 } else
1904 // A user declared getter will be synthesize when @synthesize of
1905 // the property with the same name is seen in the @implementation
1906 GetterMethod->setSynthesized(true);
1907 property->setGetterMethodDecl(GetterMethod);
1908
1909 // Skip setter if property is read-only.
1910 if (!property->isReadOnly()) {
1911 // Find the default setter and if one not found, add one.
1912 if (!SetterMethod) {
1913 // No instance method of same name as property setter name was found.
1914 // Declare a setter method and add it to the list of methods
1915 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001916 SourceLocation Loc = redeclaredProperty ?
1917 redeclaredProperty->getLocation() :
1918 property->getLocation();
1919
1920 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001921 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001922 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001923 CD, /*isInstance=*/true, /*isVariadic=*/false,
1924 /*isSynthesized=*/true,
1925 /*isImplicitlyDeclared=*/true,
1926 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001927 (property->getPropertyImplementation() ==
1928 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001929 ObjCMethodDecl::Optional :
1930 ObjCMethodDecl::Required);
1931
Ted Kremenek9d64c152010-03-12 00:38:38 +00001932 // Invent the arguments for the setter. We don't bother making a
1933 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001934 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1935 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001936 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001937 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001938 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001939 SC_None,
1940 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001941 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001942 SetterMethod->setMethodParams(Context, Argument,
1943 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001944
1945 AddPropertyAttrs(*this, SetterMethod, property);
1946
Ted Kremenek9d64c152010-03-12 00:38:38 +00001947 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001948 // FIXME: Eventually this shouldn't be needed, as the lexical context
1949 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001950 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001951 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001952 } else
1953 // A user declared setter will be synthesize when @synthesize of
1954 // the property with the same name is seen in the @implementation
1955 SetterMethod->setSynthesized(true);
1956 property->setSetterMethodDecl(SetterMethod);
1957 }
1958 // Add any synthesized methods to the global pool. This allows us to
1959 // handle the following, which is supported by GCC (and part of the design).
1960 //
1961 // @interface Foo
1962 // @property double bar;
1963 // @end
1964 //
1965 // void thisIsUnfortunate() {
1966 // id foo;
1967 // double bar = [foo bar];
1968 // }
1969 //
1970 if (GetterMethod)
1971 AddInstanceMethodToGlobalPool(GetterMethod);
1972 if (SetterMethod)
1973 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001974
1975 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1976 if (!CurrentClass) {
1977 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1978 CurrentClass = Cat->getClassInterface();
1979 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1980 CurrentClass = Impl->getClassInterface();
1981 }
1982 if (GetterMethod)
1983 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1984 if (SetterMethod)
1985 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001986}
1987
John McCalld226f652010-08-21 09:40:31 +00001988void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001989 SourceLocation Loc,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001990 unsigned &Attributes,
1991 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001992 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001993 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001994 return;
1995
1996 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001997 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001998
David Blaikie4e4d0842012-03-11 07:00:24 +00001999 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002000 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2001 PropertyTy->isObjCRetainableType()) {
2002 // 'readonly' property with no obvious lifetime.
2003 // its life time will be determined by its backing ivar.
2004 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
2005 ObjCDeclSpec::DQ_PR_copy |
2006 ObjCDeclSpec::DQ_PR_retain |
2007 ObjCDeclSpec::DQ_PR_strong |
2008 ObjCDeclSpec::DQ_PR_weak |
2009 ObjCDeclSpec::DQ_PR_assign);
2010 if ((Attributes & rel) == 0)
2011 return;
2012 }
2013
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002014 if (propertyInPrimaryClass) {
2015 // we postpone most property diagnosis until class's implementation
2016 // because, its readonly attribute may be overridden in its class
2017 // extensions making other attributes, which make no sense, to make sense.
2018 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2019 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2020 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2021 << "readonly" << "readwrite";
2022 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002023 // readonly and readwrite/assign/retain/copy conflict.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002024 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2025 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002026 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002027 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002028 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002029 ObjCDeclSpec::DQ_PR_retain |
2030 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002031 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2032 "readwrite" :
2033 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2034 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00002035 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
2036 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00002037 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2038 "copy" : "retain";
2039
2040 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
2041 diag::err_objc_property_attr_mutually_exclusive :
2042 diag::warn_objc_property_attr_mutually_exclusive)
2043 << "readonly" << which;
2044 }
2045
2046 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00002047 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2048 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2049 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002050 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002051 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00002052 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2053 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2054 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2055 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002056 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002057 }
2058
2059 // Check for more than one of { assign, copy, retain }.
2060 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2061 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2062 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2063 << "assign" << "copy";
2064 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2065 }
2066 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2067 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2068 << "assign" << "retain";
2069 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2070 }
John McCallf85e1932011-06-15 23:02:42 +00002071 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2072 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2073 << "assign" << "strong";
2074 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2075 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002076 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002077 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2078 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2079 << "assign" << "weak";
2080 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2081 }
2082 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2083 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2084 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2085 << "unsafe_unretained" << "copy";
2086 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2087 }
2088 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2089 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2090 << "unsafe_unretained" << "retain";
2091 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2092 }
2093 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2094 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2095 << "unsafe_unretained" << "strong";
2096 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2097 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002098 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002099 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2100 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2101 << "unsafe_unretained" << "weak";
2102 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2103 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002104 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2105 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2106 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2107 << "copy" << "retain";
2108 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2109 }
John McCallf85e1932011-06-15 23:02:42 +00002110 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2111 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2112 << "copy" << "strong";
2113 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2114 }
2115 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2116 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2117 << "copy" << "weak";
2118 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2119 }
2120 }
2121 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2122 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2123 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2124 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002125 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002126 }
2127 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2128 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2129 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2130 << "strong" << "weak";
2131 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002132 }
2133
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002134 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2135 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2136 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2137 << "atomic" << "nonatomic";
2138 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2139 }
2140
Ted Kremenek9d64c152010-03-12 00:38:38 +00002141 // Warn if user supplied no assignment attribute, property is
2142 // readwrite, and this is an object type.
2143 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002144 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2145 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2146 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002147 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002148 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002149 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002150 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002151 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002152 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002153 bool isAnyClassTy =
2154 (PropertyTy->isObjCClassType() ||
2155 PropertyTy->isObjCQualifiedClassType());
2156 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2157 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002158 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002159 ;
2160 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002161 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002162 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002163 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002164
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002165 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002166 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002167 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002168 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002169 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002170
2171 // FIXME: Implement warning dependent on NSCopying being
2172 // implemented. See also:
2173 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2174 // (please trim this list while you are at it).
2175 }
2176
2177 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002178 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002179 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002180 && PropertyTy->isBlockPointerType())
2181 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian7c16d582012-06-27 20:52:46 +00002182 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002183 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2184 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2185 PropertyTy->isBlockPointerType())
2186 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002187
2188 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2189 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2190 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2191
Ted Kremenek9d64c152010-03-12 00:38:38 +00002192}