blob: 43e30275e2ff4bb03c263af35a91a4e7c728269a [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);
Fariborz Jahanian14086762011-03-28 23:47:18 +000048
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000049 // Proceed with constructing the ObjCPropertDecls.
50 ObjCContainerDecl *ClassDecl =
John McCalld226f652010-08-21 09:40:31 +000051 cast<ObjCContainerDecl>(ClassCategory);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000052
Ted Kremenek28685ab2010-03-12 00:46:40 +000053 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000054 if (CDecl->IsClassExtension()) {
John McCalld226f652010-08-21 09:40:31 +000055 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000056 FD, GetterSel, SetterSel,
57 isAssign, isReadWrite,
58 Attributes,
59 isOverridingProperty, TSI,
60 MethodImplKind);
61 if (Res)
62 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
63 return Res;
64 }
65
John McCalld226f652010-08-21 09:40:31 +000066 Decl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
67 GetterSel, SetterSel,
68 isAssign, isReadWrite,
69 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000070 if (lexicalDC)
71 Res->setLexicalDeclContext(lexicalDC);
72
Fariborz Jahanian842f07b2010-03-30 22:40:11 +000073 // Validate the attributes on the @property.
74 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
75 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000076}
Ted Kremenek2d2f9362010-03-12 00:49:00 +000077
John McCalld226f652010-08-21 09:40:31 +000078Decl *
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000079Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
80 SourceLocation AtLoc, FieldDeclarator &FD,
81 Selector GetterSel, Selector SetterSel,
82 const bool isAssign,
83 const bool isReadWrite,
84 const unsigned Attributes,
85 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +000086 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000087 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000088
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000089 // Diagnose if this property is already in continuation class.
90 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000091 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +000092 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
93
94 if (CCPrimary)
95 // Check for duplicate declaration of this property in current and
96 // other class extensions.
97 for (const ObjCCategoryDecl *ClsExtDecl =
98 CCPrimary->getFirstClassExtension();
99 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
100 if (ObjCPropertyDecl *prevDecl =
101 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
102 Diag(AtLoc, diag::err_duplicate_property);
103 Diag(prevDecl->getLocation(), diag::note_property_declare);
104 return 0;
105 }
106 }
107
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000108 // Create a new ObjCPropertyDecl with the DeclContext being
109 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000110 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000111 ObjCPropertyDecl *PDecl =
112 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
113 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000114 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
115 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
116 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
117 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000118 // Set setter/getter selector name. Needed later.
119 PDecl->setGetterName(GetterSel);
120 PDecl->setSetterName(SetterSel);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000121 DC->addDecl(PDecl);
122
123 // We need to look in the @interface to see if the @property was
124 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000125 if (!CCPrimary) {
126 Diag(CDecl->getLocation(), diag::err_continuation_class);
127 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000128 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000129 }
130
131 // Find the property in continuation class's primary class only.
132 ObjCPropertyDecl *PIDecl =
133 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
134
135 if (!PIDecl) {
136 // No matching property found in the primary class. Just fall thru
137 // and add property to continuation class's primary class.
138 ObjCPropertyDecl *PDecl =
139 CreatePropertyDecl(S, CCPrimary, AtLoc,
140 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000141 Attributes, T, MethodImplKind, DC);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000142 // Mark written attribute as having no attribute because
143 // this is not a user-written property declaration in primary
144 // class.
145 PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000146
147 // A case of continuation class adding a new property in the class. This
148 // is not what it was meant for. However, gcc supports it and so should we.
149 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000150 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
151 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000152 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000153 }
154
155 // The property 'PIDecl's readonly attribute will be over-ridden
156 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000157 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000158 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
159 unsigned retainCopyNonatomic =
160 (ObjCPropertyDecl::OBJC_PR_retain |
161 ObjCPropertyDecl::OBJC_PR_copy |
162 ObjCPropertyDecl::OBJC_PR_nonatomic);
163 if ((Attributes & retainCopyNonatomic) !=
164 (PIkind & retainCopyNonatomic)) {
165 Diag(AtLoc, diag::warn_property_attr_mismatch);
166 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000167 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000168 DeclContext *DC = cast<DeclContext>(CCPrimary);
169 if (!ObjCPropertyDecl::findPropertyDecl(DC,
170 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000171 // Protocol is not in the primary class. Must build one for it.
172 ObjCDeclSpec ProtocolPropertyODS;
173 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
174 // and ObjCPropertyDecl::PropertyAttributeKind have identical
175 // values. Should consolidate both into one enum type.
176 ProtocolPropertyODS.
177 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
178 PIkind);
179
John McCalld226f652010-08-21 09:40:31 +0000180 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000181 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
182 PIDecl->getGetterName(),
183 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000184 CCPrimary, isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000185 MethodImplKind,
186 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000187 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000188 }
189 PIDecl->makeitReadWriteAttribute();
190 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
191 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
192 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
193 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
194 PIDecl->setSetterName(SetterSel);
195 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000196 // Tailor the diagnostics for the common case where a readwrite
197 // property is declared both in the @interface and the continuation.
198 // This is a common error where the user often intended the original
199 // declaration to be readonly.
200 unsigned diag =
201 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
202 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
203 ? diag::err_use_continuation_class_redeclaration_readwrite
204 : diag::err_use_continuation_class;
205 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000206 << CCPrimary->getDeclName();
207 Diag(PIDecl->getLocation(), diag::note_property_declare);
208 }
209 *isOverridingProperty = true;
210 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000211 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000212 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000213}
214
215ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
216 ObjCContainerDecl *CDecl,
217 SourceLocation AtLoc,
218 FieldDeclarator &FD,
219 Selector GetterSel,
220 Selector SetterSel,
221 const bool isAssign,
222 const bool isReadWrite,
223 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000224 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000225 tok::ObjCKeywordKind MethodImplKind,
226 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000227 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000228 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000229
230 // Issue a warning if property is 'assign' as default and its object, which is
231 // gc'able conforms to NSCopying protocol
232 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
233 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000234 if (const ObjCObjectPointerType *ObjPtrTy =
235 T->getAs<ObjCObjectPointerType>()) {
236 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
237 if (IDecl)
238 if (ObjCProtocolDecl* PNSCopying =
239 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
240 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
241 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000242 }
John McCallc12c5bb2010-05-15 11:32:37 +0000243 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000244 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
245
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000246 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000247 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
248 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000249 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000250
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000251 if (ObjCPropertyDecl *prevDecl =
252 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000253 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000254 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000255 PDecl->setInvalidDecl();
256 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000257 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000258 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000259 if (lexicalDC)
260 PDecl->setLexicalDeclContext(lexicalDC);
261 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000262
263 if (T->isArrayType() || T->isFunctionType()) {
264 Diag(AtLoc, diag::err_property_type) << T;
265 PDecl->setInvalidDecl();
266 }
267
268 ProcessDeclAttributes(S, PDecl, FD.D);
269
270 // Regardless of setter/getter attribute, we save the default getter/setter
271 // selector names in anticipation of declaration of setter/getter methods.
272 PDecl->setGetterName(GetterSel);
273 PDecl->setSetterName(SetterSel);
274
275 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
276 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
277
278 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
279 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
280
281 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
282 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
283
284 if (isReadWrite)
285 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
286
287 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
288 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
289
290 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
291 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
292
293 if (isAssign)
294 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
295
296 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
297 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000298 else if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
299 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000300
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000301 PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes());
302
Ted Kremenek28685ab2010-03-12 00:46:40 +0000303 if (MethodImplKind == tok::objc_required)
304 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
305 else if (MethodImplKind == tok::objc_optional)
306 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000307
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000308 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000309}
310
311
312/// ActOnPropertyImplDecl - This routine performs semantic checks and
313/// builds the AST node for a property implementation declaration; declared
314/// as @synthesize or @dynamic.
315///
John McCalld226f652010-08-21 09:40:31 +0000316Decl *Sema::ActOnPropertyImplDecl(Scope *S,
317 SourceLocation AtLoc,
318 SourceLocation PropertyLoc,
319 bool Synthesize,
320 Decl *ClassCatImpDecl,
321 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000322 IdentifierInfo *PropertyIvar,
323 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000324 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000325 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000326 // Make sure we have a context for the property implementation declaration.
327 if (!ClassImpDecl) {
328 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000329 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000330 }
331 ObjCPropertyDecl *property = 0;
332 ObjCInterfaceDecl* IDecl = 0;
333 // Find the class or category class where this property must have
334 // a declaration.
335 ObjCImplementationDecl *IC = 0;
336 ObjCCategoryImplDecl* CatImplClass = 0;
337 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
338 IDecl = IC->getClassInterface();
339 // We always synthesize an interface for an implementation
340 // without an interface decl. So, IDecl is always non-zero.
341 assert(IDecl &&
342 "ActOnPropertyImplDecl - @implementation without @interface");
343
344 // Look for this property declaration in the @implementation's @interface
345 property = IDecl->FindPropertyDeclaration(PropertyId);
346 if (!property) {
347 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000348 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000349 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000350 unsigned PIkind = property->getPropertyAttributesAsWritten();
351 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
352 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
353 if (AtLoc.isValid())
354 Diag(AtLoc, diag::warn_implicit_atomic_property);
355 else
356 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
357 Diag(property->getLocation(), diag::note_property_declare);
358 }
359
Ted Kremenek28685ab2010-03-12 00:46:40 +0000360 if (const ObjCCategoryDecl *CD =
361 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
362 if (!CD->IsClassExtension()) {
363 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
364 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000365 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000366 }
367 }
368 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
369 if (Synthesize) {
370 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000371 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000372 }
373 IDecl = CatImplClass->getClassInterface();
374 if (!IDecl) {
375 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000376 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000377 }
378 ObjCCategoryDecl *Category =
379 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
380
381 // If category for this implementation not found, it is an error which
382 // has already been reported eralier.
383 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000384 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000385 // Look for this property declaration in @implementation's category
386 property = Category->FindPropertyDeclaration(PropertyId);
387 if (!property) {
388 Diag(PropertyLoc, diag::error_bad_category_property_decl)
389 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000390 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000391 }
392 } else {
393 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000394 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000395 }
396 ObjCIvarDecl *Ivar = 0;
397 // Check that we have a valid, previously declared ivar for @synthesize
398 if (Synthesize) {
399 // @synthesize
400 if (!PropertyIvar)
401 PropertyIvar = PropertyId;
402 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian14086762011-03-28 23:47:18 +0000403 QualType PropertyIvarType = PropType;
404 if (PropType->isReferenceType())
405 PropertyIvarType = cast<ReferenceType>(PropType)->getPointeeType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000406 // Check that this is a previously declared 'ivar' in 'IDecl' interface
407 ObjCInterfaceDecl *ClassDeclared;
408 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
409 if (!Ivar) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000410 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
411 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000412 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000413 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000414 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000415 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000416 IDecl->makeDeclVisibleInContext(Ivar, false);
417 property->setPropertyIvarDecl(Ivar);
418
419 if (!getLangOptions().ObjCNonFragileABI)
420 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
421 // Note! I deliberately want it to fall thru so, we have a
422 // a property implementation and to avoid future warnings.
423 } else if (getLangOptions().ObjCNonFragileABI &&
424 ClassDeclared != IDecl) {
425 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
426 << property->getDeclName() << Ivar->getDeclName()
427 << ClassDeclared->getDeclName();
428 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000429 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000430 // Note! I deliberately want it to fall thru so more errors are caught.
431 }
432 QualType IvarType = Context.getCanonicalType(Ivar->getType());
433
434 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000435 if (PropertyIvarType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000436 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000437 if (isa<ObjCObjectPointerType>(PropertyIvarType)
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000438 && isa<ObjCObjectPointerType>(IvarType))
439 compat =
440 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000441 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000442 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000443 else {
444 SourceLocation Loc = PropertyIvarLoc;
445 if (Loc.isInvalid())
446 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000447 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000448 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000449 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000450 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000451 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000452 << property->getDeclName() << PropType
453 << Ivar->getDeclName() << IvarType;
454 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000455 // Note! I deliberately want it to fall thru so, we have a
456 // a property implementation and to avoid future warnings.
457 }
458
459 // FIXME! Rules for properties are somewhat different that those
460 // for assignments. Use a new routine to consolidate all cases;
461 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000462 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000463 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
464 if (lhsType != rhsType &&
465 lhsType->isArithmeticType()) {
466 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000467 << property->getDeclName() << PropType
468 << Ivar->getDeclName() << IvarType;
469 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000470 // Fall thru - see previous comment
471 }
472 // __weak is explicit. So it works on Canonical type.
473 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
474 getLangOptions().getGCMode() != LangOptions::NonGC) {
475 Diag(PropertyLoc, diag::error_weak_property)
476 << property->getDeclName() << Ivar->getDeclName();
477 // Fall thru - see previous comment
478 }
479 if ((property->getType()->isObjCObjectPointerType() ||
480 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
481 getLangOptions().getGCMode() != LangOptions::NonGC) {
482 Diag(PropertyLoc, diag::error_strong_property)
483 << property->getDeclName() << Ivar->getDeclName();
484 // Fall thru - see previous comment
485 }
486 }
487 } else if (PropertyIvar)
488 // @dynamic
489 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
490 assert (property && "ActOnPropertyImplDecl - property declaration missing");
491 ObjCPropertyImplDecl *PIDecl =
492 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
493 property,
494 (Synthesize ?
495 ObjCPropertyImplDecl::Synthesize
496 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000497 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000498 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
499 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000500 if (getLangOptions().CPlusPlus && Synthesize &&
501 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000502 // For Objective-C++, need to synthesize the AST for the IVAR object to be
503 // returned by the getter as it must conform to C++'s copy-return rules.
504 // FIXME. Eventually we want to do this for Objective-C as well.
505 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
506 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000507 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
508 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000509 Expr *IvarRefExpr =
510 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
511 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000512 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000513 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000514 SourceLocation(),
515 getterMethod->getResultType(),
516 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000517 SourceLocation(),
518 Owned(IvarRefExpr));
519 if (!Res.isInvalid()) {
520 Expr *ResExpr = Res.takeAs<Expr>();
521 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000522 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000523 PIDecl->setGetterCXXConstructor(ResExpr);
524 }
525 }
526 }
527 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
528 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000529 if (getLangOptions().CPlusPlus && Synthesize
530 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000531 // FIXME. Eventually we want to do this for Objective-C as well.
532 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
533 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000534 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
535 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000536 Expr *lhs =
537 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
538 SelfExpr, true, true);
539 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
540 ParmVarDecl *Param = (*P);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000541 QualType T = Param->getType();
542 if (T->isReferenceType())
543 T = cast<ReferenceType>(T)->getPointeeType();
544 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000545 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000546 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000547 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000548 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
549 }
550 }
551
Ted Kremenek28685ab2010-03-12 00:46:40 +0000552 if (IC) {
553 if (Synthesize)
554 if (ObjCPropertyImplDecl *PPIDecl =
555 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
556 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
557 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
558 << PropertyIvar;
559 Diag(PPIDecl->getLocation(), diag::note_previous_use);
560 }
561
562 if (ObjCPropertyImplDecl *PPIDecl
563 = IC->FindPropertyImplDecl(PropertyId)) {
564 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
565 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000566 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000567 }
568 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000569 if (getLangOptions().ObjCDefaultSynthProperties &&
570 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000571 // Diagnose if an ivar was lazily synthesdized due to a previous
572 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000573 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000574 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000575 ObjCIvarDecl *Ivar = 0;
576 if (!Synthesize)
577 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
578 else {
579 if (PropertyIvar && PropertyIvar != PropertyId)
580 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
581 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000582 // Issue diagnostics only if Ivar belongs to current class.
583 if (Ivar && Ivar->getSynthesize() &&
584 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000585 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
586 << PropertyId;
587 Ivar->setInvalidDecl();
588 }
589 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000590 } else {
591 if (Synthesize)
592 if (ObjCPropertyImplDecl *PPIDecl =
593 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
594 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
595 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
596 << PropertyIvar;
597 Diag(PPIDecl->getLocation(), diag::note_previous_use);
598 }
599
600 if (ObjCPropertyImplDecl *PPIDecl =
601 CatImplClass->FindPropertyImplDecl(PropertyId)) {
602 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
603 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000604 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000605 }
606 CatImplClass->addPropertyImplementation(PIDecl);
607 }
608
John McCalld226f652010-08-21 09:40:31 +0000609 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000610}
611
612//===----------------------------------------------------------------------===//
613// Helper methods.
614//===----------------------------------------------------------------------===//
615
Ted Kremenek9d64c152010-03-12 00:38:38 +0000616/// DiagnosePropertyMismatch - Compares two properties for their
617/// attributes and types and warns on a variety of inconsistencies.
618///
619void
620Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
621 ObjCPropertyDecl *SuperProperty,
622 const IdentifierInfo *inheritedName) {
623 ObjCPropertyDecl::PropertyAttributeKind CAttr =
624 Property->getPropertyAttributes();
625 ObjCPropertyDecl::PropertyAttributeKind SAttr =
626 SuperProperty->getPropertyAttributes();
627 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
628 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
629 Diag(Property->getLocation(), diag::warn_readonly_property)
630 << Property->getDeclName() << inheritedName;
631 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
632 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
633 Diag(Property->getLocation(), diag::warn_property_attribute)
634 << Property->getDeclName() << "copy" << inheritedName;
635 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
636 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
637 Diag(Property->getLocation(), diag::warn_property_attribute)
638 << Property->getDeclName() << "retain" << inheritedName;
639
640 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
641 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
642 Diag(Property->getLocation(), diag::warn_property_attribute)
643 << Property->getDeclName() << "atomic" << inheritedName;
644 if (Property->getSetterName() != SuperProperty->getSetterName())
645 Diag(Property->getLocation(), diag::warn_property_attribute)
646 << Property->getDeclName() << "setter" << inheritedName;
647 if (Property->getGetterName() != SuperProperty->getGetterName())
648 Diag(Property->getLocation(), diag::warn_property_attribute)
649 << Property->getDeclName() << "getter" << inheritedName;
650
651 QualType LHSType =
652 Context.getCanonicalType(SuperProperty->getType());
653 QualType RHSType =
654 Context.getCanonicalType(Property->getType());
655
656 if (!Context.typesAreCompatible(LHSType, RHSType)) {
657 // FIXME: Incorporate this test with typesAreCompatible.
658 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
659 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
660 return;
661 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
662 << Property->getType() << SuperProperty->getType() << inheritedName;
663 }
664}
665
666bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
667 ObjCMethodDecl *GetterMethod,
668 SourceLocation Loc) {
669 if (GetterMethod &&
670 GetterMethod->getResultType() != property->getType()) {
671 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000672 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000673 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000674 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000675 if (result != Compatible) {
676 Diag(Loc, diag::warn_accessor_property_type_mismatch)
677 << property->getDeclName()
678 << GetterMethod->getSelector();
679 Diag(GetterMethod->getLocation(), diag::note_declared_at);
680 return true;
681 }
682 }
683 return false;
684}
685
686/// ComparePropertiesInBaseAndSuper - This routine compares property
687/// declarations in base and its super class, if any, and issues
688/// diagnostics in a variety of inconsistant situations.
689///
690void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
691 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
692 if (!SDecl)
693 return;
694 // FIXME: O(N^2)
695 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
696 E = SDecl->prop_end(); S != E; ++S) {
697 ObjCPropertyDecl *SuperPDecl = (*S);
698 // Does property in super class has declaration in current class?
699 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
700 E = IDecl->prop_end(); I != E; ++I) {
701 ObjCPropertyDecl *PDecl = (*I);
702 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
703 DiagnosePropertyMismatch(PDecl, SuperPDecl,
704 SDecl->getIdentifier());
705 }
706 }
707}
708
709/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
710/// of properties declared in a protocol and compares their attribute against
711/// the same property declared in the class or category.
712void
713Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
714 ObjCProtocolDecl *PDecl) {
715 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
716 if (!IDecl) {
717 // Category
718 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
719 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
720 if (!CatDecl->IsClassExtension())
721 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
722 E = PDecl->prop_end(); P != E; ++P) {
723 ObjCPropertyDecl *Pr = (*P);
724 ObjCCategoryDecl::prop_iterator CP, CE;
725 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000726 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000727 if ((*CP)->getIdentifier() == Pr->getIdentifier())
728 break;
729 if (CP != CE)
730 // Property protocol already exist in class. Diagnose any mismatch.
731 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
732 }
733 return;
734 }
735 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
736 E = PDecl->prop_end(); P != E; ++P) {
737 ObjCPropertyDecl *Pr = (*P);
738 ObjCInterfaceDecl::prop_iterator CP, CE;
739 // Is this property already in class's list of properties?
740 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
741 if ((*CP)->getIdentifier() == Pr->getIdentifier())
742 break;
743 if (CP != CE)
744 // Property protocol already exist in class. Diagnose any mismatch.
745 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
746 }
747}
748
749/// CompareProperties - This routine compares properties
750/// declared in 'ClassOrProtocol' objects (which can be a class or an
751/// inherited protocol with the list of properties for class/category 'CDecl'
752///
John McCalld226f652010-08-21 09:40:31 +0000753void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
754 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000755 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
756
757 if (!IDecl) {
758 // Category
759 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
760 assert (CatDecl && "CompareProperties");
761 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
762 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
763 E = MDecl->protocol_end(); P != E; ++P)
764 // Match properties of category with those of protocol (*P)
765 MatchOneProtocolPropertiesInClass(CatDecl, *P);
766
767 // Go thru the list of protocols for this category and recursively match
768 // their properties with those in the category.
769 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
770 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000771 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000772 } else {
773 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
774 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
775 E = MD->protocol_end(); P != E; ++P)
776 MatchOneProtocolPropertiesInClass(CatDecl, *P);
777 }
778 return;
779 }
780
781 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000782 for (ObjCInterfaceDecl::all_protocol_iterator
783 P = MDecl->all_referenced_protocol_begin(),
784 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000785 // Match properties of class IDecl with those of protocol (*P).
786 MatchOneProtocolPropertiesInClass(IDecl, *P);
787
788 // Go thru the list of protocols for this class and recursively match
789 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +0000790 for (ObjCInterfaceDecl::all_protocol_iterator
791 P = IDecl->all_referenced_protocol_begin(),
792 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000793 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000794 } else {
795 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
796 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
797 E = MD->protocol_end(); P != E; ++P)
798 MatchOneProtocolPropertiesInClass(IDecl, *P);
799 }
800}
801
802/// isPropertyReadonly - Return true if property is readonly, by searching
803/// for the property in the class and in its categories and implementations
804///
805bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
806 ObjCInterfaceDecl *IDecl) {
807 // by far the most common case.
808 if (!PDecl->isReadOnly())
809 return false;
810 // Even if property is ready only, if interface has a user defined setter,
811 // it is not considered read only.
812 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
813 return false;
814
815 // Main class has the property as 'readonly'. Must search
816 // through the category list to see if the property's
817 // attribute has been over-ridden to 'readwrite'.
818 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
819 Category; Category = Category->getNextClassCategory()) {
820 // Even if property is ready only, if a category has a user defined setter,
821 // it is not considered read only.
822 if (Category->getInstanceMethod(PDecl->getSetterName()))
823 return false;
824 ObjCPropertyDecl *P =
825 Category->FindPropertyDeclaration(PDecl->getIdentifier());
826 if (P && !P->isReadOnly())
827 return false;
828 }
829
830 // Also, check for definition of a setter method in the implementation if
831 // all else failed.
832 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
833 if (ObjCImplementationDecl *IMD =
834 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
835 if (IMD->getInstanceMethod(PDecl->getSetterName()))
836 return false;
837 } else if (ObjCCategoryImplDecl *CIMD =
838 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
839 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
840 return false;
841 }
842 }
843 // Lastly, look through the implementation (if one is in scope).
844 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
845 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
846 return false;
847 // If all fails, look at the super class.
848 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
849 return isPropertyReadonly(PDecl, SIDecl);
850 return true;
851}
852
853/// CollectImmediateProperties - This routine collects all properties in
854/// the class and its conforming protocols; but not those it its super class.
855void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000856 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
857 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000858 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
859 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
860 E = IDecl->prop_end(); P != E; ++P) {
861 ObjCPropertyDecl *Prop = (*P);
862 PropMap[Prop->getIdentifier()] = Prop;
863 }
864 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000865 for (ObjCInterfaceDecl::all_protocol_iterator
866 PI = IDecl->all_referenced_protocol_begin(),
867 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000868 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000869 }
870 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
871 if (!CATDecl->IsClassExtension())
872 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
873 E = CATDecl->prop_end(); P != E; ++P) {
874 ObjCPropertyDecl *Prop = (*P);
875 PropMap[Prop->getIdentifier()] = Prop;
876 }
877 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000878 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +0000879 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000880 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000881 }
882 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
883 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
884 E = PDecl->prop_end(); P != E; ++P) {
885 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000886 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
887 // Exclude property for protocols which conform to class's super-class,
888 // as super-class has to implement the property.
889 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
890 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
891 if (!PropEntry)
892 PropEntry = Prop;
893 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000894 }
895 // scan through protocol's protocols.
896 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
897 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000898 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000899 }
900}
901
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000902/// CollectClassPropertyImplementations - This routine collects list of
903/// properties to be implemented in the class. This includes, class's
904/// and its conforming protocols' properties.
905static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
906 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
907 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
908 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
909 E = IDecl->prop_end(); P != E; ++P) {
910 ObjCPropertyDecl *Prop = (*P);
911 PropMap[Prop->getIdentifier()] = Prop;
912 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000913 for (ObjCInterfaceDecl::all_protocol_iterator
914 PI = IDecl->all_referenced_protocol_begin(),
915 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000916 CollectClassPropertyImplementations((*PI), PropMap);
917 }
918 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
919 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
920 E = PDecl->prop_end(); P != E; ++P) {
921 ObjCPropertyDecl *Prop = (*P);
922 PropMap[Prop->getIdentifier()] = Prop;
923 }
924 // scan through protocol's protocols.
925 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
926 E = PDecl->protocol_end(); PI != E; ++PI)
927 CollectClassPropertyImplementations((*PI), PropMap);
928 }
929}
930
931/// CollectSuperClassPropertyImplementations - This routine collects list of
932/// properties to be implemented in super class(s) and also coming from their
933/// conforming protocols.
934static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
935 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
936 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
937 while (SDecl) {
938 CollectClassPropertyImplementations(SDecl, PropMap);
939 SDecl = SDecl->getSuperClass();
940 }
941 }
942}
943
Ted Kremenek9d64c152010-03-12 00:38:38 +0000944/// LookupPropertyDecl - Looks up a property in the current class and all
945/// its protocols.
946ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
947 IdentifierInfo *II) {
948 if (const ObjCInterfaceDecl *IDecl =
949 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
950 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
951 E = IDecl->prop_end(); P != E; ++P) {
952 ObjCPropertyDecl *Prop = (*P);
953 if (Prop->getIdentifier() == II)
954 return Prop;
955 }
956 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000957 for (ObjCInterfaceDecl::all_protocol_iterator
958 PI = IDecl->all_referenced_protocol_begin(),
959 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000960 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
961 if (Prop)
962 return Prop;
963 }
964 }
965 else if (const ObjCProtocolDecl *PDecl =
966 dyn_cast<ObjCProtocolDecl>(CDecl)) {
967 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
968 E = PDecl->prop_end(); P != E; ++P) {
969 ObjCPropertyDecl *Prop = (*P);
970 if (Prop->getIdentifier() == II)
971 return Prop;
972 }
973 // scan through protocol's protocols.
974 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
975 E = PDecl->protocol_end(); PI != E; ++PI) {
976 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
977 if (Prop)
978 return Prop;
979 }
980 }
981 return 0;
982}
983
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000984/// DefaultSynthesizeProperties - This routine default synthesizes all
985/// properties which must be synthesized in class's @implementation.
986void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
987 ObjCInterfaceDecl *IDecl) {
988
989 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
990 CollectClassPropertyImplementations(IDecl, PropMap);
991 if (PropMap.empty())
992 return;
993 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
994 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
995
996 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
997 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
998 ObjCPropertyDecl *Prop = P->second;
999 // If property to be implemented in the super class, ignore.
1000 if (SuperPropMap[Prop->getIdentifier()])
1001 continue;
1002 // Is there a matching propery synthesize/dynamic?
1003 if (Prop->isInvalidDecl() ||
1004 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1005 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1006 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001007 // Property may have been synthesized by user.
1008 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1009 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001010 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1011 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1012 continue;
1013 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1014 continue;
1015 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001016
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001017
1018 // We use invalid SourceLocations for the synthesized ivars since they
1019 // aren't really synthesized at a particular location; they just exist.
1020 // Saying that they are located at the @implementation isn't really going
1021 // to help users.
1022 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1023 true,IMPDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001024 Prop->getIdentifier(), Prop->getIdentifier(),
1025 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001026 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001027}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001028
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001029void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001030 ObjCContainerDecl *CDecl,
1031 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001032 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1033 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1034 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1035
Ted Kremenek9d64c152010-03-12 00:38:38 +00001036 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001037 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001038 if (PropMap.empty())
1039 return;
1040
1041 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1042 for (ObjCImplDecl::propimpl_iterator
1043 I = IMPDecl->propimpl_begin(),
1044 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1045 PropImplMap.insert((*I)->getPropertyDecl());
1046
1047 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1048 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1049 ObjCPropertyDecl *Prop = P->second;
1050 // Is there a matching propery synthesize/dynamic?
1051 if (Prop->isInvalidDecl() ||
1052 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1053 PropImplMap.count(Prop))
1054 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001055 if (!InsMap.count(Prop->getGetterName())) {
1056 Diag(Prop->getLocation(),
1057 isa<ObjCCategoryDecl>(CDecl) ?
1058 diag::warn_setter_getter_impl_required_in_category :
1059 diag::warn_setter_getter_impl_required)
1060 << Prop->getDeclName() << Prop->getGetterName();
1061 Diag(IMPDecl->getLocation(),
1062 diag::note_property_impl_required);
1063 }
1064
1065 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1066 Diag(Prop->getLocation(),
1067 isa<ObjCCategoryDecl>(CDecl) ?
1068 diag::warn_setter_getter_impl_required_in_category :
1069 diag::warn_setter_getter_impl_required)
1070 << Prop->getDeclName() << Prop->getSetterName();
1071 Diag(IMPDecl->getLocation(),
1072 diag::note_property_impl_required);
1073 }
1074 }
1075}
1076
1077void
1078Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1079 ObjCContainerDecl* IDecl) {
1080 // Rules apply in non-GC mode only
1081 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1082 return;
1083 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1084 E = IDecl->prop_end();
1085 I != E; ++I) {
1086 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001087 ObjCMethodDecl *GetterMethod = 0;
1088 ObjCMethodDecl *SetterMethod = 0;
1089 bool LookedUpGetterSetter = false;
1090
Ted Kremenek9d64c152010-03-12 00:38:38 +00001091 unsigned Attributes = Property->getPropertyAttributes();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001092 unsigned AttributesAsWrittern = Property->getPropertyAttributesAsWritten();
1093
1094 if (!(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_atomic) &&
1095 !(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
1096 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1097 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1098 LookedUpGetterSetter = true;
1099 if (GetterMethod) {
1100 Diag(GetterMethod->getLocation(),
1101 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001102 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001103 Diag(Property->getLocation(), diag::note_property_declare);
1104 }
1105 if (SetterMethod) {
1106 Diag(SetterMethod->getLocation(),
1107 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001108 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001109 Diag(Property->getLocation(), diag::note_property_declare);
1110 }
1111 }
1112
Ted Kremenek9d64c152010-03-12 00:38:38 +00001113 // We only care about readwrite atomic property.
1114 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1115 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1116 continue;
1117 if (const ObjCPropertyImplDecl *PIDecl
1118 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1119 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1120 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001121 if (!LookedUpGetterSetter) {
1122 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1123 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1124 LookedUpGetterSetter = true;
1125 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001126 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1127 SourceLocation MethodLoc =
1128 (GetterMethod ? GetterMethod->getLocation()
1129 : SetterMethod->getLocation());
1130 Diag(MethodLoc, diag::warn_atomic_property_rule)
1131 << Property->getIdentifier();
1132 Diag(Property->getLocation(), diag::note_property_declare);
1133 }
1134 }
1135 }
1136}
1137
John McCall5de74d12010-11-10 07:01:40 +00001138/// AddPropertyAttrs - Propagates attributes from a property to the
1139/// implicitly-declared getter or setter for that property.
1140static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1141 ObjCPropertyDecl *Property) {
1142 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001143 for (Decl::attr_iterator A = Property->attr_begin(),
1144 AEnd = Property->attr_end();
1145 A != AEnd; ++A) {
1146 if (isa<DeprecatedAttr>(*A) ||
1147 isa<UnavailableAttr>(*A) ||
1148 isa<AvailabilityAttr>(*A))
1149 PropertyMethod->addAttr((*A)->clone(S.Context));
1150 }
John McCall5de74d12010-11-10 07:01:40 +00001151}
1152
Ted Kremenek9d64c152010-03-12 00:38:38 +00001153/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1154/// have the property type and issue diagnostics if they don't.
1155/// Also synthesize a getter/setter method if none exist (and update the
1156/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1157/// methods is the "right" thing to do.
1158void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001159 ObjCContainerDecl *CD,
1160 ObjCPropertyDecl *redeclaredProperty,
1161 ObjCContainerDecl *lexicalDC) {
1162
Ted Kremenek9d64c152010-03-12 00:38:38 +00001163 ObjCMethodDecl *GetterMethod, *SetterMethod;
1164
1165 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1166 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1167 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1168 property->getLocation());
1169
1170 if (SetterMethod) {
1171 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1172 property->getPropertyAttributes();
1173 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1174 Context.getCanonicalType(SetterMethod->getResultType()) !=
1175 Context.VoidTy)
1176 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1177 if (SetterMethod->param_size() != 1 ||
1178 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1179 Diag(property->getLocation(),
1180 diag::warn_accessor_property_type_mismatch)
1181 << property->getDeclName()
1182 << SetterMethod->getSelector();
1183 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1184 }
1185 }
1186
1187 // Synthesize getter/setter methods if none exist.
1188 // Find the default getter and if one not found, add one.
1189 // FIXME: The synthesized property we set here is misleading. We almost always
1190 // synthesize these methods unless the user explicitly provided prototypes
1191 // (which is odd, but allowed). Sema should be typechecking that the
1192 // declarations jive in that situation (which it is not currently).
1193 if (!GetterMethod) {
1194 // No instance method of same name as property getter name was found.
1195 // Declare a getter method and add it to the list of methods
1196 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001197 SourceLocation Loc = redeclaredProperty ?
1198 redeclaredProperty->getLocation() :
1199 property->getLocation();
1200
1201 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1202 property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001203 property->getType(), 0, CD, true, false, true,
1204 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001205 (property->getPropertyImplementation() ==
1206 ObjCPropertyDecl::Optional) ?
1207 ObjCMethodDecl::Optional :
1208 ObjCMethodDecl::Required);
1209 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001210
1211 AddPropertyAttrs(*this, GetterMethod, property);
1212
Ted Kremenek23173d72010-05-18 21:09:07 +00001213 // FIXME: Eventually this shouldn't be needed, as the lexical context
1214 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001215 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001216 GetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001217 } else
1218 // A user declared getter will be synthesize when @synthesize of
1219 // the property with the same name is seen in the @implementation
1220 GetterMethod->setSynthesized(true);
1221 property->setGetterMethodDecl(GetterMethod);
1222
1223 // Skip setter if property is read-only.
1224 if (!property->isReadOnly()) {
1225 // Find the default setter and if one not found, add one.
1226 if (!SetterMethod) {
1227 // No instance method of same name as property setter name was found.
1228 // Declare a setter method and add it to the list of methods
1229 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001230 SourceLocation Loc = redeclaredProperty ?
1231 redeclaredProperty->getLocation() :
1232 property->getLocation();
1233
1234 SetterMethod =
1235 ObjCMethodDecl::Create(Context, Loc, Loc,
1236 property->getSetterName(), Context.VoidTy, 0,
1237 CD, true, false, true, false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001238 (property->getPropertyImplementation() ==
1239 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001240 ObjCMethodDecl::Optional :
1241 ObjCMethodDecl::Required);
1242
Ted Kremenek9d64c152010-03-12 00:38:38 +00001243 // Invent the arguments for the setter. We don't bother making a
1244 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001245 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1246 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001247 property->getIdentifier(),
1248 property->getType(),
1249 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001250 SC_None,
1251 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001252 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001253 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001254
1255 AddPropertyAttrs(*this, SetterMethod, property);
1256
Ted Kremenek9d64c152010-03-12 00:38:38 +00001257 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001258 // FIXME: Eventually this shouldn't be needed, as the lexical context
1259 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001260 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001261 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001262 } else
1263 // A user declared setter will be synthesize when @synthesize of
1264 // the property with the same name is seen in the @implementation
1265 SetterMethod->setSynthesized(true);
1266 property->setSetterMethodDecl(SetterMethod);
1267 }
1268 // Add any synthesized methods to the global pool. This allows us to
1269 // handle the following, which is supported by GCC (and part of the design).
1270 //
1271 // @interface Foo
1272 // @property double bar;
1273 // @end
1274 //
1275 // void thisIsUnfortunate() {
1276 // id foo;
1277 // double bar = [foo bar];
1278 // }
1279 //
1280 if (GetterMethod)
1281 AddInstanceMethodToGlobalPool(GetterMethod);
1282 if (SetterMethod)
1283 AddInstanceMethodToGlobalPool(SetterMethod);
1284}
1285
John McCalld226f652010-08-21 09:40:31 +00001286void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001287 SourceLocation Loc,
1288 unsigned &Attributes) {
1289 // FIXME: Improve the reported location.
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001290 if (!PDecl)
1291 return;
1292
1293 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001294 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001295
1296 // readonly and readwrite/assign/retain/copy conflict.
1297 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1298 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1299 ObjCDeclSpec::DQ_PR_assign |
1300 ObjCDeclSpec::DQ_PR_copy |
1301 ObjCDeclSpec::DQ_PR_retain))) {
1302 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1303 "readwrite" :
1304 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1305 "assign" :
1306 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1307 "copy" : "retain";
1308
1309 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1310 diag::err_objc_property_attr_mutually_exclusive :
1311 diag::warn_objc_property_attr_mutually_exclusive)
1312 << "readonly" << which;
1313 }
1314
1315 // Check for copy or retain on non-object types.
1316 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1317 !PropertyTy->isObjCObjectPointerType() &&
1318 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001319 !Context.isObjCNSObjectType(PropertyTy) &&
1320 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001321 Diag(Loc, diag::err_objc_property_requires_object)
1322 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1323 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1324 }
1325
1326 // Check for more than one of { assign, copy, retain }.
1327 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1328 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1329 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1330 << "assign" << "copy";
1331 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1332 }
1333 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1334 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1335 << "assign" << "retain";
1336 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1337 }
1338 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1339 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1340 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1341 << "copy" << "retain";
1342 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1343 }
1344 }
1345
1346 // Warn if user supplied no assignment attribute, property is
1347 // readwrite, and this is an object type.
1348 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1349 ObjCDeclSpec::DQ_PR_retain)) &&
1350 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1351 PropertyTy->isObjCObjectPointerType()) {
1352 // Skip this warning in gc-only mode.
1353 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1354 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1355
1356 // If non-gc code warn that this is likely inappropriate.
1357 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1358 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1359
1360 // FIXME: Implement warning dependent on NSCopying being
1361 // implemented. See also:
1362 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1363 // (please trim this list while you are at it).
1364 }
1365
1366 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001367 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001368 && getLangOptions().getGCMode() == LangOptions::GCOnly
1369 && PropertyTy->isBlockPointerType())
1370 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1371}