blob: 251af224153fa31cda7e48258846fdd103c1ca70 [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 McCalld226f652010-08-21 09:40:31 +000027Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
28 FieldDeclarator &FD,
29 ObjCDeclSpec &ODS,
30 Selector GetterSel,
31 Selector SetterSel,
32 Decl *ClassCategory,
33 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000034 tok::ObjCKeywordKind MethodImplKind,
35 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000036 unsigned Attributes = ODS.getPropertyAttributes();
37 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
38 // default is readwrite!
39 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
40 // property is defaulted to 'assign' if it is readwrite and is
41 // not retain or copy
42 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
43 (isReadWrite &&
44 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
45 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000046
John McCallbf1a0282010-06-04 23:28:52 +000047 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
48 QualType T = TSI->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +000049 if (T->isReferenceType()) {
50 Diag(AtLoc, diag::error_reference_property);
John McCalld226f652010-08-21 09:40:31 +000051 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +000052 }
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000053 // Proceed with constructing the ObjCPropertDecls.
54 ObjCContainerDecl *ClassDecl =
John McCalld226f652010-08-21 09:40:31 +000055 cast<ObjCContainerDecl>(ClassCategory);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000056
Ted Kremenek28685ab2010-03-12 00:46:40 +000057 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000058 if (CDecl->IsClassExtension()) {
John McCalld226f652010-08-21 09:40:31 +000059 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000060 FD, GetterSel, SetterSel,
61 isAssign, isReadWrite,
62 Attributes,
63 isOverridingProperty, TSI,
64 MethodImplKind);
65 if (Res)
66 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
67 return Res;
68 }
69
John McCalld226f652010-08-21 09:40:31 +000070 Decl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
71 GetterSel, SetterSel,
72 isAssign, isReadWrite,
73 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000074 if (lexicalDC)
75 Res->setLexicalDeclContext(lexicalDC);
76
Fariborz Jahanian842f07b2010-03-30 22:40:11 +000077 // Validate the attributes on the @property.
78 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
79 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000080}
Ted Kremenek2d2f9362010-03-12 00:49:00 +000081
John McCalld226f652010-08-21 09:40:31 +000082Decl *
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000083Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
84 SourceLocation AtLoc, FieldDeclarator &FD,
85 Selector GetterSel, Selector SetterSel,
86 const bool isAssign,
87 const bool isReadWrite,
88 const unsigned Attributes,
89 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +000090 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000091 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000092
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000093 // Diagnose if this property is already in continuation class.
94 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000095 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenek894ae6a2010-03-15 18:47:25 +000096
Ted Kremenek9f550ff2010-03-15 20:11:46 +000097 if (ObjCPropertyDecl *prevDecl =
98 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000099 Diag(AtLoc, diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000100 Diag(prevDecl->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000101 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000102 }
103
104 // Create a new ObjCPropertyDecl with the DeclContext being
105 // the class extension.
106 ObjCPropertyDecl *PDecl =
107 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
108 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000109 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
110 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
111 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
112 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
113
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000114 DC->addDecl(PDecl);
115
116 // We need to look in the @interface to see if the @property was
117 // already declared.
118 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
119 if (!CCPrimary) {
120 Diag(CDecl->getLocation(), diag::err_continuation_class);
121 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000122 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000123 }
124
125 // Find the property in continuation class's primary class only.
126 ObjCPropertyDecl *PIDecl =
127 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
128
129 if (!PIDecl) {
130 // No matching property found in the primary class. Just fall thru
131 // and add property to continuation class's primary class.
132 ObjCPropertyDecl *PDecl =
133 CreatePropertyDecl(S, CCPrimary, AtLoc,
134 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000135 Attributes, T, MethodImplKind, DC);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000136 // Mark written attribute as having no attribute because
137 // this is not a user-written property declaration in primary
138 // class.
139 PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000140
141 // A case of continuation class adding a new property in the class. This
142 // is not what it was meant for. However, gcc supports it and so should we.
143 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000144 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
145 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000146 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000147 }
148
149 // The property 'PIDecl's readonly attribute will be over-ridden
150 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000151 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000152 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
153 unsigned retainCopyNonatomic =
154 (ObjCPropertyDecl::OBJC_PR_retain |
155 ObjCPropertyDecl::OBJC_PR_copy |
156 ObjCPropertyDecl::OBJC_PR_nonatomic);
157 if ((Attributes & retainCopyNonatomic) !=
158 (PIkind & retainCopyNonatomic)) {
159 Diag(AtLoc, diag::warn_property_attr_mismatch);
160 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000161 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000162 DeclContext *DC = cast<DeclContext>(CCPrimary);
163 if (!ObjCPropertyDecl::findPropertyDecl(DC,
164 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000165 // Protocol is not in the primary class. Must build one for it.
166 ObjCDeclSpec ProtocolPropertyODS;
167 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
168 // and ObjCPropertyDecl::PropertyAttributeKind have identical
169 // values. Should consolidate both into one enum type.
170 ProtocolPropertyODS.
171 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
172 PIkind);
173
John McCalld226f652010-08-21 09:40:31 +0000174 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000175 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
176 PIDecl->getGetterName(),
177 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000178 CCPrimary, isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000179 MethodImplKind,
180 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000181 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000182 }
183 PIDecl->makeitReadWriteAttribute();
184 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
185 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
186 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
187 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
188 PIDecl->setSetterName(SetterSel);
189 } else {
190 Diag(AtLoc, diag::err_use_continuation_class)
191 << CCPrimary->getDeclName();
192 Diag(PIDecl->getLocation(), diag::note_property_declare);
193 }
194 *isOverridingProperty = true;
195 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000196 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000197 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000198}
199
200ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
201 ObjCContainerDecl *CDecl,
202 SourceLocation AtLoc,
203 FieldDeclarator &FD,
204 Selector GetterSel,
205 Selector SetterSel,
206 const bool isAssign,
207 const bool isReadWrite,
208 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000209 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000210 tok::ObjCKeywordKind MethodImplKind,
211 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000212 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000213 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000214
215 // Issue a warning if property is 'assign' as default and its object, which is
216 // gc'able conforms to NSCopying protocol
217 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
218 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000219 if (const ObjCObjectPointerType *ObjPtrTy =
220 T->getAs<ObjCObjectPointerType>()) {
221 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
222 if (IDecl)
223 if (ObjCProtocolDecl* PNSCopying =
224 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
225 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
226 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000227 }
John McCallc12c5bb2010-05-15 11:32:37 +0000228 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000229 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
230
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000231 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000232 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
233 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000234 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000235
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000236 if (ObjCPropertyDecl *prevDecl =
237 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000238 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000239 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000240 PDecl->setInvalidDecl();
241 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000242 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000243 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000244 if (lexicalDC)
245 PDecl->setLexicalDeclContext(lexicalDC);
246 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000247
248 if (T->isArrayType() || T->isFunctionType()) {
249 Diag(AtLoc, diag::err_property_type) << T;
250 PDecl->setInvalidDecl();
251 }
252
253 ProcessDeclAttributes(S, PDecl, FD.D);
254
255 // Regardless of setter/getter attribute, we save the default getter/setter
256 // selector names in anticipation of declaration of setter/getter methods.
257 PDecl->setGetterName(GetterSel);
258 PDecl->setSetterName(SetterSel);
259
260 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
261 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
262
263 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
264 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
265
266 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
267 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
268
269 if (isReadWrite)
270 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
271
272 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
273 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
274
275 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
276 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
277
278 if (isAssign)
279 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
280
281 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
282 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
283
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000284 PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes());
285
Ted Kremenek28685ab2010-03-12 00:46:40 +0000286 if (MethodImplKind == tok::objc_required)
287 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
288 else if (MethodImplKind == tok::objc_optional)
289 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000290
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000291 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000292}
293
294
295/// ActOnPropertyImplDecl - This routine performs semantic checks and
296/// builds the AST node for a property implementation declaration; declared
297/// as @synthesize or @dynamic.
298///
John McCalld226f652010-08-21 09:40:31 +0000299Decl *Sema::ActOnPropertyImplDecl(Scope *S,
300 SourceLocation AtLoc,
301 SourceLocation PropertyLoc,
302 bool Synthesize,
303 Decl *ClassCatImpDecl,
304 IdentifierInfo *PropertyId,
305 IdentifierInfo *PropertyIvar) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000306 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000307 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000308 // Make sure we have a context for the property implementation declaration.
309 if (!ClassImpDecl) {
310 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000311 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000312 }
313 ObjCPropertyDecl *property = 0;
314 ObjCInterfaceDecl* IDecl = 0;
315 // Find the class or category class where this property must have
316 // a declaration.
317 ObjCImplementationDecl *IC = 0;
318 ObjCCategoryImplDecl* CatImplClass = 0;
319 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
320 IDecl = IC->getClassInterface();
321 // We always synthesize an interface for an implementation
322 // without an interface decl. So, IDecl is always non-zero.
323 assert(IDecl &&
324 "ActOnPropertyImplDecl - @implementation without @interface");
325
326 // Look for this property declaration in the @implementation's @interface
327 property = IDecl->FindPropertyDeclaration(PropertyId);
328 if (!property) {
329 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000330 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000331 }
332 if (const ObjCCategoryDecl *CD =
333 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
334 if (!CD->IsClassExtension()) {
335 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
336 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000337 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000338 }
339 }
340 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
341 if (Synthesize) {
342 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000343 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000344 }
345 IDecl = CatImplClass->getClassInterface();
346 if (!IDecl) {
347 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000348 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000349 }
350 ObjCCategoryDecl *Category =
351 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
352
353 // If category for this implementation not found, it is an error which
354 // has already been reported eralier.
355 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000356 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000357 // Look for this property declaration in @implementation's category
358 property = Category->FindPropertyDeclaration(PropertyId);
359 if (!property) {
360 Diag(PropertyLoc, diag::error_bad_category_property_decl)
361 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000362 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000363 }
364 } else {
365 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000366 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000367 }
368 ObjCIvarDecl *Ivar = 0;
369 // Check that we have a valid, previously declared ivar for @synthesize
370 if (Synthesize) {
371 // @synthesize
372 if (!PropertyIvar)
373 PropertyIvar = PropertyId;
374 QualType PropType = Context.getCanonicalType(property->getType());
375 // Check that this is a previously declared 'ivar' in 'IDecl' interface
376 ObjCInterfaceDecl *ClassDeclared;
377 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
378 if (!Ivar) {
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000379 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000380 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian2846b2b2010-04-06 23:36:17 +0000381 ObjCIvarDecl::Protected,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000382 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000383 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000384 IDecl->makeDeclVisibleInContext(Ivar, false);
385 property->setPropertyIvarDecl(Ivar);
386
387 if (!getLangOptions().ObjCNonFragileABI)
388 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
389 // Note! I deliberately want it to fall thru so, we have a
390 // a property implementation and to avoid future warnings.
391 } else if (getLangOptions().ObjCNonFragileABI &&
392 ClassDeclared != IDecl) {
393 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
394 << property->getDeclName() << Ivar->getDeclName()
395 << ClassDeclared->getDeclName();
396 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000397 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000398 // Note! I deliberately want it to fall thru so more errors are caught.
399 }
400 QualType IvarType = Context.getCanonicalType(Ivar->getType());
401
402 // Check that type of property and its ivar are type compatible.
403 if (PropType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000404 bool compat = false;
405 if (isa<ObjCObjectPointerType>(PropType)
406 && isa<ObjCObjectPointerType>(IvarType))
407 compat =
408 Context.canAssignObjCInterfaces(
409 PropType->getAs<ObjCObjectPointerType>(),
410 IvarType->getAs<ObjCObjectPointerType>());
411 else
412 compat = (CheckAssignmentConstraints(PropType, IvarType) == Compatible);
413 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000414 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000415 << property->getDeclName() << PropType
416 << Ivar->getDeclName() << IvarType;
417 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000418 // Note! I deliberately want it to fall thru so, we have a
419 // a property implementation and to avoid future warnings.
420 }
421
422 // FIXME! Rules for properties are somewhat different that those
423 // for assignments. Use a new routine to consolidate all cases;
424 // specifically for property redeclarations as well as for ivars.
425 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
426 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
427 if (lhsType != rhsType &&
428 lhsType->isArithmeticType()) {
429 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000430 << property->getDeclName() << PropType
431 << Ivar->getDeclName() << IvarType;
432 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000433 // Fall thru - see previous comment
434 }
435 // __weak is explicit. So it works on Canonical type.
436 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
437 getLangOptions().getGCMode() != LangOptions::NonGC) {
438 Diag(PropertyLoc, diag::error_weak_property)
439 << property->getDeclName() << Ivar->getDeclName();
440 // Fall thru - see previous comment
441 }
442 if ((property->getType()->isObjCObjectPointerType() ||
443 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
444 getLangOptions().getGCMode() != LangOptions::NonGC) {
445 Diag(PropertyLoc, diag::error_strong_property)
446 << property->getDeclName() << Ivar->getDeclName();
447 // Fall thru - see previous comment
448 }
449 }
450 } else if (PropertyIvar)
451 // @dynamic
452 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
453 assert (property && "ActOnPropertyImplDecl - property declaration missing");
454 ObjCPropertyImplDecl *PIDecl =
455 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
456 property,
457 (Synthesize ?
458 ObjCPropertyImplDecl::Synthesize
459 : ObjCPropertyImplDecl::Dynamic),
460 Ivar);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000461 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
462 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000463 if (getLangOptions().CPlusPlus && Synthesize &&
464 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000465 // For Objective-C++, need to synthesize the AST for the IVAR object to be
466 // returned by the getter as it must conform to C++'s copy-return rules.
467 // FIXME. Eventually we want to do this for Objective-C as well.
468 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
469 DeclRefExpr *SelfExpr =
470 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
471 SourceLocation());
472 Expr *IvarRefExpr =
473 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
474 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000475 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000476 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000477 SourceLocation(),
478 getterMethod->getResultType(),
479 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000480 SourceLocation(),
481 Owned(IvarRefExpr));
482 if (!Res.isInvalid()) {
483 Expr *ResExpr = Res.takeAs<Expr>();
484 if (ResExpr)
485 ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr);
486 PIDecl->setGetterCXXConstructor(ResExpr);
487 }
488 }
489 }
490 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
491 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000492 if (getLangOptions().CPlusPlus && Synthesize
493 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000494 // FIXME. Eventually we want to do this for Objective-C as well.
495 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
496 DeclRefExpr *SelfExpr =
497 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
498 SourceLocation());
499 Expr *lhs =
500 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
501 SelfExpr, true, true);
502 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
503 ParmVarDecl *Param = (*P);
504 Expr *rhs = new (Context) DeclRefExpr(Param,Param->getType(),
505 SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000506 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000507 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000508 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
509 }
510 }
511
Ted Kremenek28685ab2010-03-12 00:46:40 +0000512 if (IC) {
513 if (Synthesize)
514 if (ObjCPropertyImplDecl *PPIDecl =
515 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
516 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
517 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
518 << PropertyIvar;
519 Diag(PPIDecl->getLocation(), diag::note_previous_use);
520 }
521
522 if (ObjCPropertyImplDecl *PPIDecl
523 = IC->FindPropertyImplDecl(PropertyId)) {
524 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
525 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000526 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000527 }
528 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000529 if (getLangOptions().ObjCNonFragileABI2) {
530 // Diagnose if an ivar was lazily synthesdized due to a previous
531 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000532 // but it requires an ivar of different name.
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000533 ObjCInterfaceDecl *ClassDeclared;
534 ObjCIvarDecl *Ivar = 0;
535 if (!Synthesize)
536 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
537 else {
538 if (PropertyIvar && PropertyIvar != PropertyId)
539 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
540 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000541 // Issue diagnostics only if Ivar belongs to current class.
542 if (Ivar && Ivar->getSynthesize() &&
543 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000544 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
545 << PropertyId;
546 Ivar->setInvalidDecl();
547 }
548 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000549 } else {
550 if (Synthesize)
551 if (ObjCPropertyImplDecl *PPIDecl =
552 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
553 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
554 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
555 << PropertyIvar;
556 Diag(PPIDecl->getLocation(), diag::note_previous_use);
557 }
558
559 if (ObjCPropertyImplDecl *PPIDecl =
560 CatImplClass->FindPropertyImplDecl(PropertyId)) {
561 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
562 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000563 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000564 }
565 CatImplClass->addPropertyImplementation(PIDecl);
566 }
567
John McCalld226f652010-08-21 09:40:31 +0000568 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569}
570
571//===----------------------------------------------------------------------===//
572// Helper methods.
573//===----------------------------------------------------------------------===//
574
Ted Kremenek9d64c152010-03-12 00:38:38 +0000575/// DiagnosePropertyMismatch - Compares two properties for their
576/// attributes and types and warns on a variety of inconsistencies.
577///
578void
579Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
580 ObjCPropertyDecl *SuperProperty,
581 const IdentifierInfo *inheritedName) {
582 ObjCPropertyDecl::PropertyAttributeKind CAttr =
583 Property->getPropertyAttributes();
584 ObjCPropertyDecl::PropertyAttributeKind SAttr =
585 SuperProperty->getPropertyAttributes();
586 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
587 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
588 Diag(Property->getLocation(), diag::warn_readonly_property)
589 << Property->getDeclName() << inheritedName;
590 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
591 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
592 Diag(Property->getLocation(), diag::warn_property_attribute)
593 << Property->getDeclName() << "copy" << inheritedName;
594 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
595 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
596 Diag(Property->getLocation(), diag::warn_property_attribute)
597 << Property->getDeclName() << "retain" << inheritedName;
598
599 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
600 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
601 Diag(Property->getLocation(), diag::warn_property_attribute)
602 << Property->getDeclName() << "atomic" << inheritedName;
603 if (Property->getSetterName() != SuperProperty->getSetterName())
604 Diag(Property->getLocation(), diag::warn_property_attribute)
605 << Property->getDeclName() << "setter" << inheritedName;
606 if (Property->getGetterName() != SuperProperty->getGetterName())
607 Diag(Property->getLocation(), diag::warn_property_attribute)
608 << Property->getDeclName() << "getter" << inheritedName;
609
610 QualType LHSType =
611 Context.getCanonicalType(SuperProperty->getType());
612 QualType RHSType =
613 Context.getCanonicalType(Property->getType());
614
615 if (!Context.typesAreCompatible(LHSType, RHSType)) {
616 // FIXME: Incorporate this test with typesAreCompatible.
617 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
618 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
619 return;
620 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
621 << Property->getType() << SuperProperty->getType() << inheritedName;
622 }
623}
624
625bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
626 ObjCMethodDecl *GetterMethod,
627 SourceLocation Loc) {
628 if (GetterMethod &&
629 GetterMethod->getResultType() != property->getType()) {
630 AssignConvertType result = Incompatible;
631 if (property->getType()->isObjCObjectPointerType())
632 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
633 property->getType());
634 if (result != Compatible) {
635 Diag(Loc, diag::warn_accessor_property_type_mismatch)
636 << property->getDeclName()
637 << GetterMethod->getSelector();
638 Diag(GetterMethod->getLocation(), diag::note_declared_at);
639 return true;
640 }
641 }
642 return false;
643}
644
645/// ComparePropertiesInBaseAndSuper - This routine compares property
646/// declarations in base and its super class, if any, and issues
647/// diagnostics in a variety of inconsistant situations.
648///
649void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
650 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
651 if (!SDecl)
652 return;
653 // FIXME: O(N^2)
654 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
655 E = SDecl->prop_end(); S != E; ++S) {
656 ObjCPropertyDecl *SuperPDecl = (*S);
657 // Does property in super class has declaration in current class?
658 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
659 E = IDecl->prop_end(); I != E; ++I) {
660 ObjCPropertyDecl *PDecl = (*I);
661 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
662 DiagnosePropertyMismatch(PDecl, SuperPDecl,
663 SDecl->getIdentifier());
664 }
665 }
666}
667
668/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
669/// of properties declared in a protocol and compares their attribute against
670/// the same property declared in the class or category.
671void
672Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
673 ObjCProtocolDecl *PDecl) {
674 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
675 if (!IDecl) {
676 // Category
677 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
678 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
679 if (!CatDecl->IsClassExtension())
680 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
681 E = PDecl->prop_end(); P != E; ++P) {
682 ObjCPropertyDecl *Pr = (*P);
683 ObjCCategoryDecl::prop_iterator CP, CE;
684 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000685 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000686 if ((*CP)->getIdentifier() == Pr->getIdentifier())
687 break;
688 if (CP != CE)
689 // Property protocol already exist in class. Diagnose any mismatch.
690 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
691 }
692 return;
693 }
694 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
695 E = PDecl->prop_end(); P != E; ++P) {
696 ObjCPropertyDecl *Pr = (*P);
697 ObjCInterfaceDecl::prop_iterator CP, CE;
698 // Is this property already in class's list of properties?
699 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
700 if ((*CP)->getIdentifier() == Pr->getIdentifier())
701 break;
702 if (CP != CE)
703 // Property protocol already exist in class. Diagnose any mismatch.
704 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
705 }
706}
707
708/// CompareProperties - This routine compares properties
709/// declared in 'ClassOrProtocol' objects (which can be a class or an
710/// inherited protocol with the list of properties for class/category 'CDecl'
711///
John McCalld226f652010-08-21 09:40:31 +0000712void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
713 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000714 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
715
716 if (!IDecl) {
717 // Category
718 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
719 assert (CatDecl && "CompareProperties");
720 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
721 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
722 E = MDecl->protocol_end(); P != E; ++P)
723 // Match properties of category with those of protocol (*P)
724 MatchOneProtocolPropertiesInClass(CatDecl, *P);
725
726 // Go thru the list of protocols for this category and recursively match
727 // their properties with those in the category.
728 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
729 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000730 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000731 } else {
732 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
733 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
734 E = MD->protocol_end(); P != E; ++P)
735 MatchOneProtocolPropertiesInClass(CatDecl, *P);
736 }
737 return;
738 }
739
740 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000741 for (ObjCInterfaceDecl::all_protocol_iterator
742 P = MDecl->all_referenced_protocol_begin(),
743 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000744 // Match properties of class IDecl with those of protocol (*P).
745 MatchOneProtocolPropertiesInClass(IDecl, *P);
746
747 // Go thru the list of protocols for this class and recursively match
748 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +0000749 for (ObjCInterfaceDecl::all_protocol_iterator
750 P = IDecl->all_referenced_protocol_begin(),
751 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000752 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000753 } else {
754 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
755 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
756 E = MD->protocol_end(); P != E; ++P)
757 MatchOneProtocolPropertiesInClass(IDecl, *P);
758 }
759}
760
761/// isPropertyReadonly - Return true if property is readonly, by searching
762/// for the property in the class and in its categories and implementations
763///
764bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
765 ObjCInterfaceDecl *IDecl) {
766 // by far the most common case.
767 if (!PDecl->isReadOnly())
768 return false;
769 // Even if property is ready only, if interface has a user defined setter,
770 // it is not considered read only.
771 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
772 return false;
773
774 // Main class has the property as 'readonly'. Must search
775 // through the category list to see if the property's
776 // attribute has been over-ridden to 'readwrite'.
777 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
778 Category; Category = Category->getNextClassCategory()) {
779 // Even if property is ready only, if a category has a user defined setter,
780 // it is not considered read only.
781 if (Category->getInstanceMethod(PDecl->getSetterName()))
782 return false;
783 ObjCPropertyDecl *P =
784 Category->FindPropertyDeclaration(PDecl->getIdentifier());
785 if (P && !P->isReadOnly())
786 return false;
787 }
788
789 // Also, check for definition of a setter method in the implementation if
790 // all else failed.
791 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
792 if (ObjCImplementationDecl *IMD =
793 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
794 if (IMD->getInstanceMethod(PDecl->getSetterName()))
795 return false;
796 } else if (ObjCCategoryImplDecl *CIMD =
797 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
798 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
799 return false;
800 }
801 }
802 // Lastly, look through the implementation (if one is in scope).
803 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
804 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
805 return false;
806 // If all fails, look at the super class.
807 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
808 return isPropertyReadonly(PDecl, SIDecl);
809 return true;
810}
811
812/// CollectImmediateProperties - This routine collects all properties in
813/// the class and its conforming protocols; but not those it its super class.
814void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000815 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
816 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000817 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
818 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
819 E = IDecl->prop_end(); P != E; ++P) {
820 ObjCPropertyDecl *Prop = (*P);
821 PropMap[Prop->getIdentifier()] = Prop;
822 }
823 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000824 for (ObjCInterfaceDecl::all_protocol_iterator
825 PI = IDecl->all_referenced_protocol_begin(),
826 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000827 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000828 }
829 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
830 if (!CATDecl->IsClassExtension())
831 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
832 E = CATDecl->prop_end(); P != E; ++P) {
833 ObjCPropertyDecl *Prop = (*P);
834 PropMap[Prop->getIdentifier()] = Prop;
835 }
836 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000837 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +0000838 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000839 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000840 }
841 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
842 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
843 E = PDecl->prop_end(); P != E; ++P) {
844 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000845 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
846 // Exclude property for protocols which conform to class's super-class,
847 // as super-class has to implement the property.
848 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
849 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
850 if (!PropEntry)
851 PropEntry = Prop;
852 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000853 }
854 // scan through protocol's protocols.
855 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
856 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000857 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000858 }
859}
860
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000861/// CollectClassPropertyImplementations - This routine collects list of
862/// properties to be implemented in the class. This includes, class's
863/// and its conforming protocols' properties.
864static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
865 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
866 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
867 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
868 E = IDecl->prop_end(); P != E; ++P) {
869 ObjCPropertyDecl *Prop = (*P);
870 PropMap[Prop->getIdentifier()] = Prop;
871 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000872 for (ObjCInterfaceDecl::all_protocol_iterator
873 PI = IDecl->all_referenced_protocol_begin(),
874 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000875 CollectClassPropertyImplementations((*PI), PropMap);
876 }
877 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
878 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
879 E = PDecl->prop_end(); P != E; ++P) {
880 ObjCPropertyDecl *Prop = (*P);
881 PropMap[Prop->getIdentifier()] = Prop;
882 }
883 // scan through protocol's protocols.
884 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
885 E = PDecl->protocol_end(); PI != E; ++PI)
886 CollectClassPropertyImplementations((*PI), PropMap);
887 }
888}
889
890/// CollectSuperClassPropertyImplementations - This routine collects list of
891/// properties to be implemented in super class(s) and also coming from their
892/// conforming protocols.
893static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
894 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
895 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
896 while (SDecl) {
897 CollectClassPropertyImplementations(SDecl, PropMap);
898 SDecl = SDecl->getSuperClass();
899 }
900 }
901}
902
Ted Kremenek9d64c152010-03-12 00:38:38 +0000903/// LookupPropertyDecl - Looks up a property in the current class and all
904/// its protocols.
905ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
906 IdentifierInfo *II) {
907 if (const ObjCInterfaceDecl *IDecl =
908 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
909 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
910 E = IDecl->prop_end(); P != E; ++P) {
911 ObjCPropertyDecl *Prop = (*P);
912 if (Prop->getIdentifier() == II)
913 return Prop;
914 }
915 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000916 for (ObjCInterfaceDecl::all_protocol_iterator
917 PI = IDecl->all_referenced_protocol_begin(),
918 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000919 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
920 if (Prop)
921 return Prop;
922 }
923 }
924 else if (const ObjCProtocolDecl *PDecl =
925 dyn_cast<ObjCProtocolDecl>(CDecl)) {
926 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
927 E = PDecl->prop_end(); P != E; ++P) {
928 ObjCPropertyDecl *Prop = (*P);
929 if (Prop->getIdentifier() == II)
930 return Prop;
931 }
932 // scan through protocol's protocols.
933 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
934 E = PDecl->protocol_end(); PI != E; ++PI) {
935 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
936 if (Prop)
937 return Prop;
938 }
939 }
940 return 0;
941}
942
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000943/// DefaultSynthesizeProperties - This routine default synthesizes all
944/// properties which must be synthesized in class's @implementation.
945void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
946 ObjCInterfaceDecl *IDecl) {
947
948 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
949 CollectClassPropertyImplementations(IDecl, PropMap);
950 if (PropMap.empty())
951 return;
952 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
953 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
954
955 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
956 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
957 ObjCPropertyDecl *Prop = P->second;
958 // If property to be implemented in the super class, ignore.
959 if (SuperPropMap[Prop->getIdentifier()])
960 continue;
961 // Is there a matching propery synthesize/dynamic?
962 if (Prop->isInvalidDecl() ||
963 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
964 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
965 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000966 // Property may have been synthesized by user.
967 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
968 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +0000969 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
970 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
971 continue;
972 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
973 continue;
974 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000975
Ted Kremenek2a6af6b2010-09-24 01:23:01 +0000976
977 // We use invalid SourceLocations for the synthesized ivars since they
978 // aren't really synthesized at a particular location; they just exist.
979 // Saying that they are located at the @implementation isn't really going
980 // to help users.
981 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
982 true,IMPDecl,
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000983 Prop->getIdentifier(), Prop->getIdentifier());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +0000984 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000985}
Ted Kremenek9d64c152010-03-12 00:38:38 +0000986
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000987void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +0000988 ObjCContainerDecl *CDecl,
989 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000990 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
991 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
992 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
993
Ted Kremenek9d64c152010-03-12 00:38:38 +0000994 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000995 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000996 if (PropMap.empty())
997 return;
998
999 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1000 for (ObjCImplDecl::propimpl_iterator
1001 I = IMPDecl->propimpl_begin(),
1002 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1003 PropImplMap.insert((*I)->getPropertyDecl());
1004
1005 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1006 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1007 ObjCPropertyDecl *Prop = P->second;
1008 // Is there a matching propery synthesize/dynamic?
1009 if (Prop->isInvalidDecl() ||
1010 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1011 PropImplMap.count(Prop))
1012 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001013 if (!InsMap.count(Prop->getGetterName())) {
1014 Diag(Prop->getLocation(),
1015 isa<ObjCCategoryDecl>(CDecl) ?
1016 diag::warn_setter_getter_impl_required_in_category :
1017 diag::warn_setter_getter_impl_required)
1018 << Prop->getDeclName() << Prop->getGetterName();
1019 Diag(IMPDecl->getLocation(),
1020 diag::note_property_impl_required);
1021 }
1022
1023 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1024 Diag(Prop->getLocation(),
1025 isa<ObjCCategoryDecl>(CDecl) ?
1026 diag::warn_setter_getter_impl_required_in_category :
1027 diag::warn_setter_getter_impl_required)
1028 << Prop->getDeclName() << Prop->getSetterName();
1029 Diag(IMPDecl->getLocation(),
1030 diag::note_property_impl_required);
1031 }
1032 }
1033}
1034
1035void
1036Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1037 ObjCContainerDecl* IDecl) {
1038 // Rules apply in non-GC mode only
1039 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1040 return;
1041 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1042 E = IDecl->prop_end();
1043 I != E; ++I) {
1044 ObjCPropertyDecl *Property = (*I);
1045 unsigned Attributes = Property->getPropertyAttributes();
1046 // We only care about readwrite atomic property.
1047 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1048 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1049 continue;
1050 if (const ObjCPropertyImplDecl *PIDecl
1051 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1052 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1053 continue;
1054 ObjCMethodDecl *GetterMethod =
1055 IMPDecl->getInstanceMethod(Property->getGetterName());
1056 ObjCMethodDecl *SetterMethod =
1057 IMPDecl->getInstanceMethod(Property->getSetterName());
1058 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1059 SourceLocation MethodLoc =
1060 (GetterMethod ? GetterMethod->getLocation()
1061 : SetterMethod->getLocation());
1062 Diag(MethodLoc, diag::warn_atomic_property_rule)
1063 << Property->getIdentifier();
1064 Diag(Property->getLocation(), diag::note_property_declare);
1065 }
1066 }
1067 }
1068}
1069
1070/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1071/// have the property type and issue diagnostics if they don't.
1072/// Also synthesize a getter/setter method if none exist (and update the
1073/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1074/// methods is the "right" thing to do.
1075void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001076 ObjCContainerDecl *CD,
1077 ObjCPropertyDecl *redeclaredProperty,
1078 ObjCContainerDecl *lexicalDC) {
1079
Ted Kremenek9d64c152010-03-12 00:38:38 +00001080 ObjCMethodDecl *GetterMethod, *SetterMethod;
1081
1082 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1083 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1084 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1085 property->getLocation());
1086
1087 if (SetterMethod) {
1088 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1089 property->getPropertyAttributes();
1090 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1091 Context.getCanonicalType(SetterMethod->getResultType()) !=
1092 Context.VoidTy)
1093 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1094 if (SetterMethod->param_size() != 1 ||
1095 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1096 Diag(property->getLocation(),
1097 diag::warn_accessor_property_type_mismatch)
1098 << property->getDeclName()
1099 << SetterMethod->getSelector();
1100 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1101 }
1102 }
1103
1104 // Synthesize getter/setter methods if none exist.
1105 // Find the default getter and if one not found, add one.
1106 // FIXME: The synthesized property we set here is misleading. We almost always
1107 // synthesize these methods unless the user explicitly provided prototypes
1108 // (which is odd, but allowed). Sema should be typechecking that the
1109 // declarations jive in that situation (which it is not currently).
1110 if (!GetterMethod) {
1111 // No instance method of same name as property getter name was found.
1112 // Declare a getter method and add it to the list of methods
1113 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001114 SourceLocation Loc = redeclaredProperty ?
1115 redeclaredProperty->getLocation() :
1116 property->getLocation();
1117
1118 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1119 property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001120 property->getType(), 0, CD, true, false, true,
1121 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001122 (property->getPropertyImplementation() ==
1123 ObjCPropertyDecl::Optional) ?
1124 ObjCMethodDecl::Optional :
1125 ObjCMethodDecl::Required);
1126 CD->addDecl(GetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001127 // FIXME: Eventually this shouldn't be needed, as the lexical context
1128 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001129 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001130 GetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001131 } else
1132 // A user declared getter will be synthesize when @synthesize of
1133 // the property with the same name is seen in the @implementation
1134 GetterMethod->setSynthesized(true);
1135 property->setGetterMethodDecl(GetterMethod);
1136
1137 // Skip setter if property is read-only.
1138 if (!property->isReadOnly()) {
1139 // Find the default setter and if one not found, add one.
1140 if (!SetterMethod) {
1141 // No instance method of same name as property setter name was found.
1142 // Declare a setter method and add it to the list of methods
1143 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001144 SourceLocation Loc = redeclaredProperty ?
1145 redeclaredProperty->getLocation() :
1146 property->getLocation();
1147
1148 SetterMethod =
1149 ObjCMethodDecl::Create(Context, Loc, Loc,
1150 property->getSetterName(), Context.VoidTy, 0,
1151 CD, true, false, true, false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001152 (property->getPropertyImplementation() ==
1153 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001154 ObjCMethodDecl::Optional :
1155 ObjCMethodDecl::Required);
1156
Ted Kremenek9d64c152010-03-12 00:38:38 +00001157 // Invent the arguments for the setter. We don't bother making a
1158 // nice name for the argument.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001159 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001160 property->getIdentifier(),
1161 property->getType(),
1162 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001163 SC_None,
1164 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001165 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001166 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001167 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001168 // FIXME: Eventually this shouldn't be needed, as the lexical context
1169 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001170 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001171 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001172 } else
1173 // A user declared setter will be synthesize when @synthesize of
1174 // the property with the same name is seen in the @implementation
1175 SetterMethod->setSynthesized(true);
1176 property->setSetterMethodDecl(SetterMethod);
1177 }
1178 // Add any synthesized methods to the global pool. This allows us to
1179 // handle the following, which is supported by GCC (and part of the design).
1180 //
1181 // @interface Foo
1182 // @property double bar;
1183 // @end
1184 //
1185 // void thisIsUnfortunate() {
1186 // id foo;
1187 // double bar = [foo bar];
1188 // }
1189 //
1190 if (GetterMethod)
1191 AddInstanceMethodToGlobalPool(GetterMethod);
1192 if (SetterMethod)
1193 AddInstanceMethodToGlobalPool(SetterMethod);
1194}
1195
John McCalld226f652010-08-21 09:40:31 +00001196void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001197 SourceLocation Loc,
1198 unsigned &Attributes) {
1199 // FIXME: Improve the reported location.
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001200 if (!PDecl)
1201 return;
1202
1203 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001204 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001205
1206 // readonly and readwrite/assign/retain/copy conflict.
1207 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1208 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1209 ObjCDeclSpec::DQ_PR_assign |
1210 ObjCDeclSpec::DQ_PR_copy |
1211 ObjCDeclSpec::DQ_PR_retain))) {
1212 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1213 "readwrite" :
1214 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1215 "assign" :
1216 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1217 "copy" : "retain";
1218
1219 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1220 diag::err_objc_property_attr_mutually_exclusive :
1221 diag::warn_objc_property_attr_mutually_exclusive)
1222 << "readonly" << which;
1223 }
1224
1225 // Check for copy or retain on non-object types.
1226 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1227 !PropertyTy->isObjCObjectPointerType() &&
1228 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001229 !Context.isObjCNSObjectType(PropertyTy) &&
1230 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001231 Diag(Loc, diag::err_objc_property_requires_object)
1232 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1233 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1234 }
1235
1236 // Check for more than one of { assign, copy, retain }.
1237 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1238 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1239 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1240 << "assign" << "copy";
1241 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1242 }
1243 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1244 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1245 << "assign" << "retain";
1246 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1247 }
1248 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1249 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1250 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1251 << "copy" << "retain";
1252 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1253 }
1254 }
1255
1256 // Warn if user supplied no assignment attribute, property is
1257 // readwrite, and this is an object type.
1258 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1259 ObjCDeclSpec::DQ_PR_retain)) &&
1260 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1261 PropertyTy->isObjCObjectPointerType()) {
1262 // Skip this warning in gc-only mode.
1263 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1264 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1265
1266 // If non-gc code warn that this is likely inappropriate.
1267 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1268 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1269
1270 // FIXME: Implement warning dependent on NSCopying being
1271 // implemented. See also:
1272 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1273 // (please trim this list while you are at it).
1274 }
1275
1276 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1277 && getLangOptions().getGCMode() == LangOptions::GCOnly
1278 && PropertyTy->isBlockPointerType())
1279 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1280}