blob: 28b4dfa0ccadb74436142283112d6af505376c84 [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)) {
951 compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000952 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000953 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000954 compat =
955 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000956 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000957 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000958 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000959 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
960 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000961 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000962 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000963 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000964 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000965 << property->getDeclName() << PropType
966 << Ivar->getDeclName() << IvarType;
967 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000968 // Note! I deliberately want it to fall thru so, we have a
969 // a property implementation and to avoid future warnings.
970 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000971 else {
972 // FIXME! Rules for properties are somewhat different that those
973 // for assignments. Use a new routine to consolidate all cases;
974 // specifically for property redeclarations as well as for ivars.
975 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
976 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
977 if (lhsType != rhsType &&
978 lhsType->isArithmeticType()) {
979 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
980 << property->getDeclName() << PropType
981 << Ivar->getDeclName() << IvarType;
982 Diag(Ivar->getLocation(), diag::note_ivar_decl);
983 // Fall thru - see previous comment
984 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000985 }
986 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000987 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000988 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000989 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000990 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000991 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000992 // Fall thru - see previous comment
993 }
John McCallf85e1932011-06-15 23:02:42 +0000994 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000995 if ((property->getType()->isObjCObjectPointerType() ||
996 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000997 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000998 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000999 << property->getDeclName() << Ivar->getDeclName();
1000 // Fall thru - see previous comment
1001 }
1002 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001003 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001004 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001005 } else if (PropertyIvar)
1006 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001007 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001008
Ted Kremenek28685ab2010-03-12 00:46:40 +00001009 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1010 ObjCPropertyImplDecl *PIDecl =
1011 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1012 property,
1013 (Synthesize ?
1014 ObjCPropertyImplDecl::Synthesize
1015 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001016 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001017
Fariborz Jahanian74414712012-05-15 18:12:51 +00001018 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001019 PIDecl->setInvalidDecl();
1020
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001021 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1022 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001023 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001024 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001025 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1026 // returned by the getter as it must conform to C++'s copy-return rules.
1027 // FIXME. Eventually we want to do this for Objective-C as well.
1028 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1029 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001030 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001031 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001032 Expr *IvarRefExpr =
1033 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1034 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001035 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001036 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001037 SourceLocation(),
1038 getterMethod->getResultType(),
1039 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001040 SourceLocation(),
1041 Owned(IvarRefExpr));
1042 if (!Res.isInvalid()) {
1043 Expr *ResExpr = Res.takeAs<Expr>();
1044 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001045 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001046 PIDecl->setGetterCXXConstructor(ResExpr);
1047 }
1048 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001049 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1050 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1051 Diag(getterMethod->getLocation(),
1052 diag::warn_property_getter_owning_mismatch);
1053 Diag(property->getLocation(), diag::note_property_declare);
1054 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001055 }
1056 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1057 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001058 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1059 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001060 // FIXME. Eventually we want to do this for Objective-C as well.
1061 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1062 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001063 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001064 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001065 Expr *lhs =
1066 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1067 SelfExpr, true, true);
1068 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1069 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001070 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +00001071 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +00001072 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +00001073 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +00001074 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001075 if (property->getPropertyAttributes() &
1076 ObjCPropertyDecl::OBJC_PR_atomic) {
1077 Expr *callExpr = Res.takeAs<Expr>();
1078 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001079 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1080 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001081 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001082 if (property->getType()->isReferenceType()) {
1083 Diag(PropertyLoc,
1084 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001085 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001086 Diag(FuncDecl->getLocStart(),
1087 diag::note_callee_decl) << FuncDecl;
1088 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001089 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001090 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1091 }
1092 }
1093
Ted Kremenek28685ab2010-03-12 00:46:40 +00001094 if (IC) {
1095 if (Synthesize)
1096 if (ObjCPropertyImplDecl *PPIDecl =
1097 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1098 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1099 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1100 << PropertyIvar;
1101 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1102 }
1103
1104 if (ObjCPropertyImplDecl *PPIDecl
1105 = IC->FindPropertyImplDecl(PropertyId)) {
1106 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1107 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001108 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001109 }
1110 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001111 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001112 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001113 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001114 // Diagnose if an ivar was lazily synthesdized due to a previous
1115 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001116 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001117 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001118 ObjCIvarDecl *Ivar = 0;
1119 if (!Synthesize)
1120 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1121 else {
1122 if (PropertyIvar && PropertyIvar != PropertyId)
1123 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1124 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001125 // Issue diagnostics only if Ivar belongs to current class.
1126 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001127 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001128 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1129 << PropertyId;
1130 Ivar->setInvalidDecl();
1131 }
1132 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001133 } else {
1134 if (Synthesize)
1135 if (ObjCPropertyImplDecl *PPIDecl =
1136 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001137 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001138 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1139 << PropertyIvar;
1140 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1141 }
1142
1143 if (ObjCPropertyImplDecl *PPIDecl =
1144 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001145 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001146 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001147 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001148 }
1149 CatImplClass->addPropertyImplementation(PIDecl);
1150 }
1151
John McCalld226f652010-08-21 09:40:31 +00001152 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001153}
1154
1155//===----------------------------------------------------------------------===//
1156// Helper methods.
1157//===----------------------------------------------------------------------===//
1158
Ted Kremenek9d64c152010-03-12 00:38:38 +00001159/// DiagnosePropertyMismatch - Compares two properties for their
1160/// attributes and types and warns on a variety of inconsistencies.
1161///
1162void
1163Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1164 ObjCPropertyDecl *SuperProperty,
1165 const IdentifierInfo *inheritedName) {
1166 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1167 Property->getPropertyAttributes();
1168 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1169 SuperProperty->getPropertyAttributes();
1170 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1171 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1172 Diag(Property->getLocation(), diag::warn_readonly_property)
1173 << Property->getDeclName() << inheritedName;
1174 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1175 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1176 Diag(Property->getLocation(), diag::warn_property_attribute)
1177 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001178 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001179 unsigned CAttrRetain =
1180 (CAttr &
1181 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1182 unsigned SAttrRetain =
1183 (SAttr &
1184 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1185 bool CStrong = (CAttrRetain != 0);
1186 bool SStrong = (SAttrRetain != 0);
1187 if (CStrong != SStrong)
1188 Diag(Property->getLocation(), diag::warn_property_attribute)
1189 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1190 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001191
1192 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1193 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1194 Diag(Property->getLocation(), diag::warn_property_attribute)
1195 << Property->getDeclName() << "atomic" << inheritedName;
1196 if (Property->getSetterName() != SuperProperty->getSetterName())
1197 Diag(Property->getLocation(), diag::warn_property_attribute)
1198 << Property->getDeclName() << "setter" << inheritedName;
1199 if (Property->getGetterName() != SuperProperty->getGetterName())
1200 Diag(Property->getLocation(), diag::warn_property_attribute)
1201 << Property->getDeclName() << "getter" << inheritedName;
1202
1203 QualType LHSType =
1204 Context.getCanonicalType(SuperProperty->getType());
1205 QualType RHSType =
1206 Context.getCanonicalType(Property->getType());
1207
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001208 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001209 // Do cases not handled in above.
1210 // FIXME. For future support of covariant property types, revisit this.
1211 bool IncompatibleObjC = false;
1212 QualType ConvertedType;
1213 if (!isObjCPointerConversion(RHSType, LHSType,
1214 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001215 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001216 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1217 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001218 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1219 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001220 }
1221}
1222
1223bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1224 ObjCMethodDecl *GetterMethod,
1225 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001226 if (!GetterMethod)
1227 return false;
1228 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1229 QualType PropertyIvarType = property->getType().getNonReferenceType();
1230 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1231 if (!compat) {
1232 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1233 isa<ObjCObjectPointerType>(GetterType))
1234 compat =
1235 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001236 GetterType->getAs<ObjCObjectPointerType>(),
1237 PropertyIvarType->getAs<ObjCObjectPointerType>());
1238 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001239 != Compatible) {
1240 Diag(Loc, diag::error_property_accessor_type)
1241 << property->getDeclName() << PropertyIvarType
1242 << GetterMethod->getSelector() << GetterType;
1243 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1244 return true;
1245 } else {
1246 compat = true;
1247 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1248 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1249 if (lhsType != rhsType && lhsType->isArithmeticType())
1250 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001251 }
1252 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001253
1254 if (!compat) {
1255 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1256 << property->getDeclName()
1257 << GetterMethod->getSelector();
1258 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1259 return true;
1260 }
1261
Ted Kremenek9d64c152010-03-12 00:38:38 +00001262 return false;
1263}
1264
1265/// ComparePropertiesInBaseAndSuper - This routine compares property
1266/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001267/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001268///
1269void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1270 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1271 if (!SDecl)
1272 return;
1273 // FIXME: O(N^2)
1274 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1275 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001276 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001277 // Does property in super class has declaration in current class?
1278 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1279 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001280 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001281 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1282 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1283 SDecl->getIdentifier());
1284 }
1285 }
1286}
1287
1288/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1289/// of properties declared in a protocol and compares their attribute against
1290/// the same property declared in the class or category.
1291void
1292Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1293 ObjCProtocolDecl *PDecl) {
1294 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1295 if (!IDecl) {
1296 // Category
1297 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1298 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1299 if (!CatDecl->IsClassExtension())
1300 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1301 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001302 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001303 ObjCCategoryDecl::prop_iterator CP, CE;
1304 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001305 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001306 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001307 break;
1308 if (CP != CE)
1309 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001310 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001311 }
1312 return;
1313 }
1314 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1315 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001316 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001317 ObjCInterfaceDecl::prop_iterator CP, CE;
1318 // Is this property already in class's list of properties?
1319 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001320 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001321 break;
1322 if (CP != CE)
1323 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001324 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001325 }
1326}
1327
1328/// CompareProperties - This routine compares properties
1329/// declared in 'ClassOrProtocol' objects (which can be a class or an
1330/// inherited protocol with the list of properties for class/category 'CDecl'
1331///
John McCalld226f652010-08-21 09:40:31 +00001332void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1333 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001334 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1335
1336 if (!IDecl) {
1337 // Category
1338 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1339 assert (CatDecl && "CompareProperties");
1340 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1341 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1342 E = MDecl->protocol_end(); P != E; ++P)
1343 // Match properties of category with those of protocol (*P)
1344 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1345
1346 // Go thru the list of protocols for this category and recursively match
1347 // their properties with those in the category.
1348 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1349 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001350 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001351 } else {
1352 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1353 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1354 E = MD->protocol_end(); P != E; ++P)
1355 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1356 }
1357 return;
1358 }
1359
1360 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001361 for (ObjCInterfaceDecl::all_protocol_iterator
1362 P = MDecl->all_referenced_protocol_begin(),
1363 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001364 // Match properties of class IDecl with those of protocol (*P).
1365 MatchOneProtocolPropertiesInClass(IDecl, *P);
1366
1367 // Go thru the list of protocols for this class and recursively match
1368 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001369 for (ObjCInterfaceDecl::all_protocol_iterator
1370 P = IDecl->all_referenced_protocol_begin(),
1371 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001372 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001373 } else {
1374 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1375 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1376 E = MD->protocol_end(); P != E; ++P)
1377 MatchOneProtocolPropertiesInClass(IDecl, *P);
1378 }
1379}
1380
1381/// isPropertyReadonly - Return true if property is readonly, by searching
1382/// for the property in the class and in its categories and implementations
1383///
1384bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1385 ObjCInterfaceDecl *IDecl) {
1386 // by far the most common case.
1387 if (!PDecl->isReadOnly())
1388 return false;
1389 // Even if property is ready only, if interface has a user defined setter,
1390 // it is not considered read only.
1391 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1392 return false;
1393
1394 // Main class has the property as 'readonly'. Must search
1395 // through the category list to see if the property's
1396 // attribute has been over-ridden to 'readwrite'.
1397 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1398 Category; Category = Category->getNextClassCategory()) {
1399 // Even if property is ready only, if a category has a user defined setter,
1400 // it is not considered read only.
1401 if (Category->getInstanceMethod(PDecl->getSetterName()))
1402 return false;
1403 ObjCPropertyDecl *P =
1404 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1405 if (P && !P->isReadOnly())
1406 return false;
1407 }
1408
1409 // Also, check for definition of a setter method in the implementation if
1410 // all else failed.
1411 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1412 if (ObjCImplementationDecl *IMD =
1413 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1414 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1415 return false;
1416 } else if (ObjCCategoryImplDecl *CIMD =
1417 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1418 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1419 return false;
1420 }
1421 }
1422 // Lastly, look through the implementation (if one is in scope).
1423 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1424 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1425 return false;
1426 // If all fails, look at the super class.
1427 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1428 return isPropertyReadonly(PDecl, SIDecl);
1429 return true;
1430}
1431
1432/// CollectImmediateProperties - This routine collects all properties in
1433/// the class and its conforming protocols; but not those it its super class.
1434void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001435 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1436 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001437 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1438 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1439 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001440 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001441 PropMap[Prop->getIdentifier()] = Prop;
1442 }
1443 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001444 for (ObjCInterfaceDecl::all_protocol_iterator
1445 PI = IDecl->all_referenced_protocol_begin(),
1446 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001447 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001448 }
1449 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1450 if (!CATDecl->IsClassExtension())
1451 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1452 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001453 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001454 PropMap[Prop->getIdentifier()] = Prop;
1455 }
1456 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001457 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001458 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001459 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001460 }
1461 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1462 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1463 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001464 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001465 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1466 // Exclude property for protocols which conform to class's super-class,
1467 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001468 if (!PropertyFromSuper ||
1469 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001470 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1471 if (!PropEntry)
1472 PropEntry = Prop;
1473 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001474 }
1475 // scan through protocol's protocols.
1476 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1477 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001478 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001479 }
1480}
1481
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001482/// CollectClassPropertyImplementations - This routine collects list of
1483/// properties to be implemented in the class. This includes, class's
1484/// and its conforming protocols' properties.
1485static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1486 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1487 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1488 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1489 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001490 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001491 PropMap[Prop->getIdentifier()] = Prop;
1492 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001493 for (ObjCInterfaceDecl::all_protocol_iterator
1494 PI = IDecl->all_referenced_protocol_begin(),
1495 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001496 CollectClassPropertyImplementations((*PI), PropMap);
1497 }
1498 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1499 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1500 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001501 ObjCPropertyDecl *Prop = *P;
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00001502 // Insert into PropMap if not there already.
1503 PropMap.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001504 }
1505 // scan through protocol's protocols.
1506 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1507 E = PDecl->protocol_end(); PI != E; ++PI)
1508 CollectClassPropertyImplementations((*PI), PropMap);
1509 }
1510}
1511
1512/// CollectSuperClassPropertyImplementations - This routine collects list of
1513/// properties to be implemented in super class(s) and also coming from their
1514/// conforming protocols.
1515static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1516 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1517 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1518 while (SDecl) {
1519 CollectClassPropertyImplementations(SDecl, PropMap);
1520 SDecl = SDecl->getSuperClass();
1521 }
1522 }
1523}
1524
Ted Kremenek9d64c152010-03-12 00:38:38 +00001525/// LookupPropertyDecl - Looks up a property in the current class and all
1526/// its protocols.
1527ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1528 IdentifierInfo *II) {
1529 if (const ObjCInterfaceDecl *IDecl =
1530 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1531 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1532 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001533 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001534 if (Prop->getIdentifier() == II)
1535 return Prop;
1536 }
1537 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001538 for (ObjCInterfaceDecl::all_protocol_iterator
1539 PI = IDecl->all_referenced_protocol_begin(),
1540 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001541 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1542 if (Prop)
1543 return Prop;
1544 }
1545 }
1546 else if (const ObjCProtocolDecl *PDecl =
1547 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1548 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1549 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001550 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001551 if (Prop->getIdentifier() == II)
1552 return Prop;
1553 }
1554 // scan through protocol's protocols.
1555 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1556 E = PDecl->protocol_end(); PI != E; ++PI) {
1557 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1558 if (Prop)
1559 return Prop;
1560 }
1561 }
1562 return 0;
1563}
1564
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001565static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1566 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001567 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001568 {
1569 llvm::raw_svector_ostream os(ivarName);
1570 os << '_' << Prop->getIdentifier()->getName();
1571 }
1572 return &Ctx.Idents.get(ivarName.str());
1573}
1574
James Dennett699c9042012-06-15 07:13:21 +00001575/// \brief Default synthesizes all properties which must be synthesized
1576/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001577void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1578 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001579
1580 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1581 CollectClassPropertyImplementations(IDecl, PropMap);
1582 if (PropMap.empty())
1583 return;
1584 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1585 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1586
1587 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1588 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1589 ObjCPropertyDecl *Prop = P->second;
1590 // If property to be implemented in the super class, ignore.
1591 if (SuperPropMap[Prop->getIdentifier()])
1592 continue;
1593 // Is there a matching propery synthesize/dynamic?
1594 if (Prop->isInvalidDecl() ||
1595 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1596 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1597 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001598 // Property may have been synthesized by user.
1599 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1600 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001601 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1602 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1603 continue;
1604 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1605 continue;
1606 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001607 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1608 // We won't auto-synthesize properties declared in protocols.
1609 Diag(IMPDecl->getLocation(),
1610 diag::warn_auto_synthesizing_protocol_property);
1611 Diag(Prop->getLocation(), diag::note_property_declare);
1612 continue;
1613 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001614
1615 // We use invalid SourceLocations for the synthesized ivars since they
1616 // aren't really synthesized at a particular location; they just exist.
1617 // Saying that they are located at the @implementation isn't really going
1618 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001619 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1620 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1621 true,
1622 /* property = */ Prop->getIdentifier(),
1623 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001624 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001625 if (PIDecl) {
1626 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001627 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001628 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001629 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001630}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001631
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001632void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001633 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001634 return;
1635 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1636 if (!IC)
1637 return;
1638 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001639 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001640 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001641}
1642
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001643void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001644 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001645 const SelectorSet &InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001646 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1647 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1648 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1649
Ted Kremenek9d64c152010-03-12 00:38:38 +00001650 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001651 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001652 if (PropMap.empty())
1653 return;
1654
1655 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1656 for (ObjCImplDecl::propimpl_iterator
1657 I = IMPDecl->propimpl_begin(),
1658 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001659 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001660
1661 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1662 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1663 ObjCPropertyDecl *Prop = P->second;
1664 // Is there a matching propery synthesize/dynamic?
1665 if (Prop->isInvalidDecl() ||
1666 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001667 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001668 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001669 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001670 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001671 isa<ObjCCategoryDecl>(CDecl) ?
1672 diag::warn_setter_getter_impl_required_in_category :
1673 diag::warn_setter_getter_impl_required)
1674 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001675 Diag(Prop->getLocation(),
1676 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001677 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001678 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001679 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001680 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1681
Ted Kremenek9d64c152010-03-12 00:38:38 +00001682 }
1683
1684 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001685 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001686 isa<ObjCCategoryDecl>(CDecl) ?
1687 diag::warn_setter_getter_impl_required_in_category :
1688 diag::warn_setter_getter_impl_required)
1689 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001690 Diag(Prop->getLocation(),
1691 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001692 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001693 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001694 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001695 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001696 }
1697 }
1698}
1699
1700void
1701Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1702 ObjCContainerDecl* IDecl) {
1703 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001704 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001705 return;
1706 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1707 E = IDecl->prop_end();
1708 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001709 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001710 ObjCMethodDecl *GetterMethod = 0;
1711 ObjCMethodDecl *SetterMethod = 0;
1712 bool LookedUpGetterSetter = false;
1713
Ted Kremenek9d64c152010-03-12 00:38:38 +00001714 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001715 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001716
John McCall265941b2011-09-13 18:31:23 +00001717 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1718 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001719 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1720 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1721 LookedUpGetterSetter = true;
1722 if (GetterMethod) {
1723 Diag(GetterMethod->getLocation(),
1724 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001725 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001726 Diag(Property->getLocation(), diag::note_property_declare);
1727 }
1728 if (SetterMethod) {
1729 Diag(SetterMethod->getLocation(),
1730 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001731 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001732 Diag(Property->getLocation(), diag::note_property_declare);
1733 }
1734 }
1735
Ted Kremenek9d64c152010-03-12 00:38:38 +00001736 // We only care about readwrite atomic property.
1737 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1738 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1739 continue;
1740 if (const ObjCPropertyImplDecl *PIDecl
1741 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1742 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1743 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001744 if (!LookedUpGetterSetter) {
1745 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1746 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1747 LookedUpGetterSetter = true;
1748 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001749 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1750 SourceLocation MethodLoc =
1751 (GetterMethod ? GetterMethod->getLocation()
1752 : SetterMethod->getLocation());
1753 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001754 << Property->getIdentifier() << (GetterMethod != 0)
1755 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001756 // fixit stuff.
1757 if (!AttributesAsWritten) {
1758 if (Property->getLParenLoc().isValid()) {
1759 // @property () ... case.
1760 SourceRange PropSourceRange(Property->getAtLoc(),
1761 Property->getLParenLoc());
1762 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1763 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1764 }
1765 else {
1766 //@property id etc.
1767 SourceLocation endLoc =
1768 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1769 endLoc = endLoc.getLocWithOffset(-1);
1770 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1771 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1772 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1773 }
1774 }
1775 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1776 // @property () ... case.
1777 SourceLocation endLoc = Property->getLParenLoc();
1778 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1779 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1780 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1781 }
1782 else
1783 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001784 Diag(Property->getLocation(), diag::note_property_declare);
1785 }
1786 }
1787 }
1788}
1789
John McCallf85e1932011-06-15 23:02:42 +00001790void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001791 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001792 return;
1793
1794 for (ObjCImplementationDecl::propimpl_iterator
1795 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001796 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001797 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1798 continue;
1799
1800 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001801 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1802 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001803 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1804 if (!method)
1805 continue;
1806 ObjCMethodFamily family = method->getMethodFamily();
1807 if (family == OMF_alloc || family == OMF_copy ||
1808 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001809 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001810 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1811 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001812 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001813 Diag(PD->getLocation(), diag::note_property_declare);
1814 }
1815 }
1816 }
1817}
1818
John McCall5de74d12010-11-10 07:01:40 +00001819/// AddPropertyAttrs - Propagates attributes from a property to the
1820/// implicitly-declared getter or setter for that property.
1821static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1822 ObjCPropertyDecl *Property) {
1823 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001824 for (Decl::attr_iterator A = Property->attr_begin(),
1825 AEnd = Property->attr_end();
1826 A != AEnd; ++A) {
1827 if (isa<DeprecatedAttr>(*A) ||
1828 isa<UnavailableAttr>(*A) ||
1829 isa<AvailabilityAttr>(*A))
1830 PropertyMethod->addAttr((*A)->clone(S.Context));
1831 }
John McCall5de74d12010-11-10 07:01:40 +00001832}
1833
Ted Kremenek9d64c152010-03-12 00:38:38 +00001834/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1835/// have the property type and issue diagnostics if they don't.
1836/// Also synthesize a getter/setter method if none exist (and update the
1837/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1838/// methods is the "right" thing to do.
1839void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001840 ObjCContainerDecl *CD,
1841 ObjCPropertyDecl *redeclaredProperty,
1842 ObjCContainerDecl *lexicalDC) {
1843
Ted Kremenek9d64c152010-03-12 00:38:38 +00001844 ObjCMethodDecl *GetterMethod, *SetterMethod;
1845
1846 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1847 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1848 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1849 property->getLocation());
1850
1851 if (SetterMethod) {
1852 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1853 property->getPropertyAttributes();
1854 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1855 Context.getCanonicalType(SetterMethod->getResultType()) !=
1856 Context.VoidTy)
1857 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1858 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001859 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001860 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1861 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001862 Diag(property->getLocation(),
1863 diag::warn_accessor_property_type_mismatch)
1864 << property->getDeclName()
1865 << SetterMethod->getSelector();
1866 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1867 }
1868 }
1869
1870 // Synthesize getter/setter methods if none exist.
1871 // Find the default getter and if one not found, add one.
1872 // FIXME: The synthesized property we set here is misleading. We almost always
1873 // synthesize these methods unless the user explicitly provided prototypes
1874 // (which is odd, but allowed). Sema should be typechecking that the
1875 // declarations jive in that situation (which it is not currently).
1876 if (!GetterMethod) {
1877 // No instance method of same name as property getter name was found.
1878 // Declare a getter method and add it to the list of methods
1879 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001880 SourceLocation Loc = redeclaredProperty ?
1881 redeclaredProperty->getLocation() :
1882 property->getLocation();
1883
1884 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1885 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001886 property->getType(), 0, CD, /*isInstance=*/true,
1887 /*isVariadic=*/false, /*isSynthesized=*/true,
1888 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001889 (property->getPropertyImplementation() ==
1890 ObjCPropertyDecl::Optional) ?
1891 ObjCMethodDecl::Optional :
1892 ObjCMethodDecl::Required);
1893 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001894
1895 AddPropertyAttrs(*this, GetterMethod, property);
1896
Ted Kremenek23173d72010-05-18 21:09:07 +00001897 // FIXME: Eventually this shouldn't be needed, as the lexical context
1898 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001899 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001900 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001901 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1902 GetterMethod->addAttr(
1903 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001904 } else
1905 // A user declared getter will be synthesize when @synthesize of
1906 // the property with the same name is seen in the @implementation
1907 GetterMethod->setSynthesized(true);
1908 property->setGetterMethodDecl(GetterMethod);
1909
1910 // Skip setter if property is read-only.
1911 if (!property->isReadOnly()) {
1912 // Find the default setter and if one not found, add one.
1913 if (!SetterMethod) {
1914 // No instance method of same name as property setter name was found.
1915 // Declare a setter method and add it to the list of methods
1916 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001917 SourceLocation Loc = redeclaredProperty ?
1918 redeclaredProperty->getLocation() :
1919 property->getLocation();
1920
1921 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001922 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001923 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001924 CD, /*isInstance=*/true, /*isVariadic=*/false,
1925 /*isSynthesized=*/true,
1926 /*isImplicitlyDeclared=*/true,
1927 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001928 (property->getPropertyImplementation() ==
1929 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001930 ObjCMethodDecl::Optional :
1931 ObjCMethodDecl::Required);
1932
Ted Kremenek9d64c152010-03-12 00:38:38 +00001933 // Invent the arguments for the setter. We don't bother making a
1934 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001935 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1936 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001937 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001938 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001939 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001940 SC_None,
1941 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001942 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001943 SetterMethod->setMethodParams(Context, Argument,
1944 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001945
1946 AddPropertyAttrs(*this, SetterMethod, property);
1947
Ted Kremenek9d64c152010-03-12 00:38:38 +00001948 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001949 // FIXME: Eventually this shouldn't be needed, as the lexical context
1950 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001951 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001952 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001953 } else
1954 // A user declared setter will be synthesize when @synthesize of
1955 // the property with the same name is seen in the @implementation
1956 SetterMethod->setSynthesized(true);
1957 property->setSetterMethodDecl(SetterMethod);
1958 }
1959 // Add any synthesized methods to the global pool. This allows us to
1960 // handle the following, which is supported by GCC (and part of the design).
1961 //
1962 // @interface Foo
1963 // @property double bar;
1964 // @end
1965 //
1966 // void thisIsUnfortunate() {
1967 // id foo;
1968 // double bar = [foo bar];
1969 // }
1970 //
1971 if (GetterMethod)
1972 AddInstanceMethodToGlobalPool(GetterMethod);
1973 if (SetterMethod)
1974 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001975
1976 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1977 if (!CurrentClass) {
1978 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1979 CurrentClass = Cat->getClassInterface();
1980 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1981 CurrentClass = Impl->getClassInterface();
1982 }
1983 if (GetterMethod)
1984 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1985 if (SetterMethod)
1986 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001987}
1988
John McCalld226f652010-08-21 09:40:31 +00001989void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001990 SourceLocation Loc,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001991 unsigned &Attributes,
1992 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001993 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001994 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001995 return;
1996
1997 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001998 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001999
David Blaikie4e4d0842012-03-11 07:00:24 +00002000 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002001 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2002 PropertyTy->isObjCRetainableType()) {
2003 // 'readonly' property with no obvious lifetime.
2004 // its life time will be determined by its backing ivar.
2005 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
2006 ObjCDeclSpec::DQ_PR_copy |
2007 ObjCDeclSpec::DQ_PR_retain |
2008 ObjCDeclSpec::DQ_PR_strong |
2009 ObjCDeclSpec::DQ_PR_weak |
2010 ObjCDeclSpec::DQ_PR_assign);
2011 if ((Attributes & rel) == 0)
2012 return;
2013 }
2014
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002015 if (propertyInPrimaryClass) {
2016 // we postpone most property diagnosis until class's implementation
2017 // because, its readonly attribute may be overridden in its class
2018 // extensions making other attributes, which make no sense, to make sense.
2019 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2020 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2021 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2022 << "readonly" << "readwrite";
2023 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002024 // readonly and readwrite/assign/retain/copy conflict.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002025 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2026 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002027 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002028 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002029 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002030 ObjCDeclSpec::DQ_PR_retain |
2031 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002032 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2033 "readwrite" :
2034 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2035 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00002036 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
2037 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00002038 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2039 "copy" : "retain";
2040
2041 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
2042 diag::err_objc_property_attr_mutually_exclusive :
2043 diag::warn_objc_property_attr_mutually_exclusive)
2044 << "readonly" << which;
2045 }
2046
2047 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00002048 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2049 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2050 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002051 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002052 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00002053 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2054 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2055 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2056 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002057 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002058 }
2059
2060 // Check for more than one of { assign, copy, retain }.
2061 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2062 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2063 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2064 << "assign" << "copy";
2065 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2066 }
2067 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2068 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2069 << "assign" << "retain";
2070 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2071 }
John McCallf85e1932011-06-15 23:02:42 +00002072 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2073 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2074 << "assign" << "strong";
2075 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2076 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002077 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002078 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2079 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2080 << "assign" << "weak";
2081 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2082 }
2083 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2084 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2085 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2086 << "unsafe_unretained" << "copy";
2087 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2088 }
2089 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2090 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2091 << "unsafe_unretained" << "retain";
2092 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2093 }
2094 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2095 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2096 << "unsafe_unretained" << "strong";
2097 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2098 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002099 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002100 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2101 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2102 << "unsafe_unretained" << "weak";
2103 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2104 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002105 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2106 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2107 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2108 << "copy" << "retain";
2109 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2110 }
John McCallf85e1932011-06-15 23:02:42 +00002111 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2112 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2113 << "copy" << "strong";
2114 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2115 }
2116 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2117 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2118 << "copy" << "weak";
2119 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2120 }
2121 }
2122 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2123 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2124 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2125 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002126 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002127 }
2128 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2129 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2130 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2131 << "strong" << "weak";
2132 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002133 }
2134
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002135 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2136 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2137 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2138 << "atomic" << "nonatomic";
2139 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2140 }
2141
Ted Kremenek9d64c152010-03-12 00:38:38 +00002142 // Warn if user supplied no assignment attribute, property is
2143 // readwrite, and this is an object type.
2144 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002145 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2146 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2147 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002148 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002149 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002150 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002151 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002152 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002153 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002154 bool isAnyClassTy =
2155 (PropertyTy->isObjCClassType() ||
2156 PropertyTy->isObjCQualifiedClassType());
2157 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2158 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002159 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002160 ;
2161 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002162 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002163 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002164 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002165
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002166 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002167 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002168 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002169 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002170 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002171
2172 // FIXME: Implement warning dependent on NSCopying being
2173 // implemented. See also:
2174 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2175 // (please trim this list while you are at it).
2176 }
2177
2178 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002179 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002180 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002181 && PropertyTy->isBlockPointerType())
2182 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian7c16d582012-06-27 20:52:46 +00002183 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002184 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2185 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2186 PropertyTy->isBlockPointerType())
2187 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002188
2189 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2190 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2191 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2192
Ted Kremenek9d64c152010-03-12 00:38:38 +00002193}