blob: 37f9db1e5be765a95d32739e817b8a6756a3468a [file] [log] [blame]
Ted Kremenek9d64c152010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
Fariborz Jahanian17cb3262010-05-05 21:52:17 +000018#include "clang/AST/ExprObjC.h"
John McCall50df6ae2010-08-25 07:03:20 +000019#include "llvm/ADT/DenseSet.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000020
21using namespace clang;
22
Ted Kremenek28685ab2010-03-12 00:46:40 +000023//===----------------------------------------------------------------------===//
24// Grammar actions.
25//===----------------------------------------------------------------------===//
26
John McCalld226f652010-08-21 09:40:31 +000027Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
28 FieldDeclarator &FD,
29 ObjCDeclSpec &ODS,
30 Selector GetterSel,
31 Selector SetterSel,
32 Decl *ClassCategory,
33 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000034 tok::ObjCKeywordKind MethodImplKind,
35 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000036 unsigned Attributes = ODS.getPropertyAttributes();
37 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
38 // default is readwrite!
39 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
40 // property is defaulted to 'assign' if it is readwrite and is
41 // not retain or copy
42 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
43 (isReadWrite &&
44 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
45 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000046
John McCallbf1a0282010-06-04 23:28:52 +000047 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
48 QualType T = TSI->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +000049 if (T->isReferenceType()) {
50 Diag(AtLoc, diag::error_reference_property);
John McCalld226f652010-08-21 09:40:31 +000051 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +000052 }
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000053 // Proceed with constructing the ObjCPropertDecls.
54 ObjCContainerDecl *ClassDecl =
John McCalld226f652010-08-21 09:40:31 +000055 cast<ObjCContainerDecl>(ClassCategory);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000056
Ted Kremenek28685ab2010-03-12 00:46:40 +000057 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000058 if (CDecl->IsClassExtension()) {
John McCalld226f652010-08-21 09:40:31 +000059 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000060 FD, GetterSel, SetterSel,
61 isAssign, isReadWrite,
62 Attributes,
63 isOverridingProperty, TSI,
64 MethodImplKind);
65 if (Res)
66 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
67 return Res;
68 }
69
John McCalld226f652010-08-21 09:40:31 +000070 Decl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
71 GetterSel, SetterSel,
72 isAssign, isReadWrite,
73 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000074 if (lexicalDC)
75 Res->setLexicalDeclContext(lexicalDC);
76
Fariborz Jahanian842f07b2010-03-30 22:40:11 +000077 // Validate the attributes on the @property.
78 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
79 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000080}
Ted Kremenek2d2f9362010-03-12 00:49:00 +000081
John McCalld226f652010-08-21 09:40:31 +000082Decl *
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000083Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
84 SourceLocation AtLoc, FieldDeclarator &FD,
85 Selector GetterSel, Selector SetterSel,
86 const bool isAssign,
87 const bool isReadWrite,
88 const unsigned Attributes,
89 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +000090 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000091 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000092
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000093 // Diagnose if this property is already in continuation class.
94 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000095 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenek894ae6a2010-03-15 18:47:25 +000096
Ted Kremenek9f550ff2010-03-15 20:11:46 +000097 if (ObjCPropertyDecl *prevDecl =
98 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000099 Diag(AtLoc, diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000100 Diag(prevDecl->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000101 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000102 }
103
104 // Create a new ObjCPropertyDecl with the DeclContext being
105 // the class extension.
106 ObjCPropertyDecl *PDecl =
107 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
108 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000109 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
110 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
111 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
112 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
113
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000114 DC->addDecl(PDecl);
115
116 // We need to look in the @interface to see if the @property was
117 // already declared.
118 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
119 if (!CCPrimary) {
120 Diag(CDecl->getLocation(), diag::err_continuation_class);
121 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000122 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000123 }
124
125 // Find the property in continuation class's primary class only.
126 ObjCPropertyDecl *PIDecl =
127 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
128
129 if (!PIDecl) {
130 // No matching property found in the primary class. Just fall thru
131 // and add property to continuation class's primary class.
132 ObjCPropertyDecl *PDecl =
133 CreatePropertyDecl(S, CCPrimary, AtLoc,
134 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000135 Attributes, T, MethodImplKind, DC);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000136 // Mark written attribute as having no attribute because
137 // this is not a user-written property declaration in primary
138 // class.
139 PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000140
141 // A case of continuation class adding a new property in the class. This
142 // is not what it was meant for. However, gcc supports it and so should we.
143 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000144 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
145 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000146 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000147 }
148
149 // The property 'PIDecl's readonly attribute will be over-ridden
150 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000151 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000152 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
153 unsigned retainCopyNonatomic =
154 (ObjCPropertyDecl::OBJC_PR_retain |
155 ObjCPropertyDecl::OBJC_PR_copy |
156 ObjCPropertyDecl::OBJC_PR_nonatomic);
157 if ((Attributes & retainCopyNonatomic) !=
158 (PIkind & retainCopyNonatomic)) {
159 Diag(AtLoc, diag::warn_property_attr_mismatch);
160 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000161 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000162 DeclContext *DC = cast<DeclContext>(CCPrimary);
163 if (!ObjCPropertyDecl::findPropertyDecl(DC,
164 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000165 // Protocol is not in the primary class. Must build one for it.
166 ObjCDeclSpec ProtocolPropertyODS;
167 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
168 // and ObjCPropertyDecl::PropertyAttributeKind have identical
169 // values. Should consolidate both into one enum type.
170 ProtocolPropertyODS.
171 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
172 PIkind);
173
John McCalld226f652010-08-21 09:40:31 +0000174 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000175 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
176 PIDecl->getGetterName(),
177 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000178 CCPrimary, isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000179 MethodImplKind,
180 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000181 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000182 }
183 PIDecl->makeitReadWriteAttribute();
184 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
185 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
186 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
187 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
188 PIDecl->setSetterName(SetterSel);
189 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000190 // Tailor the diagnostics for the common case where a readwrite
191 // property is declared both in the @interface and the continuation.
192 // This is a common error where the user often intended the original
193 // declaration to be readonly.
194 unsigned diag =
195 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
196 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
197 ? diag::err_use_continuation_class_redeclaration_readwrite
198 : diag::err_use_continuation_class;
199 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000200 << CCPrimary->getDeclName();
201 Diag(PIDecl->getLocation(), diag::note_property_declare);
202 }
203 *isOverridingProperty = true;
204 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000205 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000206 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000207}
208
209ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
210 ObjCContainerDecl *CDecl,
211 SourceLocation AtLoc,
212 FieldDeclarator &FD,
213 Selector GetterSel,
214 Selector SetterSel,
215 const bool isAssign,
216 const bool isReadWrite,
217 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000218 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000219 tok::ObjCKeywordKind MethodImplKind,
220 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000221 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000222 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000223
224 // Issue a warning if property is 'assign' as default and its object, which is
225 // gc'able conforms to NSCopying protocol
226 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
227 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000228 if (const ObjCObjectPointerType *ObjPtrTy =
229 T->getAs<ObjCObjectPointerType>()) {
230 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
231 if (IDecl)
232 if (ObjCProtocolDecl* PNSCopying =
233 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
234 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
235 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000236 }
John McCallc12c5bb2010-05-15 11:32:37 +0000237 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000238 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
239
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000240 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000241 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
242 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000243 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000244
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000245 if (ObjCPropertyDecl *prevDecl =
246 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000247 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000248 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000249 PDecl->setInvalidDecl();
250 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000251 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000252 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000253 if (lexicalDC)
254 PDecl->setLexicalDeclContext(lexicalDC);
255 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000256
257 if (T->isArrayType() || T->isFunctionType()) {
258 Diag(AtLoc, diag::err_property_type) << T;
259 PDecl->setInvalidDecl();
260 }
261
262 ProcessDeclAttributes(S, PDecl, FD.D);
263
264 // Regardless of setter/getter attribute, we save the default getter/setter
265 // selector names in anticipation of declaration of setter/getter methods.
266 PDecl->setGetterName(GetterSel);
267 PDecl->setSetterName(SetterSel);
268
269 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
270 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
271
272 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
273 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
274
275 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
276 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
277
278 if (isReadWrite)
279 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
280
281 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
282 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
283
284 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
285 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
286
287 if (isAssign)
288 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
289
290 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
291 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
292
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000293 PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes());
294
Ted Kremenek28685ab2010-03-12 00:46:40 +0000295 if (MethodImplKind == tok::objc_required)
296 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
297 else if (MethodImplKind == tok::objc_optional)
298 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000299
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000300 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000301}
302
303
304/// ActOnPropertyImplDecl - This routine performs semantic checks and
305/// builds the AST node for a property implementation declaration; declared
306/// as @synthesize or @dynamic.
307///
John McCalld226f652010-08-21 09:40:31 +0000308Decl *Sema::ActOnPropertyImplDecl(Scope *S,
309 SourceLocation AtLoc,
310 SourceLocation PropertyLoc,
311 bool Synthesize,
312 Decl *ClassCatImpDecl,
313 IdentifierInfo *PropertyId,
314 IdentifierInfo *PropertyIvar) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000315 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000316 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000317 // Make sure we have a context for the property implementation declaration.
318 if (!ClassImpDecl) {
319 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000320 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000321 }
322 ObjCPropertyDecl *property = 0;
323 ObjCInterfaceDecl* IDecl = 0;
324 // Find the class or category class where this property must have
325 // a declaration.
326 ObjCImplementationDecl *IC = 0;
327 ObjCCategoryImplDecl* CatImplClass = 0;
328 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
329 IDecl = IC->getClassInterface();
330 // We always synthesize an interface for an implementation
331 // without an interface decl. So, IDecl is always non-zero.
332 assert(IDecl &&
333 "ActOnPropertyImplDecl - @implementation without @interface");
334
335 // Look for this property declaration in the @implementation's @interface
336 property = IDecl->FindPropertyDeclaration(PropertyId);
337 if (!property) {
338 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000339 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000340 }
341 if (const ObjCCategoryDecl *CD =
342 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
343 if (!CD->IsClassExtension()) {
344 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
345 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000346 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000347 }
348 }
349 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
350 if (Synthesize) {
351 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000352 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000353 }
354 IDecl = CatImplClass->getClassInterface();
355 if (!IDecl) {
356 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000357 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000358 }
359 ObjCCategoryDecl *Category =
360 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
361
362 // If category for this implementation not found, it is an error which
363 // has already been reported eralier.
364 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000365 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000366 // Look for this property declaration in @implementation's category
367 property = Category->FindPropertyDeclaration(PropertyId);
368 if (!property) {
369 Diag(PropertyLoc, diag::error_bad_category_property_decl)
370 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000371 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000372 }
373 } else {
374 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000375 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000376 }
377 ObjCIvarDecl *Ivar = 0;
378 // Check that we have a valid, previously declared ivar for @synthesize
379 if (Synthesize) {
380 // @synthesize
381 if (!PropertyIvar)
382 PropertyIvar = PropertyId;
383 QualType PropType = Context.getCanonicalType(property->getType());
384 // Check that this is a previously declared 'ivar' in 'IDecl' interface
385 ObjCInterfaceDecl *ClassDeclared;
386 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
387 if (!Ivar) {
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000388 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000389 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian2846b2b2010-04-06 23:36:17 +0000390 ObjCIvarDecl::Protected,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000391 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000392 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000393 IDecl->makeDeclVisibleInContext(Ivar, false);
394 property->setPropertyIvarDecl(Ivar);
395
396 if (!getLangOptions().ObjCNonFragileABI)
397 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
398 // Note! I deliberately want it to fall thru so, we have a
399 // a property implementation and to avoid future warnings.
400 } else if (getLangOptions().ObjCNonFragileABI &&
401 ClassDeclared != IDecl) {
402 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
403 << property->getDeclName() << Ivar->getDeclName()
404 << ClassDeclared->getDeclName();
405 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000406 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000407 // Note! I deliberately want it to fall thru so more errors are caught.
408 }
409 QualType IvarType = Context.getCanonicalType(Ivar->getType());
410
411 // Check that type of property and its ivar are type compatible.
412 if (PropType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000413 bool compat = false;
414 if (isa<ObjCObjectPointerType>(PropType)
415 && isa<ObjCObjectPointerType>(IvarType))
416 compat =
417 Context.canAssignObjCInterfaces(
418 PropType->getAs<ObjCObjectPointerType>(),
419 IvarType->getAs<ObjCObjectPointerType>());
420 else
421 compat = (CheckAssignmentConstraints(PropType, IvarType) == Compatible);
422 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000423 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000424 << property->getDeclName() << PropType
425 << Ivar->getDeclName() << IvarType;
426 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000427 // Note! I deliberately want it to fall thru so, we have a
428 // a property implementation and to avoid future warnings.
429 }
430
431 // FIXME! Rules for properties are somewhat different that those
432 // for assignments. Use a new routine to consolidate all cases;
433 // specifically for property redeclarations as well as for ivars.
434 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
435 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
436 if (lhsType != rhsType &&
437 lhsType->isArithmeticType()) {
438 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000439 << property->getDeclName() << PropType
440 << Ivar->getDeclName() << IvarType;
441 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000442 // Fall thru - see previous comment
443 }
444 // __weak is explicit. So it works on Canonical type.
445 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
446 getLangOptions().getGCMode() != LangOptions::NonGC) {
447 Diag(PropertyLoc, diag::error_weak_property)
448 << property->getDeclName() << Ivar->getDeclName();
449 // Fall thru - see previous comment
450 }
451 if ((property->getType()->isObjCObjectPointerType() ||
452 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
453 getLangOptions().getGCMode() != LangOptions::NonGC) {
454 Diag(PropertyLoc, diag::error_strong_property)
455 << property->getDeclName() << Ivar->getDeclName();
456 // Fall thru - see previous comment
457 }
458 }
459 } else if (PropertyIvar)
460 // @dynamic
461 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
462 assert (property && "ActOnPropertyImplDecl - property declaration missing");
463 ObjCPropertyImplDecl *PIDecl =
464 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
465 property,
466 (Synthesize ?
467 ObjCPropertyImplDecl::Synthesize
468 : ObjCPropertyImplDecl::Dynamic),
469 Ivar);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000470 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
471 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000472 if (getLangOptions().CPlusPlus && Synthesize &&
473 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000474 // For Objective-C++, need to synthesize the AST for the IVAR object to be
475 // returned by the getter as it must conform to C++'s copy-return rules.
476 // FIXME. Eventually we want to do this for Objective-C as well.
477 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
478 DeclRefExpr *SelfExpr =
479 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
480 SourceLocation());
481 Expr *IvarRefExpr =
482 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
483 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000484 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000485 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000486 SourceLocation(),
487 getterMethod->getResultType(),
488 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000489 SourceLocation(),
490 Owned(IvarRefExpr));
491 if (!Res.isInvalid()) {
492 Expr *ResExpr = Res.takeAs<Expr>();
493 if (ResExpr)
494 ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr);
495 PIDecl->setGetterCXXConstructor(ResExpr);
496 }
497 }
498 }
499 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
500 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000501 if (getLangOptions().CPlusPlus && Synthesize
502 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000503 // FIXME. Eventually we want to do this for Objective-C as well.
504 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
505 DeclRefExpr *SelfExpr =
506 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
507 SourceLocation());
508 Expr *lhs =
509 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
510 SelfExpr, true, true);
511 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
512 ParmVarDecl *Param = (*P);
513 Expr *rhs = new (Context) DeclRefExpr(Param,Param->getType(),
514 SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000515 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000516 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000517 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
518 }
519 }
520
Ted Kremenek28685ab2010-03-12 00:46:40 +0000521 if (IC) {
522 if (Synthesize)
523 if (ObjCPropertyImplDecl *PPIDecl =
524 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
525 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
526 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
527 << PropertyIvar;
528 Diag(PPIDecl->getLocation(), diag::note_previous_use);
529 }
530
531 if (ObjCPropertyImplDecl *PPIDecl
532 = IC->FindPropertyImplDecl(PropertyId)) {
533 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
534 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000535 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000536 }
537 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000538 if (getLangOptions().ObjCNonFragileABI2) {
539 // Diagnose if an ivar was lazily synthesdized due to a previous
540 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000541 // but it requires an ivar of different name.
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000542 ObjCInterfaceDecl *ClassDeclared;
543 ObjCIvarDecl *Ivar = 0;
544 if (!Synthesize)
545 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
546 else {
547 if (PropertyIvar && PropertyIvar != PropertyId)
548 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
549 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000550 // Issue diagnostics only if Ivar belongs to current class.
551 if (Ivar && Ivar->getSynthesize() &&
552 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000553 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
554 << PropertyId;
555 Ivar->setInvalidDecl();
556 }
557 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000558 } else {
559 if (Synthesize)
560 if (ObjCPropertyImplDecl *PPIDecl =
561 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
562 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
563 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
564 << PropertyIvar;
565 Diag(PPIDecl->getLocation(), diag::note_previous_use);
566 }
567
568 if (ObjCPropertyImplDecl *PPIDecl =
569 CatImplClass->FindPropertyImplDecl(PropertyId)) {
570 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
571 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000572 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000573 }
574 CatImplClass->addPropertyImplementation(PIDecl);
575 }
576
John McCalld226f652010-08-21 09:40:31 +0000577 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000578}
579
580//===----------------------------------------------------------------------===//
581// Helper methods.
582//===----------------------------------------------------------------------===//
583
Ted Kremenek9d64c152010-03-12 00:38:38 +0000584/// DiagnosePropertyMismatch - Compares two properties for their
585/// attributes and types and warns on a variety of inconsistencies.
586///
587void
588Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
589 ObjCPropertyDecl *SuperProperty,
590 const IdentifierInfo *inheritedName) {
591 ObjCPropertyDecl::PropertyAttributeKind CAttr =
592 Property->getPropertyAttributes();
593 ObjCPropertyDecl::PropertyAttributeKind SAttr =
594 SuperProperty->getPropertyAttributes();
595 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
596 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
597 Diag(Property->getLocation(), diag::warn_readonly_property)
598 << Property->getDeclName() << inheritedName;
599 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
600 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
601 Diag(Property->getLocation(), diag::warn_property_attribute)
602 << Property->getDeclName() << "copy" << inheritedName;
603 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
604 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
605 Diag(Property->getLocation(), diag::warn_property_attribute)
606 << Property->getDeclName() << "retain" << inheritedName;
607
608 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
609 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
610 Diag(Property->getLocation(), diag::warn_property_attribute)
611 << Property->getDeclName() << "atomic" << inheritedName;
612 if (Property->getSetterName() != SuperProperty->getSetterName())
613 Diag(Property->getLocation(), diag::warn_property_attribute)
614 << Property->getDeclName() << "setter" << inheritedName;
615 if (Property->getGetterName() != SuperProperty->getGetterName())
616 Diag(Property->getLocation(), diag::warn_property_attribute)
617 << Property->getDeclName() << "getter" << inheritedName;
618
619 QualType LHSType =
620 Context.getCanonicalType(SuperProperty->getType());
621 QualType RHSType =
622 Context.getCanonicalType(Property->getType());
623
624 if (!Context.typesAreCompatible(LHSType, RHSType)) {
625 // FIXME: Incorporate this test with typesAreCompatible.
626 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
627 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
628 return;
629 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
630 << Property->getType() << SuperProperty->getType() << inheritedName;
631 }
632}
633
634bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
635 ObjCMethodDecl *GetterMethod,
636 SourceLocation Loc) {
637 if (GetterMethod &&
638 GetterMethod->getResultType() != property->getType()) {
639 AssignConvertType result = Incompatible;
640 if (property->getType()->isObjCObjectPointerType())
641 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
642 property->getType());
643 if (result != Compatible) {
644 Diag(Loc, diag::warn_accessor_property_type_mismatch)
645 << property->getDeclName()
646 << GetterMethod->getSelector();
647 Diag(GetterMethod->getLocation(), diag::note_declared_at);
648 return true;
649 }
650 }
651 return false;
652}
653
654/// ComparePropertiesInBaseAndSuper - This routine compares property
655/// declarations in base and its super class, if any, and issues
656/// diagnostics in a variety of inconsistant situations.
657///
658void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
659 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
660 if (!SDecl)
661 return;
662 // FIXME: O(N^2)
663 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
664 E = SDecl->prop_end(); S != E; ++S) {
665 ObjCPropertyDecl *SuperPDecl = (*S);
666 // Does property in super class has declaration in current class?
667 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
668 E = IDecl->prop_end(); I != E; ++I) {
669 ObjCPropertyDecl *PDecl = (*I);
670 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
671 DiagnosePropertyMismatch(PDecl, SuperPDecl,
672 SDecl->getIdentifier());
673 }
674 }
675}
676
677/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
678/// of properties declared in a protocol and compares their attribute against
679/// the same property declared in the class or category.
680void
681Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
682 ObjCProtocolDecl *PDecl) {
683 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
684 if (!IDecl) {
685 // Category
686 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
687 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
688 if (!CatDecl->IsClassExtension())
689 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
690 E = PDecl->prop_end(); P != E; ++P) {
691 ObjCPropertyDecl *Pr = (*P);
692 ObjCCategoryDecl::prop_iterator CP, CE;
693 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000694 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000695 if ((*CP)->getIdentifier() == Pr->getIdentifier())
696 break;
697 if (CP != CE)
698 // Property protocol already exist in class. Diagnose any mismatch.
699 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
700 }
701 return;
702 }
703 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
704 E = PDecl->prop_end(); P != E; ++P) {
705 ObjCPropertyDecl *Pr = (*P);
706 ObjCInterfaceDecl::prop_iterator CP, CE;
707 // Is this property already in class's list of properties?
708 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
709 if ((*CP)->getIdentifier() == Pr->getIdentifier())
710 break;
711 if (CP != CE)
712 // Property protocol already exist in class. Diagnose any mismatch.
713 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
714 }
715}
716
717/// CompareProperties - This routine compares properties
718/// declared in 'ClassOrProtocol' objects (which can be a class or an
719/// inherited protocol with the list of properties for class/category 'CDecl'
720///
John McCalld226f652010-08-21 09:40:31 +0000721void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
722 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000723 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
724
725 if (!IDecl) {
726 // Category
727 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
728 assert (CatDecl && "CompareProperties");
729 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
730 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
731 E = MDecl->protocol_end(); P != E; ++P)
732 // Match properties of category with those of protocol (*P)
733 MatchOneProtocolPropertiesInClass(CatDecl, *P);
734
735 // Go thru the list of protocols for this category and recursively match
736 // their properties with those in the category.
737 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
738 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000739 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000740 } else {
741 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
742 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
743 E = MD->protocol_end(); P != E; ++P)
744 MatchOneProtocolPropertiesInClass(CatDecl, *P);
745 }
746 return;
747 }
748
749 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000750 for (ObjCInterfaceDecl::all_protocol_iterator
751 P = MDecl->all_referenced_protocol_begin(),
752 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000753 // Match properties of class IDecl with those of protocol (*P).
754 MatchOneProtocolPropertiesInClass(IDecl, *P);
755
756 // Go thru the list of protocols for this class and recursively match
757 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +0000758 for (ObjCInterfaceDecl::all_protocol_iterator
759 P = IDecl->all_referenced_protocol_begin(),
760 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000761 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000762 } else {
763 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
764 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
765 E = MD->protocol_end(); P != E; ++P)
766 MatchOneProtocolPropertiesInClass(IDecl, *P);
767 }
768}
769
770/// isPropertyReadonly - Return true if property is readonly, by searching
771/// for the property in the class and in its categories and implementations
772///
773bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
774 ObjCInterfaceDecl *IDecl) {
775 // by far the most common case.
776 if (!PDecl->isReadOnly())
777 return false;
778 // Even if property is ready only, if interface has a user defined setter,
779 // it is not considered read only.
780 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
781 return false;
782
783 // Main class has the property as 'readonly'. Must search
784 // through the category list to see if the property's
785 // attribute has been over-ridden to 'readwrite'.
786 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
787 Category; Category = Category->getNextClassCategory()) {
788 // Even if property is ready only, if a category has a user defined setter,
789 // it is not considered read only.
790 if (Category->getInstanceMethod(PDecl->getSetterName()))
791 return false;
792 ObjCPropertyDecl *P =
793 Category->FindPropertyDeclaration(PDecl->getIdentifier());
794 if (P && !P->isReadOnly())
795 return false;
796 }
797
798 // Also, check for definition of a setter method in the implementation if
799 // all else failed.
800 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
801 if (ObjCImplementationDecl *IMD =
802 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
803 if (IMD->getInstanceMethod(PDecl->getSetterName()))
804 return false;
805 } else if (ObjCCategoryImplDecl *CIMD =
806 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
807 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
808 return false;
809 }
810 }
811 // Lastly, look through the implementation (if one is in scope).
812 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
813 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
814 return false;
815 // If all fails, look at the super class.
816 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
817 return isPropertyReadonly(PDecl, SIDecl);
818 return true;
819}
820
821/// CollectImmediateProperties - This routine collects all properties in
822/// the class and its conforming protocols; but not those it its super class.
823void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000824 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
825 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000826 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
827 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
828 E = IDecl->prop_end(); P != E; ++P) {
829 ObjCPropertyDecl *Prop = (*P);
830 PropMap[Prop->getIdentifier()] = Prop;
831 }
832 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000833 for (ObjCInterfaceDecl::all_protocol_iterator
834 PI = IDecl->all_referenced_protocol_begin(),
835 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000836 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000837 }
838 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
839 if (!CATDecl->IsClassExtension())
840 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
841 E = CATDecl->prop_end(); P != E; ++P) {
842 ObjCPropertyDecl *Prop = (*P);
843 PropMap[Prop->getIdentifier()] = Prop;
844 }
845 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000846 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +0000847 E = CATDecl->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 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
851 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
852 E = PDecl->prop_end(); P != E; ++P) {
853 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000854 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
855 // Exclude property for protocols which conform to class's super-class,
856 // as super-class has to implement the property.
857 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
858 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
859 if (!PropEntry)
860 PropEntry = Prop;
861 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000862 }
863 // scan through protocol's protocols.
864 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
865 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000866 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000867 }
868}
869
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000870/// CollectClassPropertyImplementations - This routine collects list of
871/// properties to be implemented in the class. This includes, class's
872/// and its conforming protocols' properties.
873static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
874 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
875 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
876 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
877 E = IDecl->prop_end(); P != E; ++P) {
878 ObjCPropertyDecl *Prop = (*P);
879 PropMap[Prop->getIdentifier()] = Prop;
880 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000881 for (ObjCInterfaceDecl::all_protocol_iterator
882 PI = IDecl->all_referenced_protocol_begin(),
883 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000884 CollectClassPropertyImplementations((*PI), PropMap);
885 }
886 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
887 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
888 E = PDecl->prop_end(); P != E; ++P) {
889 ObjCPropertyDecl *Prop = (*P);
890 PropMap[Prop->getIdentifier()] = Prop;
891 }
892 // scan through protocol's protocols.
893 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
894 E = PDecl->protocol_end(); PI != E; ++PI)
895 CollectClassPropertyImplementations((*PI), PropMap);
896 }
897}
898
899/// CollectSuperClassPropertyImplementations - This routine collects list of
900/// properties to be implemented in super class(s) and also coming from their
901/// conforming protocols.
902static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
903 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
904 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
905 while (SDecl) {
906 CollectClassPropertyImplementations(SDecl, PropMap);
907 SDecl = SDecl->getSuperClass();
908 }
909 }
910}
911
Ted Kremenek9d64c152010-03-12 00:38:38 +0000912/// LookupPropertyDecl - Looks up a property in the current class and all
913/// its protocols.
914ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
915 IdentifierInfo *II) {
916 if (const ObjCInterfaceDecl *IDecl =
917 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
918 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
919 E = IDecl->prop_end(); P != E; ++P) {
920 ObjCPropertyDecl *Prop = (*P);
921 if (Prop->getIdentifier() == II)
922 return Prop;
923 }
924 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000925 for (ObjCInterfaceDecl::all_protocol_iterator
926 PI = IDecl->all_referenced_protocol_begin(),
927 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000928 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
929 if (Prop)
930 return Prop;
931 }
932 }
933 else if (const ObjCProtocolDecl *PDecl =
934 dyn_cast<ObjCProtocolDecl>(CDecl)) {
935 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
936 E = PDecl->prop_end(); P != E; ++P) {
937 ObjCPropertyDecl *Prop = (*P);
938 if (Prop->getIdentifier() == II)
939 return Prop;
940 }
941 // scan through protocol's protocols.
942 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
943 E = PDecl->protocol_end(); PI != E; ++PI) {
944 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
945 if (Prop)
946 return Prop;
947 }
948 }
949 return 0;
950}
951
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000952/// DefaultSynthesizeProperties - This routine default synthesizes all
953/// properties which must be synthesized in class's @implementation.
954void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
955 ObjCInterfaceDecl *IDecl) {
956
957 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
958 CollectClassPropertyImplementations(IDecl, PropMap);
959 if (PropMap.empty())
960 return;
961 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
962 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
963
964 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
965 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
966 ObjCPropertyDecl *Prop = P->second;
967 // If property to be implemented in the super class, ignore.
968 if (SuperPropMap[Prop->getIdentifier()])
969 continue;
970 // Is there a matching propery synthesize/dynamic?
971 if (Prop->isInvalidDecl() ||
972 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
973 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
974 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000975 // Property may have been synthesized by user.
976 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
977 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +0000978 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
979 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
980 continue;
981 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
982 continue;
983 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000984
Ted Kremenek2a6af6b2010-09-24 01:23:01 +0000985
986 // We use invalid SourceLocations for the synthesized ivars since they
987 // aren't really synthesized at a particular location; they just exist.
988 // Saying that they are located at the @implementation isn't really going
989 // to help users.
990 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
991 true,IMPDecl,
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000992 Prop->getIdentifier(), Prop->getIdentifier());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +0000993 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000994}
Ted Kremenek9d64c152010-03-12 00:38:38 +0000995
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000996void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +0000997 ObjCContainerDecl *CDecl,
998 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000999 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1000 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1001 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1002
Ted Kremenek9d64c152010-03-12 00:38:38 +00001003 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001004 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001005 if (PropMap.empty())
1006 return;
1007
1008 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1009 for (ObjCImplDecl::propimpl_iterator
1010 I = IMPDecl->propimpl_begin(),
1011 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1012 PropImplMap.insert((*I)->getPropertyDecl());
1013
1014 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1015 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1016 ObjCPropertyDecl *Prop = P->second;
1017 // Is there a matching propery synthesize/dynamic?
1018 if (Prop->isInvalidDecl() ||
1019 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1020 PropImplMap.count(Prop))
1021 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001022 if (!InsMap.count(Prop->getGetterName())) {
1023 Diag(Prop->getLocation(),
1024 isa<ObjCCategoryDecl>(CDecl) ?
1025 diag::warn_setter_getter_impl_required_in_category :
1026 diag::warn_setter_getter_impl_required)
1027 << Prop->getDeclName() << Prop->getGetterName();
1028 Diag(IMPDecl->getLocation(),
1029 diag::note_property_impl_required);
1030 }
1031
1032 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
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->getSetterName();
1038 Diag(IMPDecl->getLocation(),
1039 diag::note_property_impl_required);
1040 }
1041 }
1042}
1043
1044void
1045Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1046 ObjCContainerDecl* IDecl) {
1047 // Rules apply in non-GC mode only
1048 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1049 return;
1050 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1051 E = IDecl->prop_end();
1052 I != E; ++I) {
1053 ObjCPropertyDecl *Property = (*I);
1054 unsigned Attributes = Property->getPropertyAttributes();
1055 // We only care about readwrite atomic property.
1056 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1057 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1058 continue;
1059 if (const ObjCPropertyImplDecl *PIDecl
1060 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1061 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1062 continue;
1063 ObjCMethodDecl *GetterMethod =
1064 IMPDecl->getInstanceMethod(Property->getGetterName());
1065 ObjCMethodDecl *SetterMethod =
1066 IMPDecl->getInstanceMethod(Property->getSetterName());
1067 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1068 SourceLocation MethodLoc =
1069 (GetterMethod ? GetterMethod->getLocation()
1070 : SetterMethod->getLocation());
1071 Diag(MethodLoc, diag::warn_atomic_property_rule)
1072 << Property->getIdentifier();
1073 Diag(Property->getLocation(), diag::note_property_declare);
1074 }
1075 }
1076 }
1077}
1078
John McCall5de74d12010-11-10 07:01:40 +00001079/// AddPropertyAttrs - Propagates attributes from a property to the
1080/// implicitly-declared getter or setter for that property.
1081static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1082 ObjCPropertyDecl *Property) {
1083 // Should we just clone all attributes over?
1084 if (DeprecatedAttr *A = Property->getAttr<DeprecatedAttr>())
1085 PropertyMethod->addAttr(A->clone(S.Context));
1086 if (UnavailableAttr *A = Property->getAttr<UnavailableAttr>())
1087 PropertyMethod->addAttr(A->clone(S.Context));
1088}
1089
Ted Kremenek9d64c152010-03-12 00:38:38 +00001090/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1091/// have the property type and issue diagnostics if they don't.
1092/// Also synthesize a getter/setter method if none exist (and update the
1093/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1094/// methods is the "right" thing to do.
1095void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001096 ObjCContainerDecl *CD,
1097 ObjCPropertyDecl *redeclaredProperty,
1098 ObjCContainerDecl *lexicalDC) {
1099
Ted Kremenek9d64c152010-03-12 00:38:38 +00001100 ObjCMethodDecl *GetterMethod, *SetterMethod;
1101
1102 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1103 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1104 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1105 property->getLocation());
1106
1107 if (SetterMethod) {
1108 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1109 property->getPropertyAttributes();
1110 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1111 Context.getCanonicalType(SetterMethod->getResultType()) !=
1112 Context.VoidTy)
1113 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1114 if (SetterMethod->param_size() != 1 ||
1115 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1116 Diag(property->getLocation(),
1117 diag::warn_accessor_property_type_mismatch)
1118 << property->getDeclName()
1119 << SetterMethod->getSelector();
1120 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1121 }
1122 }
1123
1124 // Synthesize getter/setter methods if none exist.
1125 // Find the default getter and if one not found, add one.
1126 // FIXME: The synthesized property we set here is misleading. We almost always
1127 // synthesize these methods unless the user explicitly provided prototypes
1128 // (which is odd, but allowed). Sema should be typechecking that the
1129 // declarations jive in that situation (which it is not currently).
1130 if (!GetterMethod) {
1131 // No instance method of same name as property getter name was found.
1132 // Declare a getter method and add it to the list of methods
1133 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001134 SourceLocation Loc = redeclaredProperty ?
1135 redeclaredProperty->getLocation() :
1136 property->getLocation();
1137
1138 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1139 property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001140 property->getType(), 0, CD, true, false, true,
1141 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001142 (property->getPropertyImplementation() ==
1143 ObjCPropertyDecl::Optional) ?
1144 ObjCMethodDecl::Optional :
1145 ObjCMethodDecl::Required);
1146 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001147
1148 AddPropertyAttrs(*this, GetterMethod, property);
1149
Ted Kremenek23173d72010-05-18 21:09:07 +00001150 // FIXME: Eventually this shouldn't be needed, as the lexical context
1151 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001152 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001153 GetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001154 } else
1155 // A user declared getter will be synthesize when @synthesize of
1156 // the property with the same name is seen in the @implementation
1157 GetterMethod->setSynthesized(true);
1158 property->setGetterMethodDecl(GetterMethod);
1159
1160 // Skip setter if property is read-only.
1161 if (!property->isReadOnly()) {
1162 // Find the default setter and if one not found, add one.
1163 if (!SetterMethod) {
1164 // No instance method of same name as property setter name was found.
1165 // Declare a setter method and add it to the list of methods
1166 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001167 SourceLocation Loc = redeclaredProperty ?
1168 redeclaredProperty->getLocation() :
1169 property->getLocation();
1170
1171 SetterMethod =
1172 ObjCMethodDecl::Create(Context, Loc, Loc,
1173 property->getSetterName(), Context.VoidTy, 0,
1174 CD, true, false, true, false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001175 (property->getPropertyImplementation() ==
1176 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001177 ObjCMethodDecl::Optional :
1178 ObjCMethodDecl::Required);
1179
Ted Kremenek9d64c152010-03-12 00:38:38 +00001180 // Invent the arguments for the setter. We don't bother making a
1181 // nice name for the argument.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001182 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001183 property->getIdentifier(),
1184 property->getType(),
1185 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001186 SC_None,
1187 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001188 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001189 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001190
1191 AddPropertyAttrs(*this, SetterMethod, property);
1192
Ted Kremenek9d64c152010-03-12 00:38:38 +00001193 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001194 // FIXME: Eventually this shouldn't be needed, as the lexical context
1195 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001196 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001197 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001198 } else
1199 // A user declared setter will be synthesize when @synthesize of
1200 // the property with the same name is seen in the @implementation
1201 SetterMethod->setSynthesized(true);
1202 property->setSetterMethodDecl(SetterMethod);
1203 }
1204 // Add any synthesized methods to the global pool. This allows us to
1205 // handle the following, which is supported by GCC (and part of the design).
1206 //
1207 // @interface Foo
1208 // @property double bar;
1209 // @end
1210 //
1211 // void thisIsUnfortunate() {
1212 // id foo;
1213 // double bar = [foo bar];
1214 // }
1215 //
1216 if (GetterMethod)
1217 AddInstanceMethodToGlobalPool(GetterMethod);
1218 if (SetterMethod)
1219 AddInstanceMethodToGlobalPool(SetterMethod);
1220}
1221
John McCalld226f652010-08-21 09:40:31 +00001222void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001223 SourceLocation Loc,
1224 unsigned &Attributes) {
1225 // FIXME: Improve the reported location.
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001226 if (!PDecl)
1227 return;
1228
1229 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001230 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001231
1232 // readonly and readwrite/assign/retain/copy conflict.
1233 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1234 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1235 ObjCDeclSpec::DQ_PR_assign |
1236 ObjCDeclSpec::DQ_PR_copy |
1237 ObjCDeclSpec::DQ_PR_retain))) {
1238 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1239 "readwrite" :
1240 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1241 "assign" :
1242 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1243 "copy" : "retain";
1244
1245 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1246 diag::err_objc_property_attr_mutually_exclusive :
1247 diag::warn_objc_property_attr_mutually_exclusive)
1248 << "readonly" << which;
1249 }
1250
1251 // Check for copy or retain on non-object types.
1252 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1253 !PropertyTy->isObjCObjectPointerType() &&
1254 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001255 !Context.isObjCNSObjectType(PropertyTy) &&
1256 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001257 Diag(Loc, diag::err_objc_property_requires_object)
1258 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1259 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1260 }
1261
1262 // Check for more than one of { assign, copy, retain }.
1263 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1264 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1265 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1266 << "assign" << "copy";
1267 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1268 }
1269 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1270 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1271 << "assign" << "retain";
1272 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1273 }
1274 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1275 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1276 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1277 << "copy" << "retain";
1278 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1279 }
1280 }
1281
1282 // Warn if user supplied no assignment attribute, property is
1283 // readwrite, and this is an object type.
1284 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1285 ObjCDeclSpec::DQ_PR_retain)) &&
1286 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1287 PropertyTy->isObjCObjectPointerType()) {
1288 // Skip this warning in gc-only mode.
1289 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1290 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1291
1292 // If non-gc code warn that this is likely inappropriate.
1293 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1294 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1295
1296 // FIXME: Implement warning dependent on NSCopying being
1297 // implemented. See also:
1298 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1299 // (please trim this list while you are at it).
1300 }
1301
1302 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1303 && getLangOptions().getGCMode() == LangOptions::GCOnly
1304 && PropertyTy->isBlockPointerType())
1305 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1306}