blob: aa934d8e9c5882f645740769799539f0fd0122f1 [file] [log] [blame]
Ted Kremenek9d64c152010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
Fariborz Jahanian17cb3262010-05-05 21:52:17 +000018#include "clang/AST/ExprObjC.h"
John McCall50df6ae2010-08-25 07:03:20 +000019#include "llvm/ADT/DenseSet.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000020
21using namespace clang;
22
Ted Kremenek28685ab2010-03-12 00:46:40 +000023//===----------------------------------------------------------------------===//
24// Grammar actions.
25//===----------------------------------------------------------------------===//
26
John McCalld226f652010-08-21 09:40:31 +000027Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
28 FieldDeclarator &FD,
29 ObjCDeclSpec &ODS,
30 Selector GetterSel,
31 Selector SetterSel,
32 Decl *ClassCategory,
33 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000034 tok::ObjCKeywordKind MethodImplKind,
35 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000036 unsigned Attributes = ODS.getPropertyAttributes();
37 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
38 // default is readwrite!
39 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
40 // property is defaulted to 'assign' if it is readwrite and is
41 // not retain or copy
42 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
43 (isReadWrite &&
44 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
45 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000046
John McCallbf1a0282010-06-04 23:28:52 +000047 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
48 QualType T = TSI->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +000049 if (T->isReferenceType()) {
50 Diag(AtLoc, diag::error_reference_property);
John McCalld226f652010-08-21 09:40:31 +000051 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +000052 }
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000053 // Proceed with constructing the ObjCPropertDecls.
54 ObjCContainerDecl *ClassDecl =
John McCalld226f652010-08-21 09:40:31 +000055 cast<ObjCContainerDecl>(ClassCategory);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000056
Ted Kremenek28685ab2010-03-12 00:46:40 +000057 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000058 if (CDecl->IsClassExtension()) {
John McCalld226f652010-08-21 09:40:31 +000059 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000060 FD, GetterSel, SetterSel,
61 isAssign, isReadWrite,
62 Attributes,
63 isOverridingProperty, TSI,
64 MethodImplKind);
65 if (Res)
66 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
67 return Res;
68 }
69
John McCalld226f652010-08-21 09:40:31 +000070 Decl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
71 GetterSel, SetterSel,
72 isAssign, isReadWrite,
73 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000074 if (lexicalDC)
75 Res->setLexicalDeclContext(lexicalDC);
76
Fariborz Jahanian842f07b2010-03-30 22:40:11 +000077 // Validate the attributes on the @property.
78 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
79 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000080}
Ted Kremenek2d2f9362010-03-12 00:49:00 +000081
John McCalld226f652010-08-21 09:40:31 +000082Decl *
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000083Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
84 SourceLocation AtLoc, FieldDeclarator &FD,
85 Selector GetterSel, Selector SetterSel,
86 const bool isAssign,
87 const bool isReadWrite,
88 const unsigned Attributes,
89 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +000090 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000091 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000092
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000093 // Diagnose if this property is already in continuation class.
94 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000095 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +000096 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
97
98 if (CCPrimary)
99 // Check for duplicate declaration of this property in current and
100 // other class extensions.
101 for (const ObjCCategoryDecl *ClsExtDecl =
102 CCPrimary->getFirstClassExtension();
103 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
104 if (ObjCPropertyDecl *prevDecl =
105 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
106 Diag(AtLoc, diag::err_duplicate_property);
107 Diag(prevDecl->getLocation(), diag::note_property_declare);
108 return 0;
109 }
110 }
111
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000112 // Create a new ObjCPropertyDecl with the DeclContext being
113 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000114 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000115 ObjCPropertyDecl *PDecl =
116 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
117 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000118 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
119 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
120 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
121 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000122 // Set setter/getter selector name. Needed later.
123 PDecl->setGetterName(GetterSel);
124 PDecl->setSetterName(SetterSel);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000125 DC->addDecl(PDecl);
126
127 // We need to look in the @interface to see if the @property was
128 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000129 if (!CCPrimary) {
130 Diag(CDecl->getLocation(), diag::err_continuation_class);
131 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000132 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000133 }
134
135 // Find the property in continuation class's primary class only.
136 ObjCPropertyDecl *PIDecl =
137 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
138
139 if (!PIDecl) {
140 // No matching property found in the primary class. Just fall thru
141 // and add property to continuation class's primary class.
142 ObjCPropertyDecl *PDecl =
143 CreatePropertyDecl(S, CCPrimary, AtLoc,
144 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000145 Attributes, T, MethodImplKind, DC);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000146 // Mark written attribute as having no attribute because
147 // this is not a user-written property declaration in primary
148 // class.
149 PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000150
151 // A case of continuation class adding a new property in the class. This
152 // is not what it was meant for. However, gcc supports it and so should we.
153 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000154 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
155 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000156 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000157 }
158
159 // The property 'PIDecl's readonly attribute will be over-ridden
160 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000161 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000162 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
163 unsigned retainCopyNonatomic =
164 (ObjCPropertyDecl::OBJC_PR_retain |
165 ObjCPropertyDecl::OBJC_PR_copy |
166 ObjCPropertyDecl::OBJC_PR_nonatomic);
167 if ((Attributes & retainCopyNonatomic) !=
168 (PIkind & retainCopyNonatomic)) {
169 Diag(AtLoc, diag::warn_property_attr_mismatch);
170 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000171 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000172 DeclContext *DC = cast<DeclContext>(CCPrimary);
173 if (!ObjCPropertyDecl::findPropertyDecl(DC,
174 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000175 // Protocol is not in the primary class. Must build one for it.
176 ObjCDeclSpec ProtocolPropertyODS;
177 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
178 // and ObjCPropertyDecl::PropertyAttributeKind have identical
179 // values. Should consolidate both into one enum type.
180 ProtocolPropertyODS.
181 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
182 PIkind);
183
John McCalld226f652010-08-21 09:40:31 +0000184 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000185 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
186 PIDecl->getGetterName(),
187 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000188 CCPrimary, isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000189 MethodImplKind,
190 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000191 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000192 }
193 PIDecl->makeitReadWriteAttribute();
194 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
195 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
196 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
197 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
198 PIDecl->setSetterName(SetterSel);
199 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000200 // Tailor the diagnostics for the common case where a readwrite
201 // property is declared both in the @interface and the continuation.
202 // This is a common error where the user often intended the original
203 // declaration to be readonly.
204 unsigned diag =
205 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
206 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
207 ? diag::err_use_continuation_class_redeclaration_readwrite
208 : diag::err_use_continuation_class;
209 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000210 << CCPrimary->getDeclName();
211 Diag(PIDecl->getLocation(), diag::note_property_declare);
212 }
213 *isOverridingProperty = true;
214 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000215 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000216 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000217}
218
219ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
220 ObjCContainerDecl *CDecl,
221 SourceLocation AtLoc,
222 FieldDeclarator &FD,
223 Selector GetterSel,
224 Selector SetterSel,
225 const bool isAssign,
226 const bool isReadWrite,
227 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000228 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000229 tok::ObjCKeywordKind MethodImplKind,
230 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000231 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000232 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000233
234 // Issue a warning if property is 'assign' as default and its object, which is
235 // gc'able conforms to NSCopying protocol
236 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
237 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000238 if (const ObjCObjectPointerType *ObjPtrTy =
239 T->getAs<ObjCObjectPointerType>()) {
240 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
241 if (IDecl)
242 if (ObjCProtocolDecl* PNSCopying =
243 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
244 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
245 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000246 }
John McCallc12c5bb2010-05-15 11:32:37 +0000247 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000248 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
249
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000250 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000251 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
252 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000253 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000254
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000255 if (ObjCPropertyDecl *prevDecl =
256 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000257 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000258 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000259 PDecl->setInvalidDecl();
260 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000261 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000262 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000263 if (lexicalDC)
264 PDecl->setLexicalDeclContext(lexicalDC);
265 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000266
267 if (T->isArrayType() || T->isFunctionType()) {
268 Diag(AtLoc, diag::err_property_type) << T;
269 PDecl->setInvalidDecl();
270 }
271
272 ProcessDeclAttributes(S, PDecl, FD.D);
273
274 // Regardless of setter/getter attribute, we save the default getter/setter
275 // selector names in anticipation of declaration of setter/getter methods.
276 PDecl->setGetterName(GetterSel);
277 PDecl->setSetterName(SetterSel);
278
279 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
280 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
281
282 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
283 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
284
285 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
286 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
287
288 if (isReadWrite)
289 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
290
291 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
292 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
293
294 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
295 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
296
297 if (isAssign)
298 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
299
300 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
301 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
302
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000303 PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes());
304
Ted Kremenek28685ab2010-03-12 00:46:40 +0000305 if (MethodImplKind == tok::objc_required)
306 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
307 else if (MethodImplKind == tok::objc_optional)
308 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000309
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000310 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000311}
312
313
314/// ActOnPropertyImplDecl - This routine performs semantic checks and
315/// builds the AST node for a property implementation declaration; declared
316/// as @synthesize or @dynamic.
317///
John McCalld226f652010-08-21 09:40:31 +0000318Decl *Sema::ActOnPropertyImplDecl(Scope *S,
319 SourceLocation AtLoc,
320 SourceLocation PropertyLoc,
321 bool Synthesize,
322 Decl *ClassCatImpDecl,
323 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000324 IdentifierInfo *PropertyIvar,
325 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000326 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000327 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000328 // Make sure we have a context for the property implementation declaration.
329 if (!ClassImpDecl) {
330 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000331 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000332 }
333 ObjCPropertyDecl *property = 0;
334 ObjCInterfaceDecl* IDecl = 0;
335 // Find the class or category class where this property must have
336 // a declaration.
337 ObjCImplementationDecl *IC = 0;
338 ObjCCategoryImplDecl* CatImplClass = 0;
339 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
340 IDecl = IC->getClassInterface();
341 // We always synthesize an interface for an implementation
342 // without an interface decl. So, IDecl is always non-zero.
343 assert(IDecl &&
344 "ActOnPropertyImplDecl - @implementation without @interface");
345
346 // Look for this property declaration in the @implementation's @interface
347 property = IDecl->FindPropertyDeclaration(PropertyId);
348 if (!property) {
349 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000350 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000351 }
352 if (const ObjCCategoryDecl *CD =
353 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
354 if (!CD->IsClassExtension()) {
355 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
356 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000357 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000358 }
359 }
360 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
361 if (Synthesize) {
362 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000363 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000364 }
365 IDecl = CatImplClass->getClassInterface();
366 if (!IDecl) {
367 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000368 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000369 }
370 ObjCCategoryDecl *Category =
371 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
372
373 // If category for this implementation not found, it is an error which
374 // has already been reported eralier.
375 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000376 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000377 // Look for this property declaration in @implementation's category
378 property = Category->FindPropertyDeclaration(PropertyId);
379 if (!property) {
380 Diag(PropertyLoc, diag::error_bad_category_property_decl)
381 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000382 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000383 }
384 } else {
385 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000386 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000387 }
388 ObjCIvarDecl *Ivar = 0;
389 // Check that we have a valid, previously declared ivar for @synthesize
390 if (Synthesize) {
391 // @synthesize
392 if (!PropertyIvar)
393 PropertyIvar = PropertyId;
394 QualType PropType = Context.getCanonicalType(property->getType());
395 // Check that this is a previously declared 'ivar' in 'IDecl' interface
396 ObjCInterfaceDecl *ClassDeclared;
397 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
398 if (!Ivar) {
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000399 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000400 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian2846b2b2010-04-06 23:36:17 +0000401 ObjCIvarDecl::Protected,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000402 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000403 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000404 IDecl->makeDeclVisibleInContext(Ivar, false);
405 property->setPropertyIvarDecl(Ivar);
406
407 if (!getLangOptions().ObjCNonFragileABI)
408 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
409 // Note! I deliberately want it to fall thru so, we have a
410 // a property implementation and to avoid future warnings.
411 } else if (getLangOptions().ObjCNonFragileABI &&
412 ClassDeclared != IDecl) {
413 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
414 << property->getDeclName() << Ivar->getDeclName()
415 << ClassDeclared->getDeclName();
416 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000417 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000418 // Note! I deliberately want it to fall thru so more errors are caught.
419 }
420 QualType IvarType = Context.getCanonicalType(Ivar->getType());
421
422 // Check that type of property and its ivar are type compatible.
423 if (PropType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000424 bool compat = false;
425 if (isa<ObjCObjectPointerType>(PropType)
426 && isa<ObjCObjectPointerType>(IvarType))
427 compat =
428 Context.canAssignObjCInterfaces(
429 PropType->getAs<ObjCObjectPointerType>(),
430 IvarType->getAs<ObjCObjectPointerType>());
John McCall1c23e912010-11-16 02:32:08 +0000431 else
432 compat = (CheckAssignmentConstraints(PropType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000433 == Compatible);
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000434 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000435 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000436 << property->getDeclName() << PropType
437 << Ivar->getDeclName() << IvarType;
438 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000439 // Note! I deliberately want it to fall thru so, we have a
440 // a property implementation and to avoid future warnings.
441 }
442
443 // FIXME! Rules for properties are somewhat different that those
444 // for assignments. Use a new routine to consolidate all cases;
445 // specifically for property redeclarations as well as for ivars.
446 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
447 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
448 if (lhsType != rhsType &&
449 lhsType->isArithmeticType()) {
450 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000451 << property->getDeclName() << PropType
452 << Ivar->getDeclName() << IvarType;
453 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000454 // Fall thru - see previous comment
455 }
456 // __weak is explicit. So it works on Canonical type.
457 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
458 getLangOptions().getGCMode() != LangOptions::NonGC) {
459 Diag(PropertyLoc, diag::error_weak_property)
460 << property->getDeclName() << Ivar->getDeclName();
461 // Fall thru - see previous comment
462 }
463 if ((property->getType()->isObjCObjectPointerType() ||
464 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
465 getLangOptions().getGCMode() != LangOptions::NonGC) {
466 Diag(PropertyLoc, diag::error_strong_property)
467 << property->getDeclName() << Ivar->getDeclName();
468 // Fall thru - see previous comment
469 }
470 }
471 } else if (PropertyIvar)
472 // @dynamic
473 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
474 assert (property && "ActOnPropertyImplDecl - property declaration missing");
475 ObjCPropertyImplDecl *PIDecl =
476 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
477 property,
478 (Synthesize ?
479 ObjCPropertyImplDecl::Synthesize
480 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000481 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000482 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
483 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000484 if (getLangOptions().CPlusPlus && Synthesize &&
485 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000486 // For Objective-C++, need to synthesize the AST for the IVAR object to be
487 // returned by the getter as it must conform to C++'s copy-return rules.
488 // FIXME. Eventually we want to do this for Objective-C as well.
489 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
490 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000491 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
492 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000493 Expr *IvarRefExpr =
494 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
495 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000496 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000497 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000498 SourceLocation(),
499 getterMethod->getResultType(),
500 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000501 SourceLocation(),
502 Owned(IvarRefExpr));
503 if (!Res.isInvalid()) {
504 Expr *ResExpr = Res.takeAs<Expr>();
505 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000506 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000507 PIDecl->setGetterCXXConstructor(ResExpr);
508 }
509 }
510 }
511 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
512 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000513 if (getLangOptions().CPlusPlus && Synthesize
514 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000515 // FIXME. Eventually we want to do this for Objective-C as well.
516 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
517 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000518 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
519 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000520 Expr *lhs =
521 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
522 SelfExpr, true, true);
523 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
524 ParmVarDecl *Param = (*P);
John McCallf89e55a2010-11-18 06:31:45 +0000525 Expr *rhs = new (Context) DeclRefExpr(Param, Param->getType(),
526 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000527 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000528 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000529 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
530 }
531 }
532
Ted Kremenek28685ab2010-03-12 00:46:40 +0000533 if (IC) {
534 if (Synthesize)
535 if (ObjCPropertyImplDecl *PPIDecl =
536 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
537 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
538 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
539 << PropertyIvar;
540 Diag(PPIDecl->getLocation(), diag::note_previous_use);
541 }
542
543 if (ObjCPropertyImplDecl *PPIDecl
544 = IC->FindPropertyImplDecl(PropertyId)) {
545 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
546 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000547 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000548 }
549 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000550 if (getLangOptions().ObjCNonFragileABI2) {
551 // Diagnose if an ivar was lazily synthesdized due to a previous
552 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000553 // but it requires an ivar of different name.
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000554 ObjCInterfaceDecl *ClassDeclared;
555 ObjCIvarDecl *Ivar = 0;
556 if (!Synthesize)
557 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
558 else {
559 if (PropertyIvar && PropertyIvar != PropertyId)
560 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
561 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000562 // Issue diagnostics only if Ivar belongs to current class.
563 if (Ivar && Ivar->getSynthesize() &&
564 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000565 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
566 << PropertyId;
567 Ivar->setInvalidDecl();
568 }
569 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000570 } else {
571 if (Synthesize)
572 if (ObjCPropertyImplDecl *PPIDecl =
573 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
574 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
575 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
576 << PropertyIvar;
577 Diag(PPIDecl->getLocation(), diag::note_previous_use);
578 }
579
580 if (ObjCPropertyImplDecl *PPIDecl =
581 CatImplClass->FindPropertyImplDecl(PropertyId)) {
582 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
583 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000584 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000585 }
586 CatImplClass->addPropertyImplementation(PIDecl);
587 }
588
John McCalld226f652010-08-21 09:40:31 +0000589 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000590}
591
592//===----------------------------------------------------------------------===//
593// Helper methods.
594//===----------------------------------------------------------------------===//
595
Ted Kremenek9d64c152010-03-12 00:38:38 +0000596/// DiagnosePropertyMismatch - Compares two properties for their
597/// attributes and types and warns on a variety of inconsistencies.
598///
599void
600Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
601 ObjCPropertyDecl *SuperProperty,
602 const IdentifierInfo *inheritedName) {
603 ObjCPropertyDecl::PropertyAttributeKind CAttr =
604 Property->getPropertyAttributes();
605 ObjCPropertyDecl::PropertyAttributeKind SAttr =
606 SuperProperty->getPropertyAttributes();
607 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
608 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
609 Diag(Property->getLocation(), diag::warn_readonly_property)
610 << Property->getDeclName() << inheritedName;
611 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
612 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
613 Diag(Property->getLocation(), diag::warn_property_attribute)
614 << Property->getDeclName() << "copy" << inheritedName;
615 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
616 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
617 Diag(Property->getLocation(), diag::warn_property_attribute)
618 << Property->getDeclName() << "retain" << inheritedName;
619
620 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
621 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
622 Diag(Property->getLocation(), diag::warn_property_attribute)
623 << Property->getDeclName() << "atomic" << inheritedName;
624 if (Property->getSetterName() != SuperProperty->getSetterName())
625 Diag(Property->getLocation(), diag::warn_property_attribute)
626 << Property->getDeclName() << "setter" << inheritedName;
627 if (Property->getGetterName() != SuperProperty->getGetterName())
628 Diag(Property->getLocation(), diag::warn_property_attribute)
629 << Property->getDeclName() << "getter" << inheritedName;
630
631 QualType LHSType =
632 Context.getCanonicalType(SuperProperty->getType());
633 QualType RHSType =
634 Context.getCanonicalType(Property->getType());
635
636 if (!Context.typesAreCompatible(LHSType, RHSType)) {
637 // FIXME: Incorporate this test with typesAreCompatible.
638 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
639 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
640 return;
641 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
642 << Property->getType() << SuperProperty->getType() << inheritedName;
643 }
644}
645
646bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
647 ObjCMethodDecl *GetterMethod,
648 SourceLocation Loc) {
649 if (GetterMethod &&
650 GetterMethod->getResultType() != property->getType()) {
651 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000652 if (property->getType()->isObjCObjectPointerType())
Ted Kremenek9d64c152010-03-12 00:38:38 +0000653 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000654 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000655 if (result != Compatible) {
656 Diag(Loc, diag::warn_accessor_property_type_mismatch)
657 << property->getDeclName()
658 << GetterMethod->getSelector();
659 Diag(GetterMethod->getLocation(), diag::note_declared_at);
660 return true;
661 }
662 }
663 return false;
664}
665
666/// ComparePropertiesInBaseAndSuper - This routine compares property
667/// declarations in base and its super class, if any, and issues
668/// diagnostics in a variety of inconsistant situations.
669///
670void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
671 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
672 if (!SDecl)
673 return;
674 // FIXME: O(N^2)
675 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
676 E = SDecl->prop_end(); S != E; ++S) {
677 ObjCPropertyDecl *SuperPDecl = (*S);
678 // Does property in super class has declaration in current class?
679 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
680 E = IDecl->prop_end(); I != E; ++I) {
681 ObjCPropertyDecl *PDecl = (*I);
682 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
683 DiagnosePropertyMismatch(PDecl, SuperPDecl,
684 SDecl->getIdentifier());
685 }
686 }
687}
688
689/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
690/// of properties declared in a protocol and compares their attribute against
691/// the same property declared in the class or category.
692void
693Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
694 ObjCProtocolDecl *PDecl) {
695 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
696 if (!IDecl) {
697 // Category
698 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
699 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
700 if (!CatDecl->IsClassExtension())
701 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
702 E = PDecl->prop_end(); P != E; ++P) {
703 ObjCPropertyDecl *Pr = (*P);
704 ObjCCategoryDecl::prop_iterator CP, CE;
705 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000706 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000707 if ((*CP)->getIdentifier() == Pr->getIdentifier())
708 break;
709 if (CP != CE)
710 // Property protocol already exist in class. Diagnose any mismatch.
711 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
712 }
713 return;
714 }
715 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
716 E = PDecl->prop_end(); P != E; ++P) {
717 ObjCPropertyDecl *Pr = (*P);
718 ObjCInterfaceDecl::prop_iterator CP, CE;
719 // Is this property already in class's list of properties?
720 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
721 if ((*CP)->getIdentifier() == Pr->getIdentifier())
722 break;
723 if (CP != CE)
724 // Property protocol already exist in class. Diagnose any mismatch.
725 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
726 }
727}
728
729/// CompareProperties - This routine compares properties
730/// declared in 'ClassOrProtocol' objects (which can be a class or an
731/// inherited protocol with the list of properties for class/category 'CDecl'
732///
John McCalld226f652010-08-21 09:40:31 +0000733void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
734 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000735 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
736
737 if (!IDecl) {
738 // Category
739 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
740 assert (CatDecl && "CompareProperties");
741 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
742 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
743 E = MDecl->protocol_end(); P != E; ++P)
744 // Match properties of category with those of protocol (*P)
745 MatchOneProtocolPropertiesInClass(CatDecl, *P);
746
747 // Go thru the list of protocols for this category and recursively match
748 // their properties with those in the category.
749 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
750 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000751 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000752 } else {
753 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
754 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
755 E = MD->protocol_end(); P != E; ++P)
756 MatchOneProtocolPropertiesInClass(CatDecl, *P);
757 }
758 return;
759 }
760
761 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000762 for (ObjCInterfaceDecl::all_protocol_iterator
763 P = MDecl->all_referenced_protocol_begin(),
764 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000765 // Match properties of class IDecl with those of protocol (*P).
766 MatchOneProtocolPropertiesInClass(IDecl, *P);
767
768 // Go thru the list of protocols for this class and recursively match
769 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +0000770 for (ObjCInterfaceDecl::all_protocol_iterator
771 P = IDecl->all_referenced_protocol_begin(),
772 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000773 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000774 } else {
775 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
776 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
777 E = MD->protocol_end(); P != E; ++P)
778 MatchOneProtocolPropertiesInClass(IDecl, *P);
779 }
780}
781
782/// isPropertyReadonly - Return true if property is readonly, by searching
783/// for the property in the class and in its categories and implementations
784///
785bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
786 ObjCInterfaceDecl *IDecl) {
787 // by far the most common case.
788 if (!PDecl->isReadOnly())
789 return false;
790 // Even if property is ready only, if interface has a user defined setter,
791 // it is not considered read only.
792 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
793 return false;
794
795 // Main class has the property as 'readonly'. Must search
796 // through the category list to see if the property's
797 // attribute has been over-ridden to 'readwrite'.
798 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
799 Category; Category = Category->getNextClassCategory()) {
800 // Even if property is ready only, if a category has a user defined setter,
801 // it is not considered read only.
802 if (Category->getInstanceMethod(PDecl->getSetterName()))
803 return false;
804 ObjCPropertyDecl *P =
805 Category->FindPropertyDeclaration(PDecl->getIdentifier());
806 if (P && !P->isReadOnly())
807 return false;
808 }
809
810 // Also, check for definition of a setter method in the implementation if
811 // all else failed.
812 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
813 if (ObjCImplementationDecl *IMD =
814 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
815 if (IMD->getInstanceMethod(PDecl->getSetterName()))
816 return false;
817 } else if (ObjCCategoryImplDecl *CIMD =
818 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
819 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
820 return false;
821 }
822 }
823 // Lastly, look through the implementation (if one is in scope).
824 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
825 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
826 return false;
827 // If all fails, look at the super class.
828 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
829 return isPropertyReadonly(PDecl, SIDecl);
830 return true;
831}
832
833/// CollectImmediateProperties - This routine collects all properties in
834/// the class and its conforming protocols; but not those it its super class.
835void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000836 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
837 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000838 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
839 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
840 E = IDecl->prop_end(); P != E; ++P) {
841 ObjCPropertyDecl *Prop = (*P);
842 PropMap[Prop->getIdentifier()] = Prop;
843 }
844 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000845 for (ObjCInterfaceDecl::all_protocol_iterator
846 PI = IDecl->all_referenced_protocol_begin(),
847 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000848 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000849 }
850 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
851 if (!CATDecl->IsClassExtension())
852 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
853 E = CATDecl->prop_end(); P != E; ++P) {
854 ObjCPropertyDecl *Prop = (*P);
855 PropMap[Prop->getIdentifier()] = Prop;
856 }
857 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000858 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +0000859 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000860 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000861 }
862 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
863 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
864 E = PDecl->prop_end(); P != E; ++P) {
865 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000866 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
867 // Exclude property for protocols which conform to class's super-class,
868 // as super-class has to implement the property.
869 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
870 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
871 if (!PropEntry)
872 PropEntry = Prop;
873 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000874 }
875 // scan through protocol's protocols.
876 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
877 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000878 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000879 }
880}
881
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000882/// CollectClassPropertyImplementations - This routine collects list of
883/// properties to be implemented in the class. This includes, class's
884/// and its conforming protocols' properties.
885static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
886 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
887 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
888 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
889 E = IDecl->prop_end(); P != E; ++P) {
890 ObjCPropertyDecl *Prop = (*P);
891 PropMap[Prop->getIdentifier()] = Prop;
892 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000893 for (ObjCInterfaceDecl::all_protocol_iterator
894 PI = IDecl->all_referenced_protocol_begin(),
895 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000896 CollectClassPropertyImplementations((*PI), PropMap);
897 }
898 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
899 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
900 E = PDecl->prop_end(); P != E; ++P) {
901 ObjCPropertyDecl *Prop = (*P);
902 PropMap[Prop->getIdentifier()] = Prop;
903 }
904 // scan through protocol's protocols.
905 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
906 E = PDecl->protocol_end(); PI != E; ++PI)
907 CollectClassPropertyImplementations((*PI), PropMap);
908 }
909}
910
911/// CollectSuperClassPropertyImplementations - This routine collects list of
912/// properties to be implemented in super class(s) and also coming from their
913/// conforming protocols.
914static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
915 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
916 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
917 while (SDecl) {
918 CollectClassPropertyImplementations(SDecl, PropMap);
919 SDecl = SDecl->getSuperClass();
920 }
921 }
922}
923
Ted Kremenek9d64c152010-03-12 00:38:38 +0000924/// LookupPropertyDecl - Looks up a property in the current class and all
925/// its protocols.
926ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
927 IdentifierInfo *II) {
928 if (const ObjCInterfaceDecl *IDecl =
929 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
930 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
931 E = IDecl->prop_end(); P != E; ++P) {
932 ObjCPropertyDecl *Prop = (*P);
933 if (Prop->getIdentifier() == II)
934 return Prop;
935 }
936 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000937 for (ObjCInterfaceDecl::all_protocol_iterator
938 PI = IDecl->all_referenced_protocol_begin(),
939 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000940 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
941 if (Prop)
942 return Prop;
943 }
944 }
945 else if (const ObjCProtocolDecl *PDecl =
946 dyn_cast<ObjCProtocolDecl>(CDecl)) {
947 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
948 E = PDecl->prop_end(); P != E; ++P) {
949 ObjCPropertyDecl *Prop = (*P);
950 if (Prop->getIdentifier() == II)
951 return Prop;
952 }
953 // scan through protocol's protocols.
954 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
955 E = PDecl->protocol_end(); PI != E; ++PI) {
956 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
957 if (Prop)
958 return Prop;
959 }
960 }
961 return 0;
962}
963
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000964/// DefaultSynthesizeProperties - This routine default synthesizes all
965/// properties which must be synthesized in class's @implementation.
966void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
967 ObjCInterfaceDecl *IDecl) {
968
969 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
970 CollectClassPropertyImplementations(IDecl, PropMap);
971 if (PropMap.empty())
972 return;
973 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
974 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
975
976 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
977 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
978 ObjCPropertyDecl *Prop = P->second;
979 // If property to be implemented in the super class, ignore.
980 if (SuperPropMap[Prop->getIdentifier()])
981 continue;
982 // Is there a matching propery synthesize/dynamic?
983 if (Prop->isInvalidDecl() ||
984 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
985 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
986 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000987 // Property may have been synthesized by user.
988 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
989 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +0000990 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
991 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
992 continue;
993 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
994 continue;
995 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000996
Ted Kremenek2a6af6b2010-09-24 01:23:01 +0000997
998 // We use invalid SourceLocations for the synthesized ivars since they
999 // aren't really synthesized at a particular location; they just exist.
1000 // Saying that they are located at the @implementation isn't really going
1001 // to help users.
1002 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1003 true,IMPDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001004 Prop->getIdentifier(), Prop->getIdentifier(),
1005 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001006 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001007}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001008
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001009void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001010 ObjCContainerDecl *CDecl,
1011 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001012 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1013 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1014 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1015
Ted Kremenek9d64c152010-03-12 00:38:38 +00001016 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001017 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001018 if (PropMap.empty())
1019 return;
1020
1021 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1022 for (ObjCImplDecl::propimpl_iterator
1023 I = IMPDecl->propimpl_begin(),
1024 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1025 PropImplMap.insert((*I)->getPropertyDecl());
1026
1027 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1028 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1029 ObjCPropertyDecl *Prop = P->second;
1030 // Is there a matching propery synthesize/dynamic?
1031 if (Prop->isInvalidDecl() ||
1032 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1033 PropImplMap.count(Prop))
1034 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001035 if (!InsMap.count(Prop->getGetterName())) {
1036 Diag(Prop->getLocation(),
1037 isa<ObjCCategoryDecl>(CDecl) ?
1038 diag::warn_setter_getter_impl_required_in_category :
1039 diag::warn_setter_getter_impl_required)
1040 << Prop->getDeclName() << Prop->getGetterName();
1041 Diag(IMPDecl->getLocation(),
1042 diag::note_property_impl_required);
1043 }
1044
1045 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1046 Diag(Prop->getLocation(),
1047 isa<ObjCCategoryDecl>(CDecl) ?
1048 diag::warn_setter_getter_impl_required_in_category :
1049 diag::warn_setter_getter_impl_required)
1050 << Prop->getDeclName() << Prop->getSetterName();
1051 Diag(IMPDecl->getLocation(),
1052 diag::note_property_impl_required);
1053 }
1054 }
1055}
1056
1057void
1058Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1059 ObjCContainerDecl* IDecl) {
1060 // Rules apply in non-GC mode only
1061 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1062 return;
1063 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1064 E = IDecl->prop_end();
1065 I != E; ++I) {
1066 ObjCPropertyDecl *Property = (*I);
1067 unsigned Attributes = Property->getPropertyAttributes();
1068 // We only care about readwrite atomic property.
1069 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1070 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1071 continue;
1072 if (const ObjCPropertyImplDecl *PIDecl
1073 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1074 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1075 continue;
1076 ObjCMethodDecl *GetterMethod =
1077 IMPDecl->getInstanceMethod(Property->getGetterName());
1078 ObjCMethodDecl *SetterMethod =
1079 IMPDecl->getInstanceMethod(Property->getSetterName());
1080 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1081 SourceLocation MethodLoc =
1082 (GetterMethod ? GetterMethod->getLocation()
1083 : SetterMethod->getLocation());
1084 Diag(MethodLoc, diag::warn_atomic_property_rule)
1085 << Property->getIdentifier();
1086 Diag(Property->getLocation(), diag::note_property_declare);
1087 }
1088 }
1089 }
1090}
1091
John McCall5de74d12010-11-10 07:01:40 +00001092/// AddPropertyAttrs - Propagates attributes from a property to the
1093/// implicitly-declared getter or setter for that property.
1094static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1095 ObjCPropertyDecl *Property) {
1096 // Should we just clone all attributes over?
1097 if (DeprecatedAttr *A = Property->getAttr<DeprecatedAttr>())
1098 PropertyMethod->addAttr(A->clone(S.Context));
1099 if (UnavailableAttr *A = Property->getAttr<UnavailableAttr>())
1100 PropertyMethod->addAttr(A->clone(S.Context));
1101}
1102
Ted Kremenek9d64c152010-03-12 00:38:38 +00001103/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1104/// have the property type and issue diagnostics if they don't.
1105/// Also synthesize a getter/setter method if none exist (and update the
1106/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1107/// methods is the "right" thing to do.
1108void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001109 ObjCContainerDecl *CD,
1110 ObjCPropertyDecl *redeclaredProperty,
1111 ObjCContainerDecl *lexicalDC) {
1112
Ted Kremenek9d64c152010-03-12 00:38:38 +00001113 ObjCMethodDecl *GetterMethod, *SetterMethod;
1114
1115 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1116 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1117 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1118 property->getLocation());
1119
1120 if (SetterMethod) {
1121 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1122 property->getPropertyAttributes();
1123 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1124 Context.getCanonicalType(SetterMethod->getResultType()) !=
1125 Context.VoidTy)
1126 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1127 if (SetterMethod->param_size() != 1 ||
1128 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1129 Diag(property->getLocation(),
1130 diag::warn_accessor_property_type_mismatch)
1131 << property->getDeclName()
1132 << SetterMethod->getSelector();
1133 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1134 }
1135 }
1136
1137 // Synthesize getter/setter methods if none exist.
1138 // Find the default getter and if one not found, add one.
1139 // FIXME: The synthesized property we set here is misleading. We almost always
1140 // synthesize these methods unless the user explicitly provided prototypes
1141 // (which is odd, but allowed). Sema should be typechecking that the
1142 // declarations jive in that situation (which it is not currently).
1143 if (!GetterMethod) {
1144 // No instance method of same name as property getter name was found.
1145 // Declare a getter method and add it to the list of methods
1146 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001147 SourceLocation Loc = redeclaredProperty ?
1148 redeclaredProperty->getLocation() :
1149 property->getLocation();
1150
1151 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1152 property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001153 property->getType(), 0, CD, true, false, true,
1154 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001155 (property->getPropertyImplementation() ==
1156 ObjCPropertyDecl::Optional) ?
1157 ObjCMethodDecl::Optional :
1158 ObjCMethodDecl::Required);
1159 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001160
1161 AddPropertyAttrs(*this, GetterMethod, property);
1162
Ted Kremenek23173d72010-05-18 21:09:07 +00001163 // FIXME: Eventually this shouldn't be needed, as the lexical context
1164 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001165 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001166 GetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001167 } else
1168 // A user declared getter will be synthesize when @synthesize of
1169 // the property with the same name is seen in the @implementation
1170 GetterMethod->setSynthesized(true);
1171 property->setGetterMethodDecl(GetterMethod);
1172
1173 // Skip setter if property is read-only.
1174 if (!property->isReadOnly()) {
1175 // Find the default setter and if one not found, add one.
1176 if (!SetterMethod) {
1177 // No instance method of same name as property setter name was found.
1178 // Declare a setter method and add it to the list of methods
1179 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001180 SourceLocation Loc = redeclaredProperty ?
1181 redeclaredProperty->getLocation() :
1182 property->getLocation();
1183
1184 SetterMethod =
1185 ObjCMethodDecl::Create(Context, Loc, Loc,
1186 property->getSetterName(), Context.VoidTy, 0,
1187 CD, true, false, true, false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001188 (property->getPropertyImplementation() ==
1189 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001190 ObjCMethodDecl::Optional :
1191 ObjCMethodDecl::Required);
1192
Ted Kremenek9d64c152010-03-12 00:38:38 +00001193 // Invent the arguments for the setter. We don't bother making a
1194 // nice name for the argument.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001195 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001196 property->getIdentifier(),
1197 property->getType(),
1198 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001199 SC_None,
1200 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001201 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001202 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001203
1204 AddPropertyAttrs(*this, SetterMethod, property);
1205
Ted Kremenek9d64c152010-03-12 00:38:38 +00001206 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001207 // FIXME: Eventually this shouldn't be needed, as the lexical context
1208 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001209 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001210 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001211 } else
1212 // A user declared setter will be synthesize when @synthesize of
1213 // the property with the same name is seen in the @implementation
1214 SetterMethod->setSynthesized(true);
1215 property->setSetterMethodDecl(SetterMethod);
1216 }
1217 // Add any synthesized methods to the global pool. This allows us to
1218 // handle the following, which is supported by GCC (and part of the design).
1219 //
1220 // @interface Foo
1221 // @property double bar;
1222 // @end
1223 //
1224 // void thisIsUnfortunate() {
1225 // id foo;
1226 // double bar = [foo bar];
1227 // }
1228 //
1229 if (GetterMethod)
1230 AddInstanceMethodToGlobalPool(GetterMethod);
1231 if (SetterMethod)
1232 AddInstanceMethodToGlobalPool(SetterMethod);
1233}
1234
John McCalld226f652010-08-21 09:40:31 +00001235void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001236 SourceLocation Loc,
1237 unsigned &Attributes) {
1238 // FIXME: Improve the reported location.
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001239 if (!PDecl)
1240 return;
1241
1242 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001243 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001244
1245 // readonly and readwrite/assign/retain/copy conflict.
1246 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1247 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1248 ObjCDeclSpec::DQ_PR_assign |
1249 ObjCDeclSpec::DQ_PR_copy |
1250 ObjCDeclSpec::DQ_PR_retain))) {
1251 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1252 "readwrite" :
1253 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1254 "assign" :
1255 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1256 "copy" : "retain";
1257
1258 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1259 diag::err_objc_property_attr_mutually_exclusive :
1260 diag::warn_objc_property_attr_mutually_exclusive)
1261 << "readonly" << which;
1262 }
1263
1264 // Check for copy or retain on non-object types.
1265 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1266 !PropertyTy->isObjCObjectPointerType() &&
1267 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001268 !Context.isObjCNSObjectType(PropertyTy) &&
1269 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001270 Diag(Loc, diag::err_objc_property_requires_object)
1271 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1272 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1273 }
1274
1275 // Check for more than one of { assign, copy, retain }.
1276 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1277 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1278 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1279 << "assign" << "copy";
1280 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1281 }
1282 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1283 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1284 << "assign" << "retain";
1285 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1286 }
1287 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1288 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1289 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1290 << "copy" << "retain";
1291 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1292 }
1293 }
1294
1295 // Warn if user supplied no assignment attribute, property is
1296 // readwrite, and this is an object type.
1297 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1298 ObjCDeclSpec::DQ_PR_retain)) &&
1299 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1300 PropertyTy->isObjCObjectPointerType()) {
1301 // Skip this warning in gc-only mode.
1302 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1303 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1304
1305 // If non-gc code warn that this is likely inappropriate.
1306 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1307 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1308
1309 // FIXME: Implement warning dependent on NSCopying being
1310 // implemented. See also:
1311 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1312 // (please trim this list while you are at it).
1313 }
1314
1315 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1316 && getLangOptions().getGCMode() == LangOptions::GCOnly
1317 && PropertyTy->isBlockPointerType())
1318 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1319}