blob: 9f50dea233bc5ea4214b67fbdfe6cdaf633ac795 [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 Jahanian58a76492011-08-22 18:34:22 +0000147 ObjCCategoryDecl *CDecl = 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.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000238 ContextRAII SavedContext(*this, CCPrimary);
239
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);
John McCalld226f652010-08-21 09:40:31 +0000247 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000248 }
249 PIDecl->makeitReadWriteAttribute();
250 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
251 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000252 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
253 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000254 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
255 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
256 PIDecl->setSetterName(SetterSel);
257 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000258 // Tailor the diagnostics for the common case where a readwrite
259 // property is declared both in the @interface and the continuation.
260 // This is a common error where the user often intended the original
261 // declaration to be readonly.
262 unsigned diag =
263 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
264 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
265 ? diag::err_use_continuation_class_redeclaration_readwrite
266 : diag::err_use_continuation_class;
267 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000268 << CCPrimary->getDeclName();
269 Diag(PIDecl->getLocation(), diag::note_property_declare);
270 }
271 *isOverridingProperty = true;
272 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000273 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000274 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000275}
276
277ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
278 ObjCContainerDecl *CDecl,
279 SourceLocation AtLoc,
280 FieldDeclarator &FD,
281 Selector GetterSel,
282 Selector SetterSel,
283 const bool isAssign,
284 const bool isReadWrite,
285 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000286 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000287 tok::ObjCKeywordKind MethodImplKind,
288 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000289 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000290 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000291
292 // Issue a warning if property is 'assign' as default and its object, which is
293 // gc'able conforms to NSCopying protocol
294 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
295 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000296 if (const ObjCObjectPointerType *ObjPtrTy =
297 T->getAs<ObjCObjectPointerType>()) {
298 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
299 if (IDecl)
300 if (ObjCProtocolDecl* PNSCopying =
301 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
302 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
303 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000304 }
John McCallc12c5bb2010-05-15 11:32:37 +0000305 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000306 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
307
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000308 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000309 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
310 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000311 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000312
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000313 if (ObjCPropertyDecl *prevDecl =
314 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000315 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000316 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000317 PDecl->setInvalidDecl();
318 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000319 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000320 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000321 if (lexicalDC)
322 PDecl->setLexicalDeclContext(lexicalDC);
323 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000324
325 if (T->isArrayType() || T->isFunctionType()) {
326 Diag(AtLoc, diag::err_property_type) << T;
327 PDecl->setInvalidDecl();
328 }
329
330 ProcessDeclAttributes(S, PDecl, FD.D);
331
332 // Regardless of setter/getter attribute, we save the default getter/setter
333 // selector names in anticipation of declaration of setter/getter methods.
334 PDecl->setGetterName(GetterSel);
335 PDecl->setSetterName(SetterSel);
336
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000337 unsigned attributesAsWritten = 0;
338 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
339 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
340 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
341 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
342 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
343 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
344 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
345 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
346 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
347 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
348 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
349 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
350 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
351 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
352 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
353 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
354 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
355 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
356 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
357 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
358 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
359 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
360 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
361 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
362
363 PDecl->setPropertyAttributesAsWritten(
364 (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten);
365
Ted Kremenek28685ab2010-03-12 00:46:40 +0000366 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
367 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
368
369 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
371
372 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
373 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
374
375 if (isReadWrite)
376 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
377
378 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
379 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
380
John McCallf85e1932011-06-15 23:02:42 +0000381 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
382 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
383
384 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
385 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
386
Ted Kremenek28685ab2010-03-12 00:46:40 +0000387 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
388 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
389
John McCallf85e1932011-06-15 23:02:42 +0000390 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
391 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
392
Ted Kremenek28685ab2010-03-12 00:46:40 +0000393 if (isAssign)
394 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
395
396 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
397 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000398 else if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
399 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000400
John McCallf85e1932011-06-15 23:02:42 +0000401 // 'unsafe_unretained' is alias for 'assign'.
402 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
403 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
404 if (isAssign)
405 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
406
Ted Kremenek28685ab2010-03-12 00:46:40 +0000407 if (MethodImplKind == tok::objc_required)
408 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
409 else if (MethodImplKind == tok::objc_optional)
410 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000411
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000412 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000413}
414
John McCallf85e1932011-06-15 23:02:42 +0000415static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
416 ObjCPropertyDecl *property,
417 ObjCIvarDecl *ivar) {
418 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
419
420 QualType propertyType = property->getType();
421 Qualifiers::ObjCLifetime propertyLifetime = propertyType.getObjCLifetime();
422 ObjCPropertyDecl::PropertyAttributeKind propertyKind
423 = property->getPropertyAttributes();
424
425 QualType ivarType = ivar->getType();
426 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
427
428 // Case 1: strong properties.
429 if (propertyLifetime == Qualifiers::OCL_Strong ||
430 (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
431 ObjCPropertyDecl::OBJC_PR_strong |
432 ObjCPropertyDecl::OBJC_PR_copy))) {
433 switch (ivarLifetime) {
434 case Qualifiers::OCL_Strong:
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_Weak:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000445 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000446 << property->getDeclName()
447 << ivar->getDeclName()
448 << ivarLifetime;
449 break;
450 }
451
452 // Case 2: weak properties.
453 } else if (propertyLifetime == Qualifiers::OCL_Weak ||
454 (propertyKind & ObjCPropertyDecl::OBJC_PR_weak)) {
455 switch (ivarLifetime) {
456 case Qualifiers::OCL_Weak:
457 // Okay.
458 return;
459
460 case Qualifiers::OCL_None:
461 case Qualifiers::OCL_Autoreleasing:
462 // These aren't valid lifetimes for object ivars; don't diagnose twice.
463 return;
464
465 case Qualifiers::OCL_ExplicitNone:
466 case Qualifiers::OCL_Strong:
467 S.Diag(propertyImplLoc, diag::error_weak_property)
468 << property->getDeclName()
469 << ivar->getDeclName();
470 break;
471 }
472
473 // Case 3: assign properties.
474 } else if ((propertyKind & ObjCPropertyDecl::OBJC_PR_assign) &&
475 propertyType->isObjCRetainableType()) {
476 switch (ivarLifetime) {
477 case Qualifiers::OCL_ExplicitNone:
478 // Okay.
479 return;
480
481 case Qualifiers::OCL_None:
482 case Qualifiers::OCL_Autoreleasing:
483 // These aren't valid lifetimes for object ivars; don't diagnose twice.
484 return;
485
486 case Qualifiers::OCL_Weak:
487 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000488 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000489 << property->getDeclName()
Fariborz Jahanian94144492011-08-26 21:21:19 +0000490 << ivar->getDeclName()
491 << ((property->getPropertyAttributesAsWritten()
492 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
John McCallf85e1932011-06-15 23:02:42 +0000493 break;
494 }
495
496 // Any other property should be ignored.
497 } else {
498 return;
499 }
500
501 S.Diag(property->getLocation(), diag::note_property_declare);
502}
503
Ted Kremenek28685ab2010-03-12 00:46:40 +0000504
505/// ActOnPropertyImplDecl - This routine performs semantic checks and
506/// builds the AST node for a property implementation declaration; declared
507/// as @synthesize or @dynamic.
508///
John McCalld226f652010-08-21 09:40:31 +0000509Decl *Sema::ActOnPropertyImplDecl(Scope *S,
510 SourceLocation AtLoc,
511 SourceLocation PropertyLoc,
512 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000513 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000514 IdentifierInfo *PropertyIvar,
515 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000516 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000517 cast_or_null<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000518 // Make sure we have a context for the property implementation declaration.
519 if (!ClassImpDecl) {
520 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000521 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000522 }
523 ObjCPropertyDecl *property = 0;
524 ObjCInterfaceDecl* IDecl = 0;
525 // Find the class or category class where this property must have
526 // a declaration.
527 ObjCImplementationDecl *IC = 0;
528 ObjCCategoryImplDecl* CatImplClass = 0;
529 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
530 IDecl = IC->getClassInterface();
531 // We always synthesize an interface for an implementation
532 // without an interface decl. So, IDecl is always non-zero.
533 assert(IDecl &&
534 "ActOnPropertyImplDecl - @implementation without @interface");
535
536 // Look for this property declaration in the @implementation's @interface
537 property = IDecl->FindPropertyDeclaration(PropertyId);
538 if (!property) {
539 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000540 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000541 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000542 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000543 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
544 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000545 if (AtLoc.isValid())
546 Diag(AtLoc, diag::warn_implicit_atomic_property);
547 else
548 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
549 Diag(property->getLocation(), diag::note_property_declare);
550 }
551
Ted Kremenek28685ab2010-03-12 00:46:40 +0000552 if (const ObjCCategoryDecl *CD =
553 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
554 if (!CD->IsClassExtension()) {
555 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
556 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000557 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000558 }
559 }
560 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
561 if (Synthesize) {
562 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000563 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000564 }
565 IDecl = CatImplClass->getClassInterface();
566 if (!IDecl) {
567 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000568 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569 }
570 ObjCCategoryDecl *Category =
571 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
572
573 // If category for this implementation not found, it is an error which
574 // has already been reported eralier.
575 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000576 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000577 // Look for this property declaration in @implementation's category
578 property = Category->FindPropertyDeclaration(PropertyId);
579 if (!property) {
580 Diag(PropertyLoc, diag::error_bad_category_property_decl)
581 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000582 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000583 }
584 } else {
585 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000586 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000587 }
588 ObjCIvarDecl *Ivar = 0;
589 // Check that we have a valid, previously declared ivar for @synthesize
590 if (Synthesize) {
591 // @synthesize
592 if (!PropertyIvar)
593 PropertyIvar = PropertyId;
John McCallf85e1932011-06-15 23:02:42 +0000594 ObjCPropertyDecl::PropertyAttributeKind kind
595 = property->getPropertyAttributes();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000596 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000597
598 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
599 !getLangOptions().ObjCAutoRefCount &&
600 getLangOptions().getGCMode() != LangOptions::NonGC) {
601 if (PropType.isObjCGCStrong()) {
602 Diag(PropertyLoc,
603 diag::err_gc_weak_property_strong_type);
604 Diag(property->getLocation(), diag::note_property_declare);
605 }
606 else
607 PropType = Context.getObjCGCQualType(PropType, Qualifiers::Weak);
608 }
Fariborz Jahanian14086762011-03-28 23:47:18 +0000609 QualType PropertyIvarType = PropType;
610 if (PropType->isReferenceType())
611 PropertyIvarType = cast<ReferenceType>(PropType)->getPointeeType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000612 // Check that this is a previously declared 'ivar' in 'IDecl' interface
613 ObjCInterfaceDecl *ClassDeclared;
614 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
615 if (!Ivar) {
John McCallf85e1932011-06-15 23:02:42 +0000616 // In ARC, give the ivar a lifetime qualifier based on its
617 // property attributes.
618 if (getLangOptions().ObjCAutoRefCount &&
619 !PropertyIvarType.getObjCLifetime()) {
620
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000621 if (!property->hasWrittenStorageAttribute() &&
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +0000622 property->getType()->isObjCRetainableType() &&
623 !(kind & ObjCPropertyDecl::OBJC_PR_strong) ) {
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000624 Diag(PropertyLoc,
625 diag::err_arc_objc_property_default_assign_on_object);
626 Diag(property->getLocation(), diag::note_property_declare);
627 }
628
John McCallf85e1932011-06-15 23:02:42 +0000629 // retain/copy have retaining lifetime.
630 if (kind & (ObjCPropertyDecl::OBJC_PR_retain |
631 ObjCPropertyDecl::OBJC_PR_strong |
632 ObjCPropertyDecl::OBJC_PR_copy)) {
633 Qualifiers qs;
634 qs.addObjCLifetime(Qualifiers::OCL_Strong);
635 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
636 }
637 else if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
John McCall9f084a32011-07-06 00:26:06 +0000638 if (!getLangOptions().ObjCRuntimeHasWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000639 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
640 Diag(property->getLocation(), diag::note_property_declare);
641 }
642 Qualifiers qs;
643 qs.addObjCLifetime(Qualifiers::OCL_Weak);
644 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
645 }
646 else if (kind & ObjCPropertyDecl::OBJC_PR_assign &&
647 PropertyIvarType->isObjCRetainableType()) {
648 // assume that an 'assign' property synthesizes __unsafe_unretained
649 // ivar
650 Qualifiers qs;
651 qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
652 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
653 }
654 }
655
656 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
657 !getLangOptions().ObjCAutoRefCount &&
658 getLangOptions().getGCMode() == LangOptions::NonGC) {
659 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
660 Diag(property->getLocation(), diag::note_property_declare);
661 }
662
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000663 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
664 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000665 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000666 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000667 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000668 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000669 IDecl->makeDeclVisibleInContext(Ivar, false);
670 property->setPropertyIvarDecl(Ivar);
671
672 if (!getLangOptions().ObjCNonFragileABI)
673 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
674 // Note! I deliberately want it to fall thru so, we have a
675 // a property implementation and to avoid future warnings.
676 } else if (getLangOptions().ObjCNonFragileABI &&
677 ClassDeclared != IDecl) {
678 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
679 << property->getDeclName() << Ivar->getDeclName()
680 << ClassDeclared->getDeclName();
681 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000682 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000683 // Note! I deliberately want it to fall thru so more errors are caught.
684 }
685 QualType IvarType = Context.getCanonicalType(Ivar->getType());
686
687 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000688 if (PropertyIvarType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000689 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000690 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000691 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000692 compat =
693 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000694 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000695 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000696 else {
697 SourceLocation Loc = PropertyIvarLoc;
698 if (Loc.isInvalid())
699 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000700 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000701 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000702 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000703 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000704 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000705 << property->getDeclName() << PropType
706 << Ivar->getDeclName() << IvarType;
707 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000708 // Note! I deliberately want it to fall thru so, we have a
709 // a property implementation and to avoid future warnings.
710 }
711
712 // FIXME! Rules for properties are somewhat different that those
713 // for assignments. Use a new routine to consolidate all cases;
714 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000715 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000716 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
717 if (lhsType != rhsType &&
718 lhsType->isArithmeticType()) {
719 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000720 << property->getDeclName() << PropType
721 << Ivar->getDeclName() << IvarType;
722 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000723 // Fall thru - see previous comment
724 }
725 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000726 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
727 getLangOptions().getGCMode() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000728 Diag(PropertyLoc, diag::error_weak_property)
729 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000730 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000731 // Fall thru - see previous comment
732 }
John McCallf85e1932011-06-15 23:02:42 +0000733 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000734 if ((property->getType()->isObjCObjectPointerType() ||
735 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
736 getLangOptions().getGCMode() != LangOptions::NonGC) {
737 Diag(PropertyLoc, diag::error_strong_property)
738 << property->getDeclName() << Ivar->getDeclName();
739 // Fall thru - see previous comment
740 }
741 }
John McCallf85e1932011-06-15 23:02:42 +0000742 if (getLangOptions().ObjCAutoRefCount)
743 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000744 } else if (PropertyIvar)
745 // @dynamic
746 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000747
Ted Kremenek28685ab2010-03-12 00:46:40 +0000748 assert (property && "ActOnPropertyImplDecl - property declaration missing");
749 ObjCPropertyImplDecl *PIDecl =
750 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
751 property,
752 (Synthesize ?
753 ObjCPropertyImplDecl::Synthesize
754 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000755 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000756 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
757 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000758 if (getLangOptions().CPlusPlus && Synthesize &&
759 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000760 // For Objective-C++, need to synthesize the AST for the IVAR object to be
761 // returned by the getter as it must conform to C++'s copy-return rules.
762 // FIXME. Eventually we want to do this for Objective-C as well.
763 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
764 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000765 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
766 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000767 Expr *IvarRefExpr =
768 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
769 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000770 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000771 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000772 SourceLocation(),
773 getterMethod->getResultType(),
774 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000775 SourceLocation(),
776 Owned(IvarRefExpr));
777 if (!Res.isInvalid()) {
778 Expr *ResExpr = Res.takeAs<Expr>();
779 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000780 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000781 PIDecl->setGetterCXXConstructor(ResExpr);
782 }
783 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000784 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
785 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
786 Diag(getterMethod->getLocation(),
787 diag::warn_property_getter_owning_mismatch);
788 Diag(property->getLocation(), diag::note_property_declare);
789 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000790 }
791 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
792 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000793 if (getLangOptions().CPlusPlus && Synthesize
794 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000795 // FIXME. Eventually we want to do this for Objective-C as well.
796 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
797 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000798 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
799 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000800 Expr *lhs =
801 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
802 SelfExpr, true, true);
803 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
804 ParmVarDecl *Param = (*P);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000805 QualType T = Param->getType();
806 if (T->isReferenceType())
Fariborz Jahanian61750f22011-03-30 16:59:30 +0000807 T = T->getAs<ReferenceType>()->getPointeeType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000808 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000809 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000810 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000811 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000812 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
813 }
814 }
815
Ted Kremenek28685ab2010-03-12 00:46:40 +0000816 if (IC) {
817 if (Synthesize)
818 if (ObjCPropertyImplDecl *PPIDecl =
819 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
820 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
821 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
822 << PropertyIvar;
823 Diag(PPIDecl->getLocation(), diag::note_previous_use);
824 }
825
826 if (ObjCPropertyImplDecl *PPIDecl
827 = IC->FindPropertyImplDecl(PropertyId)) {
828 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
829 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000830 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000831 }
832 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000833 if (getLangOptions().ObjCDefaultSynthProperties &&
834 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000835 // Diagnose if an ivar was lazily synthesdized due to a previous
836 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000837 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000838 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000839 ObjCIvarDecl *Ivar = 0;
840 if (!Synthesize)
841 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
842 else {
843 if (PropertyIvar && PropertyIvar != PropertyId)
844 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
845 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000846 // Issue diagnostics only if Ivar belongs to current class.
847 if (Ivar && Ivar->getSynthesize() &&
848 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000849 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
850 << PropertyId;
851 Ivar->setInvalidDecl();
852 }
853 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000854 } else {
855 if (Synthesize)
856 if (ObjCPropertyImplDecl *PPIDecl =
857 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
858 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
859 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
860 << PropertyIvar;
861 Diag(PPIDecl->getLocation(), diag::note_previous_use);
862 }
863
864 if (ObjCPropertyImplDecl *PPIDecl =
865 CatImplClass->FindPropertyImplDecl(PropertyId)) {
866 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
867 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000868 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000869 }
870 CatImplClass->addPropertyImplementation(PIDecl);
871 }
872
John McCalld226f652010-08-21 09:40:31 +0000873 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000874}
875
876//===----------------------------------------------------------------------===//
877// Helper methods.
878//===----------------------------------------------------------------------===//
879
Ted Kremenek9d64c152010-03-12 00:38:38 +0000880/// DiagnosePropertyMismatch - Compares two properties for their
881/// attributes and types and warns on a variety of inconsistencies.
882///
883void
884Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
885 ObjCPropertyDecl *SuperProperty,
886 const IdentifierInfo *inheritedName) {
887 ObjCPropertyDecl::PropertyAttributeKind CAttr =
888 Property->getPropertyAttributes();
889 ObjCPropertyDecl::PropertyAttributeKind SAttr =
890 SuperProperty->getPropertyAttributes();
891 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
892 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
893 Diag(Property->getLocation(), diag::warn_readonly_property)
894 << Property->getDeclName() << inheritedName;
895 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
896 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
897 Diag(Property->getLocation(), diag::warn_property_attribute)
898 << Property->getDeclName() << "copy" << inheritedName;
John McCallf85e1932011-06-15 23:02:42 +0000899 else {
900 unsigned CAttrRetain =
901 (CAttr &
902 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
903 unsigned SAttrRetain =
904 (SAttr &
905 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
906 bool CStrong = (CAttrRetain != 0);
907 bool SStrong = (SAttrRetain != 0);
908 if (CStrong != SStrong)
909 Diag(Property->getLocation(), diag::warn_property_attribute)
910 << Property->getDeclName() << "retain (or strong)" << inheritedName;
911 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000912
913 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
914 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
915 Diag(Property->getLocation(), diag::warn_property_attribute)
916 << Property->getDeclName() << "atomic" << inheritedName;
917 if (Property->getSetterName() != SuperProperty->getSetterName())
918 Diag(Property->getLocation(), diag::warn_property_attribute)
919 << Property->getDeclName() << "setter" << inheritedName;
920 if (Property->getGetterName() != SuperProperty->getGetterName())
921 Diag(Property->getLocation(), diag::warn_property_attribute)
922 << Property->getDeclName() << "getter" << inheritedName;
923
924 QualType LHSType =
925 Context.getCanonicalType(SuperProperty->getType());
926 QualType RHSType =
927 Context.getCanonicalType(Property->getType());
928
Fariborz Jahanianc286f382011-07-12 22:05:16 +0000929 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000930 // Do cases not handled in above.
931 // FIXME. For future support of covariant property types, revisit this.
932 bool IncompatibleObjC = false;
933 QualType ConvertedType;
934 if (!isObjCPointerConversion(RHSType, LHSType,
935 ConvertedType, IncompatibleObjC) ||
936 IncompatibleObjC)
937 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
938 << Property->getType() << SuperProperty->getType() << inheritedName;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000939 }
940}
941
942bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
943 ObjCMethodDecl *GetterMethod,
944 SourceLocation Loc) {
945 if (GetterMethod &&
946 GetterMethod->getResultType() != property->getType()) {
947 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000948 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000949 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000950 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000951 if (result != Compatible) {
952 Diag(Loc, diag::warn_accessor_property_type_mismatch)
953 << property->getDeclName()
954 << GetterMethod->getSelector();
955 Diag(GetterMethod->getLocation(), diag::note_declared_at);
956 return true;
957 }
958 }
959 return false;
960}
961
962/// ComparePropertiesInBaseAndSuper - This routine compares property
963/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000964/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000965///
966void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
967 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
968 if (!SDecl)
969 return;
970 // FIXME: O(N^2)
971 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
972 E = SDecl->prop_end(); S != E; ++S) {
973 ObjCPropertyDecl *SuperPDecl = (*S);
974 // Does property in super class has declaration in current class?
975 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
976 E = IDecl->prop_end(); I != E; ++I) {
977 ObjCPropertyDecl *PDecl = (*I);
978 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
979 DiagnosePropertyMismatch(PDecl, SuperPDecl,
980 SDecl->getIdentifier());
981 }
982 }
983}
984
985/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
986/// of properties declared in a protocol and compares their attribute against
987/// the same property declared in the class or category.
988void
989Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
990 ObjCProtocolDecl *PDecl) {
991 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
992 if (!IDecl) {
993 // Category
994 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
995 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
996 if (!CatDecl->IsClassExtension())
997 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
998 E = PDecl->prop_end(); P != E; ++P) {
999 ObjCPropertyDecl *Pr = (*P);
1000 ObjCCategoryDecl::prop_iterator CP, CE;
1001 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001002 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001003 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1004 break;
1005 if (CP != CE)
1006 // Property protocol already exist in class. Diagnose any mismatch.
1007 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1008 }
1009 return;
1010 }
1011 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1012 E = PDecl->prop_end(); P != E; ++P) {
1013 ObjCPropertyDecl *Pr = (*P);
1014 ObjCInterfaceDecl::prop_iterator CP, CE;
1015 // Is this property already in class's list of properties?
1016 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1017 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1018 break;
1019 if (CP != CE)
1020 // Property protocol already exist in class. Diagnose any mismatch.
1021 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1022 }
1023}
1024
1025/// CompareProperties - This routine compares properties
1026/// declared in 'ClassOrProtocol' objects (which can be a class or an
1027/// inherited protocol with the list of properties for class/category 'CDecl'
1028///
John McCalld226f652010-08-21 09:40:31 +00001029void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1030 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001031 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1032
1033 if (!IDecl) {
1034 // Category
1035 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1036 assert (CatDecl && "CompareProperties");
1037 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1038 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1039 E = MDecl->protocol_end(); P != E; ++P)
1040 // Match properties of category with those of protocol (*P)
1041 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1042
1043 // Go thru the list of protocols for this category and recursively match
1044 // their properties with those in the category.
1045 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1046 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001047 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001048 } else {
1049 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1050 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1051 E = MD->protocol_end(); P != E; ++P)
1052 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1053 }
1054 return;
1055 }
1056
1057 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001058 for (ObjCInterfaceDecl::all_protocol_iterator
1059 P = MDecl->all_referenced_protocol_begin(),
1060 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001061 // Match properties of class IDecl with those of protocol (*P).
1062 MatchOneProtocolPropertiesInClass(IDecl, *P);
1063
1064 // Go thru the list of protocols for this class and recursively match
1065 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001066 for (ObjCInterfaceDecl::all_protocol_iterator
1067 P = IDecl->all_referenced_protocol_begin(),
1068 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001069 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001070 } else {
1071 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1072 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1073 E = MD->protocol_end(); P != E; ++P)
1074 MatchOneProtocolPropertiesInClass(IDecl, *P);
1075 }
1076}
1077
1078/// isPropertyReadonly - Return true if property is readonly, by searching
1079/// for the property in the class and in its categories and implementations
1080///
1081bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1082 ObjCInterfaceDecl *IDecl) {
1083 // by far the most common case.
1084 if (!PDecl->isReadOnly())
1085 return false;
1086 // Even if property is ready only, if interface has a user defined setter,
1087 // it is not considered read only.
1088 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1089 return false;
1090
1091 // Main class has the property as 'readonly'. Must search
1092 // through the category list to see if the property's
1093 // attribute has been over-ridden to 'readwrite'.
1094 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1095 Category; Category = Category->getNextClassCategory()) {
1096 // Even if property is ready only, if a category has a user defined setter,
1097 // it is not considered read only.
1098 if (Category->getInstanceMethod(PDecl->getSetterName()))
1099 return false;
1100 ObjCPropertyDecl *P =
1101 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1102 if (P && !P->isReadOnly())
1103 return false;
1104 }
1105
1106 // Also, check for definition of a setter method in the implementation if
1107 // all else failed.
1108 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1109 if (ObjCImplementationDecl *IMD =
1110 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1111 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1112 return false;
1113 } else if (ObjCCategoryImplDecl *CIMD =
1114 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1115 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1116 return false;
1117 }
1118 }
1119 // Lastly, look through the implementation (if one is in scope).
1120 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1121 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1122 return false;
1123 // If all fails, look at the super class.
1124 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1125 return isPropertyReadonly(PDecl, SIDecl);
1126 return true;
1127}
1128
1129/// CollectImmediateProperties - This routine collects all properties in
1130/// the class and its conforming protocols; but not those it its super class.
1131void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001132 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1133 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001134 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1135 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1136 E = IDecl->prop_end(); P != E; ++P) {
1137 ObjCPropertyDecl *Prop = (*P);
1138 PropMap[Prop->getIdentifier()] = Prop;
1139 }
1140 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001141 for (ObjCInterfaceDecl::all_protocol_iterator
1142 PI = IDecl->all_referenced_protocol_begin(),
1143 E = IDecl->all_referenced_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 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1147 if (!CATDecl->IsClassExtension())
1148 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1149 E = CATDecl->prop_end(); P != E; ++P) {
1150 ObjCPropertyDecl *Prop = (*P);
1151 PropMap[Prop->getIdentifier()] = Prop;
1152 }
1153 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001154 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001155 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001156 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001157 }
1158 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1159 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1160 E = PDecl->prop_end(); P != E; ++P) {
1161 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001162 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1163 // Exclude property for protocols which conform to class's super-class,
1164 // as super-class has to implement the property.
1165 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
1166 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1167 if (!PropEntry)
1168 PropEntry = Prop;
1169 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001170 }
1171 // scan through protocol's protocols.
1172 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1173 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001174 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001175 }
1176}
1177
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001178/// CollectClassPropertyImplementations - This routine collects list of
1179/// properties to be implemented in the class. This includes, class's
1180/// and its conforming protocols' properties.
1181static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1182 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1183 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1184 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1185 E = IDecl->prop_end(); P != E; ++P) {
1186 ObjCPropertyDecl *Prop = (*P);
1187 PropMap[Prop->getIdentifier()] = Prop;
1188 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001189 for (ObjCInterfaceDecl::all_protocol_iterator
1190 PI = IDecl->all_referenced_protocol_begin(),
1191 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001192 CollectClassPropertyImplementations((*PI), PropMap);
1193 }
1194 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1195 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1196 E = PDecl->prop_end(); P != E; ++P) {
1197 ObjCPropertyDecl *Prop = (*P);
1198 PropMap[Prop->getIdentifier()] = Prop;
1199 }
1200 // scan through protocol's protocols.
1201 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1202 E = PDecl->protocol_end(); PI != E; ++PI)
1203 CollectClassPropertyImplementations((*PI), PropMap);
1204 }
1205}
1206
1207/// CollectSuperClassPropertyImplementations - This routine collects list of
1208/// properties to be implemented in super class(s) and also coming from their
1209/// conforming protocols.
1210static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1211 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1212 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1213 while (SDecl) {
1214 CollectClassPropertyImplementations(SDecl, PropMap);
1215 SDecl = SDecl->getSuperClass();
1216 }
1217 }
1218}
1219
Ted Kremenek9d64c152010-03-12 00:38:38 +00001220/// LookupPropertyDecl - Looks up a property in the current class and all
1221/// its protocols.
1222ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1223 IdentifierInfo *II) {
1224 if (const ObjCInterfaceDecl *IDecl =
1225 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1226 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1227 E = IDecl->prop_end(); P != E; ++P) {
1228 ObjCPropertyDecl *Prop = (*P);
1229 if (Prop->getIdentifier() == II)
1230 return Prop;
1231 }
1232 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001233 for (ObjCInterfaceDecl::all_protocol_iterator
1234 PI = IDecl->all_referenced_protocol_begin(),
1235 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001236 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1237 if (Prop)
1238 return Prop;
1239 }
1240 }
1241 else if (const ObjCProtocolDecl *PDecl =
1242 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1243 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1244 E = PDecl->prop_end(); P != E; ++P) {
1245 ObjCPropertyDecl *Prop = (*P);
1246 if (Prop->getIdentifier() == II)
1247 return Prop;
1248 }
1249 // scan through protocol's protocols.
1250 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1251 E = PDecl->protocol_end(); PI != E; ++PI) {
1252 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1253 if (Prop)
1254 return Prop;
1255 }
1256 }
1257 return 0;
1258}
1259
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001260/// DefaultSynthesizeProperties - This routine default synthesizes all
1261/// properties which must be synthesized in class's @implementation.
1262void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
1263 ObjCInterfaceDecl *IDecl) {
1264
1265 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1266 CollectClassPropertyImplementations(IDecl, PropMap);
1267 if (PropMap.empty())
1268 return;
1269 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1270 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1271
1272 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1273 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1274 ObjCPropertyDecl *Prop = P->second;
1275 // If property to be implemented in the super class, ignore.
1276 if (SuperPropMap[Prop->getIdentifier()])
1277 continue;
1278 // Is there a matching propery synthesize/dynamic?
1279 if (Prop->isInvalidDecl() ||
1280 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1281 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1282 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001283 // Property may have been synthesized by user.
1284 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1285 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001286 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1287 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1288 continue;
1289 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1290 continue;
1291 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001292
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001293
1294 // We use invalid SourceLocations for the synthesized ivars since they
1295 // aren't really synthesized at a particular location; they just exist.
1296 // Saying that they are located at the @implementation isn't really going
1297 // to help users.
1298 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001299 true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001300 Prop->getIdentifier(), Prop->getIdentifier(),
1301 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001302 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001303}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001304
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001305void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1306 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1307 return;
1308 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1309 if (!IC)
1310 return;
1311 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1312 DefaultSynthesizeProperties(S, IC, IDecl);
1313}
1314
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001315void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001316 ObjCContainerDecl *CDecl,
1317 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001318 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1319 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1320 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1321
Ted Kremenek9d64c152010-03-12 00:38:38 +00001322 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001323 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001324 if (PropMap.empty())
1325 return;
1326
1327 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1328 for (ObjCImplDecl::propimpl_iterator
1329 I = IMPDecl->propimpl_begin(),
1330 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1331 PropImplMap.insert((*I)->getPropertyDecl());
1332
1333 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1334 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1335 ObjCPropertyDecl *Prop = P->second;
1336 // Is there a matching propery synthesize/dynamic?
1337 if (Prop->isInvalidDecl() ||
1338 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001339 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001340 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001341 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001342 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001343 isa<ObjCCategoryDecl>(CDecl) ?
1344 diag::warn_setter_getter_impl_required_in_category :
1345 diag::warn_setter_getter_impl_required)
1346 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001347 Diag(Prop->getLocation(),
1348 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001349 }
1350
1351 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001352 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001353 isa<ObjCCategoryDecl>(CDecl) ?
1354 diag::warn_setter_getter_impl_required_in_category :
1355 diag::warn_setter_getter_impl_required)
1356 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001357 Diag(Prop->getLocation(),
1358 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001359 }
1360 }
1361}
1362
1363void
1364Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1365 ObjCContainerDecl* IDecl) {
1366 // Rules apply in non-GC mode only
1367 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1368 return;
1369 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1370 E = IDecl->prop_end();
1371 I != E; ++I) {
1372 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001373 ObjCMethodDecl *GetterMethod = 0;
1374 ObjCMethodDecl *SetterMethod = 0;
1375 bool LookedUpGetterSetter = false;
1376
Ted Kremenek9d64c152010-03-12 00:38:38 +00001377 unsigned Attributes = Property->getPropertyAttributes();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001378 unsigned AttributesAsWrittern = Property->getPropertyAttributesAsWritten();
1379
Fariborz Jahanian45937ae2011-06-11 00:45:12 +00001380 if (!(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_atomic) &&
1381 !(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001382 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1383 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1384 LookedUpGetterSetter = true;
1385 if (GetterMethod) {
1386 Diag(GetterMethod->getLocation(),
1387 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001388 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001389 Diag(Property->getLocation(), diag::note_property_declare);
1390 }
1391 if (SetterMethod) {
1392 Diag(SetterMethod->getLocation(),
1393 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001394 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001395 Diag(Property->getLocation(), diag::note_property_declare);
1396 }
1397 }
1398
Ted Kremenek9d64c152010-03-12 00:38:38 +00001399 // We only care about readwrite atomic property.
1400 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1401 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1402 continue;
1403 if (const ObjCPropertyImplDecl *PIDecl
1404 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1405 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1406 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001407 if (!LookedUpGetterSetter) {
1408 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1409 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1410 LookedUpGetterSetter = true;
1411 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001412 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1413 SourceLocation MethodLoc =
1414 (GetterMethod ? GetterMethod->getLocation()
1415 : SetterMethod->getLocation());
1416 Diag(MethodLoc, diag::warn_atomic_property_rule)
1417 << Property->getIdentifier();
1418 Diag(Property->getLocation(), diag::note_property_declare);
1419 }
1420 }
1421 }
1422}
1423
John McCallf85e1932011-06-15 23:02:42 +00001424void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
1425 if (getLangOptions().getGCMode() == LangOptions::GCOnly)
1426 return;
1427
1428 for (ObjCImplementationDecl::propimpl_iterator
1429 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1430 ObjCPropertyImplDecl *PID = *i;
1431 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1432 continue;
1433
1434 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001435 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1436 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001437 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1438 if (!method)
1439 continue;
1440 ObjCMethodFamily family = method->getMethodFamily();
1441 if (family == OMF_alloc || family == OMF_copy ||
1442 family == OMF_mutableCopy || family == OMF_new) {
1443 if (getLangOptions().ObjCAutoRefCount)
1444 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1445 else
1446 Diag(PID->getLocation(), diag::warn_ownin_getter_rule);
1447 Diag(PD->getLocation(), diag::note_property_declare);
1448 }
1449 }
1450 }
1451}
1452
John McCall5de74d12010-11-10 07:01:40 +00001453/// AddPropertyAttrs - Propagates attributes from a property to the
1454/// implicitly-declared getter or setter for that property.
1455static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1456 ObjCPropertyDecl *Property) {
1457 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001458 for (Decl::attr_iterator A = Property->attr_begin(),
1459 AEnd = Property->attr_end();
1460 A != AEnd; ++A) {
1461 if (isa<DeprecatedAttr>(*A) ||
1462 isa<UnavailableAttr>(*A) ||
1463 isa<AvailabilityAttr>(*A))
1464 PropertyMethod->addAttr((*A)->clone(S.Context));
1465 }
John McCall5de74d12010-11-10 07:01:40 +00001466}
1467
Ted Kremenek9d64c152010-03-12 00:38:38 +00001468/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1469/// have the property type and issue diagnostics if they don't.
1470/// Also synthesize a getter/setter method if none exist (and update the
1471/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1472/// methods is the "right" thing to do.
1473void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001474 ObjCContainerDecl *CD,
1475 ObjCPropertyDecl *redeclaredProperty,
1476 ObjCContainerDecl *lexicalDC) {
1477
Ted Kremenek9d64c152010-03-12 00:38:38 +00001478 ObjCMethodDecl *GetterMethod, *SetterMethod;
1479
1480 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1481 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1482 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1483 property->getLocation());
1484
1485 if (SetterMethod) {
1486 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1487 property->getPropertyAttributes();
1488 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1489 Context.getCanonicalType(SetterMethod->getResultType()) !=
1490 Context.VoidTy)
1491 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1492 if (SetterMethod->param_size() != 1 ||
1493 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1494 Diag(property->getLocation(),
1495 diag::warn_accessor_property_type_mismatch)
1496 << property->getDeclName()
1497 << SetterMethod->getSelector();
1498 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1499 }
1500 }
1501
1502 // Synthesize getter/setter methods if none exist.
1503 // Find the default getter and if one not found, add one.
1504 // FIXME: The synthesized property we set here is misleading. We almost always
1505 // synthesize these methods unless the user explicitly provided prototypes
1506 // (which is odd, but allowed). Sema should be typechecking that the
1507 // declarations jive in that situation (which it is not currently).
1508 if (!GetterMethod) {
1509 // No instance method of same name as property getter name was found.
1510 // Declare a getter method and add it to the list of methods
1511 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001512 SourceLocation Loc = redeclaredProperty ?
1513 redeclaredProperty->getLocation() :
1514 property->getLocation();
1515
1516 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1517 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001518 property->getType(), 0, CD, /*isInstance=*/true,
1519 /*isVariadic=*/false, /*isSynthesized=*/true,
1520 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001521 (property->getPropertyImplementation() ==
1522 ObjCPropertyDecl::Optional) ?
1523 ObjCMethodDecl::Optional :
1524 ObjCMethodDecl::Required);
1525 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001526
1527 AddPropertyAttrs(*this, GetterMethod, property);
1528
Ted Kremenek23173d72010-05-18 21:09:07 +00001529 // FIXME: Eventually this shouldn't be needed, as the lexical context
1530 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001531 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001532 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001533 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1534 GetterMethod->addAttr(
1535 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001536 } else
1537 // A user declared getter will be synthesize when @synthesize of
1538 // the property with the same name is seen in the @implementation
1539 GetterMethod->setSynthesized(true);
1540 property->setGetterMethodDecl(GetterMethod);
1541
1542 // Skip setter if property is read-only.
1543 if (!property->isReadOnly()) {
1544 // Find the default setter and if one not found, add one.
1545 if (!SetterMethod) {
1546 // No instance method of same name as property setter name was found.
1547 // Declare a setter method and add it to the list of methods
1548 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001549 SourceLocation Loc = redeclaredProperty ?
1550 redeclaredProperty->getLocation() :
1551 property->getLocation();
1552
1553 SetterMethod =
1554 ObjCMethodDecl::Create(Context, Loc, Loc,
1555 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001556 CD, /*isInstance=*/true, /*isVariadic=*/false,
1557 /*isSynthesized=*/true,
1558 /*isImplicitlyDeclared=*/true,
1559 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001560 (property->getPropertyImplementation() ==
1561 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001562 ObjCMethodDecl::Optional :
1563 ObjCMethodDecl::Required);
1564
Ted Kremenek9d64c152010-03-12 00:38:38 +00001565 // Invent the arguments for the setter. We don't bother making a
1566 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001567 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1568 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001569 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001570 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001571 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001572 SC_None,
1573 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001574 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001575 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001576
1577 AddPropertyAttrs(*this, SetterMethod, property);
1578
Ted Kremenek9d64c152010-03-12 00:38:38 +00001579 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001580 // FIXME: Eventually this shouldn't be needed, as the lexical context
1581 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001582 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001583 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001584 } else
1585 // A user declared setter will be synthesize when @synthesize of
1586 // the property with the same name is seen in the @implementation
1587 SetterMethod->setSynthesized(true);
1588 property->setSetterMethodDecl(SetterMethod);
1589 }
1590 // Add any synthesized methods to the global pool. This allows us to
1591 // handle the following, which is supported by GCC (and part of the design).
1592 //
1593 // @interface Foo
1594 // @property double bar;
1595 // @end
1596 //
1597 // void thisIsUnfortunate() {
1598 // id foo;
1599 // double bar = [foo bar];
1600 // }
1601 //
1602 if (GetterMethod)
1603 AddInstanceMethodToGlobalPool(GetterMethod);
1604 if (SetterMethod)
1605 AddInstanceMethodToGlobalPool(SetterMethod);
1606}
1607
John McCalld226f652010-08-21 09:40:31 +00001608void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001609 SourceLocation Loc,
1610 unsigned &Attributes) {
1611 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001612 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001613 return;
1614
1615 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001616 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001617
1618 // readonly and readwrite/assign/retain/copy conflict.
1619 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1620 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1621 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001622 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001623 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001624 ObjCDeclSpec::DQ_PR_retain |
1625 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001626 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1627 "readwrite" :
1628 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1629 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001630 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1631 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001632 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1633 "copy" : "retain";
1634
1635 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1636 diag::err_objc_property_attr_mutually_exclusive :
1637 diag::warn_objc_property_attr_mutually_exclusive)
1638 << "readonly" << which;
1639 }
1640
1641 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001642 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1643 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1644 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001645 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001646 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001647 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1648 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1649 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1650 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001651 }
1652
1653 // Check for more than one of { assign, copy, retain }.
1654 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1655 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1656 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1657 << "assign" << "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 << "assign" << "retain";
1663 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1664 }
John McCallf85e1932011-06-15 23:02:42 +00001665 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1666 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1667 << "assign" << "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 << "assign" << "weak";
1674 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1675 }
1676 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1677 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1678 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1679 << "unsafe_unretained" << "copy";
1680 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1681 }
1682 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1683 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1684 << "unsafe_unretained" << "retain";
1685 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1686 }
1687 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1688 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1689 << "unsafe_unretained" << "strong";
1690 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1691 }
1692 if (getLangOptions().ObjCAutoRefCount &&
1693 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1694 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1695 << "unsafe_unretained" << "weak";
1696 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1697 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001698 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1699 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1700 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1701 << "copy" << "retain";
1702 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1703 }
John McCallf85e1932011-06-15 23:02:42 +00001704 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1705 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1706 << "copy" << "strong";
1707 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1708 }
1709 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1710 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1711 << "copy" << "weak";
1712 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1713 }
1714 }
1715 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1716 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1717 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1718 << "retain" << "weak";
1719 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1720 }
1721 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1722 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1723 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1724 << "strong" << "weak";
1725 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001726 }
1727
1728 // Warn if user supplied no assignment attribute, property is
1729 // readwrite, and this is an object type.
1730 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001731 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1732 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1733 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001734 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1735 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001736 if (getLangOptions().ObjCAutoRefCount)
1737 // With arc, @property definitions should default to (strong) when
1738 // not specified
1739 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
1740 else {
1741 // Skip this warning in gc-only mode.
1742 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1743 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001744
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001745 // If non-gc code warn that this is likely inappropriate.
1746 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1747 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1748 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001749
1750 // FIXME: Implement warning dependent on NSCopying being
1751 // implemented. See also:
1752 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1753 // (please trim this list while you are at it).
1754 }
1755
1756 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001757 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001758 && getLangOptions().getGCMode() == LangOptions::GCOnly
1759 && PropertyTy->isBlockPointerType())
1760 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1761}