blob: 607b7b1782af1f90214382e9f95f6261947ee3a9 [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,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000321 IdentifierInfo *PropertyIvar,
322 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000323 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000324 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000325 // Make sure we have a context for the property implementation declaration.
326 if (!ClassImpDecl) {
327 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000328 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000329 }
330 ObjCPropertyDecl *property = 0;
331 ObjCInterfaceDecl* IDecl = 0;
332 // Find the class or category class where this property must have
333 // a declaration.
334 ObjCImplementationDecl *IC = 0;
335 ObjCCategoryImplDecl* CatImplClass = 0;
336 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
337 IDecl = IC->getClassInterface();
338 // We always synthesize an interface for an implementation
339 // without an interface decl. So, IDecl is always non-zero.
340 assert(IDecl &&
341 "ActOnPropertyImplDecl - @implementation without @interface");
342
343 // Look for this property declaration in the @implementation's @interface
344 property = IDecl->FindPropertyDeclaration(PropertyId);
345 if (!property) {
346 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000347 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000348 }
349 if (const ObjCCategoryDecl *CD =
350 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
351 if (!CD->IsClassExtension()) {
352 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
353 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000354 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000355 }
356 }
357 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
358 if (Synthesize) {
359 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000360 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000361 }
362 IDecl = CatImplClass->getClassInterface();
363 if (!IDecl) {
364 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000365 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000366 }
367 ObjCCategoryDecl *Category =
368 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
369
370 // If category for this implementation not found, it is an error which
371 // has already been reported eralier.
372 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000373 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000374 // Look for this property declaration in @implementation's category
375 property = Category->FindPropertyDeclaration(PropertyId);
376 if (!property) {
377 Diag(PropertyLoc, diag::error_bad_category_property_decl)
378 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000379 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000380 }
381 } else {
382 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000383 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000384 }
385 ObjCIvarDecl *Ivar = 0;
386 // Check that we have a valid, previously declared ivar for @synthesize
387 if (Synthesize) {
388 // @synthesize
389 if (!PropertyIvar)
390 PropertyIvar = PropertyId;
391 QualType PropType = Context.getCanonicalType(property->getType());
392 // Check that this is a previously declared 'ivar' in 'IDecl' interface
393 ObjCInterfaceDecl *ClassDeclared;
394 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
395 if (!Ivar) {
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000396 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000397 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian2846b2b2010-04-06 23:36:17 +0000398 ObjCIvarDecl::Protected,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000399 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000400 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000401 IDecl->makeDeclVisibleInContext(Ivar, false);
402 property->setPropertyIvarDecl(Ivar);
403
404 if (!getLangOptions().ObjCNonFragileABI)
405 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
406 // Note! I deliberately want it to fall thru so, we have a
407 // a property implementation and to avoid future warnings.
408 } else if (getLangOptions().ObjCNonFragileABI &&
409 ClassDeclared != IDecl) {
410 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
411 << property->getDeclName() << Ivar->getDeclName()
412 << ClassDeclared->getDeclName();
413 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000414 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000415 // Note! I deliberately want it to fall thru so more errors are caught.
416 }
417 QualType IvarType = Context.getCanonicalType(Ivar->getType());
418
419 // Check that type of property and its ivar are type compatible.
420 if (PropType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000421 bool compat = false;
422 if (isa<ObjCObjectPointerType>(PropType)
423 && isa<ObjCObjectPointerType>(IvarType))
424 compat =
425 Context.canAssignObjCInterfaces(
426 PropType->getAs<ObjCObjectPointerType>(),
427 IvarType->getAs<ObjCObjectPointerType>());
John McCall1c23e912010-11-16 02:32:08 +0000428 else
429 compat = (CheckAssignmentConstraints(PropType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000430 == Compatible);
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000431 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000432 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000433 << property->getDeclName() << PropType
434 << Ivar->getDeclName() << IvarType;
435 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000436 // Note! I deliberately want it to fall thru so, we have a
437 // a property implementation and to avoid future warnings.
438 }
439
440 // FIXME! Rules for properties are somewhat different that those
441 // for assignments. Use a new routine to consolidate all cases;
442 // specifically for property redeclarations as well as for ivars.
443 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
444 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
445 if (lhsType != rhsType &&
446 lhsType->isArithmeticType()) {
447 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000448 << property->getDeclName() << PropType
449 << Ivar->getDeclName() << IvarType;
450 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000451 // Fall thru - see previous comment
452 }
453 // __weak is explicit. So it works on Canonical type.
454 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
455 getLangOptions().getGCMode() != LangOptions::NonGC) {
456 Diag(PropertyLoc, diag::error_weak_property)
457 << property->getDeclName() << Ivar->getDeclName();
458 // Fall thru - see previous comment
459 }
460 if ((property->getType()->isObjCObjectPointerType() ||
461 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
462 getLangOptions().getGCMode() != LangOptions::NonGC) {
463 Diag(PropertyLoc, diag::error_strong_property)
464 << property->getDeclName() << Ivar->getDeclName();
465 // Fall thru - see previous comment
466 }
467 }
468 } else if (PropertyIvar)
469 // @dynamic
470 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
471 assert (property && "ActOnPropertyImplDecl - property declaration missing");
472 ObjCPropertyImplDecl *PIDecl =
473 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
474 property,
475 (Synthesize ?
476 ObjCPropertyImplDecl::Synthesize
477 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000478 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000479 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
480 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000481 if (getLangOptions().CPlusPlus && Synthesize &&
482 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000483 // For Objective-C++, need to synthesize the AST for the IVAR object to be
484 // returned by the getter as it must conform to C++'s copy-return rules.
485 // FIXME. Eventually we want to do this for Objective-C as well.
486 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
487 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000488 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
489 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000490 Expr *IvarRefExpr =
491 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
492 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000493 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000494 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000495 SourceLocation(),
496 getterMethod->getResultType(),
497 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000498 SourceLocation(),
499 Owned(IvarRefExpr));
500 if (!Res.isInvalid()) {
501 Expr *ResExpr = Res.takeAs<Expr>();
502 if (ResExpr)
503 ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr);
504 PIDecl->setGetterCXXConstructor(ResExpr);
505 }
506 }
507 }
508 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
509 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000510 if (getLangOptions().CPlusPlus && Synthesize
511 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000512 // FIXME. Eventually we want to do this for Objective-C as well.
513 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
514 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000515 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
516 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000517 Expr *lhs =
518 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
519 SelfExpr, true, true);
520 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
521 ParmVarDecl *Param = (*P);
John McCallf89e55a2010-11-18 06:31:45 +0000522 Expr *rhs = new (Context) DeclRefExpr(Param, Param->getType(),
523 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000524 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000525 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000526 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
527 }
528 }
529
Ted Kremenek28685ab2010-03-12 00:46:40 +0000530 if (IC) {
531 if (Synthesize)
532 if (ObjCPropertyImplDecl *PPIDecl =
533 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
534 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
535 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
536 << PropertyIvar;
537 Diag(PPIDecl->getLocation(), diag::note_previous_use);
538 }
539
540 if (ObjCPropertyImplDecl *PPIDecl
541 = IC->FindPropertyImplDecl(PropertyId)) {
542 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
543 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000544 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000545 }
546 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000547 if (getLangOptions().ObjCNonFragileABI2) {
548 // Diagnose if an ivar was lazily synthesdized due to a previous
549 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000550 // but it requires an ivar of different name.
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000551 ObjCInterfaceDecl *ClassDeclared;
552 ObjCIvarDecl *Ivar = 0;
553 if (!Synthesize)
554 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
555 else {
556 if (PropertyIvar && PropertyIvar != PropertyId)
557 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
558 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000559 // Issue diagnostics only if Ivar belongs to current class.
560 if (Ivar && Ivar->getSynthesize() &&
561 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000562 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
563 << PropertyId;
564 Ivar->setInvalidDecl();
565 }
566 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000567 } else {
568 if (Synthesize)
569 if (ObjCPropertyImplDecl *PPIDecl =
570 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
571 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
572 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
573 << PropertyIvar;
574 Diag(PPIDecl->getLocation(), diag::note_previous_use);
575 }
576
577 if (ObjCPropertyImplDecl *PPIDecl =
578 CatImplClass->FindPropertyImplDecl(PropertyId)) {
579 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
580 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000581 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000582 }
583 CatImplClass->addPropertyImplementation(PIDecl);
584 }
585
John McCalld226f652010-08-21 09:40:31 +0000586 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000587}
588
589//===----------------------------------------------------------------------===//
590// Helper methods.
591//===----------------------------------------------------------------------===//
592
Ted Kremenek9d64c152010-03-12 00:38:38 +0000593/// DiagnosePropertyMismatch - Compares two properties for their
594/// attributes and types and warns on a variety of inconsistencies.
595///
596void
597Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
598 ObjCPropertyDecl *SuperProperty,
599 const IdentifierInfo *inheritedName) {
600 ObjCPropertyDecl::PropertyAttributeKind CAttr =
601 Property->getPropertyAttributes();
602 ObjCPropertyDecl::PropertyAttributeKind SAttr =
603 SuperProperty->getPropertyAttributes();
604 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
605 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
606 Diag(Property->getLocation(), diag::warn_readonly_property)
607 << Property->getDeclName() << inheritedName;
608 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
609 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
610 Diag(Property->getLocation(), diag::warn_property_attribute)
611 << Property->getDeclName() << "copy" << inheritedName;
612 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
613 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
614 Diag(Property->getLocation(), diag::warn_property_attribute)
615 << Property->getDeclName() << "retain" << inheritedName;
616
617 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
618 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
619 Diag(Property->getLocation(), diag::warn_property_attribute)
620 << Property->getDeclName() << "atomic" << inheritedName;
621 if (Property->getSetterName() != SuperProperty->getSetterName())
622 Diag(Property->getLocation(), diag::warn_property_attribute)
623 << Property->getDeclName() << "setter" << inheritedName;
624 if (Property->getGetterName() != SuperProperty->getGetterName())
625 Diag(Property->getLocation(), diag::warn_property_attribute)
626 << Property->getDeclName() << "getter" << inheritedName;
627
628 QualType LHSType =
629 Context.getCanonicalType(SuperProperty->getType());
630 QualType RHSType =
631 Context.getCanonicalType(Property->getType());
632
633 if (!Context.typesAreCompatible(LHSType, RHSType)) {
634 // FIXME: Incorporate this test with typesAreCompatible.
635 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
636 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
637 return;
638 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
639 << Property->getType() << SuperProperty->getType() << inheritedName;
640 }
641}
642
643bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
644 ObjCMethodDecl *GetterMethod,
645 SourceLocation Loc) {
646 if (GetterMethod &&
647 GetterMethod->getResultType() != property->getType()) {
648 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000649 if (property->getType()->isObjCObjectPointerType())
Ted Kremenek9d64c152010-03-12 00:38:38 +0000650 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000651 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000652 if (result != Compatible) {
653 Diag(Loc, diag::warn_accessor_property_type_mismatch)
654 << property->getDeclName()
655 << GetterMethod->getSelector();
656 Diag(GetterMethod->getLocation(), diag::note_declared_at);
657 return true;
658 }
659 }
660 return false;
661}
662
663/// ComparePropertiesInBaseAndSuper - This routine compares property
664/// declarations in base and its super class, if any, and issues
665/// diagnostics in a variety of inconsistant situations.
666///
667void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
668 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
669 if (!SDecl)
670 return;
671 // FIXME: O(N^2)
672 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
673 E = SDecl->prop_end(); S != E; ++S) {
674 ObjCPropertyDecl *SuperPDecl = (*S);
675 // Does property in super class has declaration in current class?
676 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
677 E = IDecl->prop_end(); I != E; ++I) {
678 ObjCPropertyDecl *PDecl = (*I);
679 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
680 DiagnosePropertyMismatch(PDecl, SuperPDecl,
681 SDecl->getIdentifier());
682 }
683 }
684}
685
686/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
687/// of properties declared in a protocol and compares their attribute against
688/// the same property declared in the class or category.
689void
690Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
691 ObjCProtocolDecl *PDecl) {
692 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
693 if (!IDecl) {
694 // Category
695 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
696 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
697 if (!CatDecl->IsClassExtension())
698 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
699 E = PDecl->prop_end(); P != E; ++P) {
700 ObjCPropertyDecl *Pr = (*P);
701 ObjCCategoryDecl::prop_iterator CP, CE;
702 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000703 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000704 if ((*CP)->getIdentifier() == Pr->getIdentifier())
705 break;
706 if (CP != CE)
707 // Property protocol already exist in class. Diagnose any mismatch.
708 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
709 }
710 return;
711 }
712 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
713 E = PDecl->prop_end(); P != E; ++P) {
714 ObjCPropertyDecl *Pr = (*P);
715 ObjCInterfaceDecl::prop_iterator CP, CE;
716 // Is this property already in class's list of properties?
717 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
718 if ((*CP)->getIdentifier() == Pr->getIdentifier())
719 break;
720 if (CP != CE)
721 // Property protocol already exist in class. Diagnose any mismatch.
722 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
723 }
724}
725
726/// CompareProperties - This routine compares properties
727/// declared in 'ClassOrProtocol' objects (which can be a class or an
728/// inherited protocol with the list of properties for class/category 'CDecl'
729///
John McCalld226f652010-08-21 09:40:31 +0000730void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
731 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000732 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
733
734 if (!IDecl) {
735 // Category
736 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
737 assert (CatDecl && "CompareProperties");
738 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
739 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
740 E = MDecl->protocol_end(); P != E; ++P)
741 // Match properties of category with those of protocol (*P)
742 MatchOneProtocolPropertiesInClass(CatDecl, *P);
743
744 // Go thru the list of protocols for this category and recursively match
745 // their properties with those in the category.
746 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
747 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000748 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000749 } else {
750 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
751 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
752 E = MD->protocol_end(); P != E; ++P)
753 MatchOneProtocolPropertiesInClass(CatDecl, *P);
754 }
755 return;
756 }
757
758 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000759 for (ObjCInterfaceDecl::all_protocol_iterator
760 P = MDecl->all_referenced_protocol_begin(),
761 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000762 // Match properties of class IDecl with those of protocol (*P).
763 MatchOneProtocolPropertiesInClass(IDecl, *P);
764
765 // Go thru the list of protocols for this class and recursively match
766 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +0000767 for (ObjCInterfaceDecl::all_protocol_iterator
768 P = IDecl->all_referenced_protocol_begin(),
769 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000770 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000771 } else {
772 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
773 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
774 E = MD->protocol_end(); P != E; ++P)
775 MatchOneProtocolPropertiesInClass(IDecl, *P);
776 }
777}
778
779/// isPropertyReadonly - Return true if property is readonly, by searching
780/// for the property in the class and in its categories and implementations
781///
782bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
783 ObjCInterfaceDecl *IDecl) {
784 // by far the most common case.
785 if (!PDecl->isReadOnly())
786 return false;
787 // Even if property is ready only, if interface has a user defined setter,
788 // it is not considered read only.
789 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
790 return false;
791
792 // Main class has the property as 'readonly'. Must search
793 // through the category list to see if the property's
794 // attribute has been over-ridden to 'readwrite'.
795 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
796 Category; Category = Category->getNextClassCategory()) {
797 // Even if property is ready only, if a category has a user defined setter,
798 // it is not considered read only.
799 if (Category->getInstanceMethod(PDecl->getSetterName()))
800 return false;
801 ObjCPropertyDecl *P =
802 Category->FindPropertyDeclaration(PDecl->getIdentifier());
803 if (P && !P->isReadOnly())
804 return false;
805 }
806
807 // Also, check for definition of a setter method in the implementation if
808 // all else failed.
809 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
810 if (ObjCImplementationDecl *IMD =
811 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
812 if (IMD->getInstanceMethod(PDecl->getSetterName()))
813 return false;
814 } else if (ObjCCategoryImplDecl *CIMD =
815 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
816 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
817 return false;
818 }
819 }
820 // Lastly, look through the implementation (if one is in scope).
821 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
822 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
823 return false;
824 // If all fails, look at the super class.
825 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
826 return isPropertyReadonly(PDecl, SIDecl);
827 return true;
828}
829
830/// CollectImmediateProperties - This routine collects all properties in
831/// the class and its conforming protocols; but not those it its super class.
832void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000833 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
834 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000835 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
836 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
837 E = IDecl->prop_end(); P != E; ++P) {
838 ObjCPropertyDecl *Prop = (*P);
839 PropMap[Prop->getIdentifier()] = Prop;
840 }
841 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000842 for (ObjCInterfaceDecl::all_protocol_iterator
843 PI = IDecl->all_referenced_protocol_begin(),
844 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000845 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000846 }
847 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
848 if (!CATDecl->IsClassExtension())
849 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
850 E = CATDecl->prop_end(); P != E; ++P) {
851 ObjCPropertyDecl *Prop = (*P);
852 PropMap[Prop->getIdentifier()] = Prop;
853 }
854 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000855 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +0000856 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000857 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000858 }
859 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
860 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
861 E = PDecl->prop_end(); P != E; ++P) {
862 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000863 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
864 // Exclude property for protocols which conform to class's super-class,
865 // as super-class has to implement the property.
866 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
867 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
868 if (!PropEntry)
869 PropEntry = Prop;
870 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000871 }
872 // scan through protocol's protocols.
873 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
874 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000875 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000876 }
877}
878
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000879/// CollectClassPropertyImplementations - This routine collects list of
880/// properties to be implemented in the class. This includes, class's
881/// and its conforming protocols' properties.
882static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
883 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
884 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
885 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
886 E = IDecl->prop_end(); P != E; ++P) {
887 ObjCPropertyDecl *Prop = (*P);
888 PropMap[Prop->getIdentifier()] = Prop;
889 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000890 for (ObjCInterfaceDecl::all_protocol_iterator
891 PI = IDecl->all_referenced_protocol_begin(),
892 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000893 CollectClassPropertyImplementations((*PI), PropMap);
894 }
895 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
896 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
897 E = PDecl->prop_end(); P != E; ++P) {
898 ObjCPropertyDecl *Prop = (*P);
899 PropMap[Prop->getIdentifier()] = Prop;
900 }
901 // scan through protocol's protocols.
902 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
903 E = PDecl->protocol_end(); PI != E; ++PI)
904 CollectClassPropertyImplementations((*PI), PropMap);
905 }
906}
907
908/// CollectSuperClassPropertyImplementations - This routine collects list of
909/// properties to be implemented in super class(s) and also coming from their
910/// conforming protocols.
911static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
912 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
913 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
914 while (SDecl) {
915 CollectClassPropertyImplementations(SDecl, PropMap);
916 SDecl = SDecl->getSuperClass();
917 }
918 }
919}
920
Ted Kremenek9d64c152010-03-12 00:38:38 +0000921/// LookupPropertyDecl - Looks up a property in the current class and all
922/// its protocols.
923ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
924 IdentifierInfo *II) {
925 if (const ObjCInterfaceDecl *IDecl =
926 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
927 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
928 E = IDecl->prop_end(); P != E; ++P) {
929 ObjCPropertyDecl *Prop = (*P);
930 if (Prop->getIdentifier() == II)
931 return Prop;
932 }
933 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000934 for (ObjCInterfaceDecl::all_protocol_iterator
935 PI = IDecl->all_referenced_protocol_begin(),
936 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000937 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
938 if (Prop)
939 return Prop;
940 }
941 }
942 else if (const ObjCProtocolDecl *PDecl =
943 dyn_cast<ObjCProtocolDecl>(CDecl)) {
944 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
945 E = PDecl->prop_end(); P != E; ++P) {
946 ObjCPropertyDecl *Prop = (*P);
947 if (Prop->getIdentifier() == II)
948 return Prop;
949 }
950 // scan through protocol's protocols.
951 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
952 E = PDecl->protocol_end(); PI != E; ++PI) {
953 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
954 if (Prop)
955 return Prop;
956 }
957 }
958 return 0;
959}
960
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000961/// DefaultSynthesizeProperties - This routine default synthesizes all
962/// properties which must be synthesized in class's @implementation.
963void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
964 ObjCInterfaceDecl *IDecl) {
965
966 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
967 CollectClassPropertyImplementations(IDecl, PropMap);
968 if (PropMap.empty())
969 return;
970 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
971 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
972
973 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
974 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
975 ObjCPropertyDecl *Prop = P->second;
976 // If property to be implemented in the super class, ignore.
977 if (SuperPropMap[Prop->getIdentifier()])
978 continue;
979 // Is there a matching propery synthesize/dynamic?
980 if (Prop->isInvalidDecl() ||
981 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
982 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
983 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000984 // Property may have been synthesized by user.
985 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
986 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +0000987 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
988 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
989 continue;
990 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
991 continue;
992 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000993
Ted Kremenek2a6af6b2010-09-24 01:23:01 +0000994
995 // We use invalid SourceLocations for the synthesized ivars since they
996 // aren't really synthesized at a particular location; they just exist.
997 // Saying that they are located at the @implementation isn't really going
998 // to help users.
999 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1000 true,IMPDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001001 Prop->getIdentifier(), Prop->getIdentifier(),
1002 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001003 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001004}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001005
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001006void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001007 ObjCContainerDecl *CDecl,
1008 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001009 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1010 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1011 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1012
Ted Kremenek9d64c152010-03-12 00:38:38 +00001013 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001014 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001015 if (PropMap.empty())
1016 return;
1017
1018 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1019 for (ObjCImplDecl::propimpl_iterator
1020 I = IMPDecl->propimpl_begin(),
1021 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1022 PropImplMap.insert((*I)->getPropertyDecl());
1023
1024 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1025 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1026 ObjCPropertyDecl *Prop = P->second;
1027 // Is there a matching propery synthesize/dynamic?
1028 if (Prop->isInvalidDecl() ||
1029 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1030 PropImplMap.count(Prop))
1031 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001032 if (!InsMap.count(Prop->getGetterName())) {
1033 Diag(Prop->getLocation(),
1034 isa<ObjCCategoryDecl>(CDecl) ?
1035 diag::warn_setter_getter_impl_required_in_category :
1036 diag::warn_setter_getter_impl_required)
1037 << Prop->getDeclName() << Prop->getGetterName();
1038 Diag(IMPDecl->getLocation(),
1039 diag::note_property_impl_required);
1040 }
1041
1042 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1043 Diag(Prop->getLocation(),
1044 isa<ObjCCategoryDecl>(CDecl) ?
1045 diag::warn_setter_getter_impl_required_in_category :
1046 diag::warn_setter_getter_impl_required)
1047 << Prop->getDeclName() << Prop->getSetterName();
1048 Diag(IMPDecl->getLocation(),
1049 diag::note_property_impl_required);
1050 }
1051 }
1052}
1053
1054void
1055Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1056 ObjCContainerDecl* IDecl) {
1057 // Rules apply in non-GC mode only
1058 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1059 return;
1060 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1061 E = IDecl->prop_end();
1062 I != E; ++I) {
1063 ObjCPropertyDecl *Property = (*I);
1064 unsigned Attributes = Property->getPropertyAttributes();
1065 // We only care about readwrite atomic property.
1066 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1067 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1068 continue;
1069 if (const ObjCPropertyImplDecl *PIDecl
1070 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1071 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1072 continue;
1073 ObjCMethodDecl *GetterMethod =
1074 IMPDecl->getInstanceMethod(Property->getGetterName());
1075 ObjCMethodDecl *SetterMethod =
1076 IMPDecl->getInstanceMethod(Property->getSetterName());
1077 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1078 SourceLocation MethodLoc =
1079 (GetterMethod ? GetterMethod->getLocation()
1080 : SetterMethod->getLocation());
1081 Diag(MethodLoc, diag::warn_atomic_property_rule)
1082 << Property->getIdentifier();
1083 Diag(Property->getLocation(), diag::note_property_declare);
1084 }
1085 }
1086 }
1087}
1088
John McCall5de74d12010-11-10 07:01:40 +00001089/// AddPropertyAttrs - Propagates attributes from a property to the
1090/// implicitly-declared getter or setter for that property.
1091static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1092 ObjCPropertyDecl *Property) {
1093 // Should we just clone all attributes over?
1094 if (DeprecatedAttr *A = Property->getAttr<DeprecatedAttr>())
1095 PropertyMethod->addAttr(A->clone(S.Context));
1096 if (UnavailableAttr *A = Property->getAttr<UnavailableAttr>())
1097 PropertyMethod->addAttr(A->clone(S.Context));
1098}
1099
Ted Kremenek9d64c152010-03-12 00:38:38 +00001100/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1101/// have the property type and issue diagnostics if they don't.
1102/// Also synthesize a getter/setter method if none exist (and update the
1103/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1104/// methods is the "right" thing to do.
1105void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001106 ObjCContainerDecl *CD,
1107 ObjCPropertyDecl *redeclaredProperty,
1108 ObjCContainerDecl *lexicalDC) {
1109
Ted Kremenek9d64c152010-03-12 00:38:38 +00001110 ObjCMethodDecl *GetterMethod, *SetterMethod;
1111
1112 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1113 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1114 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1115 property->getLocation());
1116
1117 if (SetterMethod) {
1118 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1119 property->getPropertyAttributes();
1120 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1121 Context.getCanonicalType(SetterMethod->getResultType()) !=
1122 Context.VoidTy)
1123 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1124 if (SetterMethod->param_size() != 1 ||
1125 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1126 Diag(property->getLocation(),
1127 diag::warn_accessor_property_type_mismatch)
1128 << property->getDeclName()
1129 << SetterMethod->getSelector();
1130 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1131 }
1132 }
1133
1134 // Synthesize getter/setter methods if none exist.
1135 // Find the default getter and if one not found, add one.
1136 // FIXME: The synthesized property we set here is misleading. We almost always
1137 // synthesize these methods unless the user explicitly provided prototypes
1138 // (which is odd, but allowed). Sema should be typechecking that the
1139 // declarations jive in that situation (which it is not currently).
1140 if (!GetterMethod) {
1141 // No instance method of same name as property getter name was found.
1142 // Declare a getter method and add it to the list of methods
1143 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001144 SourceLocation Loc = redeclaredProperty ?
1145 redeclaredProperty->getLocation() :
1146 property->getLocation();
1147
1148 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1149 property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001150 property->getType(), 0, CD, true, false, true,
1151 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001152 (property->getPropertyImplementation() ==
1153 ObjCPropertyDecl::Optional) ?
1154 ObjCMethodDecl::Optional :
1155 ObjCMethodDecl::Required);
1156 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001157
1158 AddPropertyAttrs(*this, GetterMethod, property);
1159
Ted Kremenek23173d72010-05-18 21:09:07 +00001160 // FIXME: Eventually this shouldn't be needed, as the lexical context
1161 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001162 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001163 GetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001164 } else
1165 // A user declared getter will be synthesize when @synthesize of
1166 // the property with the same name is seen in the @implementation
1167 GetterMethod->setSynthesized(true);
1168 property->setGetterMethodDecl(GetterMethod);
1169
1170 // Skip setter if property is read-only.
1171 if (!property->isReadOnly()) {
1172 // Find the default setter and if one not found, add one.
1173 if (!SetterMethod) {
1174 // No instance method of same name as property setter name was found.
1175 // Declare a setter method and add it to the list of methods
1176 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001177 SourceLocation Loc = redeclaredProperty ?
1178 redeclaredProperty->getLocation() :
1179 property->getLocation();
1180
1181 SetterMethod =
1182 ObjCMethodDecl::Create(Context, Loc, Loc,
1183 property->getSetterName(), Context.VoidTy, 0,
1184 CD, true, false, true, false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001185 (property->getPropertyImplementation() ==
1186 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001187 ObjCMethodDecl::Optional :
1188 ObjCMethodDecl::Required);
1189
Ted Kremenek9d64c152010-03-12 00:38:38 +00001190 // Invent the arguments for the setter. We don't bother making a
1191 // nice name for the argument.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001192 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001193 property->getIdentifier(),
1194 property->getType(),
1195 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001196 SC_None,
1197 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001198 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001199 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001200
1201 AddPropertyAttrs(*this, SetterMethod, property);
1202
Ted Kremenek9d64c152010-03-12 00:38:38 +00001203 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001204 // FIXME: Eventually this shouldn't be needed, as the lexical context
1205 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001206 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001207 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001208 } else
1209 // A user declared setter will be synthesize when @synthesize of
1210 // the property with the same name is seen in the @implementation
1211 SetterMethod->setSynthesized(true);
1212 property->setSetterMethodDecl(SetterMethod);
1213 }
1214 // Add any synthesized methods to the global pool. This allows us to
1215 // handle the following, which is supported by GCC (and part of the design).
1216 //
1217 // @interface Foo
1218 // @property double bar;
1219 // @end
1220 //
1221 // void thisIsUnfortunate() {
1222 // id foo;
1223 // double bar = [foo bar];
1224 // }
1225 //
1226 if (GetterMethod)
1227 AddInstanceMethodToGlobalPool(GetterMethod);
1228 if (SetterMethod)
1229 AddInstanceMethodToGlobalPool(SetterMethod);
1230}
1231
John McCalld226f652010-08-21 09:40:31 +00001232void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001233 SourceLocation Loc,
1234 unsigned &Attributes) {
1235 // FIXME: Improve the reported location.
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001236 if (!PDecl)
1237 return;
1238
1239 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001240 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001241
1242 // readonly and readwrite/assign/retain/copy conflict.
1243 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1244 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1245 ObjCDeclSpec::DQ_PR_assign |
1246 ObjCDeclSpec::DQ_PR_copy |
1247 ObjCDeclSpec::DQ_PR_retain))) {
1248 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1249 "readwrite" :
1250 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1251 "assign" :
1252 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1253 "copy" : "retain";
1254
1255 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1256 diag::err_objc_property_attr_mutually_exclusive :
1257 diag::warn_objc_property_attr_mutually_exclusive)
1258 << "readonly" << which;
1259 }
1260
1261 // Check for copy or retain on non-object types.
1262 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1263 !PropertyTy->isObjCObjectPointerType() &&
1264 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001265 !Context.isObjCNSObjectType(PropertyTy) &&
1266 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001267 Diag(Loc, diag::err_objc_property_requires_object)
1268 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1269 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1270 }
1271
1272 // Check for more than one of { assign, copy, retain }.
1273 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1274 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1275 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1276 << "assign" << "copy";
1277 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1278 }
1279 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1280 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1281 << "assign" << "retain";
1282 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1283 }
1284 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1285 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1286 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1287 << "copy" << "retain";
1288 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1289 }
1290 }
1291
1292 // Warn if user supplied no assignment attribute, property is
1293 // readwrite, and this is an object type.
1294 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1295 ObjCDeclSpec::DQ_PR_retain)) &&
1296 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1297 PropertyTy->isObjCObjectPointerType()) {
1298 // Skip this warning in gc-only mode.
1299 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1300 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1301
1302 // If non-gc code warn that this is likely inappropriate.
1303 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1304 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1305
1306 // FIXME: Implement warning dependent on NSCopying being
1307 // implemented. See also:
1308 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1309 // (please trim this list while you are at it).
1310 }
1311
1312 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1313 && getLangOptions().getGCMode() == LangOptions::GCOnly
1314 && PropertyTy->isBlockPointerType())
1315 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1316}