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