blob: e754e7337798e9313eeac2a40fb310b76f3d5e72 [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"
John McCall50df6ae2010-08-25 07:03:20 +000021#include "llvm/ADT/DenseSet.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000022
23using namespace clang;
24
Ted Kremenek28685ab2010-03-12 00:46:40 +000025//===----------------------------------------------------------------------===//
26// Grammar actions.
27//===----------------------------------------------------------------------===//
28
John McCall265941b2011-09-13 18:31:23 +000029/// getImpliedARCOwnership - Given a set of property attributes and a
30/// type, infer an expected lifetime. The type's ownership qualification
31/// is not considered.
32///
33/// Returns OCL_None if the attributes as stated do not imply an ownership.
34/// Never returns OCL_Autoreleasing.
35static Qualifiers::ObjCLifetime getImpliedARCOwnership(
36 ObjCPropertyDecl::PropertyAttributeKind attrs,
37 QualType type) {
38 // retain, strong, copy, weak, and unsafe_unretained are only legal
39 // on properties of retainable pointer type.
40 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
41 ObjCPropertyDecl::OBJC_PR_strong |
42 ObjCPropertyDecl::OBJC_PR_copy)) {
Fariborz Jahanian5fa065b2011-10-13 23:45:45 +000043 return type->getObjCARCImplicitLifetime();
John McCall265941b2011-09-13 18:31:23 +000044 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
45 return Qualifiers::OCL_Weak;
46 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
47 return Qualifiers::OCL_ExplicitNone;
48 }
49
50 // assign can appear on other types, so we have to check the
51 // property type.
52 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
53 type->isObjCRetainableType()) {
54 return Qualifiers::OCL_ExplicitNone;
55 }
56
57 return Qualifiers::OCL_None;
58}
59
John McCallf85e1932011-06-15 23:02:42 +000060/// Check the internal consistency of a property declaration.
61static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
62 if (property->isInvalidDecl()) return;
63
64 ObjCPropertyDecl::PropertyAttributeKind propertyKind
65 = property->getPropertyAttributes();
66 Qualifiers::ObjCLifetime propertyLifetime
67 = property->getType().getObjCLifetime();
68
69 // Nothing to do if we don't have a lifetime.
70 if (propertyLifetime == Qualifiers::OCL_None) return;
71
John McCall265941b2011-09-13 18:31:23 +000072 Qualifiers::ObjCLifetime expectedLifetime
73 = getImpliedARCOwnership(propertyKind, property->getType());
74 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000075 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000076 // attribute. That's okay, but restore reasonable invariants by
77 // setting the property attribute according to the lifetime
78 // qualifier.
79 ObjCPropertyDecl::PropertyAttributeKind attr;
80 if (propertyLifetime == Qualifiers::OCL_Strong) {
81 attr = ObjCPropertyDecl::OBJC_PR_strong;
82 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
83 attr = ObjCPropertyDecl::OBJC_PR_weak;
84 } else {
85 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
86 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
87 }
88 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000089 return;
90 }
91
92 if (propertyLifetime == expectedLifetime) return;
93
94 property->setInvalidDecl();
95 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000096 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +000097 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +000098 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +000099 << propertyLifetime;
100}
101
John McCalld226f652010-08-21 09:40:31 +0000102Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
103 FieldDeclarator &FD,
104 ObjCDeclSpec &ODS,
105 Selector GetterSel,
106 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000107 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000108 tok::ObjCKeywordKind MethodImplKind,
109 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000110 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000111 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
112 QualType T = TSI->getType();
Douglas Gregore289d812011-09-13 17:21:33 +0000113 if ((getLangOptions().getGC() != LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +0000114 T.isObjCGCWeak()) ||
115 (getLangOptions().ObjCAutoRefCount &&
116 T.getObjCLifetime() == Qualifiers::OCL_Weak))
117 Attributes |= ObjCDeclSpec::DQ_PR_weak;
118
Ted Kremenek28685ab2010-03-12 00:46:40 +0000119 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
120 // default is readwrite!
121 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
122 // property is defaulted to 'assign' if it is readwrite and is
123 // not retain or copy
124 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
125 (isReadWrite &&
126 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +0000127 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
128 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
129 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
130 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000131
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000132 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000133 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000134
Ted Kremenek28685ab2010-03-12 00:46:40 +0000135 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000136 if (CDecl->IsClassExtension()) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000137 Decl *Res = HandlePropertyInClassExtension(S, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000138 FD, GetterSel, SetterSel,
139 isAssign, isReadWrite,
140 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000141 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000142 isOverridingProperty, TSI,
143 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000144 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000145 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000146 if (getLangOptions().ObjCAutoRefCount)
147 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
148 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000149 return Res;
150 }
151
John McCallf85e1932011-06-15 23:02:42 +0000152 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
153 GetterSel, SetterSel,
154 isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000155 Attributes,
156 ODS.getPropertyAttributes(),
157 TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000158 if (lexicalDC)
159 Res->setLexicalDeclContext(lexicalDC);
160
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000161 // Validate the attributes on the @property.
162 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000163
164 if (getLangOptions().ObjCAutoRefCount)
165 checkARCPropertyDecl(*this, Res);
166
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000167 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000168}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000169
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000170static ObjCPropertyDecl::PropertyAttributeKind
171makePropertyAttributesAsWritten(unsigned Attributes) {
172 unsigned attributesAsWritten = 0;
173 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
174 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
175 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
176 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
177 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
178 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
179 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
180 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
181 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
182 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
183 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
184 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
185 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
186 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
187 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
188 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
189 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
190 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
191 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
192 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
193 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
194 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
195 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
196 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
197
198 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
199}
200
John McCalld226f652010-08-21 09:40:31 +0000201Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000202Sema::HandlePropertyInClassExtension(Scope *S,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000203 SourceLocation AtLoc, FieldDeclarator &FD,
204 Selector GetterSel, Selector SetterSel,
205 const bool isAssign,
206 const bool isReadWrite,
207 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000208 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000209 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000210 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000211 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000212 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000213 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000214 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000215 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000216 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
217
218 if (CCPrimary)
219 // Check for duplicate declaration of this property in current and
220 // other class extensions.
221 for (const ObjCCategoryDecl *ClsExtDecl =
222 CCPrimary->getFirstClassExtension();
223 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
224 if (ObjCPropertyDecl *prevDecl =
225 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
226 Diag(AtLoc, diag::err_duplicate_property);
227 Diag(prevDecl->getLocation(), diag::note_property_declare);
228 return 0;
229 }
230 }
231
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000232 // Create a new ObjCPropertyDecl with the DeclContext being
233 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000234 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000235 ObjCPropertyDecl *PDecl =
236 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
237 PropertyId, AtLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000238 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000239 makePropertyAttributesAsWritten(AttributesAsWritten));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000240 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
241 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
242 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
243 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000244 // Set setter/getter selector name. Needed later.
245 PDecl->setGetterName(GetterSel);
246 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000247 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000248 DC->addDecl(PDecl);
249
250 // We need to look in the @interface to see if the @property was
251 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000252 if (!CCPrimary) {
253 Diag(CDecl->getLocation(), diag::err_continuation_class);
254 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000255 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000256 }
257
258 // Find the property in continuation class's primary class only.
259 ObjCPropertyDecl *PIDecl =
260 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
261
262 if (!PIDecl) {
263 // No matching property found in the primary class. Just fall thru
264 // and add property to continuation class's primary class.
265 ObjCPropertyDecl *PDecl =
266 CreatePropertyDecl(S, CCPrimary, AtLoc,
267 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000268 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000269
270 // A case of continuation class adding a new property in the class. This
271 // is not what it was meant for. However, gcc supports it and so should we.
272 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000273 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
274 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000275 if (ASTMutationListener *L = Context.getASTMutationListener())
276 L->AddedObjCPropertyInClassExtension(PDecl, /*OrigProp=*/0, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000277 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000278 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000279 if (PIDecl->getType().getCanonicalType()
280 != PDecl->getType().getCanonicalType()) {
281 Diag(AtLoc,
Fariborz Jahaniand3c147f2011-11-28 18:38:27 +0000282 diag::err_type_mismatch_continuation_class) << PDecl->getType();
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000283 Diag(PIDecl->getLocation(), diag::note_property_declare);
284 }
285
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000286 // The property 'PIDecl's readonly attribute will be over-ridden
287 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000288 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000289 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
290 unsigned retainCopyNonatomic =
291 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000292 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000293 ObjCPropertyDecl::OBJC_PR_copy |
294 ObjCPropertyDecl::OBJC_PR_nonatomic);
295 if ((Attributes & retainCopyNonatomic) !=
296 (PIkind & retainCopyNonatomic)) {
297 Diag(AtLoc, diag::warn_property_attr_mismatch);
298 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000299 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000300 DeclContext *DC = cast<DeclContext>(CCPrimary);
301 if (!ObjCPropertyDecl::findPropertyDecl(DC,
302 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000303 // Protocol is not in the primary class. Must build one for it.
304 ObjCDeclSpec ProtocolPropertyODS;
305 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
306 // and ObjCPropertyDecl::PropertyAttributeKind have identical
307 // values. Should consolidate both into one enum type.
308 ProtocolPropertyODS.
309 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
310 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000311 // Must re-establish the context from class extension to primary
312 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000313 ContextRAII SavedContext(*this, CCPrimary);
314
John McCalld226f652010-08-21 09:40:31 +0000315 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000316 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
317 PIDecl->getGetterName(),
318 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000319 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000320 MethodImplKind,
321 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000322 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000323 }
324 PIDecl->makeitReadWriteAttribute();
325 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
326 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000327 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
328 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000329 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
330 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
331 PIDecl->setSetterName(SetterSel);
332 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000333 // Tailor the diagnostics for the common case where a readwrite
334 // property is declared both in the @interface and the continuation.
335 // This is a common error where the user often intended the original
336 // declaration to be readonly.
337 unsigned diag =
338 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
339 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
340 ? diag::err_use_continuation_class_redeclaration_readwrite
341 : diag::err_use_continuation_class;
342 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000343 << CCPrimary->getDeclName();
344 Diag(PIDecl->getLocation(), diag::note_property_declare);
345 }
346 *isOverridingProperty = true;
347 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000348 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000349 if (ASTMutationListener *L = Context.getASTMutationListener())
350 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000351 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000352}
353
354ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
355 ObjCContainerDecl *CDecl,
356 SourceLocation AtLoc,
357 FieldDeclarator &FD,
358 Selector GetterSel,
359 Selector SetterSel,
360 const bool isAssign,
361 const bool isReadWrite,
362 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000363 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000364 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000365 tok::ObjCKeywordKind MethodImplKind,
366 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000367 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000368 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000369
370 // Issue a warning if property is 'assign' as default and its object, which is
371 // gc'able conforms to NSCopying protocol
Douglas Gregore289d812011-09-13 17:21:33 +0000372 if (getLangOptions().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000373 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000374 if (const ObjCObjectPointerType *ObjPtrTy =
375 T->getAs<ObjCObjectPointerType>()) {
376 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
377 if (IDecl)
378 if (ObjCProtocolDecl* PNSCopying =
379 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
380 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
381 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000382 }
John McCallc12c5bb2010-05-15 11:32:37 +0000383 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000384 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
385
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000386 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000387 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
388 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000389 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000390
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000391 if (ObjCPropertyDecl *prevDecl =
392 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000393 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000394 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000395 PDecl->setInvalidDecl();
396 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000397 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000398 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000399 if (lexicalDC)
400 PDecl->setLexicalDeclContext(lexicalDC);
401 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000402
403 if (T->isArrayType() || T->isFunctionType()) {
404 Diag(AtLoc, diag::err_property_type) << T;
405 PDecl->setInvalidDecl();
406 }
407
408 ProcessDeclAttributes(S, PDecl, FD.D);
409
410 // Regardless of setter/getter attribute, we save the default getter/setter
411 // selector names in anticipation of declaration of setter/getter methods.
412 PDecl->setGetterName(GetterSel);
413 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000414 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000415 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000416
Ted Kremenek28685ab2010-03-12 00:46:40 +0000417 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
418 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
419
420 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
421 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
422
423 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
424 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
425
426 if (isReadWrite)
427 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
428
429 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
430 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
431
John McCallf85e1932011-06-15 23:02:42 +0000432 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
433 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
434
435 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
436 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
437
Ted Kremenek28685ab2010-03-12 00:46:40 +0000438 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
439 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
440
John McCallf85e1932011-06-15 23:02:42 +0000441 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
442 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
443
Ted Kremenek28685ab2010-03-12 00:46:40 +0000444 if (isAssign)
445 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
446
John McCall265941b2011-09-13 18:31:23 +0000447 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000448 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
449 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000450 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000451 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000452
John McCallf85e1932011-06-15 23:02:42 +0000453 // 'unsafe_unretained' is alias for 'assign'.
454 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
455 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
456 if (isAssign)
457 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
458
Ted Kremenek28685ab2010-03-12 00:46:40 +0000459 if (MethodImplKind == tok::objc_required)
460 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
461 else if (MethodImplKind == tok::objc_optional)
462 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000463
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000464 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000465}
466
John McCallf85e1932011-06-15 23:02:42 +0000467static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
468 ObjCPropertyDecl *property,
469 ObjCIvarDecl *ivar) {
470 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
471
John McCallf85e1932011-06-15 23:02:42 +0000472 QualType ivarType = ivar->getType();
473 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000474
John McCall265941b2011-09-13 18:31:23 +0000475 // The lifetime implied by the property's attributes.
476 Qualifiers::ObjCLifetime propertyLifetime =
477 getImpliedARCOwnership(property->getPropertyAttributes(),
478 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000479
John McCall265941b2011-09-13 18:31:23 +0000480 // We're fine if they match.
481 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000482
John McCall265941b2011-09-13 18:31:23 +0000483 // These aren't valid lifetimes for object ivars; don't diagnose twice.
484 if (ivarLifetime == Qualifiers::OCL_None ||
485 ivarLifetime == Qualifiers::OCL_Autoreleasing)
486 return;
John McCallf85e1932011-06-15 23:02:42 +0000487
John McCall265941b2011-09-13 18:31:23 +0000488 switch (propertyLifetime) {
489 case Qualifiers::OCL_Strong:
490 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
491 << property->getDeclName()
492 << ivar->getDeclName()
493 << ivarLifetime;
494 break;
John McCallf85e1932011-06-15 23:02:42 +0000495
John McCall265941b2011-09-13 18:31:23 +0000496 case Qualifiers::OCL_Weak:
497 S.Diag(propertyImplLoc, diag::error_weak_property)
498 << property->getDeclName()
499 << ivar->getDeclName();
500 break;
John McCallf85e1932011-06-15 23:02:42 +0000501
John McCall265941b2011-09-13 18:31:23 +0000502 case Qualifiers::OCL_ExplicitNone:
503 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
504 << property->getDeclName()
505 << ivar->getDeclName()
506 << ((property->getPropertyAttributesAsWritten()
507 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
508 break;
John McCallf85e1932011-06-15 23:02:42 +0000509
John McCall265941b2011-09-13 18:31:23 +0000510 case Qualifiers::OCL_Autoreleasing:
511 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000512
John McCall265941b2011-09-13 18:31:23 +0000513 case Qualifiers::OCL_None:
514 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000515 return;
516 }
517
518 S.Diag(property->getLocation(), diag::note_property_declare);
519}
520
Ted Kremenek28685ab2010-03-12 00:46:40 +0000521
522/// ActOnPropertyImplDecl - This routine performs semantic checks and
523/// builds the AST node for a property implementation declaration; declared
524/// as @synthesize or @dynamic.
525///
John McCalld226f652010-08-21 09:40:31 +0000526Decl *Sema::ActOnPropertyImplDecl(Scope *S,
527 SourceLocation AtLoc,
528 SourceLocation PropertyLoc,
529 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000530 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000531 IdentifierInfo *PropertyIvar,
532 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000533 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000534 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000535 // Make sure we have a context for the property implementation declaration.
536 if (!ClassImpDecl) {
537 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000538 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000539 }
540 ObjCPropertyDecl *property = 0;
541 ObjCInterfaceDecl* IDecl = 0;
542 // Find the class or category class where this property must have
543 // a declaration.
544 ObjCImplementationDecl *IC = 0;
545 ObjCCategoryImplDecl* CatImplClass = 0;
546 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
547 IDecl = IC->getClassInterface();
548 // We always synthesize an interface for an implementation
549 // without an interface decl. So, IDecl is always non-zero.
550 assert(IDecl &&
551 "ActOnPropertyImplDecl - @implementation without @interface");
552
553 // Look for this property declaration in the @implementation's @interface
554 property = IDecl->FindPropertyDeclaration(PropertyId);
555 if (!property) {
556 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000557 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000558 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000559 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000560 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
561 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000562 if (AtLoc.isValid())
563 Diag(AtLoc, diag::warn_implicit_atomic_property);
564 else
565 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
566 Diag(property->getLocation(), diag::note_property_declare);
567 }
568
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569 if (const ObjCCategoryDecl *CD =
570 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
571 if (!CD->IsClassExtension()) {
572 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
573 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000574 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000575 }
576 }
577 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
578 if (Synthesize) {
579 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000580 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000581 }
582 IDecl = CatImplClass->getClassInterface();
583 if (!IDecl) {
584 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000585 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000586 }
587 ObjCCategoryDecl *Category =
588 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
589
590 // If category for this implementation not found, it is an error which
591 // has already been reported eralier.
592 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000593 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000594 // Look for this property declaration in @implementation's category
595 property = Category->FindPropertyDeclaration(PropertyId);
596 if (!property) {
597 Diag(PropertyLoc, diag::error_bad_category_property_decl)
598 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000599 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000600 }
601 } else {
602 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000603 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000604 }
605 ObjCIvarDecl *Ivar = 0;
606 // Check that we have a valid, previously declared ivar for @synthesize
607 if (Synthesize) {
608 // @synthesize
609 if (!PropertyIvar)
610 PropertyIvar = PropertyId;
John McCallf85e1932011-06-15 23:02:42 +0000611 ObjCPropertyDecl::PropertyAttributeKind kind
612 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000613 QualType PropType = property->getType();
614
615 QualType PropertyIvarType = PropType.getNonReferenceType();
616
617 // Add GC __weak to the ivar type if the property is weak.
618 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
Douglas Gregore289d812011-09-13 17:21:33 +0000619 getLangOptions().getGC() != LangOptions::NonGC) {
John McCall265941b2011-09-13 18:31:23 +0000620 assert(!getLangOptions().ObjCAutoRefCount);
621 if (PropertyIvarType.isObjCGCStrong()) {
622 Diag(PropertyLoc, diag::err_gc_weak_property_strong_type);
623 Diag(property->getLocation(), diag::note_property_declare);
624 } else {
625 PropertyIvarType =
626 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000627 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000628 }
John McCall265941b2011-09-13 18:31:23 +0000629
Ted Kremenek28685ab2010-03-12 00:46:40 +0000630 // Check that this is a previously declared 'ivar' in 'IDecl' interface
631 ObjCInterfaceDecl *ClassDeclared;
632 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
633 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000634 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000635 // property attributes.
636 if (getLangOptions().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000637 !PropertyIvarType.getObjCLifetime() &&
638 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000639
John McCall265941b2011-09-13 18:31:23 +0000640 // It's an error if we have to do this and the user didn't
641 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000642 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000643 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000644 Diag(PropertyLoc,
645 diag::err_arc_objc_property_default_assign_on_object);
646 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000647 } else {
648 Qualifiers::ObjCLifetime lifetime =
649 getImpliedARCOwnership(kind, PropertyIvarType);
650 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000651 if (lifetime == Qualifiers::OCL_Weak) {
652 bool err = false;
653 if (const ObjCObjectPointerType *ObjT =
654 PropertyIvarType->getAs<ObjCObjectPointerType>())
655 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
656 Diag(PropertyLoc, diag::err_arc_weak_unavailable_property);
657 Diag(property->getLocation(), diag::note_property_declare);
658 err = true;
659 }
660 if (!err && !getLangOptions().ObjCRuntimeHasWeak) {
661 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
662 Diag(property->getLocation(), diag::note_property_declare);
663 }
John McCallf85e1932011-06-15 23:02:42 +0000664 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000665
John McCallf85e1932011-06-15 23:02:42 +0000666 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000667 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000668 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
669 }
John McCallf85e1932011-06-15 23:02:42 +0000670 }
671
672 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
673 !getLangOptions().ObjCAutoRefCount &&
Douglas Gregore289d812011-09-13 17:21:33 +0000674 getLangOptions().getGC() == LangOptions::NonGC) {
John McCallf85e1932011-06-15 23:02:42 +0000675 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
676 Diag(property->getLocation(), diag::note_property_declare);
677 }
678
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000679 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
680 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000681 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000682 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000683 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000684 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000685 IDecl->makeDeclVisibleInContext(Ivar, false);
686 property->setPropertyIvarDecl(Ivar);
687
688 if (!getLangOptions().ObjCNonFragileABI)
689 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
690 // Note! I deliberately want it to fall thru so, we have a
691 // a property implementation and to avoid future warnings.
692 } else if (getLangOptions().ObjCNonFragileABI &&
693 ClassDeclared != IDecl) {
694 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
695 << property->getDeclName() << Ivar->getDeclName()
696 << ClassDeclared->getDeclName();
697 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000698 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000699 // Note! I deliberately want it to fall thru so more errors are caught.
700 }
701 QualType IvarType = Context.getCanonicalType(Ivar->getType());
702
703 // Check that type of property and its ivar are type compatible.
John McCall265941b2011-09-13 18:31:23 +0000704 if (Context.getCanonicalType(PropertyIvarType) != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000705 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000706 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000707 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000708 compat =
709 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000710 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000711 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000712 else {
713 SourceLocation Loc = PropertyIvarLoc;
714 if (Loc.isInvalid())
715 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000716 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000717 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000718 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000719 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000720 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000721 << property->getDeclName() << PropType
722 << Ivar->getDeclName() << IvarType;
723 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000724 // Note! I deliberately want it to fall thru so, we have a
725 // a property implementation and to avoid future warnings.
726 }
727
728 // FIXME! Rules for properties are somewhat different that those
729 // for assignments. Use a new routine to consolidate all cases;
730 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000731 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000732 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
733 if (lhsType != rhsType &&
734 lhsType->isArithmeticType()) {
735 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000736 << property->getDeclName() << PropType
737 << Ivar->getDeclName() << IvarType;
738 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000739 // Fall thru - see previous comment
740 }
741 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000742 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000743 getLangOptions().getGC() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000744 Diag(PropertyLoc, diag::error_weak_property)
745 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000746 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000747 // Fall thru - see previous comment
748 }
John McCallf85e1932011-06-15 23:02:42 +0000749 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000750 if ((property->getType()->isObjCObjectPointerType() ||
751 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000752 getLangOptions().getGC() != LangOptions::NonGC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000753 Diag(PropertyLoc, diag::error_strong_property)
754 << property->getDeclName() << Ivar->getDeclName();
755 // Fall thru - see previous comment
756 }
757 }
John McCallf85e1932011-06-15 23:02:42 +0000758 if (getLangOptions().ObjCAutoRefCount)
759 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000760 } else if (PropertyIvar)
761 // @dynamic
762 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000763
Ted Kremenek28685ab2010-03-12 00:46:40 +0000764 assert (property && "ActOnPropertyImplDecl - property declaration missing");
765 ObjCPropertyImplDecl *PIDecl =
766 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
767 property,
768 (Synthesize ?
769 ObjCPropertyImplDecl::Synthesize
770 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000771 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000772 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
773 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000774 if (getLangOptions().CPlusPlus && Synthesize &&
775 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000776 // For Objective-C++, need to synthesize the AST for the IVAR object to be
777 // returned by the getter as it must conform to C++'s copy-return rules.
778 // FIXME. Eventually we want to do this for Objective-C as well.
779 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
780 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000781 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
782 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000783 Expr *IvarRefExpr =
784 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
785 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000786 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000787 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000788 SourceLocation(),
789 getterMethod->getResultType(),
790 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000791 SourceLocation(),
792 Owned(IvarRefExpr));
793 if (!Res.isInvalid()) {
794 Expr *ResExpr = Res.takeAs<Expr>();
795 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000796 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000797 PIDecl->setGetterCXXConstructor(ResExpr);
798 }
799 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000800 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
801 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
802 Diag(getterMethod->getLocation(),
803 diag::warn_property_getter_owning_mismatch);
804 Diag(property->getLocation(), diag::note_property_declare);
805 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000806 }
807 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
808 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000809 if (getLangOptions().CPlusPlus && Synthesize
810 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000811 // FIXME. Eventually we want to do this for Objective-C as well.
812 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
813 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000814 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
815 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000816 Expr *lhs =
817 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
818 SelfExpr, true, true);
819 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
820 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +0000821 QualType T = Param->getType().getNonReferenceType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000822 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000823 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000824 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000825 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000826 if (property->getPropertyAttributes() &
827 ObjCPropertyDecl::OBJC_PR_atomic) {
828 Expr *callExpr = Res.takeAs<Expr>();
829 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000830 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
831 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000832 if (!FuncDecl->isTrivial())
833 Diag(PropertyLoc,
834 diag::warn_atomic_property_nontrivial_assign_op)
835 << property->getType();
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000836 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000837 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
838 }
839 }
840
Ted Kremenek28685ab2010-03-12 00:46:40 +0000841 if (IC) {
842 if (Synthesize)
843 if (ObjCPropertyImplDecl *PPIDecl =
844 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
845 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
846 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
847 << PropertyIvar;
848 Diag(PPIDecl->getLocation(), diag::note_previous_use);
849 }
850
851 if (ObjCPropertyImplDecl *PPIDecl
852 = IC->FindPropertyImplDecl(PropertyId)) {
853 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
854 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000855 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000856 }
857 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000858 if (getLangOptions().ObjCDefaultSynthProperties &&
859 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000860 // Diagnose if an ivar was lazily synthesdized due to a previous
861 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000862 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000863 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000864 ObjCIvarDecl *Ivar = 0;
865 if (!Synthesize)
866 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
867 else {
868 if (PropertyIvar && PropertyIvar != PropertyId)
869 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
870 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000871 // Issue diagnostics only if Ivar belongs to current class.
872 if (Ivar && Ivar->getSynthesize() &&
873 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000874 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
875 << PropertyId;
876 Ivar->setInvalidDecl();
877 }
878 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000879 } else {
880 if (Synthesize)
881 if (ObjCPropertyImplDecl *PPIDecl =
882 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
883 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
884 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
885 << PropertyIvar;
886 Diag(PPIDecl->getLocation(), diag::note_previous_use);
887 }
888
889 if (ObjCPropertyImplDecl *PPIDecl =
890 CatImplClass->FindPropertyImplDecl(PropertyId)) {
891 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
892 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000893 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000894 }
895 CatImplClass->addPropertyImplementation(PIDecl);
896 }
897
John McCalld226f652010-08-21 09:40:31 +0000898 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000899}
900
901//===----------------------------------------------------------------------===//
902// Helper methods.
903//===----------------------------------------------------------------------===//
904
Ted Kremenek9d64c152010-03-12 00:38:38 +0000905/// DiagnosePropertyMismatch - Compares two properties for their
906/// attributes and types and warns on a variety of inconsistencies.
907///
908void
909Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
910 ObjCPropertyDecl *SuperProperty,
911 const IdentifierInfo *inheritedName) {
912 ObjCPropertyDecl::PropertyAttributeKind CAttr =
913 Property->getPropertyAttributes();
914 ObjCPropertyDecl::PropertyAttributeKind SAttr =
915 SuperProperty->getPropertyAttributes();
916 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
917 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
918 Diag(Property->getLocation(), diag::warn_readonly_property)
919 << Property->getDeclName() << inheritedName;
920 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
921 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
922 Diag(Property->getLocation(), diag::warn_property_attribute)
923 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +0000924 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +0000925 unsigned CAttrRetain =
926 (CAttr &
927 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
928 unsigned SAttrRetain =
929 (SAttr &
930 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
931 bool CStrong = (CAttrRetain != 0);
932 bool SStrong = (SAttrRetain != 0);
933 if (CStrong != SStrong)
934 Diag(Property->getLocation(), diag::warn_property_attribute)
935 << Property->getDeclName() << "retain (or strong)" << inheritedName;
936 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000937
938 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
939 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
940 Diag(Property->getLocation(), diag::warn_property_attribute)
941 << Property->getDeclName() << "atomic" << inheritedName;
942 if (Property->getSetterName() != SuperProperty->getSetterName())
943 Diag(Property->getLocation(), diag::warn_property_attribute)
944 << Property->getDeclName() << "setter" << inheritedName;
945 if (Property->getGetterName() != SuperProperty->getGetterName())
946 Diag(Property->getLocation(), diag::warn_property_attribute)
947 << Property->getDeclName() << "getter" << inheritedName;
948
949 QualType LHSType =
950 Context.getCanonicalType(SuperProperty->getType());
951 QualType RHSType =
952 Context.getCanonicalType(Property->getType());
953
Fariborz Jahanianc286f382011-07-12 22:05:16 +0000954 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000955 // Do cases not handled in above.
956 // FIXME. For future support of covariant property types, revisit this.
957 bool IncompatibleObjC = false;
958 QualType ConvertedType;
959 if (!isObjCPointerConversion(RHSType, LHSType,
960 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +0000961 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000962 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
963 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +0000964 Diag(SuperProperty->getLocation(), diag::note_property_declare);
965 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000966 }
967}
968
969bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
970 ObjCMethodDecl *GetterMethod,
971 SourceLocation Loc) {
972 if (GetterMethod &&
John McCall3c3b7f92011-10-25 17:37:35 +0000973 !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(),
974 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000975 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000976 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000977 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000978 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000979 if (result != Compatible) {
980 Diag(Loc, diag::warn_accessor_property_type_mismatch)
981 << property->getDeclName()
982 << GetterMethod->getSelector();
983 Diag(GetterMethod->getLocation(), diag::note_declared_at);
984 return true;
985 }
986 }
987 return false;
988}
989
990/// ComparePropertiesInBaseAndSuper - This routine compares property
991/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000992/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000993///
994void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
995 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
996 if (!SDecl)
997 return;
998 // FIXME: O(N^2)
999 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1000 E = SDecl->prop_end(); S != E; ++S) {
1001 ObjCPropertyDecl *SuperPDecl = (*S);
1002 // Does property in super class has declaration in current class?
1003 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1004 E = IDecl->prop_end(); I != E; ++I) {
1005 ObjCPropertyDecl *PDecl = (*I);
1006 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1007 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1008 SDecl->getIdentifier());
1009 }
1010 }
1011}
1012
1013/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1014/// of properties declared in a protocol and compares their attribute against
1015/// the same property declared in the class or category.
1016void
1017Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1018 ObjCProtocolDecl *PDecl) {
1019 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1020 if (!IDecl) {
1021 // Category
1022 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1023 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1024 if (!CatDecl->IsClassExtension())
1025 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1026 E = PDecl->prop_end(); P != E; ++P) {
1027 ObjCPropertyDecl *Pr = (*P);
1028 ObjCCategoryDecl::prop_iterator CP, CE;
1029 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001030 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001031 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1032 break;
1033 if (CP != CE)
1034 // Property protocol already exist in class. Diagnose any mismatch.
1035 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1036 }
1037 return;
1038 }
1039 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1040 E = PDecl->prop_end(); P != E; ++P) {
1041 ObjCPropertyDecl *Pr = (*P);
1042 ObjCInterfaceDecl::prop_iterator CP, CE;
1043 // Is this property already in class's list of properties?
1044 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1045 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1046 break;
1047 if (CP != CE)
1048 // Property protocol already exist in class. Diagnose any mismatch.
1049 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1050 }
1051}
1052
1053/// CompareProperties - This routine compares properties
1054/// declared in 'ClassOrProtocol' objects (which can be a class or an
1055/// inherited protocol with the list of properties for class/category 'CDecl'
1056///
John McCalld226f652010-08-21 09:40:31 +00001057void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1058 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001059 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1060
1061 if (!IDecl) {
1062 // Category
1063 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1064 assert (CatDecl && "CompareProperties");
1065 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1066 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1067 E = MDecl->protocol_end(); P != E; ++P)
1068 // Match properties of category with those of protocol (*P)
1069 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1070
1071 // Go thru the list of protocols for this category and recursively match
1072 // their properties with those in the category.
1073 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1074 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001075 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001076 } else {
1077 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1078 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1079 E = MD->protocol_end(); P != E; ++P)
1080 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1081 }
1082 return;
1083 }
1084
1085 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001086 for (ObjCInterfaceDecl::all_protocol_iterator
1087 P = MDecl->all_referenced_protocol_begin(),
1088 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001089 // Match properties of class IDecl with those of protocol (*P).
1090 MatchOneProtocolPropertiesInClass(IDecl, *P);
1091
1092 // Go thru the list of protocols for this class and recursively match
1093 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001094 for (ObjCInterfaceDecl::all_protocol_iterator
1095 P = IDecl->all_referenced_protocol_begin(),
1096 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001097 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001098 } else {
1099 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1100 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1101 E = MD->protocol_end(); P != E; ++P)
1102 MatchOneProtocolPropertiesInClass(IDecl, *P);
1103 }
1104}
1105
1106/// isPropertyReadonly - Return true if property is readonly, by searching
1107/// for the property in the class and in its categories and implementations
1108///
1109bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1110 ObjCInterfaceDecl *IDecl) {
1111 // by far the most common case.
1112 if (!PDecl->isReadOnly())
1113 return false;
1114 // Even if property is ready only, if interface has a user defined setter,
1115 // it is not considered read only.
1116 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1117 return false;
1118
1119 // Main class has the property as 'readonly'. Must search
1120 // through the category list to see if the property's
1121 // attribute has been over-ridden to 'readwrite'.
1122 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1123 Category; Category = Category->getNextClassCategory()) {
1124 // Even if property is ready only, if a category has a user defined setter,
1125 // it is not considered read only.
1126 if (Category->getInstanceMethod(PDecl->getSetterName()))
1127 return false;
1128 ObjCPropertyDecl *P =
1129 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1130 if (P && !P->isReadOnly())
1131 return false;
1132 }
1133
1134 // Also, check for definition of a setter method in the implementation if
1135 // all else failed.
1136 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1137 if (ObjCImplementationDecl *IMD =
1138 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1139 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1140 return false;
1141 } else if (ObjCCategoryImplDecl *CIMD =
1142 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1143 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1144 return false;
1145 }
1146 }
1147 // Lastly, look through the implementation (if one is in scope).
1148 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1149 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1150 return false;
1151 // If all fails, look at the super class.
1152 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1153 return isPropertyReadonly(PDecl, SIDecl);
1154 return true;
1155}
1156
1157/// CollectImmediateProperties - This routine collects all properties in
1158/// the class and its conforming protocols; but not those it its super class.
1159void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001160 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1161 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001162 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1163 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1164 E = IDecl->prop_end(); P != E; ++P) {
1165 ObjCPropertyDecl *Prop = (*P);
1166 PropMap[Prop->getIdentifier()] = Prop;
1167 }
1168 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001169 for (ObjCInterfaceDecl::all_protocol_iterator
1170 PI = IDecl->all_referenced_protocol_begin(),
1171 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001172 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001173 }
1174 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1175 if (!CATDecl->IsClassExtension())
1176 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1177 E = CATDecl->prop_end(); P != E; ++P) {
1178 ObjCPropertyDecl *Prop = (*P);
1179 PropMap[Prop->getIdentifier()] = Prop;
1180 }
1181 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001182 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001183 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001184 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001185 }
1186 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1187 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1188 E = PDecl->prop_end(); P != E; ++P) {
1189 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001190 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1191 // Exclude property for protocols which conform to class's super-class,
1192 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001193 if (!PropertyFromSuper ||
1194 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001195 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1196 if (!PropEntry)
1197 PropEntry = Prop;
1198 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001199 }
1200 // scan through protocol's protocols.
1201 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1202 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001203 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001204 }
1205}
1206
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001207/// CollectClassPropertyImplementations - This routine collects list of
1208/// properties to be implemented in the class. This includes, class's
1209/// and its conforming protocols' properties.
1210static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1211 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1212 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1213 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1214 E = IDecl->prop_end(); P != E; ++P) {
1215 ObjCPropertyDecl *Prop = (*P);
1216 PropMap[Prop->getIdentifier()] = Prop;
1217 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001218 for (ObjCInterfaceDecl::all_protocol_iterator
1219 PI = IDecl->all_referenced_protocol_begin(),
1220 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001221 CollectClassPropertyImplementations((*PI), PropMap);
1222 }
1223 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1224 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1225 E = PDecl->prop_end(); P != E; ++P) {
1226 ObjCPropertyDecl *Prop = (*P);
1227 PropMap[Prop->getIdentifier()] = Prop;
1228 }
1229 // scan through protocol's protocols.
1230 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1231 E = PDecl->protocol_end(); PI != E; ++PI)
1232 CollectClassPropertyImplementations((*PI), PropMap);
1233 }
1234}
1235
1236/// CollectSuperClassPropertyImplementations - This routine collects list of
1237/// properties to be implemented in super class(s) and also coming from their
1238/// conforming protocols.
1239static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1240 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1241 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1242 while (SDecl) {
1243 CollectClassPropertyImplementations(SDecl, PropMap);
1244 SDecl = SDecl->getSuperClass();
1245 }
1246 }
1247}
1248
Ted Kremenek9d64c152010-03-12 00:38:38 +00001249/// LookupPropertyDecl - Looks up a property in the current class and all
1250/// its protocols.
1251ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1252 IdentifierInfo *II) {
1253 if (const ObjCInterfaceDecl *IDecl =
1254 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1255 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1256 E = IDecl->prop_end(); P != E; ++P) {
1257 ObjCPropertyDecl *Prop = (*P);
1258 if (Prop->getIdentifier() == II)
1259 return Prop;
1260 }
1261 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001262 for (ObjCInterfaceDecl::all_protocol_iterator
1263 PI = IDecl->all_referenced_protocol_begin(),
1264 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001265 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1266 if (Prop)
1267 return Prop;
1268 }
1269 }
1270 else if (const ObjCProtocolDecl *PDecl =
1271 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1272 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1273 E = PDecl->prop_end(); P != E; ++P) {
1274 ObjCPropertyDecl *Prop = (*P);
1275 if (Prop->getIdentifier() == II)
1276 return Prop;
1277 }
1278 // scan through protocol's protocols.
1279 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1280 E = PDecl->protocol_end(); PI != E; ++PI) {
1281 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1282 if (Prop)
1283 return Prop;
1284 }
1285 }
1286 return 0;
1287}
1288
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001289static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1290 ASTContext &Ctx) {
1291 llvm::SmallString<128> ivarName;
1292 {
1293 llvm::raw_svector_ostream os(ivarName);
1294 os << '_' << Prop->getIdentifier()->getName();
1295 }
1296 return &Ctx.Idents.get(ivarName.str());
1297}
1298
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001299/// DefaultSynthesizeProperties - This routine default synthesizes all
1300/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001301void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1302 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001303
1304 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1305 CollectClassPropertyImplementations(IDecl, PropMap);
1306 if (PropMap.empty())
1307 return;
1308 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1309 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1310
1311 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1312 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1313 ObjCPropertyDecl *Prop = P->second;
1314 // If property to be implemented in the super class, ignore.
1315 if (SuperPropMap[Prop->getIdentifier()])
1316 continue;
1317 // Is there a matching propery synthesize/dynamic?
1318 if (Prop->isInvalidDecl() ||
1319 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1320 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1321 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001322 // Property may have been synthesized by user.
1323 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1324 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001325 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1326 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1327 continue;
1328 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1329 continue;
1330 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001331
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001332
1333 // We use invalid SourceLocations for the synthesized ivars since they
1334 // aren't really synthesized at a particular location; they just exist.
1335 // Saying that they are located at the @implementation isn't really going
1336 // to help users.
1337 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001338 true,
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001339 /* property = */ Prop->getIdentifier(),
1340 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001341 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001342 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001343}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001344
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001345void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1346 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1347 return;
1348 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1349 if (!IC)
1350 return;
1351 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1352 DefaultSynthesizeProperties(S, IC, IDecl);
1353}
1354
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001355void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001356 ObjCContainerDecl *CDecl,
1357 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001358 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1359 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1360 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1361
Ted Kremenek9d64c152010-03-12 00:38:38 +00001362 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001363 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001364 if (PropMap.empty())
1365 return;
1366
1367 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1368 for (ObjCImplDecl::propimpl_iterator
1369 I = IMPDecl->propimpl_begin(),
1370 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1371 PropImplMap.insert((*I)->getPropertyDecl());
1372
1373 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1374 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1375 ObjCPropertyDecl *Prop = P->second;
1376 // Is there a matching propery synthesize/dynamic?
1377 if (Prop->isInvalidDecl() ||
1378 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001379 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001380 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001381 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001382 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001383 isa<ObjCCategoryDecl>(CDecl) ?
1384 diag::warn_setter_getter_impl_required_in_category :
1385 diag::warn_setter_getter_impl_required)
1386 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001387 Diag(Prop->getLocation(),
1388 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001389 }
1390
1391 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001392 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001393 isa<ObjCCategoryDecl>(CDecl) ?
1394 diag::warn_setter_getter_impl_required_in_category :
1395 diag::warn_setter_getter_impl_required)
1396 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001397 Diag(Prop->getLocation(),
1398 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001399 }
1400 }
1401}
1402
1403void
1404Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1405 ObjCContainerDecl* IDecl) {
1406 // Rules apply in non-GC mode only
Douglas Gregore289d812011-09-13 17:21:33 +00001407 if (getLangOptions().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001408 return;
1409 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1410 E = IDecl->prop_end();
1411 I != E; ++I) {
1412 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001413 ObjCMethodDecl *GetterMethod = 0;
1414 ObjCMethodDecl *SetterMethod = 0;
1415 bool LookedUpGetterSetter = false;
1416
Ted Kremenek9d64c152010-03-12 00:38:38 +00001417 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001418 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001419
John McCall265941b2011-09-13 18:31:23 +00001420 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1421 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001422 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1423 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1424 LookedUpGetterSetter = true;
1425 if (GetterMethod) {
1426 Diag(GetterMethod->getLocation(),
1427 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001428 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001429 Diag(Property->getLocation(), diag::note_property_declare);
1430 }
1431 if (SetterMethod) {
1432 Diag(SetterMethod->getLocation(),
1433 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001434 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001435 Diag(Property->getLocation(), diag::note_property_declare);
1436 }
1437 }
1438
Ted Kremenek9d64c152010-03-12 00:38:38 +00001439 // We only care about readwrite atomic property.
1440 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1441 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1442 continue;
1443 if (const ObjCPropertyImplDecl *PIDecl
1444 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1445 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1446 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001447 if (!LookedUpGetterSetter) {
1448 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1449 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1450 LookedUpGetterSetter = true;
1451 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001452 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1453 SourceLocation MethodLoc =
1454 (GetterMethod ? GetterMethod->getLocation()
1455 : SetterMethod->getLocation());
1456 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001457 << Property->getIdentifier() << (GetterMethod != 0)
1458 << (SetterMethod != 0);
1459 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001460 Diag(Property->getLocation(), diag::note_property_declare);
1461 }
1462 }
1463 }
1464}
1465
John McCallf85e1932011-06-15 23:02:42 +00001466void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
Douglas Gregore289d812011-09-13 17:21:33 +00001467 if (getLangOptions().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001468 return;
1469
1470 for (ObjCImplementationDecl::propimpl_iterator
1471 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1472 ObjCPropertyImplDecl *PID = *i;
1473 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1474 continue;
1475
1476 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001477 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1478 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001479 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1480 if (!method)
1481 continue;
1482 ObjCMethodFamily family = method->getMethodFamily();
1483 if (family == OMF_alloc || family == OMF_copy ||
1484 family == OMF_mutableCopy || family == OMF_new) {
1485 if (getLangOptions().ObjCAutoRefCount)
1486 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1487 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001488 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001489 Diag(PD->getLocation(), diag::note_property_declare);
1490 }
1491 }
1492 }
1493}
1494
John McCall5de74d12010-11-10 07:01:40 +00001495/// AddPropertyAttrs - Propagates attributes from a property to the
1496/// implicitly-declared getter or setter for that property.
1497static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1498 ObjCPropertyDecl *Property) {
1499 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001500 for (Decl::attr_iterator A = Property->attr_begin(),
1501 AEnd = Property->attr_end();
1502 A != AEnd; ++A) {
1503 if (isa<DeprecatedAttr>(*A) ||
1504 isa<UnavailableAttr>(*A) ||
1505 isa<AvailabilityAttr>(*A))
1506 PropertyMethod->addAttr((*A)->clone(S.Context));
1507 }
John McCall5de74d12010-11-10 07:01:40 +00001508}
1509
Ted Kremenek9d64c152010-03-12 00:38:38 +00001510/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1511/// have the property type and issue diagnostics if they don't.
1512/// Also synthesize a getter/setter method if none exist (and update the
1513/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1514/// methods is the "right" thing to do.
1515void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001516 ObjCContainerDecl *CD,
1517 ObjCPropertyDecl *redeclaredProperty,
1518 ObjCContainerDecl *lexicalDC) {
1519
Ted Kremenek9d64c152010-03-12 00:38:38 +00001520 ObjCMethodDecl *GetterMethod, *SetterMethod;
1521
1522 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1523 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1524 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1525 property->getLocation());
1526
1527 if (SetterMethod) {
1528 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1529 property->getPropertyAttributes();
1530 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1531 Context.getCanonicalType(SetterMethod->getResultType()) !=
1532 Context.VoidTy)
1533 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1534 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001535 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001536 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1537 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001538 Diag(property->getLocation(),
1539 diag::warn_accessor_property_type_mismatch)
1540 << property->getDeclName()
1541 << SetterMethod->getSelector();
1542 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1543 }
1544 }
1545
1546 // Synthesize getter/setter methods if none exist.
1547 // Find the default getter and if one not found, add one.
1548 // FIXME: The synthesized property we set here is misleading. We almost always
1549 // synthesize these methods unless the user explicitly provided prototypes
1550 // (which is odd, but allowed). Sema should be typechecking that the
1551 // declarations jive in that situation (which it is not currently).
1552 if (!GetterMethod) {
1553 // No instance method of same name as property getter name was found.
1554 // Declare a getter method and add it to the list of methods
1555 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001556 SourceLocation Loc = redeclaredProperty ?
1557 redeclaredProperty->getLocation() :
1558 property->getLocation();
1559
1560 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1561 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001562 property->getType(), 0, CD, /*isInstance=*/true,
1563 /*isVariadic=*/false, /*isSynthesized=*/true,
1564 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001565 (property->getPropertyImplementation() ==
1566 ObjCPropertyDecl::Optional) ?
1567 ObjCMethodDecl::Optional :
1568 ObjCMethodDecl::Required);
1569 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001570
1571 AddPropertyAttrs(*this, GetterMethod, property);
1572
Ted Kremenek23173d72010-05-18 21:09:07 +00001573 // FIXME: Eventually this shouldn't be needed, as the lexical context
1574 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001575 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001576 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001577 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1578 GetterMethod->addAttr(
1579 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001580 } else
1581 // A user declared getter will be synthesize when @synthesize of
1582 // the property with the same name is seen in the @implementation
1583 GetterMethod->setSynthesized(true);
1584 property->setGetterMethodDecl(GetterMethod);
1585
1586 // Skip setter if property is read-only.
1587 if (!property->isReadOnly()) {
1588 // Find the default setter and if one not found, add one.
1589 if (!SetterMethod) {
1590 // No instance method of same name as property setter name was found.
1591 // Declare a setter method and add it to the list of methods
1592 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001593 SourceLocation Loc = redeclaredProperty ?
1594 redeclaredProperty->getLocation() :
1595 property->getLocation();
1596
1597 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001598 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001599 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001600 CD, /*isInstance=*/true, /*isVariadic=*/false,
1601 /*isSynthesized=*/true,
1602 /*isImplicitlyDeclared=*/true,
1603 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001604 (property->getPropertyImplementation() ==
1605 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001606 ObjCMethodDecl::Optional :
1607 ObjCMethodDecl::Required);
1608
Ted Kremenek9d64c152010-03-12 00:38:38 +00001609 // Invent the arguments for the setter. We don't bother making a
1610 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001611 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1612 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001613 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001614 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001615 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001616 SC_None,
1617 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001618 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001619 SetterMethod->setMethodParams(Context, Argument,
1620 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001621
1622 AddPropertyAttrs(*this, SetterMethod, property);
1623
Ted Kremenek9d64c152010-03-12 00:38:38 +00001624 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001625 // FIXME: Eventually this shouldn't be needed, as the lexical context
1626 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001627 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001628 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001629 } else
1630 // A user declared setter will be synthesize when @synthesize of
1631 // the property with the same name is seen in the @implementation
1632 SetterMethod->setSynthesized(true);
1633 property->setSetterMethodDecl(SetterMethod);
1634 }
1635 // Add any synthesized methods to the global pool. This allows us to
1636 // handle the following, which is supported by GCC (and part of the design).
1637 //
1638 // @interface Foo
1639 // @property double bar;
1640 // @end
1641 //
1642 // void thisIsUnfortunate() {
1643 // id foo;
1644 // double bar = [foo bar];
1645 // }
1646 //
1647 if (GetterMethod)
1648 AddInstanceMethodToGlobalPool(GetterMethod);
1649 if (SetterMethod)
1650 AddInstanceMethodToGlobalPool(SetterMethod);
1651}
1652
John McCalld226f652010-08-21 09:40:31 +00001653void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001654 SourceLocation Loc,
1655 unsigned &Attributes) {
1656 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001657 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001658 return;
1659
1660 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001661 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001662
1663 // readonly and readwrite/assign/retain/copy conflict.
1664 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1665 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1666 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001667 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001668 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001669 ObjCDeclSpec::DQ_PR_retain |
1670 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001671 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1672 "readwrite" :
1673 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1674 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001675 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1676 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001677 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1678 "copy" : "retain";
1679
1680 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1681 diag::err_objc_property_attr_mutually_exclusive :
1682 diag::warn_objc_property_attr_mutually_exclusive)
1683 << "readonly" << which;
1684 }
1685
1686 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001687 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1688 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1689 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001690 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001691 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001692 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1693 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1694 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1695 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001696 }
1697
1698 // Check for more than one of { assign, copy, retain }.
1699 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1700 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1701 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1702 << "assign" << "copy";
1703 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1704 }
1705 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1706 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1707 << "assign" << "retain";
1708 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1709 }
John McCallf85e1932011-06-15 23:02:42 +00001710 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1711 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1712 << "assign" << "strong";
1713 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1714 }
1715 if (getLangOptions().ObjCAutoRefCount &&
1716 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1717 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1718 << "assign" << "weak";
1719 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1720 }
1721 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1722 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1723 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1724 << "unsafe_unretained" << "copy";
1725 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1726 }
1727 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1728 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1729 << "unsafe_unretained" << "retain";
1730 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1731 }
1732 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1733 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1734 << "unsafe_unretained" << "strong";
1735 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1736 }
1737 if (getLangOptions().ObjCAutoRefCount &&
1738 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1739 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1740 << "unsafe_unretained" << "weak";
1741 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1742 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001743 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1744 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1745 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1746 << "copy" << "retain";
1747 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1748 }
John McCallf85e1932011-06-15 23:02:42 +00001749 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1750 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1751 << "copy" << "strong";
1752 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1753 }
1754 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1755 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1756 << "copy" << "weak";
1757 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1758 }
1759 }
1760 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1761 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1762 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1763 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001764 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001765 }
1766 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1767 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1768 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1769 << "strong" << "weak";
1770 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001771 }
1772
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00001773 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
1774 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
1775 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1776 << "atomic" << "nonatomic";
1777 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
1778 }
1779
Ted Kremenek9d64c152010-03-12 00:38:38 +00001780 // Warn if user supplied no assignment attribute, property is
1781 // readwrite, and this is an object type.
1782 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001783 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1784 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1785 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001786 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001787 if (getLangOptions().ObjCAutoRefCount)
1788 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001789 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001790 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001791 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001792 // Skip this warning in gc-only mode.
Douglas Gregore289d812011-09-13 17:21:33 +00001793 if (getLangOptions().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001794 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001795
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001796 // If non-gc code warn that this is likely inappropriate.
Douglas Gregore289d812011-09-13 17:21:33 +00001797 if (getLangOptions().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001798 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1799 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001800
1801 // FIXME: Implement warning dependent on NSCopying being
1802 // implemented. See also:
1803 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1804 // (please trim this list while you are at it).
1805 }
1806
1807 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001808 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Douglas Gregore289d812011-09-13 17:21:33 +00001809 && getLangOptions().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001810 && PropertyTy->isBlockPointerType())
1811 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001812 else if (getLangOptions().ObjCAutoRefCount &&
1813 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1814 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1815 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1816 PropertyTy->isBlockPointerType())
1817 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00001818
1819 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1820 (Attributes & ObjCDeclSpec::DQ_PR_setter))
1821 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
1822
Ted Kremenek9d64c152010-03-12 00:38:38 +00001823}