blob: 9602c2bd5b68d2f713dc1aa0c4a6b2faf6b71a37 [file] [log] [blame]
Ted Kremenek9d64c152010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
Fariborz Jahanian17cb3262010-05-05 21:52:17 +000018#include "clang/AST/ExprObjC.h"
John McCall50df6ae2010-08-25 07:03:20 +000019#include "llvm/ADT/DenseSet.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000020
21using namespace clang;
22
Ted Kremenek28685ab2010-03-12 00:46:40 +000023//===----------------------------------------------------------------------===//
24// Grammar actions.
25//===----------------------------------------------------------------------===//
26
John McCallf85e1932011-06-15 23:02:42 +000027/// Check the internal consistency of a property declaration.
28static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
29 if (property->isInvalidDecl()) return;
30
31 ObjCPropertyDecl::PropertyAttributeKind propertyKind
32 = property->getPropertyAttributes();
33 Qualifiers::ObjCLifetime propertyLifetime
34 = property->getType().getObjCLifetime();
35
36 // Nothing to do if we don't have a lifetime.
37 if (propertyLifetime == Qualifiers::OCL_None) return;
38
39 Qualifiers::ObjCLifetime expectedLifetime;
40 unsigned selector;
41
42 // Strong properties should have either strong or no lifetime.
43 if (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
44 ObjCPropertyDecl::OBJC_PR_strong |
45 ObjCPropertyDecl::OBJC_PR_copy)) {
46 expectedLifetime = Qualifiers::OCL_Strong;
47 selector = 0;
48 } else if (propertyKind & ObjCPropertyDecl::OBJC_PR_weak) {
49 expectedLifetime = Qualifiers::OCL_Weak;
50 selector = 1;
51 } else if (propertyKind & (ObjCPropertyDecl::OBJC_PR_assign |
52 ObjCPropertyDecl::OBJC_PR_unsafe_unretained) &&
53 property->getType()->isObjCRetainableType()) {
54 expectedLifetime = Qualifiers::OCL_ExplicitNone;
55 selector = 2;
56 } else {
57 // We have a lifetime qualifier but no dominating property
58 // attribute. That's okay.
59 return;
60 }
61
62 if (propertyLifetime == expectedLifetime) return;
63
64 property->setInvalidDecl();
65 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000066 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +000067 << property->getDeclName()
68 << selector
69 << propertyLifetime;
70}
71
John McCalld226f652010-08-21 09:40:31 +000072Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
73 FieldDeclarator &FD,
74 ObjCDeclSpec &ODS,
75 Selector GetterSel,
76 Selector SetterSel,
77 Decl *ClassCategory,
78 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000079 tok::ObjCKeywordKind MethodImplKind,
80 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000081 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +000082 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
83 QualType T = TSI->getType();
84 if ((getLangOptions().getGCMode() != LangOptions::NonGC &&
85 T.isObjCGCWeak()) ||
86 (getLangOptions().ObjCAutoRefCount &&
87 T.getObjCLifetime() == Qualifiers::OCL_Weak))
88 Attributes |= ObjCDeclSpec::DQ_PR_weak;
89
Ted Kremenek28685ab2010-03-12 00:46:40 +000090 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
91 // default is readwrite!
92 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
93 // property is defaulted to 'assign' if it is readwrite and is
94 // not retain or copy
95 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
96 (isReadWrite &&
97 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +000098 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
99 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
100 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
101 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000102
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000103 // Proceed with constructing the ObjCPropertDecls.
104 ObjCContainerDecl *ClassDecl =
John McCalld226f652010-08-21 09:40:31 +0000105 cast<ObjCContainerDecl>(ClassCategory);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000106
Ted Kremenek28685ab2010-03-12 00:46:40 +0000107 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000108 if (CDecl->IsClassExtension()) {
John McCalld226f652010-08-21 09:40:31 +0000109 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000110 FD, GetterSel, SetterSel,
111 isAssign, isReadWrite,
112 Attributes,
113 isOverridingProperty, TSI,
114 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000115 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000116 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000117 if (getLangOptions().ObjCAutoRefCount)
118 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
119 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000120 return Res;
121 }
122
John McCallf85e1932011-06-15 23:02:42 +0000123 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
124 GetterSel, SetterSel,
125 isAssign, isReadWrite,
126 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000127 if (lexicalDC)
128 Res->setLexicalDeclContext(lexicalDC);
129
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000130 // Validate the attributes on the @property.
131 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000132
133 if (getLangOptions().ObjCAutoRefCount)
134 checkARCPropertyDecl(*this, Res);
135
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000136 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000137}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000138
John McCalld226f652010-08-21 09:40:31 +0000139Decl *
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000140Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
141 SourceLocation AtLoc, FieldDeclarator &FD,
142 Selector GetterSel, Selector SetterSel,
143 const bool isAssign,
144 const bool isReadWrite,
145 const unsigned Attributes,
146 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000147 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000148 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000149
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000150 // Diagnose if this property is already in continuation class.
151 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000152 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000153 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
154
155 if (CCPrimary)
156 // Check for duplicate declaration of this property in current and
157 // other class extensions.
158 for (const ObjCCategoryDecl *ClsExtDecl =
159 CCPrimary->getFirstClassExtension();
160 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
161 if (ObjCPropertyDecl *prevDecl =
162 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
163 Diag(AtLoc, diag::err_duplicate_property);
164 Diag(prevDecl->getLocation(), diag::note_property_declare);
165 return 0;
166 }
167 }
168
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000169 // Create a new ObjCPropertyDecl with the DeclContext being
170 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000171 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000172 ObjCPropertyDecl *PDecl =
173 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
174 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000175 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
176 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
177 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
178 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000179 // Set setter/getter selector name. Needed later.
180 PDecl->setGetterName(GetterSel);
181 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000182 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000183 DC->addDecl(PDecl);
184
185 // We need to look in the @interface to see if the @property was
186 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000187 if (!CCPrimary) {
188 Diag(CDecl->getLocation(), diag::err_continuation_class);
189 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000190 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000191 }
192
193 // Find the property in continuation class's primary class only.
194 ObjCPropertyDecl *PIDecl =
195 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
196
197 if (!PIDecl) {
198 // No matching property found in the primary class. Just fall thru
199 // and add property to continuation class's primary class.
200 ObjCPropertyDecl *PDecl =
201 CreatePropertyDecl(S, CCPrimary, AtLoc,
202 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000203 Attributes, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000204
205 // A case of continuation class adding a new property in the class. This
206 // is not what it was meant for. However, gcc supports it and so should we.
207 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000208 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
209 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000210 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000211 }
212
213 // The property 'PIDecl's readonly attribute will be over-ridden
214 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000215 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000216 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
217 unsigned retainCopyNonatomic =
218 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000219 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000220 ObjCPropertyDecl::OBJC_PR_copy |
221 ObjCPropertyDecl::OBJC_PR_nonatomic);
222 if ((Attributes & retainCopyNonatomic) !=
223 (PIkind & retainCopyNonatomic)) {
224 Diag(AtLoc, diag::warn_property_attr_mismatch);
225 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000226 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000227 DeclContext *DC = cast<DeclContext>(CCPrimary);
228 if (!ObjCPropertyDecl::findPropertyDecl(DC,
229 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000230 // Protocol is not in the primary class. Must build one for it.
231 ObjCDeclSpec ProtocolPropertyODS;
232 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
233 // and ObjCPropertyDecl::PropertyAttributeKind have identical
234 // values. Should consolidate both into one enum type.
235 ProtocolPropertyODS.
236 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
237 PIkind);
238
John McCalld226f652010-08-21 09:40:31 +0000239 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000240 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
241 PIDecl->getGetterName(),
242 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000243 CCPrimary, isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000244 MethodImplKind,
245 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000246 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000247 }
248 PIDecl->makeitReadWriteAttribute();
249 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
250 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000251 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
252 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000253 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
254 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
255 PIDecl->setSetterName(SetterSel);
256 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000257 // Tailor the diagnostics for the common case where a readwrite
258 // property is declared both in the @interface and the continuation.
259 // This is a common error where the user often intended the original
260 // declaration to be readonly.
261 unsigned diag =
262 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
263 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
264 ? diag::err_use_continuation_class_redeclaration_readwrite
265 : diag::err_use_continuation_class;
266 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000267 << CCPrimary->getDeclName();
268 Diag(PIDecl->getLocation(), diag::note_property_declare);
269 }
270 *isOverridingProperty = true;
271 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000272 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000273 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000274}
275
276ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
277 ObjCContainerDecl *CDecl,
278 SourceLocation AtLoc,
279 FieldDeclarator &FD,
280 Selector GetterSel,
281 Selector SetterSel,
282 const bool isAssign,
283 const bool isReadWrite,
284 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000285 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000286 tok::ObjCKeywordKind MethodImplKind,
287 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000288 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000289 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000290
291 // Issue a warning if property is 'assign' as default and its object, which is
292 // gc'able conforms to NSCopying protocol
293 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
294 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000295 if (const ObjCObjectPointerType *ObjPtrTy =
296 T->getAs<ObjCObjectPointerType>()) {
297 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
298 if (IDecl)
299 if (ObjCProtocolDecl* PNSCopying =
300 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
301 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
302 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000303 }
John McCallc12c5bb2010-05-15 11:32:37 +0000304 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000305 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
306
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000307 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000308 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
309 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000310 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000311
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000312 if (ObjCPropertyDecl *prevDecl =
313 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000314 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000315 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000316 PDecl->setInvalidDecl();
317 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000318 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000319 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000320 if (lexicalDC)
321 PDecl->setLexicalDeclContext(lexicalDC);
322 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000323
324 if (T->isArrayType() || T->isFunctionType()) {
325 Diag(AtLoc, diag::err_property_type) << T;
326 PDecl->setInvalidDecl();
327 }
328
329 ProcessDeclAttributes(S, PDecl, FD.D);
330
331 // Regardless of setter/getter attribute, we save the default getter/setter
332 // selector names in anticipation of declaration of setter/getter methods.
333 PDecl->setGetterName(GetterSel);
334 PDecl->setSetterName(SetterSel);
335
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000336 unsigned attributesAsWritten = 0;
337 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
338 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
339 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
340 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
341 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
342 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
343 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
344 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
345 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
346 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
347 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
348 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
349 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
350 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
351 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
352 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
353 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
354 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
355 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
356 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
357 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
358 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
359 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
360 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
361
362 PDecl->setPropertyAttributesAsWritten(
363 (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten);
364
Ted Kremenek28685ab2010-03-12 00:46:40 +0000365 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
366 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
367
368 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
369 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
370
371 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
372 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
373
374 if (isReadWrite)
375 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
376
377 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
378 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
379
John McCallf85e1932011-06-15 23:02:42 +0000380 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
381 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
382
383 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
384 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
385
Ted Kremenek28685ab2010-03-12 00:46:40 +0000386 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
387 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
388
John McCallf85e1932011-06-15 23:02:42 +0000389 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
390 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
391
Ted Kremenek28685ab2010-03-12 00:46:40 +0000392 if (isAssign)
393 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
394
395 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
396 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000397 else if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
398 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000399
John McCallf85e1932011-06-15 23:02:42 +0000400 // 'unsafe_unretained' is alias for 'assign'.
401 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
402 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
403 if (isAssign)
404 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
405
Ted Kremenek28685ab2010-03-12 00:46:40 +0000406 if (MethodImplKind == tok::objc_required)
407 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
408 else if (MethodImplKind == tok::objc_optional)
409 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000410
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000411 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000412}
413
John McCallf85e1932011-06-15 23:02:42 +0000414static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
415 ObjCPropertyDecl *property,
416 ObjCIvarDecl *ivar) {
417 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
418
419 QualType propertyType = property->getType();
420 Qualifiers::ObjCLifetime propertyLifetime = propertyType.getObjCLifetime();
421 ObjCPropertyDecl::PropertyAttributeKind propertyKind
422 = property->getPropertyAttributes();
423
424 QualType ivarType = ivar->getType();
425 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
426
427 // Case 1: strong properties.
428 if (propertyLifetime == Qualifiers::OCL_Strong ||
429 (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
430 ObjCPropertyDecl::OBJC_PR_strong |
431 ObjCPropertyDecl::OBJC_PR_copy))) {
432 switch (ivarLifetime) {
433 case Qualifiers::OCL_Strong:
434 // Okay.
435 return;
436
437 case Qualifiers::OCL_None:
438 case Qualifiers::OCL_Autoreleasing:
439 // These aren't valid lifetimes for object ivars; don't diagnose twice.
440 return;
441
442 case Qualifiers::OCL_ExplicitNone:
443 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000444 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000445 << property->getDeclName()
446 << ivar->getDeclName()
447 << ivarLifetime;
448 break;
449 }
450
451 // Case 2: weak properties.
452 } else if (propertyLifetime == Qualifiers::OCL_Weak ||
453 (propertyKind & ObjCPropertyDecl::OBJC_PR_weak)) {
454 switch (ivarLifetime) {
455 case Qualifiers::OCL_Weak:
456 // Okay.
457 return;
458
459 case Qualifiers::OCL_None:
460 case Qualifiers::OCL_Autoreleasing:
461 // These aren't valid lifetimes for object ivars; don't diagnose twice.
462 return;
463
464 case Qualifiers::OCL_ExplicitNone:
465 case Qualifiers::OCL_Strong:
466 S.Diag(propertyImplLoc, diag::error_weak_property)
467 << property->getDeclName()
468 << ivar->getDeclName();
469 break;
470 }
471
472 // Case 3: assign properties.
473 } else if ((propertyKind & ObjCPropertyDecl::OBJC_PR_assign) &&
474 propertyType->isObjCRetainableType()) {
475 switch (ivarLifetime) {
476 case Qualifiers::OCL_ExplicitNone:
477 // Okay.
478 return;
479
480 case Qualifiers::OCL_None:
481 case Qualifiers::OCL_Autoreleasing:
482 // These aren't valid lifetimes for object ivars; don't diagnose twice.
483 return;
484
485 case Qualifiers::OCL_Weak:
486 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000487 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000488 << property->getDeclName()
489 << ivar->getDeclName();
490 break;
491 }
492
493 // Any other property should be ignored.
494 } else {
495 return;
496 }
497
498 S.Diag(property->getLocation(), diag::note_property_declare);
499}
500
Ted Kremenek28685ab2010-03-12 00:46:40 +0000501
502/// ActOnPropertyImplDecl - This routine performs semantic checks and
503/// builds the AST node for a property implementation declaration; declared
504/// as @synthesize or @dynamic.
505///
John McCalld226f652010-08-21 09:40:31 +0000506Decl *Sema::ActOnPropertyImplDecl(Scope *S,
507 SourceLocation AtLoc,
508 SourceLocation PropertyLoc,
509 bool Synthesize,
510 Decl *ClassCatImpDecl,
511 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000512 IdentifierInfo *PropertyIvar,
513 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000514 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000515 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000516 // Make sure we have a context for the property implementation declaration.
517 if (!ClassImpDecl) {
518 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000519 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000520 }
521 ObjCPropertyDecl *property = 0;
522 ObjCInterfaceDecl* IDecl = 0;
523 // Find the class or category class where this property must have
524 // a declaration.
525 ObjCImplementationDecl *IC = 0;
526 ObjCCategoryImplDecl* CatImplClass = 0;
527 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
528 IDecl = IC->getClassInterface();
529 // We always synthesize an interface for an implementation
530 // without an interface decl. So, IDecl is always non-zero.
531 assert(IDecl &&
532 "ActOnPropertyImplDecl - @implementation without @interface");
533
534 // Look for this property declaration in the @implementation's @interface
535 property = IDecl->FindPropertyDeclaration(PropertyId);
536 if (!property) {
537 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000538 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000539 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000540 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000541 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
542 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000543 if (AtLoc.isValid())
544 Diag(AtLoc, diag::warn_implicit_atomic_property);
545 else
546 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
547 Diag(property->getLocation(), diag::note_property_declare);
548 }
549
Ted Kremenek28685ab2010-03-12 00:46:40 +0000550 if (const ObjCCategoryDecl *CD =
551 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
552 if (!CD->IsClassExtension()) {
553 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
554 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000555 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000556 }
557 }
558 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
559 if (Synthesize) {
560 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000561 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000562 }
563 IDecl = CatImplClass->getClassInterface();
564 if (!IDecl) {
565 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000566 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000567 }
568 ObjCCategoryDecl *Category =
569 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
570
571 // If category for this implementation not found, it is an error which
572 // has already been reported eralier.
573 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000574 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000575 // Look for this property declaration in @implementation's category
576 property = Category->FindPropertyDeclaration(PropertyId);
577 if (!property) {
578 Diag(PropertyLoc, diag::error_bad_category_property_decl)
579 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000580 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000581 }
582 } else {
583 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000584 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000585 }
586 ObjCIvarDecl *Ivar = 0;
587 // Check that we have a valid, previously declared ivar for @synthesize
588 if (Synthesize) {
589 // @synthesize
590 if (!PropertyIvar)
591 PropertyIvar = PropertyId;
John McCallf85e1932011-06-15 23:02:42 +0000592 ObjCPropertyDecl::PropertyAttributeKind kind
593 = property->getPropertyAttributes();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000594 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian14086762011-03-28 23:47:18 +0000595 QualType PropertyIvarType = PropType;
596 if (PropType->isReferenceType())
597 PropertyIvarType = cast<ReferenceType>(PropType)->getPointeeType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000598 // Check that this is a previously declared 'ivar' in 'IDecl' interface
599 ObjCInterfaceDecl *ClassDeclared;
600 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
601 if (!Ivar) {
John McCallf85e1932011-06-15 23:02:42 +0000602 // In ARC, give the ivar a lifetime qualifier based on its
603 // property attributes.
604 if (getLangOptions().ObjCAutoRefCount &&
605 !PropertyIvarType.getObjCLifetime()) {
606
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000607 if (!property->hasWrittenStorageAttribute() &&
608 property->getType()->isObjCRetainableType()) {
609 Diag(PropertyLoc,
610 diag::err_arc_objc_property_default_assign_on_object);
611 Diag(property->getLocation(), diag::note_property_declare);
612 }
613
John McCallf85e1932011-06-15 23:02:42 +0000614 // retain/copy have retaining lifetime.
615 if (kind & (ObjCPropertyDecl::OBJC_PR_retain |
616 ObjCPropertyDecl::OBJC_PR_strong |
617 ObjCPropertyDecl::OBJC_PR_copy)) {
618 Qualifiers qs;
619 qs.addObjCLifetime(Qualifiers::OCL_Strong);
620 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
621 }
622 else if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
John McCall9f084a32011-07-06 00:26:06 +0000623 if (!getLangOptions().ObjCRuntimeHasWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000624 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
625 Diag(property->getLocation(), diag::note_property_declare);
626 }
627 Qualifiers qs;
628 qs.addObjCLifetime(Qualifiers::OCL_Weak);
629 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
630 }
631 else if (kind & ObjCPropertyDecl::OBJC_PR_assign &&
632 PropertyIvarType->isObjCRetainableType()) {
633 // assume that an 'assign' property synthesizes __unsafe_unretained
634 // ivar
635 Qualifiers qs;
636 qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
637 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
638 }
639 }
640
641 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
642 !getLangOptions().ObjCAutoRefCount &&
643 getLangOptions().getGCMode() == LangOptions::NonGC) {
644 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
645 Diag(property->getLocation(), diag::note_property_declare);
646 }
647
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000648 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
649 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000650 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000651 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000652 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000653 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000654 IDecl->makeDeclVisibleInContext(Ivar, false);
655 property->setPropertyIvarDecl(Ivar);
656
657 if (!getLangOptions().ObjCNonFragileABI)
658 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
659 // Note! I deliberately want it to fall thru so, we have a
660 // a property implementation and to avoid future warnings.
661 } else if (getLangOptions().ObjCNonFragileABI &&
662 ClassDeclared != IDecl) {
663 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
664 << property->getDeclName() << Ivar->getDeclName()
665 << ClassDeclared->getDeclName();
666 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000667 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000668 // Note! I deliberately want it to fall thru so more errors are caught.
669 }
670 QualType IvarType = Context.getCanonicalType(Ivar->getType());
671
672 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000673 if (PropertyIvarType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000674 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000675 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000676 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000677 compat =
678 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000679 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000680 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000681 else {
682 SourceLocation Loc = PropertyIvarLoc;
683 if (Loc.isInvalid())
684 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000685 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000686 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000687 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000688 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000689 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000690 << property->getDeclName() << PropType
691 << Ivar->getDeclName() << IvarType;
692 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000693 // Note! I deliberately want it to fall thru so, we have a
694 // a property implementation and to avoid future warnings.
695 }
696
697 // FIXME! Rules for properties are somewhat different that those
698 // for assignments. Use a new routine to consolidate all cases;
699 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000700 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000701 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
702 if (lhsType != rhsType &&
703 lhsType->isArithmeticType()) {
704 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 // Fall thru - see previous comment
709 }
710 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000711 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
712 getLangOptions().getGCMode() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000713 Diag(PropertyLoc, diag::error_weak_property)
714 << property->getDeclName() << Ivar->getDeclName();
715 // Fall thru - see previous comment
716 }
John McCallf85e1932011-06-15 23:02:42 +0000717 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000718 if ((property->getType()->isObjCObjectPointerType() ||
719 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
720 getLangOptions().getGCMode() != LangOptions::NonGC) {
721 Diag(PropertyLoc, diag::error_strong_property)
722 << property->getDeclName() << Ivar->getDeclName();
723 // Fall thru - see previous comment
724 }
725 }
John McCallf85e1932011-06-15 23:02:42 +0000726 if (getLangOptions().ObjCAutoRefCount)
727 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000728 } else if (PropertyIvar)
729 // @dynamic
730 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000731
Ted Kremenek28685ab2010-03-12 00:46:40 +0000732 assert (property && "ActOnPropertyImplDecl - property declaration missing");
733 ObjCPropertyImplDecl *PIDecl =
734 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
735 property,
736 (Synthesize ?
737 ObjCPropertyImplDecl::Synthesize
738 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000739 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000740 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
741 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000742 if (getLangOptions().CPlusPlus && Synthesize &&
743 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000744 // For Objective-C++, need to synthesize the AST for the IVAR object to be
745 // returned by the getter as it must conform to C++'s copy-return rules.
746 // FIXME. Eventually we want to do this for Objective-C as well.
747 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
748 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000749 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
750 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000751 Expr *IvarRefExpr =
752 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
753 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000754 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000755 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000756 SourceLocation(),
757 getterMethod->getResultType(),
758 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000759 SourceLocation(),
760 Owned(IvarRefExpr));
761 if (!Res.isInvalid()) {
762 Expr *ResExpr = Res.takeAs<Expr>();
763 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000764 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000765 PIDecl->setGetterCXXConstructor(ResExpr);
766 }
767 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000768 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
769 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
770 Diag(getterMethod->getLocation(),
771 diag::warn_property_getter_owning_mismatch);
772 Diag(property->getLocation(), diag::note_property_declare);
773 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000774 }
775 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
776 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000777 if (getLangOptions().CPlusPlus && Synthesize
778 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000779 // FIXME. Eventually we want to do this for Objective-C as well.
780 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
781 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000782 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
783 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000784 Expr *lhs =
785 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
786 SelfExpr, true, true);
787 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
788 ParmVarDecl *Param = (*P);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000789 QualType T = Param->getType();
790 if (T->isReferenceType())
Fariborz Jahanian61750f22011-03-30 16:59:30 +0000791 T = T->getAs<ReferenceType>()->getPointeeType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000792 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000793 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000794 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000795 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000796 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
797 }
798 }
799
Ted Kremenek28685ab2010-03-12 00:46:40 +0000800 if (IC) {
801 if (Synthesize)
802 if (ObjCPropertyImplDecl *PPIDecl =
803 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
804 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
805 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
806 << PropertyIvar;
807 Diag(PPIDecl->getLocation(), diag::note_previous_use);
808 }
809
810 if (ObjCPropertyImplDecl *PPIDecl
811 = IC->FindPropertyImplDecl(PropertyId)) {
812 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
813 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000814 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000815 }
816 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000817 if (getLangOptions().ObjCDefaultSynthProperties &&
818 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000819 // Diagnose if an ivar was lazily synthesdized due to a previous
820 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000821 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000822 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000823 ObjCIvarDecl *Ivar = 0;
824 if (!Synthesize)
825 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
826 else {
827 if (PropertyIvar && PropertyIvar != PropertyId)
828 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
829 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000830 // Issue diagnostics only if Ivar belongs to current class.
831 if (Ivar && Ivar->getSynthesize() &&
832 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000833 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
834 << PropertyId;
835 Ivar->setInvalidDecl();
836 }
837 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000838 } else {
839 if (Synthesize)
840 if (ObjCPropertyImplDecl *PPIDecl =
841 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
842 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
843 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
844 << PropertyIvar;
845 Diag(PPIDecl->getLocation(), diag::note_previous_use);
846 }
847
848 if (ObjCPropertyImplDecl *PPIDecl =
849 CatImplClass->FindPropertyImplDecl(PropertyId)) {
850 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
851 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000852 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000853 }
854 CatImplClass->addPropertyImplementation(PIDecl);
855 }
856
John McCalld226f652010-08-21 09:40:31 +0000857 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000858}
859
860//===----------------------------------------------------------------------===//
861// Helper methods.
862//===----------------------------------------------------------------------===//
863
Ted Kremenek9d64c152010-03-12 00:38:38 +0000864/// DiagnosePropertyMismatch - Compares two properties for their
865/// attributes and types and warns on a variety of inconsistencies.
866///
867void
868Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
869 ObjCPropertyDecl *SuperProperty,
870 const IdentifierInfo *inheritedName) {
871 ObjCPropertyDecl::PropertyAttributeKind CAttr =
872 Property->getPropertyAttributes();
873 ObjCPropertyDecl::PropertyAttributeKind SAttr =
874 SuperProperty->getPropertyAttributes();
875 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
876 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
877 Diag(Property->getLocation(), diag::warn_readonly_property)
878 << Property->getDeclName() << inheritedName;
879 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
880 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
881 Diag(Property->getLocation(), diag::warn_property_attribute)
882 << Property->getDeclName() << "copy" << inheritedName;
John McCallf85e1932011-06-15 23:02:42 +0000883 else {
884 unsigned CAttrRetain =
885 (CAttr &
886 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
887 unsigned SAttrRetain =
888 (SAttr &
889 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
890 bool CStrong = (CAttrRetain != 0);
891 bool SStrong = (SAttrRetain != 0);
892 if (CStrong != SStrong)
893 Diag(Property->getLocation(), diag::warn_property_attribute)
894 << Property->getDeclName() << "retain (or strong)" << inheritedName;
895 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000896
897 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
898 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
899 Diag(Property->getLocation(), diag::warn_property_attribute)
900 << Property->getDeclName() << "atomic" << inheritedName;
901 if (Property->getSetterName() != SuperProperty->getSetterName())
902 Diag(Property->getLocation(), diag::warn_property_attribute)
903 << Property->getDeclName() << "setter" << inheritedName;
904 if (Property->getGetterName() != SuperProperty->getGetterName())
905 Diag(Property->getLocation(), diag::warn_property_attribute)
906 << Property->getDeclName() << "getter" << inheritedName;
907
908 QualType LHSType =
909 Context.getCanonicalType(SuperProperty->getType());
910 QualType RHSType =
911 Context.getCanonicalType(Property->getType());
912
Fariborz Jahanianc286f382011-07-12 22:05:16 +0000913 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000914 // Do cases not handled in above.
915 // FIXME. For future support of covariant property types, revisit this.
916 bool IncompatibleObjC = false;
917 QualType ConvertedType;
918 if (!isObjCPointerConversion(RHSType, LHSType,
919 ConvertedType, IncompatibleObjC) ||
920 IncompatibleObjC)
921 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
922 << Property->getType() << SuperProperty->getType() << inheritedName;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000923 }
924}
925
926bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
927 ObjCMethodDecl *GetterMethod,
928 SourceLocation Loc) {
929 if (GetterMethod &&
930 GetterMethod->getResultType() != property->getType()) {
931 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000932 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000933 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000934 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000935 if (result != Compatible) {
936 Diag(Loc, diag::warn_accessor_property_type_mismatch)
937 << property->getDeclName()
938 << GetterMethod->getSelector();
939 Diag(GetterMethod->getLocation(), diag::note_declared_at);
940 return true;
941 }
942 }
943 return false;
944}
945
946/// ComparePropertiesInBaseAndSuper - This routine compares property
947/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000948/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000949///
950void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
951 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
952 if (!SDecl)
953 return;
954 // FIXME: O(N^2)
955 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
956 E = SDecl->prop_end(); S != E; ++S) {
957 ObjCPropertyDecl *SuperPDecl = (*S);
958 // Does property in super class has declaration in current class?
959 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
960 E = IDecl->prop_end(); I != E; ++I) {
961 ObjCPropertyDecl *PDecl = (*I);
962 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
963 DiagnosePropertyMismatch(PDecl, SuperPDecl,
964 SDecl->getIdentifier());
965 }
966 }
967}
968
969/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
970/// of properties declared in a protocol and compares their attribute against
971/// the same property declared in the class or category.
972void
973Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
974 ObjCProtocolDecl *PDecl) {
975 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
976 if (!IDecl) {
977 // Category
978 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
979 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
980 if (!CatDecl->IsClassExtension())
981 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
982 E = PDecl->prop_end(); P != E; ++P) {
983 ObjCPropertyDecl *Pr = (*P);
984 ObjCCategoryDecl::prop_iterator CP, CE;
985 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000986 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000987 if ((*CP)->getIdentifier() == Pr->getIdentifier())
988 break;
989 if (CP != CE)
990 // Property protocol already exist in class. Diagnose any mismatch.
991 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
992 }
993 return;
994 }
995 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
996 E = PDecl->prop_end(); P != E; ++P) {
997 ObjCPropertyDecl *Pr = (*P);
998 ObjCInterfaceDecl::prop_iterator CP, CE;
999 // Is this property already in class's list of properties?
1000 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1001 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1002 break;
1003 if (CP != CE)
1004 // Property protocol already exist in class. Diagnose any mismatch.
1005 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1006 }
1007}
1008
1009/// CompareProperties - This routine compares properties
1010/// declared in 'ClassOrProtocol' objects (which can be a class or an
1011/// inherited protocol with the list of properties for class/category 'CDecl'
1012///
John McCalld226f652010-08-21 09:40:31 +00001013void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1014 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001015 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1016
1017 if (!IDecl) {
1018 // Category
1019 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1020 assert (CatDecl && "CompareProperties");
1021 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1022 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1023 E = MDecl->protocol_end(); P != E; ++P)
1024 // Match properties of category with those of protocol (*P)
1025 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1026
1027 // Go thru the list of protocols for this category and recursively match
1028 // their properties with those in the category.
1029 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1030 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001031 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001032 } else {
1033 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1034 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1035 E = MD->protocol_end(); P != E; ++P)
1036 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1037 }
1038 return;
1039 }
1040
1041 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001042 for (ObjCInterfaceDecl::all_protocol_iterator
1043 P = MDecl->all_referenced_protocol_begin(),
1044 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001045 // Match properties of class IDecl with those of protocol (*P).
1046 MatchOneProtocolPropertiesInClass(IDecl, *P);
1047
1048 // Go thru the list of protocols for this class and recursively match
1049 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001050 for (ObjCInterfaceDecl::all_protocol_iterator
1051 P = IDecl->all_referenced_protocol_begin(),
1052 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001053 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001054 } else {
1055 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1056 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1057 E = MD->protocol_end(); P != E; ++P)
1058 MatchOneProtocolPropertiesInClass(IDecl, *P);
1059 }
1060}
1061
1062/// isPropertyReadonly - Return true if property is readonly, by searching
1063/// for the property in the class and in its categories and implementations
1064///
1065bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1066 ObjCInterfaceDecl *IDecl) {
1067 // by far the most common case.
1068 if (!PDecl->isReadOnly())
1069 return false;
1070 // Even if property is ready only, if interface has a user defined setter,
1071 // it is not considered read only.
1072 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1073 return false;
1074
1075 // Main class has the property as 'readonly'. Must search
1076 // through the category list to see if the property's
1077 // attribute has been over-ridden to 'readwrite'.
1078 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1079 Category; Category = Category->getNextClassCategory()) {
1080 // Even if property is ready only, if a category has a user defined setter,
1081 // it is not considered read only.
1082 if (Category->getInstanceMethod(PDecl->getSetterName()))
1083 return false;
1084 ObjCPropertyDecl *P =
1085 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1086 if (P && !P->isReadOnly())
1087 return false;
1088 }
1089
1090 // Also, check for definition of a setter method in the implementation if
1091 // all else failed.
1092 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1093 if (ObjCImplementationDecl *IMD =
1094 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1095 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1096 return false;
1097 } else if (ObjCCategoryImplDecl *CIMD =
1098 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1099 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1100 return false;
1101 }
1102 }
1103 // Lastly, look through the implementation (if one is in scope).
1104 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1105 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1106 return false;
1107 // If all fails, look at the super class.
1108 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1109 return isPropertyReadonly(PDecl, SIDecl);
1110 return true;
1111}
1112
1113/// CollectImmediateProperties - This routine collects all properties in
1114/// the class and its conforming protocols; but not those it its super class.
1115void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001116 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1117 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001118 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1119 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1120 E = IDecl->prop_end(); P != E; ++P) {
1121 ObjCPropertyDecl *Prop = (*P);
1122 PropMap[Prop->getIdentifier()] = Prop;
1123 }
1124 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001125 for (ObjCInterfaceDecl::all_protocol_iterator
1126 PI = IDecl->all_referenced_protocol_begin(),
1127 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001128 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001129 }
1130 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1131 if (!CATDecl->IsClassExtension())
1132 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1133 E = CATDecl->prop_end(); P != E; ++P) {
1134 ObjCPropertyDecl *Prop = (*P);
1135 PropMap[Prop->getIdentifier()] = Prop;
1136 }
1137 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001138 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001139 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001140 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001141 }
1142 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1143 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1144 E = PDecl->prop_end(); P != E; ++P) {
1145 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001146 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1147 // Exclude property for protocols which conform to class's super-class,
1148 // as super-class has to implement the property.
1149 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
1150 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1151 if (!PropEntry)
1152 PropEntry = Prop;
1153 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001154 }
1155 // scan through protocol's protocols.
1156 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1157 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001158 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001159 }
1160}
1161
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001162/// CollectClassPropertyImplementations - This routine collects list of
1163/// properties to be implemented in the class. This includes, class's
1164/// and its conforming protocols' properties.
1165static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1166 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1167 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1168 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1169 E = IDecl->prop_end(); P != E; ++P) {
1170 ObjCPropertyDecl *Prop = (*P);
1171 PropMap[Prop->getIdentifier()] = Prop;
1172 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001173 for (ObjCInterfaceDecl::all_protocol_iterator
1174 PI = IDecl->all_referenced_protocol_begin(),
1175 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001176 CollectClassPropertyImplementations((*PI), PropMap);
1177 }
1178 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1179 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1180 E = PDecl->prop_end(); P != E; ++P) {
1181 ObjCPropertyDecl *Prop = (*P);
1182 PropMap[Prop->getIdentifier()] = Prop;
1183 }
1184 // scan through protocol's protocols.
1185 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1186 E = PDecl->protocol_end(); PI != E; ++PI)
1187 CollectClassPropertyImplementations((*PI), PropMap);
1188 }
1189}
1190
1191/// CollectSuperClassPropertyImplementations - This routine collects list of
1192/// properties to be implemented in super class(s) and also coming from their
1193/// conforming protocols.
1194static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1195 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1196 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1197 while (SDecl) {
1198 CollectClassPropertyImplementations(SDecl, PropMap);
1199 SDecl = SDecl->getSuperClass();
1200 }
1201 }
1202}
1203
Ted Kremenek9d64c152010-03-12 00:38:38 +00001204/// LookupPropertyDecl - Looks up a property in the current class and all
1205/// its protocols.
1206ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1207 IdentifierInfo *II) {
1208 if (const ObjCInterfaceDecl *IDecl =
1209 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1210 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1211 E = IDecl->prop_end(); P != E; ++P) {
1212 ObjCPropertyDecl *Prop = (*P);
1213 if (Prop->getIdentifier() == II)
1214 return Prop;
1215 }
1216 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001217 for (ObjCInterfaceDecl::all_protocol_iterator
1218 PI = IDecl->all_referenced_protocol_begin(),
1219 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001220 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1221 if (Prop)
1222 return Prop;
1223 }
1224 }
1225 else if (const ObjCProtocolDecl *PDecl =
1226 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1227 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1228 E = PDecl->prop_end(); P != E; ++P) {
1229 ObjCPropertyDecl *Prop = (*P);
1230 if (Prop->getIdentifier() == II)
1231 return Prop;
1232 }
1233 // scan through protocol's protocols.
1234 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1235 E = PDecl->protocol_end(); PI != E; ++PI) {
1236 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1237 if (Prop)
1238 return Prop;
1239 }
1240 }
1241 return 0;
1242}
1243
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001244/// DefaultSynthesizeProperties - This routine default synthesizes all
1245/// properties which must be synthesized in class's @implementation.
1246void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
1247 ObjCInterfaceDecl *IDecl) {
1248
1249 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1250 CollectClassPropertyImplementations(IDecl, PropMap);
1251 if (PropMap.empty())
1252 return;
1253 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1254 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1255
1256 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1257 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1258 ObjCPropertyDecl *Prop = P->second;
1259 // If property to be implemented in the super class, ignore.
1260 if (SuperPropMap[Prop->getIdentifier()])
1261 continue;
1262 // Is there a matching propery synthesize/dynamic?
1263 if (Prop->isInvalidDecl() ||
1264 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1265 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1266 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001267 // Property may have been synthesized by user.
1268 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1269 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001270 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1271 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1272 continue;
1273 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1274 continue;
1275 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001276
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001277
1278 // We use invalid SourceLocations for the synthesized ivars since they
1279 // aren't really synthesized at a particular location; they just exist.
1280 // Saying that they are located at the @implementation isn't really going
1281 // to help users.
1282 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1283 true,IMPDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001284 Prop->getIdentifier(), Prop->getIdentifier(),
1285 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001286 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001287}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001288
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001289void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001290 ObjCContainerDecl *CDecl,
1291 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001292 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1293 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1294 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1295
Ted Kremenek9d64c152010-03-12 00:38:38 +00001296 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001297 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001298 if (PropMap.empty())
1299 return;
1300
1301 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1302 for (ObjCImplDecl::propimpl_iterator
1303 I = IMPDecl->propimpl_begin(),
1304 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1305 PropImplMap.insert((*I)->getPropertyDecl());
1306
1307 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1308 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1309 ObjCPropertyDecl *Prop = P->second;
1310 // Is there a matching propery synthesize/dynamic?
1311 if (Prop->isInvalidDecl() ||
1312 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001313 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001314 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001315 if (!InsMap.count(Prop->getGetterName())) {
1316 Diag(Prop->getLocation(),
1317 isa<ObjCCategoryDecl>(CDecl) ?
1318 diag::warn_setter_getter_impl_required_in_category :
1319 diag::warn_setter_getter_impl_required)
1320 << Prop->getDeclName() << Prop->getGetterName();
1321 Diag(IMPDecl->getLocation(),
1322 diag::note_property_impl_required);
1323 }
1324
1325 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1326 Diag(Prop->getLocation(),
1327 isa<ObjCCategoryDecl>(CDecl) ?
1328 diag::warn_setter_getter_impl_required_in_category :
1329 diag::warn_setter_getter_impl_required)
1330 << Prop->getDeclName() << Prop->getSetterName();
1331 Diag(IMPDecl->getLocation(),
1332 diag::note_property_impl_required);
1333 }
1334 }
1335}
1336
1337void
1338Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1339 ObjCContainerDecl* IDecl) {
1340 // Rules apply in non-GC mode only
1341 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1342 return;
1343 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1344 E = IDecl->prop_end();
1345 I != E; ++I) {
1346 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001347 ObjCMethodDecl *GetterMethod = 0;
1348 ObjCMethodDecl *SetterMethod = 0;
1349 bool LookedUpGetterSetter = false;
1350
Ted Kremenek9d64c152010-03-12 00:38:38 +00001351 unsigned Attributes = Property->getPropertyAttributes();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001352 unsigned AttributesAsWrittern = Property->getPropertyAttributesAsWritten();
1353
Fariborz Jahanian45937ae2011-06-11 00:45:12 +00001354 if (!(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_atomic) &&
1355 !(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001356 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1357 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1358 LookedUpGetterSetter = true;
1359 if (GetterMethod) {
1360 Diag(GetterMethod->getLocation(),
1361 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001362 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001363 Diag(Property->getLocation(), diag::note_property_declare);
1364 }
1365 if (SetterMethod) {
1366 Diag(SetterMethod->getLocation(),
1367 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001368 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001369 Diag(Property->getLocation(), diag::note_property_declare);
1370 }
1371 }
1372
Ted Kremenek9d64c152010-03-12 00:38:38 +00001373 // We only care about readwrite atomic property.
1374 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1375 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1376 continue;
1377 if (const ObjCPropertyImplDecl *PIDecl
1378 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1379 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1380 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001381 if (!LookedUpGetterSetter) {
1382 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1383 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1384 LookedUpGetterSetter = true;
1385 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001386 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1387 SourceLocation MethodLoc =
1388 (GetterMethod ? GetterMethod->getLocation()
1389 : SetterMethod->getLocation());
1390 Diag(MethodLoc, diag::warn_atomic_property_rule)
1391 << Property->getIdentifier();
1392 Diag(Property->getLocation(), diag::note_property_declare);
1393 }
1394 }
1395 }
1396}
1397
John McCallf85e1932011-06-15 23:02:42 +00001398void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
1399 if (getLangOptions().getGCMode() == LangOptions::GCOnly)
1400 return;
1401
1402 for (ObjCImplementationDecl::propimpl_iterator
1403 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1404 ObjCPropertyImplDecl *PID = *i;
1405 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1406 continue;
1407
1408 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001409 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1410 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001411 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1412 if (!method)
1413 continue;
1414 ObjCMethodFamily family = method->getMethodFamily();
1415 if (family == OMF_alloc || family == OMF_copy ||
1416 family == OMF_mutableCopy || family == OMF_new) {
1417 if (getLangOptions().ObjCAutoRefCount)
1418 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1419 else
1420 Diag(PID->getLocation(), diag::warn_ownin_getter_rule);
1421 Diag(PD->getLocation(), diag::note_property_declare);
1422 }
1423 }
1424 }
1425}
1426
John McCall5de74d12010-11-10 07:01:40 +00001427/// AddPropertyAttrs - Propagates attributes from a property to the
1428/// implicitly-declared getter or setter for that property.
1429static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1430 ObjCPropertyDecl *Property) {
1431 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001432 for (Decl::attr_iterator A = Property->attr_begin(),
1433 AEnd = Property->attr_end();
1434 A != AEnd; ++A) {
1435 if (isa<DeprecatedAttr>(*A) ||
1436 isa<UnavailableAttr>(*A) ||
1437 isa<AvailabilityAttr>(*A))
1438 PropertyMethod->addAttr((*A)->clone(S.Context));
1439 }
John McCall5de74d12010-11-10 07:01:40 +00001440}
1441
Ted Kremenek9d64c152010-03-12 00:38:38 +00001442/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1443/// have the property type and issue diagnostics if they don't.
1444/// Also synthesize a getter/setter method if none exist (and update the
1445/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1446/// methods is the "right" thing to do.
1447void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001448 ObjCContainerDecl *CD,
1449 ObjCPropertyDecl *redeclaredProperty,
1450 ObjCContainerDecl *lexicalDC) {
1451
Ted Kremenek9d64c152010-03-12 00:38:38 +00001452 ObjCMethodDecl *GetterMethod, *SetterMethod;
1453
1454 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1455 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1456 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1457 property->getLocation());
1458
1459 if (SetterMethod) {
1460 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1461 property->getPropertyAttributes();
1462 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1463 Context.getCanonicalType(SetterMethod->getResultType()) !=
1464 Context.VoidTy)
1465 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1466 if (SetterMethod->param_size() != 1 ||
1467 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1468 Diag(property->getLocation(),
1469 diag::warn_accessor_property_type_mismatch)
1470 << property->getDeclName()
1471 << SetterMethod->getSelector();
1472 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1473 }
1474 }
1475
1476 // Synthesize getter/setter methods if none exist.
1477 // Find the default getter and if one not found, add one.
1478 // FIXME: The synthesized property we set here is misleading. We almost always
1479 // synthesize these methods unless the user explicitly provided prototypes
1480 // (which is odd, but allowed). Sema should be typechecking that the
1481 // declarations jive in that situation (which it is not currently).
1482 if (!GetterMethod) {
1483 // No instance method of same name as property getter name was found.
1484 // Declare a getter method and add it to the list of methods
1485 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001486 SourceLocation Loc = redeclaredProperty ?
1487 redeclaredProperty->getLocation() :
1488 property->getLocation();
1489
1490 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1491 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001492 property->getType(), 0, CD, /*isInstance=*/true,
1493 /*isVariadic=*/false, /*isSynthesized=*/true,
1494 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001495 (property->getPropertyImplementation() ==
1496 ObjCPropertyDecl::Optional) ?
1497 ObjCMethodDecl::Optional :
1498 ObjCMethodDecl::Required);
1499 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001500
1501 AddPropertyAttrs(*this, GetterMethod, property);
1502
Ted Kremenek23173d72010-05-18 21:09:07 +00001503 // FIXME: Eventually this shouldn't be needed, as the lexical context
1504 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001505 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001506 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001507 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1508 GetterMethod->addAttr(
1509 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001510 } else
1511 // A user declared getter will be synthesize when @synthesize of
1512 // the property with the same name is seen in the @implementation
1513 GetterMethod->setSynthesized(true);
1514 property->setGetterMethodDecl(GetterMethod);
1515
1516 // Skip setter if property is read-only.
1517 if (!property->isReadOnly()) {
1518 // Find the default setter and if one not found, add one.
1519 if (!SetterMethod) {
1520 // No instance method of same name as property setter name was found.
1521 // Declare a setter method and add it to the list of methods
1522 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001523 SourceLocation Loc = redeclaredProperty ?
1524 redeclaredProperty->getLocation() :
1525 property->getLocation();
1526
1527 SetterMethod =
1528 ObjCMethodDecl::Create(Context, Loc, Loc,
1529 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001530 CD, /*isInstance=*/true, /*isVariadic=*/false,
1531 /*isSynthesized=*/true,
1532 /*isImplicitlyDeclared=*/true,
1533 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001534 (property->getPropertyImplementation() ==
1535 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001536 ObjCMethodDecl::Optional :
1537 ObjCMethodDecl::Required);
1538
Ted Kremenek9d64c152010-03-12 00:38:38 +00001539 // Invent the arguments for the setter. We don't bother making a
1540 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001541 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1542 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001543 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001544 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001545 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001546 SC_None,
1547 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001548 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001549 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001550
1551 AddPropertyAttrs(*this, SetterMethod, property);
1552
Ted Kremenek9d64c152010-03-12 00:38:38 +00001553 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001554 // FIXME: Eventually this shouldn't be needed, as the lexical context
1555 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001556 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001557 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001558 } else
1559 // A user declared setter will be synthesize when @synthesize of
1560 // the property with the same name is seen in the @implementation
1561 SetterMethod->setSynthesized(true);
1562 property->setSetterMethodDecl(SetterMethod);
1563 }
1564 // Add any synthesized methods to the global pool. This allows us to
1565 // handle the following, which is supported by GCC (and part of the design).
1566 //
1567 // @interface Foo
1568 // @property double bar;
1569 // @end
1570 //
1571 // void thisIsUnfortunate() {
1572 // id foo;
1573 // double bar = [foo bar];
1574 // }
1575 //
1576 if (GetterMethod)
1577 AddInstanceMethodToGlobalPool(GetterMethod);
1578 if (SetterMethod)
1579 AddInstanceMethodToGlobalPool(SetterMethod);
1580}
1581
John McCalld226f652010-08-21 09:40:31 +00001582void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001583 SourceLocation Loc,
1584 unsigned &Attributes) {
1585 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001586 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001587 return;
1588
1589 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001590 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001591
1592 // readonly and readwrite/assign/retain/copy conflict.
1593 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1594 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1595 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001596 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001597 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001598 ObjCDeclSpec::DQ_PR_retain |
1599 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001600 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1601 "readwrite" :
1602 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1603 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001604 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1605 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001606 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1607 "copy" : "retain";
1608
1609 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1610 diag::err_objc_property_attr_mutually_exclusive :
1611 diag::warn_objc_property_attr_mutually_exclusive)
1612 << "readonly" << which;
1613 }
1614
1615 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001616 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1617 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1618 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001619 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001620 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001621 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1622 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1623 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1624 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001625 }
1626
1627 // Check for more than one of { assign, copy, retain }.
1628 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1629 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1630 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1631 << "assign" << "copy";
1632 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1633 }
1634 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1635 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1636 << "assign" << "retain";
1637 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1638 }
John McCallf85e1932011-06-15 23:02:42 +00001639 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1640 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1641 << "assign" << "strong";
1642 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1643 }
1644 if (getLangOptions().ObjCAutoRefCount &&
1645 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1646 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1647 << "assign" << "weak";
1648 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1649 }
1650 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1651 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1652 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1653 << "unsafe_unretained" << "copy";
1654 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1655 }
1656 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1657 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1658 << "unsafe_unretained" << "retain";
1659 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1660 }
1661 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1662 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1663 << "unsafe_unretained" << "strong";
1664 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1665 }
1666 if (getLangOptions().ObjCAutoRefCount &&
1667 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1668 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1669 << "unsafe_unretained" << "weak";
1670 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1671 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001672 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1673 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1674 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1675 << "copy" << "retain";
1676 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1677 }
John McCallf85e1932011-06-15 23:02:42 +00001678 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1679 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1680 << "copy" << "strong";
1681 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1682 }
1683 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1684 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1685 << "copy" << "weak";
1686 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1687 }
1688 }
1689 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1690 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1691 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1692 << "retain" << "weak";
1693 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1694 }
1695 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1696 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1697 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1698 << "strong" << "weak";
1699 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001700 }
1701
1702 // Warn if user supplied no assignment attribute, property is
1703 // readwrite, and this is an object type.
1704 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001705 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1706 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1707 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001708 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1709 PropertyTy->isObjCObjectPointerType()) {
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +00001710 // Skip this warning in gc-only mode.
1711 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1712 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001713
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +00001714 // If non-gc code warn that this is likely inappropriate.
1715 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1716 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001717
1718 // FIXME: Implement warning dependent on NSCopying being
1719 // implemented. See also:
1720 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1721 // (please trim this list while you are at it).
1722 }
1723
1724 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001725 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001726 && getLangOptions().getGCMode() == LangOptions::GCOnly
1727 && PropertyTy->isBlockPointerType())
1728 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1729}