blob: 33c5e71b5eb47975488cd694f2fc4bc1b125df56 [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 McCall265941b2011-09-13 18:31:23 +000027/// getImpliedARCOwnership - Given a set of property attributes and a
28/// type, infer an expected lifetime. The type's ownership qualification
29/// is not considered.
30///
31/// Returns OCL_None if the attributes as stated do not imply an ownership.
32/// Never returns OCL_Autoreleasing.
33static Qualifiers::ObjCLifetime getImpliedARCOwnership(
34 ObjCPropertyDecl::PropertyAttributeKind attrs,
35 QualType type) {
36 // retain, strong, copy, weak, and unsafe_unretained are only legal
37 // on properties of retainable pointer type.
38 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
39 ObjCPropertyDecl::OBJC_PR_strong |
40 ObjCPropertyDecl::OBJC_PR_copy)) {
41 return Qualifiers::OCL_Strong;
42 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
43 return Qualifiers::OCL_Weak;
44 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
45 return Qualifiers::OCL_ExplicitNone;
46 }
47
48 // assign can appear on other types, so we have to check the
49 // property type.
50 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
51 type->isObjCRetainableType()) {
52 return Qualifiers::OCL_ExplicitNone;
53 }
54
55 return Qualifiers::OCL_None;
56}
57
John McCallf85e1932011-06-15 23:02:42 +000058/// Check the internal consistency of a property declaration.
59static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
60 if (property->isInvalidDecl()) return;
61
62 ObjCPropertyDecl::PropertyAttributeKind propertyKind
63 = property->getPropertyAttributes();
64 Qualifiers::ObjCLifetime propertyLifetime
65 = property->getType().getObjCLifetime();
66
67 // Nothing to do if we don't have a lifetime.
68 if (propertyLifetime == Qualifiers::OCL_None) return;
69
John McCall265941b2011-09-13 18:31:23 +000070 Qualifiers::ObjCLifetime expectedLifetime
71 = getImpliedARCOwnership(propertyKind, property->getType());
72 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000073 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000074 // attribute. That's okay, but restore reasonable invariants by
75 // setting the property attribute according to the lifetime
76 // qualifier.
77 ObjCPropertyDecl::PropertyAttributeKind attr;
78 if (propertyLifetime == Qualifiers::OCL_Strong) {
79 attr = ObjCPropertyDecl::OBJC_PR_strong;
80 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
81 attr = ObjCPropertyDecl::OBJC_PR_weak;
82 } else {
83 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
84 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
85 }
86 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000087 return;
88 }
89
90 if (propertyLifetime == expectedLifetime) return;
91
92 property->setInvalidDecl();
93 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000094 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +000095 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +000096 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +000097 << propertyLifetime;
98}
99
John McCalld226f652010-08-21 09:40:31 +0000100Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
101 FieldDeclarator &FD,
102 ObjCDeclSpec &ODS,
103 Selector GetterSel,
104 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000105 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000106 tok::ObjCKeywordKind MethodImplKind,
107 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000108 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000109 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
110 QualType T = TSI->getType();
Douglas Gregore289d812011-09-13 17:21:33 +0000111 if ((getLangOptions().getGC() != LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +0000112 T.isObjCGCWeak()) ||
113 (getLangOptions().ObjCAutoRefCount &&
114 T.getObjCLifetime() == Qualifiers::OCL_Weak))
115 Attributes |= ObjCDeclSpec::DQ_PR_weak;
116
Ted Kremenek28685ab2010-03-12 00:46:40 +0000117 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
118 // default is readwrite!
119 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
120 // property is defaulted to 'assign' if it is readwrite and is
121 // not retain or copy
122 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
123 (isReadWrite &&
124 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +0000125 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
126 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
127 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
128 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000129
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000130 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000131 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000132
Ted Kremenek28685ab2010-03-12 00:46:40 +0000133 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000134 if (CDecl->IsClassExtension()) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000135 Decl *Res = HandlePropertyInClassExtension(S, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000136 FD, GetterSel, SetterSel,
137 isAssign, isReadWrite,
138 Attributes,
139 isOverridingProperty, TSI,
140 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000141 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000142 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000143 if (getLangOptions().ObjCAutoRefCount)
144 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
145 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000146 return Res;
147 }
148
John McCallf85e1932011-06-15 23:02:42 +0000149 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
150 GetterSel, SetterSel,
151 isAssign, isReadWrite,
152 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000153 if (lexicalDC)
154 Res->setLexicalDeclContext(lexicalDC);
155
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000156 // Validate the attributes on the @property.
157 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000158
159 if (getLangOptions().ObjCAutoRefCount)
160 checkARCPropertyDecl(*this, Res);
161
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000162 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000163}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000164
John McCalld226f652010-08-21 09:40:31 +0000165Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000166Sema::HandlePropertyInClassExtension(Scope *S,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000167 SourceLocation AtLoc, FieldDeclarator &FD,
168 Selector GetterSel, Selector SetterSel,
169 const bool isAssign,
170 const bool isReadWrite,
171 const unsigned Attributes,
172 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000173 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000174 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000175 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000176 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000177 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000178 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000179 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
180
181 if (CCPrimary)
182 // Check for duplicate declaration of this property in current and
183 // other class extensions.
184 for (const ObjCCategoryDecl *ClsExtDecl =
185 CCPrimary->getFirstClassExtension();
186 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
187 if (ObjCPropertyDecl *prevDecl =
188 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
189 Diag(AtLoc, diag::err_duplicate_property);
190 Diag(prevDecl->getLocation(), diag::note_property_declare);
191 return 0;
192 }
193 }
194
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000195 // Create a new ObjCPropertyDecl with the DeclContext being
196 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000197 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000198 ObjCPropertyDecl *PDecl =
199 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
200 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000201 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
202 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
203 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
204 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000205 // Set setter/getter selector name. Needed later.
206 PDecl->setGetterName(GetterSel);
207 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000208 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000209 DC->addDecl(PDecl);
210
211 // We need to look in the @interface to see if the @property was
212 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000213 if (!CCPrimary) {
214 Diag(CDecl->getLocation(), diag::err_continuation_class);
215 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000216 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000217 }
218
219 // Find the property in continuation class's primary class only.
220 ObjCPropertyDecl *PIDecl =
221 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
222
223 if (!PIDecl) {
224 // No matching property found in the primary class. Just fall thru
225 // and add property to continuation class's primary class.
226 ObjCPropertyDecl *PDecl =
227 CreatePropertyDecl(S, CCPrimary, AtLoc,
228 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000229 Attributes, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000230
231 // A case of continuation class adding a new property in the class. This
232 // is not what it was meant for. However, gcc supports it and so should we.
233 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000234 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
235 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000236 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000237 }
238
239 // The property 'PIDecl's readonly attribute will be over-ridden
240 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000241 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000242 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
243 unsigned retainCopyNonatomic =
244 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000245 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000246 ObjCPropertyDecl::OBJC_PR_copy |
247 ObjCPropertyDecl::OBJC_PR_nonatomic);
248 if ((Attributes & retainCopyNonatomic) !=
249 (PIkind & retainCopyNonatomic)) {
250 Diag(AtLoc, diag::warn_property_attr_mismatch);
251 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000252 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000253 DeclContext *DC = cast<DeclContext>(CCPrimary);
254 if (!ObjCPropertyDecl::findPropertyDecl(DC,
255 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000256 // Protocol is not in the primary class. Must build one for it.
257 ObjCDeclSpec ProtocolPropertyODS;
258 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
259 // and ObjCPropertyDecl::PropertyAttributeKind have identical
260 // values. Should consolidate both into one enum type.
261 ProtocolPropertyODS.
262 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
263 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000264 // Must re-establish the context from class extension to primary
265 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000266 ContextRAII SavedContext(*this, CCPrimary);
267
John McCalld226f652010-08-21 09:40:31 +0000268 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000269 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
270 PIDecl->getGetterName(),
271 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000272 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000273 MethodImplKind,
274 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000275 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000276 }
277 PIDecl->makeitReadWriteAttribute();
278 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
279 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000280 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
281 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000282 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
283 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
284 PIDecl->setSetterName(SetterSel);
285 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000286 // Tailor the diagnostics for the common case where a readwrite
287 // property is declared both in the @interface and the continuation.
288 // This is a common error where the user often intended the original
289 // declaration to be readonly.
290 unsigned diag =
291 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
292 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
293 ? diag::err_use_continuation_class_redeclaration_readwrite
294 : diag::err_use_continuation_class;
295 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000296 << CCPrimary->getDeclName();
297 Diag(PIDecl->getLocation(), diag::note_property_declare);
298 }
299 *isOverridingProperty = true;
300 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000301 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000302 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000303}
304
305ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
306 ObjCContainerDecl *CDecl,
307 SourceLocation AtLoc,
308 FieldDeclarator &FD,
309 Selector GetterSel,
310 Selector SetterSel,
311 const bool isAssign,
312 const bool isReadWrite,
313 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000314 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000315 tok::ObjCKeywordKind MethodImplKind,
316 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000317 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000318 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000319
320 // Issue a warning if property is 'assign' as default and its object, which is
321 // gc'able conforms to NSCopying protocol
Douglas Gregore289d812011-09-13 17:21:33 +0000322 if (getLangOptions().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000323 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000324 if (const ObjCObjectPointerType *ObjPtrTy =
325 T->getAs<ObjCObjectPointerType>()) {
326 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
327 if (IDecl)
328 if (ObjCProtocolDecl* PNSCopying =
329 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
330 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
331 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000332 }
John McCallc12c5bb2010-05-15 11:32:37 +0000333 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000334 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
335
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000336 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000337 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
338 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000339 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000340
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000341 if (ObjCPropertyDecl *prevDecl =
342 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000343 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000344 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000345 PDecl->setInvalidDecl();
346 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000347 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000348 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000349 if (lexicalDC)
350 PDecl->setLexicalDeclContext(lexicalDC);
351 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000352
353 if (T->isArrayType() || T->isFunctionType()) {
354 Diag(AtLoc, diag::err_property_type) << T;
355 PDecl->setInvalidDecl();
356 }
357
358 ProcessDeclAttributes(S, PDecl, FD.D);
359
360 // Regardless of setter/getter attribute, we save the default getter/setter
361 // selector names in anticipation of declaration of setter/getter methods.
362 PDecl->setGetterName(GetterSel);
363 PDecl->setSetterName(SetterSel);
364
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000365 unsigned attributesAsWritten = 0;
366 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
367 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
368 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
369 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
370 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
371 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
372 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
373 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
374 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
375 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
376 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
377 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
378 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
379 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
380 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
381 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
382 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
383 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
384 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
385 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
386 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
387 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
388 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
389 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
390
391 PDecl->setPropertyAttributesAsWritten(
392 (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten);
393
Ted Kremenek28685ab2010-03-12 00:46:40 +0000394 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
395 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
396
397 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
398 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
399
400 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
401 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
402
403 if (isReadWrite)
404 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
405
406 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
407 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
408
John McCallf85e1932011-06-15 23:02:42 +0000409 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
410 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
411
412 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
413 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
414
Ted Kremenek28685ab2010-03-12 00:46:40 +0000415 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
416 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
417
John McCallf85e1932011-06-15 23:02:42 +0000418 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
419 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
420
Ted Kremenek28685ab2010-03-12 00:46:40 +0000421 if (isAssign)
422 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
423
John McCall265941b2011-09-13 18:31:23 +0000424 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000425 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
426 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000427 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000428 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000429
John McCallf85e1932011-06-15 23:02:42 +0000430 // 'unsafe_unretained' is alias for 'assign'.
431 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
432 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
433 if (isAssign)
434 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
435
Ted Kremenek28685ab2010-03-12 00:46:40 +0000436 if (MethodImplKind == tok::objc_required)
437 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
438 else if (MethodImplKind == tok::objc_optional)
439 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000440
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000441 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000442}
443
John McCallf85e1932011-06-15 23:02:42 +0000444static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
445 ObjCPropertyDecl *property,
446 ObjCIvarDecl *ivar) {
447 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
448
John McCallf85e1932011-06-15 23:02:42 +0000449 QualType ivarType = ivar->getType();
450 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000451
John McCall265941b2011-09-13 18:31:23 +0000452 // The lifetime implied by the property's attributes.
453 Qualifiers::ObjCLifetime propertyLifetime =
454 getImpliedARCOwnership(property->getPropertyAttributes(),
455 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000456
John McCall265941b2011-09-13 18:31:23 +0000457 // We're fine if they match.
458 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000459
John McCall265941b2011-09-13 18:31:23 +0000460 // These aren't valid lifetimes for object ivars; don't diagnose twice.
461 if (ivarLifetime == Qualifiers::OCL_None ||
462 ivarLifetime == Qualifiers::OCL_Autoreleasing)
463 return;
John McCallf85e1932011-06-15 23:02:42 +0000464
John McCall265941b2011-09-13 18:31:23 +0000465 switch (propertyLifetime) {
466 case Qualifiers::OCL_Strong:
467 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
468 << property->getDeclName()
469 << ivar->getDeclName()
470 << ivarLifetime;
471 break;
John McCallf85e1932011-06-15 23:02:42 +0000472
John McCall265941b2011-09-13 18:31:23 +0000473 case Qualifiers::OCL_Weak:
474 S.Diag(propertyImplLoc, diag::error_weak_property)
475 << property->getDeclName()
476 << ivar->getDeclName();
477 break;
John McCallf85e1932011-06-15 23:02:42 +0000478
John McCall265941b2011-09-13 18:31:23 +0000479 case Qualifiers::OCL_ExplicitNone:
480 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
481 << property->getDeclName()
482 << ivar->getDeclName()
483 << ((property->getPropertyAttributesAsWritten()
484 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
485 break;
John McCallf85e1932011-06-15 23:02:42 +0000486
John McCall265941b2011-09-13 18:31:23 +0000487 case Qualifiers::OCL_Autoreleasing:
488 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000489
John McCall265941b2011-09-13 18:31:23 +0000490 case Qualifiers::OCL_None:
491 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000492 return;
493 }
494
495 S.Diag(property->getLocation(), diag::note_property_declare);
496}
497
Ted Kremenek28685ab2010-03-12 00:46:40 +0000498
499/// ActOnPropertyImplDecl - This routine performs semantic checks and
500/// builds the AST node for a property implementation declaration; declared
501/// as @synthesize or @dynamic.
502///
John McCalld226f652010-08-21 09:40:31 +0000503Decl *Sema::ActOnPropertyImplDecl(Scope *S,
504 SourceLocation AtLoc,
505 SourceLocation PropertyLoc,
506 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000507 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000508 IdentifierInfo *PropertyIvar,
509 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000510 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000511 cast_or_null<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000512 // Make sure we have a context for the property implementation declaration.
513 if (!ClassImpDecl) {
514 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000515 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000516 }
517 ObjCPropertyDecl *property = 0;
518 ObjCInterfaceDecl* IDecl = 0;
519 // Find the class or category class where this property must have
520 // a declaration.
521 ObjCImplementationDecl *IC = 0;
522 ObjCCategoryImplDecl* CatImplClass = 0;
523 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
524 IDecl = IC->getClassInterface();
525 // We always synthesize an interface for an implementation
526 // without an interface decl. So, IDecl is always non-zero.
527 assert(IDecl &&
528 "ActOnPropertyImplDecl - @implementation without @interface");
529
530 // Look for this property declaration in the @implementation's @interface
531 property = IDecl->FindPropertyDeclaration(PropertyId);
532 if (!property) {
533 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000534 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000535 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000536 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000537 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
538 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000539 if (AtLoc.isValid())
540 Diag(AtLoc, diag::warn_implicit_atomic_property);
541 else
542 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
543 Diag(property->getLocation(), diag::note_property_declare);
544 }
545
Ted Kremenek28685ab2010-03-12 00:46:40 +0000546 if (const ObjCCategoryDecl *CD =
547 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
548 if (!CD->IsClassExtension()) {
549 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
550 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000551 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000552 }
553 }
554 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
555 if (Synthesize) {
556 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000557 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000558 }
559 IDecl = CatImplClass->getClassInterface();
560 if (!IDecl) {
561 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000562 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000563 }
564 ObjCCategoryDecl *Category =
565 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
566
567 // If category for this implementation not found, it is an error which
568 // has already been reported eralier.
569 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000570 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000571 // Look for this property declaration in @implementation's category
572 property = Category->FindPropertyDeclaration(PropertyId);
573 if (!property) {
574 Diag(PropertyLoc, diag::error_bad_category_property_decl)
575 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000576 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000577 }
578 } else {
579 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000580 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000581 }
582 ObjCIvarDecl *Ivar = 0;
583 // Check that we have a valid, previously declared ivar for @synthesize
584 if (Synthesize) {
585 // @synthesize
586 if (!PropertyIvar)
587 PropertyIvar = PropertyId;
John McCallf85e1932011-06-15 23:02:42 +0000588 ObjCPropertyDecl::PropertyAttributeKind kind
589 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000590 QualType PropType = property->getType();
591
592 QualType PropertyIvarType = PropType.getNonReferenceType();
593
594 // Add GC __weak to the ivar type if the property is weak.
595 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
Douglas Gregore289d812011-09-13 17:21:33 +0000596 getLangOptions().getGC() != LangOptions::NonGC) {
John McCall265941b2011-09-13 18:31:23 +0000597 assert(!getLangOptions().ObjCAutoRefCount);
598 if (PropertyIvarType.isObjCGCStrong()) {
599 Diag(PropertyLoc, diag::err_gc_weak_property_strong_type);
600 Diag(property->getLocation(), diag::note_property_declare);
601 } else {
602 PropertyIvarType =
603 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000604 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000605 }
John McCall265941b2011-09-13 18:31:23 +0000606
Ted Kremenek28685ab2010-03-12 00:46:40 +0000607 // Check that this is a previously declared 'ivar' in 'IDecl' interface
608 ObjCInterfaceDecl *ClassDeclared;
609 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
610 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000611 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000612 // property attributes.
613 if (getLangOptions().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000614 !PropertyIvarType.getObjCLifetime() &&
615 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000616
John McCall265941b2011-09-13 18:31:23 +0000617 // It's an error if we have to do this and the user didn't
618 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000619 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000620 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000621 Diag(PropertyLoc,
622 diag::err_arc_objc_property_default_assign_on_object);
623 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000624 } else {
625 Qualifiers::ObjCLifetime lifetime =
626 getImpliedARCOwnership(kind, PropertyIvarType);
627 assert(lifetime && "no lifetime for property?");
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000628
John McCall265941b2011-09-13 18:31:23 +0000629 if (lifetime == Qualifiers::OCL_Weak &&
630 !getLangOptions().ObjCRuntimeHasWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000631 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
632 Diag(property->getLocation(), diag::note_property_declare);
633 }
John McCall265941b2011-09-13 18:31:23 +0000634
John McCallf85e1932011-06-15 23:02:42 +0000635 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000636 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000637 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
638 }
John McCallf85e1932011-06-15 23:02:42 +0000639 }
640
641 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
642 !getLangOptions().ObjCAutoRefCount &&
Douglas Gregore289d812011-09-13 17:21:33 +0000643 getLangOptions().getGC() == LangOptions::NonGC) {
John McCallf85e1932011-06-15 23:02:42 +0000644 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.
John McCall265941b2011-09-13 18:31:23 +0000673 if (Context.getCanonicalType(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() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000712 getLangOptions().getGC() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000713 Diag(PropertyLoc, diag::error_weak_property)
714 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000715 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000716 // Fall thru - see previous comment
717 }
John McCallf85e1932011-06-15 23:02:42 +0000718 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000719 if ((property->getType()->isObjCObjectPointerType() ||
720 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000721 getLangOptions().getGC() != LangOptions::NonGC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000722 Diag(PropertyLoc, diag::error_strong_property)
723 << property->getDeclName() << Ivar->getDeclName();
724 // Fall thru - see previous comment
725 }
726 }
John McCallf85e1932011-06-15 23:02:42 +0000727 if (getLangOptions().ObjCAutoRefCount)
728 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000729 } else if (PropertyIvar)
730 // @dynamic
731 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000732
Ted Kremenek28685ab2010-03-12 00:46:40 +0000733 assert (property && "ActOnPropertyImplDecl - property declaration missing");
734 ObjCPropertyImplDecl *PIDecl =
735 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
736 property,
737 (Synthesize ?
738 ObjCPropertyImplDecl::Synthesize
739 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000740 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000741 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
742 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000743 if (getLangOptions().CPlusPlus && Synthesize &&
744 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000745 // For Objective-C++, need to synthesize the AST for the IVAR object to be
746 // returned by the getter as it must conform to C++'s copy-return rules.
747 // FIXME. Eventually we want to do this for Objective-C as well.
748 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
749 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000750 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
751 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000752 Expr *IvarRefExpr =
753 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
754 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000755 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000756 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000757 SourceLocation(),
758 getterMethod->getResultType(),
759 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000760 SourceLocation(),
761 Owned(IvarRefExpr));
762 if (!Res.isInvalid()) {
763 Expr *ResExpr = Res.takeAs<Expr>();
764 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000765 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000766 PIDecl->setGetterCXXConstructor(ResExpr);
767 }
768 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000769 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
770 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
771 Diag(getterMethod->getLocation(),
772 diag::warn_property_getter_owning_mismatch);
773 Diag(property->getLocation(), diag::note_property_declare);
774 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000775 }
776 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
777 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000778 if (getLangOptions().CPlusPlus && Synthesize
779 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000780 // FIXME. Eventually we want to do this for Objective-C as well.
781 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
782 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000783 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
784 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000785 Expr *lhs =
786 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
787 SelfExpr, true, true);
788 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
789 ParmVarDecl *Param = (*P);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000790 QualType T = Param->getType();
791 if (T->isReferenceType())
Fariborz Jahanian61750f22011-03-30 16:59:30 +0000792 T = T->getAs<ReferenceType>()->getPointeeType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000793 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000794 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000795 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000796 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000797 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
798 }
799 }
800
Ted Kremenek28685ab2010-03-12 00:46:40 +0000801 if (IC) {
802 if (Synthesize)
803 if (ObjCPropertyImplDecl *PPIDecl =
804 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
805 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
806 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
807 << PropertyIvar;
808 Diag(PPIDecl->getLocation(), diag::note_previous_use);
809 }
810
811 if (ObjCPropertyImplDecl *PPIDecl
812 = IC->FindPropertyImplDecl(PropertyId)) {
813 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
814 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000815 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000816 }
817 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000818 if (getLangOptions().ObjCDefaultSynthProperties &&
819 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000820 // Diagnose if an ivar was lazily synthesdized due to a previous
821 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000822 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000823 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000824 ObjCIvarDecl *Ivar = 0;
825 if (!Synthesize)
826 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
827 else {
828 if (PropertyIvar && PropertyIvar != PropertyId)
829 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
830 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000831 // Issue diagnostics only if Ivar belongs to current class.
832 if (Ivar && Ivar->getSynthesize() &&
833 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000834 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
835 << PropertyId;
836 Ivar->setInvalidDecl();
837 }
838 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000839 } else {
840 if (Synthesize)
841 if (ObjCPropertyImplDecl *PPIDecl =
842 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
843 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
844 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
845 << PropertyIvar;
846 Diag(PPIDecl->getLocation(), diag::note_previous_use);
847 }
848
849 if (ObjCPropertyImplDecl *PPIDecl =
850 CatImplClass->FindPropertyImplDecl(PropertyId)) {
851 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
852 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000853 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000854 }
855 CatImplClass->addPropertyImplementation(PIDecl);
856 }
857
John McCalld226f652010-08-21 09:40:31 +0000858 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000859}
860
861//===----------------------------------------------------------------------===//
862// Helper methods.
863//===----------------------------------------------------------------------===//
864
Ted Kremenek9d64c152010-03-12 00:38:38 +0000865/// DiagnosePropertyMismatch - Compares two properties for their
866/// attributes and types and warns on a variety of inconsistencies.
867///
868void
869Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
870 ObjCPropertyDecl *SuperProperty,
871 const IdentifierInfo *inheritedName) {
872 ObjCPropertyDecl::PropertyAttributeKind CAttr =
873 Property->getPropertyAttributes();
874 ObjCPropertyDecl::PropertyAttributeKind SAttr =
875 SuperProperty->getPropertyAttributes();
876 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
877 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
878 Diag(Property->getLocation(), diag::warn_readonly_property)
879 << Property->getDeclName() << inheritedName;
880 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
881 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
882 Diag(Property->getLocation(), diag::warn_property_attribute)
883 << Property->getDeclName() << "copy" << inheritedName;
John McCallf85e1932011-06-15 23:02:42 +0000884 else {
885 unsigned CAttrRetain =
886 (CAttr &
887 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
888 unsigned SAttrRetain =
889 (SAttr &
890 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
891 bool CStrong = (CAttrRetain != 0);
892 bool SStrong = (SAttrRetain != 0);
893 if (CStrong != SStrong)
894 Diag(Property->getLocation(), diag::warn_property_attribute)
895 << Property->getDeclName() << "retain (or strong)" << inheritedName;
896 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000897
898 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
899 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
900 Diag(Property->getLocation(), diag::warn_property_attribute)
901 << Property->getDeclName() << "atomic" << inheritedName;
902 if (Property->getSetterName() != SuperProperty->getSetterName())
903 Diag(Property->getLocation(), diag::warn_property_attribute)
904 << Property->getDeclName() << "setter" << inheritedName;
905 if (Property->getGetterName() != SuperProperty->getGetterName())
906 Diag(Property->getLocation(), diag::warn_property_attribute)
907 << Property->getDeclName() << "getter" << inheritedName;
908
909 QualType LHSType =
910 Context.getCanonicalType(SuperProperty->getType());
911 QualType RHSType =
912 Context.getCanonicalType(Property->getType());
913
Fariborz Jahanianc286f382011-07-12 22:05:16 +0000914 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000915 // Do cases not handled in above.
916 // FIXME. For future support of covariant property types, revisit this.
917 bool IncompatibleObjC = false;
918 QualType ConvertedType;
919 if (!isObjCPointerConversion(RHSType, LHSType,
920 ConvertedType, IncompatibleObjC) ||
921 IncompatibleObjC)
922 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
923 << Property->getType() << SuperProperty->getType() << inheritedName;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000924 }
925}
926
927bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
928 ObjCMethodDecl *GetterMethod,
929 SourceLocation Loc) {
930 if (GetterMethod &&
931 GetterMethod->getResultType() != property->getType()) {
932 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000933 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000934 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000935 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000936 if (result != Compatible) {
937 Diag(Loc, diag::warn_accessor_property_type_mismatch)
938 << property->getDeclName()
939 << GetterMethod->getSelector();
940 Diag(GetterMethod->getLocation(), diag::note_declared_at);
941 return true;
942 }
943 }
944 return false;
945}
946
947/// ComparePropertiesInBaseAndSuper - This routine compares property
948/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000949/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000950///
951void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
952 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
953 if (!SDecl)
954 return;
955 // FIXME: O(N^2)
956 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
957 E = SDecl->prop_end(); S != E; ++S) {
958 ObjCPropertyDecl *SuperPDecl = (*S);
959 // Does property in super class has declaration in current class?
960 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
961 E = IDecl->prop_end(); I != E; ++I) {
962 ObjCPropertyDecl *PDecl = (*I);
963 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
964 DiagnosePropertyMismatch(PDecl, SuperPDecl,
965 SDecl->getIdentifier());
966 }
967 }
968}
969
970/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
971/// of properties declared in a protocol and compares their attribute against
972/// the same property declared in the class or category.
973void
974Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
975 ObjCProtocolDecl *PDecl) {
976 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
977 if (!IDecl) {
978 // Category
979 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
980 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
981 if (!CatDecl->IsClassExtension())
982 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
983 E = PDecl->prop_end(); P != E; ++P) {
984 ObjCPropertyDecl *Pr = (*P);
985 ObjCCategoryDecl::prop_iterator CP, CE;
986 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000987 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000988 if ((*CP)->getIdentifier() == Pr->getIdentifier())
989 break;
990 if (CP != CE)
991 // Property protocol already exist in class. Diagnose any mismatch.
992 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
993 }
994 return;
995 }
996 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
997 E = PDecl->prop_end(); P != E; ++P) {
998 ObjCPropertyDecl *Pr = (*P);
999 ObjCInterfaceDecl::prop_iterator CP, CE;
1000 // Is this property already in class's list of properties?
1001 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1002 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1003 break;
1004 if (CP != CE)
1005 // Property protocol already exist in class. Diagnose any mismatch.
1006 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1007 }
1008}
1009
1010/// CompareProperties - This routine compares properties
1011/// declared in 'ClassOrProtocol' objects (which can be a class or an
1012/// inherited protocol with the list of properties for class/category 'CDecl'
1013///
John McCalld226f652010-08-21 09:40:31 +00001014void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1015 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001016 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1017
1018 if (!IDecl) {
1019 // Category
1020 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1021 assert (CatDecl && "CompareProperties");
1022 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1023 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1024 E = MDecl->protocol_end(); P != E; ++P)
1025 // Match properties of category with those of protocol (*P)
1026 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1027
1028 // Go thru the list of protocols for this category and recursively match
1029 // their properties with those in the category.
1030 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1031 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001032 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001033 } else {
1034 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1035 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1036 E = MD->protocol_end(); P != E; ++P)
1037 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1038 }
1039 return;
1040 }
1041
1042 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001043 for (ObjCInterfaceDecl::all_protocol_iterator
1044 P = MDecl->all_referenced_protocol_begin(),
1045 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001046 // Match properties of class IDecl with those of protocol (*P).
1047 MatchOneProtocolPropertiesInClass(IDecl, *P);
1048
1049 // Go thru the list of protocols for this class and recursively match
1050 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001051 for (ObjCInterfaceDecl::all_protocol_iterator
1052 P = IDecl->all_referenced_protocol_begin(),
1053 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001054 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001055 } else {
1056 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1057 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1058 E = MD->protocol_end(); P != E; ++P)
1059 MatchOneProtocolPropertiesInClass(IDecl, *P);
1060 }
1061}
1062
1063/// isPropertyReadonly - Return true if property is readonly, by searching
1064/// for the property in the class and in its categories and implementations
1065///
1066bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1067 ObjCInterfaceDecl *IDecl) {
1068 // by far the most common case.
1069 if (!PDecl->isReadOnly())
1070 return false;
1071 // Even if property is ready only, if interface has a user defined setter,
1072 // it is not considered read only.
1073 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1074 return false;
1075
1076 // Main class has the property as 'readonly'. Must search
1077 // through the category list to see if the property's
1078 // attribute has been over-ridden to 'readwrite'.
1079 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1080 Category; Category = Category->getNextClassCategory()) {
1081 // Even if property is ready only, if a category has a user defined setter,
1082 // it is not considered read only.
1083 if (Category->getInstanceMethod(PDecl->getSetterName()))
1084 return false;
1085 ObjCPropertyDecl *P =
1086 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1087 if (P && !P->isReadOnly())
1088 return false;
1089 }
1090
1091 // Also, check for definition of a setter method in the implementation if
1092 // all else failed.
1093 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1094 if (ObjCImplementationDecl *IMD =
1095 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1096 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1097 return false;
1098 } else if (ObjCCategoryImplDecl *CIMD =
1099 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1100 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1101 return false;
1102 }
1103 }
1104 // Lastly, look through the implementation (if one is in scope).
1105 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1106 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1107 return false;
1108 // If all fails, look at the super class.
1109 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1110 return isPropertyReadonly(PDecl, SIDecl);
1111 return true;
1112}
1113
1114/// CollectImmediateProperties - This routine collects all properties in
1115/// the class and its conforming protocols; but not those it its super class.
1116void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001117 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1118 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001119 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1120 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1121 E = IDecl->prop_end(); P != E; ++P) {
1122 ObjCPropertyDecl *Prop = (*P);
1123 PropMap[Prop->getIdentifier()] = Prop;
1124 }
1125 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001126 for (ObjCInterfaceDecl::all_protocol_iterator
1127 PI = IDecl->all_referenced_protocol_begin(),
1128 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001129 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001130 }
1131 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1132 if (!CATDecl->IsClassExtension())
1133 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1134 E = CATDecl->prop_end(); P != E; ++P) {
1135 ObjCPropertyDecl *Prop = (*P);
1136 PropMap[Prop->getIdentifier()] = Prop;
1137 }
1138 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001139 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001140 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001141 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001142 }
1143 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1144 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1145 E = PDecl->prop_end(); P != E; ++P) {
1146 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001147 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1148 // Exclude property for protocols which conform to class's super-class,
1149 // as super-class has to implement the property.
1150 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
1151 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1152 if (!PropEntry)
1153 PropEntry = Prop;
1154 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001155 }
1156 // scan through protocol's protocols.
1157 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1158 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001159 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001160 }
1161}
1162
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001163/// CollectClassPropertyImplementations - This routine collects list of
1164/// properties to be implemented in the class. This includes, class's
1165/// and its conforming protocols' properties.
1166static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1167 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1168 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1169 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1170 E = IDecl->prop_end(); P != E; ++P) {
1171 ObjCPropertyDecl *Prop = (*P);
1172 PropMap[Prop->getIdentifier()] = Prop;
1173 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001174 for (ObjCInterfaceDecl::all_protocol_iterator
1175 PI = IDecl->all_referenced_protocol_begin(),
1176 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001177 CollectClassPropertyImplementations((*PI), PropMap);
1178 }
1179 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1180 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1181 E = PDecl->prop_end(); P != E; ++P) {
1182 ObjCPropertyDecl *Prop = (*P);
1183 PropMap[Prop->getIdentifier()] = Prop;
1184 }
1185 // scan through protocol's protocols.
1186 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1187 E = PDecl->protocol_end(); PI != E; ++PI)
1188 CollectClassPropertyImplementations((*PI), PropMap);
1189 }
1190}
1191
1192/// CollectSuperClassPropertyImplementations - This routine collects list of
1193/// properties to be implemented in super class(s) and also coming from their
1194/// conforming protocols.
1195static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1196 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1197 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1198 while (SDecl) {
1199 CollectClassPropertyImplementations(SDecl, PropMap);
1200 SDecl = SDecl->getSuperClass();
1201 }
1202 }
1203}
1204
Ted Kremenek9d64c152010-03-12 00:38:38 +00001205/// LookupPropertyDecl - Looks up a property in the current class and all
1206/// its protocols.
1207ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1208 IdentifierInfo *II) {
1209 if (const ObjCInterfaceDecl *IDecl =
1210 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1211 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1212 E = IDecl->prop_end(); P != E; ++P) {
1213 ObjCPropertyDecl *Prop = (*P);
1214 if (Prop->getIdentifier() == II)
1215 return Prop;
1216 }
1217 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001218 for (ObjCInterfaceDecl::all_protocol_iterator
1219 PI = IDecl->all_referenced_protocol_begin(),
1220 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001221 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1222 if (Prop)
1223 return Prop;
1224 }
1225 }
1226 else if (const ObjCProtocolDecl *PDecl =
1227 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1228 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1229 E = PDecl->prop_end(); P != E; ++P) {
1230 ObjCPropertyDecl *Prop = (*P);
1231 if (Prop->getIdentifier() == II)
1232 return Prop;
1233 }
1234 // scan through protocol's protocols.
1235 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1236 E = PDecl->protocol_end(); PI != E; ++PI) {
1237 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1238 if (Prop)
1239 return Prop;
1240 }
1241 }
1242 return 0;
1243}
1244
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001245/// DefaultSynthesizeProperties - This routine default synthesizes all
1246/// properties which must be synthesized in class's @implementation.
1247void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
1248 ObjCInterfaceDecl *IDecl) {
1249
1250 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1251 CollectClassPropertyImplementations(IDecl, PropMap);
1252 if (PropMap.empty())
1253 return;
1254 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1255 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1256
1257 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1258 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1259 ObjCPropertyDecl *Prop = P->second;
1260 // If property to be implemented in the super class, ignore.
1261 if (SuperPropMap[Prop->getIdentifier()])
1262 continue;
1263 // Is there a matching propery synthesize/dynamic?
1264 if (Prop->isInvalidDecl() ||
1265 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1266 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1267 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001268 // Property may have been synthesized by user.
1269 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1270 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001271 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1272 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1273 continue;
1274 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1275 continue;
1276 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001277
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001278
1279 // We use invalid SourceLocations for the synthesized ivars since they
1280 // aren't really synthesized at a particular location; they just exist.
1281 // Saying that they are located at the @implementation isn't really going
1282 // to help users.
1283 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001284 true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001285 Prop->getIdentifier(), Prop->getIdentifier(),
1286 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001287 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001288}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001289
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001290void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1291 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1292 return;
1293 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1294 if (!IC)
1295 return;
1296 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1297 DefaultSynthesizeProperties(S, IC, IDecl);
1298}
1299
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001300void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001301 ObjCContainerDecl *CDecl,
1302 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001303 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1304 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1305 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1306
Ted Kremenek9d64c152010-03-12 00:38:38 +00001307 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001308 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001309 if (PropMap.empty())
1310 return;
1311
1312 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1313 for (ObjCImplDecl::propimpl_iterator
1314 I = IMPDecl->propimpl_begin(),
1315 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1316 PropImplMap.insert((*I)->getPropertyDecl());
1317
1318 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1319 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1320 ObjCPropertyDecl *Prop = P->second;
1321 // Is there a matching propery synthesize/dynamic?
1322 if (Prop->isInvalidDecl() ||
1323 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001324 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001325 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001326 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001327 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001328 isa<ObjCCategoryDecl>(CDecl) ?
1329 diag::warn_setter_getter_impl_required_in_category :
1330 diag::warn_setter_getter_impl_required)
1331 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001332 Diag(Prop->getLocation(),
1333 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001334 }
1335
1336 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001337 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001338 isa<ObjCCategoryDecl>(CDecl) ?
1339 diag::warn_setter_getter_impl_required_in_category :
1340 diag::warn_setter_getter_impl_required)
1341 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001342 Diag(Prop->getLocation(),
1343 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001344 }
1345 }
1346}
1347
1348void
1349Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1350 ObjCContainerDecl* IDecl) {
1351 // Rules apply in non-GC mode only
Douglas Gregore289d812011-09-13 17:21:33 +00001352 if (getLangOptions().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001353 return;
1354 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1355 E = IDecl->prop_end();
1356 I != E; ++I) {
1357 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001358 ObjCMethodDecl *GetterMethod = 0;
1359 ObjCMethodDecl *SetterMethod = 0;
1360 bool LookedUpGetterSetter = false;
1361
Ted Kremenek9d64c152010-03-12 00:38:38 +00001362 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001363 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001364
John McCall265941b2011-09-13 18:31:23 +00001365 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1366 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001367 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1368 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1369 LookedUpGetterSetter = true;
1370 if (GetterMethod) {
1371 Diag(GetterMethod->getLocation(),
1372 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001373 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001374 Diag(Property->getLocation(), diag::note_property_declare);
1375 }
1376 if (SetterMethod) {
1377 Diag(SetterMethod->getLocation(),
1378 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001379 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001380 Diag(Property->getLocation(), diag::note_property_declare);
1381 }
1382 }
1383
Ted Kremenek9d64c152010-03-12 00:38:38 +00001384 // We only care about readwrite atomic property.
1385 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1386 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1387 continue;
1388 if (const ObjCPropertyImplDecl *PIDecl
1389 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1390 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1391 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001392 if (!LookedUpGetterSetter) {
1393 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1394 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1395 LookedUpGetterSetter = true;
1396 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001397 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1398 SourceLocation MethodLoc =
1399 (GetterMethod ? GetterMethod->getLocation()
1400 : SetterMethod->getLocation());
1401 Diag(MethodLoc, diag::warn_atomic_property_rule)
1402 << Property->getIdentifier();
1403 Diag(Property->getLocation(), diag::note_property_declare);
1404 }
1405 }
1406 }
1407}
1408
John McCallf85e1932011-06-15 23:02:42 +00001409void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
Douglas Gregore289d812011-09-13 17:21:33 +00001410 if (getLangOptions().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001411 return;
1412
1413 for (ObjCImplementationDecl::propimpl_iterator
1414 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1415 ObjCPropertyImplDecl *PID = *i;
1416 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1417 continue;
1418
1419 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001420 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1421 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001422 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1423 if (!method)
1424 continue;
1425 ObjCMethodFamily family = method->getMethodFamily();
1426 if (family == OMF_alloc || family == OMF_copy ||
1427 family == OMF_mutableCopy || family == OMF_new) {
1428 if (getLangOptions().ObjCAutoRefCount)
1429 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1430 else
1431 Diag(PID->getLocation(), diag::warn_ownin_getter_rule);
1432 Diag(PD->getLocation(), diag::note_property_declare);
1433 }
1434 }
1435 }
1436}
1437
John McCall5de74d12010-11-10 07:01:40 +00001438/// AddPropertyAttrs - Propagates attributes from a property to the
1439/// implicitly-declared getter or setter for that property.
1440static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1441 ObjCPropertyDecl *Property) {
1442 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001443 for (Decl::attr_iterator A = Property->attr_begin(),
1444 AEnd = Property->attr_end();
1445 A != AEnd; ++A) {
1446 if (isa<DeprecatedAttr>(*A) ||
1447 isa<UnavailableAttr>(*A) ||
1448 isa<AvailabilityAttr>(*A))
1449 PropertyMethod->addAttr((*A)->clone(S.Context));
1450 }
John McCall5de74d12010-11-10 07:01:40 +00001451}
1452
Ted Kremenek9d64c152010-03-12 00:38:38 +00001453/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1454/// have the property type and issue diagnostics if they don't.
1455/// Also synthesize a getter/setter method if none exist (and update the
1456/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1457/// methods is the "right" thing to do.
1458void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001459 ObjCContainerDecl *CD,
1460 ObjCPropertyDecl *redeclaredProperty,
1461 ObjCContainerDecl *lexicalDC) {
1462
Ted Kremenek9d64c152010-03-12 00:38:38 +00001463 ObjCMethodDecl *GetterMethod, *SetterMethod;
1464
1465 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1466 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1467 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1468 property->getLocation());
1469
1470 if (SetterMethod) {
1471 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1472 property->getPropertyAttributes();
1473 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1474 Context.getCanonicalType(SetterMethod->getResultType()) !=
1475 Context.VoidTy)
1476 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1477 if (SetterMethod->param_size() != 1 ||
1478 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1479 Diag(property->getLocation(),
1480 diag::warn_accessor_property_type_mismatch)
1481 << property->getDeclName()
1482 << SetterMethod->getSelector();
1483 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1484 }
1485 }
1486
1487 // Synthesize getter/setter methods if none exist.
1488 // Find the default getter and if one not found, add one.
1489 // FIXME: The synthesized property we set here is misleading. We almost always
1490 // synthesize these methods unless the user explicitly provided prototypes
1491 // (which is odd, but allowed). Sema should be typechecking that the
1492 // declarations jive in that situation (which it is not currently).
1493 if (!GetterMethod) {
1494 // No instance method of same name as property getter name was found.
1495 // Declare a getter method and add it to the list of methods
1496 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001497 SourceLocation Loc = redeclaredProperty ?
1498 redeclaredProperty->getLocation() :
1499 property->getLocation();
1500
1501 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1502 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001503 property->getType(), 0, CD, /*isInstance=*/true,
1504 /*isVariadic=*/false, /*isSynthesized=*/true,
1505 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001506 (property->getPropertyImplementation() ==
1507 ObjCPropertyDecl::Optional) ?
1508 ObjCMethodDecl::Optional :
1509 ObjCMethodDecl::Required);
1510 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001511
1512 AddPropertyAttrs(*this, GetterMethod, property);
1513
Ted Kremenek23173d72010-05-18 21:09:07 +00001514 // FIXME: Eventually this shouldn't be needed, as the lexical context
1515 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001516 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001517 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001518 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1519 GetterMethod->addAttr(
1520 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001521 } else
1522 // A user declared getter will be synthesize when @synthesize of
1523 // the property with the same name is seen in the @implementation
1524 GetterMethod->setSynthesized(true);
1525 property->setGetterMethodDecl(GetterMethod);
1526
1527 // Skip setter if property is read-only.
1528 if (!property->isReadOnly()) {
1529 // Find the default setter and if one not found, add one.
1530 if (!SetterMethod) {
1531 // No instance method of same name as property setter name was found.
1532 // Declare a setter method and add it to the list of methods
1533 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001534 SourceLocation Loc = redeclaredProperty ?
1535 redeclaredProperty->getLocation() :
1536 property->getLocation();
1537
1538 SetterMethod =
1539 ObjCMethodDecl::Create(Context, Loc, Loc,
1540 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001541 CD, /*isInstance=*/true, /*isVariadic=*/false,
1542 /*isSynthesized=*/true,
1543 /*isImplicitlyDeclared=*/true,
1544 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001545 (property->getPropertyImplementation() ==
1546 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001547 ObjCMethodDecl::Optional :
1548 ObjCMethodDecl::Required);
1549
Ted Kremenek9d64c152010-03-12 00:38:38 +00001550 // Invent the arguments for the setter. We don't bother making a
1551 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001552 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1553 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001554 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001555 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001556 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001557 SC_None,
1558 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001559 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001560 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001561
1562 AddPropertyAttrs(*this, SetterMethod, property);
1563
Ted Kremenek9d64c152010-03-12 00:38:38 +00001564 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001565 // FIXME: Eventually this shouldn't be needed, as the lexical context
1566 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001567 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001568 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001569 } else
1570 // A user declared setter will be synthesize when @synthesize of
1571 // the property with the same name is seen in the @implementation
1572 SetterMethod->setSynthesized(true);
1573 property->setSetterMethodDecl(SetterMethod);
1574 }
1575 // Add any synthesized methods to the global pool. This allows us to
1576 // handle the following, which is supported by GCC (and part of the design).
1577 //
1578 // @interface Foo
1579 // @property double bar;
1580 // @end
1581 //
1582 // void thisIsUnfortunate() {
1583 // id foo;
1584 // double bar = [foo bar];
1585 // }
1586 //
1587 if (GetterMethod)
1588 AddInstanceMethodToGlobalPool(GetterMethod);
1589 if (SetterMethod)
1590 AddInstanceMethodToGlobalPool(SetterMethod);
1591}
1592
John McCalld226f652010-08-21 09:40:31 +00001593void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001594 SourceLocation Loc,
1595 unsigned &Attributes) {
1596 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001597 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001598 return;
1599
1600 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001601 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001602
1603 // readonly and readwrite/assign/retain/copy conflict.
1604 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1605 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1606 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001607 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001608 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001609 ObjCDeclSpec::DQ_PR_retain |
1610 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001611 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1612 "readwrite" :
1613 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1614 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001615 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1616 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001617 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1618 "copy" : "retain";
1619
1620 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1621 diag::err_objc_property_attr_mutually_exclusive :
1622 diag::warn_objc_property_attr_mutually_exclusive)
1623 << "readonly" << which;
1624 }
1625
1626 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001627 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1628 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1629 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001630 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001631 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001632 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1633 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1634 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1635 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001636 }
1637
1638 // Check for more than one of { assign, copy, retain }.
1639 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1640 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1641 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1642 << "assign" << "copy";
1643 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1644 }
1645 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1646 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1647 << "assign" << "retain";
1648 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1649 }
John McCallf85e1932011-06-15 23:02:42 +00001650 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1651 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1652 << "assign" << "strong";
1653 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1654 }
1655 if (getLangOptions().ObjCAutoRefCount &&
1656 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1657 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1658 << "assign" << "weak";
1659 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1660 }
1661 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1662 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1663 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1664 << "unsafe_unretained" << "copy";
1665 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1666 }
1667 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1668 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1669 << "unsafe_unretained" << "retain";
1670 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1671 }
1672 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1673 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1674 << "unsafe_unretained" << "strong";
1675 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1676 }
1677 if (getLangOptions().ObjCAutoRefCount &&
1678 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1679 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1680 << "unsafe_unretained" << "weak";
1681 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1682 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001683 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1684 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1685 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1686 << "copy" << "retain";
1687 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1688 }
John McCallf85e1932011-06-15 23:02:42 +00001689 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1690 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1691 << "copy" << "strong";
1692 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1693 }
1694 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1695 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1696 << "copy" << "weak";
1697 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1698 }
1699 }
1700 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1701 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1702 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1703 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001704 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001705 }
1706 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1707 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1708 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1709 << "strong" << "weak";
1710 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001711 }
1712
1713 // Warn if user supplied no assignment attribute, property is
1714 // readwrite, and this is an object type.
1715 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001716 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1717 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1718 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001719 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1720 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001721 if (getLangOptions().ObjCAutoRefCount)
1722 // With arc, @property definitions should default to (strong) when
1723 // not specified
1724 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
1725 else {
1726 // Skip this warning in gc-only mode.
Douglas Gregore289d812011-09-13 17:21:33 +00001727 if (getLangOptions().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001728 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001729
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001730 // If non-gc code warn that this is likely inappropriate.
Douglas Gregore289d812011-09-13 17:21:33 +00001731 if (getLangOptions().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001732 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1733 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001734
1735 // FIXME: Implement warning dependent on NSCopying being
1736 // implemented. See also:
1737 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1738 // (please trim this list while you are at it).
1739 }
1740
1741 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001742 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Douglas Gregore289d812011-09-13 17:21:33 +00001743 && getLangOptions().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001744 && PropertyTy->isBlockPointerType())
1745 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001746 else if (getLangOptions().ObjCAutoRefCount &&
1747 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1748 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1749 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1750 PropertyTy->isBlockPointerType())
1751 Diag(Loc, diag::warn_objc_property_retain_of_block);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001752}