blob: e1c0f4af99e928dfa4af82d5292deada213cc729 [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();
Fariborz Jahanian2a289142010-11-10 18:01:36 +000096 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
97
98 if (CCPrimary)
99 // Check for duplicate declaration of this property in current and
100 // other class extensions.
101 for (const ObjCCategoryDecl *ClsExtDecl =
102 CCPrimary->getFirstClassExtension();
103 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
104 if (ObjCPropertyDecl *prevDecl =
105 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
106 Diag(AtLoc, diag::err_duplicate_property);
107 Diag(prevDecl->getLocation(), diag::note_property_declare);
108 return 0;
109 }
110 }
111
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000112 // Create a new ObjCPropertyDecl with the DeclContext being
113 // the class extension.
114 ObjCPropertyDecl *PDecl =
115 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
116 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000117 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
118 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
119 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
120 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
121
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000122 DC->addDecl(PDecl);
123
124 // We need to look in the @interface to see if the @property was
125 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000126 if (!CCPrimary) {
127 Diag(CDecl->getLocation(), diag::err_continuation_class);
128 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000129 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000130 }
131
132 // Find the property in continuation class's primary class only.
133 ObjCPropertyDecl *PIDecl =
134 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
135
136 if (!PIDecl) {
137 // No matching property found in the primary class. Just fall thru
138 // and add property to continuation class's primary class.
139 ObjCPropertyDecl *PDecl =
140 CreatePropertyDecl(S, CCPrimary, AtLoc,
141 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000142 Attributes, T, MethodImplKind, DC);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000143 // Mark written attribute as having no attribute because
144 // this is not a user-written property declaration in primary
145 // class.
146 PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000147
148 // A case of continuation class adding a new property in the class. This
149 // is not what it was meant for. However, gcc supports it and so should we.
150 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000151 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
152 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000153 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000154 }
155
156 // The property 'PIDecl's readonly attribute will be over-ridden
157 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000158 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000159 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
160 unsigned retainCopyNonatomic =
161 (ObjCPropertyDecl::OBJC_PR_retain |
162 ObjCPropertyDecl::OBJC_PR_copy |
163 ObjCPropertyDecl::OBJC_PR_nonatomic);
164 if ((Attributes & retainCopyNonatomic) !=
165 (PIkind & retainCopyNonatomic)) {
166 Diag(AtLoc, diag::warn_property_attr_mismatch);
167 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000168 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000169 DeclContext *DC = cast<DeclContext>(CCPrimary);
170 if (!ObjCPropertyDecl::findPropertyDecl(DC,
171 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000172 // Protocol is not in the primary class. Must build one for it.
173 ObjCDeclSpec ProtocolPropertyODS;
174 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
175 // and ObjCPropertyDecl::PropertyAttributeKind have identical
176 // values. Should consolidate both into one enum type.
177 ProtocolPropertyODS.
178 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
179 PIkind);
180
John McCalld226f652010-08-21 09:40:31 +0000181 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000182 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
183 PIDecl->getGetterName(),
184 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000185 CCPrimary, isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000186 MethodImplKind,
187 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000188 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000189 }
190 PIDecl->makeitReadWriteAttribute();
191 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
192 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
193 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
194 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
195 PIDecl->setSetterName(SetterSel);
196 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000197 // Tailor the diagnostics for the common case where a readwrite
198 // property is declared both in the @interface and the continuation.
199 // This is a common error where the user often intended the original
200 // declaration to be readonly.
201 unsigned diag =
202 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
203 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
204 ? diag::err_use_continuation_class_redeclaration_readwrite
205 : diag::err_use_continuation_class;
206 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000207 << CCPrimary->getDeclName();
208 Diag(PIDecl->getLocation(), diag::note_property_declare);
209 }
210 *isOverridingProperty = true;
211 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000212 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000213 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000214}
215
216ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
217 ObjCContainerDecl *CDecl,
218 SourceLocation AtLoc,
219 FieldDeclarator &FD,
220 Selector GetterSel,
221 Selector SetterSel,
222 const bool isAssign,
223 const bool isReadWrite,
224 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000225 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000226 tok::ObjCKeywordKind MethodImplKind,
227 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000228 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000229 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000230
231 // Issue a warning if property is 'assign' as default and its object, which is
232 // gc'able conforms to NSCopying protocol
233 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
234 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000235 if (const ObjCObjectPointerType *ObjPtrTy =
236 T->getAs<ObjCObjectPointerType>()) {
237 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
238 if (IDecl)
239 if (ObjCProtocolDecl* PNSCopying =
240 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
241 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
242 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000243 }
John McCallc12c5bb2010-05-15 11:32:37 +0000244 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000245 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
246
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000247 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000248 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
249 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000250 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000251
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000252 if (ObjCPropertyDecl *prevDecl =
253 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000254 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000255 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000256 PDecl->setInvalidDecl();
257 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000258 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000259 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000260 if (lexicalDC)
261 PDecl->setLexicalDeclContext(lexicalDC);
262 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000263
264 if (T->isArrayType() || T->isFunctionType()) {
265 Diag(AtLoc, diag::err_property_type) << T;
266 PDecl->setInvalidDecl();
267 }
268
269 ProcessDeclAttributes(S, PDecl, FD.D);
270
271 // Regardless of setter/getter attribute, we save the default getter/setter
272 // selector names in anticipation of declaration of setter/getter methods.
273 PDecl->setGetterName(GetterSel);
274 PDecl->setSetterName(SetterSel);
275
276 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
277 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
278
279 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
280 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
281
282 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
283 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
284
285 if (isReadWrite)
286 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
287
288 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
289 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
290
291 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
292 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
293
294 if (isAssign)
295 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
296
297 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
298 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
299
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000300 PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes());
301
Ted Kremenek28685ab2010-03-12 00:46:40 +0000302 if (MethodImplKind == tok::objc_required)
303 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
304 else if (MethodImplKind == tok::objc_optional)
305 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000306
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000307 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000308}
309
310
311/// ActOnPropertyImplDecl - This routine performs semantic checks and
312/// builds the AST node for a property implementation declaration; declared
313/// as @synthesize or @dynamic.
314///
John McCalld226f652010-08-21 09:40:31 +0000315Decl *Sema::ActOnPropertyImplDecl(Scope *S,
316 SourceLocation AtLoc,
317 SourceLocation PropertyLoc,
318 bool Synthesize,
319 Decl *ClassCatImpDecl,
320 IdentifierInfo *PropertyId,
321 IdentifierInfo *PropertyIvar) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000322 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000323 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000324 // Make sure we have a context for the property implementation declaration.
325 if (!ClassImpDecl) {
326 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000327 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000328 }
329 ObjCPropertyDecl *property = 0;
330 ObjCInterfaceDecl* IDecl = 0;
331 // Find the class or category class where this property must have
332 // a declaration.
333 ObjCImplementationDecl *IC = 0;
334 ObjCCategoryImplDecl* CatImplClass = 0;
335 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
336 IDecl = IC->getClassInterface();
337 // We always synthesize an interface for an implementation
338 // without an interface decl. So, IDecl is always non-zero.
339 assert(IDecl &&
340 "ActOnPropertyImplDecl - @implementation without @interface");
341
342 // Look for this property declaration in the @implementation's @interface
343 property = IDecl->FindPropertyDeclaration(PropertyId);
344 if (!property) {
345 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000346 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000347 }
348 if (const ObjCCategoryDecl *CD =
349 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
350 if (!CD->IsClassExtension()) {
351 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
352 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000353 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000354 }
355 }
356 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
357 if (Synthesize) {
358 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000359 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000360 }
361 IDecl = CatImplClass->getClassInterface();
362 if (!IDecl) {
363 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000364 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000365 }
366 ObjCCategoryDecl *Category =
367 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
368
369 // If category for this implementation not found, it is an error which
370 // has already been reported eralier.
371 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000372 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000373 // Look for this property declaration in @implementation's category
374 property = Category->FindPropertyDeclaration(PropertyId);
375 if (!property) {
376 Diag(PropertyLoc, diag::error_bad_category_property_decl)
377 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000378 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000379 }
380 } else {
381 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000382 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000383 }
384 ObjCIvarDecl *Ivar = 0;
385 // Check that we have a valid, previously declared ivar for @synthesize
386 if (Synthesize) {
387 // @synthesize
388 if (!PropertyIvar)
389 PropertyIvar = PropertyId;
390 QualType PropType = Context.getCanonicalType(property->getType());
391 // Check that this is a previously declared 'ivar' in 'IDecl' interface
392 ObjCInterfaceDecl *ClassDeclared;
393 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
394 if (!Ivar) {
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000395 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000396 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian2846b2b2010-04-06 23:36:17 +0000397 ObjCIvarDecl::Protected,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000398 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000399 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000400 IDecl->makeDeclVisibleInContext(Ivar, false);
401 property->setPropertyIvarDecl(Ivar);
402
403 if (!getLangOptions().ObjCNonFragileABI)
404 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
405 // Note! I deliberately want it to fall thru so, we have a
406 // a property implementation and to avoid future warnings.
407 } else if (getLangOptions().ObjCNonFragileABI &&
408 ClassDeclared != IDecl) {
409 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
410 << property->getDeclName() << Ivar->getDeclName()
411 << ClassDeclared->getDeclName();
412 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000413 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000414 // Note! I deliberately want it to fall thru so more errors are caught.
415 }
416 QualType IvarType = Context.getCanonicalType(Ivar->getType());
417
418 // Check that type of property and its ivar are type compatible.
419 if (PropType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000420 bool compat = false;
421 if (isa<ObjCObjectPointerType>(PropType)
422 && isa<ObjCObjectPointerType>(IvarType))
423 compat =
424 Context.canAssignObjCInterfaces(
425 PropType->getAs<ObjCObjectPointerType>(),
426 IvarType->getAs<ObjCObjectPointerType>());
427 else
428 compat = (CheckAssignmentConstraints(PropType, IvarType) == Compatible);
429 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000430 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000431 << property->getDeclName() << PropType
432 << Ivar->getDeclName() << IvarType;
433 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000434 // Note! I deliberately want it to fall thru so, we have a
435 // a property implementation and to avoid future warnings.
436 }
437
438 // FIXME! Rules for properties are somewhat different that those
439 // for assignments. Use a new routine to consolidate all cases;
440 // specifically for property redeclarations as well as for ivars.
441 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
442 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
443 if (lhsType != rhsType &&
444 lhsType->isArithmeticType()) {
445 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000446 << property->getDeclName() << PropType
447 << Ivar->getDeclName() << IvarType;
448 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000449 // Fall thru - see previous comment
450 }
451 // __weak is explicit. So it works on Canonical type.
452 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
453 getLangOptions().getGCMode() != LangOptions::NonGC) {
454 Diag(PropertyLoc, diag::error_weak_property)
455 << property->getDeclName() << Ivar->getDeclName();
456 // Fall thru - see previous comment
457 }
458 if ((property->getType()->isObjCObjectPointerType() ||
459 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
460 getLangOptions().getGCMode() != LangOptions::NonGC) {
461 Diag(PropertyLoc, diag::error_strong_property)
462 << property->getDeclName() << Ivar->getDeclName();
463 // Fall thru - see previous comment
464 }
465 }
466 } else if (PropertyIvar)
467 // @dynamic
468 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
469 assert (property && "ActOnPropertyImplDecl - property declaration missing");
470 ObjCPropertyImplDecl *PIDecl =
471 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
472 property,
473 (Synthesize ?
474 ObjCPropertyImplDecl::Synthesize
475 : ObjCPropertyImplDecl::Dynamic),
476 Ivar);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000477 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
478 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000479 if (getLangOptions().CPlusPlus && Synthesize &&
480 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000481 // For Objective-C++, need to synthesize the AST for the IVAR object to be
482 // returned by the getter as it must conform to C++'s copy-return rules.
483 // FIXME. Eventually we want to do this for Objective-C as well.
484 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
485 DeclRefExpr *SelfExpr =
486 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
487 SourceLocation());
488 Expr *IvarRefExpr =
489 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
490 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000491 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000492 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000493 SourceLocation(),
494 getterMethod->getResultType(),
495 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000496 SourceLocation(),
497 Owned(IvarRefExpr));
498 if (!Res.isInvalid()) {
499 Expr *ResExpr = Res.takeAs<Expr>();
500 if (ResExpr)
501 ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr);
502 PIDecl->setGetterCXXConstructor(ResExpr);
503 }
504 }
505 }
506 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
507 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000508 if (getLangOptions().CPlusPlus && Synthesize
509 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000510 // FIXME. Eventually we want to do this for Objective-C as well.
511 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
512 DeclRefExpr *SelfExpr =
513 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
514 SourceLocation());
515 Expr *lhs =
516 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
517 SelfExpr, true, true);
518 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
519 ParmVarDecl *Param = (*P);
520 Expr *rhs = new (Context) DeclRefExpr(Param,Param->getType(),
521 SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000522 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000523 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000524 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
525 }
526 }
527
Ted Kremenek28685ab2010-03-12 00:46:40 +0000528 if (IC) {
529 if (Synthesize)
530 if (ObjCPropertyImplDecl *PPIDecl =
531 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
532 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
533 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
534 << PropertyIvar;
535 Diag(PPIDecl->getLocation(), diag::note_previous_use);
536 }
537
538 if (ObjCPropertyImplDecl *PPIDecl
539 = IC->FindPropertyImplDecl(PropertyId)) {
540 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
541 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000542 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000543 }
544 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000545 if (getLangOptions().ObjCNonFragileABI2) {
546 // Diagnose if an ivar was lazily synthesdized due to a previous
547 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000548 // but it requires an ivar of different name.
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000549 ObjCInterfaceDecl *ClassDeclared;
550 ObjCIvarDecl *Ivar = 0;
551 if (!Synthesize)
552 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
553 else {
554 if (PropertyIvar && PropertyIvar != PropertyId)
555 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
556 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000557 // Issue diagnostics only if Ivar belongs to current class.
558 if (Ivar && Ivar->getSynthesize() &&
559 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000560 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
561 << PropertyId;
562 Ivar->setInvalidDecl();
563 }
564 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000565 } else {
566 if (Synthesize)
567 if (ObjCPropertyImplDecl *PPIDecl =
568 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
569 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
570 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
571 << PropertyIvar;
572 Diag(PPIDecl->getLocation(), diag::note_previous_use);
573 }
574
575 if (ObjCPropertyImplDecl *PPIDecl =
576 CatImplClass->FindPropertyImplDecl(PropertyId)) {
577 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
578 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000579 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000580 }
581 CatImplClass->addPropertyImplementation(PIDecl);
582 }
583
John McCalld226f652010-08-21 09:40:31 +0000584 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000585}
586
587//===----------------------------------------------------------------------===//
588// Helper methods.
589//===----------------------------------------------------------------------===//
590
Ted Kremenek9d64c152010-03-12 00:38:38 +0000591/// DiagnosePropertyMismatch - Compares two properties for their
592/// attributes and types and warns on a variety of inconsistencies.
593///
594void
595Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
596 ObjCPropertyDecl *SuperProperty,
597 const IdentifierInfo *inheritedName) {
598 ObjCPropertyDecl::PropertyAttributeKind CAttr =
599 Property->getPropertyAttributes();
600 ObjCPropertyDecl::PropertyAttributeKind SAttr =
601 SuperProperty->getPropertyAttributes();
602 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
603 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
604 Diag(Property->getLocation(), diag::warn_readonly_property)
605 << Property->getDeclName() << inheritedName;
606 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
607 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
608 Diag(Property->getLocation(), diag::warn_property_attribute)
609 << Property->getDeclName() << "copy" << inheritedName;
610 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
611 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
612 Diag(Property->getLocation(), diag::warn_property_attribute)
613 << Property->getDeclName() << "retain" << inheritedName;
614
615 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
616 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
617 Diag(Property->getLocation(), diag::warn_property_attribute)
618 << Property->getDeclName() << "atomic" << inheritedName;
619 if (Property->getSetterName() != SuperProperty->getSetterName())
620 Diag(Property->getLocation(), diag::warn_property_attribute)
621 << Property->getDeclName() << "setter" << inheritedName;
622 if (Property->getGetterName() != SuperProperty->getGetterName())
623 Diag(Property->getLocation(), diag::warn_property_attribute)
624 << Property->getDeclName() << "getter" << inheritedName;
625
626 QualType LHSType =
627 Context.getCanonicalType(SuperProperty->getType());
628 QualType RHSType =
629 Context.getCanonicalType(Property->getType());
630
631 if (!Context.typesAreCompatible(LHSType, RHSType)) {
632 // FIXME: Incorporate this test with typesAreCompatible.
633 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
634 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
635 return;
636 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
637 << Property->getType() << SuperProperty->getType() << inheritedName;
638 }
639}
640
641bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
642 ObjCMethodDecl *GetterMethod,
643 SourceLocation Loc) {
644 if (GetterMethod &&
645 GetterMethod->getResultType() != property->getType()) {
646 AssignConvertType result = Incompatible;
647 if (property->getType()->isObjCObjectPointerType())
648 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
649 property->getType());
650 if (result != Compatible) {
651 Diag(Loc, diag::warn_accessor_property_type_mismatch)
652 << property->getDeclName()
653 << GetterMethod->getSelector();
654 Diag(GetterMethod->getLocation(), diag::note_declared_at);
655 return true;
656 }
657 }
658 return false;
659}
660
661/// ComparePropertiesInBaseAndSuper - This routine compares property
662/// declarations in base and its super class, if any, and issues
663/// diagnostics in a variety of inconsistant situations.
664///
665void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
666 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
667 if (!SDecl)
668 return;
669 // FIXME: O(N^2)
670 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
671 E = SDecl->prop_end(); S != E; ++S) {
672 ObjCPropertyDecl *SuperPDecl = (*S);
673 // Does property in super class has declaration in current class?
674 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
675 E = IDecl->prop_end(); I != E; ++I) {
676 ObjCPropertyDecl *PDecl = (*I);
677 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
678 DiagnosePropertyMismatch(PDecl, SuperPDecl,
679 SDecl->getIdentifier());
680 }
681 }
682}
683
684/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
685/// of properties declared in a protocol and compares their attribute against
686/// the same property declared in the class or category.
687void
688Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
689 ObjCProtocolDecl *PDecl) {
690 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
691 if (!IDecl) {
692 // Category
693 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
694 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
695 if (!CatDecl->IsClassExtension())
696 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
697 E = PDecl->prop_end(); P != E; ++P) {
698 ObjCPropertyDecl *Pr = (*P);
699 ObjCCategoryDecl::prop_iterator CP, CE;
700 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000701 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000702 if ((*CP)->getIdentifier() == Pr->getIdentifier())
703 break;
704 if (CP != CE)
705 // Property protocol already exist in class. Diagnose any mismatch.
706 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
707 }
708 return;
709 }
710 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
711 E = PDecl->prop_end(); P != E; ++P) {
712 ObjCPropertyDecl *Pr = (*P);
713 ObjCInterfaceDecl::prop_iterator CP, CE;
714 // Is this property already in class's list of properties?
715 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
716 if ((*CP)->getIdentifier() == Pr->getIdentifier())
717 break;
718 if (CP != CE)
719 // Property protocol already exist in class. Diagnose any mismatch.
720 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
721 }
722}
723
724/// CompareProperties - This routine compares properties
725/// declared in 'ClassOrProtocol' objects (which can be a class or an
726/// inherited protocol with the list of properties for class/category 'CDecl'
727///
John McCalld226f652010-08-21 09:40:31 +0000728void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
729 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000730 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
731
732 if (!IDecl) {
733 // Category
734 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
735 assert (CatDecl && "CompareProperties");
736 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
737 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
738 E = MDecl->protocol_end(); P != E; ++P)
739 // Match properties of category with those of protocol (*P)
740 MatchOneProtocolPropertiesInClass(CatDecl, *P);
741
742 // Go thru the list of protocols for this category and recursively match
743 // their properties with those in the category.
744 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
745 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000746 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000747 } else {
748 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
749 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
750 E = MD->protocol_end(); P != E; ++P)
751 MatchOneProtocolPropertiesInClass(CatDecl, *P);
752 }
753 return;
754 }
755
756 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000757 for (ObjCInterfaceDecl::all_protocol_iterator
758 P = MDecl->all_referenced_protocol_begin(),
759 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000760 // Match properties of class IDecl with those of protocol (*P).
761 MatchOneProtocolPropertiesInClass(IDecl, *P);
762
763 // Go thru the list of protocols for this class and recursively match
764 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +0000765 for (ObjCInterfaceDecl::all_protocol_iterator
766 P = IDecl->all_referenced_protocol_begin(),
767 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000768 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000769 } else {
770 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
771 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
772 E = MD->protocol_end(); P != E; ++P)
773 MatchOneProtocolPropertiesInClass(IDecl, *P);
774 }
775}
776
777/// isPropertyReadonly - Return true if property is readonly, by searching
778/// for the property in the class and in its categories and implementations
779///
780bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
781 ObjCInterfaceDecl *IDecl) {
782 // by far the most common case.
783 if (!PDecl->isReadOnly())
784 return false;
785 // Even if property is ready only, if interface has a user defined setter,
786 // it is not considered read only.
787 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
788 return false;
789
790 // Main class has the property as 'readonly'. Must search
791 // through the category list to see if the property's
792 // attribute has been over-ridden to 'readwrite'.
793 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
794 Category; Category = Category->getNextClassCategory()) {
795 // Even if property is ready only, if a category has a user defined setter,
796 // it is not considered read only.
797 if (Category->getInstanceMethod(PDecl->getSetterName()))
798 return false;
799 ObjCPropertyDecl *P =
800 Category->FindPropertyDeclaration(PDecl->getIdentifier());
801 if (P && !P->isReadOnly())
802 return false;
803 }
804
805 // Also, check for definition of a setter method in the implementation if
806 // all else failed.
807 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
808 if (ObjCImplementationDecl *IMD =
809 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
810 if (IMD->getInstanceMethod(PDecl->getSetterName()))
811 return false;
812 } else if (ObjCCategoryImplDecl *CIMD =
813 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
814 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
815 return false;
816 }
817 }
818 // Lastly, look through the implementation (if one is in scope).
819 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
820 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
821 return false;
822 // If all fails, look at the super class.
823 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
824 return isPropertyReadonly(PDecl, SIDecl);
825 return true;
826}
827
828/// CollectImmediateProperties - This routine collects all properties in
829/// the class and its conforming protocols; but not those it its super class.
830void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000831 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
832 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000833 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
834 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
835 E = IDecl->prop_end(); P != E; ++P) {
836 ObjCPropertyDecl *Prop = (*P);
837 PropMap[Prop->getIdentifier()] = Prop;
838 }
839 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000840 for (ObjCInterfaceDecl::all_protocol_iterator
841 PI = IDecl->all_referenced_protocol_begin(),
842 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000843 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000844 }
845 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
846 if (!CATDecl->IsClassExtension())
847 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
848 E = CATDecl->prop_end(); P != E; ++P) {
849 ObjCPropertyDecl *Prop = (*P);
850 PropMap[Prop->getIdentifier()] = Prop;
851 }
852 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000853 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +0000854 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000855 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000856 }
857 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
858 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
859 E = PDecl->prop_end(); P != E; ++P) {
860 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000861 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
862 // Exclude property for protocols which conform to class's super-class,
863 // as super-class has to implement the property.
864 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
865 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
866 if (!PropEntry)
867 PropEntry = Prop;
868 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000869 }
870 // scan through protocol's protocols.
871 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
872 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000873 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000874 }
875}
876
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000877/// CollectClassPropertyImplementations - This routine collects list of
878/// properties to be implemented in the class. This includes, class's
879/// and its conforming protocols' properties.
880static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
881 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
882 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
883 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
884 E = IDecl->prop_end(); P != E; ++P) {
885 ObjCPropertyDecl *Prop = (*P);
886 PropMap[Prop->getIdentifier()] = Prop;
887 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000888 for (ObjCInterfaceDecl::all_protocol_iterator
889 PI = IDecl->all_referenced_protocol_begin(),
890 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000891 CollectClassPropertyImplementations((*PI), PropMap);
892 }
893 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
894 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
895 E = PDecl->prop_end(); P != E; ++P) {
896 ObjCPropertyDecl *Prop = (*P);
897 PropMap[Prop->getIdentifier()] = Prop;
898 }
899 // scan through protocol's protocols.
900 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
901 E = PDecl->protocol_end(); PI != E; ++PI)
902 CollectClassPropertyImplementations((*PI), PropMap);
903 }
904}
905
906/// CollectSuperClassPropertyImplementations - This routine collects list of
907/// properties to be implemented in super class(s) and also coming from their
908/// conforming protocols.
909static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
910 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
911 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
912 while (SDecl) {
913 CollectClassPropertyImplementations(SDecl, PropMap);
914 SDecl = SDecl->getSuperClass();
915 }
916 }
917}
918
Ted Kremenek9d64c152010-03-12 00:38:38 +0000919/// LookupPropertyDecl - Looks up a property in the current class and all
920/// its protocols.
921ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
922 IdentifierInfo *II) {
923 if (const ObjCInterfaceDecl *IDecl =
924 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
925 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
926 E = IDecl->prop_end(); P != E; ++P) {
927 ObjCPropertyDecl *Prop = (*P);
928 if (Prop->getIdentifier() == II)
929 return Prop;
930 }
931 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000932 for (ObjCInterfaceDecl::all_protocol_iterator
933 PI = IDecl->all_referenced_protocol_begin(),
934 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000935 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
936 if (Prop)
937 return Prop;
938 }
939 }
940 else if (const ObjCProtocolDecl *PDecl =
941 dyn_cast<ObjCProtocolDecl>(CDecl)) {
942 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
943 E = PDecl->prop_end(); P != E; ++P) {
944 ObjCPropertyDecl *Prop = (*P);
945 if (Prop->getIdentifier() == II)
946 return Prop;
947 }
948 // scan through protocol's protocols.
949 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
950 E = PDecl->protocol_end(); PI != E; ++PI) {
951 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
952 if (Prop)
953 return Prop;
954 }
955 }
956 return 0;
957}
958
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000959/// DefaultSynthesizeProperties - This routine default synthesizes all
960/// properties which must be synthesized in class's @implementation.
961void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
962 ObjCInterfaceDecl *IDecl) {
963
964 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
965 CollectClassPropertyImplementations(IDecl, PropMap);
966 if (PropMap.empty())
967 return;
968 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
969 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
970
971 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
972 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
973 ObjCPropertyDecl *Prop = P->second;
974 // If property to be implemented in the super class, ignore.
975 if (SuperPropMap[Prop->getIdentifier()])
976 continue;
977 // Is there a matching propery synthesize/dynamic?
978 if (Prop->isInvalidDecl() ||
979 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
980 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
981 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000982 // Property may have been synthesized by user.
983 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
984 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +0000985 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
986 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
987 continue;
988 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
989 continue;
990 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000991
Ted Kremenek2a6af6b2010-09-24 01:23:01 +0000992
993 // We use invalid SourceLocations for the synthesized ivars since they
994 // aren't really synthesized at a particular location; they just exist.
995 // Saying that they are located at the @implementation isn't really going
996 // to help users.
997 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
998 true,IMPDecl,
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000999 Prop->getIdentifier(), Prop->getIdentifier());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001000 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001001}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001002
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001003void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001004 ObjCContainerDecl *CDecl,
1005 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001006 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1007 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1008 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1009
Ted Kremenek9d64c152010-03-12 00:38:38 +00001010 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001011 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001012 if (PropMap.empty())
1013 return;
1014
1015 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1016 for (ObjCImplDecl::propimpl_iterator
1017 I = IMPDecl->propimpl_begin(),
1018 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1019 PropImplMap.insert((*I)->getPropertyDecl());
1020
1021 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1022 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1023 ObjCPropertyDecl *Prop = P->second;
1024 // Is there a matching propery synthesize/dynamic?
1025 if (Prop->isInvalidDecl() ||
1026 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1027 PropImplMap.count(Prop))
1028 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001029 if (!InsMap.count(Prop->getGetterName())) {
1030 Diag(Prop->getLocation(),
1031 isa<ObjCCategoryDecl>(CDecl) ?
1032 diag::warn_setter_getter_impl_required_in_category :
1033 diag::warn_setter_getter_impl_required)
1034 << Prop->getDeclName() << Prop->getGetterName();
1035 Diag(IMPDecl->getLocation(),
1036 diag::note_property_impl_required);
1037 }
1038
1039 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1040 Diag(Prop->getLocation(),
1041 isa<ObjCCategoryDecl>(CDecl) ?
1042 diag::warn_setter_getter_impl_required_in_category :
1043 diag::warn_setter_getter_impl_required)
1044 << Prop->getDeclName() << Prop->getSetterName();
1045 Diag(IMPDecl->getLocation(),
1046 diag::note_property_impl_required);
1047 }
1048 }
1049}
1050
1051void
1052Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1053 ObjCContainerDecl* IDecl) {
1054 // Rules apply in non-GC mode only
1055 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1056 return;
1057 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1058 E = IDecl->prop_end();
1059 I != E; ++I) {
1060 ObjCPropertyDecl *Property = (*I);
1061 unsigned Attributes = Property->getPropertyAttributes();
1062 // We only care about readwrite atomic property.
1063 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1064 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1065 continue;
1066 if (const ObjCPropertyImplDecl *PIDecl
1067 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1068 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1069 continue;
1070 ObjCMethodDecl *GetterMethod =
1071 IMPDecl->getInstanceMethod(Property->getGetterName());
1072 ObjCMethodDecl *SetterMethod =
1073 IMPDecl->getInstanceMethod(Property->getSetterName());
1074 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1075 SourceLocation MethodLoc =
1076 (GetterMethod ? GetterMethod->getLocation()
1077 : SetterMethod->getLocation());
1078 Diag(MethodLoc, diag::warn_atomic_property_rule)
1079 << Property->getIdentifier();
1080 Diag(Property->getLocation(), diag::note_property_declare);
1081 }
1082 }
1083 }
1084}
1085
John McCall5de74d12010-11-10 07:01:40 +00001086/// AddPropertyAttrs - Propagates attributes from a property to the
1087/// implicitly-declared getter or setter for that property.
1088static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1089 ObjCPropertyDecl *Property) {
1090 // Should we just clone all attributes over?
1091 if (DeprecatedAttr *A = Property->getAttr<DeprecatedAttr>())
1092 PropertyMethod->addAttr(A->clone(S.Context));
1093 if (UnavailableAttr *A = Property->getAttr<UnavailableAttr>())
1094 PropertyMethod->addAttr(A->clone(S.Context));
1095}
1096
Ted Kremenek9d64c152010-03-12 00:38:38 +00001097/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1098/// have the property type and issue diagnostics if they don't.
1099/// Also synthesize a getter/setter method if none exist (and update the
1100/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1101/// methods is the "right" thing to do.
1102void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001103 ObjCContainerDecl *CD,
1104 ObjCPropertyDecl *redeclaredProperty,
1105 ObjCContainerDecl *lexicalDC) {
1106
Ted Kremenek9d64c152010-03-12 00:38:38 +00001107 ObjCMethodDecl *GetterMethod, *SetterMethod;
1108
1109 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1110 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1111 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1112 property->getLocation());
1113
1114 if (SetterMethod) {
1115 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1116 property->getPropertyAttributes();
1117 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1118 Context.getCanonicalType(SetterMethod->getResultType()) !=
1119 Context.VoidTy)
1120 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1121 if (SetterMethod->param_size() != 1 ||
1122 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1123 Diag(property->getLocation(),
1124 diag::warn_accessor_property_type_mismatch)
1125 << property->getDeclName()
1126 << SetterMethod->getSelector();
1127 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1128 }
1129 }
1130
1131 // Synthesize getter/setter methods if none exist.
1132 // Find the default getter and if one not found, add one.
1133 // FIXME: The synthesized property we set here is misleading. We almost always
1134 // synthesize these methods unless the user explicitly provided prototypes
1135 // (which is odd, but allowed). Sema should be typechecking that the
1136 // declarations jive in that situation (which it is not currently).
1137 if (!GetterMethod) {
1138 // No instance method of same name as property getter name was found.
1139 // Declare a getter method and add it to the list of methods
1140 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001141 SourceLocation Loc = redeclaredProperty ?
1142 redeclaredProperty->getLocation() :
1143 property->getLocation();
1144
1145 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1146 property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001147 property->getType(), 0, CD, true, false, true,
1148 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001149 (property->getPropertyImplementation() ==
1150 ObjCPropertyDecl::Optional) ?
1151 ObjCMethodDecl::Optional :
1152 ObjCMethodDecl::Required);
1153 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001154
1155 AddPropertyAttrs(*this, GetterMethod, property);
1156
Ted Kremenek23173d72010-05-18 21:09:07 +00001157 // FIXME: Eventually this shouldn't be needed, as the lexical context
1158 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001159 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001160 GetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001161 } else
1162 // A user declared getter will be synthesize when @synthesize of
1163 // the property with the same name is seen in the @implementation
1164 GetterMethod->setSynthesized(true);
1165 property->setGetterMethodDecl(GetterMethod);
1166
1167 // Skip setter if property is read-only.
1168 if (!property->isReadOnly()) {
1169 // Find the default setter and if one not found, add one.
1170 if (!SetterMethod) {
1171 // No instance method of same name as property setter name was found.
1172 // Declare a setter method and add it to the list of methods
1173 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001174 SourceLocation Loc = redeclaredProperty ?
1175 redeclaredProperty->getLocation() :
1176 property->getLocation();
1177
1178 SetterMethod =
1179 ObjCMethodDecl::Create(Context, Loc, Loc,
1180 property->getSetterName(), Context.VoidTy, 0,
1181 CD, true, false, true, false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001182 (property->getPropertyImplementation() ==
1183 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001184 ObjCMethodDecl::Optional :
1185 ObjCMethodDecl::Required);
1186
Ted Kremenek9d64c152010-03-12 00:38:38 +00001187 // Invent the arguments for the setter. We don't bother making a
1188 // nice name for the argument.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001189 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001190 property->getIdentifier(),
1191 property->getType(),
1192 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001193 SC_None,
1194 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001195 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001196 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001197
1198 AddPropertyAttrs(*this, SetterMethod, property);
1199
Ted Kremenek9d64c152010-03-12 00:38:38 +00001200 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001201 // FIXME: Eventually this shouldn't be needed, as the lexical context
1202 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001203 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001204 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001205 } else
1206 // A user declared setter will be synthesize when @synthesize of
1207 // the property with the same name is seen in the @implementation
1208 SetterMethod->setSynthesized(true);
1209 property->setSetterMethodDecl(SetterMethod);
1210 }
1211 // Add any synthesized methods to the global pool. This allows us to
1212 // handle the following, which is supported by GCC (and part of the design).
1213 //
1214 // @interface Foo
1215 // @property double bar;
1216 // @end
1217 //
1218 // void thisIsUnfortunate() {
1219 // id foo;
1220 // double bar = [foo bar];
1221 // }
1222 //
1223 if (GetterMethod)
1224 AddInstanceMethodToGlobalPool(GetterMethod);
1225 if (SetterMethod)
1226 AddInstanceMethodToGlobalPool(SetterMethod);
1227}
1228
John McCalld226f652010-08-21 09:40:31 +00001229void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001230 SourceLocation Loc,
1231 unsigned &Attributes) {
1232 // FIXME: Improve the reported location.
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001233 if (!PDecl)
1234 return;
1235
1236 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001237 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001238
1239 // readonly and readwrite/assign/retain/copy conflict.
1240 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1241 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1242 ObjCDeclSpec::DQ_PR_assign |
1243 ObjCDeclSpec::DQ_PR_copy |
1244 ObjCDeclSpec::DQ_PR_retain))) {
1245 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1246 "readwrite" :
1247 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1248 "assign" :
1249 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1250 "copy" : "retain";
1251
1252 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1253 diag::err_objc_property_attr_mutually_exclusive :
1254 diag::warn_objc_property_attr_mutually_exclusive)
1255 << "readonly" << which;
1256 }
1257
1258 // Check for copy or retain on non-object types.
1259 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1260 !PropertyTy->isObjCObjectPointerType() &&
1261 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001262 !Context.isObjCNSObjectType(PropertyTy) &&
1263 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001264 Diag(Loc, diag::err_objc_property_requires_object)
1265 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1266 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1267 }
1268
1269 // Check for more than one of { assign, copy, retain }.
1270 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1271 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1272 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1273 << "assign" << "copy";
1274 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1275 }
1276 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1277 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1278 << "assign" << "retain";
1279 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1280 }
1281 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1282 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1283 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1284 << "copy" << "retain";
1285 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1286 }
1287 }
1288
1289 // Warn if user supplied no assignment attribute, property is
1290 // readwrite, and this is an object type.
1291 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1292 ObjCDeclSpec::DQ_PR_retain)) &&
1293 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1294 PropertyTy->isObjCObjectPointerType()) {
1295 // Skip this warning in gc-only mode.
1296 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1297 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1298
1299 // If non-gc code warn that this is likely inappropriate.
1300 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1301 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1302
1303 // FIXME: Implement warning dependent on NSCopying being
1304 // implemented. See also:
1305 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1306 // (please trim this list while you are at it).
1307 }
1308
1309 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1310 && getLangOptions().getGCMode() == LangOptions::GCOnly
1311 && PropertyTy->isBlockPointerType())
1312 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1313}