blob: ccb9a15ff257b9cdbcf2dcc5542da54a591e9763 [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
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Sema.h"
16#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"
Ted Kremenek9d64c152010-03-12 00:38:38 +000019
20using namespace clang;
21
Ted Kremenek28685ab2010-03-12 00:46:40 +000022//===----------------------------------------------------------------------===//
23// Grammar actions.
24//===----------------------------------------------------------------------===//
25
John McCalld226f652010-08-21 09:40:31 +000026Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
27 FieldDeclarator &FD,
28 ObjCDeclSpec &ODS,
29 Selector GetterSel,
30 Selector SetterSel,
31 Decl *ClassCategory,
32 bool *isOverridingProperty,
33 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000034 unsigned Attributes = ODS.getPropertyAttributes();
35 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
36 // default is readwrite!
37 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
38 // property is defaulted to 'assign' if it is readwrite and is
39 // not retain or copy
40 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
41 (isReadWrite &&
42 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
43 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000044
John McCallbf1a0282010-06-04 23:28:52 +000045 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
46 QualType T = TSI->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +000047 if (T->isReferenceType()) {
48 Diag(AtLoc, diag::error_reference_property);
John McCalld226f652010-08-21 09:40:31 +000049 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +000050 }
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000051 // Proceed with constructing the ObjCPropertDecls.
52 ObjCContainerDecl *ClassDecl =
John McCalld226f652010-08-21 09:40:31 +000053 cast<ObjCContainerDecl>(ClassCategory);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000054
Ted Kremenek28685ab2010-03-12 00:46:40 +000055 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000056 if (CDecl->IsClassExtension()) {
John McCalld226f652010-08-21 09:40:31 +000057 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000058 FD, GetterSel, SetterSel,
59 isAssign, isReadWrite,
60 Attributes,
61 isOverridingProperty, TSI,
62 MethodImplKind);
63 if (Res)
64 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
65 return Res;
66 }
67
John McCalld226f652010-08-21 09:40:31 +000068 Decl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
69 GetterSel, SetterSel,
70 isAssign, isReadWrite,
71 Attributes, TSI, MethodImplKind);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +000072 // Validate the attributes on the @property.
73 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
74 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000075}
Ted Kremenek2d2f9362010-03-12 00:49:00 +000076
John McCalld226f652010-08-21 09:40:31 +000077Decl *
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000078Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
79 SourceLocation AtLoc, FieldDeclarator &FD,
80 Selector GetterSel, Selector SetterSel,
81 const bool isAssign,
82 const bool isReadWrite,
83 const unsigned Attributes,
84 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +000085 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000086 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000087
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000088 // Diagnose if this property is already in continuation class.
89 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000090 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenek894ae6a2010-03-15 18:47:25 +000091
Ted Kremenek9f550ff2010-03-15 20:11:46 +000092 if (ObjCPropertyDecl *prevDecl =
93 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000094 Diag(AtLoc, diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +000095 Diag(prevDecl->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +000096 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000097 }
98
99 // Create a new ObjCPropertyDecl with the DeclContext being
100 // the class extension.
101 ObjCPropertyDecl *PDecl =
102 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
103 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000104 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
105 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
106 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
107 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
108
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000109 DC->addDecl(PDecl);
110
111 // We need to look in the @interface to see if the @property was
112 // already declared.
113 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
114 if (!CCPrimary) {
115 Diag(CDecl->getLocation(), diag::err_continuation_class);
116 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000117 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000118 }
119
120 // Find the property in continuation class's primary class only.
121 ObjCPropertyDecl *PIDecl =
122 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
123
124 if (!PIDecl) {
125 // No matching property found in the primary class. Just fall thru
126 // and add property to continuation class's primary class.
127 ObjCPropertyDecl *PDecl =
128 CreatePropertyDecl(S, CCPrimary, AtLoc,
129 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000130 Attributes, T, MethodImplKind, DC);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000131 // Mark written attribute as having no attribute because
132 // this is not a user-written property declaration in primary
133 // class.
134 PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000135
136 // A case of continuation class adding a new property in the class. This
137 // is not what it was meant for. However, gcc supports it and so should we.
138 // Make sure setter/getters are declared here.
139 ProcessPropertyDecl(PDecl, CCPrimary);
John McCalld226f652010-08-21 09:40:31 +0000140 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000141
142 }
143
144 // The property 'PIDecl's readonly attribute will be over-ridden
145 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000146 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000147 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
148 unsigned retainCopyNonatomic =
149 (ObjCPropertyDecl::OBJC_PR_retain |
150 ObjCPropertyDecl::OBJC_PR_copy |
151 ObjCPropertyDecl::OBJC_PR_nonatomic);
152 if ((Attributes & retainCopyNonatomic) !=
153 (PIkind & retainCopyNonatomic)) {
154 Diag(AtLoc, diag::warn_property_attr_mismatch);
155 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000156 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000157 DeclContext *DC = cast<DeclContext>(CCPrimary);
158 if (!ObjCPropertyDecl::findPropertyDecl(DC,
159 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000160 // Protocol is not in the primary class. Must build one for it.
161 ObjCDeclSpec ProtocolPropertyODS;
162 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
163 // and ObjCPropertyDecl::PropertyAttributeKind have identical
164 // values. Should consolidate both into one enum type.
165 ProtocolPropertyODS.
166 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
167 PIkind);
168
John McCalld226f652010-08-21 09:40:31 +0000169 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000170 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
171 PIDecl->getGetterName(),
172 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000173 CCPrimary, isOverridingProperty,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000174 MethodImplKind);
John McCalld226f652010-08-21 09:40:31 +0000175 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000176 }
177 PIDecl->makeitReadWriteAttribute();
178 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
179 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
180 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
181 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
182 PIDecl->setSetterName(SetterSel);
183 } else {
184 Diag(AtLoc, diag::err_use_continuation_class)
185 << CCPrimary->getDeclName();
186 Diag(PIDecl->getLocation(), diag::note_property_declare);
187 }
188 *isOverridingProperty = true;
189 // Make sure setter decl is synthesized, and added to primary class's list.
190 ProcessPropertyDecl(PIDecl, CCPrimary);
John McCalld226f652010-08-21 09:40:31 +0000191 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000192}
193
194ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
195 ObjCContainerDecl *CDecl,
196 SourceLocation AtLoc,
197 FieldDeclarator &FD,
198 Selector GetterSel,
199 Selector SetterSel,
200 const bool isAssign,
201 const bool isReadWrite,
202 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000203 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000204 tok::ObjCKeywordKind MethodImplKind,
205 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000206 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000207 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000208
209 // Issue a warning if property is 'assign' as default and its object, which is
210 // gc'able conforms to NSCopying protocol
211 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
212 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000213 if (const ObjCObjectPointerType *ObjPtrTy =
214 T->getAs<ObjCObjectPointerType>()) {
215 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
216 if (IDecl)
217 if (ObjCProtocolDecl* PNSCopying =
218 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
219 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
220 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000221 }
John McCallc12c5bb2010-05-15 11:32:37 +0000222 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000223 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
224
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000225 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000226 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
227 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000228 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000229
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000230 if (ObjCPropertyDecl *prevDecl =
231 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000232 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000233 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000234 PDecl->setInvalidDecl();
235 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000236 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000237 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000238 if (lexicalDC)
239 PDecl->setLexicalDeclContext(lexicalDC);
240 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000241
242 if (T->isArrayType() || T->isFunctionType()) {
243 Diag(AtLoc, diag::err_property_type) << T;
244 PDecl->setInvalidDecl();
245 }
246
247 ProcessDeclAttributes(S, PDecl, FD.D);
248
249 // Regardless of setter/getter attribute, we save the default getter/setter
250 // selector names in anticipation of declaration of setter/getter methods.
251 PDecl->setGetterName(GetterSel);
252 PDecl->setSetterName(SetterSel);
253
254 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
255 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
256
257 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
258 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
259
260 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
261 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
262
263 if (isReadWrite)
264 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
265
266 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
267 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
268
269 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
270 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
271
272 if (isAssign)
273 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
274
275 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
276 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
277
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000278 PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes());
279
Ted Kremenek28685ab2010-03-12 00:46:40 +0000280 if (MethodImplKind == tok::objc_required)
281 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
282 else if (MethodImplKind == tok::objc_optional)
283 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000284
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000285 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000286}
287
288
289/// ActOnPropertyImplDecl - This routine performs semantic checks and
290/// builds the AST node for a property implementation declaration; declared
291/// as @synthesize or @dynamic.
292///
John McCalld226f652010-08-21 09:40:31 +0000293Decl *Sema::ActOnPropertyImplDecl(Scope *S,
294 SourceLocation AtLoc,
295 SourceLocation PropertyLoc,
296 bool Synthesize,
297 Decl *ClassCatImpDecl,
298 IdentifierInfo *PropertyId,
299 IdentifierInfo *PropertyIvar) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000300 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000301 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000302 // Make sure we have a context for the property implementation declaration.
303 if (!ClassImpDecl) {
304 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000305 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000306 }
307 ObjCPropertyDecl *property = 0;
308 ObjCInterfaceDecl* IDecl = 0;
309 // Find the class or category class where this property must have
310 // a declaration.
311 ObjCImplementationDecl *IC = 0;
312 ObjCCategoryImplDecl* CatImplClass = 0;
313 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
314 IDecl = IC->getClassInterface();
315 // We always synthesize an interface for an implementation
316 // without an interface decl. So, IDecl is always non-zero.
317 assert(IDecl &&
318 "ActOnPropertyImplDecl - @implementation without @interface");
319
320 // Look for this property declaration in the @implementation's @interface
321 property = IDecl->FindPropertyDeclaration(PropertyId);
322 if (!property) {
323 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000324 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000325 }
326 if (const ObjCCategoryDecl *CD =
327 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
328 if (!CD->IsClassExtension()) {
329 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
330 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000331 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000332 }
333 }
334 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
335 if (Synthesize) {
336 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000337 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000338 }
339 IDecl = CatImplClass->getClassInterface();
340 if (!IDecl) {
341 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000342 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000343 }
344 ObjCCategoryDecl *Category =
345 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
346
347 // If category for this implementation not found, it is an error which
348 // has already been reported eralier.
349 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000350 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000351 // Look for this property declaration in @implementation's category
352 property = Category->FindPropertyDeclaration(PropertyId);
353 if (!property) {
354 Diag(PropertyLoc, diag::error_bad_category_property_decl)
355 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000356 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000357 }
358 } else {
359 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000360 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000361 }
362 ObjCIvarDecl *Ivar = 0;
363 // Check that we have a valid, previously declared ivar for @synthesize
364 if (Synthesize) {
365 // @synthesize
366 if (!PropertyIvar)
367 PropertyIvar = PropertyId;
368 QualType PropType = Context.getCanonicalType(property->getType());
369 // Check that this is a previously declared 'ivar' in 'IDecl' interface
370 ObjCInterfaceDecl *ClassDeclared;
371 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
372 if (!Ivar) {
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000373 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000374 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian2846b2b2010-04-06 23:36:17 +0000375 ObjCIvarDecl::Protected,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000376 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000377 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000378 IDecl->makeDeclVisibleInContext(Ivar, false);
379 property->setPropertyIvarDecl(Ivar);
380
381 if (!getLangOptions().ObjCNonFragileABI)
382 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
383 // Note! I deliberately want it to fall thru so, we have a
384 // a property implementation and to avoid future warnings.
385 } else if (getLangOptions().ObjCNonFragileABI &&
386 ClassDeclared != IDecl) {
387 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
388 << property->getDeclName() << Ivar->getDeclName()
389 << ClassDeclared->getDeclName();
390 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000391 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000392 // Note! I deliberately want it to fall thru so more errors are caught.
393 }
394 QualType IvarType = Context.getCanonicalType(Ivar->getType());
395
396 // Check that type of property and its ivar are type compatible.
397 if (PropType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000398 bool compat = false;
399 if (isa<ObjCObjectPointerType>(PropType)
400 && isa<ObjCObjectPointerType>(IvarType))
401 compat =
402 Context.canAssignObjCInterfaces(
403 PropType->getAs<ObjCObjectPointerType>(),
404 IvarType->getAs<ObjCObjectPointerType>());
405 else
406 compat = (CheckAssignmentConstraints(PropType, IvarType) == Compatible);
407 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000408 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000409 << property->getDeclName() << PropType
410 << Ivar->getDeclName() << IvarType;
411 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000412 // Note! I deliberately want it to fall thru so, we have a
413 // a property implementation and to avoid future warnings.
414 }
415
416 // FIXME! Rules for properties are somewhat different that those
417 // for assignments. Use a new routine to consolidate all cases;
418 // specifically for property redeclarations as well as for ivars.
419 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
420 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
421 if (lhsType != rhsType &&
422 lhsType->isArithmeticType()) {
423 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000424 << property->getDeclName() << PropType
425 << Ivar->getDeclName() << IvarType;
426 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000427 // Fall thru - see previous comment
428 }
429 // __weak is explicit. So it works on Canonical type.
430 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
431 getLangOptions().getGCMode() != LangOptions::NonGC) {
432 Diag(PropertyLoc, diag::error_weak_property)
433 << property->getDeclName() << Ivar->getDeclName();
434 // Fall thru - see previous comment
435 }
436 if ((property->getType()->isObjCObjectPointerType() ||
437 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
438 getLangOptions().getGCMode() != LangOptions::NonGC) {
439 Diag(PropertyLoc, diag::error_strong_property)
440 << property->getDeclName() << Ivar->getDeclName();
441 // Fall thru - see previous comment
442 }
443 }
444 } else if (PropertyIvar)
445 // @dynamic
446 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
447 assert (property && "ActOnPropertyImplDecl - property declaration missing");
448 ObjCPropertyImplDecl *PIDecl =
449 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
450 property,
451 (Synthesize ?
452 ObjCPropertyImplDecl::Synthesize
453 : ObjCPropertyImplDecl::Dynamic),
454 Ivar);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000455 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
456 getterMethod->createImplicitParams(Context, IDecl);
457 if (getLangOptions().CPlusPlus && Synthesize) {
458 // For Objective-C++, need to synthesize the AST for the IVAR object to be
459 // returned by the getter as it must conform to C++'s copy-return rules.
460 // FIXME. Eventually we want to do this for Objective-C as well.
461 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
462 DeclRefExpr *SelfExpr =
463 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
464 SourceLocation());
465 Expr *IvarRefExpr =
466 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
467 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000468 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000469 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000470 SourceLocation(),
471 getterMethod->getResultType(),
472 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000473 SourceLocation(),
474 Owned(IvarRefExpr));
475 if (!Res.isInvalid()) {
476 Expr *ResExpr = Res.takeAs<Expr>();
477 if (ResExpr)
478 ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr);
479 PIDecl->setGetterCXXConstructor(ResExpr);
480 }
481 }
482 }
483 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
484 setterMethod->createImplicitParams(Context, IDecl);
485 if (getLangOptions().CPlusPlus && Synthesize) {
486 // FIXME. Eventually we want to do this for Objective-C as well.
487 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
488 DeclRefExpr *SelfExpr =
489 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
490 SourceLocation());
491 Expr *lhs =
492 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
493 SelfExpr, true, true);
494 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
495 ParmVarDecl *Param = (*P);
496 Expr *rhs = new (Context) DeclRefExpr(Param,Param->getType(),
497 SourceLocation());
John McCall60d7b3a2010-08-24 06:29:42 +0000498 ExprResult Res = BuildBinOp(S, SourceLocation(),
499 BinaryOperator::Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000500 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
501 }
502 }
503
Ted Kremenek28685ab2010-03-12 00:46:40 +0000504 if (IC) {
505 if (Synthesize)
506 if (ObjCPropertyImplDecl *PPIDecl =
507 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
508 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
509 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
510 << PropertyIvar;
511 Diag(PPIDecl->getLocation(), diag::note_previous_use);
512 }
513
514 if (ObjCPropertyImplDecl *PPIDecl
515 = IC->FindPropertyImplDecl(PropertyId)) {
516 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
517 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000518 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000519 }
520 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000521 if (getLangOptions().ObjCNonFragileABI2) {
522 // Diagnose if an ivar was lazily synthesdized due to a previous
523 // use and if 1) property is @dynamic or 2) property is synthesized
524 // but it requires a dirreferently named ivar.
525 ObjCInterfaceDecl *ClassDeclared;
526 ObjCIvarDecl *Ivar = 0;
527 if (!Synthesize)
528 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
529 else {
530 if (PropertyIvar && PropertyIvar != PropertyId)
531 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
532 }
533 if (Ivar && Ivar->getSynthesize()) {
534 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
535 << PropertyId;
536 Ivar->setInvalidDecl();
537 }
538 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000539 } else {
540 if (Synthesize)
541 if (ObjCPropertyImplDecl *PPIDecl =
542 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
543 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
544 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
545 << PropertyIvar;
546 Diag(PPIDecl->getLocation(), diag::note_previous_use);
547 }
548
549 if (ObjCPropertyImplDecl *PPIDecl =
550 CatImplClass->FindPropertyImplDecl(PropertyId)) {
551 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
552 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000553 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000554 }
555 CatImplClass->addPropertyImplementation(PIDecl);
556 }
557
John McCalld226f652010-08-21 09:40:31 +0000558 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000559}
560
561//===----------------------------------------------------------------------===//
562// Helper methods.
563//===----------------------------------------------------------------------===//
564
Ted Kremenek9d64c152010-03-12 00:38:38 +0000565/// DiagnosePropertyMismatch - Compares two properties for their
566/// attributes and types and warns on a variety of inconsistencies.
567///
568void
569Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
570 ObjCPropertyDecl *SuperProperty,
571 const IdentifierInfo *inheritedName) {
572 ObjCPropertyDecl::PropertyAttributeKind CAttr =
573 Property->getPropertyAttributes();
574 ObjCPropertyDecl::PropertyAttributeKind SAttr =
575 SuperProperty->getPropertyAttributes();
576 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
577 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
578 Diag(Property->getLocation(), diag::warn_readonly_property)
579 << Property->getDeclName() << inheritedName;
580 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
581 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
582 Diag(Property->getLocation(), diag::warn_property_attribute)
583 << Property->getDeclName() << "copy" << inheritedName;
584 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
585 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
586 Diag(Property->getLocation(), diag::warn_property_attribute)
587 << Property->getDeclName() << "retain" << inheritedName;
588
589 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
590 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
591 Diag(Property->getLocation(), diag::warn_property_attribute)
592 << Property->getDeclName() << "atomic" << inheritedName;
593 if (Property->getSetterName() != SuperProperty->getSetterName())
594 Diag(Property->getLocation(), diag::warn_property_attribute)
595 << Property->getDeclName() << "setter" << inheritedName;
596 if (Property->getGetterName() != SuperProperty->getGetterName())
597 Diag(Property->getLocation(), diag::warn_property_attribute)
598 << Property->getDeclName() << "getter" << inheritedName;
599
600 QualType LHSType =
601 Context.getCanonicalType(SuperProperty->getType());
602 QualType RHSType =
603 Context.getCanonicalType(Property->getType());
604
605 if (!Context.typesAreCompatible(LHSType, RHSType)) {
606 // FIXME: Incorporate this test with typesAreCompatible.
607 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
608 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
609 return;
610 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
611 << Property->getType() << SuperProperty->getType() << inheritedName;
612 }
613}
614
615bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
616 ObjCMethodDecl *GetterMethod,
617 SourceLocation Loc) {
618 if (GetterMethod &&
619 GetterMethod->getResultType() != property->getType()) {
620 AssignConvertType result = Incompatible;
621 if (property->getType()->isObjCObjectPointerType())
622 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
623 property->getType());
624 if (result != Compatible) {
625 Diag(Loc, diag::warn_accessor_property_type_mismatch)
626 << property->getDeclName()
627 << GetterMethod->getSelector();
628 Diag(GetterMethod->getLocation(), diag::note_declared_at);
629 return true;
630 }
631 }
632 return false;
633}
634
635/// ComparePropertiesInBaseAndSuper - This routine compares property
636/// declarations in base and its super class, if any, and issues
637/// diagnostics in a variety of inconsistant situations.
638///
639void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
640 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
641 if (!SDecl)
642 return;
643 // FIXME: O(N^2)
644 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
645 E = SDecl->prop_end(); S != E; ++S) {
646 ObjCPropertyDecl *SuperPDecl = (*S);
647 // Does property in super class has declaration in current class?
648 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
649 E = IDecl->prop_end(); I != E; ++I) {
650 ObjCPropertyDecl *PDecl = (*I);
651 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
652 DiagnosePropertyMismatch(PDecl, SuperPDecl,
653 SDecl->getIdentifier());
654 }
655 }
656}
657
658/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
659/// of properties declared in a protocol and compares their attribute against
660/// the same property declared in the class or category.
661void
662Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
663 ObjCProtocolDecl *PDecl) {
664 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
665 if (!IDecl) {
666 // Category
667 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
668 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
669 if (!CatDecl->IsClassExtension())
670 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
671 E = PDecl->prop_end(); P != E; ++P) {
672 ObjCPropertyDecl *Pr = (*P);
673 ObjCCategoryDecl::prop_iterator CP, CE;
674 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000675 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000676 if ((*CP)->getIdentifier() == Pr->getIdentifier())
677 break;
678 if (CP != CE)
679 // Property protocol already exist in class. Diagnose any mismatch.
680 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
681 }
682 return;
683 }
684 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
685 E = PDecl->prop_end(); P != E; ++P) {
686 ObjCPropertyDecl *Pr = (*P);
687 ObjCInterfaceDecl::prop_iterator CP, CE;
688 // Is this property already in class's list of properties?
689 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
690 if ((*CP)->getIdentifier() == Pr->getIdentifier())
691 break;
692 if (CP != CE)
693 // Property protocol already exist in class. Diagnose any mismatch.
694 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
695 }
696}
697
698/// CompareProperties - This routine compares properties
699/// declared in 'ClassOrProtocol' objects (which can be a class or an
700/// inherited protocol with the list of properties for class/category 'CDecl'
701///
John McCalld226f652010-08-21 09:40:31 +0000702void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
703 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000704 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
705
706 if (!IDecl) {
707 // Category
708 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
709 assert (CatDecl && "CompareProperties");
710 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
711 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
712 E = MDecl->protocol_end(); P != E; ++P)
713 // Match properties of category with those of protocol (*P)
714 MatchOneProtocolPropertiesInClass(CatDecl, *P);
715
716 // Go thru the list of protocols for this category and recursively match
717 // their properties with those in the category.
718 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
719 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000720 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000721 } else {
722 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
723 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
724 E = MD->protocol_end(); P != E; ++P)
725 MatchOneProtocolPropertiesInClass(CatDecl, *P);
726 }
727 return;
728 }
729
730 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
731 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
732 E = MDecl->protocol_end(); P != E; ++P)
733 // Match properties of class IDecl with those of protocol (*P).
734 MatchOneProtocolPropertiesInClass(IDecl, *P);
735
736 // Go thru the list of protocols for this class and recursively match
737 // their properties with those declared in the class.
738 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
739 E = IDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000740 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000741 } else {
742 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
743 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
744 E = MD->protocol_end(); P != E; ++P)
745 MatchOneProtocolPropertiesInClass(IDecl, *P);
746 }
747}
748
749/// isPropertyReadonly - Return true if property is readonly, by searching
750/// for the property in the class and in its categories and implementations
751///
752bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
753 ObjCInterfaceDecl *IDecl) {
754 // by far the most common case.
755 if (!PDecl->isReadOnly())
756 return false;
757 // Even if property is ready only, if interface has a user defined setter,
758 // it is not considered read only.
759 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
760 return false;
761
762 // Main class has the property as 'readonly'. Must search
763 // through the category list to see if the property's
764 // attribute has been over-ridden to 'readwrite'.
765 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
766 Category; Category = Category->getNextClassCategory()) {
767 // Even if property is ready only, if a category has a user defined setter,
768 // it is not considered read only.
769 if (Category->getInstanceMethod(PDecl->getSetterName()))
770 return false;
771 ObjCPropertyDecl *P =
772 Category->FindPropertyDeclaration(PDecl->getIdentifier());
773 if (P && !P->isReadOnly())
774 return false;
775 }
776
777 // Also, check for definition of a setter method in the implementation if
778 // all else failed.
779 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
780 if (ObjCImplementationDecl *IMD =
781 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
782 if (IMD->getInstanceMethod(PDecl->getSetterName()))
783 return false;
784 } else if (ObjCCategoryImplDecl *CIMD =
785 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
786 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
787 return false;
788 }
789 }
790 // Lastly, look through the implementation (if one is in scope).
791 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
792 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
793 return false;
794 // If all fails, look at the super class.
795 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
796 return isPropertyReadonly(PDecl, SIDecl);
797 return true;
798}
799
800/// CollectImmediateProperties - This routine collects all properties in
801/// the class and its conforming protocols; but not those it its super class.
802void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000803 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
804 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000805 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
806 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
807 E = IDecl->prop_end(); P != E; ++P) {
808 ObjCPropertyDecl *Prop = (*P);
809 PropMap[Prop->getIdentifier()] = Prop;
810 }
811 // scan through class's protocols.
812 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
813 E = IDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000814 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000815 }
816 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
817 if (!CATDecl->IsClassExtension())
818 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
819 E = CATDecl->prop_end(); P != E; ++P) {
820 ObjCPropertyDecl *Prop = (*P);
821 PropMap[Prop->getIdentifier()] = Prop;
822 }
823 // scan through class's protocols.
824 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
825 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000826 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000827 }
828 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
829 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
830 E = PDecl->prop_end(); P != E; ++P) {
831 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000832 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
833 // Exclude property for protocols which conform to class's super-class,
834 // as super-class has to implement the property.
835 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
836 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
837 if (!PropEntry)
838 PropEntry = Prop;
839 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000840 }
841 // scan through protocol's protocols.
842 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
843 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000844 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000845 }
846}
847
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000848/// CollectClassPropertyImplementations - This routine collects list of
849/// properties to be implemented in the class. This includes, class's
850/// and its conforming protocols' properties.
851static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
852 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
853 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
854 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
855 E = IDecl->prop_end(); P != E; ++P) {
856 ObjCPropertyDecl *Prop = (*P);
857 PropMap[Prop->getIdentifier()] = Prop;
858 }
859 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
860 E = IDecl->protocol_end(); PI != E; ++PI)
861 CollectClassPropertyImplementations((*PI), PropMap);
862 }
863 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
864 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
865 E = PDecl->prop_end(); P != E; ++P) {
866 ObjCPropertyDecl *Prop = (*P);
867 PropMap[Prop->getIdentifier()] = Prop;
868 }
869 // scan through protocol's protocols.
870 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
871 E = PDecl->protocol_end(); PI != E; ++PI)
872 CollectClassPropertyImplementations((*PI), PropMap);
873 }
874}
875
876/// CollectSuperClassPropertyImplementations - This routine collects list of
877/// properties to be implemented in super class(s) and also coming from their
878/// conforming protocols.
879static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
880 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
881 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
882 while (SDecl) {
883 CollectClassPropertyImplementations(SDecl, PropMap);
884 SDecl = SDecl->getSuperClass();
885 }
886 }
887}
888
Ted Kremenek9d64c152010-03-12 00:38:38 +0000889/// LookupPropertyDecl - Looks up a property in the current class and all
890/// its protocols.
891ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
892 IdentifierInfo *II) {
893 if (const ObjCInterfaceDecl *IDecl =
894 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
895 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
896 E = IDecl->prop_end(); P != E; ++P) {
897 ObjCPropertyDecl *Prop = (*P);
898 if (Prop->getIdentifier() == II)
899 return Prop;
900 }
901 // scan through class's protocols.
902 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
903 E = IDecl->protocol_end(); PI != E; ++PI) {
904 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
905 if (Prop)
906 return Prop;
907 }
908 }
909 else if (const ObjCProtocolDecl *PDecl =
910 dyn_cast<ObjCProtocolDecl>(CDecl)) {
911 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
912 E = PDecl->prop_end(); P != E; ++P) {
913 ObjCPropertyDecl *Prop = (*P);
914 if (Prop->getIdentifier() == II)
915 return Prop;
916 }
917 // scan through protocol's protocols.
918 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
919 E = PDecl->protocol_end(); PI != E; ++PI) {
920 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
921 if (Prop)
922 return Prop;
923 }
924 }
925 return 0;
926}
927
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000928/// DefaultSynthesizeProperties - This routine default synthesizes all
929/// properties which must be synthesized in class's @implementation.
930void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
931 ObjCInterfaceDecl *IDecl) {
932
933 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
934 CollectClassPropertyImplementations(IDecl, PropMap);
935 if (PropMap.empty())
936 return;
937 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
938 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
939
940 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
941 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
942 ObjCPropertyDecl *Prop = P->second;
943 // If property to be implemented in the super class, ignore.
944 if (SuperPropMap[Prop->getIdentifier()])
945 continue;
946 // Is there a matching propery synthesize/dynamic?
947 if (Prop->isInvalidDecl() ||
948 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
949 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
950 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000951 // Property may have been synthesized by user.
952 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
953 continue;
954
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000955 ActOnPropertyImplDecl(S, IMPDecl->getLocation(), IMPDecl->getLocation(),
John McCalld226f652010-08-21 09:40:31 +0000956 true, IMPDecl,
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000957 Prop->getIdentifier(), Prop->getIdentifier());
958 }
959}
Ted Kremenek9d64c152010-03-12 00:38:38 +0000960
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000961void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +0000962 ObjCContainerDecl *CDecl,
963 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000964 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
965 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
966 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
967
Ted Kremenek9d64c152010-03-12 00:38:38 +0000968 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000969 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000970 if (PropMap.empty())
971 return;
972
973 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
974 for (ObjCImplDecl::propimpl_iterator
975 I = IMPDecl->propimpl_begin(),
976 EI = IMPDecl->propimpl_end(); I != EI; ++I)
977 PropImplMap.insert((*I)->getPropertyDecl());
978
979 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
980 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
981 ObjCPropertyDecl *Prop = P->second;
982 // Is there a matching propery synthesize/dynamic?
983 if (Prop->isInvalidDecl() ||
984 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
985 PropImplMap.count(Prop))
986 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000987 if (!InsMap.count(Prop->getGetterName())) {
988 Diag(Prop->getLocation(),
989 isa<ObjCCategoryDecl>(CDecl) ?
990 diag::warn_setter_getter_impl_required_in_category :
991 diag::warn_setter_getter_impl_required)
992 << Prop->getDeclName() << Prop->getGetterName();
993 Diag(IMPDecl->getLocation(),
994 diag::note_property_impl_required);
995 }
996
997 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
998 Diag(Prop->getLocation(),
999 isa<ObjCCategoryDecl>(CDecl) ?
1000 diag::warn_setter_getter_impl_required_in_category :
1001 diag::warn_setter_getter_impl_required)
1002 << Prop->getDeclName() << Prop->getSetterName();
1003 Diag(IMPDecl->getLocation(),
1004 diag::note_property_impl_required);
1005 }
1006 }
1007}
1008
1009void
1010Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1011 ObjCContainerDecl* IDecl) {
1012 // Rules apply in non-GC mode only
1013 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1014 return;
1015 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1016 E = IDecl->prop_end();
1017 I != E; ++I) {
1018 ObjCPropertyDecl *Property = (*I);
1019 unsigned Attributes = Property->getPropertyAttributes();
1020 // We only care about readwrite atomic property.
1021 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1022 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1023 continue;
1024 if (const ObjCPropertyImplDecl *PIDecl
1025 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1026 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1027 continue;
1028 ObjCMethodDecl *GetterMethod =
1029 IMPDecl->getInstanceMethod(Property->getGetterName());
1030 ObjCMethodDecl *SetterMethod =
1031 IMPDecl->getInstanceMethod(Property->getSetterName());
1032 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1033 SourceLocation MethodLoc =
1034 (GetterMethod ? GetterMethod->getLocation()
1035 : SetterMethod->getLocation());
1036 Diag(MethodLoc, diag::warn_atomic_property_rule)
1037 << Property->getIdentifier();
1038 Diag(Property->getLocation(), diag::note_property_declare);
1039 }
1040 }
1041 }
1042}
1043
1044/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1045/// have the property type and issue diagnostics if they don't.
1046/// Also synthesize a getter/setter method if none exist (and update the
1047/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1048/// methods is the "right" thing to do.
1049void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1050 ObjCContainerDecl *CD) {
1051 ObjCMethodDecl *GetterMethod, *SetterMethod;
1052
1053 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1054 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1055 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1056 property->getLocation());
1057
1058 if (SetterMethod) {
1059 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1060 property->getPropertyAttributes();
1061 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1062 Context.getCanonicalType(SetterMethod->getResultType()) !=
1063 Context.VoidTy)
1064 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1065 if (SetterMethod->param_size() != 1 ||
1066 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1067 Diag(property->getLocation(),
1068 diag::warn_accessor_property_type_mismatch)
1069 << property->getDeclName()
1070 << SetterMethod->getSelector();
1071 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1072 }
1073 }
1074
1075 // Synthesize getter/setter methods if none exist.
1076 // Find the default getter and if one not found, add one.
1077 // FIXME: The synthesized property we set here is misleading. We almost always
1078 // synthesize these methods unless the user explicitly provided prototypes
1079 // (which is odd, but allowed). Sema should be typechecking that the
1080 // declarations jive in that situation (which it is not currently).
1081 if (!GetterMethod) {
1082 // No instance method of same name as property getter name was found.
1083 // Declare a getter method and add it to the list of methods
1084 // for this class.
1085 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1086 property->getLocation(), property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001087 property->getType(), 0, CD, true, false, true,
1088 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001089 (property->getPropertyImplementation() ==
1090 ObjCPropertyDecl::Optional) ?
1091 ObjCMethodDecl::Optional :
1092 ObjCMethodDecl::Required);
1093 CD->addDecl(GetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001094 // FIXME: Eventually this shouldn't be needed, as the lexical context
1095 // and the real context should be the same.
1096 if (DeclContext *lexicalDC = property->getLexicalDeclContext())
1097 GetterMethod->setLexicalDeclContext(lexicalDC);
1098
Ted Kremenek9d64c152010-03-12 00:38:38 +00001099 } else
1100 // A user declared getter will be synthesize when @synthesize of
1101 // the property with the same name is seen in the @implementation
1102 GetterMethod->setSynthesized(true);
1103 property->setGetterMethodDecl(GetterMethod);
1104
1105 // Skip setter if property is read-only.
1106 if (!property->isReadOnly()) {
1107 // Find the default setter and if one not found, add one.
1108 if (!SetterMethod) {
1109 // No instance method of same name as property setter name was found.
1110 // Declare a setter method and add it to the list of methods
1111 // for this class.
1112 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1113 property->getLocation(),
1114 property->getSetterName(),
1115 Context.VoidTy, 0, CD, true, false, true,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001116 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001117 (property->getPropertyImplementation() ==
1118 ObjCPropertyDecl::Optional) ?
1119 ObjCMethodDecl::Optional :
1120 ObjCMethodDecl::Required);
1121 // Invent the arguments for the setter. We don't bother making a
1122 // nice name for the argument.
1123 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1124 property->getLocation(),
1125 property->getIdentifier(),
1126 property->getType(),
1127 /*TInfo=*/0,
1128 VarDecl::None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001129 VarDecl::None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001130 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001131 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001132 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001133 // FIXME: Eventually this shouldn't be needed, as the lexical context
1134 // and the real context should be the same.
1135 if (DeclContext *lexicalDC = property->getLexicalDeclContext())
1136 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001137 } else
1138 // A user declared setter will be synthesize when @synthesize of
1139 // the property with the same name is seen in the @implementation
1140 SetterMethod->setSynthesized(true);
1141 property->setSetterMethodDecl(SetterMethod);
1142 }
1143 // Add any synthesized methods to the global pool. This allows us to
1144 // handle the following, which is supported by GCC (and part of the design).
1145 //
1146 // @interface Foo
1147 // @property double bar;
1148 // @end
1149 //
1150 // void thisIsUnfortunate() {
1151 // id foo;
1152 // double bar = [foo bar];
1153 // }
1154 //
1155 if (GetterMethod)
1156 AddInstanceMethodToGlobalPool(GetterMethod);
1157 if (SetterMethod)
1158 AddInstanceMethodToGlobalPool(SetterMethod);
1159}
1160
John McCalld226f652010-08-21 09:40:31 +00001161void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001162 SourceLocation Loc,
1163 unsigned &Attributes) {
1164 // FIXME: Improve the reported location.
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001165 if (!PDecl)
1166 return;
1167
1168 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001169 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001170
1171 // readonly and readwrite/assign/retain/copy conflict.
1172 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1173 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1174 ObjCDeclSpec::DQ_PR_assign |
1175 ObjCDeclSpec::DQ_PR_copy |
1176 ObjCDeclSpec::DQ_PR_retain))) {
1177 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1178 "readwrite" :
1179 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1180 "assign" :
1181 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1182 "copy" : "retain";
1183
1184 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1185 diag::err_objc_property_attr_mutually_exclusive :
1186 diag::warn_objc_property_attr_mutually_exclusive)
1187 << "readonly" << which;
1188 }
1189
1190 // Check for copy or retain on non-object types.
1191 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1192 !PropertyTy->isObjCObjectPointerType() &&
1193 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001194 !Context.isObjCNSObjectType(PropertyTy) &&
1195 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001196 Diag(Loc, diag::err_objc_property_requires_object)
1197 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1198 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1199 }
1200
1201 // Check for more than one of { assign, copy, retain }.
1202 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1203 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1204 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1205 << "assign" << "copy";
1206 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1207 }
1208 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1209 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1210 << "assign" << "retain";
1211 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1212 }
1213 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1214 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1215 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1216 << "copy" << "retain";
1217 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1218 }
1219 }
1220
1221 // Warn if user supplied no assignment attribute, property is
1222 // readwrite, and this is an object type.
1223 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1224 ObjCDeclSpec::DQ_PR_retain)) &&
1225 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1226 PropertyTy->isObjCObjectPointerType()) {
1227 // Skip this warning in gc-only mode.
1228 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1229 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1230
1231 // If non-gc code warn that this is likely inappropriate.
1232 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1233 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1234
1235 // FIXME: Implement warning dependent on NSCopying being
1236 // implemented. See also:
1237 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1238 // (please trim this list while you are at it).
1239 }
1240
1241 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1242 && getLangOptions().getGCMode() == LangOptions::GCOnly
1243 && PropertyTy->isBlockPointerType())
1244 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1245}