blob: ddb7eebd864a1db3bd30e137396e9daa74dd89c8 [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,
John McCalld226f652010-08-21 09:40:31 +000077 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000078 tok::ObjCKeywordKind MethodImplKind,
79 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000080 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +000081 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
82 QualType T = TSI->getType();
83 if ((getLangOptions().getGCMode() != LangOptions::NonGC &&
84 T.isObjCGCWeak()) ||
85 (getLangOptions().ObjCAutoRefCount &&
86 T.getObjCLifetime() == Qualifiers::OCL_Weak))
87 Attributes |= ObjCDeclSpec::DQ_PR_weak;
88
Ted Kremenek28685ab2010-03-12 00:46:40 +000089 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
90 // default is readwrite!
91 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
92 // property is defaulted to 'assign' if it is readwrite and is
93 // not retain or copy
94 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
95 (isReadWrite &&
96 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +000097 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
98 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
99 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
100 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000101
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000102 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000103 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000104
Ted Kremenek28685ab2010-03-12 00:46:40 +0000105 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000106 if (CDecl->IsClassExtension()) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000107 Decl *Res = HandlePropertyInClassExtension(S, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000108 FD, GetterSel, SetterSel,
109 isAssign, isReadWrite,
110 Attributes,
111 isOverridingProperty, TSI,
112 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000113 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000114 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000115 if (getLangOptions().ObjCAutoRefCount)
116 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
117 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000118 return Res;
119 }
120
John McCallf85e1932011-06-15 23:02:42 +0000121 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
122 GetterSel, SetterSel,
123 isAssign, isReadWrite,
124 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000125 if (lexicalDC)
126 Res->setLexicalDeclContext(lexicalDC);
127
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000128 // Validate the attributes on the @property.
129 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000130
131 if (getLangOptions().ObjCAutoRefCount)
132 checkARCPropertyDecl(*this, Res);
133
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000134 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000135}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000136
John McCalld226f652010-08-21 09:40:31 +0000137Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000138Sema::HandlePropertyInClassExtension(Scope *S,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000139 SourceLocation AtLoc, FieldDeclarator &FD,
140 Selector GetterSel, Selector SetterSel,
141 const bool isAssign,
142 const bool isReadWrite,
143 const unsigned Attributes,
144 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000145 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000146 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000147 ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000148 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000149 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000150 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000151 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
152
153 if (CCPrimary)
154 // Check for duplicate declaration of this property in current and
155 // other class extensions.
156 for (const ObjCCategoryDecl *ClsExtDecl =
157 CCPrimary->getFirstClassExtension();
158 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
159 if (ObjCPropertyDecl *prevDecl =
160 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
161 Diag(AtLoc, diag::err_duplicate_property);
162 Diag(prevDecl->getLocation(), diag::note_property_declare);
163 return 0;
164 }
165 }
166
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000167 // Create a new ObjCPropertyDecl with the DeclContext being
168 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000169 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000170 ObjCPropertyDecl *PDecl =
171 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
172 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000173 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
174 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
175 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
176 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000177 // Set setter/getter selector name. Needed later.
178 PDecl->setGetterName(GetterSel);
179 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000180 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000181 DC->addDecl(PDecl);
182
183 // We need to look in the @interface to see if the @property was
184 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000185 if (!CCPrimary) {
186 Diag(CDecl->getLocation(), diag::err_continuation_class);
187 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000188 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000189 }
190
191 // Find the property in continuation class's primary class only.
192 ObjCPropertyDecl *PIDecl =
193 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
194
195 if (!PIDecl) {
196 // No matching property found in the primary class. Just fall thru
197 // and add property to continuation class's primary class.
198 ObjCPropertyDecl *PDecl =
199 CreatePropertyDecl(S, CCPrimary, AtLoc,
200 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000201 Attributes, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000202
203 // A case of continuation class adding a new property in the class. This
204 // is not what it was meant for. However, gcc supports it and so should we.
205 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000206 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
207 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000208 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000209 }
210
211 // The property 'PIDecl's readonly attribute will be over-ridden
212 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000213 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000214 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
215 unsigned retainCopyNonatomic =
216 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000217 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000218 ObjCPropertyDecl::OBJC_PR_copy |
219 ObjCPropertyDecl::OBJC_PR_nonatomic);
220 if ((Attributes & retainCopyNonatomic) !=
221 (PIkind & retainCopyNonatomic)) {
222 Diag(AtLoc, diag::warn_property_attr_mismatch);
223 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000224 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000225 DeclContext *DC = cast<DeclContext>(CCPrimary);
226 if (!ObjCPropertyDecl::findPropertyDecl(DC,
227 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000228 // Protocol is not in the primary class. Must build one for it.
229 ObjCDeclSpec ProtocolPropertyODS;
230 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
231 // and ObjCPropertyDecl::PropertyAttributeKind have identical
232 // values. Should consolidate both into one enum type.
233 ProtocolPropertyODS.
234 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
235 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000236 // Must re-establish the context from class extension to primary
237 // class context.
238 ActOnObjCContainerFinishDefinition(CDecl);
239 ActOnObjCContainerStartDefinition(CCPrimary);
John McCalld226f652010-08-21 09:40:31 +0000240 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000241 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
242 PIDecl->getGetterName(),
243 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000244 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000245 MethodImplKind,
246 /* lexicalDC = */ CDecl);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000247 // restore class extension context.
248 ActOnObjCContainerFinishDefinition(CCPrimary);
249 ActOnObjCContainerStartDefinition(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000250 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000251 }
252 PIDecl->makeitReadWriteAttribute();
253 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
254 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000255 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
256 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000257 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
258 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
259 PIDecl->setSetterName(SetterSel);
260 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000261 // Tailor the diagnostics for the common case where a readwrite
262 // property is declared both in the @interface and the continuation.
263 // This is a common error where the user often intended the original
264 // declaration to be readonly.
265 unsigned diag =
266 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
267 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
268 ? diag::err_use_continuation_class_redeclaration_readwrite
269 : diag::err_use_continuation_class;
270 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000271 << CCPrimary->getDeclName();
272 Diag(PIDecl->getLocation(), diag::note_property_declare);
273 }
274 *isOverridingProperty = true;
275 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000276 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000277 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000278}
279
280ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
281 ObjCContainerDecl *CDecl,
282 SourceLocation AtLoc,
283 FieldDeclarator &FD,
284 Selector GetterSel,
285 Selector SetterSel,
286 const bool isAssign,
287 const bool isReadWrite,
288 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000289 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000290 tok::ObjCKeywordKind MethodImplKind,
291 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000292 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000293 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000294
295 // Issue a warning if property is 'assign' as default and its object, which is
296 // gc'able conforms to NSCopying protocol
297 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
298 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000299 if (const ObjCObjectPointerType *ObjPtrTy =
300 T->getAs<ObjCObjectPointerType>()) {
301 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
302 if (IDecl)
303 if (ObjCProtocolDecl* PNSCopying =
304 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
305 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
306 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000307 }
John McCallc12c5bb2010-05-15 11:32:37 +0000308 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000309 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
310
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000311 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000312 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
313 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000314 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000315
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000316 if (ObjCPropertyDecl *prevDecl =
317 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000318 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000319 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000320 PDecl->setInvalidDecl();
321 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000322 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000323 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000324 if (lexicalDC)
325 PDecl->setLexicalDeclContext(lexicalDC);
326 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000327
328 if (T->isArrayType() || T->isFunctionType()) {
329 Diag(AtLoc, diag::err_property_type) << T;
330 PDecl->setInvalidDecl();
331 }
332
333 ProcessDeclAttributes(S, PDecl, FD.D);
334
335 // Regardless of setter/getter attribute, we save the default getter/setter
336 // selector names in anticipation of declaration of setter/getter methods.
337 PDecl->setGetterName(GetterSel);
338 PDecl->setSetterName(SetterSel);
339
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000340 unsigned attributesAsWritten = 0;
341 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
342 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
343 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
344 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
345 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
346 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
347 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
348 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
349 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
350 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
351 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
352 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
353 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
354 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
355 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
356 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
357 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
358 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
359 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
360 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
361 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
362 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
363 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
364 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
365
366 PDecl->setPropertyAttributesAsWritten(
367 (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten);
368
Ted Kremenek28685ab2010-03-12 00:46:40 +0000369 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
371
372 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
373 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
374
375 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
376 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
377
378 if (isReadWrite)
379 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
380
381 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
382 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
383
John McCallf85e1932011-06-15 23:02:42 +0000384 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
385 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
386
387 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
388 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
389
Ted Kremenek28685ab2010-03-12 00:46:40 +0000390 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
391 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
392
John McCallf85e1932011-06-15 23:02:42 +0000393 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
394 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
395
Ted Kremenek28685ab2010-03-12 00:46:40 +0000396 if (isAssign)
397 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
398
399 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
400 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000401 else if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
402 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000403
John McCallf85e1932011-06-15 23:02:42 +0000404 // 'unsafe_unretained' is alias for 'assign'.
405 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
406 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
407 if (isAssign)
408 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
409
Ted Kremenek28685ab2010-03-12 00:46:40 +0000410 if (MethodImplKind == tok::objc_required)
411 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
412 else if (MethodImplKind == tok::objc_optional)
413 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000414
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000415 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000416}
417
John McCallf85e1932011-06-15 23:02:42 +0000418static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
419 ObjCPropertyDecl *property,
420 ObjCIvarDecl *ivar) {
421 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
422
423 QualType propertyType = property->getType();
424 Qualifiers::ObjCLifetime propertyLifetime = propertyType.getObjCLifetime();
425 ObjCPropertyDecl::PropertyAttributeKind propertyKind
426 = property->getPropertyAttributes();
427
428 QualType ivarType = ivar->getType();
429 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
430
431 // Case 1: strong properties.
432 if (propertyLifetime == Qualifiers::OCL_Strong ||
433 (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
434 ObjCPropertyDecl::OBJC_PR_strong |
435 ObjCPropertyDecl::OBJC_PR_copy))) {
436 switch (ivarLifetime) {
437 case Qualifiers::OCL_Strong:
438 // Okay.
439 return;
440
441 case Qualifiers::OCL_None:
442 case Qualifiers::OCL_Autoreleasing:
443 // These aren't valid lifetimes for object ivars; don't diagnose twice.
444 return;
445
446 case Qualifiers::OCL_ExplicitNone:
447 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000448 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000449 << property->getDeclName()
450 << ivar->getDeclName()
451 << ivarLifetime;
452 break;
453 }
454
455 // Case 2: weak properties.
456 } else if (propertyLifetime == Qualifiers::OCL_Weak ||
457 (propertyKind & ObjCPropertyDecl::OBJC_PR_weak)) {
458 switch (ivarLifetime) {
459 case Qualifiers::OCL_Weak:
460 // Okay.
461 return;
462
463 case Qualifiers::OCL_None:
464 case Qualifiers::OCL_Autoreleasing:
465 // These aren't valid lifetimes for object ivars; don't diagnose twice.
466 return;
467
468 case Qualifiers::OCL_ExplicitNone:
469 case Qualifiers::OCL_Strong:
470 S.Diag(propertyImplLoc, diag::error_weak_property)
471 << property->getDeclName()
472 << ivar->getDeclName();
473 break;
474 }
475
476 // Case 3: assign properties.
477 } else if ((propertyKind & ObjCPropertyDecl::OBJC_PR_assign) &&
478 propertyType->isObjCRetainableType()) {
479 switch (ivarLifetime) {
480 case Qualifiers::OCL_ExplicitNone:
481 // Okay.
482 return;
483
484 case Qualifiers::OCL_None:
485 case Qualifiers::OCL_Autoreleasing:
486 // These aren't valid lifetimes for object ivars; don't diagnose twice.
487 return;
488
489 case Qualifiers::OCL_Weak:
490 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000491 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000492 << property->getDeclName()
493 << ivar->getDeclName();
494 break;
495 }
496
497 // Any other property should be ignored.
498 } else {
499 return;
500 }
501
502 S.Diag(property->getLocation(), diag::note_property_declare);
503}
504
Ted Kremenek28685ab2010-03-12 00:46:40 +0000505
506/// ActOnPropertyImplDecl - This routine performs semantic checks and
507/// builds the AST node for a property implementation declaration; declared
508/// as @synthesize or @dynamic.
509///
John McCalld226f652010-08-21 09:40:31 +0000510Decl *Sema::ActOnPropertyImplDecl(Scope *S,
511 SourceLocation AtLoc,
512 SourceLocation PropertyLoc,
513 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000514 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000515 IdentifierInfo *PropertyIvar,
516 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000517 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000518 cast_or_null<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000519 // Make sure we have a context for the property implementation declaration.
520 if (!ClassImpDecl) {
521 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000522 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000523 }
524 ObjCPropertyDecl *property = 0;
525 ObjCInterfaceDecl* IDecl = 0;
526 // Find the class or category class where this property must have
527 // a declaration.
528 ObjCImplementationDecl *IC = 0;
529 ObjCCategoryImplDecl* CatImplClass = 0;
530 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
531 IDecl = IC->getClassInterface();
532 // We always synthesize an interface for an implementation
533 // without an interface decl. So, IDecl is always non-zero.
534 assert(IDecl &&
535 "ActOnPropertyImplDecl - @implementation without @interface");
536
537 // Look for this property declaration in the @implementation's @interface
538 property = IDecl->FindPropertyDeclaration(PropertyId);
539 if (!property) {
540 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000541 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000542 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000543 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000544 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
545 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000546 if (AtLoc.isValid())
547 Diag(AtLoc, diag::warn_implicit_atomic_property);
548 else
549 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
550 Diag(property->getLocation(), diag::note_property_declare);
551 }
552
Ted Kremenek28685ab2010-03-12 00:46:40 +0000553 if (const ObjCCategoryDecl *CD =
554 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
555 if (!CD->IsClassExtension()) {
556 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
557 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000558 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000559 }
560 }
561 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
562 if (Synthesize) {
563 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000564 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000565 }
566 IDecl = CatImplClass->getClassInterface();
567 if (!IDecl) {
568 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000569 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000570 }
571 ObjCCategoryDecl *Category =
572 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
573
574 // If category for this implementation not found, it is an error which
575 // has already been reported eralier.
576 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000577 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000578 // Look for this property declaration in @implementation's category
579 property = Category->FindPropertyDeclaration(PropertyId);
580 if (!property) {
581 Diag(PropertyLoc, diag::error_bad_category_property_decl)
582 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000583 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000584 }
585 } else {
586 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000587 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000588 }
589 ObjCIvarDecl *Ivar = 0;
590 // Check that we have a valid, previously declared ivar for @synthesize
591 if (Synthesize) {
592 // @synthesize
593 if (!PropertyIvar)
594 PropertyIvar = PropertyId;
John McCallf85e1932011-06-15 23:02:42 +0000595 ObjCPropertyDecl::PropertyAttributeKind kind
596 = property->getPropertyAttributes();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000597 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian14086762011-03-28 23:47:18 +0000598 QualType PropertyIvarType = PropType;
599 if (PropType->isReferenceType())
600 PropertyIvarType = cast<ReferenceType>(PropType)->getPointeeType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000601 // Check that this is a previously declared 'ivar' in 'IDecl' interface
602 ObjCInterfaceDecl *ClassDeclared;
603 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
604 if (!Ivar) {
John McCallf85e1932011-06-15 23:02:42 +0000605 // In ARC, give the ivar a lifetime qualifier based on its
606 // property attributes.
607 if (getLangOptions().ObjCAutoRefCount &&
608 !PropertyIvarType.getObjCLifetime()) {
609
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000610 if (!property->hasWrittenStorageAttribute() &&
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +0000611 property->getType()->isObjCRetainableType() &&
612 !(kind & ObjCPropertyDecl::OBJC_PR_strong) ) {
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000613 Diag(PropertyLoc,
614 diag::err_arc_objc_property_default_assign_on_object);
615 Diag(property->getLocation(), diag::note_property_declare);
616 }
617
John McCallf85e1932011-06-15 23:02:42 +0000618 // retain/copy have retaining lifetime.
619 if (kind & (ObjCPropertyDecl::OBJC_PR_retain |
620 ObjCPropertyDecl::OBJC_PR_strong |
621 ObjCPropertyDecl::OBJC_PR_copy)) {
622 Qualifiers qs;
623 qs.addObjCLifetime(Qualifiers::OCL_Strong);
624 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
625 }
626 else if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
John McCall9f084a32011-07-06 00:26:06 +0000627 if (!getLangOptions().ObjCRuntimeHasWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000628 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
629 Diag(property->getLocation(), diag::note_property_declare);
630 }
631 Qualifiers qs;
632 qs.addObjCLifetime(Qualifiers::OCL_Weak);
633 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
634 }
635 else if (kind & ObjCPropertyDecl::OBJC_PR_assign &&
636 PropertyIvarType->isObjCRetainableType()) {
637 // assume that an 'assign' property synthesizes __unsafe_unretained
638 // ivar
639 Qualifiers qs;
640 qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
641 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
642 }
643 }
644
645 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
646 !getLangOptions().ObjCAutoRefCount &&
647 getLangOptions().getGCMode() == LangOptions::NonGC) {
648 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
649 Diag(property->getLocation(), diag::note_property_declare);
650 }
651
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000652 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
653 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000654 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000655 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000656 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000657 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000658 IDecl->makeDeclVisibleInContext(Ivar, false);
659 property->setPropertyIvarDecl(Ivar);
660
661 if (!getLangOptions().ObjCNonFragileABI)
662 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
663 // Note! I deliberately want it to fall thru so, we have a
664 // a property implementation and to avoid future warnings.
665 } else if (getLangOptions().ObjCNonFragileABI &&
666 ClassDeclared != IDecl) {
667 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
668 << property->getDeclName() << Ivar->getDeclName()
669 << ClassDeclared->getDeclName();
670 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000671 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000672 // Note! I deliberately want it to fall thru so more errors are caught.
673 }
674 QualType IvarType = Context.getCanonicalType(Ivar->getType());
675
676 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000677 if (PropertyIvarType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000678 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000679 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000680 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000681 compat =
682 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000683 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000684 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000685 else {
686 SourceLocation Loc = PropertyIvarLoc;
687 if (Loc.isInvalid())
688 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000689 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000690 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000691 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000692 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000693 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000694 << property->getDeclName() << PropType
695 << Ivar->getDeclName() << IvarType;
696 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000697 // Note! I deliberately want it to fall thru so, we have a
698 // a property implementation and to avoid future warnings.
699 }
700
701 // FIXME! Rules for properties are somewhat different that those
702 // for assignments. Use a new routine to consolidate all cases;
703 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000704 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000705 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
706 if (lhsType != rhsType &&
707 lhsType->isArithmeticType()) {
708 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000709 << property->getDeclName() << PropType
710 << Ivar->getDeclName() << IvarType;
711 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000712 // Fall thru - see previous comment
713 }
714 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000715 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
716 getLangOptions().getGCMode() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000717 Diag(PropertyLoc, diag::error_weak_property)
718 << property->getDeclName() << Ivar->getDeclName();
719 // Fall thru - see previous comment
720 }
John McCallf85e1932011-06-15 23:02:42 +0000721 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000722 if ((property->getType()->isObjCObjectPointerType() ||
723 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
724 getLangOptions().getGCMode() != LangOptions::NonGC) {
725 Diag(PropertyLoc, diag::error_strong_property)
726 << property->getDeclName() << Ivar->getDeclName();
727 // Fall thru - see previous comment
728 }
729 }
John McCallf85e1932011-06-15 23:02:42 +0000730 if (getLangOptions().ObjCAutoRefCount)
731 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000732 } else if (PropertyIvar)
733 // @dynamic
734 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000735
Ted Kremenek28685ab2010-03-12 00:46:40 +0000736 assert (property && "ActOnPropertyImplDecl - property declaration missing");
737 ObjCPropertyImplDecl *PIDecl =
738 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
739 property,
740 (Synthesize ?
741 ObjCPropertyImplDecl::Synthesize
742 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000743 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000744 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
745 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000746 if (getLangOptions().CPlusPlus && Synthesize &&
747 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000748 // For Objective-C++, need to synthesize the AST for the IVAR object to be
749 // returned by the getter as it must conform to C++'s copy-return rules.
750 // FIXME. Eventually we want to do this for Objective-C as well.
751 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
752 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000753 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
754 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000755 Expr *IvarRefExpr =
756 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
757 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000758 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000759 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000760 SourceLocation(),
761 getterMethod->getResultType(),
762 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000763 SourceLocation(),
764 Owned(IvarRefExpr));
765 if (!Res.isInvalid()) {
766 Expr *ResExpr = Res.takeAs<Expr>();
767 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000768 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000769 PIDecl->setGetterCXXConstructor(ResExpr);
770 }
771 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000772 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
773 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
774 Diag(getterMethod->getLocation(),
775 diag::warn_property_getter_owning_mismatch);
776 Diag(property->getLocation(), diag::note_property_declare);
777 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000778 }
779 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
780 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000781 if (getLangOptions().CPlusPlus && Synthesize
782 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000783 // FIXME. Eventually we want to do this for Objective-C as well.
784 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
785 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000786 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
787 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000788 Expr *lhs =
789 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
790 SelfExpr, true, true);
791 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
792 ParmVarDecl *Param = (*P);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000793 QualType T = Param->getType();
794 if (T->isReferenceType())
Fariborz Jahanian61750f22011-03-30 16:59:30 +0000795 T = T->getAs<ReferenceType>()->getPointeeType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000796 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000797 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000798 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000799 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000800 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
801 }
802 }
803
Ted Kremenek28685ab2010-03-12 00:46:40 +0000804 if (IC) {
805 if (Synthesize)
806 if (ObjCPropertyImplDecl *PPIDecl =
807 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
808 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
809 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
810 << PropertyIvar;
811 Diag(PPIDecl->getLocation(), diag::note_previous_use);
812 }
813
814 if (ObjCPropertyImplDecl *PPIDecl
815 = IC->FindPropertyImplDecl(PropertyId)) {
816 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
817 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000818 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000819 }
820 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000821 if (getLangOptions().ObjCDefaultSynthProperties &&
822 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000823 // Diagnose if an ivar was lazily synthesdized due to a previous
824 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000825 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000826 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000827 ObjCIvarDecl *Ivar = 0;
828 if (!Synthesize)
829 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
830 else {
831 if (PropertyIvar && PropertyIvar != PropertyId)
832 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
833 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000834 // Issue diagnostics only if Ivar belongs to current class.
835 if (Ivar && Ivar->getSynthesize() &&
836 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000837 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
838 << PropertyId;
839 Ivar->setInvalidDecl();
840 }
841 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000842 } else {
843 if (Synthesize)
844 if (ObjCPropertyImplDecl *PPIDecl =
845 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
846 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
847 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
848 << PropertyIvar;
849 Diag(PPIDecl->getLocation(), diag::note_previous_use);
850 }
851
852 if (ObjCPropertyImplDecl *PPIDecl =
853 CatImplClass->FindPropertyImplDecl(PropertyId)) {
854 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
855 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000856 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000857 }
858 CatImplClass->addPropertyImplementation(PIDecl);
859 }
860
John McCalld226f652010-08-21 09:40:31 +0000861 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000862}
863
864//===----------------------------------------------------------------------===//
865// Helper methods.
866//===----------------------------------------------------------------------===//
867
Ted Kremenek9d64c152010-03-12 00:38:38 +0000868/// DiagnosePropertyMismatch - Compares two properties for their
869/// attributes and types and warns on a variety of inconsistencies.
870///
871void
872Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
873 ObjCPropertyDecl *SuperProperty,
874 const IdentifierInfo *inheritedName) {
875 ObjCPropertyDecl::PropertyAttributeKind CAttr =
876 Property->getPropertyAttributes();
877 ObjCPropertyDecl::PropertyAttributeKind SAttr =
878 SuperProperty->getPropertyAttributes();
879 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
880 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
881 Diag(Property->getLocation(), diag::warn_readonly_property)
882 << Property->getDeclName() << inheritedName;
883 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
884 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
885 Diag(Property->getLocation(), diag::warn_property_attribute)
886 << Property->getDeclName() << "copy" << inheritedName;
John McCallf85e1932011-06-15 23:02:42 +0000887 else {
888 unsigned CAttrRetain =
889 (CAttr &
890 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
891 unsigned SAttrRetain =
892 (SAttr &
893 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
894 bool CStrong = (CAttrRetain != 0);
895 bool SStrong = (SAttrRetain != 0);
896 if (CStrong != SStrong)
897 Diag(Property->getLocation(), diag::warn_property_attribute)
898 << Property->getDeclName() << "retain (or strong)" << inheritedName;
899 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000900
901 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
902 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
903 Diag(Property->getLocation(), diag::warn_property_attribute)
904 << Property->getDeclName() << "atomic" << inheritedName;
905 if (Property->getSetterName() != SuperProperty->getSetterName())
906 Diag(Property->getLocation(), diag::warn_property_attribute)
907 << Property->getDeclName() << "setter" << inheritedName;
908 if (Property->getGetterName() != SuperProperty->getGetterName())
909 Diag(Property->getLocation(), diag::warn_property_attribute)
910 << Property->getDeclName() << "getter" << inheritedName;
911
912 QualType LHSType =
913 Context.getCanonicalType(SuperProperty->getType());
914 QualType RHSType =
915 Context.getCanonicalType(Property->getType());
916
Fariborz Jahanianc286f382011-07-12 22:05:16 +0000917 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000918 // Do cases not handled in above.
919 // FIXME. For future support of covariant property types, revisit this.
920 bool IncompatibleObjC = false;
921 QualType ConvertedType;
922 if (!isObjCPointerConversion(RHSType, LHSType,
923 ConvertedType, IncompatibleObjC) ||
924 IncompatibleObjC)
925 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
926 << Property->getType() << SuperProperty->getType() << inheritedName;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000927 }
928}
929
930bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
931 ObjCMethodDecl *GetterMethod,
932 SourceLocation Loc) {
933 if (GetterMethod &&
934 GetterMethod->getResultType() != property->getType()) {
935 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000936 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000937 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000938 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000939 if (result != Compatible) {
940 Diag(Loc, diag::warn_accessor_property_type_mismatch)
941 << property->getDeclName()
942 << GetterMethod->getSelector();
943 Diag(GetterMethod->getLocation(), diag::note_declared_at);
944 return true;
945 }
946 }
947 return false;
948}
949
950/// ComparePropertiesInBaseAndSuper - This routine compares property
951/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000952/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000953///
954void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
955 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
956 if (!SDecl)
957 return;
958 // FIXME: O(N^2)
959 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
960 E = SDecl->prop_end(); S != E; ++S) {
961 ObjCPropertyDecl *SuperPDecl = (*S);
962 // Does property in super class has declaration in current class?
963 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
964 E = IDecl->prop_end(); I != E; ++I) {
965 ObjCPropertyDecl *PDecl = (*I);
966 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
967 DiagnosePropertyMismatch(PDecl, SuperPDecl,
968 SDecl->getIdentifier());
969 }
970 }
971}
972
973/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
974/// of properties declared in a protocol and compares their attribute against
975/// the same property declared in the class or category.
976void
977Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
978 ObjCProtocolDecl *PDecl) {
979 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
980 if (!IDecl) {
981 // Category
982 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
983 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
984 if (!CatDecl->IsClassExtension())
985 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
986 E = PDecl->prop_end(); P != E; ++P) {
987 ObjCPropertyDecl *Pr = (*P);
988 ObjCCategoryDecl::prop_iterator CP, CE;
989 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000990 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000991 if ((*CP)->getIdentifier() == Pr->getIdentifier())
992 break;
993 if (CP != CE)
994 // Property protocol already exist in class. Diagnose any mismatch.
995 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
996 }
997 return;
998 }
999 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1000 E = PDecl->prop_end(); P != E; ++P) {
1001 ObjCPropertyDecl *Pr = (*P);
1002 ObjCInterfaceDecl::prop_iterator CP, CE;
1003 // Is this property already in class's list of properties?
1004 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1005 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1006 break;
1007 if (CP != CE)
1008 // Property protocol already exist in class. Diagnose any mismatch.
1009 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1010 }
1011}
1012
1013/// CompareProperties - This routine compares properties
1014/// declared in 'ClassOrProtocol' objects (which can be a class or an
1015/// inherited protocol with the list of properties for class/category 'CDecl'
1016///
John McCalld226f652010-08-21 09:40:31 +00001017void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1018 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001019 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1020
1021 if (!IDecl) {
1022 // Category
1023 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1024 assert (CatDecl && "CompareProperties");
1025 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1026 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1027 E = MDecl->protocol_end(); P != E; ++P)
1028 // Match properties of category with those of protocol (*P)
1029 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1030
1031 // Go thru the list of protocols for this category and recursively match
1032 // their properties with those in the category.
1033 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1034 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001035 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001036 } else {
1037 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1038 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1039 E = MD->protocol_end(); P != E; ++P)
1040 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1041 }
1042 return;
1043 }
1044
1045 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001046 for (ObjCInterfaceDecl::all_protocol_iterator
1047 P = MDecl->all_referenced_protocol_begin(),
1048 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001049 // Match properties of class IDecl with those of protocol (*P).
1050 MatchOneProtocolPropertiesInClass(IDecl, *P);
1051
1052 // Go thru the list of protocols for this class and recursively match
1053 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001054 for (ObjCInterfaceDecl::all_protocol_iterator
1055 P = IDecl->all_referenced_protocol_begin(),
1056 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001057 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001058 } else {
1059 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1060 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1061 E = MD->protocol_end(); P != E; ++P)
1062 MatchOneProtocolPropertiesInClass(IDecl, *P);
1063 }
1064}
1065
1066/// isPropertyReadonly - Return true if property is readonly, by searching
1067/// for the property in the class and in its categories and implementations
1068///
1069bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1070 ObjCInterfaceDecl *IDecl) {
1071 // by far the most common case.
1072 if (!PDecl->isReadOnly())
1073 return false;
1074 // Even if property is ready only, if interface has a user defined setter,
1075 // it is not considered read only.
1076 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1077 return false;
1078
1079 // Main class has the property as 'readonly'. Must search
1080 // through the category list to see if the property's
1081 // attribute has been over-ridden to 'readwrite'.
1082 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1083 Category; Category = Category->getNextClassCategory()) {
1084 // Even if property is ready only, if a category has a user defined setter,
1085 // it is not considered read only.
1086 if (Category->getInstanceMethod(PDecl->getSetterName()))
1087 return false;
1088 ObjCPropertyDecl *P =
1089 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1090 if (P && !P->isReadOnly())
1091 return false;
1092 }
1093
1094 // Also, check for definition of a setter method in the implementation if
1095 // all else failed.
1096 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1097 if (ObjCImplementationDecl *IMD =
1098 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1099 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1100 return false;
1101 } else if (ObjCCategoryImplDecl *CIMD =
1102 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1103 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1104 return false;
1105 }
1106 }
1107 // Lastly, look through the implementation (if one is in scope).
1108 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1109 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1110 return false;
1111 // If all fails, look at the super class.
1112 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1113 return isPropertyReadonly(PDecl, SIDecl);
1114 return true;
1115}
1116
1117/// CollectImmediateProperties - This routine collects all properties in
1118/// the class and its conforming protocols; but not those it its super class.
1119void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001120 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1121 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001122 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1123 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1124 E = IDecl->prop_end(); P != E; ++P) {
1125 ObjCPropertyDecl *Prop = (*P);
1126 PropMap[Prop->getIdentifier()] = Prop;
1127 }
1128 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001129 for (ObjCInterfaceDecl::all_protocol_iterator
1130 PI = IDecl->all_referenced_protocol_begin(),
1131 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001132 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001133 }
1134 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1135 if (!CATDecl->IsClassExtension())
1136 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1137 E = CATDecl->prop_end(); P != E; ++P) {
1138 ObjCPropertyDecl *Prop = (*P);
1139 PropMap[Prop->getIdentifier()] = Prop;
1140 }
1141 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001142 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001143 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001144 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001145 }
1146 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1147 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1148 E = PDecl->prop_end(); P != E; ++P) {
1149 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001150 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1151 // Exclude property for protocols which conform to class's super-class,
1152 // as super-class has to implement the property.
1153 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
1154 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1155 if (!PropEntry)
1156 PropEntry = Prop;
1157 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001158 }
1159 // scan through protocol's protocols.
1160 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1161 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001162 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001163 }
1164}
1165
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001166/// CollectClassPropertyImplementations - This routine collects list of
1167/// properties to be implemented in the class. This includes, class's
1168/// and its conforming protocols' properties.
1169static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1170 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1171 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1172 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1173 E = IDecl->prop_end(); P != E; ++P) {
1174 ObjCPropertyDecl *Prop = (*P);
1175 PropMap[Prop->getIdentifier()] = Prop;
1176 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001177 for (ObjCInterfaceDecl::all_protocol_iterator
1178 PI = IDecl->all_referenced_protocol_begin(),
1179 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001180 CollectClassPropertyImplementations((*PI), PropMap);
1181 }
1182 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1183 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1184 E = PDecl->prop_end(); P != E; ++P) {
1185 ObjCPropertyDecl *Prop = (*P);
1186 PropMap[Prop->getIdentifier()] = Prop;
1187 }
1188 // scan through protocol's protocols.
1189 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1190 E = PDecl->protocol_end(); PI != E; ++PI)
1191 CollectClassPropertyImplementations((*PI), PropMap);
1192 }
1193}
1194
1195/// CollectSuperClassPropertyImplementations - This routine collects list of
1196/// properties to be implemented in super class(s) and also coming from their
1197/// conforming protocols.
1198static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1199 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1200 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1201 while (SDecl) {
1202 CollectClassPropertyImplementations(SDecl, PropMap);
1203 SDecl = SDecl->getSuperClass();
1204 }
1205 }
1206}
1207
Ted Kremenek9d64c152010-03-12 00:38:38 +00001208/// LookupPropertyDecl - Looks up a property in the current class and all
1209/// its protocols.
1210ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1211 IdentifierInfo *II) {
1212 if (const ObjCInterfaceDecl *IDecl =
1213 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1214 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1215 E = IDecl->prop_end(); P != E; ++P) {
1216 ObjCPropertyDecl *Prop = (*P);
1217 if (Prop->getIdentifier() == II)
1218 return Prop;
1219 }
1220 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001221 for (ObjCInterfaceDecl::all_protocol_iterator
1222 PI = IDecl->all_referenced_protocol_begin(),
1223 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001224 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1225 if (Prop)
1226 return Prop;
1227 }
1228 }
1229 else if (const ObjCProtocolDecl *PDecl =
1230 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1231 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1232 E = PDecl->prop_end(); P != E; ++P) {
1233 ObjCPropertyDecl *Prop = (*P);
1234 if (Prop->getIdentifier() == II)
1235 return Prop;
1236 }
1237 // scan through protocol's protocols.
1238 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1239 E = PDecl->protocol_end(); PI != E; ++PI) {
1240 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1241 if (Prop)
1242 return Prop;
1243 }
1244 }
1245 return 0;
1246}
1247
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001248/// DefaultSynthesizeProperties - This routine default synthesizes all
1249/// properties which must be synthesized in class's @implementation.
1250void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
1251 ObjCInterfaceDecl *IDecl) {
1252
1253 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1254 CollectClassPropertyImplementations(IDecl, PropMap);
1255 if (PropMap.empty())
1256 return;
1257 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1258 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1259
1260 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1261 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1262 ObjCPropertyDecl *Prop = P->second;
1263 // If property to be implemented in the super class, ignore.
1264 if (SuperPropMap[Prop->getIdentifier()])
1265 continue;
1266 // Is there a matching propery synthesize/dynamic?
1267 if (Prop->isInvalidDecl() ||
1268 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1269 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1270 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001271 // Property may have been synthesized by user.
1272 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1273 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001274 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1275 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1276 continue;
1277 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1278 continue;
1279 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001280
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001281
1282 // We use invalid SourceLocations for the synthesized ivars since they
1283 // aren't really synthesized at a particular location; they just exist.
1284 // Saying that they are located at the @implementation isn't really going
1285 // to help users.
1286 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001287 true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001288 Prop->getIdentifier(), Prop->getIdentifier(),
1289 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001290 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001291}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001292
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001293void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001294 ObjCContainerDecl *CDecl,
1295 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001296 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1297 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1298 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1299
Ted Kremenek9d64c152010-03-12 00:38:38 +00001300 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001301 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001302 if (PropMap.empty())
1303 return;
1304
1305 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1306 for (ObjCImplDecl::propimpl_iterator
1307 I = IMPDecl->propimpl_begin(),
1308 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1309 PropImplMap.insert((*I)->getPropertyDecl());
1310
1311 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1312 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1313 ObjCPropertyDecl *Prop = P->second;
1314 // Is there a matching propery synthesize/dynamic?
1315 if (Prop->isInvalidDecl() ||
1316 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001317 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001318 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001319 if (!InsMap.count(Prop->getGetterName())) {
1320 Diag(Prop->getLocation(),
1321 isa<ObjCCategoryDecl>(CDecl) ?
1322 diag::warn_setter_getter_impl_required_in_category :
1323 diag::warn_setter_getter_impl_required)
1324 << Prop->getDeclName() << Prop->getGetterName();
1325 Diag(IMPDecl->getLocation(),
1326 diag::note_property_impl_required);
1327 }
1328
1329 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1330 Diag(Prop->getLocation(),
1331 isa<ObjCCategoryDecl>(CDecl) ?
1332 diag::warn_setter_getter_impl_required_in_category :
1333 diag::warn_setter_getter_impl_required)
1334 << Prop->getDeclName() << Prop->getSetterName();
1335 Diag(IMPDecl->getLocation(),
1336 diag::note_property_impl_required);
1337 }
1338 }
1339}
1340
1341void
1342Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1343 ObjCContainerDecl* IDecl) {
1344 // Rules apply in non-GC mode only
1345 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1346 return;
1347 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1348 E = IDecl->prop_end();
1349 I != E; ++I) {
1350 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001351 ObjCMethodDecl *GetterMethod = 0;
1352 ObjCMethodDecl *SetterMethod = 0;
1353 bool LookedUpGetterSetter = false;
1354
Ted Kremenek9d64c152010-03-12 00:38:38 +00001355 unsigned Attributes = Property->getPropertyAttributes();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001356 unsigned AttributesAsWrittern = Property->getPropertyAttributesAsWritten();
1357
Fariborz Jahanian45937ae2011-06-11 00:45:12 +00001358 if (!(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_atomic) &&
1359 !(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001360 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1361 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1362 LookedUpGetterSetter = true;
1363 if (GetterMethod) {
1364 Diag(GetterMethod->getLocation(),
1365 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001366 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001367 Diag(Property->getLocation(), diag::note_property_declare);
1368 }
1369 if (SetterMethod) {
1370 Diag(SetterMethod->getLocation(),
1371 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001372 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001373 Diag(Property->getLocation(), diag::note_property_declare);
1374 }
1375 }
1376
Ted Kremenek9d64c152010-03-12 00:38:38 +00001377 // We only care about readwrite atomic property.
1378 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1379 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1380 continue;
1381 if (const ObjCPropertyImplDecl *PIDecl
1382 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1383 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1384 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001385 if (!LookedUpGetterSetter) {
1386 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1387 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1388 LookedUpGetterSetter = true;
1389 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001390 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1391 SourceLocation MethodLoc =
1392 (GetterMethod ? GetterMethod->getLocation()
1393 : SetterMethod->getLocation());
1394 Diag(MethodLoc, diag::warn_atomic_property_rule)
1395 << Property->getIdentifier();
1396 Diag(Property->getLocation(), diag::note_property_declare);
1397 }
1398 }
1399 }
1400}
1401
John McCallf85e1932011-06-15 23:02:42 +00001402void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
1403 if (getLangOptions().getGCMode() == LangOptions::GCOnly)
1404 return;
1405
1406 for (ObjCImplementationDecl::propimpl_iterator
1407 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1408 ObjCPropertyImplDecl *PID = *i;
1409 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1410 continue;
1411
1412 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001413 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1414 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001415 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1416 if (!method)
1417 continue;
1418 ObjCMethodFamily family = method->getMethodFamily();
1419 if (family == OMF_alloc || family == OMF_copy ||
1420 family == OMF_mutableCopy || family == OMF_new) {
1421 if (getLangOptions().ObjCAutoRefCount)
1422 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1423 else
1424 Diag(PID->getLocation(), diag::warn_ownin_getter_rule);
1425 Diag(PD->getLocation(), diag::note_property_declare);
1426 }
1427 }
1428 }
1429}
1430
John McCall5de74d12010-11-10 07:01:40 +00001431/// AddPropertyAttrs - Propagates attributes from a property to the
1432/// implicitly-declared getter or setter for that property.
1433static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1434 ObjCPropertyDecl *Property) {
1435 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001436 for (Decl::attr_iterator A = Property->attr_begin(),
1437 AEnd = Property->attr_end();
1438 A != AEnd; ++A) {
1439 if (isa<DeprecatedAttr>(*A) ||
1440 isa<UnavailableAttr>(*A) ||
1441 isa<AvailabilityAttr>(*A))
1442 PropertyMethod->addAttr((*A)->clone(S.Context));
1443 }
John McCall5de74d12010-11-10 07:01:40 +00001444}
1445
Ted Kremenek9d64c152010-03-12 00:38:38 +00001446/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1447/// have the property type and issue diagnostics if they don't.
1448/// Also synthesize a getter/setter method if none exist (and update the
1449/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1450/// methods is the "right" thing to do.
1451void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001452 ObjCContainerDecl *CD,
1453 ObjCPropertyDecl *redeclaredProperty,
1454 ObjCContainerDecl *lexicalDC) {
1455
Ted Kremenek9d64c152010-03-12 00:38:38 +00001456 ObjCMethodDecl *GetterMethod, *SetterMethod;
1457
1458 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1459 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1460 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1461 property->getLocation());
1462
1463 if (SetterMethod) {
1464 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1465 property->getPropertyAttributes();
1466 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1467 Context.getCanonicalType(SetterMethod->getResultType()) !=
1468 Context.VoidTy)
1469 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1470 if (SetterMethod->param_size() != 1 ||
1471 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1472 Diag(property->getLocation(),
1473 diag::warn_accessor_property_type_mismatch)
1474 << property->getDeclName()
1475 << SetterMethod->getSelector();
1476 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1477 }
1478 }
1479
1480 // Synthesize getter/setter methods if none exist.
1481 // Find the default getter and if one not found, add one.
1482 // FIXME: The synthesized property we set here is misleading. We almost always
1483 // synthesize these methods unless the user explicitly provided prototypes
1484 // (which is odd, but allowed). Sema should be typechecking that the
1485 // declarations jive in that situation (which it is not currently).
1486 if (!GetterMethod) {
1487 // No instance method of same name as property getter name was found.
1488 // Declare a getter method and add it to the list of methods
1489 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001490 SourceLocation Loc = redeclaredProperty ?
1491 redeclaredProperty->getLocation() :
1492 property->getLocation();
1493
1494 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1495 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001496 property->getType(), 0, CD, /*isInstance=*/true,
1497 /*isVariadic=*/false, /*isSynthesized=*/true,
1498 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001499 (property->getPropertyImplementation() ==
1500 ObjCPropertyDecl::Optional) ?
1501 ObjCMethodDecl::Optional :
1502 ObjCMethodDecl::Required);
1503 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001504
1505 AddPropertyAttrs(*this, GetterMethod, property);
1506
Ted Kremenek23173d72010-05-18 21:09:07 +00001507 // FIXME: Eventually this shouldn't be needed, as the lexical context
1508 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001509 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001510 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001511 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1512 GetterMethod->addAttr(
1513 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001514 } else
1515 // A user declared getter will be synthesize when @synthesize of
1516 // the property with the same name is seen in the @implementation
1517 GetterMethod->setSynthesized(true);
1518 property->setGetterMethodDecl(GetterMethod);
1519
1520 // Skip setter if property is read-only.
1521 if (!property->isReadOnly()) {
1522 // Find the default setter and if one not found, add one.
1523 if (!SetterMethod) {
1524 // No instance method of same name as property setter name was found.
1525 // Declare a setter method and add it to the list of methods
1526 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001527 SourceLocation Loc = redeclaredProperty ?
1528 redeclaredProperty->getLocation() :
1529 property->getLocation();
1530
1531 SetterMethod =
1532 ObjCMethodDecl::Create(Context, Loc, Loc,
1533 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001534 CD, /*isInstance=*/true, /*isVariadic=*/false,
1535 /*isSynthesized=*/true,
1536 /*isImplicitlyDeclared=*/true,
1537 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001538 (property->getPropertyImplementation() ==
1539 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001540 ObjCMethodDecl::Optional :
1541 ObjCMethodDecl::Required);
1542
Ted Kremenek9d64c152010-03-12 00:38:38 +00001543 // Invent the arguments for the setter. We don't bother making a
1544 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001545 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1546 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001547 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001548 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001549 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001550 SC_None,
1551 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001552 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001553 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001554
1555 AddPropertyAttrs(*this, SetterMethod, property);
1556
Ted Kremenek9d64c152010-03-12 00:38:38 +00001557 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001558 // FIXME: Eventually this shouldn't be needed, as the lexical context
1559 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001560 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001561 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001562 } else
1563 // A user declared setter will be synthesize when @synthesize of
1564 // the property with the same name is seen in the @implementation
1565 SetterMethod->setSynthesized(true);
1566 property->setSetterMethodDecl(SetterMethod);
1567 }
1568 // Add any synthesized methods to the global pool. This allows us to
1569 // handle the following, which is supported by GCC (and part of the design).
1570 //
1571 // @interface Foo
1572 // @property double bar;
1573 // @end
1574 //
1575 // void thisIsUnfortunate() {
1576 // id foo;
1577 // double bar = [foo bar];
1578 // }
1579 //
1580 if (GetterMethod)
1581 AddInstanceMethodToGlobalPool(GetterMethod);
1582 if (SetterMethod)
1583 AddInstanceMethodToGlobalPool(SetterMethod);
1584}
1585
John McCalld226f652010-08-21 09:40:31 +00001586void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001587 SourceLocation Loc,
1588 unsigned &Attributes) {
1589 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001590 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001591 return;
1592
1593 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001594 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001595
1596 // readonly and readwrite/assign/retain/copy conflict.
1597 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1598 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1599 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001600 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001601 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001602 ObjCDeclSpec::DQ_PR_retain |
1603 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001604 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1605 "readwrite" :
1606 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1607 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001608 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1609 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001610 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1611 "copy" : "retain";
1612
1613 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1614 diag::err_objc_property_attr_mutually_exclusive :
1615 diag::warn_objc_property_attr_mutually_exclusive)
1616 << "readonly" << which;
1617 }
1618
1619 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001620 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1621 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1622 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001623 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001624 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001625 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1626 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1627 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1628 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001629 }
1630
1631 // Check for more than one of { assign, copy, retain }.
1632 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1633 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1634 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1635 << "assign" << "copy";
1636 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1637 }
1638 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1639 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1640 << "assign" << "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 << "assign" << "strong";
1646 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1647 }
1648 if (getLangOptions().ObjCAutoRefCount &&
1649 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1650 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1651 << "assign" << "weak";
1652 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1653 }
1654 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1655 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1656 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1657 << "unsafe_unretained" << "copy";
1658 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1659 }
1660 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1661 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1662 << "unsafe_unretained" << "retain";
1663 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1664 }
1665 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1666 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1667 << "unsafe_unretained" << "strong";
1668 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1669 }
1670 if (getLangOptions().ObjCAutoRefCount &&
1671 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1672 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1673 << "unsafe_unretained" << "weak";
1674 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1675 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001676 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1677 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1678 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1679 << "copy" << "retain";
1680 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1681 }
John McCallf85e1932011-06-15 23:02:42 +00001682 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1683 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1684 << "copy" << "strong";
1685 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1686 }
1687 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1688 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1689 << "copy" << "weak";
1690 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1691 }
1692 }
1693 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1694 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1695 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1696 << "retain" << "weak";
1697 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1698 }
1699 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1700 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1701 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1702 << "strong" << "weak";
1703 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001704 }
1705
1706 // Warn if user supplied no assignment attribute, property is
1707 // readwrite, and this is an object type.
1708 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001709 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1710 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1711 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001712 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1713 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001714 if (getLangOptions().ObjCAutoRefCount)
1715 // With arc, @property definitions should default to (strong) when
1716 // not specified
1717 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
1718 else {
1719 // Skip this warning in gc-only mode.
1720 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1721 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001722
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001723 // If non-gc code warn that this is likely inappropriate.
1724 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1725 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1726 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001727
1728 // FIXME: Implement warning dependent on NSCopying being
1729 // implemented. See also:
1730 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1731 // (please trim this list while you are at it).
1732 }
1733
1734 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001735 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001736 && getLangOptions().getGCMode() == LangOptions::GCOnly
1737 && PropertyTy->isBlockPointerType())
1738 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1739}