blob: 012e2569c33380718a69a3dac66cea01c4928272 [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"
John McCall50df6ae2010-08-25 07:03:20 +000019#include "llvm/ADT/DenseSet.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000020
21using namespace clang;
22
Ted Kremenek28685ab2010-03-12 00:46:40 +000023//===----------------------------------------------------------------------===//
24// Grammar actions.
25//===----------------------------------------------------------------------===//
26
John McCallf85e1932011-06-15 23:02:42 +000027/// Check the internal consistency of a property declaration.
28static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
29 if (property->isInvalidDecl()) return;
30
31 ObjCPropertyDecl::PropertyAttributeKind propertyKind
32 = property->getPropertyAttributes();
33 Qualifiers::ObjCLifetime propertyLifetime
34 = property->getType().getObjCLifetime();
35
36 // Nothing to do if we don't have a lifetime.
37 if (propertyLifetime == Qualifiers::OCL_None) return;
38
39 Qualifiers::ObjCLifetime expectedLifetime;
40 unsigned selector;
41
42 // Strong properties should have either strong or no lifetime.
43 if (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
44 ObjCPropertyDecl::OBJC_PR_strong |
45 ObjCPropertyDecl::OBJC_PR_copy)) {
46 expectedLifetime = Qualifiers::OCL_Strong;
47 selector = 0;
48 } else if (propertyKind & ObjCPropertyDecl::OBJC_PR_weak) {
49 expectedLifetime = Qualifiers::OCL_Weak;
50 selector = 1;
51 } else if (propertyKind & (ObjCPropertyDecl::OBJC_PR_assign |
52 ObjCPropertyDecl::OBJC_PR_unsafe_unretained) &&
53 property->getType()->isObjCRetainableType()) {
54 expectedLifetime = Qualifiers::OCL_ExplicitNone;
55 selector = 2;
56 } else {
57 // We have a lifetime qualifier but no dominating property
58 // attribute. That's okay.
59 return;
60 }
61
62 if (propertyLifetime == expectedLifetime) return;
63
64 property->setInvalidDecl();
65 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000066 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +000067 << property->getDeclName()
68 << selector
69 << propertyLifetime;
70}
71
John McCalld226f652010-08-21 09:40:31 +000072Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
73 FieldDeclarator &FD,
74 ObjCDeclSpec &ODS,
75 Selector GetterSel,
76 Selector SetterSel,
77 Decl *ClassCategory,
78 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000079 tok::ObjCKeywordKind MethodImplKind,
80 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000081 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +000082 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
83 QualType T = TSI->getType();
84 if ((getLangOptions().getGCMode() != LangOptions::NonGC &&
85 T.isObjCGCWeak()) ||
86 (getLangOptions().ObjCAutoRefCount &&
87 T.getObjCLifetime() == Qualifiers::OCL_Weak))
88 Attributes |= ObjCDeclSpec::DQ_PR_weak;
89
Ted Kremenek28685ab2010-03-12 00:46:40 +000090 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
91 // default is readwrite!
92 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
93 // property is defaulted to 'assign' if it is readwrite and is
94 // not retain or copy
95 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
96 (isReadWrite &&
97 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +000098 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
99 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
100 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
101 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000102
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000103 // Proceed with constructing the ObjCPropertDecls.
104 ObjCContainerDecl *ClassDecl =
John McCalld226f652010-08-21 09:40:31 +0000105 cast<ObjCContainerDecl>(ClassCategory);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000106
Ted Kremenek28685ab2010-03-12 00:46:40 +0000107 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000108 if (CDecl->IsClassExtension()) {
John McCalld226f652010-08-21 09:40:31 +0000109 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000110 FD, GetterSel, SetterSel,
111 isAssign, isReadWrite,
112 Attributes,
113 isOverridingProperty, TSI,
114 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000115 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000116 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000117 if (getLangOptions().ObjCAutoRefCount)
118 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
119 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000120 return Res;
121 }
122
John McCallf85e1932011-06-15 23:02:42 +0000123 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
124 GetterSel, SetterSel,
125 isAssign, isReadWrite,
126 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000127 if (lexicalDC)
128 Res->setLexicalDeclContext(lexicalDC);
129
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000130 // Validate the attributes on the @property.
131 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000132
133 if (getLangOptions().ObjCAutoRefCount)
134 checkARCPropertyDecl(*this, Res);
135
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000136 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000137}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000138
John McCalld226f652010-08-21 09:40:31 +0000139Decl *
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000140Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
141 SourceLocation AtLoc, FieldDeclarator &FD,
142 Selector GetterSel, Selector SetterSel,
143 const bool isAssign,
144 const bool isReadWrite,
145 const unsigned Attributes,
146 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000147 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000148 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000149
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000150 // Diagnose if this property is already in continuation class.
151 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000152 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000153 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
154
155 if (CCPrimary)
156 // Check for duplicate declaration of this property in current and
157 // other class extensions.
158 for (const ObjCCategoryDecl *ClsExtDecl =
159 CCPrimary->getFirstClassExtension();
160 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
161 if (ObjCPropertyDecl *prevDecl =
162 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
163 Diag(AtLoc, diag::err_duplicate_property);
164 Diag(prevDecl->getLocation(), diag::note_property_declare);
165 return 0;
166 }
167 }
168
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000169 // Create a new ObjCPropertyDecl with the DeclContext being
170 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000171 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000172 ObjCPropertyDecl *PDecl =
173 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
174 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000175 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
176 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
177 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
178 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000179 // Set setter/getter selector name. Needed later.
180 PDecl->setGetterName(GetterSel);
181 PDecl->setSetterName(SetterSel);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000182 DC->addDecl(PDecl);
183
184 // We need to look in the @interface to see if the @property was
185 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000186 if (!CCPrimary) {
187 Diag(CDecl->getLocation(), diag::err_continuation_class);
188 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000189 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000190 }
191
192 // Find the property in continuation class's primary class only.
193 ObjCPropertyDecl *PIDecl =
194 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
195
196 if (!PIDecl) {
197 // No matching property found in the primary class. Just fall thru
198 // and add property to continuation class's primary class.
199 ObjCPropertyDecl *PDecl =
200 CreatePropertyDecl(S, CCPrimary, AtLoc,
201 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000202 Attributes, T, MethodImplKind, DC);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000203 // Mark written attribute as having no attribute because
204 // this is not a user-written property declaration in primary
205 // class.
206 PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000207
208 // A case of continuation class adding a new property in the class. This
209 // is not what it was meant for. However, gcc supports it and so should we.
210 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000211 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
212 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000213 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000214 }
215
216 // The property 'PIDecl's readonly attribute will be over-ridden
217 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000218 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000219 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
220 unsigned retainCopyNonatomic =
221 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000222 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000223 ObjCPropertyDecl::OBJC_PR_copy |
224 ObjCPropertyDecl::OBJC_PR_nonatomic);
225 if ((Attributes & retainCopyNonatomic) !=
226 (PIkind & retainCopyNonatomic)) {
227 Diag(AtLoc, diag::warn_property_attr_mismatch);
228 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000229 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000230 DeclContext *DC = cast<DeclContext>(CCPrimary);
231 if (!ObjCPropertyDecl::findPropertyDecl(DC,
232 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000233 // Protocol is not in the primary class. Must build one for it.
234 ObjCDeclSpec ProtocolPropertyODS;
235 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
236 // and ObjCPropertyDecl::PropertyAttributeKind have identical
237 // values. Should consolidate both into one enum type.
238 ProtocolPropertyODS.
239 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
240 PIkind);
241
John McCalld226f652010-08-21 09:40:31 +0000242 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000243 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
244 PIDecl->getGetterName(),
245 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000246 CCPrimary, isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000247 MethodImplKind,
248 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000249 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000250 }
251 PIDecl->makeitReadWriteAttribute();
252 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
253 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000254 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
255 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
257 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
258 PIDecl->setSetterName(SetterSel);
259 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000260 // Tailor the diagnostics for the common case where a readwrite
261 // property is declared both in the @interface and the continuation.
262 // This is a common error where the user often intended the original
263 // declaration to be readonly.
264 unsigned diag =
265 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
266 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
267 ? diag::err_use_continuation_class_redeclaration_readwrite
268 : diag::err_use_continuation_class;
269 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000270 << CCPrimary->getDeclName();
271 Diag(PIDecl->getLocation(), diag::note_property_declare);
272 }
273 *isOverridingProperty = true;
274 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000275 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000276 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000277}
278
279ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
280 ObjCContainerDecl *CDecl,
281 SourceLocation AtLoc,
282 FieldDeclarator &FD,
283 Selector GetterSel,
284 Selector SetterSel,
285 const bool isAssign,
286 const bool isReadWrite,
287 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000288 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000289 tok::ObjCKeywordKind MethodImplKind,
290 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000291 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000292 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000293
294 // Issue a warning if property is 'assign' as default and its object, which is
295 // gc'able conforms to NSCopying protocol
296 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
297 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000298 if (const ObjCObjectPointerType *ObjPtrTy =
299 T->getAs<ObjCObjectPointerType>()) {
300 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
301 if (IDecl)
302 if (ObjCProtocolDecl* PNSCopying =
303 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
304 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
305 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000306 }
John McCallc12c5bb2010-05-15 11:32:37 +0000307 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000308 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
309
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000310 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000311 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
312 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000313 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000314
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000315 if (ObjCPropertyDecl *prevDecl =
316 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000317 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000318 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000319 PDecl->setInvalidDecl();
320 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000321 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000322 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000323 if (lexicalDC)
324 PDecl->setLexicalDeclContext(lexicalDC);
325 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000326
327 if (T->isArrayType() || T->isFunctionType()) {
328 Diag(AtLoc, diag::err_property_type) << T;
329 PDecl->setInvalidDecl();
330 }
331
332 ProcessDeclAttributes(S, PDecl, FD.D);
333
334 // Regardless of setter/getter attribute, we save the default getter/setter
335 // selector names in anticipation of declaration of setter/getter methods.
336 PDecl->setGetterName(GetterSel);
337 PDecl->setSetterName(SetterSel);
338
339 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
340 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
341
342 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
343 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
344
345 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
346 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
347
348 if (isReadWrite)
349 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
350
351 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
352 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
353
John McCallf85e1932011-06-15 23:02:42 +0000354 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
355 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
356
357 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
358 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
359
Ted Kremenek28685ab2010-03-12 00:46:40 +0000360 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
361 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
362
John McCallf85e1932011-06-15 23:02:42 +0000363 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
364 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
365
Ted Kremenek28685ab2010-03-12 00:46:40 +0000366 if (isAssign)
367 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
368
369 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000371 else if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
372 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000373
John McCallf85e1932011-06-15 23:02:42 +0000374 // FIXME: Why do PropertyAttributesAsWritten get set from PropertyAttributes,
375 // shouldn't PropertyAttributesAsWritten get set *only* through the attributes
376 // of the ObjCDeclSpec ?
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000377 PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes());
John McCallf85e1932011-06-15 23:02:42 +0000378
379 // 'unsafe_unretained' is alias for 'assign'.
380 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
381 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
382 if (isAssign)
383 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
384
Ted Kremenek28685ab2010-03-12 00:46:40 +0000385 if (MethodImplKind == tok::objc_required)
386 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
387 else if (MethodImplKind == tok::objc_optional)
388 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000389
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000390 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000391}
392
John McCallf85e1932011-06-15 23:02:42 +0000393static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
394 ObjCPropertyDecl *property,
395 ObjCIvarDecl *ivar) {
396 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
397
398 QualType propertyType = property->getType();
399 Qualifiers::ObjCLifetime propertyLifetime = propertyType.getObjCLifetime();
400 ObjCPropertyDecl::PropertyAttributeKind propertyKind
401 = property->getPropertyAttributes();
402
403 QualType ivarType = ivar->getType();
404 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
405
406 // Case 1: strong properties.
407 if (propertyLifetime == Qualifiers::OCL_Strong ||
408 (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
409 ObjCPropertyDecl::OBJC_PR_strong |
410 ObjCPropertyDecl::OBJC_PR_copy))) {
411 switch (ivarLifetime) {
412 case Qualifiers::OCL_Strong:
413 // Okay.
414 return;
415
416 case Qualifiers::OCL_None:
417 case Qualifiers::OCL_Autoreleasing:
418 // These aren't valid lifetimes for object ivars; don't diagnose twice.
419 return;
420
421 case Qualifiers::OCL_ExplicitNone:
422 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000423 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000424 << property->getDeclName()
425 << ivar->getDeclName()
426 << ivarLifetime;
427 break;
428 }
429
430 // Case 2: weak properties.
431 } else if (propertyLifetime == Qualifiers::OCL_Weak ||
432 (propertyKind & ObjCPropertyDecl::OBJC_PR_weak)) {
433 switch (ivarLifetime) {
434 case Qualifiers::OCL_Weak:
435 // Okay.
436 return;
437
438 case Qualifiers::OCL_None:
439 case Qualifiers::OCL_Autoreleasing:
440 // These aren't valid lifetimes for object ivars; don't diagnose twice.
441 return;
442
443 case Qualifiers::OCL_ExplicitNone:
444 case Qualifiers::OCL_Strong:
445 S.Diag(propertyImplLoc, diag::error_weak_property)
446 << property->getDeclName()
447 << ivar->getDeclName();
448 break;
449 }
450
451 // Case 3: assign properties.
452 } else if ((propertyKind & ObjCPropertyDecl::OBJC_PR_assign) &&
453 propertyType->isObjCRetainableType()) {
454 switch (ivarLifetime) {
455 case Qualifiers::OCL_ExplicitNone:
456 // Okay.
457 return;
458
459 case Qualifiers::OCL_None:
460 case Qualifiers::OCL_Autoreleasing:
461 // These aren't valid lifetimes for object ivars; don't diagnose twice.
462 return;
463
464 case Qualifiers::OCL_Weak:
465 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000466 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000467 << property->getDeclName()
468 << ivar->getDeclName();
469 break;
470 }
471
472 // Any other property should be ignored.
473 } else {
474 return;
475 }
476
477 S.Diag(property->getLocation(), diag::note_property_declare);
478}
479
Ted Kremenek28685ab2010-03-12 00:46:40 +0000480
481/// ActOnPropertyImplDecl - This routine performs semantic checks and
482/// builds the AST node for a property implementation declaration; declared
483/// as @synthesize or @dynamic.
484///
John McCalld226f652010-08-21 09:40:31 +0000485Decl *Sema::ActOnPropertyImplDecl(Scope *S,
486 SourceLocation AtLoc,
487 SourceLocation PropertyLoc,
488 bool Synthesize,
489 Decl *ClassCatImpDecl,
490 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000491 IdentifierInfo *PropertyIvar,
492 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000493 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000494 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000495 // Make sure we have a context for the property implementation declaration.
496 if (!ClassImpDecl) {
497 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000498 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000499 }
500 ObjCPropertyDecl *property = 0;
501 ObjCInterfaceDecl* IDecl = 0;
502 // Find the class or category class where this property must have
503 // a declaration.
504 ObjCImplementationDecl *IC = 0;
505 ObjCCategoryImplDecl* CatImplClass = 0;
506 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
507 IDecl = IC->getClassInterface();
508 // We always synthesize an interface for an implementation
509 // without an interface decl. So, IDecl is always non-zero.
510 assert(IDecl &&
511 "ActOnPropertyImplDecl - @implementation without @interface");
512
513 // Look for this property declaration in the @implementation's @interface
514 property = IDecl->FindPropertyDeclaration(PropertyId);
515 if (!property) {
516 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000517 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000518 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000519 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000520 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
521 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000522 if (AtLoc.isValid())
523 Diag(AtLoc, diag::warn_implicit_atomic_property);
524 else
525 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
526 Diag(property->getLocation(), diag::note_property_declare);
527 }
528
Ted Kremenek28685ab2010-03-12 00:46:40 +0000529 if (const ObjCCategoryDecl *CD =
530 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
531 if (!CD->IsClassExtension()) {
532 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
533 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000534 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000535 }
536 }
537 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
538 if (Synthesize) {
539 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000540 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000541 }
542 IDecl = CatImplClass->getClassInterface();
543 if (!IDecl) {
544 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000545 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000546 }
547 ObjCCategoryDecl *Category =
548 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
549
550 // If category for this implementation not found, it is an error which
551 // has already been reported eralier.
552 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000553 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000554 // Look for this property declaration in @implementation's category
555 property = Category->FindPropertyDeclaration(PropertyId);
556 if (!property) {
557 Diag(PropertyLoc, diag::error_bad_category_property_decl)
558 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000559 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000560 }
561 } else {
562 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000563 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000564 }
565 ObjCIvarDecl *Ivar = 0;
566 // Check that we have a valid, previously declared ivar for @synthesize
567 if (Synthesize) {
568 // @synthesize
569 if (!PropertyIvar)
570 PropertyIvar = PropertyId;
John McCallf85e1932011-06-15 23:02:42 +0000571 ObjCPropertyDecl::PropertyAttributeKind kind
572 = property->getPropertyAttributes();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000573 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian14086762011-03-28 23:47:18 +0000574 QualType PropertyIvarType = PropType;
575 if (PropType->isReferenceType())
576 PropertyIvarType = cast<ReferenceType>(PropType)->getPointeeType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000577 // Check that this is a previously declared 'ivar' in 'IDecl' interface
578 ObjCInterfaceDecl *ClassDeclared;
579 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
580 if (!Ivar) {
John McCallf85e1932011-06-15 23:02:42 +0000581 // In ARC, give the ivar a lifetime qualifier based on its
582 // property attributes.
583 if (getLangOptions().ObjCAutoRefCount &&
584 !PropertyIvarType.getObjCLifetime()) {
585
586 // retain/copy have retaining lifetime.
587 if (kind & (ObjCPropertyDecl::OBJC_PR_retain |
588 ObjCPropertyDecl::OBJC_PR_strong |
589 ObjCPropertyDecl::OBJC_PR_copy)) {
590 Qualifiers qs;
591 qs.addObjCLifetime(Qualifiers::OCL_Strong);
592 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
593 }
594 else if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
595 if (getLangOptions().ObjCNoAutoRefCountRuntime) {
596 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
597 Diag(property->getLocation(), diag::note_property_declare);
598 }
599 Qualifiers qs;
600 qs.addObjCLifetime(Qualifiers::OCL_Weak);
601 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
602 }
603 else if (kind & ObjCPropertyDecl::OBJC_PR_assign &&
604 PropertyIvarType->isObjCRetainableType()) {
605 // assume that an 'assign' property synthesizes __unsafe_unretained
606 // ivar
607 Qualifiers qs;
608 qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
609 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
610 }
611 }
612
613 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
614 !getLangOptions().ObjCAutoRefCount &&
615 getLangOptions().getGCMode() == LangOptions::NonGC) {
616 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
617 Diag(property->getLocation(), diag::note_property_declare);
618 }
619
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000620 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
621 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000622 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000623 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000624 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000625 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000626 IDecl->makeDeclVisibleInContext(Ivar, false);
627 property->setPropertyIvarDecl(Ivar);
628
629 if (!getLangOptions().ObjCNonFragileABI)
630 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
631 // Note! I deliberately want it to fall thru so, we have a
632 // a property implementation and to avoid future warnings.
633 } else if (getLangOptions().ObjCNonFragileABI &&
634 ClassDeclared != IDecl) {
635 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
636 << property->getDeclName() << Ivar->getDeclName()
637 << ClassDeclared->getDeclName();
638 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000639 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000640 // Note! I deliberately want it to fall thru so more errors are caught.
641 }
642 QualType IvarType = Context.getCanonicalType(Ivar->getType());
643
644 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000645 if (PropertyIvarType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000646 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000647 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000648 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000649 compat =
650 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000651 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000652 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000653 else {
654 SourceLocation Loc = PropertyIvarLoc;
655 if (Loc.isInvalid())
656 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000657 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000658 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000659 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000660 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000661 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000662 << property->getDeclName() << PropType
663 << Ivar->getDeclName() << IvarType;
664 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000665 // Note! I deliberately want it to fall thru so, we have a
666 // a property implementation and to avoid future warnings.
667 }
668
669 // FIXME! Rules for properties are somewhat different that those
670 // for assignments. Use a new routine to consolidate all cases;
671 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000672 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000673 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
674 if (lhsType != rhsType &&
675 lhsType->isArithmeticType()) {
676 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000677 << property->getDeclName() << PropType
678 << Ivar->getDeclName() << IvarType;
679 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000680 // Fall thru - see previous comment
681 }
682 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000683 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
684 getLangOptions().getGCMode() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000685 Diag(PropertyLoc, diag::error_weak_property)
686 << property->getDeclName() << Ivar->getDeclName();
687 // Fall thru - see previous comment
688 }
John McCallf85e1932011-06-15 23:02:42 +0000689 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000690 if ((property->getType()->isObjCObjectPointerType() ||
691 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
692 getLangOptions().getGCMode() != LangOptions::NonGC) {
693 Diag(PropertyLoc, diag::error_strong_property)
694 << property->getDeclName() << Ivar->getDeclName();
695 // Fall thru - see previous comment
696 }
697 }
John McCallf85e1932011-06-15 23:02:42 +0000698 if (getLangOptions().ObjCAutoRefCount)
699 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000700 } else if (PropertyIvar)
701 // @dynamic
702 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000703
Ted Kremenek28685ab2010-03-12 00:46:40 +0000704 assert (property && "ActOnPropertyImplDecl - property declaration missing");
705 ObjCPropertyImplDecl *PIDecl =
706 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
707 property,
708 (Synthesize ?
709 ObjCPropertyImplDecl::Synthesize
710 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000711 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000712 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
713 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000714 if (getLangOptions().CPlusPlus && Synthesize &&
715 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000716 // For Objective-C++, need to synthesize the AST for the IVAR object to be
717 // returned by the getter as it must conform to C++'s copy-return rules.
718 // FIXME. Eventually we want to do this for Objective-C as well.
719 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
720 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000721 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
722 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000723 Expr *IvarRefExpr =
724 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
725 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000726 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000727 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000728 SourceLocation(),
729 getterMethod->getResultType(),
730 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000731 SourceLocation(),
732 Owned(IvarRefExpr));
733 if (!Res.isInvalid()) {
734 Expr *ResExpr = Res.takeAs<Expr>();
735 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000736 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000737 PIDecl->setGetterCXXConstructor(ResExpr);
738 }
739 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000740 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
741 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
742 Diag(getterMethod->getLocation(),
743 diag::warn_property_getter_owning_mismatch);
744 Diag(property->getLocation(), diag::note_property_declare);
745 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000746 }
747 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
748 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000749 if (getLangOptions().CPlusPlus && Synthesize
750 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000751 // FIXME. Eventually we want to do this for Objective-C as well.
752 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
753 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000754 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
755 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000756 Expr *lhs =
757 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
758 SelfExpr, true, true);
759 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
760 ParmVarDecl *Param = (*P);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000761 QualType T = Param->getType();
762 if (T->isReferenceType())
Fariborz Jahanian61750f22011-03-30 16:59:30 +0000763 T = T->getAs<ReferenceType>()->getPointeeType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000764 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000765 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000766 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000767 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000768 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
769 }
770 }
771
Ted Kremenek28685ab2010-03-12 00:46:40 +0000772 if (IC) {
773 if (Synthesize)
774 if (ObjCPropertyImplDecl *PPIDecl =
775 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
776 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
777 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
778 << PropertyIvar;
779 Diag(PPIDecl->getLocation(), diag::note_previous_use);
780 }
781
782 if (ObjCPropertyImplDecl *PPIDecl
783 = IC->FindPropertyImplDecl(PropertyId)) {
784 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
785 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000786 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000787 }
788 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000789 if (getLangOptions().ObjCDefaultSynthProperties &&
790 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000791 // Diagnose if an ivar was lazily synthesdized due to a previous
792 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000793 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000794 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000795 ObjCIvarDecl *Ivar = 0;
796 if (!Synthesize)
797 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
798 else {
799 if (PropertyIvar && PropertyIvar != PropertyId)
800 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
801 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000802 // Issue diagnostics only if Ivar belongs to current class.
803 if (Ivar && Ivar->getSynthesize() &&
804 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000805 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
806 << PropertyId;
807 Ivar->setInvalidDecl();
808 }
809 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000810 } else {
811 if (Synthesize)
812 if (ObjCPropertyImplDecl *PPIDecl =
813 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
814 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
815 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
816 << PropertyIvar;
817 Diag(PPIDecl->getLocation(), diag::note_previous_use);
818 }
819
820 if (ObjCPropertyImplDecl *PPIDecl =
821 CatImplClass->FindPropertyImplDecl(PropertyId)) {
822 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
823 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000824 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000825 }
826 CatImplClass->addPropertyImplementation(PIDecl);
827 }
828
John McCalld226f652010-08-21 09:40:31 +0000829 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000830}
831
832//===----------------------------------------------------------------------===//
833// Helper methods.
834//===----------------------------------------------------------------------===//
835
Ted Kremenek9d64c152010-03-12 00:38:38 +0000836/// DiagnosePropertyMismatch - Compares two properties for their
837/// attributes and types and warns on a variety of inconsistencies.
838///
839void
840Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
841 ObjCPropertyDecl *SuperProperty,
842 const IdentifierInfo *inheritedName) {
843 ObjCPropertyDecl::PropertyAttributeKind CAttr =
844 Property->getPropertyAttributes();
845 ObjCPropertyDecl::PropertyAttributeKind SAttr =
846 SuperProperty->getPropertyAttributes();
847 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
848 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
849 Diag(Property->getLocation(), diag::warn_readonly_property)
850 << Property->getDeclName() << inheritedName;
851 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
852 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
853 Diag(Property->getLocation(), diag::warn_property_attribute)
854 << Property->getDeclName() << "copy" << inheritedName;
John McCallf85e1932011-06-15 23:02:42 +0000855 else {
856 unsigned CAttrRetain =
857 (CAttr &
858 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
859 unsigned SAttrRetain =
860 (SAttr &
861 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
862 bool CStrong = (CAttrRetain != 0);
863 bool SStrong = (SAttrRetain != 0);
864 if (CStrong != SStrong)
865 Diag(Property->getLocation(), diag::warn_property_attribute)
866 << Property->getDeclName() << "retain (or strong)" << inheritedName;
867 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000868
869 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
870 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
871 Diag(Property->getLocation(), diag::warn_property_attribute)
872 << Property->getDeclName() << "atomic" << inheritedName;
873 if (Property->getSetterName() != SuperProperty->getSetterName())
874 Diag(Property->getLocation(), diag::warn_property_attribute)
875 << Property->getDeclName() << "setter" << inheritedName;
876 if (Property->getGetterName() != SuperProperty->getGetterName())
877 Diag(Property->getLocation(), diag::warn_property_attribute)
878 << Property->getDeclName() << "getter" << inheritedName;
879
880 QualType LHSType =
881 Context.getCanonicalType(SuperProperty->getType());
882 QualType RHSType =
883 Context.getCanonicalType(Property->getType());
884
885 if (!Context.typesAreCompatible(LHSType, RHSType)) {
886 // FIXME: Incorporate this test with typesAreCompatible.
887 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
888 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
889 return;
890 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
891 << Property->getType() << SuperProperty->getType() << inheritedName;
892 }
893}
894
895bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
896 ObjCMethodDecl *GetterMethod,
897 SourceLocation Loc) {
898 if (GetterMethod &&
899 GetterMethod->getResultType() != property->getType()) {
900 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000901 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000902 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000903 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000904 if (result != Compatible) {
905 Diag(Loc, diag::warn_accessor_property_type_mismatch)
906 << property->getDeclName()
907 << GetterMethod->getSelector();
908 Diag(GetterMethod->getLocation(), diag::note_declared_at);
909 return true;
910 }
911 }
912 return false;
913}
914
915/// ComparePropertiesInBaseAndSuper - This routine compares property
916/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000917/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000918///
919void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
920 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
921 if (!SDecl)
922 return;
923 // FIXME: O(N^2)
924 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
925 E = SDecl->prop_end(); S != E; ++S) {
926 ObjCPropertyDecl *SuperPDecl = (*S);
927 // Does property in super class has declaration in current class?
928 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
929 E = IDecl->prop_end(); I != E; ++I) {
930 ObjCPropertyDecl *PDecl = (*I);
931 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
932 DiagnosePropertyMismatch(PDecl, SuperPDecl,
933 SDecl->getIdentifier());
934 }
935 }
936}
937
938/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
939/// of properties declared in a protocol and compares their attribute against
940/// the same property declared in the class or category.
941void
942Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
943 ObjCProtocolDecl *PDecl) {
944 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
945 if (!IDecl) {
946 // Category
947 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
948 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
949 if (!CatDecl->IsClassExtension())
950 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
951 E = PDecl->prop_end(); P != E; ++P) {
952 ObjCPropertyDecl *Pr = (*P);
953 ObjCCategoryDecl::prop_iterator CP, CE;
954 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000955 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000956 if ((*CP)->getIdentifier() == Pr->getIdentifier())
957 break;
958 if (CP != CE)
959 // Property protocol already exist in class. Diagnose any mismatch.
960 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
961 }
962 return;
963 }
964 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
965 E = PDecl->prop_end(); P != E; ++P) {
966 ObjCPropertyDecl *Pr = (*P);
967 ObjCInterfaceDecl::prop_iterator CP, CE;
968 // Is this property already in class's list of properties?
969 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
970 if ((*CP)->getIdentifier() == Pr->getIdentifier())
971 break;
972 if (CP != CE)
973 // Property protocol already exist in class. Diagnose any mismatch.
974 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
975 }
976}
977
978/// CompareProperties - This routine compares properties
979/// declared in 'ClassOrProtocol' objects (which can be a class or an
980/// inherited protocol with the list of properties for class/category 'CDecl'
981///
John McCalld226f652010-08-21 09:40:31 +0000982void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
983 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000984 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
985
986 if (!IDecl) {
987 // Category
988 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
989 assert (CatDecl && "CompareProperties");
990 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
991 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
992 E = MDecl->protocol_end(); P != E; ++P)
993 // Match properties of category with those of protocol (*P)
994 MatchOneProtocolPropertiesInClass(CatDecl, *P);
995
996 // Go thru the list of protocols for this category and recursively match
997 // their properties with those in the category.
998 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
999 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001000 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001001 } else {
1002 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1003 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1004 E = MD->protocol_end(); P != E; ++P)
1005 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1006 }
1007 return;
1008 }
1009
1010 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001011 for (ObjCInterfaceDecl::all_protocol_iterator
1012 P = MDecl->all_referenced_protocol_begin(),
1013 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001014 // Match properties of class IDecl with those of protocol (*P).
1015 MatchOneProtocolPropertiesInClass(IDecl, *P);
1016
1017 // Go thru the list of protocols for this class and recursively match
1018 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001019 for (ObjCInterfaceDecl::all_protocol_iterator
1020 P = IDecl->all_referenced_protocol_begin(),
1021 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001022 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001023 } else {
1024 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1025 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1026 E = MD->protocol_end(); P != E; ++P)
1027 MatchOneProtocolPropertiesInClass(IDecl, *P);
1028 }
1029}
1030
1031/// isPropertyReadonly - Return true if property is readonly, by searching
1032/// for the property in the class and in its categories and implementations
1033///
1034bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1035 ObjCInterfaceDecl *IDecl) {
1036 // by far the most common case.
1037 if (!PDecl->isReadOnly())
1038 return false;
1039 // Even if property is ready only, if interface has a user defined setter,
1040 // it is not considered read only.
1041 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1042 return false;
1043
1044 // Main class has the property as 'readonly'. Must search
1045 // through the category list to see if the property's
1046 // attribute has been over-ridden to 'readwrite'.
1047 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1048 Category; Category = Category->getNextClassCategory()) {
1049 // Even if property is ready only, if a category has a user defined setter,
1050 // it is not considered read only.
1051 if (Category->getInstanceMethod(PDecl->getSetterName()))
1052 return false;
1053 ObjCPropertyDecl *P =
1054 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1055 if (P && !P->isReadOnly())
1056 return false;
1057 }
1058
1059 // Also, check for definition of a setter method in the implementation if
1060 // all else failed.
1061 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1062 if (ObjCImplementationDecl *IMD =
1063 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1064 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1065 return false;
1066 } else if (ObjCCategoryImplDecl *CIMD =
1067 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1068 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1069 return false;
1070 }
1071 }
1072 // Lastly, look through the implementation (if one is in scope).
1073 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1074 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1075 return false;
1076 // If all fails, look at the super class.
1077 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1078 return isPropertyReadonly(PDecl, SIDecl);
1079 return true;
1080}
1081
1082/// CollectImmediateProperties - This routine collects all properties in
1083/// the class and its conforming protocols; but not those it its super class.
1084void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001085 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1086 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001087 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1088 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1089 E = IDecl->prop_end(); P != E; ++P) {
1090 ObjCPropertyDecl *Prop = (*P);
1091 PropMap[Prop->getIdentifier()] = Prop;
1092 }
1093 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001094 for (ObjCInterfaceDecl::all_protocol_iterator
1095 PI = IDecl->all_referenced_protocol_begin(),
1096 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001097 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001098 }
1099 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1100 if (!CATDecl->IsClassExtension())
1101 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1102 E = CATDecl->prop_end(); P != E; ++P) {
1103 ObjCPropertyDecl *Prop = (*P);
1104 PropMap[Prop->getIdentifier()] = Prop;
1105 }
1106 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001107 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001108 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001109 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001110 }
1111 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1112 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1113 E = PDecl->prop_end(); P != E; ++P) {
1114 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001115 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1116 // Exclude property for protocols which conform to class's super-class,
1117 // as super-class has to implement the property.
1118 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
1119 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1120 if (!PropEntry)
1121 PropEntry = Prop;
1122 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001123 }
1124 // scan through protocol's protocols.
1125 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1126 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001127 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001128 }
1129}
1130
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001131/// CollectClassPropertyImplementations - This routine collects list of
1132/// properties to be implemented in the class. This includes, class's
1133/// and its conforming protocols' properties.
1134static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1135 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1136 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1137 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1138 E = IDecl->prop_end(); P != E; ++P) {
1139 ObjCPropertyDecl *Prop = (*P);
1140 PropMap[Prop->getIdentifier()] = Prop;
1141 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001142 for (ObjCInterfaceDecl::all_protocol_iterator
1143 PI = IDecl->all_referenced_protocol_begin(),
1144 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001145 CollectClassPropertyImplementations((*PI), PropMap);
1146 }
1147 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1148 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1149 E = PDecl->prop_end(); P != E; ++P) {
1150 ObjCPropertyDecl *Prop = (*P);
1151 PropMap[Prop->getIdentifier()] = Prop;
1152 }
1153 // scan through protocol's protocols.
1154 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1155 E = PDecl->protocol_end(); PI != E; ++PI)
1156 CollectClassPropertyImplementations((*PI), PropMap);
1157 }
1158}
1159
1160/// CollectSuperClassPropertyImplementations - This routine collects list of
1161/// properties to be implemented in super class(s) and also coming from their
1162/// conforming protocols.
1163static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1164 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1165 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1166 while (SDecl) {
1167 CollectClassPropertyImplementations(SDecl, PropMap);
1168 SDecl = SDecl->getSuperClass();
1169 }
1170 }
1171}
1172
Ted Kremenek9d64c152010-03-12 00:38:38 +00001173/// LookupPropertyDecl - Looks up a property in the current class and all
1174/// its protocols.
1175ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1176 IdentifierInfo *II) {
1177 if (const ObjCInterfaceDecl *IDecl =
1178 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1179 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1180 E = IDecl->prop_end(); P != E; ++P) {
1181 ObjCPropertyDecl *Prop = (*P);
1182 if (Prop->getIdentifier() == II)
1183 return Prop;
1184 }
1185 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001186 for (ObjCInterfaceDecl::all_protocol_iterator
1187 PI = IDecl->all_referenced_protocol_begin(),
1188 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001189 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1190 if (Prop)
1191 return Prop;
1192 }
1193 }
1194 else if (const ObjCProtocolDecl *PDecl =
1195 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1196 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1197 E = PDecl->prop_end(); P != E; ++P) {
1198 ObjCPropertyDecl *Prop = (*P);
1199 if (Prop->getIdentifier() == II)
1200 return Prop;
1201 }
1202 // scan through protocol's protocols.
1203 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1204 E = PDecl->protocol_end(); PI != E; ++PI) {
1205 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1206 if (Prop)
1207 return Prop;
1208 }
1209 }
1210 return 0;
1211}
1212
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001213/// DefaultSynthesizeProperties - This routine default synthesizes all
1214/// properties which must be synthesized in class's @implementation.
1215void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
1216 ObjCInterfaceDecl *IDecl) {
1217
1218 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1219 CollectClassPropertyImplementations(IDecl, PropMap);
1220 if (PropMap.empty())
1221 return;
1222 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1223 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1224
1225 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1226 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1227 ObjCPropertyDecl *Prop = P->second;
1228 // If property to be implemented in the super class, ignore.
1229 if (SuperPropMap[Prop->getIdentifier()])
1230 continue;
1231 // Is there a matching propery synthesize/dynamic?
1232 if (Prop->isInvalidDecl() ||
1233 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1234 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1235 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001236 // Property may have been synthesized by user.
1237 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1238 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001239 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1240 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1241 continue;
1242 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1243 continue;
1244 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001245
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001246
1247 // We use invalid SourceLocations for the synthesized ivars since they
1248 // aren't really synthesized at a particular location; they just exist.
1249 // Saying that they are located at the @implementation isn't really going
1250 // to help users.
1251 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1252 true,IMPDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001253 Prop->getIdentifier(), Prop->getIdentifier(),
1254 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001255 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001256}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001257
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001258void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001259 ObjCContainerDecl *CDecl,
1260 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001261 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1262 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1263 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1264
Ted Kremenek9d64c152010-03-12 00:38:38 +00001265 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001266 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001267 if (PropMap.empty())
1268 return;
1269
1270 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1271 for (ObjCImplDecl::propimpl_iterator
1272 I = IMPDecl->propimpl_begin(),
1273 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1274 PropImplMap.insert((*I)->getPropertyDecl());
1275
1276 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1277 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1278 ObjCPropertyDecl *Prop = P->second;
1279 // Is there a matching propery synthesize/dynamic?
1280 if (Prop->isInvalidDecl() ||
1281 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001282 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001283 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001284 if (!InsMap.count(Prop->getGetterName())) {
1285 Diag(Prop->getLocation(),
1286 isa<ObjCCategoryDecl>(CDecl) ?
1287 diag::warn_setter_getter_impl_required_in_category :
1288 diag::warn_setter_getter_impl_required)
1289 << Prop->getDeclName() << Prop->getGetterName();
1290 Diag(IMPDecl->getLocation(),
1291 diag::note_property_impl_required);
1292 }
1293
1294 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1295 Diag(Prop->getLocation(),
1296 isa<ObjCCategoryDecl>(CDecl) ?
1297 diag::warn_setter_getter_impl_required_in_category :
1298 diag::warn_setter_getter_impl_required)
1299 << Prop->getDeclName() << Prop->getSetterName();
1300 Diag(IMPDecl->getLocation(),
1301 diag::note_property_impl_required);
1302 }
1303 }
1304}
1305
1306void
1307Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1308 ObjCContainerDecl* IDecl) {
1309 // Rules apply in non-GC mode only
1310 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1311 return;
1312 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1313 E = IDecl->prop_end();
1314 I != E; ++I) {
1315 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001316 ObjCMethodDecl *GetterMethod = 0;
1317 ObjCMethodDecl *SetterMethod = 0;
1318 bool LookedUpGetterSetter = false;
1319
Ted Kremenek9d64c152010-03-12 00:38:38 +00001320 unsigned Attributes = Property->getPropertyAttributes();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001321 unsigned AttributesAsWrittern = Property->getPropertyAttributesAsWritten();
1322
Fariborz Jahanian45937ae2011-06-11 00:45:12 +00001323 if (!(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_atomic) &&
1324 !(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001325 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1326 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1327 LookedUpGetterSetter = true;
1328 if (GetterMethod) {
1329 Diag(GetterMethod->getLocation(),
1330 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001331 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001332 Diag(Property->getLocation(), diag::note_property_declare);
1333 }
1334 if (SetterMethod) {
1335 Diag(SetterMethod->getLocation(),
1336 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001337 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001338 Diag(Property->getLocation(), diag::note_property_declare);
1339 }
1340 }
1341
Ted Kremenek9d64c152010-03-12 00:38:38 +00001342 // We only care about readwrite atomic property.
1343 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1344 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1345 continue;
1346 if (const ObjCPropertyImplDecl *PIDecl
1347 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1348 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1349 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001350 if (!LookedUpGetterSetter) {
1351 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1352 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1353 LookedUpGetterSetter = true;
1354 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001355 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1356 SourceLocation MethodLoc =
1357 (GetterMethod ? GetterMethod->getLocation()
1358 : SetterMethod->getLocation());
1359 Diag(MethodLoc, diag::warn_atomic_property_rule)
1360 << Property->getIdentifier();
1361 Diag(Property->getLocation(), diag::note_property_declare);
1362 }
1363 }
1364 }
1365}
1366
John McCallf85e1932011-06-15 23:02:42 +00001367void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
1368 if (getLangOptions().getGCMode() == LangOptions::GCOnly)
1369 return;
1370
1371 for (ObjCImplementationDecl::propimpl_iterator
1372 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1373 ObjCPropertyImplDecl *PID = *i;
1374 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1375 continue;
1376
1377 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001378 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1379 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001380 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1381 if (!method)
1382 continue;
1383 ObjCMethodFamily family = method->getMethodFamily();
1384 if (family == OMF_alloc || family == OMF_copy ||
1385 family == OMF_mutableCopy || family == OMF_new) {
1386 if (getLangOptions().ObjCAutoRefCount)
1387 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1388 else
1389 Diag(PID->getLocation(), diag::warn_ownin_getter_rule);
1390 Diag(PD->getLocation(), diag::note_property_declare);
1391 }
1392 }
1393 }
1394}
1395
John McCall5de74d12010-11-10 07:01:40 +00001396/// AddPropertyAttrs - Propagates attributes from a property to the
1397/// implicitly-declared getter or setter for that property.
1398static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1399 ObjCPropertyDecl *Property) {
1400 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001401 for (Decl::attr_iterator A = Property->attr_begin(),
1402 AEnd = Property->attr_end();
1403 A != AEnd; ++A) {
1404 if (isa<DeprecatedAttr>(*A) ||
1405 isa<UnavailableAttr>(*A) ||
1406 isa<AvailabilityAttr>(*A))
1407 PropertyMethod->addAttr((*A)->clone(S.Context));
1408 }
John McCall5de74d12010-11-10 07:01:40 +00001409}
1410
Ted Kremenek9d64c152010-03-12 00:38:38 +00001411/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1412/// have the property type and issue diagnostics if they don't.
1413/// Also synthesize a getter/setter method if none exist (and update the
1414/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1415/// methods is the "right" thing to do.
1416void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001417 ObjCContainerDecl *CD,
1418 ObjCPropertyDecl *redeclaredProperty,
1419 ObjCContainerDecl *lexicalDC) {
1420
Ted Kremenek9d64c152010-03-12 00:38:38 +00001421 ObjCMethodDecl *GetterMethod, *SetterMethod;
1422
1423 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1424 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1425 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1426 property->getLocation());
1427
1428 if (SetterMethod) {
1429 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1430 property->getPropertyAttributes();
1431 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1432 Context.getCanonicalType(SetterMethod->getResultType()) !=
1433 Context.VoidTy)
1434 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1435 if (SetterMethod->param_size() != 1 ||
1436 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1437 Diag(property->getLocation(),
1438 diag::warn_accessor_property_type_mismatch)
1439 << property->getDeclName()
1440 << SetterMethod->getSelector();
1441 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1442 }
1443 }
1444
1445 // Synthesize getter/setter methods if none exist.
1446 // Find the default getter and if one not found, add one.
1447 // FIXME: The synthesized property we set here is misleading. We almost always
1448 // synthesize these methods unless the user explicitly provided prototypes
1449 // (which is odd, but allowed). Sema should be typechecking that the
1450 // declarations jive in that situation (which it is not currently).
1451 if (!GetterMethod) {
1452 // No instance method of same name as property getter name was found.
1453 // Declare a getter method and add it to the list of methods
1454 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001455 SourceLocation Loc = redeclaredProperty ?
1456 redeclaredProperty->getLocation() :
1457 property->getLocation();
1458
1459 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1460 property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001461 property->getType(), 0, CD, true, false, true,
1462 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001463 (property->getPropertyImplementation() ==
1464 ObjCPropertyDecl::Optional) ?
1465 ObjCMethodDecl::Optional :
1466 ObjCMethodDecl::Required);
1467 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001468
1469 AddPropertyAttrs(*this, GetterMethod, property);
1470
Ted Kremenek23173d72010-05-18 21:09:07 +00001471 // FIXME: Eventually this shouldn't be needed, as the lexical context
1472 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001473 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001474 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001475 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1476 GetterMethod->addAttr(
1477 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001478 } else
1479 // A user declared getter will be synthesize when @synthesize of
1480 // the property with the same name is seen in the @implementation
1481 GetterMethod->setSynthesized(true);
1482 property->setGetterMethodDecl(GetterMethod);
1483
1484 // Skip setter if property is read-only.
1485 if (!property->isReadOnly()) {
1486 // Find the default setter and if one not found, add one.
1487 if (!SetterMethod) {
1488 // No instance method of same name as property setter name was found.
1489 // Declare a setter method and add it to the list of methods
1490 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001491 SourceLocation Loc = redeclaredProperty ?
1492 redeclaredProperty->getLocation() :
1493 property->getLocation();
1494
1495 SetterMethod =
1496 ObjCMethodDecl::Create(Context, Loc, Loc,
1497 property->getSetterName(), Context.VoidTy, 0,
1498 CD, true, false, true, false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001499 (property->getPropertyImplementation() ==
1500 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001501 ObjCMethodDecl::Optional :
1502 ObjCMethodDecl::Required);
1503
Ted Kremenek9d64c152010-03-12 00:38:38 +00001504 // Invent the arguments for the setter. We don't bother making a
1505 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001506 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1507 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001508 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001509 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001510 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001511 SC_None,
1512 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001513 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001514 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001515
1516 AddPropertyAttrs(*this, SetterMethod, property);
1517
Ted Kremenek9d64c152010-03-12 00:38:38 +00001518 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001519 // FIXME: Eventually this shouldn't be needed, as the lexical context
1520 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001521 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001522 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001523 } else
1524 // A user declared setter will be synthesize when @synthesize of
1525 // the property with the same name is seen in the @implementation
1526 SetterMethod->setSynthesized(true);
1527 property->setSetterMethodDecl(SetterMethod);
1528 }
1529 // Add any synthesized methods to the global pool. This allows us to
1530 // handle the following, which is supported by GCC (and part of the design).
1531 //
1532 // @interface Foo
1533 // @property double bar;
1534 // @end
1535 //
1536 // void thisIsUnfortunate() {
1537 // id foo;
1538 // double bar = [foo bar];
1539 // }
1540 //
1541 if (GetterMethod)
1542 AddInstanceMethodToGlobalPool(GetterMethod);
1543 if (SetterMethod)
1544 AddInstanceMethodToGlobalPool(SetterMethod);
1545}
1546
John McCalld226f652010-08-21 09:40:31 +00001547void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001548 SourceLocation Loc,
1549 unsigned &Attributes) {
1550 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001551 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001552 return;
1553
1554 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001555 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001556
1557 // readonly and readwrite/assign/retain/copy conflict.
1558 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1559 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1560 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001561 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001562 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001563 ObjCDeclSpec::DQ_PR_retain |
1564 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001565 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1566 "readwrite" :
1567 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1568 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001569 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1570 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001571 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1572 "copy" : "retain";
1573
1574 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1575 diag::err_objc_property_attr_mutually_exclusive :
1576 diag::warn_objc_property_attr_mutually_exclusive)
1577 << "readonly" << which;
1578 }
1579
1580 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001581 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1582 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1583 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001584 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001585 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001586 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1587 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1588 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1589 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001590 }
1591
1592 // Check for more than one of { assign, copy, retain }.
1593 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1594 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1595 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1596 << "assign" << "copy";
1597 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1598 }
1599 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1600 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1601 << "assign" << "retain";
1602 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1603 }
John McCallf85e1932011-06-15 23:02:42 +00001604 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1605 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1606 << "assign" << "strong";
1607 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1608 }
1609 if (getLangOptions().ObjCAutoRefCount &&
1610 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1611 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1612 << "assign" << "weak";
1613 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1614 }
1615 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1616 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1617 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1618 << "unsafe_unretained" << "copy";
1619 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1620 }
1621 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1622 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1623 << "unsafe_unretained" << "retain";
1624 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1625 }
1626 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1627 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1628 << "unsafe_unretained" << "strong";
1629 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1630 }
1631 if (getLangOptions().ObjCAutoRefCount &&
1632 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1633 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1634 << "unsafe_unretained" << "weak";
1635 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1636 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001637 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1638 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1639 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1640 << "copy" << "retain";
1641 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1642 }
John McCallf85e1932011-06-15 23:02:42 +00001643 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1644 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1645 << "copy" << "strong";
1646 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1647 }
1648 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1649 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1650 << "copy" << "weak";
1651 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1652 }
1653 }
1654 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1655 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1656 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1657 << "retain" << "weak";
1658 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1659 }
1660 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1661 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1662 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1663 << "strong" << "weak";
1664 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001665 }
1666
1667 // Warn if user supplied no assignment attribute, property is
1668 // readwrite, and this is an object type.
1669 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001670 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1671 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1672 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001673 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1674 PropertyTy->isObjCObjectPointerType()) {
John McCallf85e1932011-06-15 23:02:42 +00001675 if (getLangOptions().ObjCAutoRefCount)
1676 Diag(Loc, diag::err_arc_objc_property_default_assign_on_object);
1677 else {
1678 // Skip this warning in gc-only mode.
1679 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1680 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001681
John McCallf85e1932011-06-15 23:02:42 +00001682 // If non-gc code warn that this is likely inappropriate.
1683 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1684 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1685 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001686
1687 // FIXME: Implement warning dependent on NSCopying being
1688 // implemented. See also:
1689 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1690 // (please trim this list while you are at it).
1691 }
1692
1693 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001694 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001695 && getLangOptions().getGCMode() == LangOptions::GCOnly
1696 && PropertyTy->isBlockPointerType())
1697 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1698}