blob: db23c84f2e20c8e9683e798e6fcb4cb7732bdfb3 [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 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000238 if (PIDecl->getType().getCanonicalType()
239 != PDecl->getType().getCanonicalType()) {
240 Diag(AtLoc,
241 diag::error_type_mismatch_continuation_class) << PDecl->getType();
242 Diag(PIDecl->getLocation(), diag::note_property_declare);
243 }
244
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000245 // The property 'PIDecl's readonly attribute will be over-ridden
246 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000247 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000248 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
249 unsigned retainCopyNonatomic =
250 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000251 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000252 ObjCPropertyDecl::OBJC_PR_copy |
253 ObjCPropertyDecl::OBJC_PR_nonatomic);
254 if ((Attributes & retainCopyNonatomic) !=
255 (PIkind & retainCopyNonatomic)) {
256 Diag(AtLoc, diag::warn_property_attr_mismatch);
257 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000258 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000259 DeclContext *DC = cast<DeclContext>(CCPrimary);
260 if (!ObjCPropertyDecl::findPropertyDecl(DC,
261 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000262 // Protocol is not in the primary class. Must build one for it.
263 ObjCDeclSpec ProtocolPropertyODS;
264 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
265 // and ObjCPropertyDecl::PropertyAttributeKind have identical
266 // values. Should consolidate both into one enum type.
267 ProtocolPropertyODS.
268 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
269 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000270 // Must re-establish the context from class extension to primary
271 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000272 ContextRAII SavedContext(*this, CCPrimary);
273
John McCalld226f652010-08-21 09:40:31 +0000274 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000275 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
276 PIDecl->getGetterName(),
277 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000278 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000279 MethodImplKind,
280 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000281 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000282 }
283 PIDecl->makeitReadWriteAttribute();
284 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
285 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000286 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
287 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000288 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
289 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
290 PIDecl->setSetterName(SetterSel);
291 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000292 // Tailor the diagnostics for the common case where a readwrite
293 // property is declared both in the @interface and the continuation.
294 // This is a common error where the user often intended the original
295 // declaration to be readonly.
296 unsigned diag =
297 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
298 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
299 ? diag::err_use_continuation_class_redeclaration_readwrite
300 : diag::err_use_continuation_class;
301 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000302 << CCPrimary->getDeclName();
303 Diag(PIDecl->getLocation(), diag::note_property_declare);
304 }
305 *isOverridingProperty = true;
306 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000307 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000308 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000309}
310
311ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
312 ObjCContainerDecl *CDecl,
313 SourceLocation AtLoc,
314 FieldDeclarator &FD,
315 Selector GetterSel,
316 Selector SetterSel,
317 const bool isAssign,
318 const bool isReadWrite,
319 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000320 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000321 tok::ObjCKeywordKind MethodImplKind,
322 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000323 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000324 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000325
326 // Issue a warning if property is 'assign' as default and its object, which is
327 // gc'able conforms to NSCopying protocol
Douglas Gregore289d812011-09-13 17:21:33 +0000328 if (getLangOptions().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000329 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000330 if (const ObjCObjectPointerType *ObjPtrTy =
331 T->getAs<ObjCObjectPointerType>()) {
332 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
333 if (IDecl)
334 if (ObjCProtocolDecl* PNSCopying =
335 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
336 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
337 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000338 }
John McCallc12c5bb2010-05-15 11:32:37 +0000339 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000340 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
341
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000342 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000343 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
344 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000345 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000346
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000347 if (ObjCPropertyDecl *prevDecl =
348 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000349 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000350 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000351 PDecl->setInvalidDecl();
352 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000353 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000354 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000355 if (lexicalDC)
356 PDecl->setLexicalDeclContext(lexicalDC);
357 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000358
359 if (T->isArrayType() || T->isFunctionType()) {
360 Diag(AtLoc, diag::err_property_type) << T;
361 PDecl->setInvalidDecl();
362 }
363
364 ProcessDeclAttributes(S, PDecl, FD.D);
365
366 // Regardless of setter/getter attribute, we save the default getter/setter
367 // selector names in anticipation of declaration of setter/getter methods.
368 PDecl->setGetterName(GetterSel);
369 PDecl->setSetterName(SetterSel);
370
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000371 unsigned attributesAsWritten = 0;
372 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
373 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
374 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
375 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
376 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
377 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
378 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
379 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
380 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
381 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
382 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
383 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
384 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
385 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
386 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
387 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
388 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
389 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
390 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
391 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
392 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
393 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
394 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
395 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
396
397 PDecl->setPropertyAttributesAsWritten(
398 (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten);
399
Ted Kremenek28685ab2010-03-12 00:46:40 +0000400 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
401 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
402
403 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
404 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
405
406 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
407 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
408
409 if (isReadWrite)
410 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
411
412 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
413 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
414
John McCallf85e1932011-06-15 23:02:42 +0000415 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
416 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
417
418 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
419 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
420
Ted Kremenek28685ab2010-03-12 00:46:40 +0000421 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
422 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
423
John McCallf85e1932011-06-15 23:02:42 +0000424 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
425 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
426
Ted Kremenek28685ab2010-03-12 00:46:40 +0000427 if (isAssign)
428 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
429
John McCall265941b2011-09-13 18:31:23 +0000430 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000431 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
432 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000433 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000434 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000435
John McCallf85e1932011-06-15 23:02:42 +0000436 // 'unsafe_unretained' is alias for 'assign'.
437 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
438 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
439 if (isAssign)
440 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
441
Ted Kremenek28685ab2010-03-12 00:46:40 +0000442 if (MethodImplKind == tok::objc_required)
443 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
444 else if (MethodImplKind == tok::objc_optional)
445 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000446
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000447 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000448}
449
John McCallf85e1932011-06-15 23:02:42 +0000450static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
451 ObjCPropertyDecl *property,
452 ObjCIvarDecl *ivar) {
453 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
454
John McCallf85e1932011-06-15 23:02:42 +0000455 QualType ivarType = ivar->getType();
456 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000457
John McCall265941b2011-09-13 18:31:23 +0000458 // The lifetime implied by the property's attributes.
459 Qualifiers::ObjCLifetime propertyLifetime =
460 getImpliedARCOwnership(property->getPropertyAttributes(),
461 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000462
John McCall265941b2011-09-13 18:31:23 +0000463 // We're fine if they match.
464 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000465
John McCall265941b2011-09-13 18:31:23 +0000466 // These aren't valid lifetimes for object ivars; don't diagnose twice.
467 if (ivarLifetime == Qualifiers::OCL_None ||
468 ivarLifetime == Qualifiers::OCL_Autoreleasing)
469 return;
John McCallf85e1932011-06-15 23:02:42 +0000470
John McCall265941b2011-09-13 18:31:23 +0000471 switch (propertyLifetime) {
472 case Qualifiers::OCL_Strong:
473 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
474 << property->getDeclName()
475 << ivar->getDeclName()
476 << ivarLifetime;
477 break;
John McCallf85e1932011-06-15 23:02:42 +0000478
John McCall265941b2011-09-13 18:31:23 +0000479 case Qualifiers::OCL_Weak:
480 S.Diag(propertyImplLoc, diag::error_weak_property)
481 << property->getDeclName()
482 << ivar->getDeclName();
483 break;
John McCallf85e1932011-06-15 23:02:42 +0000484
John McCall265941b2011-09-13 18:31:23 +0000485 case Qualifiers::OCL_ExplicitNone:
486 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
487 << property->getDeclName()
488 << ivar->getDeclName()
489 << ((property->getPropertyAttributesAsWritten()
490 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
491 break;
John McCallf85e1932011-06-15 23:02:42 +0000492
John McCall265941b2011-09-13 18:31:23 +0000493 case Qualifiers::OCL_Autoreleasing:
494 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000495
John McCall265941b2011-09-13 18:31:23 +0000496 case Qualifiers::OCL_None:
497 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000498 return;
499 }
500
501 S.Diag(property->getLocation(), diag::note_property_declare);
502}
503
Ted Kremenek28685ab2010-03-12 00:46:40 +0000504
505/// ActOnPropertyImplDecl - This routine performs semantic checks and
506/// builds the AST node for a property implementation declaration; declared
507/// as @synthesize or @dynamic.
508///
John McCalld226f652010-08-21 09:40:31 +0000509Decl *Sema::ActOnPropertyImplDecl(Scope *S,
510 SourceLocation AtLoc,
511 SourceLocation PropertyLoc,
512 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000513 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000514 IdentifierInfo *PropertyIvar,
515 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000516 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000517 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000518 // Make sure we have a context for the property implementation declaration.
519 if (!ClassImpDecl) {
520 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000521 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000522 }
523 ObjCPropertyDecl *property = 0;
524 ObjCInterfaceDecl* IDecl = 0;
525 // Find the class or category class where this property must have
526 // a declaration.
527 ObjCImplementationDecl *IC = 0;
528 ObjCCategoryImplDecl* CatImplClass = 0;
529 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
530 IDecl = IC->getClassInterface();
531 // We always synthesize an interface for an implementation
532 // without an interface decl. So, IDecl is always non-zero.
533 assert(IDecl &&
534 "ActOnPropertyImplDecl - @implementation without @interface");
535
536 // Look for this property declaration in the @implementation's @interface
537 property = IDecl->FindPropertyDeclaration(PropertyId);
538 if (!property) {
539 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000540 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000541 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000542 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000543 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
544 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000545 if (AtLoc.isValid())
546 Diag(AtLoc, diag::warn_implicit_atomic_property);
547 else
548 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
549 Diag(property->getLocation(), diag::note_property_declare);
550 }
551
Ted Kremenek28685ab2010-03-12 00:46:40 +0000552 if (const ObjCCategoryDecl *CD =
553 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
554 if (!CD->IsClassExtension()) {
555 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
556 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000557 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000558 }
559 }
560 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
561 if (Synthesize) {
562 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000563 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000564 }
565 IDecl = CatImplClass->getClassInterface();
566 if (!IDecl) {
567 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000568 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569 }
570 ObjCCategoryDecl *Category =
571 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
572
573 // If category for this implementation not found, it is an error which
574 // has already been reported eralier.
575 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000576 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000577 // Look for this property declaration in @implementation's category
578 property = Category->FindPropertyDeclaration(PropertyId);
579 if (!property) {
580 Diag(PropertyLoc, diag::error_bad_category_property_decl)
581 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000582 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000583 }
584 } else {
585 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000586 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000587 }
588 ObjCIvarDecl *Ivar = 0;
589 // Check that we have a valid, previously declared ivar for @synthesize
590 if (Synthesize) {
591 // @synthesize
592 if (!PropertyIvar)
593 PropertyIvar = PropertyId;
John McCallf85e1932011-06-15 23:02:42 +0000594 ObjCPropertyDecl::PropertyAttributeKind kind
595 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000596 QualType PropType = property->getType();
597
598 QualType PropertyIvarType = PropType.getNonReferenceType();
599
600 // Add GC __weak to the ivar type if the property is weak.
601 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
Douglas Gregore289d812011-09-13 17:21:33 +0000602 getLangOptions().getGC() != LangOptions::NonGC) {
John McCall265941b2011-09-13 18:31:23 +0000603 assert(!getLangOptions().ObjCAutoRefCount);
604 if (PropertyIvarType.isObjCGCStrong()) {
605 Diag(PropertyLoc, diag::err_gc_weak_property_strong_type);
606 Diag(property->getLocation(), diag::note_property_declare);
607 } else {
608 PropertyIvarType =
609 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000610 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000611 }
John McCall265941b2011-09-13 18:31:23 +0000612
Ted Kremenek28685ab2010-03-12 00:46:40 +0000613 // Check that this is a previously declared 'ivar' in 'IDecl' interface
614 ObjCInterfaceDecl *ClassDeclared;
615 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
616 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000617 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000618 // property attributes.
619 if (getLangOptions().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000620 !PropertyIvarType.getObjCLifetime() &&
621 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000622
John McCall265941b2011-09-13 18:31:23 +0000623 // It's an error if we have to do this and the user didn't
624 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000625 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000626 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000627 Diag(PropertyLoc,
628 diag::err_arc_objc_property_default_assign_on_object);
629 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000630 } else {
631 Qualifiers::ObjCLifetime lifetime =
632 getImpliedARCOwnership(kind, PropertyIvarType);
633 assert(lifetime && "no lifetime for property?");
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000634
John McCall265941b2011-09-13 18:31:23 +0000635 if (lifetime == Qualifiers::OCL_Weak &&
636 !getLangOptions().ObjCRuntimeHasWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000637 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
638 Diag(property->getLocation(), diag::note_property_declare);
639 }
John McCall265941b2011-09-13 18:31:23 +0000640
John McCallf85e1932011-06-15 23:02:42 +0000641 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000642 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000643 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
644 }
John McCallf85e1932011-06-15 23:02:42 +0000645 }
646
647 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
648 !getLangOptions().ObjCAutoRefCount &&
Douglas Gregore289d812011-09-13 17:21:33 +0000649 getLangOptions().getGC() == LangOptions::NonGC) {
John McCallf85e1932011-06-15 23:02:42 +0000650 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
651 Diag(property->getLocation(), diag::note_property_declare);
652 }
653
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000654 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
655 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000656 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000657 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000658 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000659 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000660 IDecl->makeDeclVisibleInContext(Ivar, false);
661 property->setPropertyIvarDecl(Ivar);
662
663 if (!getLangOptions().ObjCNonFragileABI)
664 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
665 // Note! I deliberately want it to fall thru so, we have a
666 // a property implementation and to avoid future warnings.
667 } else if (getLangOptions().ObjCNonFragileABI &&
668 ClassDeclared != IDecl) {
669 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
670 << property->getDeclName() << Ivar->getDeclName()
671 << ClassDeclared->getDeclName();
672 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000673 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000674 // Note! I deliberately want it to fall thru so more errors are caught.
675 }
676 QualType IvarType = Context.getCanonicalType(Ivar->getType());
677
678 // Check that type of property and its ivar are type compatible.
John McCall265941b2011-09-13 18:31:23 +0000679 if (Context.getCanonicalType(PropertyIvarType) != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000680 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000681 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000682 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000683 compat =
684 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000685 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000686 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000687 else {
688 SourceLocation Loc = PropertyIvarLoc;
689 if (Loc.isInvalid())
690 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000691 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000692 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000693 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000694 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000695 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000696 << property->getDeclName() << PropType
697 << Ivar->getDeclName() << IvarType;
698 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000699 // Note! I deliberately want it to fall thru so, we have a
700 // a property implementation and to avoid future warnings.
701 }
702
703 // FIXME! Rules for properties are somewhat different that those
704 // for assignments. Use a new routine to consolidate all cases;
705 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000706 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000707 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
708 if (lhsType != rhsType &&
709 lhsType->isArithmeticType()) {
710 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000711 << property->getDeclName() << PropType
712 << Ivar->getDeclName() << IvarType;
713 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000714 // Fall thru - see previous comment
715 }
716 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000717 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000718 getLangOptions().getGC() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000719 Diag(PropertyLoc, diag::error_weak_property)
720 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000721 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000722 // Fall thru - see previous comment
723 }
John McCallf85e1932011-06-15 23:02:42 +0000724 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000725 if ((property->getType()->isObjCObjectPointerType() ||
726 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000727 getLangOptions().getGC() != LangOptions::NonGC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000728 Diag(PropertyLoc, diag::error_strong_property)
729 << property->getDeclName() << Ivar->getDeclName();
730 // Fall thru - see previous comment
731 }
732 }
John McCallf85e1932011-06-15 23:02:42 +0000733 if (getLangOptions().ObjCAutoRefCount)
734 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000735 } else if (PropertyIvar)
736 // @dynamic
737 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000738
Ted Kremenek28685ab2010-03-12 00:46:40 +0000739 assert (property && "ActOnPropertyImplDecl - property declaration missing");
740 ObjCPropertyImplDecl *PIDecl =
741 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
742 property,
743 (Synthesize ?
744 ObjCPropertyImplDecl::Synthesize
745 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000746 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000747 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
748 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000749 if (getLangOptions().CPlusPlus && Synthesize &&
750 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000751 // For Objective-C++, need to synthesize the AST for the IVAR object to be
752 // returned by the getter as it must conform to C++'s copy-return rules.
753 // FIXME. Eventually we want to do this for Objective-C as well.
754 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
755 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000756 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
757 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000758 Expr *IvarRefExpr =
759 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
760 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000761 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000762 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000763 SourceLocation(),
764 getterMethod->getResultType(),
765 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000766 SourceLocation(),
767 Owned(IvarRefExpr));
768 if (!Res.isInvalid()) {
769 Expr *ResExpr = Res.takeAs<Expr>();
770 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000771 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000772 PIDecl->setGetterCXXConstructor(ResExpr);
773 }
774 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000775 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
776 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
777 Diag(getterMethod->getLocation(),
778 diag::warn_property_getter_owning_mismatch);
779 Diag(property->getLocation(), diag::note_property_declare);
780 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000781 }
782 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
783 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000784 if (getLangOptions().CPlusPlus && Synthesize
785 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000786 // FIXME. Eventually we want to do this for Objective-C as well.
787 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
788 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000789 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
790 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000791 Expr *lhs =
792 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
793 SelfExpr, true, true);
794 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
795 ParmVarDecl *Param = (*P);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000796 QualType T = Param->getType();
797 if (T->isReferenceType())
Fariborz Jahanian61750f22011-03-30 16:59:30 +0000798 T = T->getAs<ReferenceType>()->getPointeeType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000799 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000800 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000801 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000802 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000803 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
804 }
805 }
806
Ted Kremenek28685ab2010-03-12 00:46:40 +0000807 if (IC) {
808 if (Synthesize)
809 if (ObjCPropertyImplDecl *PPIDecl =
810 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
811 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
812 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
813 << PropertyIvar;
814 Diag(PPIDecl->getLocation(), diag::note_previous_use);
815 }
816
817 if (ObjCPropertyImplDecl *PPIDecl
818 = IC->FindPropertyImplDecl(PropertyId)) {
819 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
820 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000821 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000822 }
823 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000824 if (getLangOptions().ObjCDefaultSynthProperties &&
825 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000826 // Diagnose if an ivar was lazily synthesdized due to a previous
827 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000828 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000829 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000830 ObjCIvarDecl *Ivar = 0;
831 if (!Synthesize)
832 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
833 else {
834 if (PropertyIvar && PropertyIvar != PropertyId)
835 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
836 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000837 // Issue diagnostics only if Ivar belongs to current class.
838 if (Ivar && Ivar->getSynthesize() &&
839 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000840 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
841 << PropertyId;
842 Ivar->setInvalidDecl();
843 }
844 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000845 } else {
846 if (Synthesize)
847 if (ObjCPropertyImplDecl *PPIDecl =
848 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
849 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
850 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
851 << PropertyIvar;
852 Diag(PPIDecl->getLocation(), diag::note_previous_use);
853 }
854
855 if (ObjCPropertyImplDecl *PPIDecl =
856 CatImplClass->FindPropertyImplDecl(PropertyId)) {
857 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
858 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000859 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000860 }
861 CatImplClass->addPropertyImplementation(PIDecl);
862 }
863
John McCalld226f652010-08-21 09:40:31 +0000864 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000865}
866
867//===----------------------------------------------------------------------===//
868// Helper methods.
869//===----------------------------------------------------------------------===//
870
Ted Kremenek9d64c152010-03-12 00:38:38 +0000871/// DiagnosePropertyMismatch - Compares two properties for their
872/// attributes and types and warns on a variety of inconsistencies.
873///
874void
875Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
876 ObjCPropertyDecl *SuperProperty,
877 const IdentifierInfo *inheritedName) {
878 ObjCPropertyDecl::PropertyAttributeKind CAttr =
879 Property->getPropertyAttributes();
880 ObjCPropertyDecl::PropertyAttributeKind SAttr =
881 SuperProperty->getPropertyAttributes();
882 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
883 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
884 Diag(Property->getLocation(), diag::warn_readonly_property)
885 << Property->getDeclName() << inheritedName;
886 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
887 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
888 Diag(Property->getLocation(), diag::warn_property_attribute)
889 << Property->getDeclName() << "copy" << inheritedName;
John McCallf85e1932011-06-15 23:02:42 +0000890 else {
891 unsigned CAttrRetain =
892 (CAttr &
893 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
894 unsigned SAttrRetain =
895 (SAttr &
896 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
897 bool CStrong = (CAttrRetain != 0);
898 bool SStrong = (SAttrRetain != 0);
899 if (CStrong != SStrong)
900 Diag(Property->getLocation(), diag::warn_property_attribute)
901 << Property->getDeclName() << "retain (or strong)" << inheritedName;
902 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000903
904 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
905 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
906 Diag(Property->getLocation(), diag::warn_property_attribute)
907 << Property->getDeclName() << "atomic" << inheritedName;
908 if (Property->getSetterName() != SuperProperty->getSetterName())
909 Diag(Property->getLocation(), diag::warn_property_attribute)
910 << Property->getDeclName() << "setter" << inheritedName;
911 if (Property->getGetterName() != SuperProperty->getGetterName())
912 Diag(Property->getLocation(), diag::warn_property_attribute)
913 << Property->getDeclName() << "getter" << inheritedName;
914
915 QualType LHSType =
916 Context.getCanonicalType(SuperProperty->getType());
917 QualType RHSType =
918 Context.getCanonicalType(Property->getType());
919
Fariborz Jahanianc286f382011-07-12 22:05:16 +0000920 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000921 // Do cases not handled in above.
922 // FIXME. For future support of covariant property types, revisit this.
923 bool IncompatibleObjC = false;
924 QualType ConvertedType;
925 if (!isObjCPointerConversion(RHSType, LHSType,
926 ConvertedType, IncompatibleObjC) ||
927 IncompatibleObjC)
928 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
929 << Property->getType() << SuperProperty->getType() << inheritedName;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000930 }
931}
932
933bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
934 ObjCMethodDecl *GetterMethod,
935 SourceLocation Loc) {
936 if (GetterMethod &&
937 GetterMethod->getResultType() != property->getType()) {
938 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000939 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000940 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000941 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000942 if (result != Compatible) {
943 Diag(Loc, diag::warn_accessor_property_type_mismatch)
944 << property->getDeclName()
945 << GetterMethod->getSelector();
946 Diag(GetterMethod->getLocation(), diag::note_declared_at);
947 return true;
948 }
949 }
950 return false;
951}
952
953/// ComparePropertiesInBaseAndSuper - This routine compares property
954/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000955/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000956///
957void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
958 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
959 if (!SDecl)
960 return;
961 // FIXME: O(N^2)
962 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
963 E = SDecl->prop_end(); S != E; ++S) {
964 ObjCPropertyDecl *SuperPDecl = (*S);
965 // Does property in super class has declaration in current class?
966 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
967 E = IDecl->prop_end(); I != E; ++I) {
968 ObjCPropertyDecl *PDecl = (*I);
969 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
970 DiagnosePropertyMismatch(PDecl, SuperPDecl,
971 SDecl->getIdentifier());
972 }
973 }
974}
975
976/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
977/// of properties declared in a protocol and compares their attribute against
978/// the same property declared in the class or category.
979void
980Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
981 ObjCProtocolDecl *PDecl) {
982 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
983 if (!IDecl) {
984 // Category
985 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
986 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
987 if (!CatDecl->IsClassExtension())
988 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
989 E = PDecl->prop_end(); P != E; ++P) {
990 ObjCPropertyDecl *Pr = (*P);
991 ObjCCategoryDecl::prop_iterator CP, CE;
992 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000993 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000994 if ((*CP)->getIdentifier() == Pr->getIdentifier())
995 break;
996 if (CP != CE)
997 // Property protocol already exist in class. Diagnose any mismatch.
998 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
999 }
1000 return;
1001 }
1002 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1003 E = PDecl->prop_end(); P != E; ++P) {
1004 ObjCPropertyDecl *Pr = (*P);
1005 ObjCInterfaceDecl::prop_iterator CP, CE;
1006 // Is this property already in class's list of properties?
1007 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1008 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1009 break;
1010 if (CP != CE)
1011 // Property protocol already exist in class. Diagnose any mismatch.
1012 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1013 }
1014}
1015
1016/// CompareProperties - This routine compares properties
1017/// declared in 'ClassOrProtocol' objects (which can be a class or an
1018/// inherited protocol with the list of properties for class/category 'CDecl'
1019///
John McCalld226f652010-08-21 09:40:31 +00001020void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1021 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001022 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1023
1024 if (!IDecl) {
1025 // Category
1026 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1027 assert (CatDecl && "CompareProperties");
1028 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1029 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1030 E = MDecl->protocol_end(); P != E; ++P)
1031 // Match properties of category with those of protocol (*P)
1032 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1033
1034 // Go thru the list of protocols for this category and recursively match
1035 // their properties with those in the category.
1036 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1037 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001038 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001039 } else {
1040 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1041 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1042 E = MD->protocol_end(); P != E; ++P)
1043 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1044 }
1045 return;
1046 }
1047
1048 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001049 for (ObjCInterfaceDecl::all_protocol_iterator
1050 P = MDecl->all_referenced_protocol_begin(),
1051 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001052 // Match properties of class IDecl with those of protocol (*P).
1053 MatchOneProtocolPropertiesInClass(IDecl, *P);
1054
1055 // Go thru the list of protocols for this class and recursively match
1056 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001057 for (ObjCInterfaceDecl::all_protocol_iterator
1058 P = IDecl->all_referenced_protocol_begin(),
1059 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001060 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001061 } else {
1062 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1063 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1064 E = MD->protocol_end(); P != E; ++P)
1065 MatchOneProtocolPropertiesInClass(IDecl, *P);
1066 }
1067}
1068
1069/// isPropertyReadonly - Return true if property is readonly, by searching
1070/// for the property in the class and in its categories and implementations
1071///
1072bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1073 ObjCInterfaceDecl *IDecl) {
1074 // by far the most common case.
1075 if (!PDecl->isReadOnly())
1076 return false;
1077 // Even if property is ready only, if interface has a user defined setter,
1078 // it is not considered read only.
1079 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1080 return false;
1081
1082 // Main class has the property as 'readonly'. Must search
1083 // through the category list to see if the property's
1084 // attribute has been over-ridden to 'readwrite'.
1085 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1086 Category; Category = Category->getNextClassCategory()) {
1087 // Even if property is ready only, if a category has a user defined setter,
1088 // it is not considered read only.
1089 if (Category->getInstanceMethod(PDecl->getSetterName()))
1090 return false;
1091 ObjCPropertyDecl *P =
1092 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1093 if (P && !P->isReadOnly())
1094 return false;
1095 }
1096
1097 // Also, check for definition of a setter method in the implementation if
1098 // all else failed.
1099 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1100 if (ObjCImplementationDecl *IMD =
1101 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1102 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1103 return false;
1104 } else if (ObjCCategoryImplDecl *CIMD =
1105 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1106 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1107 return false;
1108 }
1109 }
1110 // Lastly, look through the implementation (if one is in scope).
1111 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1112 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1113 return false;
1114 // If all fails, look at the super class.
1115 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1116 return isPropertyReadonly(PDecl, SIDecl);
1117 return true;
1118}
1119
1120/// CollectImmediateProperties - This routine collects all properties in
1121/// the class and its conforming protocols; but not those it its super class.
1122void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001123 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1124 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001125 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1126 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1127 E = IDecl->prop_end(); P != E; ++P) {
1128 ObjCPropertyDecl *Prop = (*P);
1129 PropMap[Prop->getIdentifier()] = Prop;
1130 }
1131 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001132 for (ObjCInterfaceDecl::all_protocol_iterator
1133 PI = IDecl->all_referenced_protocol_begin(),
1134 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001135 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001136 }
1137 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1138 if (!CATDecl->IsClassExtension())
1139 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1140 E = CATDecl->prop_end(); P != E; ++P) {
1141 ObjCPropertyDecl *Prop = (*P);
1142 PropMap[Prop->getIdentifier()] = Prop;
1143 }
1144 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001145 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001146 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001147 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001148 }
1149 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1150 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1151 E = PDecl->prop_end(); P != E; ++P) {
1152 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001153 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1154 // Exclude property for protocols which conform to class's super-class,
1155 // as super-class has to implement the property.
1156 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
1157 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1158 if (!PropEntry)
1159 PropEntry = Prop;
1160 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001161 }
1162 // scan through protocol's protocols.
1163 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1164 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001165 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001166 }
1167}
1168
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001169/// CollectClassPropertyImplementations - This routine collects list of
1170/// properties to be implemented in the class. This includes, class's
1171/// and its conforming protocols' properties.
1172static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1173 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1174 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1175 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1176 E = IDecl->prop_end(); P != E; ++P) {
1177 ObjCPropertyDecl *Prop = (*P);
1178 PropMap[Prop->getIdentifier()] = Prop;
1179 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001180 for (ObjCInterfaceDecl::all_protocol_iterator
1181 PI = IDecl->all_referenced_protocol_begin(),
1182 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001183 CollectClassPropertyImplementations((*PI), PropMap);
1184 }
1185 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1186 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1187 E = PDecl->prop_end(); P != E; ++P) {
1188 ObjCPropertyDecl *Prop = (*P);
1189 PropMap[Prop->getIdentifier()] = Prop;
1190 }
1191 // scan through protocol's protocols.
1192 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1193 E = PDecl->protocol_end(); PI != E; ++PI)
1194 CollectClassPropertyImplementations((*PI), PropMap);
1195 }
1196}
1197
1198/// CollectSuperClassPropertyImplementations - This routine collects list of
1199/// properties to be implemented in super class(s) and also coming from their
1200/// conforming protocols.
1201static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1202 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1203 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1204 while (SDecl) {
1205 CollectClassPropertyImplementations(SDecl, PropMap);
1206 SDecl = SDecl->getSuperClass();
1207 }
1208 }
1209}
1210
Ted Kremenek9d64c152010-03-12 00:38:38 +00001211/// LookupPropertyDecl - Looks up a property in the current class and all
1212/// its protocols.
1213ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1214 IdentifierInfo *II) {
1215 if (const ObjCInterfaceDecl *IDecl =
1216 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1217 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1218 E = IDecl->prop_end(); P != E; ++P) {
1219 ObjCPropertyDecl *Prop = (*P);
1220 if (Prop->getIdentifier() == II)
1221 return Prop;
1222 }
1223 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001224 for (ObjCInterfaceDecl::all_protocol_iterator
1225 PI = IDecl->all_referenced_protocol_begin(),
1226 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001227 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1228 if (Prop)
1229 return Prop;
1230 }
1231 }
1232 else if (const ObjCProtocolDecl *PDecl =
1233 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1234 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1235 E = PDecl->prop_end(); P != E; ++P) {
1236 ObjCPropertyDecl *Prop = (*P);
1237 if (Prop->getIdentifier() == II)
1238 return Prop;
1239 }
1240 // scan through protocol's protocols.
1241 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1242 E = PDecl->protocol_end(); PI != E; ++PI) {
1243 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1244 if (Prop)
1245 return Prop;
1246 }
1247 }
1248 return 0;
1249}
1250
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001251/// DefaultSynthesizeProperties - This routine default synthesizes all
1252/// properties which must be synthesized in class's @implementation.
1253void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
1254 ObjCInterfaceDecl *IDecl) {
1255
1256 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1257 CollectClassPropertyImplementations(IDecl, PropMap);
1258 if (PropMap.empty())
1259 return;
1260 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1261 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1262
1263 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1264 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1265 ObjCPropertyDecl *Prop = P->second;
1266 // If property to be implemented in the super class, ignore.
1267 if (SuperPropMap[Prop->getIdentifier()])
1268 continue;
1269 // Is there a matching propery synthesize/dynamic?
1270 if (Prop->isInvalidDecl() ||
1271 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1272 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1273 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001274 // Property may have been synthesized by user.
1275 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1276 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001277 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1278 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1279 continue;
1280 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1281 continue;
1282 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001283
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001284
1285 // We use invalid SourceLocations for the synthesized ivars since they
1286 // aren't really synthesized at a particular location; they just exist.
1287 // Saying that they are located at the @implementation isn't really going
1288 // to help users.
1289 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001290 true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001291 Prop->getIdentifier(), Prop->getIdentifier(),
1292 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001293 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001294}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001295
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001296void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1297 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1298 return;
1299 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1300 if (!IC)
1301 return;
1302 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1303 DefaultSynthesizeProperties(S, IC, IDecl);
1304}
1305
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001306void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001307 ObjCContainerDecl *CDecl,
1308 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001309 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1310 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1311 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1312
Ted Kremenek9d64c152010-03-12 00:38:38 +00001313 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001314 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001315 if (PropMap.empty())
1316 return;
1317
1318 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1319 for (ObjCImplDecl::propimpl_iterator
1320 I = IMPDecl->propimpl_begin(),
1321 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1322 PropImplMap.insert((*I)->getPropertyDecl());
1323
1324 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1325 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1326 ObjCPropertyDecl *Prop = P->second;
1327 // Is there a matching propery synthesize/dynamic?
1328 if (Prop->isInvalidDecl() ||
1329 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001330 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001331 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001332 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001333 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001334 isa<ObjCCategoryDecl>(CDecl) ?
1335 diag::warn_setter_getter_impl_required_in_category :
1336 diag::warn_setter_getter_impl_required)
1337 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001338 Diag(Prop->getLocation(),
1339 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001340 }
1341
1342 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001343 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001344 isa<ObjCCategoryDecl>(CDecl) ?
1345 diag::warn_setter_getter_impl_required_in_category :
1346 diag::warn_setter_getter_impl_required)
1347 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001348 Diag(Prop->getLocation(),
1349 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001350 }
1351 }
1352}
1353
1354void
1355Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1356 ObjCContainerDecl* IDecl) {
1357 // Rules apply in non-GC mode only
Douglas Gregore289d812011-09-13 17:21:33 +00001358 if (getLangOptions().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001359 return;
1360 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1361 E = IDecl->prop_end();
1362 I != E; ++I) {
1363 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001364 ObjCMethodDecl *GetterMethod = 0;
1365 ObjCMethodDecl *SetterMethod = 0;
1366 bool LookedUpGetterSetter = false;
1367
Ted Kremenek9d64c152010-03-12 00:38:38 +00001368 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001369 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001370
John McCall265941b2011-09-13 18:31:23 +00001371 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1372 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001373 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1374 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1375 LookedUpGetterSetter = true;
1376 if (GetterMethod) {
1377 Diag(GetterMethod->getLocation(),
1378 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001379 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001380 Diag(Property->getLocation(), diag::note_property_declare);
1381 }
1382 if (SetterMethod) {
1383 Diag(SetterMethod->getLocation(),
1384 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001385 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001386 Diag(Property->getLocation(), diag::note_property_declare);
1387 }
1388 }
1389
Ted Kremenek9d64c152010-03-12 00:38:38 +00001390 // We only care about readwrite atomic property.
1391 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1392 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1393 continue;
1394 if (const ObjCPropertyImplDecl *PIDecl
1395 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1396 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1397 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001398 if (!LookedUpGetterSetter) {
1399 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1400 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1401 LookedUpGetterSetter = true;
1402 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001403 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1404 SourceLocation MethodLoc =
1405 (GetterMethod ? GetterMethod->getLocation()
1406 : SetterMethod->getLocation());
1407 Diag(MethodLoc, diag::warn_atomic_property_rule)
1408 << Property->getIdentifier();
1409 Diag(Property->getLocation(), diag::note_property_declare);
1410 }
1411 }
1412 }
1413}
1414
John McCallf85e1932011-06-15 23:02:42 +00001415void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
Douglas Gregore289d812011-09-13 17:21:33 +00001416 if (getLangOptions().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001417 return;
1418
1419 for (ObjCImplementationDecl::propimpl_iterator
1420 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1421 ObjCPropertyImplDecl *PID = *i;
1422 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1423 continue;
1424
1425 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001426 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1427 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001428 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1429 if (!method)
1430 continue;
1431 ObjCMethodFamily family = method->getMethodFamily();
1432 if (family == OMF_alloc || family == OMF_copy ||
1433 family == OMF_mutableCopy || family == OMF_new) {
1434 if (getLangOptions().ObjCAutoRefCount)
1435 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1436 else
1437 Diag(PID->getLocation(), diag::warn_ownin_getter_rule);
1438 Diag(PD->getLocation(), diag::note_property_declare);
1439 }
1440 }
1441 }
1442}
1443
John McCall5de74d12010-11-10 07:01:40 +00001444/// AddPropertyAttrs - Propagates attributes from a property to the
1445/// implicitly-declared getter or setter for that property.
1446static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1447 ObjCPropertyDecl *Property) {
1448 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001449 for (Decl::attr_iterator A = Property->attr_begin(),
1450 AEnd = Property->attr_end();
1451 A != AEnd; ++A) {
1452 if (isa<DeprecatedAttr>(*A) ||
1453 isa<UnavailableAttr>(*A) ||
1454 isa<AvailabilityAttr>(*A))
1455 PropertyMethod->addAttr((*A)->clone(S.Context));
1456 }
John McCall5de74d12010-11-10 07:01:40 +00001457}
1458
Ted Kremenek9d64c152010-03-12 00:38:38 +00001459/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1460/// have the property type and issue diagnostics if they don't.
1461/// Also synthesize a getter/setter method if none exist (and update the
1462/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1463/// methods is the "right" thing to do.
1464void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001465 ObjCContainerDecl *CD,
1466 ObjCPropertyDecl *redeclaredProperty,
1467 ObjCContainerDecl *lexicalDC) {
1468
Ted Kremenek9d64c152010-03-12 00:38:38 +00001469 ObjCMethodDecl *GetterMethod, *SetterMethod;
1470
1471 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1472 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1473 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1474 property->getLocation());
1475
1476 if (SetterMethod) {
1477 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1478 property->getPropertyAttributes();
1479 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1480 Context.getCanonicalType(SetterMethod->getResultType()) !=
1481 Context.VoidTy)
1482 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1483 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001484 !Context.hasSameUnqualifiedType(
1485 (*SetterMethod->param_begin())->getType(), property->getType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001486 Diag(property->getLocation(),
1487 diag::warn_accessor_property_type_mismatch)
1488 << property->getDeclName()
1489 << SetterMethod->getSelector();
1490 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1491 }
1492 }
1493
1494 // Synthesize getter/setter methods if none exist.
1495 // Find the default getter and if one not found, add one.
1496 // FIXME: The synthesized property we set here is misleading. We almost always
1497 // synthesize these methods unless the user explicitly provided prototypes
1498 // (which is odd, but allowed). Sema should be typechecking that the
1499 // declarations jive in that situation (which it is not currently).
1500 if (!GetterMethod) {
1501 // No instance method of same name as property getter name was found.
1502 // Declare a getter method and add it to the list of methods
1503 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001504 SourceLocation Loc = redeclaredProperty ?
1505 redeclaredProperty->getLocation() :
1506 property->getLocation();
1507
1508 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1509 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001510 property->getType(), 0, CD, /*isInstance=*/true,
1511 /*isVariadic=*/false, /*isSynthesized=*/true,
1512 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001513 (property->getPropertyImplementation() ==
1514 ObjCPropertyDecl::Optional) ?
1515 ObjCMethodDecl::Optional :
1516 ObjCMethodDecl::Required);
1517 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001518
1519 AddPropertyAttrs(*this, GetterMethod, property);
1520
Ted Kremenek23173d72010-05-18 21:09:07 +00001521 // FIXME: Eventually this shouldn't be needed, as the lexical context
1522 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001523 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001524 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001525 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1526 GetterMethod->addAttr(
1527 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001528 } else
1529 // A user declared getter will be synthesize when @synthesize of
1530 // the property with the same name is seen in the @implementation
1531 GetterMethod->setSynthesized(true);
1532 property->setGetterMethodDecl(GetterMethod);
1533
1534 // Skip setter if property is read-only.
1535 if (!property->isReadOnly()) {
1536 // Find the default setter and if one not found, add one.
1537 if (!SetterMethod) {
1538 // No instance method of same name as property setter name was found.
1539 // Declare a setter method and add it to the list of methods
1540 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001541 SourceLocation Loc = redeclaredProperty ?
1542 redeclaredProperty->getLocation() :
1543 property->getLocation();
1544
1545 SetterMethod =
1546 ObjCMethodDecl::Create(Context, Loc, Loc,
1547 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001548 CD, /*isInstance=*/true, /*isVariadic=*/false,
1549 /*isSynthesized=*/true,
1550 /*isImplicitlyDeclared=*/true,
1551 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001552 (property->getPropertyImplementation() ==
1553 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001554 ObjCMethodDecl::Optional :
1555 ObjCMethodDecl::Required);
1556
Ted Kremenek9d64c152010-03-12 00:38:38 +00001557 // Invent the arguments for the setter. We don't bother making a
1558 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001559 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1560 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001561 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001562 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001563 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001564 SC_None,
1565 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001566 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001567 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001568
1569 AddPropertyAttrs(*this, SetterMethod, property);
1570
Ted Kremenek9d64c152010-03-12 00:38:38 +00001571 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001572 // FIXME: Eventually this shouldn't be needed, as the lexical context
1573 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001574 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001575 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001576 } else
1577 // A user declared setter will be synthesize when @synthesize of
1578 // the property with the same name is seen in the @implementation
1579 SetterMethod->setSynthesized(true);
1580 property->setSetterMethodDecl(SetterMethod);
1581 }
1582 // Add any synthesized methods to the global pool. This allows us to
1583 // handle the following, which is supported by GCC (and part of the design).
1584 //
1585 // @interface Foo
1586 // @property double bar;
1587 // @end
1588 //
1589 // void thisIsUnfortunate() {
1590 // id foo;
1591 // double bar = [foo bar];
1592 // }
1593 //
1594 if (GetterMethod)
1595 AddInstanceMethodToGlobalPool(GetterMethod);
1596 if (SetterMethod)
1597 AddInstanceMethodToGlobalPool(SetterMethod);
1598}
1599
John McCalld226f652010-08-21 09:40:31 +00001600void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001601 SourceLocation Loc,
1602 unsigned &Attributes) {
1603 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001604 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001605 return;
1606
1607 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001608 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001609
1610 // readonly and readwrite/assign/retain/copy conflict.
1611 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1612 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1613 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001614 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001615 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001616 ObjCDeclSpec::DQ_PR_retain |
1617 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001618 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1619 "readwrite" :
1620 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1621 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001622 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1623 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001624 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1625 "copy" : "retain";
1626
1627 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1628 diag::err_objc_property_attr_mutually_exclusive :
1629 diag::warn_objc_property_attr_mutually_exclusive)
1630 << "readonly" << which;
1631 }
1632
1633 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001634 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1635 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1636 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001637 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001638 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001639 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1640 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1641 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1642 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001643 }
1644
1645 // Check for more than one of { assign, copy, retain }.
1646 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1647 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1648 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1649 << "assign" << "copy";
1650 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1651 }
1652 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1653 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1654 << "assign" << "retain";
1655 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1656 }
John McCallf85e1932011-06-15 23:02:42 +00001657 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1658 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1659 << "assign" << "strong";
1660 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1661 }
1662 if (getLangOptions().ObjCAutoRefCount &&
1663 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1664 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1665 << "assign" << "weak";
1666 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1667 }
1668 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1669 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1670 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1671 << "unsafe_unretained" << "copy";
1672 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1673 }
1674 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1675 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1676 << "unsafe_unretained" << "retain";
1677 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1678 }
1679 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1680 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1681 << "unsafe_unretained" << "strong";
1682 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1683 }
1684 if (getLangOptions().ObjCAutoRefCount &&
1685 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1686 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1687 << "unsafe_unretained" << "weak";
1688 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1689 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001690 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1691 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1692 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1693 << "copy" << "retain";
1694 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1695 }
John McCallf85e1932011-06-15 23:02:42 +00001696 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1697 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1698 << "copy" << "strong";
1699 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1700 }
1701 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1702 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1703 << "copy" << "weak";
1704 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1705 }
1706 }
1707 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1708 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1709 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1710 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001711 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001712 }
1713 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1714 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1715 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1716 << "strong" << "weak";
1717 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001718 }
1719
1720 // Warn if user supplied no assignment attribute, property is
1721 // readwrite, and this is an object type.
1722 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001723 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1724 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1725 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001726 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1727 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001728 if (getLangOptions().ObjCAutoRefCount)
1729 // With arc, @property definitions should default to (strong) when
1730 // not specified
1731 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
1732 else {
1733 // Skip this warning in gc-only mode.
Douglas Gregore289d812011-09-13 17:21:33 +00001734 if (getLangOptions().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001735 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001736
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001737 // If non-gc code warn that this is likely inappropriate.
Douglas Gregore289d812011-09-13 17:21:33 +00001738 if (getLangOptions().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001739 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1740 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001741
1742 // FIXME: Implement warning dependent on NSCopying being
1743 // implemented. See also:
1744 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1745 // (please trim this list while you are at it).
1746 }
1747
1748 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001749 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Douglas Gregore289d812011-09-13 17:21:33 +00001750 && getLangOptions().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001751 && PropertyTy->isBlockPointerType())
1752 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001753 else if (getLangOptions().ObjCAutoRefCount &&
1754 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1755 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1756 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1757 PropertyTy->isBlockPointerType())
1758 Diag(Loc, diag::warn_objc_property_retain_of_block);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001759}