blob: 80c440907489a79eba8ea23160c82b73966c5575 [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.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001156 if (!PropertyFromSuper ||
1157 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001158 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1159 if (!PropEntry)
1160 PropEntry = Prop;
1161 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001162 }
1163 // scan through protocol's protocols.
1164 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1165 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001166 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001167 }
1168}
1169
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001170/// CollectClassPropertyImplementations - This routine collects list of
1171/// properties to be implemented in the class. This includes, class's
1172/// and its conforming protocols' properties.
1173static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1174 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1175 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1176 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1177 E = IDecl->prop_end(); P != E; ++P) {
1178 ObjCPropertyDecl *Prop = (*P);
1179 PropMap[Prop->getIdentifier()] = Prop;
1180 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001181 for (ObjCInterfaceDecl::all_protocol_iterator
1182 PI = IDecl->all_referenced_protocol_begin(),
1183 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001184 CollectClassPropertyImplementations((*PI), PropMap);
1185 }
1186 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1187 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1188 E = PDecl->prop_end(); P != E; ++P) {
1189 ObjCPropertyDecl *Prop = (*P);
1190 PropMap[Prop->getIdentifier()] = Prop;
1191 }
1192 // scan through protocol's protocols.
1193 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1194 E = PDecl->protocol_end(); PI != E; ++PI)
1195 CollectClassPropertyImplementations((*PI), PropMap);
1196 }
1197}
1198
1199/// CollectSuperClassPropertyImplementations - This routine collects list of
1200/// properties to be implemented in super class(s) and also coming from their
1201/// conforming protocols.
1202static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1203 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1204 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1205 while (SDecl) {
1206 CollectClassPropertyImplementations(SDecl, PropMap);
1207 SDecl = SDecl->getSuperClass();
1208 }
1209 }
1210}
1211
Ted Kremenek9d64c152010-03-12 00:38:38 +00001212/// LookupPropertyDecl - Looks up a property in the current class and all
1213/// its protocols.
1214ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1215 IdentifierInfo *II) {
1216 if (const ObjCInterfaceDecl *IDecl =
1217 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1218 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1219 E = IDecl->prop_end(); P != E; ++P) {
1220 ObjCPropertyDecl *Prop = (*P);
1221 if (Prop->getIdentifier() == II)
1222 return Prop;
1223 }
1224 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001225 for (ObjCInterfaceDecl::all_protocol_iterator
1226 PI = IDecl->all_referenced_protocol_begin(),
1227 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001228 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1229 if (Prop)
1230 return Prop;
1231 }
1232 }
1233 else if (const ObjCProtocolDecl *PDecl =
1234 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1235 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1236 E = PDecl->prop_end(); P != E; ++P) {
1237 ObjCPropertyDecl *Prop = (*P);
1238 if (Prop->getIdentifier() == II)
1239 return Prop;
1240 }
1241 // scan through protocol's protocols.
1242 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1243 E = PDecl->protocol_end(); PI != E; ++PI) {
1244 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1245 if (Prop)
1246 return Prop;
1247 }
1248 }
1249 return 0;
1250}
1251
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001252static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1253 ASTContext &Ctx) {
1254 llvm::SmallString<128> ivarName;
1255 {
1256 llvm::raw_svector_ostream os(ivarName);
1257 os << '_' << Prop->getIdentifier()->getName();
1258 }
1259 return &Ctx.Idents.get(ivarName.str());
1260}
1261
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001262/// DefaultSynthesizeProperties - This routine default synthesizes all
1263/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001264void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1265 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001266
1267 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1268 CollectClassPropertyImplementations(IDecl, PropMap);
1269 if (PropMap.empty())
1270 return;
1271 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1272 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1273
1274 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1275 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1276 ObjCPropertyDecl *Prop = P->second;
1277 // If property to be implemented in the super class, ignore.
1278 if (SuperPropMap[Prop->getIdentifier()])
1279 continue;
1280 // Is there a matching propery synthesize/dynamic?
1281 if (Prop->isInvalidDecl() ||
1282 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1283 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1284 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001285 // Property may have been synthesized by user.
1286 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1287 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001288 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1289 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1290 continue;
1291 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1292 continue;
1293 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001294
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001295
1296 // We use invalid SourceLocations for the synthesized ivars since they
1297 // aren't really synthesized at a particular location; they just exist.
1298 // Saying that they are located at the @implementation isn't really going
1299 // to help users.
1300 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001301 true,
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001302 /* property = */ Prop->getIdentifier(),
1303 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001304 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001305 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001306}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001307
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001308void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1309 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1310 return;
1311 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1312 if (!IC)
1313 return;
1314 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1315 DefaultSynthesizeProperties(S, IC, IDecl);
1316}
1317
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001318void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001319 ObjCContainerDecl *CDecl,
1320 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001321 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1322 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1323 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1324
Ted Kremenek9d64c152010-03-12 00:38:38 +00001325 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001326 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001327 if (PropMap.empty())
1328 return;
1329
1330 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1331 for (ObjCImplDecl::propimpl_iterator
1332 I = IMPDecl->propimpl_begin(),
1333 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1334 PropImplMap.insert((*I)->getPropertyDecl());
1335
1336 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1337 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1338 ObjCPropertyDecl *Prop = P->second;
1339 // Is there a matching propery synthesize/dynamic?
1340 if (Prop->isInvalidDecl() ||
1341 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001342 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001343 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001344 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001345 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001346 isa<ObjCCategoryDecl>(CDecl) ?
1347 diag::warn_setter_getter_impl_required_in_category :
1348 diag::warn_setter_getter_impl_required)
1349 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001350 Diag(Prop->getLocation(),
1351 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001352 }
1353
1354 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001355 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001356 isa<ObjCCategoryDecl>(CDecl) ?
1357 diag::warn_setter_getter_impl_required_in_category :
1358 diag::warn_setter_getter_impl_required)
1359 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001360 Diag(Prop->getLocation(),
1361 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001362 }
1363 }
1364}
1365
1366void
1367Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1368 ObjCContainerDecl* IDecl) {
1369 // Rules apply in non-GC mode only
Douglas Gregore289d812011-09-13 17:21:33 +00001370 if (getLangOptions().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001371 return;
1372 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1373 E = IDecl->prop_end();
1374 I != E; ++I) {
1375 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001376 ObjCMethodDecl *GetterMethod = 0;
1377 ObjCMethodDecl *SetterMethod = 0;
1378 bool LookedUpGetterSetter = false;
1379
Ted Kremenek9d64c152010-03-12 00:38:38 +00001380 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001381 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001382
John McCall265941b2011-09-13 18:31:23 +00001383 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1384 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001385 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1386 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1387 LookedUpGetterSetter = true;
1388 if (GetterMethod) {
1389 Diag(GetterMethod->getLocation(),
1390 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001391 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001392 Diag(Property->getLocation(), diag::note_property_declare);
1393 }
1394 if (SetterMethod) {
1395 Diag(SetterMethod->getLocation(),
1396 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001397 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001398 Diag(Property->getLocation(), diag::note_property_declare);
1399 }
1400 }
1401
Ted Kremenek9d64c152010-03-12 00:38:38 +00001402 // We only care about readwrite atomic property.
1403 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1404 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1405 continue;
1406 if (const ObjCPropertyImplDecl *PIDecl
1407 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1408 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1409 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001410 if (!LookedUpGetterSetter) {
1411 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1412 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1413 LookedUpGetterSetter = true;
1414 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001415 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1416 SourceLocation MethodLoc =
1417 (GetterMethod ? GetterMethod->getLocation()
1418 : SetterMethod->getLocation());
1419 Diag(MethodLoc, diag::warn_atomic_property_rule)
1420 << Property->getIdentifier();
1421 Diag(Property->getLocation(), diag::note_property_declare);
1422 }
1423 }
1424 }
1425}
1426
John McCallf85e1932011-06-15 23:02:42 +00001427void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
Douglas Gregore289d812011-09-13 17:21:33 +00001428 if (getLangOptions().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001429 return;
1430
1431 for (ObjCImplementationDecl::propimpl_iterator
1432 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1433 ObjCPropertyImplDecl *PID = *i;
1434 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1435 continue;
1436
1437 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001438 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1439 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001440 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1441 if (!method)
1442 continue;
1443 ObjCMethodFamily family = method->getMethodFamily();
1444 if (family == OMF_alloc || family == OMF_copy ||
1445 family == OMF_mutableCopy || family == OMF_new) {
1446 if (getLangOptions().ObjCAutoRefCount)
1447 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1448 else
1449 Diag(PID->getLocation(), diag::warn_ownin_getter_rule);
1450 Diag(PD->getLocation(), diag::note_property_declare);
1451 }
1452 }
1453 }
1454}
1455
John McCall5de74d12010-11-10 07:01:40 +00001456/// AddPropertyAttrs - Propagates attributes from a property to the
1457/// implicitly-declared getter or setter for that property.
1458static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1459 ObjCPropertyDecl *Property) {
1460 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001461 for (Decl::attr_iterator A = Property->attr_begin(),
1462 AEnd = Property->attr_end();
1463 A != AEnd; ++A) {
1464 if (isa<DeprecatedAttr>(*A) ||
1465 isa<UnavailableAttr>(*A) ||
1466 isa<AvailabilityAttr>(*A))
1467 PropertyMethod->addAttr((*A)->clone(S.Context));
1468 }
John McCall5de74d12010-11-10 07:01:40 +00001469}
1470
Ted Kremenek9d64c152010-03-12 00:38:38 +00001471/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1472/// have the property type and issue diagnostics if they don't.
1473/// Also synthesize a getter/setter method if none exist (and update the
1474/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1475/// methods is the "right" thing to do.
1476void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001477 ObjCContainerDecl *CD,
1478 ObjCPropertyDecl *redeclaredProperty,
1479 ObjCContainerDecl *lexicalDC) {
1480
Ted Kremenek9d64c152010-03-12 00:38:38 +00001481 ObjCMethodDecl *GetterMethod, *SetterMethod;
1482
1483 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1484 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1485 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1486 property->getLocation());
1487
1488 if (SetterMethod) {
1489 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1490 property->getPropertyAttributes();
1491 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1492 Context.getCanonicalType(SetterMethod->getResultType()) !=
1493 Context.VoidTy)
1494 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1495 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001496 !Context.hasSameUnqualifiedType(
1497 (*SetterMethod->param_begin())->getType(), property->getType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001498 Diag(property->getLocation(),
1499 diag::warn_accessor_property_type_mismatch)
1500 << property->getDeclName()
1501 << SetterMethod->getSelector();
1502 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1503 }
1504 }
1505
1506 // Synthesize getter/setter methods if none exist.
1507 // Find the default getter and if one not found, add one.
1508 // FIXME: The synthesized property we set here is misleading. We almost always
1509 // synthesize these methods unless the user explicitly provided prototypes
1510 // (which is odd, but allowed). Sema should be typechecking that the
1511 // declarations jive in that situation (which it is not currently).
1512 if (!GetterMethod) {
1513 // No instance method of same name as property getter name was found.
1514 // Declare a getter method and add it to the list of methods
1515 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001516 SourceLocation Loc = redeclaredProperty ?
1517 redeclaredProperty->getLocation() :
1518 property->getLocation();
1519
1520 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1521 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001522 property->getType(), 0, CD, /*isInstance=*/true,
1523 /*isVariadic=*/false, /*isSynthesized=*/true,
1524 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001525 (property->getPropertyImplementation() ==
1526 ObjCPropertyDecl::Optional) ?
1527 ObjCMethodDecl::Optional :
1528 ObjCMethodDecl::Required);
1529 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001530
1531 AddPropertyAttrs(*this, GetterMethod, property);
1532
Ted Kremenek23173d72010-05-18 21:09:07 +00001533 // FIXME: Eventually this shouldn't be needed, as the lexical context
1534 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001535 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001536 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001537 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1538 GetterMethod->addAttr(
1539 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001540 } else
1541 // A user declared getter will be synthesize when @synthesize of
1542 // the property with the same name is seen in the @implementation
1543 GetterMethod->setSynthesized(true);
1544 property->setGetterMethodDecl(GetterMethod);
1545
1546 // Skip setter if property is read-only.
1547 if (!property->isReadOnly()) {
1548 // Find the default setter and if one not found, add one.
1549 if (!SetterMethod) {
1550 // No instance method of same name as property setter name was found.
1551 // Declare a setter method and add it to the list of methods
1552 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001553 SourceLocation Loc = redeclaredProperty ?
1554 redeclaredProperty->getLocation() :
1555 property->getLocation();
1556
1557 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001558 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001559 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001560 CD, /*isInstance=*/true, /*isVariadic=*/false,
1561 /*isSynthesized=*/true,
1562 /*isImplicitlyDeclared=*/true,
1563 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001564 (property->getPropertyImplementation() ==
1565 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001566 ObjCMethodDecl::Optional :
1567 ObjCMethodDecl::Required);
1568
Ted Kremenek9d64c152010-03-12 00:38:38 +00001569 // Invent the arguments for the setter. We don't bother making a
1570 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001571 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1572 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001573 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001574 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001575 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001576 SC_None,
1577 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001578 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001579 SetterMethod->setMethodParams(Context, Argument,
1580 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001581
1582 AddPropertyAttrs(*this, SetterMethod, property);
1583
Ted Kremenek9d64c152010-03-12 00:38:38 +00001584 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001585 // FIXME: Eventually this shouldn't be needed, as the lexical context
1586 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001587 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001588 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001589 } else
1590 // A user declared setter will be synthesize when @synthesize of
1591 // the property with the same name is seen in the @implementation
1592 SetterMethod->setSynthesized(true);
1593 property->setSetterMethodDecl(SetterMethod);
1594 }
1595 // Add any synthesized methods to the global pool. This allows us to
1596 // handle the following, which is supported by GCC (and part of the design).
1597 //
1598 // @interface Foo
1599 // @property double bar;
1600 // @end
1601 //
1602 // void thisIsUnfortunate() {
1603 // id foo;
1604 // double bar = [foo bar];
1605 // }
1606 //
1607 if (GetterMethod)
1608 AddInstanceMethodToGlobalPool(GetterMethod);
1609 if (SetterMethod)
1610 AddInstanceMethodToGlobalPool(SetterMethod);
1611}
1612
John McCalld226f652010-08-21 09:40:31 +00001613void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001614 SourceLocation Loc,
1615 unsigned &Attributes) {
1616 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001617 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001618 return;
1619
1620 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001621 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001622
1623 // readonly and readwrite/assign/retain/copy conflict.
1624 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1625 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1626 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001627 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001628 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001629 ObjCDeclSpec::DQ_PR_retain |
1630 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001631 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1632 "readwrite" :
1633 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1634 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001635 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1636 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001637 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1638 "copy" : "retain";
1639
1640 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1641 diag::err_objc_property_attr_mutually_exclusive :
1642 diag::warn_objc_property_attr_mutually_exclusive)
1643 << "readonly" << which;
1644 }
1645
1646 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001647 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1648 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1649 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001650 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001651 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001652 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1653 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1654 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1655 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001656 }
1657
1658 // Check for more than one of { assign, copy, retain }.
1659 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1660 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1661 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1662 << "assign" << "copy";
1663 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1664 }
1665 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1666 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1667 << "assign" << "retain";
1668 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1669 }
John McCallf85e1932011-06-15 23:02:42 +00001670 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1671 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1672 << "assign" << "strong";
1673 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1674 }
1675 if (getLangOptions().ObjCAutoRefCount &&
1676 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1677 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1678 << "assign" << "weak";
1679 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1680 }
1681 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1682 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1683 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1684 << "unsafe_unretained" << "copy";
1685 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1686 }
1687 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1688 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1689 << "unsafe_unretained" << "retain";
1690 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1691 }
1692 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1693 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1694 << "unsafe_unretained" << "strong";
1695 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1696 }
1697 if (getLangOptions().ObjCAutoRefCount &&
1698 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1699 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1700 << "unsafe_unretained" << "weak";
1701 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1702 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001703 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1704 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1705 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1706 << "copy" << "retain";
1707 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1708 }
John McCallf85e1932011-06-15 23:02:42 +00001709 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1710 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1711 << "copy" << "strong";
1712 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1713 }
1714 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1715 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1716 << "copy" << "weak";
1717 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1718 }
1719 }
1720 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1721 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1722 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1723 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001724 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001725 }
1726 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1727 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1728 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1729 << "strong" << "weak";
1730 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001731 }
1732
1733 // Warn if user supplied no assignment attribute, property is
1734 // readwrite, and this is an object type.
1735 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001736 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1737 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1738 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001739 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1740 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001741 if (getLangOptions().ObjCAutoRefCount)
1742 // With arc, @property definitions should default to (strong) when
1743 // not specified
1744 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
1745 else {
1746 // Skip this warning in gc-only mode.
Douglas Gregore289d812011-09-13 17:21:33 +00001747 if (getLangOptions().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001748 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001749
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001750 // If non-gc code warn that this is likely inappropriate.
Douglas Gregore289d812011-09-13 17:21:33 +00001751 if (getLangOptions().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001752 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1753 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001754
1755 // FIXME: Implement warning dependent on NSCopying being
1756 // implemented. See also:
1757 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1758 // (please trim this list while you are at it).
1759 }
1760
1761 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001762 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Douglas Gregore289d812011-09-13 17:21:33 +00001763 && getLangOptions().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001764 && PropertyTy->isBlockPointerType())
1765 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001766 else if (getLangOptions().ObjCAutoRefCount &&
1767 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1768 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1769 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1770 PropertyTy->isBlockPointerType())
1771 Diag(Loc, diag::warn_objc_property_retain_of_block);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001772}