blob: 4c439f90e89008f403b7d668c5640ad352a39a17 [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>());
John McCalldaa8e4e2010-11-15 09:13:47 +0000427 else {
428 CastKind K = CK_Invalid;
429 compat = (CheckAssignmentConstraints(PropType, IvarType, K)
430 == Compatible);
431 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000432 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000433 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000434 << property->getDeclName() << PropType
435 << Ivar->getDeclName() << IvarType;
436 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000437 // Note! I deliberately want it to fall thru so, we have a
438 // a property implementation and to avoid future warnings.
439 }
440
441 // FIXME! Rules for properties are somewhat different that those
442 // for assignments. Use a new routine to consolidate all cases;
443 // specifically for property redeclarations as well as for ivars.
444 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
445 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
446 if (lhsType != rhsType &&
447 lhsType->isArithmeticType()) {
448 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000449 << property->getDeclName() << PropType
450 << Ivar->getDeclName() << IvarType;
451 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000452 // Fall thru - see previous comment
453 }
454 // __weak is explicit. So it works on Canonical type.
455 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
456 getLangOptions().getGCMode() != LangOptions::NonGC) {
457 Diag(PropertyLoc, diag::error_weak_property)
458 << property->getDeclName() << Ivar->getDeclName();
459 // Fall thru - see previous comment
460 }
461 if ((property->getType()->isObjCObjectPointerType() ||
462 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
463 getLangOptions().getGCMode() != LangOptions::NonGC) {
464 Diag(PropertyLoc, diag::error_strong_property)
465 << property->getDeclName() << Ivar->getDeclName();
466 // Fall thru - see previous comment
467 }
468 }
469 } else if (PropertyIvar)
470 // @dynamic
471 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
472 assert (property && "ActOnPropertyImplDecl - property declaration missing");
473 ObjCPropertyImplDecl *PIDecl =
474 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
475 property,
476 (Synthesize ?
477 ObjCPropertyImplDecl::Synthesize
478 : ObjCPropertyImplDecl::Dynamic),
479 Ivar);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000480 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
481 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000482 if (getLangOptions().CPlusPlus && Synthesize &&
483 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000484 // For Objective-C++, need to synthesize the AST for the IVAR object to be
485 // returned by the getter as it must conform to C++'s copy-return rules.
486 // FIXME. Eventually we want to do this for Objective-C as well.
487 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
488 DeclRefExpr *SelfExpr =
489 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
490 SourceLocation());
491 Expr *IvarRefExpr =
492 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
493 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000494 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000495 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000496 SourceLocation(),
497 getterMethod->getResultType(),
498 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000499 SourceLocation(),
500 Owned(IvarRefExpr));
501 if (!Res.isInvalid()) {
502 Expr *ResExpr = Res.takeAs<Expr>();
503 if (ResExpr)
504 ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr);
505 PIDecl->setGetterCXXConstructor(ResExpr);
506 }
507 }
508 }
509 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
510 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000511 if (getLangOptions().CPlusPlus && Synthesize
512 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000513 // FIXME. Eventually we want to do this for Objective-C as well.
514 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
515 DeclRefExpr *SelfExpr =
516 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
517 SourceLocation());
518 Expr *lhs =
519 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
520 SelfExpr, true, true);
521 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
522 ParmVarDecl *Param = (*P);
523 Expr *rhs = new (Context) DeclRefExpr(Param,Param->getType(),
524 SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000525 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000526 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000527 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
528 }
529 }
530
Ted Kremenek28685ab2010-03-12 00:46:40 +0000531 if (IC) {
532 if (Synthesize)
533 if (ObjCPropertyImplDecl *PPIDecl =
534 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
535 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
536 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
537 << PropertyIvar;
538 Diag(PPIDecl->getLocation(), diag::note_previous_use);
539 }
540
541 if (ObjCPropertyImplDecl *PPIDecl
542 = IC->FindPropertyImplDecl(PropertyId)) {
543 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
544 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000545 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000546 }
547 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000548 if (getLangOptions().ObjCNonFragileABI2) {
549 // Diagnose if an ivar was lazily synthesdized due to a previous
550 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000551 // but it requires an ivar of different name.
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000552 ObjCInterfaceDecl *ClassDeclared;
553 ObjCIvarDecl *Ivar = 0;
554 if (!Synthesize)
555 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
556 else {
557 if (PropertyIvar && PropertyIvar != PropertyId)
558 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
559 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000560 // Issue diagnostics only if Ivar belongs to current class.
561 if (Ivar && Ivar->getSynthesize() &&
562 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000563 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
564 << PropertyId;
565 Ivar->setInvalidDecl();
566 }
567 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000568 } else {
569 if (Synthesize)
570 if (ObjCPropertyImplDecl *PPIDecl =
571 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
572 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
573 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
574 << PropertyIvar;
575 Diag(PPIDecl->getLocation(), diag::note_previous_use);
576 }
577
578 if (ObjCPropertyImplDecl *PPIDecl =
579 CatImplClass->FindPropertyImplDecl(PropertyId)) {
580 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
581 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000582 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000583 }
584 CatImplClass->addPropertyImplementation(PIDecl);
585 }
586
John McCalld226f652010-08-21 09:40:31 +0000587 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000588}
589
590//===----------------------------------------------------------------------===//
591// Helper methods.
592//===----------------------------------------------------------------------===//
593
Ted Kremenek9d64c152010-03-12 00:38:38 +0000594/// DiagnosePropertyMismatch - Compares two properties for their
595/// attributes and types and warns on a variety of inconsistencies.
596///
597void
598Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
599 ObjCPropertyDecl *SuperProperty,
600 const IdentifierInfo *inheritedName) {
601 ObjCPropertyDecl::PropertyAttributeKind CAttr =
602 Property->getPropertyAttributes();
603 ObjCPropertyDecl::PropertyAttributeKind SAttr =
604 SuperProperty->getPropertyAttributes();
605 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
606 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
607 Diag(Property->getLocation(), diag::warn_readonly_property)
608 << Property->getDeclName() << inheritedName;
609 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
610 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
611 Diag(Property->getLocation(), diag::warn_property_attribute)
612 << Property->getDeclName() << "copy" << inheritedName;
613 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
614 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
615 Diag(Property->getLocation(), diag::warn_property_attribute)
616 << Property->getDeclName() << "retain" << inheritedName;
617
618 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
619 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
620 Diag(Property->getLocation(), diag::warn_property_attribute)
621 << Property->getDeclName() << "atomic" << inheritedName;
622 if (Property->getSetterName() != SuperProperty->getSetterName())
623 Diag(Property->getLocation(), diag::warn_property_attribute)
624 << Property->getDeclName() << "setter" << inheritedName;
625 if (Property->getGetterName() != SuperProperty->getGetterName())
626 Diag(Property->getLocation(), diag::warn_property_attribute)
627 << Property->getDeclName() << "getter" << inheritedName;
628
629 QualType LHSType =
630 Context.getCanonicalType(SuperProperty->getType());
631 QualType RHSType =
632 Context.getCanonicalType(Property->getType());
633
634 if (!Context.typesAreCompatible(LHSType, RHSType)) {
635 // FIXME: Incorporate this test with typesAreCompatible.
636 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
637 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
638 return;
639 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
640 << Property->getType() << SuperProperty->getType() << inheritedName;
641 }
642}
643
644bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
645 ObjCMethodDecl *GetterMethod,
646 SourceLocation Loc) {
647 if (GetterMethod &&
648 GetterMethod->getResultType() != property->getType()) {
649 AssignConvertType result = Incompatible;
John McCalldaa8e4e2010-11-15 09:13:47 +0000650 if (property->getType()->isObjCObjectPointerType()) {
651 CastKind Kind = CK_Invalid;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000652 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
John McCalldaa8e4e2010-11-15 09:13:47 +0000653 property->getType(), Kind);
654 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000655 if (result != Compatible) {
656 Diag(Loc, diag::warn_accessor_property_type_mismatch)
657 << property->getDeclName()
658 << GetterMethod->getSelector();
659 Diag(GetterMethod->getLocation(), diag::note_declared_at);
660 return true;
661 }
662 }
663 return false;
664}
665
666/// ComparePropertiesInBaseAndSuper - This routine compares property
667/// declarations in base and its super class, if any, and issues
668/// diagnostics in a variety of inconsistant situations.
669///
670void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
671 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
672 if (!SDecl)
673 return;
674 // FIXME: O(N^2)
675 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
676 E = SDecl->prop_end(); S != E; ++S) {
677 ObjCPropertyDecl *SuperPDecl = (*S);
678 // Does property in super class has declaration in current class?
679 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
680 E = IDecl->prop_end(); I != E; ++I) {
681 ObjCPropertyDecl *PDecl = (*I);
682 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
683 DiagnosePropertyMismatch(PDecl, SuperPDecl,
684 SDecl->getIdentifier());
685 }
686 }
687}
688
689/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
690/// of properties declared in a protocol and compares their attribute against
691/// the same property declared in the class or category.
692void
693Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
694 ObjCProtocolDecl *PDecl) {
695 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
696 if (!IDecl) {
697 // Category
698 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
699 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
700 if (!CatDecl->IsClassExtension())
701 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
702 E = PDecl->prop_end(); P != E; ++P) {
703 ObjCPropertyDecl *Pr = (*P);
704 ObjCCategoryDecl::prop_iterator CP, CE;
705 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000706 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000707 if ((*CP)->getIdentifier() == Pr->getIdentifier())
708 break;
709 if (CP != CE)
710 // Property protocol already exist in class. Diagnose any mismatch.
711 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
712 }
713 return;
714 }
715 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
716 E = PDecl->prop_end(); P != E; ++P) {
717 ObjCPropertyDecl *Pr = (*P);
718 ObjCInterfaceDecl::prop_iterator CP, CE;
719 // Is this property already in class's list of properties?
720 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
721 if ((*CP)->getIdentifier() == Pr->getIdentifier())
722 break;
723 if (CP != CE)
724 // Property protocol already exist in class. Diagnose any mismatch.
725 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
726 }
727}
728
729/// CompareProperties - This routine compares properties
730/// declared in 'ClassOrProtocol' objects (which can be a class or an
731/// inherited protocol with the list of properties for class/category 'CDecl'
732///
John McCalld226f652010-08-21 09:40:31 +0000733void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
734 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000735 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
736
737 if (!IDecl) {
738 // Category
739 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
740 assert (CatDecl && "CompareProperties");
741 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
742 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
743 E = MDecl->protocol_end(); P != E; ++P)
744 // Match properties of category with those of protocol (*P)
745 MatchOneProtocolPropertiesInClass(CatDecl, *P);
746
747 // Go thru the list of protocols for this category and recursively match
748 // their properties with those in the category.
749 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
750 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000751 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000752 } else {
753 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
754 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
755 E = MD->protocol_end(); P != E; ++P)
756 MatchOneProtocolPropertiesInClass(CatDecl, *P);
757 }
758 return;
759 }
760
761 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000762 for (ObjCInterfaceDecl::all_protocol_iterator
763 P = MDecl->all_referenced_protocol_begin(),
764 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000765 // Match properties of class IDecl with those of protocol (*P).
766 MatchOneProtocolPropertiesInClass(IDecl, *P);
767
768 // Go thru the list of protocols for this class and recursively match
769 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +0000770 for (ObjCInterfaceDecl::all_protocol_iterator
771 P = IDecl->all_referenced_protocol_begin(),
772 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000773 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000774 } else {
775 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
776 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
777 E = MD->protocol_end(); P != E; ++P)
778 MatchOneProtocolPropertiesInClass(IDecl, *P);
779 }
780}
781
782/// isPropertyReadonly - Return true if property is readonly, by searching
783/// for the property in the class and in its categories and implementations
784///
785bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
786 ObjCInterfaceDecl *IDecl) {
787 // by far the most common case.
788 if (!PDecl->isReadOnly())
789 return false;
790 // Even if property is ready only, if interface has a user defined setter,
791 // it is not considered read only.
792 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
793 return false;
794
795 // Main class has the property as 'readonly'. Must search
796 // through the category list to see if the property's
797 // attribute has been over-ridden to 'readwrite'.
798 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
799 Category; Category = Category->getNextClassCategory()) {
800 // Even if property is ready only, if a category has a user defined setter,
801 // it is not considered read only.
802 if (Category->getInstanceMethod(PDecl->getSetterName()))
803 return false;
804 ObjCPropertyDecl *P =
805 Category->FindPropertyDeclaration(PDecl->getIdentifier());
806 if (P && !P->isReadOnly())
807 return false;
808 }
809
810 // Also, check for definition of a setter method in the implementation if
811 // all else failed.
812 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
813 if (ObjCImplementationDecl *IMD =
814 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
815 if (IMD->getInstanceMethod(PDecl->getSetterName()))
816 return false;
817 } else if (ObjCCategoryImplDecl *CIMD =
818 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
819 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
820 return false;
821 }
822 }
823 // Lastly, look through the implementation (if one is in scope).
824 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
825 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
826 return false;
827 // If all fails, look at the super class.
828 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
829 return isPropertyReadonly(PDecl, SIDecl);
830 return true;
831}
832
833/// CollectImmediateProperties - This routine collects all properties in
834/// the class and its conforming protocols; but not those it its super class.
835void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000836 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
837 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000838 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
839 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
840 E = IDecl->prop_end(); P != E; ++P) {
841 ObjCPropertyDecl *Prop = (*P);
842 PropMap[Prop->getIdentifier()] = Prop;
843 }
844 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000845 for (ObjCInterfaceDecl::all_protocol_iterator
846 PI = IDecl->all_referenced_protocol_begin(),
847 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000848 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000849 }
850 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
851 if (!CATDecl->IsClassExtension())
852 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
853 E = CATDecl->prop_end(); P != E; ++P) {
854 ObjCPropertyDecl *Prop = (*P);
855 PropMap[Prop->getIdentifier()] = Prop;
856 }
857 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000858 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +0000859 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000860 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000861 }
862 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
863 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
864 E = PDecl->prop_end(); P != E; ++P) {
865 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000866 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
867 // Exclude property for protocols which conform to class's super-class,
868 // as super-class has to implement the property.
869 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
870 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
871 if (!PropEntry)
872 PropEntry = Prop;
873 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000874 }
875 // scan through protocol's protocols.
876 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
877 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000878 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000879 }
880}
881
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000882/// CollectClassPropertyImplementations - This routine collects list of
883/// properties to be implemented in the class. This includes, class's
884/// and its conforming protocols' properties.
885static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
886 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
887 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
888 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
889 E = IDecl->prop_end(); P != E; ++P) {
890 ObjCPropertyDecl *Prop = (*P);
891 PropMap[Prop->getIdentifier()] = Prop;
892 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000893 for (ObjCInterfaceDecl::all_protocol_iterator
894 PI = IDecl->all_referenced_protocol_begin(),
895 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000896 CollectClassPropertyImplementations((*PI), PropMap);
897 }
898 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
899 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
900 E = PDecl->prop_end(); P != E; ++P) {
901 ObjCPropertyDecl *Prop = (*P);
902 PropMap[Prop->getIdentifier()] = Prop;
903 }
904 // scan through protocol's protocols.
905 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
906 E = PDecl->protocol_end(); PI != E; ++PI)
907 CollectClassPropertyImplementations((*PI), PropMap);
908 }
909}
910
911/// CollectSuperClassPropertyImplementations - This routine collects list of
912/// properties to be implemented in super class(s) and also coming from their
913/// conforming protocols.
914static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
915 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
916 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
917 while (SDecl) {
918 CollectClassPropertyImplementations(SDecl, PropMap);
919 SDecl = SDecl->getSuperClass();
920 }
921 }
922}
923
Ted Kremenek9d64c152010-03-12 00:38:38 +0000924/// LookupPropertyDecl - Looks up a property in the current class and all
925/// its protocols.
926ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
927 IdentifierInfo *II) {
928 if (const ObjCInterfaceDecl *IDecl =
929 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
930 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
931 E = IDecl->prop_end(); P != E; ++P) {
932 ObjCPropertyDecl *Prop = (*P);
933 if (Prop->getIdentifier() == II)
934 return Prop;
935 }
936 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000937 for (ObjCInterfaceDecl::all_protocol_iterator
938 PI = IDecl->all_referenced_protocol_begin(),
939 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000940 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
941 if (Prop)
942 return Prop;
943 }
944 }
945 else if (const ObjCProtocolDecl *PDecl =
946 dyn_cast<ObjCProtocolDecl>(CDecl)) {
947 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
948 E = PDecl->prop_end(); P != E; ++P) {
949 ObjCPropertyDecl *Prop = (*P);
950 if (Prop->getIdentifier() == II)
951 return Prop;
952 }
953 // scan through protocol's protocols.
954 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
955 E = PDecl->protocol_end(); PI != E; ++PI) {
956 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
957 if (Prop)
958 return Prop;
959 }
960 }
961 return 0;
962}
963
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000964/// DefaultSynthesizeProperties - This routine default synthesizes all
965/// properties which must be synthesized in class's @implementation.
966void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
967 ObjCInterfaceDecl *IDecl) {
968
969 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
970 CollectClassPropertyImplementations(IDecl, PropMap);
971 if (PropMap.empty())
972 return;
973 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
974 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
975
976 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
977 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
978 ObjCPropertyDecl *Prop = P->second;
979 // If property to be implemented in the super class, ignore.
980 if (SuperPropMap[Prop->getIdentifier()])
981 continue;
982 // Is there a matching propery synthesize/dynamic?
983 if (Prop->isInvalidDecl() ||
984 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
985 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
986 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000987 // Property may have been synthesized by user.
988 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
989 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +0000990 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
991 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
992 continue;
993 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
994 continue;
995 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000996
Ted Kremenek2a6af6b2010-09-24 01:23:01 +0000997
998 // We use invalid SourceLocations for the synthesized ivars since they
999 // aren't really synthesized at a particular location; they just exist.
1000 // Saying that they are located at the @implementation isn't really going
1001 // to help users.
1002 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1003 true,IMPDecl,
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001004 Prop->getIdentifier(), Prop->getIdentifier());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001005 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001006}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001007
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001008void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001009 ObjCContainerDecl *CDecl,
1010 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001011 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1012 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1013 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1014
Ted Kremenek9d64c152010-03-12 00:38:38 +00001015 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001016 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001017 if (PropMap.empty())
1018 return;
1019
1020 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1021 for (ObjCImplDecl::propimpl_iterator
1022 I = IMPDecl->propimpl_begin(),
1023 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1024 PropImplMap.insert((*I)->getPropertyDecl());
1025
1026 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1027 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1028 ObjCPropertyDecl *Prop = P->second;
1029 // Is there a matching propery synthesize/dynamic?
1030 if (Prop->isInvalidDecl() ||
1031 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1032 PropImplMap.count(Prop))
1033 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001034 if (!InsMap.count(Prop->getGetterName())) {
1035 Diag(Prop->getLocation(),
1036 isa<ObjCCategoryDecl>(CDecl) ?
1037 diag::warn_setter_getter_impl_required_in_category :
1038 diag::warn_setter_getter_impl_required)
1039 << Prop->getDeclName() << Prop->getGetterName();
1040 Diag(IMPDecl->getLocation(),
1041 diag::note_property_impl_required);
1042 }
1043
1044 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1045 Diag(Prop->getLocation(),
1046 isa<ObjCCategoryDecl>(CDecl) ?
1047 diag::warn_setter_getter_impl_required_in_category :
1048 diag::warn_setter_getter_impl_required)
1049 << Prop->getDeclName() << Prop->getSetterName();
1050 Diag(IMPDecl->getLocation(),
1051 diag::note_property_impl_required);
1052 }
1053 }
1054}
1055
1056void
1057Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1058 ObjCContainerDecl* IDecl) {
1059 // Rules apply in non-GC mode only
1060 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1061 return;
1062 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1063 E = IDecl->prop_end();
1064 I != E; ++I) {
1065 ObjCPropertyDecl *Property = (*I);
1066 unsigned Attributes = Property->getPropertyAttributes();
1067 // We only care about readwrite atomic property.
1068 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1069 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1070 continue;
1071 if (const ObjCPropertyImplDecl *PIDecl
1072 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1073 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1074 continue;
1075 ObjCMethodDecl *GetterMethod =
1076 IMPDecl->getInstanceMethod(Property->getGetterName());
1077 ObjCMethodDecl *SetterMethod =
1078 IMPDecl->getInstanceMethod(Property->getSetterName());
1079 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1080 SourceLocation MethodLoc =
1081 (GetterMethod ? GetterMethod->getLocation()
1082 : SetterMethod->getLocation());
1083 Diag(MethodLoc, diag::warn_atomic_property_rule)
1084 << Property->getIdentifier();
1085 Diag(Property->getLocation(), diag::note_property_declare);
1086 }
1087 }
1088 }
1089}
1090
John McCall5de74d12010-11-10 07:01:40 +00001091/// AddPropertyAttrs - Propagates attributes from a property to the
1092/// implicitly-declared getter or setter for that property.
1093static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1094 ObjCPropertyDecl *Property) {
1095 // Should we just clone all attributes over?
1096 if (DeprecatedAttr *A = Property->getAttr<DeprecatedAttr>())
1097 PropertyMethod->addAttr(A->clone(S.Context));
1098 if (UnavailableAttr *A = Property->getAttr<UnavailableAttr>())
1099 PropertyMethod->addAttr(A->clone(S.Context));
1100}
1101
Ted Kremenek9d64c152010-03-12 00:38:38 +00001102/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1103/// have the property type and issue diagnostics if they don't.
1104/// Also synthesize a getter/setter method if none exist (and update the
1105/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1106/// methods is the "right" thing to do.
1107void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001108 ObjCContainerDecl *CD,
1109 ObjCPropertyDecl *redeclaredProperty,
1110 ObjCContainerDecl *lexicalDC) {
1111
Ted Kremenek9d64c152010-03-12 00:38:38 +00001112 ObjCMethodDecl *GetterMethod, *SetterMethod;
1113
1114 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1115 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1116 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1117 property->getLocation());
1118
1119 if (SetterMethod) {
1120 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1121 property->getPropertyAttributes();
1122 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1123 Context.getCanonicalType(SetterMethod->getResultType()) !=
1124 Context.VoidTy)
1125 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1126 if (SetterMethod->param_size() != 1 ||
1127 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1128 Diag(property->getLocation(),
1129 diag::warn_accessor_property_type_mismatch)
1130 << property->getDeclName()
1131 << SetterMethod->getSelector();
1132 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1133 }
1134 }
1135
1136 // Synthesize getter/setter methods if none exist.
1137 // Find the default getter and if one not found, add one.
1138 // FIXME: The synthesized property we set here is misleading. We almost always
1139 // synthesize these methods unless the user explicitly provided prototypes
1140 // (which is odd, but allowed). Sema should be typechecking that the
1141 // declarations jive in that situation (which it is not currently).
1142 if (!GetterMethod) {
1143 // No instance method of same name as property getter name was found.
1144 // Declare a getter method and add it to the list of methods
1145 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001146 SourceLocation Loc = redeclaredProperty ?
1147 redeclaredProperty->getLocation() :
1148 property->getLocation();
1149
1150 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1151 property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001152 property->getType(), 0, CD, true, false, true,
1153 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001154 (property->getPropertyImplementation() ==
1155 ObjCPropertyDecl::Optional) ?
1156 ObjCMethodDecl::Optional :
1157 ObjCMethodDecl::Required);
1158 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001159
1160 AddPropertyAttrs(*this, GetterMethod, property);
1161
Ted Kremenek23173d72010-05-18 21:09:07 +00001162 // FIXME: Eventually this shouldn't be needed, as the lexical context
1163 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001164 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001165 GetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001166 } else
1167 // A user declared getter will be synthesize when @synthesize of
1168 // the property with the same name is seen in the @implementation
1169 GetterMethod->setSynthesized(true);
1170 property->setGetterMethodDecl(GetterMethod);
1171
1172 // Skip setter if property is read-only.
1173 if (!property->isReadOnly()) {
1174 // Find the default setter and if one not found, add one.
1175 if (!SetterMethod) {
1176 // No instance method of same name as property setter name was found.
1177 // Declare a setter method and add it to the list of methods
1178 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001179 SourceLocation Loc = redeclaredProperty ?
1180 redeclaredProperty->getLocation() :
1181 property->getLocation();
1182
1183 SetterMethod =
1184 ObjCMethodDecl::Create(Context, Loc, Loc,
1185 property->getSetterName(), Context.VoidTy, 0,
1186 CD, true, false, true, false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001187 (property->getPropertyImplementation() ==
1188 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001189 ObjCMethodDecl::Optional :
1190 ObjCMethodDecl::Required);
1191
Ted Kremenek9d64c152010-03-12 00:38:38 +00001192 // Invent the arguments for the setter. We don't bother making a
1193 // nice name for the argument.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001194 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001195 property->getIdentifier(),
1196 property->getType(),
1197 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001198 SC_None,
1199 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001200 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001201 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001202
1203 AddPropertyAttrs(*this, SetterMethod, property);
1204
Ted Kremenek9d64c152010-03-12 00:38:38 +00001205 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001206 // FIXME: Eventually this shouldn't be needed, as the lexical context
1207 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001208 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001209 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001210 } else
1211 // A user declared setter will be synthesize when @synthesize of
1212 // the property with the same name is seen in the @implementation
1213 SetterMethod->setSynthesized(true);
1214 property->setSetterMethodDecl(SetterMethod);
1215 }
1216 // Add any synthesized methods to the global pool. This allows us to
1217 // handle the following, which is supported by GCC (and part of the design).
1218 //
1219 // @interface Foo
1220 // @property double bar;
1221 // @end
1222 //
1223 // void thisIsUnfortunate() {
1224 // id foo;
1225 // double bar = [foo bar];
1226 // }
1227 //
1228 if (GetterMethod)
1229 AddInstanceMethodToGlobalPool(GetterMethod);
1230 if (SetterMethod)
1231 AddInstanceMethodToGlobalPool(SetterMethod);
1232}
1233
John McCalld226f652010-08-21 09:40:31 +00001234void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001235 SourceLocation Loc,
1236 unsigned &Attributes) {
1237 // FIXME: Improve the reported location.
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001238 if (!PDecl)
1239 return;
1240
1241 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001242 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001243
1244 // readonly and readwrite/assign/retain/copy conflict.
1245 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1246 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1247 ObjCDeclSpec::DQ_PR_assign |
1248 ObjCDeclSpec::DQ_PR_copy |
1249 ObjCDeclSpec::DQ_PR_retain))) {
1250 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1251 "readwrite" :
1252 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1253 "assign" :
1254 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1255 "copy" : "retain";
1256
1257 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1258 diag::err_objc_property_attr_mutually_exclusive :
1259 diag::warn_objc_property_attr_mutually_exclusive)
1260 << "readonly" << which;
1261 }
1262
1263 // Check for copy or retain on non-object types.
1264 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1265 !PropertyTy->isObjCObjectPointerType() &&
1266 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001267 !Context.isObjCNSObjectType(PropertyTy) &&
1268 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001269 Diag(Loc, diag::err_objc_property_requires_object)
1270 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1271 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1272 }
1273
1274 // Check for more than one of { assign, copy, retain }.
1275 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1276 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1277 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1278 << "assign" << "copy";
1279 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1280 }
1281 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1282 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1283 << "assign" << "retain";
1284 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1285 }
1286 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1287 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1288 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1289 << "copy" << "retain";
1290 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1291 }
1292 }
1293
1294 // Warn if user supplied no assignment attribute, property is
1295 // readwrite, and this is an object type.
1296 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1297 ObjCDeclSpec::DQ_PR_retain)) &&
1298 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1299 PropertyTy->isObjCObjectPointerType()) {
1300 // Skip this warning in gc-only mode.
1301 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1302 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1303
1304 // If non-gc code warn that this is likely inappropriate.
1305 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1306 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1307
1308 // FIXME: Implement warning dependent on NSCopying being
1309 // implemented. See also:
1310 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1311 // (please trim this list while you are at it).
1312 }
1313
1314 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1315 && getLangOptions().getGCMode() == LangOptions::GCOnly
1316 && PropertyTy->isBlockPointerType())
1317 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1318}