blob: 0f6ae4c7c7e44048ade785918c8504d88e7726e9 [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);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000352 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000353 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000354 }
355
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000356 // The property 'PIDecl's readonly attribute will be over-ridden
357 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000358 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000359 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000360 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000361 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
362 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000363 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
364 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000365 Diag(AtLoc, diag::warn_property_attr_mismatch);
366 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000367 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000368 DeclContext *DC = cast<DeclContext>(CCPrimary);
369 if (!ObjCPropertyDecl::findPropertyDecl(DC,
370 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000371 // Protocol is not in the primary class. Must build one for it.
372 ObjCDeclSpec ProtocolPropertyODS;
373 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
374 // and ObjCPropertyDecl::PropertyAttributeKind have identical
375 // values. Should consolidate both into one enum type.
376 ProtocolPropertyODS.
377 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
378 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000379 // Must re-establish the context from class extension to primary
380 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000381 ContextRAII SavedContext(*this, CCPrimary);
382
John McCalld226f652010-08-21 09:40:31 +0000383 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000384 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000385 PIDecl->getGetterName(),
386 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000387 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000388 MethodImplKind,
389 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000390 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000391 }
392 PIDecl->makeitReadWriteAttribute();
393 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
394 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000395 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
396 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000397 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
398 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
399 PIDecl->setSetterName(SetterSel);
400 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000401 // Tailor the diagnostics for the common case where a readwrite
402 // property is declared both in the @interface and the continuation.
403 // This is a common error where the user often intended the original
404 // declaration to be readonly.
405 unsigned diag =
406 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
407 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
408 ? diag::err_use_continuation_class_redeclaration_readwrite
409 : diag::err_use_continuation_class;
410 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000411 << CCPrimary->getDeclName();
412 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000413 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000414 }
415 *isOverridingProperty = true;
416 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000417 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000418 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
419 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000420 if (ASTMutationListener *L = Context.getASTMutationListener())
421 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000422 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000423}
424
425ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
426 ObjCContainerDecl *CDecl,
427 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000428 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000429 FieldDeclarator &FD,
430 Selector GetterSel,
431 Selector SetterSel,
432 const bool isAssign,
433 const bool isReadWrite,
434 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000435 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000436 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000437 tok::ObjCKeywordKind MethodImplKind,
438 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000439 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000440 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000441
442 // Issue a warning if property is 'assign' as default and its object, which is
443 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000444 if (getLangOpts().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000445 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000446 if (const ObjCObjectPointerType *ObjPtrTy =
447 T->getAs<ObjCObjectPointerType>()) {
448 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
449 if (IDecl)
450 if (ObjCProtocolDecl* PNSCopying =
451 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
452 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
453 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000454 }
John McCallc12c5bb2010-05-15 11:32:37 +0000455 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000456 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
457
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000458 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000459 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
460 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000461 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000462
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000463 if (ObjCPropertyDecl *prevDecl =
464 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000465 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000466 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000467 PDecl->setInvalidDecl();
468 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000469 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000470 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000471 if (lexicalDC)
472 PDecl->setLexicalDeclContext(lexicalDC);
473 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000474
475 if (T->isArrayType() || T->isFunctionType()) {
476 Diag(AtLoc, diag::err_property_type) << T;
477 PDecl->setInvalidDecl();
478 }
479
480 ProcessDeclAttributes(S, PDecl, FD.D);
481
482 // Regardless of setter/getter attribute, we save the default getter/setter
483 // selector names in anticipation of declaration of setter/getter methods.
484 PDecl->setGetterName(GetterSel);
485 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000486 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000487 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000488
Ted Kremenek28685ab2010-03-12 00:46:40 +0000489 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
490 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
491
492 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
493 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
494
495 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
496 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
497
498 if (isReadWrite)
499 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
500
501 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
502 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
503
John McCallf85e1932011-06-15 23:02:42 +0000504 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
505 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
506
507 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
508 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
509
Ted Kremenek28685ab2010-03-12 00:46:40 +0000510 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
511 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
512
John McCallf85e1932011-06-15 23:02:42 +0000513 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
514 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
515
Ted Kremenek28685ab2010-03-12 00:46:40 +0000516 if (isAssign)
517 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
518
John McCall265941b2011-09-13 18:31:23 +0000519 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000520 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
521 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000522 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000523 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000524
John McCallf85e1932011-06-15 23:02:42 +0000525 // 'unsafe_unretained' is alias for 'assign'.
526 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
527 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
528 if (isAssign)
529 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
530
Ted Kremenek28685ab2010-03-12 00:46:40 +0000531 if (MethodImplKind == tok::objc_required)
532 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
533 else if (MethodImplKind == tok::objc_optional)
534 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000535
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000536 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000537}
538
John McCallf85e1932011-06-15 23:02:42 +0000539static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
540 ObjCPropertyDecl *property,
541 ObjCIvarDecl *ivar) {
542 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
543
John McCallf85e1932011-06-15 23:02:42 +0000544 QualType ivarType = ivar->getType();
545 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000546
John McCall265941b2011-09-13 18:31:23 +0000547 // The lifetime implied by the property's attributes.
548 Qualifiers::ObjCLifetime propertyLifetime =
549 getImpliedARCOwnership(property->getPropertyAttributes(),
550 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000551
John McCall265941b2011-09-13 18:31:23 +0000552 // We're fine if they match.
553 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000554
John McCall265941b2011-09-13 18:31:23 +0000555 // These aren't valid lifetimes for object ivars; don't diagnose twice.
556 if (ivarLifetime == Qualifiers::OCL_None ||
557 ivarLifetime == Qualifiers::OCL_Autoreleasing)
558 return;
John McCallf85e1932011-06-15 23:02:42 +0000559
John McCalld64c2eb2012-08-20 23:36:59 +0000560 // If the ivar is private, and it's implicitly __unsafe_unretained
561 // becaues of its type, then pretend it was actually implicitly
562 // __strong. This is only sound because we're processing the
563 // property implementation before parsing any method bodies.
564 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
565 propertyLifetime == Qualifiers::OCL_Strong &&
566 ivar->getAccessControl() == ObjCIvarDecl::Private) {
567 SplitQualType split = ivarType.split();
568 if (split.Quals.hasObjCLifetime()) {
569 assert(ivarType->isObjCARCImplicitlyUnretainedType());
570 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
571 ivarType = S.Context.getQualifiedType(split);
572 ivar->setType(ivarType);
573 return;
574 }
575 }
576
John McCall265941b2011-09-13 18:31:23 +0000577 switch (propertyLifetime) {
578 case Qualifiers::OCL_Strong:
579 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
580 << property->getDeclName()
581 << ivar->getDeclName()
582 << ivarLifetime;
583 break;
John McCallf85e1932011-06-15 23:02:42 +0000584
John McCall265941b2011-09-13 18:31:23 +0000585 case Qualifiers::OCL_Weak:
586 S.Diag(propertyImplLoc, diag::error_weak_property)
587 << property->getDeclName()
588 << ivar->getDeclName();
589 break;
John McCallf85e1932011-06-15 23:02:42 +0000590
John McCall265941b2011-09-13 18:31:23 +0000591 case Qualifiers::OCL_ExplicitNone:
592 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
593 << property->getDeclName()
594 << ivar->getDeclName()
595 << ((property->getPropertyAttributesAsWritten()
596 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
597 break;
John McCallf85e1932011-06-15 23:02:42 +0000598
John McCall265941b2011-09-13 18:31:23 +0000599 case Qualifiers::OCL_Autoreleasing:
600 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000601
John McCall265941b2011-09-13 18:31:23 +0000602 case Qualifiers::OCL_None:
603 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000604 return;
605 }
606
607 S.Diag(property->getLocation(), diag::note_property_declare);
608}
609
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000610/// setImpliedPropertyAttributeForReadOnlyProperty -
611/// This routine evaludates life-time attributes for a 'readonly'
612/// property with no known lifetime of its own, using backing
613/// 'ivar's attribute, if any. If no backing 'ivar', property's
614/// life-time is assumed 'strong'.
615static void setImpliedPropertyAttributeForReadOnlyProperty(
616 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
617 Qualifiers::ObjCLifetime propertyLifetime =
618 getImpliedARCOwnership(property->getPropertyAttributes(),
619 property->getType());
620 if (propertyLifetime != Qualifiers::OCL_None)
621 return;
622
623 if (!ivar) {
624 // if no backing ivar, make property 'strong'.
625 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
626 return;
627 }
628 // property assumes owenership of backing ivar.
629 QualType ivarType = ivar->getType();
630 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
631 if (ivarLifetime == Qualifiers::OCL_Strong)
632 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
633 else if (ivarLifetime == Qualifiers::OCL_Weak)
634 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
635 return;
636}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000637
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000638/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
639/// attribute declared in primary class and attributes overridden in any of its
640/// class extensions.
641static void
642DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
643 ObjCPropertyDecl *property) {
644 unsigned Attributes = property->getPropertyAttributesAsWritten();
645 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
646 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
647 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000648 ObjCPropertyDecl *ClassExtProperty = 0;
649 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
650 E = CDecl->prop_end(); P != E; ++P) {
651 if ((*P)->getIdentifier() == property->getIdentifier()) {
652 ClassExtProperty = *P;
653 break;
654 }
655 }
656 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000657 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000658 unsigned classExtPropertyAttr =
659 ClassExtProperty->getPropertyAttributesAsWritten();
660 // We are issuing the warning that we postponed because class extensions
661 // can override readonly->readwrite and 'setter' attributes originally
662 // placed on class's property declaration now make sense in the overridden
663 // property.
664 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
665 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000666 (classExtPropertyAttr &
667 (ObjCDeclSpec::DQ_PR_readwrite|
668 ObjCDeclSpec::DQ_PR_assign |
669 ObjCDeclSpec::DQ_PR_unsafe_unretained |
670 ObjCDeclSpec::DQ_PR_copy |
671 ObjCDeclSpec::DQ_PR_retain |
672 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000673 continue;
674 warn = true;
675 break;
676 }
677 }
678 }
679 if (warn) {
680 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
681 ObjCDeclSpec::DQ_PR_unsafe_unretained |
682 ObjCDeclSpec::DQ_PR_copy |
683 ObjCDeclSpec::DQ_PR_retain |
684 ObjCDeclSpec::DQ_PR_strong);
685 if (Attributes & setterAttrs) {
686 const char * which =
687 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
688 "assign" :
689 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
690 "unsafe_unretained" :
691 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
692 "copy" :
693 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
694 "retain" : "strong";
695
696 S.Diag(property->getLocation(),
697 diag::warn_objc_property_attr_mutually_exclusive)
698 << "readonly" << which;
699 }
700 }
701
702
703}
704
Ted Kremenek28685ab2010-03-12 00:46:40 +0000705/// ActOnPropertyImplDecl - This routine performs semantic checks and
706/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000707/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000708///
John McCalld226f652010-08-21 09:40:31 +0000709Decl *Sema::ActOnPropertyImplDecl(Scope *S,
710 SourceLocation AtLoc,
711 SourceLocation PropertyLoc,
712 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000713 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000714 IdentifierInfo *PropertyIvar,
715 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000716 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000717 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000718 // Make sure we have a context for the property implementation declaration.
719 if (!ClassImpDecl) {
720 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000721 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000722 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000723 if (PropertyIvarLoc.isInvalid())
724 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000725 SourceLocation PropertyDiagLoc = PropertyLoc;
726 if (PropertyDiagLoc.isInvalid())
727 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000728 ObjCPropertyDecl *property = 0;
729 ObjCInterfaceDecl* IDecl = 0;
730 // Find the class or category class where this property must have
731 // a declaration.
732 ObjCImplementationDecl *IC = 0;
733 ObjCCategoryImplDecl* CatImplClass = 0;
734 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
735 IDecl = IC->getClassInterface();
736 // We always synthesize an interface for an implementation
737 // without an interface decl. So, IDecl is always non-zero.
738 assert(IDecl &&
739 "ActOnPropertyImplDecl - @implementation without @interface");
740
741 // Look for this property declaration in the @implementation's @interface
742 property = IDecl->FindPropertyDeclaration(PropertyId);
743 if (!property) {
744 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000745 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000746 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000747 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000748 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
749 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000750 if (AtLoc.isValid())
751 Diag(AtLoc, diag::warn_implicit_atomic_property);
752 else
753 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
754 Diag(property->getLocation(), diag::note_property_declare);
755 }
756
Ted Kremenek28685ab2010-03-12 00:46:40 +0000757 if (const ObjCCategoryDecl *CD =
758 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
759 if (!CD->IsClassExtension()) {
760 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
761 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000762 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000763 }
764 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000765
766 if (Synthesize&&
767 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
768 property->hasAttr<IBOutletAttr>() &&
769 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000770 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
771 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000772 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000773 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000774 property->getLParenLoc(), readonlyLoc)) {
775 SourceLocation endLoc =
776 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
777 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
778 Diag(property->getLocation(),
779 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
780 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
781 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000782 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000783
784 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000785
Ted Kremenek28685ab2010-03-12 00:46:40 +0000786 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
787 if (Synthesize) {
788 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000789 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000790 }
791 IDecl = CatImplClass->getClassInterface();
792 if (!IDecl) {
793 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000794 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000795 }
796 ObjCCategoryDecl *Category =
797 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
798
799 // If category for this implementation not found, it is an error which
800 // has already been reported eralier.
801 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000802 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000803 // Look for this property declaration in @implementation's category
804 property = Category->FindPropertyDeclaration(PropertyId);
805 if (!property) {
806 Diag(PropertyLoc, diag::error_bad_category_property_decl)
807 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000808 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000809 }
810 } else {
811 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000812 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000813 }
814 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000815 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000816 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000817 // Check that we have a valid, previously declared ivar for @synthesize
818 if (Synthesize) {
819 // @synthesize
820 if (!PropertyIvar)
821 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000822 // Check that this is a previously declared 'ivar' in 'IDecl' interface
823 ObjCInterfaceDecl *ClassDeclared;
824 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
825 QualType PropType = property->getType();
826 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000827
828 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000829 diag::err_incomplete_synthesized_property,
830 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000831 Diag(property->getLocation(), diag::note_property_declare);
832 CompleteTypeErr = true;
833 }
834
David Blaikie4e4d0842012-03-11 07:00:24 +0000835 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000836 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000837 ObjCPropertyDecl::OBJC_PR_readonly) &&
838 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000839 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
840 }
841
John McCallf85e1932011-06-15 23:02:42 +0000842 ObjCPropertyDecl::PropertyAttributeKind kind
843 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000844
845 // Add GC __weak to the ivar type if the property is weak.
846 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000847 getLangOpts().getGC() != LangOptions::NonGC) {
848 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000849 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000850 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000851 Diag(property->getLocation(), diag::note_property_declare);
852 } else {
853 PropertyIvarType =
854 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000855 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000856 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000857 if (AtLoc.isInvalid()) {
858 // Check when default synthesizing a property that there is
859 // an ivar matching property name and issue warning; since this
860 // is the most common case of not using an ivar used for backing
861 // property in non-default synthesis case.
862 ObjCInterfaceDecl *ClassDeclared=0;
863 ObjCIvarDecl *originalIvar =
864 IDecl->lookupInstanceVariable(property->getIdentifier(),
865 ClassDeclared);
866 if (originalIvar) {
867 Diag(PropertyDiagLoc,
868 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000869 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000870 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000871 Diag(property->getLocation(), diag::note_property_declare);
872 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000873 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000874 }
875
876 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000877 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000878 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000879 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000880 !PropertyIvarType.getObjCLifetime() &&
881 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000882
John McCall265941b2011-09-13 18:31:23 +0000883 // It's an error if we have to do this and the user didn't
884 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000885 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000886 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000887 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000888 diag::err_arc_objc_property_default_assign_on_object);
889 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000890 } else {
891 Qualifiers::ObjCLifetime lifetime =
892 getImpliedARCOwnership(kind, PropertyIvarType);
893 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000894 if (lifetime == Qualifiers::OCL_Weak) {
895 bool err = false;
896 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000897 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
898 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
899 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000900 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000901 Diag(property->getLocation(), diag::note_property_declare);
902 err = true;
903 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000904 }
John McCall0a7dd782012-08-21 02:47:43 +0000905 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000906 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000907 Diag(property->getLocation(), diag::note_property_declare);
908 }
John McCallf85e1932011-06-15 23:02:42 +0000909 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000910
John McCallf85e1932011-06-15 23:02:42 +0000911 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000912 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000913 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
914 }
John McCallf85e1932011-06-15 23:02:42 +0000915 }
916
917 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000918 !getLangOpts().ObjCAutoRefCount &&
919 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000920 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000921 Diag(property->getLocation(), diag::note_property_declare);
922 }
923
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000924 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000925 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000926 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000927 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000928 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000929 if (CompleteTypeErr)
930 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000931 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000932 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000933 property->setPropertyIvarDecl(Ivar);
934
John McCall260611a2012-06-20 06:18:46 +0000935 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000936 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
937 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000938 // Note! I deliberately want it to fall thru so, we have a
939 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000940 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000941 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000942 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000943 << property->getDeclName() << Ivar->getDeclName()
944 << ClassDeclared->getDeclName();
945 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000946 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000947 // Note! I deliberately want it to fall thru so more errors are caught.
948 }
949 QualType IvarType = Context.getCanonicalType(Ivar->getType());
950
951 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000952 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +0000953 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000954 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +0000955 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000956 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000957 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000958 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000959 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000960 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
961 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000962 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000963 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000964 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000965 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000966 << property->getDeclName() << PropType
967 << Ivar->getDeclName() << IvarType;
968 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000969 // Note! I deliberately want it to fall thru so, we have a
970 // a property implementation and to avoid future warnings.
971 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000972 else {
973 // FIXME! Rules for properties are somewhat different that those
974 // for assignments. Use a new routine to consolidate all cases;
975 // specifically for property redeclarations as well as for ivars.
976 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
977 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
978 if (lhsType != rhsType &&
979 lhsType->isArithmeticType()) {
980 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
981 << property->getDeclName() << PropType
982 << Ivar->getDeclName() << IvarType;
983 Diag(Ivar->getLocation(), diag::note_ivar_decl);
984 // Fall thru - see previous comment
985 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000986 }
987 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000988 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000989 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000990 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000991 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000992 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000993 // Fall thru - see previous comment
994 }
John McCallf85e1932011-06-15 23:02:42 +0000995 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000996 if ((property->getType()->isObjCObjectPointerType() ||
997 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000998 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000999 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001000 << property->getDeclName() << Ivar->getDeclName();
1001 // Fall thru - see previous comment
1002 }
1003 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001004 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001005 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001006 } else if (PropertyIvar)
1007 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001008 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001009
Ted Kremenek28685ab2010-03-12 00:46:40 +00001010 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1011 ObjCPropertyImplDecl *PIDecl =
1012 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1013 property,
1014 (Synthesize ?
1015 ObjCPropertyImplDecl::Synthesize
1016 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001017 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001018
Fariborz Jahanian74414712012-05-15 18:12:51 +00001019 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001020 PIDecl->setInvalidDecl();
1021
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001022 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1023 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001024 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001025 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001026 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1027 // returned by the getter as it must conform to C++'s copy-return rules.
1028 // FIXME. Eventually we want to do this for Objective-C as well.
1029 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1030 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001031 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001032 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001033 Expr *IvarRefExpr =
1034 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1035 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001036 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001037 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001038 SourceLocation(),
1039 getterMethod->getResultType(),
1040 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001041 SourceLocation(),
1042 Owned(IvarRefExpr));
1043 if (!Res.isInvalid()) {
1044 Expr *ResExpr = Res.takeAs<Expr>();
1045 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001046 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001047 PIDecl->setGetterCXXConstructor(ResExpr);
1048 }
1049 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001050 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1051 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1052 Diag(getterMethod->getLocation(),
1053 diag::warn_property_getter_owning_mismatch);
1054 Diag(property->getLocation(), diag::note_property_declare);
1055 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001056 }
1057 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1058 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001059 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1060 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001061 // FIXME. Eventually we want to do this for Objective-C as well.
1062 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1063 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001064 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001065 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001066 Expr *lhs =
1067 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1068 SelfExpr, true, true);
1069 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1070 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001071 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +00001072 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +00001073 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +00001074 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +00001075 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001076 if (property->getPropertyAttributes() &
1077 ObjCPropertyDecl::OBJC_PR_atomic) {
1078 Expr *callExpr = Res.takeAs<Expr>();
1079 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001080 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1081 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001082 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001083 if (property->getType()->isReferenceType()) {
1084 Diag(PropertyLoc,
1085 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001086 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001087 Diag(FuncDecl->getLocStart(),
1088 diag::note_callee_decl) << FuncDecl;
1089 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001090 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001091 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1092 }
1093 }
1094
Ted Kremenek28685ab2010-03-12 00:46:40 +00001095 if (IC) {
1096 if (Synthesize)
1097 if (ObjCPropertyImplDecl *PPIDecl =
1098 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1099 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1100 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1101 << PropertyIvar;
1102 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1103 }
1104
1105 if (ObjCPropertyImplDecl *PPIDecl
1106 = IC->FindPropertyImplDecl(PropertyId)) {
1107 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1108 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001109 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001110 }
1111 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001112 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001113 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001114 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001115 // Diagnose if an ivar was lazily synthesdized due to a previous
1116 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001117 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001118 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001119 ObjCIvarDecl *Ivar = 0;
1120 if (!Synthesize)
1121 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1122 else {
1123 if (PropertyIvar && PropertyIvar != PropertyId)
1124 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1125 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001126 // Issue diagnostics only if Ivar belongs to current class.
1127 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001128 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001129 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1130 << PropertyId;
1131 Ivar->setInvalidDecl();
1132 }
1133 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001134 } else {
1135 if (Synthesize)
1136 if (ObjCPropertyImplDecl *PPIDecl =
1137 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001138 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001139 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1140 << PropertyIvar;
1141 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1142 }
1143
1144 if (ObjCPropertyImplDecl *PPIDecl =
1145 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001146 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001147 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001148 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001149 }
1150 CatImplClass->addPropertyImplementation(PIDecl);
1151 }
1152
John McCalld226f652010-08-21 09:40:31 +00001153 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001154}
1155
1156//===----------------------------------------------------------------------===//
1157// Helper methods.
1158//===----------------------------------------------------------------------===//
1159
Ted Kremenek9d64c152010-03-12 00:38:38 +00001160/// DiagnosePropertyMismatch - Compares two properties for their
1161/// attributes and types and warns on a variety of inconsistencies.
1162///
1163void
1164Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1165 ObjCPropertyDecl *SuperProperty,
1166 const IdentifierInfo *inheritedName) {
1167 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1168 Property->getPropertyAttributes();
1169 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1170 SuperProperty->getPropertyAttributes();
1171 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1172 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1173 Diag(Property->getLocation(), diag::warn_readonly_property)
1174 << Property->getDeclName() << inheritedName;
1175 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1176 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1177 Diag(Property->getLocation(), diag::warn_property_attribute)
1178 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001179 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001180 unsigned CAttrRetain =
1181 (CAttr &
1182 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1183 unsigned SAttrRetain =
1184 (SAttr &
1185 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1186 bool CStrong = (CAttrRetain != 0);
1187 bool SStrong = (SAttrRetain != 0);
1188 if (CStrong != SStrong)
1189 Diag(Property->getLocation(), diag::warn_property_attribute)
1190 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1191 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001192
1193 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1194 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1195 Diag(Property->getLocation(), diag::warn_property_attribute)
1196 << Property->getDeclName() << "atomic" << inheritedName;
1197 if (Property->getSetterName() != SuperProperty->getSetterName())
1198 Diag(Property->getLocation(), diag::warn_property_attribute)
1199 << Property->getDeclName() << "setter" << inheritedName;
1200 if (Property->getGetterName() != SuperProperty->getGetterName())
1201 Diag(Property->getLocation(), diag::warn_property_attribute)
1202 << Property->getDeclName() << "getter" << inheritedName;
1203
1204 QualType LHSType =
1205 Context.getCanonicalType(SuperProperty->getType());
1206 QualType RHSType =
1207 Context.getCanonicalType(Property->getType());
1208
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001209 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001210 // Do cases not handled in above.
1211 // FIXME. For future support of covariant property types, revisit this.
1212 bool IncompatibleObjC = false;
1213 QualType ConvertedType;
1214 if (!isObjCPointerConversion(RHSType, LHSType,
1215 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001216 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001217 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1218 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001219 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1220 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001221 }
1222}
1223
1224bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1225 ObjCMethodDecl *GetterMethod,
1226 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001227 if (!GetterMethod)
1228 return false;
1229 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1230 QualType PropertyIvarType = property->getType().getNonReferenceType();
1231 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1232 if (!compat) {
1233 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1234 isa<ObjCObjectPointerType>(GetterType))
1235 compat =
1236 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001237 GetterType->getAs<ObjCObjectPointerType>(),
1238 PropertyIvarType->getAs<ObjCObjectPointerType>());
1239 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001240 != Compatible) {
1241 Diag(Loc, diag::error_property_accessor_type)
1242 << property->getDeclName() << PropertyIvarType
1243 << GetterMethod->getSelector() << GetterType;
1244 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1245 return true;
1246 } else {
1247 compat = true;
1248 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1249 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1250 if (lhsType != rhsType && lhsType->isArithmeticType())
1251 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001252 }
1253 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001254
1255 if (!compat) {
1256 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1257 << property->getDeclName()
1258 << GetterMethod->getSelector();
1259 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1260 return true;
1261 }
1262
Ted Kremenek9d64c152010-03-12 00:38:38 +00001263 return false;
1264}
1265
1266/// ComparePropertiesInBaseAndSuper - This routine compares property
1267/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001268/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001269///
1270void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1271 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1272 if (!SDecl)
1273 return;
1274 // FIXME: O(N^2)
1275 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1276 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001277 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001278 // Does property in super class has declaration in current class?
1279 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1280 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001281 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001282 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1283 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1284 SDecl->getIdentifier());
1285 }
1286 }
1287}
1288
1289/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1290/// of properties declared in a protocol and compares their attribute against
1291/// the same property declared in the class or category.
1292void
1293Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1294 ObjCProtocolDecl *PDecl) {
1295 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1296 if (!IDecl) {
1297 // Category
1298 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1299 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1300 if (!CatDecl->IsClassExtension())
1301 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1302 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001303 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001304 ObjCCategoryDecl::prop_iterator CP, CE;
1305 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001306 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001307 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001308 break;
1309 if (CP != CE)
1310 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001311 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001312 }
1313 return;
1314 }
1315 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1316 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001317 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001318 ObjCInterfaceDecl::prop_iterator CP, CE;
1319 // Is this property already in class's list of properties?
1320 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001321 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001322 break;
1323 if (CP != CE)
1324 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001325 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001326 }
1327}
1328
1329/// CompareProperties - This routine compares properties
1330/// declared in 'ClassOrProtocol' objects (which can be a class or an
1331/// inherited protocol with the list of properties for class/category 'CDecl'
1332///
John McCalld226f652010-08-21 09:40:31 +00001333void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1334 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001335 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1336
1337 if (!IDecl) {
1338 // Category
1339 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1340 assert (CatDecl && "CompareProperties");
1341 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1342 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1343 E = MDecl->protocol_end(); P != E; ++P)
1344 // Match properties of category with those of protocol (*P)
1345 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1346
1347 // Go thru the list of protocols for this category and recursively match
1348 // their properties with those in the category.
1349 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1350 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001351 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001352 } else {
1353 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1354 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1355 E = MD->protocol_end(); P != E; ++P)
1356 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1357 }
1358 return;
1359 }
1360
1361 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001362 for (ObjCInterfaceDecl::all_protocol_iterator
1363 P = MDecl->all_referenced_protocol_begin(),
1364 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001365 // Match properties of class IDecl with those of protocol (*P).
1366 MatchOneProtocolPropertiesInClass(IDecl, *P);
1367
1368 // Go thru the list of protocols for this class and recursively match
1369 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001370 for (ObjCInterfaceDecl::all_protocol_iterator
1371 P = IDecl->all_referenced_protocol_begin(),
1372 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001373 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001374 } else {
1375 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1376 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1377 E = MD->protocol_end(); P != E; ++P)
1378 MatchOneProtocolPropertiesInClass(IDecl, *P);
1379 }
1380}
1381
1382/// isPropertyReadonly - Return true if property is readonly, by searching
1383/// for the property in the class and in its categories and implementations
1384///
1385bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1386 ObjCInterfaceDecl *IDecl) {
1387 // by far the most common case.
1388 if (!PDecl->isReadOnly())
1389 return false;
1390 // Even if property is ready only, if interface has a user defined setter,
1391 // it is not considered read only.
1392 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1393 return false;
1394
1395 // Main class has the property as 'readonly'. Must search
1396 // through the category list to see if the property's
1397 // attribute has been over-ridden to 'readwrite'.
1398 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1399 Category; Category = Category->getNextClassCategory()) {
1400 // Even if property is ready only, if a category has a user defined setter,
1401 // it is not considered read only.
1402 if (Category->getInstanceMethod(PDecl->getSetterName()))
1403 return false;
1404 ObjCPropertyDecl *P =
1405 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1406 if (P && !P->isReadOnly())
1407 return false;
1408 }
1409
1410 // Also, check for definition of a setter method in the implementation if
1411 // all else failed.
1412 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1413 if (ObjCImplementationDecl *IMD =
1414 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1415 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1416 return false;
1417 } else if (ObjCCategoryImplDecl *CIMD =
1418 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1419 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1420 return false;
1421 }
1422 }
1423 // Lastly, look through the implementation (if one is in scope).
1424 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1425 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1426 return false;
1427 // If all fails, look at the super class.
1428 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1429 return isPropertyReadonly(PDecl, SIDecl);
1430 return true;
1431}
1432
1433/// CollectImmediateProperties - This routine collects all properties in
1434/// the class and its conforming protocols; but not those it its super class.
1435void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001436 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1437 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001438 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1439 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1440 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001441 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001442 PropMap[Prop->getIdentifier()] = Prop;
1443 }
1444 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001445 for (ObjCInterfaceDecl::all_protocol_iterator
1446 PI = IDecl->all_referenced_protocol_begin(),
1447 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001448 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001449 }
1450 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1451 if (!CATDecl->IsClassExtension())
1452 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1453 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001454 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001455 PropMap[Prop->getIdentifier()] = Prop;
1456 }
1457 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001458 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001459 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001460 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001461 }
1462 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1463 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1464 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001465 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001466 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1467 // Exclude property for protocols which conform to class's super-class,
1468 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001469 if (!PropertyFromSuper ||
1470 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001471 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1472 if (!PropEntry)
1473 PropEntry = Prop;
1474 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001475 }
1476 // scan through protocol's protocols.
1477 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1478 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001479 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001480 }
1481}
1482
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001483/// CollectClassPropertyImplementations - This routine collects list of
1484/// properties to be implemented in the class. This includes, class's
1485/// and its conforming protocols' properties.
1486static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1487 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1488 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1489 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1490 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001491 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001492 PropMap[Prop->getIdentifier()] = Prop;
1493 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001494 for (ObjCInterfaceDecl::all_protocol_iterator
1495 PI = IDecl->all_referenced_protocol_begin(),
1496 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001497 CollectClassPropertyImplementations((*PI), PropMap);
1498 }
1499 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1500 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1501 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001502 ObjCPropertyDecl *Prop = *P;
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00001503 // Insert into PropMap if not there already.
1504 PropMap.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001505 }
1506 // scan through protocol's protocols.
1507 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1508 E = PDecl->protocol_end(); PI != E; ++PI)
1509 CollectClassPropertyImplementations((*PI), PropMap);
1510 }
1511}
1512
1513/// CollectSuperClassPropertyImplementations - This routine collects list of
1514/// properties to be implemented in super class(s) and also coming from their
1515/// conforming protocols.
1516static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1517 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1518 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1519 while (SDecl) {
1520 CollectClassPropertyImplementations(SDecl, PropMap);
1521 SDecl = SDecl->getSuperClass();
1522 }
1523 }
1524}
1525
Ted Kremenek9d64c152010-03-12 00:38:38 +00001526/// LookupPropertyDecl - Looks up a property in the current class and all
1527/// its protocols.
1528ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1529 IdentifierInfo *II) {
1530 if (const ObjCInterfaceDecl *IDecl =
1531 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1532 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1533 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001534 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001535 if (Prop->getIdentifier() == II)
1536 return Prop;
1537 }
1538 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001539 for (ObjCInterfaceDecl::all_protocol_iterator
1540 PI = IDecl->all_referenced_protocol_begin(),
1541 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001542 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1543 if (Prop)
1544 return Prop;
1545 }
1546 }
1547 else if (const ObjCProtocolDecl *PDecl =
1548 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1549 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1550 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001551 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001552 if (Prop->getIdentifier() == II)
1553 return Prop;
1554 }
1555 // scan through protocol's protocols.
1556 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1557 E = PDecl->protocol_end(); PI != E; ++PI) {
1558 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1559 if (Prop)
1560 return Prop;
1561 }
1562 }
1563 return 0;
1564}
1565
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001566static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1567 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001568 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001569 {
1570 llvm::raw_svector_ostream os(ivarName);
1571 os << '_' << Prop->getIdentifier()->getName();
1572 }
1573 return &Ctx.Idents.get(ivarName.str());
1574}
1575
James Dennett699c9042012-06-15 07:13:21 +00001576/// \brief Default synthesizes all properties which must be synthesized
1577/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001578void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1579 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001580
1581 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1582 CollectClassPropertyImplementations(IDecl, PropMap);
1583 if (PropMap.empty())
1584 return;
1585 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1586 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1587
1588 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1589 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1590 ObjCPropertyDecl *Prop = P->second;
1591 // If property to be implemented in the super class, ignore.
1592 if (SuperPropMap[Prop->getIdentifier()])
1593 continue;
1594 // Is there a matching propery synthesize/dynamic?
1595 if (Prop->isInvalidDecl() ||
1596 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1597 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1598 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001599 // Property may have been synthesized by user.
1600 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1601 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001602 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1603 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1604 continue;
1605 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1606 continue;
1607 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001608 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1609 // We won't auto-synthesize properties declared in protocols.
1610 Diag(IMPDecl->getLocation(),
1611 diag::warn_auto_synthesizing_protocol_property);
1612 Diag(Prop->getLocation(), diag::note_property_declare);
1613 continue;
1614 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001615
1616 // We use invalid SourceLocations for the synthesized ivars since they
1617 // aren't really synthesized at a particular location; they just exist.
1618 // Saying that they are located at the @implementation isn't really going
1619 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001620 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1621 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1622 true,
1623 /* property = */ Prop->getIdentifier(),
1624 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001625 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001626 if (PIDecl) {
1627 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001628 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001629 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001630 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001631}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001632
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001633void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001634 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001635 return;
1636 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1637 if (!IC)
1638 return;
1639 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001640 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001641 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001642}
1643
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001644void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001645 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001646 const SelectorSet &InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001647 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1648 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1649 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1650
Ted Kremenek9d64c152010-03-12 00:38:38 +00001651 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001652 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001653 if (PropMap.empty())
1654 return;
1655
1656 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1657 for (ObjCImplDecl::propimpl_iterator
1658 I = IMPDecl->propimpl_begin(),
1659 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001660 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001661
1662 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1663 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1664 ObjCPropertyDecl *Prop = P->second;
1665 // Is there a matching propery synthesize/dynamic?
1666 if (Prop->isInvalidDecl() ||
1667 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001668 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001669 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001670 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001671 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001672 isa<ObjCCategoryDecl>(CDecl) ?
1673 diag::warn_setter_getter_impl_required_in_category :
1674 diag::warn_setter_getter_impl_required)
1675 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001676 Diag(Prop->getLocation(),
1677 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001678 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001679 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001680 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001681 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1682
Ted Kremenek9d64c152010-03-12 00:38:38 +00001683 }
1684
1685 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001686 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001687 isa<ObjCCategoryDecl>(CDecl) ?
1688 diag::warn_setter_getter_impl_required_in_category :
1689 diag::warn_setter_getter_impl_required)
1690 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001691 Diag(Prop->getLocation(),
1692 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001693 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001694 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001695 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001696 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001697 }
1698 }
1699}
1700
1701void
1702Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1703 ObjCContainerDecl* IDecl) {
1704 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001705 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001706 return;
1707 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1708 E = IDecl->prop_end();
1709 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001710 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001711 ObjCMethodDecl *GetterMethod = 0;
1712 ObjCMethodDecl *SetterMethod = 0;
1713 bool LookedUpGetterSetter = false;
1714
Ted Kremenek9d64c152010-03-12 00:38:38 +00001715 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001716 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001717
John McCall265941b2011-09-13 18:31:23 +00001718 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1719 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001720 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1721 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1722 LookedUpGetterSetter = true;
1723 if (GetterMethod) {
1724 Diag(GetterMethod->getLocation(),
1725 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001726 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001727 Diag(Property->getLocation(), diag::note_property_declare);
1728 }
1729 if (SetterMethod) {
1730 Diag(SetterMethod->getLocation(),
1731 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001732 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001733 Diag(Property->getLocation(), diag::note_property_declare);
1734 }
1735 }
1736
Ted Kremenek9d64c152010-03-12 00:38:38 +00001737 // We only care about readwrite atomic property.
1738 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1739 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1740 continue;
1741 if (const ObjCPropertyImplDecl *PIDecl
1742 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1743 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1744 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001745 if (!LookedUpGetterSetter) {
1746 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1747 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1748 LookedUpGetterSetter = true;
1749 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001750 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1751 SourceLocation MethodLoc =
1752 (GetterMethod ? GetterMethod->getLocation()
1753 : SetterMethod->getLocation());
1754 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001755 << Property->getIdentifier() << (GetterMethod != 0)
1756 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001757 // fixit stuff.
1758 if (!AttributesAsWritten) {
1759 if (Property->getLParenLoc().isValid()) {
1760 // @property () ... case.
1761 SourceRange PropSourceRange(Property->getAtLoc(),
1762 Property->getLParenLoc());
1763 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1764 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1765 }
1766 else {
1767 //@property id etc.
1768 SourceLocation endLoc =
1769 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1770 endLoc = endLoc.getLocWithOffset(-1);
1771 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1772 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1773 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1774 }
1775 }
1776 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1777 // @property () ... case.
1778 SourceLocation endLoc = Property->getLParenLoc();
1779 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1780 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1781 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1782 }
1783 else
1784 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001785 Diag(Property->getLocation(), diag::note_property_declare);
1786 }
1787 }
1788 }
1789}
1790
John McCallf85e1932011-06-15 23:02:42 +00001791void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001792 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001793 return;
1794
1795 for (ObjCImplementationDecl::propimpl_iterator
1796 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001797 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001798 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1799 continue;
1800
1801 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001802 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1803 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001804 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1805 if (!method)
1806 continue;
1807 ObjCMethodFamily family = method->getMethodFamily();
1808 if (family == OMF_alloc || family == OMF_copy ||
1809 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001810 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001811 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1812 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001813 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001814 Diag(PD->getLocation(), diag::note_property_declare);
1815 }
1816 }
1817 }
1818}
1819
John McCall5de74d12010-11-10 07:01:40 +00001820/// AddPropertyAttrs - Propagates attributes from a property to the
1821/// implicitly-declared getter or setter for that property.
1822static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1823 ObjCPropertyDecl *Property) {
1824 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001825 for (Decl::attr_iterator A = Property->attr_begin(),
1826 AEnd = Property->attr_end();
1827 A != AEnd; ++A) {
1828 if (isa<DeprecatedAttr>(*A) ||
1829 isa<UnavailableAttr>(*A) ||
1830 isa<AvailabilityAttr>(*A))
1831 PropertyMethod->addAttr((*A)->clone(S.Context));
1832 }
John McCall5de74d12010-11-10 07:01:40 +00001833}
1834
Ted Kremenek9d64c152010-03-12 00:38:38 +00001835/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1836/// have the property type and issue diagnostics if they don't.
1837/// Also synthesize a getter/setter method if none exist (and update the
1838/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1839/// methods is the "right" thing to do.
1840void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001841 ObjCContainerDecl *CD,
1842 ObjCPropertyDecl *redeclaredProperty,
1843 ObjCContainerDecl *lexicalDC) {
1844
Ted Kremenek9d64c152010-03-12 00:38:38 +00001845 ObjCMethodDecl *GetterMethod, *SetterMethod;
1846
1847 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1848 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1849 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1850 property->getLocation());
1851
1852 if (SetterMethod) {
1853 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1854 property->getPropertyAttributes();
1855 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1856 Context.getCanonicalType(SetterMethod->getResultType()) !=
1857 Context.VoidTy)
1858 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1859 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001860 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001861 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1862 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001863 Diag(property->getLocation(),
1864 diag::warn_accessor_property_type_mismatch)
1865 << property->getDeclName()
1866 << SetterMethod->getSelector();
1867 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1868 }
1869 }
1870
1871 // Synthesize getter/setter methods if none exist.
1872 // Find the default getter and if one not found, add one.
1873 // FIXME: The synthesized property we set here is misleading. We almost always
1874 // synthesize these methods unless the user explicitly provided prototypes
1875 // (which is odd, but allowed). Sema should be typechecking that the
1876 // declarations jive in that situation (which it is not currently).
1877 if (!GetterMethod) {
1878 // No instance method of same name as property getter name was found.
1879 // Declare a getter method and add it to the list of methods
1880 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001881 SourceLocation Loc = redeclaredProperty ?
1882 redeclaredProperty->getLocation() :
1883 property->getLocation();
1884
1885 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1886 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001887 property->getType(), 0, CD, /*isInstance=*/true,
1888 /*isVariadic=*/false, /*isSynthesized=*/true,
1889 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001890 (property->getPropertyImplementation() ==
1891 ObjCPropertyDecl::Optional) ?
1892 ObjCMethodDecl::Optional :
1893 ObjCMethodDecl::Required);
1894 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001895
1896 AddPropertyAttrs(*this, GetterMethod, property);
1897
Ted Kremenek23173d72010-05-18 21:09:07 +00001898 // FIXME: Eventually this shouldn't be needed, as the lexical context
1899 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001900 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001901 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001902 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1903 GetterMethod->addAttr(
1904 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001905 } else
1906 // A user declared getter will be synthesize when @synthesize of
1907 // the property with the same name is seen in the @implementation
1908 GetterMethod->setSynthesized(true);
1909 property->setGetterMethodDecl(GetterMethod);
1910
1911 // Skip setter if property is read-only.
1912 if (!property->isReadOnly()) {
1913 // Find the default setter and if one not found, add one.
1914 if (!SetterMethod) {
1915 // No instance method of same name as property setter name was found.
1916 // Declare a setter method and add it to the list of methods
1917 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001918 SourceLocation Loc = redeclaredProperty ?
1919 redeclaredProperty->getLocation() :
1920 property->getLocation();
1921
1922 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001923 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001924 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001925 CD, /*isInstance=*/true, /*isVariadic=*/false,
1926 /*isSynthesized=*/true,
1927 /*isImplicitlyDeclared=*/true,
1928 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001929 (property->getPropertyImplementation() ==
1930 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001931 ObjCMethodDecl::Optional :
1932 ObjCMethodDecl::Required);
1933
Ted Kremenek9d64c152010-03-12 00:38:38 +00001934 // Invent the arguments for the setter. We don't bother making a
1935 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001936 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1937 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001938 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001939 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001940 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001941 SC_None,
1942 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001943 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001944 SetterMethod->setMethodParams(Context, Argument,
1945 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001946
1947 AddPropertyAttrs(*this, SetterMethod, property);
1948
Ted Kremenek9d64c152010-03-12 00:38:38 +00001949 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001950 // FIXME: Eventually this shouldn't be needed, as the lexical context
1951 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001952 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001953 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001954 } else
1955 // A user declared setter will be synthesize when @synthesize of
1956 // the property with the same name is seen in the @implementation
1957 SetterMethod->setSynthesized(true);
1958 property->setSetterMethodDecl(SetterMethod);
1959 }
1960 // Add any synthesized methods to the global pool. This allows us to
1961 // handle the following, which is supported by GCC (and part of the design).
1962 //
1963 // @interface Foo
1964 // @property double bar;
1965 // @end
1966 //
1967 // void thisIsUnfortunate() {
1968 // id foo;
1969 // double bar = [foo bar];
1970 // }
1971 //
1972 if (GetterMethod)
1973 AddInstanceMethodToGlobalPool(GetterMethod);
1974 if (SetterMethod)
1975 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001976
1977 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1978 if (!CurrentClass) {
1979 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1980 CurrentClass = Cat->getClassInterface();
1981 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1982 CurrentClass = Impl->getClassInterface();
1983 }
1984 if (GetterMethod)
1985 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1986 if (SetterMethod)
1987 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001988}
1989
John McCalld226f652010-08-21 09:40:31 +00001990void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001991 SourceLocation Loc,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001992 unsigned &Attributes,
1993 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001994 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001995 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001996 return;
1997
1998 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001999 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002000
David Blaikie4e4d0842012-03-11 07:00:24 +00002001 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002002 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2003 PropertyTy->isObjCRetainableType()) {
2004 // 'readonly' property with no obvious lifetime.
2005 // its life time will be determined by its backing ivar.
2006 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
2007 ObjCDeclSpec::DQ_PR_copy |
2008 ObjCDeclSpec::DQ_PR_retain |
2009 ObjCDeclSpec::DQ_PR_strong |
2010 ObjCDeclSpec::DQ_PR_weak |
2011 ObjCDeclSpec::DQ_PR_assign);
2012 if ((Attributes & rel) == 0)
2013 return;
2014 }
2015
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002016 if (propertyInPrimaryClass) {
2017 // we postpone most property diagnosis until class's implementation
2018 // because, its readonly attribute may be overridden in its class
2019 // extensions making other attributes, which make no sense, to make sense.
2020 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2021 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2022 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2023 << "readonly" << "readwrite";
2024 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002025 // readonly and readwrite/assign/retain/copy conflict.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002026 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2027 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002028 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002029 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002030 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002031 ObjCDeclSpec::DQ_PR_retain |
2032 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002033 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2034 "readwrite" :
2035 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2036 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00002037 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
2038 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00002039 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2040 "copy" : "retain";
2041
2042 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
2043 diag::err_objc_property_attr_mutually_exclusive :
2044 diag::warn_objc_property_attr_mutually_exclusive)
2045 << "readonly" << which;
2046 }
2047
2048 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00002049 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2050 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2051 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002052 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002053 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00002054 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2055 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2056 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2057 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002058 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002059 }
2060
2061 // Check for more than one of { assign, copy, retain }.
2062 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2063 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2064 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2065 << "assign" << "copy";
2066 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2067 }
2068 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2069 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2070 << "assign" << "retain";
2071 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2072 }
John McCallf85e1932011-06-15 23:02:42 +00002073 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2074 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2075 << "assign" << "strong";
2076 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2077 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002078 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002079 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2080 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2081 << "assign" << "weak";
2082 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2083 }
2084 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2085 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2086 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2087 << "unsafe_unretained" << "copy";
2088 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2089 }
2090 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2091 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2092 << "unsafe_unretained" << "retain";
2093 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2094 }
2095 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2096 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2097 << "unsafe_unretained" << "strong";
2098 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2099 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002100 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002101 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2102 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2103 << "unsafe_unretained" << "weak";
2104 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2105 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002106 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2107 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2108 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2109 << "copy" << "retain";
2110 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2111 }
John McCallf85e1932011-06-15 23:02:42 +00002112 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2113 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2114 << "copy" << "strong";
2115 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2116 }
2117 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2118 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2119 << "copy" << "weak";
2120 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2121 }
2122 }
2123 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2124 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2125 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2126 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002127 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002128 }
2129 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2130 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2131 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2132 << "strong" << "weak";
2133 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002134 }
2135
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002136 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2137 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2138 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2139 << "atomic" << "nonatomic";
2140 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2141 }
2142
Ted Kremenek9d64c152010-03-12 00:38:38 +00002143 // Warn if user supplied no assignment attribute, property is
2144 // readwrite, and this is an object type.
2145 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002146 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2147 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2148 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002149 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002150 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002151 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002152 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002153 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002154 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002155 bool isAnyClassTy =
2156 (PropertyTy->isObjCClassType() ||
2157 PropertyTy->isObjCQualifiedClassType());
2158 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2159 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002160 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002161 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002162 else if (propertyInPrimaryClass) {
2163 // Don't issue warning on property with no life time in class
2164 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002165 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002166 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002167 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002168
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002169 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002170 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002171 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002172 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002173 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002174
2175 // FIXME: Implement warning dependent on NSCopying being
2176 // implemented. See also:
2177 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2178 // (please trim this list while you are at it).
2179 }
2180
2181 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002182 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002183 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002184 && PropertyTy->isBlockPointerType())
2185 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian7c16d582012-06-27 20:52:46 +00002186 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002187 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2188 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2189 PropertyTy->isBlockPointerType())
2190 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002191
2192 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2193 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2194 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2195
Ted Kremenek9d64c152010-03-12 00:38:38 +00002196}