blob: a27d2a8a2f5f223d8bbc441029c9bef9d764b17d [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
15#include "Sema.h"
Fariborz Jahanian17cb3262010-05-05 21:52:17 +000016#include "SemaInit.h"
17#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
25Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
26 FieldDeclarator &FD,
27 ObjCDeclSpec &ODS,
28 Selector GetterSel,
29 Selector SetterSel,
30 DeclPtrTy ClassCategory,
31 bool *isOverridingProperty,
32 tok::ObjCKeywordKind MethodImplKind) {
33 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);
48 return DeclPtrTy();
49 }
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000050 // Proceed with constructing the ObjCPropertDecls.
51 ObjCContainerDecl *ClassDecl =
52 cast<ObjCContainerDecl>(ClassCategory.getAs<Decl>());
53
Ted Kremenek28685ab2010-03-12 00:46:40 +000054 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000055 if (CDecl->IsClassExtension())
56 return HandlePropertyInClassExtension(S, CDecl, AtLoc,
57 FD, GetterSel, SetterSel,
58 isAssign, isReadWrite,
59 Attributes,
John McCall83a230c2010-06-04 20:50:08 +000060 isOverridingProperty, TSI,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000061 MethodImplKind);
Ted Kremenek28685ab2010-03-12 00:46:40 +000062
Fariborz Jahanian842f07b2010-03-30 22:40:11 +000063 DeclPtrTy Res = DeclPtrTy::make(CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000064 GetterSel, SetterSel,
65 isAssign, isReadWrite,
John McCall83a230c2010-06-04 20:50:08 +000066 Attributes, TSI, MethodImplKind));
Fariborz Jahanian842f07b2010-03-30 22:40:11 +000067 // Validate the attributes on the @property.
68 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
69 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000070}
Ted Kremenek2d2f9362010-03-12 00:49:00 +000071
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000072Sema::DeclPtrTy
73Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
74 SourceLocation AtLoc, FieldDeclarator &FD,
75 Selector GetterSel, Selector SetterSel,
76 const bool isAssign,
77 const bool isReadWrite,
78 const unsigned Attributes,
79 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +000080 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000081 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000082
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000083 // Diagnose if this property is already in continuation class.
84 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000085 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenek894ae6a2010-03-15 18:47:25 +000086
Ted Kremenek9f550ff2010-03-15 20:11:46 +000087 if (ObjCPropertyDecl *prevDecl =
88 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000089 Diag(AtLoc, diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +000090 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000091 return DeclPtrTy();
92 }
93
94 // Create a new ObjCPropertyDecl with the DeclContext being
95 // the class extension.
96 ObjCPropertyDecl *PDecl =
97 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
98 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +000099 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
100 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
101 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
102 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
103
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000104 DC->addDecl(PDecl);
105
106 // We need to look in the @interface to see if the @property was
107 // already declared.
108 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
109 if (!CCPrimary) {
110 Diag(CDecl->getLocation(), diag::err_continuation_class);
111 *isOverridingProperty = true;
112 return DeclPtrTy();
113 }
114
115 // Find the property in continuation class's primary class only.
116 ObjCPropertyDecl *PIDecl =
117 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
118
119 if (!PIDecl) {
120 // No matching property found in the primary class. Just fall thru
121 // and add property to continuation class's primary class.
122 ObjCPropertyDecl *PDecl =
123 CreatePropertyDecl(S, CCPrimary, AtLoc,
124 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000125 Attributes, T, MethodImplKind, DC);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000126 // Mark written attribute as having no attribute because
127 // this is not a user-written property declaration in primary
128 // class.
129 PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000130
131 // A case of continuation class adding a new property in the class. This
132 // is not what it was meant for. However, gcc supports it and so should we.
133 // Make sure setter/getters are declared here.
134 ProcessPropertyDecl(PDecl, CCPrimary);
135 return DeclPtrTy::make(PDecl);
136
137 }
138
139 // The property 'PIDecl's readonly attribute will be over-ridden
140 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000141 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000142 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
143 unsigned retainCopyNonatomic =
144 (ObjCPropertyDecl::OBJC_PR_retain |
145 ObjCPropertyDecl::OBJC_PR_copy |
146 ObjCPropertyDecl::OBJC_PR_nonatomic);
147 if ((Attributes & retainCopyNonatomic) !=
148 (PIkind & retainCopyNonatomic)) {
149 Diag(AtLoc, diag::warn_property_attr_mismatch);
150 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000151 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000152 DeclContext *DC = cast<DeclContext>(CCPrimary);
153 if (!ObjCPropertyDecl::findPropertyDecl(DC,
154 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000155 // Protocol is not in the primary class. Must build one for it.
156 ObjCDeclSpec ProtocolPropertyODS;
157 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
158 // and ObjCPropertyDecl::PropertyAttributeKind have identical
159 // values. Should consolidate both into one enum type.
160 ProtocolPropertyODS.
161 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
162 PIkind);
163
164 DeclPtrTy ProtocolPtrTy =
165 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
166 PIDecl->getGetterName(),
167 PIDecl->getSetterName(),
168 DeclPtrTy::make(CCPrimary), isOverridingProperty,
169 MethodImplKind);
Ted Kremeneke9686572010-04-05 23:45:09 +0000170 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy.getAs<Decl>());
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000171 }
172 PIDecl->makeitReadWriteAttribute();
173 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
174 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
175 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
176 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
177 PIDecl->setSetterName(SetterSel);
178 } else {
179 Diag(AtLoc, diag::err_use_continuation_class)
180 << CCPrimary->getDeclName();
181 Diag(PIDecl->getLocation(), diag::note_property_declare);
182 }
183 *isOverridingProperty = true;
184 // Make sure setter decl is synthesized, and added to primary class's list.
185 ProcessPropertyDecl(PIDecl, CCPrimary);
186 return DeclPtrTy();
187}
188
189ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
190 ObjCContainerDecl *CDecl,
191 SourceLocation AtLoc,
192 FieldDeclarator &FD,
193 Selector GetterSel,
194 Selector SetterSel,
195 const bool isAssign,
196 const bool isReadWrite,
197 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000198 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000199 tok::ObjCKeywordKind MethodImplKind,
200 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000201 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000202 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000203
204 // Issue a warning if property is 'assign' as default and its object, which is
205 // gc'able conforms to NSCopying protocol
206 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
207 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000208 if (const ObjCObjectPointerType *ObjPtrTy =
209 T->getAs<ObjCObjectPointerType>()) {
210 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
211 if (IDecl)
212 if (ObjCProtocolDecl* PNSCopying =
213 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
214 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
215 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000216 }
John McCallc12c5bb2010-05-15 11:32:37 +0000217 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000218 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
219
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000220 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000221 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
222 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000223 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000224
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000225 if (ObjCPropertyDecl *prevDecl =
226 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000227 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000228 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000229 PDecl->setInvalidDecl();
230 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000231 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000232 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000233 if (lexicalDC)
234 PDecl->setLexicalDeclContext(lexicalDC);
235 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000236
237 if (T->isArrayType() || T->isFunctionType()) {
238 Diag(AtLoc, diag::err_property_type) << T;
239 PDecl->setInvalidDecl();
240 }
241
242 ProcessDeclAttributes(S, PDecl, FD.D);
243
244 // Regardless of setter/getter attribute, we save the default getter/setter
245 // selector names in anticipation of declaration of setter/getter methods.
246 PDecl->setGetterName(GetterSel);
247 PDecl->setSetterName(SetterSel);
248
249 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
250 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
251
252 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
253 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
254
255 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
256 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
257
258 if (isReadWrite)
259 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
260
261 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
262 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
263
264 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
265 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
266
267 if (isAssign)
268 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
269
270 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
271 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
272
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000273 PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes());
274
Ted Kremenek28685ab2010-03-12 00:46:40 +0000275 if (MethodImplKind == tok::objc_required)
276 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
277 else if (MethodImplKind == tok::objc_optional)
278 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000279
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000280 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000281}
282
283
284/// ActOnPropertyImplDecl - This routine performs semantic checks and
285/// builds the AST node for a property implementation declaration; declared
286/// as @synthesize or @dynamic.
287///
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000288Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(Scope *S,
289 SourceLocation AtLoc,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000290 SourceLocation PropertyLoc,
291 bool Synthesize,
292 DeclPtrTy ClassCatImpDecl,
293 IdentifierInfo *PropertyId,
294 IdentifierInfo *PropertyIvar) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000295 ObjCContainerDecl *ClassImpDecl =
296 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl.getAs<Decl>());
Ted Kremenek28685ab2010-03-12 00:46:40 +0000297 // Make sure we have a context for the property implementation declaration.
298 if (!ClassImpDecl) {
299 Diag(AtLoc, diag::error_missing_property_context);
300 return DeclPtrTy();
301 }
302 ObjCPropertyDecl *property = 0;
303 ObjCInterfaceDecl* IDecl = 0;
304 // Find the class or category class where this property must have
305 // a declaration.
306 ObjCImplementationDecl *IC = 0;
307 ObjCCategoryImplDecl* CatImplClass = 0;
308 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
309 IDecl = IC->getClassInterface();
310 // We always synthesize an interface for an implementation
311 // without an interface decl. So, IDecl is always non-zero.
312 assert(IDecl &&
313 "ActOnPropertyImplDecl - @implementation without @interface");
314
315 // Look for this property declaration in the @implementation's @interface
316 property = IDecl->FindPropertyDeclaration(PropertyId);
317 if (!property) {
318 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
319 return DeclPtrTy();
320 }
321 if (const ObjCCategoryDecl *CD =
322 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
323 if (!CD->IsClassExtension()) {
324 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
325 Diag(property->getLocation(), diag::note_property_declare);
326 return DeclPtrTy();
327 }
328 }
329 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
330 if (Synthesize) {
331 Diag(AtLoc, diag::error_synthesize_category_decl);
332 return DeclPtrTy();
333 }
334 IDecl = CatImplClass->getClassInterface();
335 if (!IDecl) {
336 Diag(AtLoc, diag::error_missing_property_interface);
337 return DeclPtrTy();
338 }
339 ObjCCategoryDecl *Category =
340 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
341
342 // If category for this implementation not found, it is an error which
343 // has already been reported eralier.
344 if (!Category)
345 return DeclPtrTy();
346 // Look for this property declaration in @implementation's category
347 property = Category->FindPropertyDeclaration(PropertyId);
348 if (!property) {
349 Diag(PropertyLoc, diag::error_bad_category_property_decl)
350 << Category->getDeclName();
351 return DeclPtrTy();
352 }
353 } else {
354 Diag(AtLoc, diag::error_bad_property_context);
355 return DeclPtrTy();
356 }
357 ObjCIvarDecl *Ivar = 0;
358 // Check that we have a valid, previously declared ivar for @synthesize
359 if (Synthesize) {
360 // @synthesize
361 if (!PropertyIvar)
362 PropertyIvar = PropertyId;
363 QualType PropType = Context.getCanonicalType(property->getType());
364 // Check that this is a previously declared 'ivar' in 'IDecl' interface
365 ObjCInterfaceDecl *ClassDeclared;
366 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
367 if (!Ivar) {
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000368 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000369 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian2846b2b2010-04-06 23:36:17 +0000370 ObjCIvarDecl::Protected,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000371 (Expr *)0);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000372 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000373 IDecl->makeDeclVisibleInContext(Ivar, false);
374 property->setPropertyIvarDecl(Ivar);
375
376 if (!getLangOptions().ObjCNonFragileABI)
377 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
378 // Note! I deliberately want it to fall thru so, we have a
379 // a property implementation and to avoid future warnings.
380 } else if (getLangOptions().ObjCNonFragileABI &&
381 ClassDeclared != IDecl) {
382 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
383 << property->getDeclName() << Ivar->getDeclName()
384 << ClassDeclared->getDeclName();
385 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
386 << Ivar << Ivar->getNameAsCString();
387 // Note! I deliberately want it to fall thru so more errors are caught.
388 }
389 QualType IvarType = Context.getCanonicalType(Ivar->getType());
390
391 // Check that type of property and its ivar are type compatible.
392 if (PropType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000393 bool compat = false;
394 if (isa<ObjCObjectPointerType>(PropType)
395 && isa<ObjCObjectPointerType>(IvarType))
396 compat =
397 Context.canAssignObjCInterfaces(
398 PropType->getAs<ObjCObjectPointerType>(),
399 IvarType->getAs<ObjCObjectPointerType>());
400 else
401 compat = (CheckAssignmentConstraints(PropType, IvarType) == Compatible);
402 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000403 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000404 << property->getDeclName() << PropType
405 << Ivar->getDeclName() << IvarType;
406 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000407 // Note! I deliberately want it to fall thru so, we have a
408 // a property implementation and to avoid future warnings.
409 }
410
411 // FIXME! Rules for properties are somewhat different that those
412 // for assignments. Use a new routine to consolidate all cases;
413 // specifically for property redeclarations as well as for ivars.
414 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
415 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
416 if (lhsType != rhsType &&
417 lhsType->isArithmeticType()) {
418 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000419 << property->getDeclName() << PropType
420 << Ivar->getDeclName() << IvarType;
421 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000422 // Fall thru - see previous comment
423 }
424 // __weak is explicit. So it works on Canonical type.
425 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
426 getLangOptions().getGCMode() != LangOptions::NonGC) {
427 Diag(PropertyLoc, diag::error_weak_property)
428 << property->getDeclName() << Ivar->getDeclName();
429 // Fall thru - see previous comment
430 }
431 if ((property->getType()->isObjCObjectPointerType() ||
432 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
433 getLangOptions().getGCMode() != LangOptions::NonGC) {
434 Diag(PropertyLoc, diag::error_strong_property)
435 << property->getDeclName() << Ivar->getDeclName();
436 // Fall thru - see previous comment
437 }
438 }
439 } else if (PropertyIvar)
440 // @dynamic
441 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
442 assert (property && "ActOnPropertyImplDecl - property declaration missing");
443 ObjCPropertyImplDecl *PIDecl =
444 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
445 property,
446 (Synthesize ?
447 ObjCPropertyImplDecl::Synthesize
448 : ObjCPropertyImplDecl::Dynamic),
449 Ivar);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000450 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
451 getterMethod->createImplicitParams(Context, IDecl);
452 if (getLangOptions().CPlusPlus && Synthesize) {
453 // For Objective-C++, need to synthesize the AST for the IVAR object to be
454 // returned by the getter as it must conform to C++'s copy-return rules.
455 // FIXME. Eventually we want to do this for Objective-C as well.
456 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
457 DeclRefExpr *SelfExpr =
458 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
459 SourceLocation());
460 Expr *IvarRefExpr =
461 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
462 SelfExpr, true, true);
463 OwningExprResult Res =
464 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000465 SourceLocation(),
466 getterMethod->getResultType(),
467 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000468 SourceLocation(),
469 Owned(IvarRefExpr));
470 if (!Res.isInvalid()) {
471 Expr *ResExpr = Res.takeAs<Expr>();
472 if (ResExpr)
473 ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr);
474 PIDecl->setGetterCXXConstructor(ResExpr);
475 }
476 }
477 }
478 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
479 setterMethod->createImplicitParams(Context, IDecl);
480 if (getLangOptions().CPlusPlus && Synthesize) {
481 // FIXME. Eventually we want to do this for Objective-C as well.
482 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
483 DeclRefExpr *SelfExpr =
484 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
485 SourceLocation());
486 Expr *lhs =
487 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
488 SelfExpr, true, true);
489 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
490 ParmVarDecl *Param = (*P);
491 Expr *rhs = new (Context) DeclRefExpr(Param,Param->getType(),
492 SourceLocation());
493 OwningExprResult Res = BuildBinOp(S, SourceLocation(),
494 BinaryOperator::Assign, lhs, rhs);
495 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
496 }
497 }
498
Ted Kremenek28685ab2010-03-12 00:46:40 +0000499 if (IC) {
500 if (Synthesize)
501 if (ObjCPropertyImplDecl *PPIDecl =
502 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
503 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
504 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
505 << PropertyIvar;
506 Diag(PPIDecl->getLocation(), diag::note_previous_use);
507 }
508
509 if (ObjCPropertyImplDecl *PPIDecl
510 = IC->FindPropertyImplDecl(PropertyId)) {
511 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
512 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
513 return DeclPtrTy();
514 }
515 IC->addPropertyImplementation(PIDecl);
516 } else {
517 if (Synthesize)
518 if (ObjCPropertyImplDecl *PPIDecl =
519 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
520 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
521 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
522 << PropertyIvar;
523 Diag(PPIDecl->getLocation(), diag::note_previous_use);
524 }
525
526 if (ObjCPropertyImplDecl *PPIDecl =
527 CatImplClass->FindPropertyImplDecl(PropertyId)) {
528 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
529 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
530 return DeclPtrTy();
531 }
532 CatImplClass->addPropertyImplementation(PIDecl);
533 }
534
535 return DeclPtrTy::make(PIDecl);
536}
537
538//===----------------------------------------------------------------------===//
539// Helper methods.
540//===----------------------------------------------------------------------===//
541
Ted Kremenek9d64c152010-03-12 00:38:38 +0000542/// DiagnosePropertyMismatch - Compares two properties for their
543/// attributes and types and warns on a variety of inconsistencies.
544///
545void
546Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
547 ObjCPropertyDecl *SuperProperty,
548 const IdentifierInfo *inheritedName) {
549 ObjCPropertyDecl::PropertyAttributeKind CAttr =
550 Property->getPropertyAttributes();
551 ObjCPropertyDecl::PropertyAttributeKind SAttr =
552 SuperProperty->getPropertyAttributes();
553 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
554 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
555 Diag(Property->getLocation(), diag::warn_readonly_property)
556 << Property->getDeclName() << inheritedName;
557 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
558 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
559 Diag(Property->getLocation(), diag::warn_property_attribute)
560 << Property->getDeclName() << "copy" << inheritedName;
561 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
562 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
563 Diag(Property->getLocation(), diag::warn_property_attribute)
564 << Property->getDeclName() << "retain" << inheritedName;
565
566 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
567 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
568 Diag(Property->getLocation(), diag::warn_property_attribute)
569 << Property->getDeclName() << "atomic" << inheritedName;
570 if (Property->getSetterName() != SuperProperty->getSetterName())
571 Diag(Property->getLocation(), diag::warn_property_attribute)
572 << Property->getDeclName() << "setter" << inheritedName;
573 if (Property->getGetterName() != SuperProperty->getGetterName())
574 Diag(Property->getLocation(), diag::warn_property_attribute)
575 << Property->getDeclName() << "getter" << inheritedName;
576
577 QualType LHSType =
578 Context.getCanonicalType(SuperProperty->getType());
579 QualType RHSType =
580 Context.getCanonicalType(Property->getType());
581
582 if (!Context.typesAreCompatible(LHSType, RHSType)) {
583 // FIXME: Incorporate this test with typesAreCompatible.
584 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
585 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
586 return;
587 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
588 << Property->getType() << SuperProperty->getType() << inheritedName;
589 }
590}
591
592bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
593 ObjCMethodDecl *GetterMethod,
594 SourceLocation Loc) {
595 if (GetterMethod &&
596 GetterMethod->getResultType() != property->getType()) {
597 AssignConvertType result = Incompatible;
598 if (property->getType()->isObjCObjectPointerType())
599 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
600 property->getType());
601 if (result != Compatible) {
602 Diag(Loc, diag::warn_accessor_property_type_mismatch)
603 << property->getDeclName()
604 << GetterMethod->getSelector();
605 Diag(GetterMethod->getLocation(), diag::note_declared_at);
606 return true;
607 }
608 }
609 return false;
610}
611
612/// ComparePropertiesInBaseAndSuper - This routine compares property
613/// declarations in base and its super class, if any, and issues
614/// diagnostics in a variety of inconsistant situations.
615///
616void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
617 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
618 if (!SDecl)
619 return;
620 // FIXME: O(N^2)
621 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
622 E = SDecl->prop_end(); S != E; ++S) {
623 ObjCPropertyDecl *SuperPDecl = (*S);
624 // Does property in super class has declaration in current class?
625 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
626 E = IDecl->prop_end(); I != E; ++I) {
627 ObjCPropertyDecl *PDecl = (*I);
628 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
629 DiagnosePropertyMismatch(PDecl, SuperPDecl,
630 SDecl->getIdentifier());
631 }
632 }
633}
634
635/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
636/// of properties declared in a protocol and compares their attribute against
637/// the same property declared in the class or category.
638void
639Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
640 ObjCProtocolDecl *PDecl) {
641 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
642 if (!IDecl) {
643 // Category
644 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
645 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
646 if (!CatDecl->IsClassExtension())
647 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
648 E = PDecl->prop_end(); P != E; ++P) {
649 ObjCPropertyDecl *Pr = (*P);
650 ObjCCategoryDecl::prop_iterator CP, CE;
651 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000652 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000653 if ((*CP)->getIdentifier() == Pr->getIdentifier())
654 break;
655 if (CP != CE)
656 // Property protocol already exist in class. Diagnose any mismatch.
657 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
658 }
659 return;
660 }
661 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
662 E = PDecl->prop_end(); P != E; ++P) {
663 ObjCPropertyDecl *Pr = (*P);
664 ObjCInterfaceDecl::prop_iterator CP, CE;
665 // Is this property already in class's list of properties?
666 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
667 if ((*CP)->getIdentifier() == Pr->getIdentifier())
668 break;
669 if (CP != CE)
670 // Property protocol already exist in class. Diagnose any mismatch.
671 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
672 }
673}
674
675/// CompareProperties - This routine compares properties
676/// declared in 'ClassOrProtocol' objects (which can be a class or an
677/// inherited protocol with the list of properties for class/category 'CDecl'
678///
679void Sema::CompareProperties(Decl *CDecl,
680 DeclPtrTy ClassOrProtocol) {
681 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
682 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
683
684 if (!IDecl) {
685 // Category
686 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
687 assert (CatDecl && "CompareProperties");
688 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
689 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
690 E = MDecl->protocol_end(); P != E; ++P)
691 // Match properties of category with those of protocol (*P)
692 MatchOneProtocolPropertiesInClass(CatDecl, *P);
693
694 // Go thru the list of protocols for this category and recursively match
695 // their properties with those in the category.
696 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
697 E = CatDecl->protocol_end(); P != E; ++P)
698 CompareProperties(CatDecl, DeclPtrTy::make(*P));
699 } else {
700 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
701 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
702 E = MD->protocol_end(); P != E; ++P)
703 MatchOneProtocolPropertiesInClass(CatDecl, *P);
704 }
705 return;
706 }
707
708 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
709 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
710 E = MDecl->protocol_end(); P != E; ++P)
711 // Match properties of class IDecl with those of protocol (*P).
712 MatchOneProtocolPropertiesInClass(IDecl, *P);
713
714 // Go thru the list of protocols for this class and recursively match
715 // their properties with those declared in the class.
716 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
717 E = IDecl->protocol_end(); P != E; ++P)
718 CompareProperties(IDecl, DeclPtrTy::make(*P));
719 } else {
720 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
721 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
722 E = MD->protocol_end(); P != E; ++P)
723 MatchOneProtocolPropertiesInClass(IDecl, *P);
724 }
725}
726
727/// isPropertyReadonly - Return true if property is readonly, by searching
728/// for the property in the class and in its categories and implementations
729///
730bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
731 ObjCInterfaceDecl *IDecl) {
732 // by far the most common case.
733 if (!PDecl->isReadOnly())
734 return false;
735 // Even if property is ready only, if interface has a user defined setter,
736 // it is not considered read only.
737 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
738 return false;
739
740 // Main class has the property as 'readonly'. Must search
741 // through the category list to see if the property's
742 // attribute has been over-ridden to 'readwrite'.
743 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
744 Category; Category = Category->getNextClassCategory()) {
745 // Even if property is ready only, if a category has a user defined setter,
746 // it is not considered read only.
747 if (Category->getInstanceMethod(PDecl->getSetterName()))
748 return false;
749 ObjCPropertyDecl *P =
750 Category->FindPropertyDeclaration(PDecl->getIdentifier());
751 if (P && !P->isReadOnly())
752 return false;
753 }
754
755 // Also, check for definition of a setter method in the implementation if
756 // all else failed.
757 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
758 if (ObjCImplementationDecl *IMD =
759 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
760 if (IMD->getInstanceMethod(PDecl->getSetterName()))
761 return false;
762 } else if (ObjCCategoryImplDecl *CIMD =
763 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
764 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
765 return false;
766 }
767 }
768 // Lastly, look through the implementation (if one is in scope).
769 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
770 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
771 return false;
772 // If all fails, look at the super class.
773 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
774 return isPropertyReadonly(PDecl, SIDecl);
775 return true;
776}
777
778/// CollectImmediateProperties - This routine collects all properties in
779/// the class and its conforming protocols; but not those it its super class.
780void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
781 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
782 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
783 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
784 E = IDecl->prop_end(); P != E; ++P) {
785 ObjCPropertyDecl *Prop = (*P);
786 PropMap[Prop->getIdentifier()] = Prop;
787 }
788 // scan through class's protocols.
789 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
790 E = IDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian738698d2010-05-03 15:49:20 +0000791 // Exclude property for protocols which conform to class's super-class,
792 // as super-class has to implement the property.
793 if (!ProtocolConformsToSuperClass(IDecl, (*PI)))
794 CollectImmediateProperties((*PI), PropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000795 }
796 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
797 if (!CATDecl->IsClassExtension())
798 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
799 E = CATDecl->prop_end(); P != E; ++P) {
800 ObjCPropertyDecl *Prop = (*P);
801 PropMap[Prop->getIdentifier()] = Prop;
802 }
803 // scan through class's protocols.
804 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
805 E = CATDecl->protocol_end(); PI != E; ++PI)
806 CollectImmediateProperties((*PI), PropMap);
807 }
808 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
809 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
810 E = PDecl->prop_end(); P != E; ++P) {
811 ObjCPropertyDecl *Prop = (*P);
812 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
813 if (!PropEntry)
814 PropEntry = Prop;
815 }
816 // scan through protocol's protocols.
817 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
818 E = PDecl->protocol_end(); PI != E; ++PI)
819 CollectImmediateProperties((*PI), PropMap);
820 }
821}
822
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000823/// CollectClassPropertyImplementations - This routine collects list of
824/// properties to be implemented in the class. This includes, class's
825/// and its conforming protocols' properties.
826static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
827 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
828 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
829 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
830 E = IDecl->prop_end(); P != E; ++P) {
831 ObjCPropertyDecl *Prop = (*P);
832 PropMap[Prop->getIdentifier()] = Prop;
833 }
834 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
835 E = IDecl->protocol_end(); PI != E; ++PI)
836 CollectClassPropertyImplementations((*PI), PropMap);
837 }
838 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
839 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
840 E = PDecl->prop_end(); P != E; ++P) {
841 ObjCPropertyDecl *Prop = (*P);
842 PropMap[Prop->getIdentifier()] = Prop;
843 }
844 // scan through protocol's protocols.
845 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
846 E = PDecl->protocol_end(); PI != E; ++PI)
847 CollectClassPropertyImplementations((*PI), PropMap);
848 }
849}
850
851/// CollectSuperClassPropertyImplementations - This routine collects list of
852/// properties to be implemented in super class(s) and also coming from their
853/// conforming protocols.
854static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
855 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
856 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
857 while (SDecl) {
858 CollectClassPropertyImplementations(SDecl, PropMap);
859 SDecl = SDecl->getSuperClass();
860 }
861 }
862}
863
Fariborz Jahanian738698d2010-05-03 15:49:20 +0000864/// ProtocolConformsToSuperClass - Returns true if class's given protocol
865/// conforms to one of its super class's protocols.
866bool Sema::ProtocolConformsToSuperClass(const ObjCInterfaceDecl *IDecl,
867 const ObjCProtocolDecl *PDecl) {
868 if (const ObjCInterfaceDecl *CDecl = IDecl->getSuperClass()) {
869 for (ObjCInterfaceDecl::protocol_iterator PI = CDecl->protocol_begin(),
870 E = CDecl->protocol_end(); PI != E; ++PI) {
871 if (ProtocolConformsToProtocol((*PI), PDecl))
872 return true;
873 return ProtocolConformsToSuperClass(CDecl, PDecl);
874 }
875 }
876 return false;
877}
878
879bool Sema::ProtocolConformsToProtocol(const ObjCProtocolDecl *NestedProtocol,
880 const ObjCProtocolDecl *PDecl) {
881 if (PDecl->getIdentifier() == NestedProtocol->getIdentifier())
882 return true;
883 // scan through protocol's protocols.
884 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
885 E = PDecl->protocol_end(); PI != E; ++PI)
886 if (ProtocolConformsToProtocol(NestedProtocol, (*PI)))
887 return true;
888 return false;
889}
890
Ted Kremenek9d64c152010-03-12 00:38:38 +0000891/// LookupPropertyDecl - Looks up a property in the current class and all
892/// its protocols.
893ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
894 IdentifierInfo *II) {
895 if (const ObjCInterfaceDecl *IDecl =
896 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
897 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
898 E = IDecl->prop_end(); P != E; ++P) {
899 ObjCPropertyDecl *Prop = (*P);
900 if (Prop->getIdentifier() == II)
901 return Prop;
902 }
903 // scan through class's protocols.
904 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
905 E = IDecl->protocol_end(); PI != E; ++PI) {
906 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
907 if (Prop)
908 return Prop;
909 }
910 }
911 else if (const ObjCProtocolDecl *PDecl =
912 dyn_cast<ObjCProtocolDecl>(CDecl)) {
913 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
914 E = PDecl->prop_end(); P != E; ++P) {
915 ObjCPropertyDecl *Prop = (*P);
916 if (Prop->getIdentifier() == II)
917 return Prop;
918 }
919 // scan through protocol's protocols.
920 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
921 E = PDecl->protocol_end(); PI != E; ++PI) {
922 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
923 if (Prop)
924 return Prop;
925 }
926 }
927 return 0;
928}
929
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000930/// DefaultSynthesizeProperties - This routine default synthesizes all
931/// properties which must be synthesized in class's @implementation.
932void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
933 ObjCInterfaceDecl *IDecl) {
934
935 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
936 CollectClassPropertyImplementations(IDecl, PropMap);
937 if (PropMap.empty())
938 return;
939 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
940 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
941
942 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
943 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
944 ObjCPropertyDecl *Prop = P->second;
945 // If property to be implemented in the super class, ignore.
946 if (SuperPropMap[Prop->getIdentifier()])
947 continue;
948 // Is there a matching propery synthesize/dynamic?
949 if (Prop->isInvalidDecl() ||
950 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
951 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
952 continue;
953 ActOnPropertyImplDecl(S, IMPDecl->getLocation(), IMPDecl->getLocation(),
954 true, DeclPtrTy::make(IMPDecl),
955 Prop->getIdentifier(), Prop->getIdentifier());
956 }
957}
Ted Kremenek9d64c152010-03-12 00:38:38 +0000958
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000959void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +0000960 ObjCContainerDecl *CDecl,
961 const llvm::DenseSet<Selector>& InsMap) {
962 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
963 CollectImmediateProperties(CDecl, PropMap);
964 if (PropMap.empty())
965 return;
966
967 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
968 for (ObjCImplDecl::propimpl_iterator
969 I = IMPDecl->propimpl_begin(),
970 EI = IMPDecl->propimpl_end(); I != EI; ++I)
971 PropImplMap.insert((*I)->getPropertyDecl());
972
973 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
974 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
975 ObjCPropertyDecl *Prop = P->second;
976 // Is there a matching propery synthesize/dynamic?
977 if (Prop->isInvalidDecl() ||
978 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
979 PropImplMap.count(Prop))
980 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000981 if (!InsMap.count(Prop->getGetterName())) {
982 Diag(Prop->getLocation(),
983 isa<ObjCCategoryDecl>(CDecl) ?
984 diag::warn_setter_getter_impl_required_in_category :
985 diag::warn_setter_getter_impl_required)
986 << Prop->getDeclName() << Prop->getGetterName();
987 Diag(IMPDecl->getLocation(),
988 diag::note_property_impl_required);
989 }
990
991 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
992 Diag(Prop->getLocation(),
993 isa<ObjCCategoryDecl>(CDecl) ?
994 diag::warn_setter_getter_impl_required_in_category :
995 diag::warn_setter_getter_impl_required)
996 << Prop->getDeclName() << Prop->getSetterName();
997 Diag(IMPDecl->getLocation(),
998 diag::note_property_impl_required);
999 }
1000 }
1001}
1002
1003void
1004Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1005 ObjCContainerDecl* IDecl) {
1006 // Rules apply in non-GC mode only
1007 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1008 return;
1009 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1010 E = IDecl->prop_end();
1011 I != E; ++I) {
1012 ObjCPropertyDecl *Property = (*I);
1013 unsigned Attributes = Property->getPropertyAttributes();
1014 // We only care about readwrite atomic property.
1015 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1016 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1017 continue;
1018 if (const ObjCPropertyImplDecl *PIDecl
1019 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1020 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1021 continue;
1022 ObjCMethodDecl *GetterMethod =
1023 IMPDecl->getInstanceMethod(Property->getGetterName());
1024 ObjCMethodDecl *SetterMethod =
1025 IMPDecl->getInstanceMethod(Property->getSetterName());
1026 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1027 SourceLocation MethodLoc =
1028 (GetterMethod ? GetterMethod->getLocation()
1029 : SetterMethod->getLocation());
1030 Diag(MethodLoc, diag::warn_atomic_property_rule)
1031 << Property->getIdentifier();
1032 Diag(Property->getLocation(), diag::note_property_declare);
1033 }
1034 }
1035 }
1036}
1037
1038/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1039/// have the property type and issue diagnostics if they don't.
1040/// Also synthesize a getter/setter method if none exist (and update the
1041/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1042/// methods is the "right" thing to do.
1043void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1044 ObjCContainerDecl *CD) {
1045 ObjCMethodDecl *GetterMethod, *SetterMethod;
1046
1047 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1048 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1049 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1050 property->getLocation());
1051
1052 if (SetterMethod) {
1053 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1054 property->getPropertyAttributes();
1055 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1056 Context.getCanonicalType(SetterMethod->getResultType()) !=
1057 Context.VoidTy)
1058 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1059 if (SetterMethod->param_size() != 1 ||
1060 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1061 Diag(property->getLocation(),
1062 diag::warn_accessor_property_type_mismatch)
1063 << property->getDeclName()
1064 << SetterMethod->getSelector();
1065 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1066 }
1067 }
1068
1069 // Synthesize getter/setter methods if none exist.
1070 // Find the default getter and if one not found, add one.
1071 // FIXME: The synthesized property we set here is misleading. We almost always
1072 // synthesize these methods unless the user explicitly provided prototypes
1073 // (which is odd, but allowed). Sema should be typechecking that the
1074 // declarations jive in that situation (which it is not currently).
1075 if (!GetterMethod) {
1076 // No instance method of same name as property getter name was found.
1077 // Declare a getter method and add it to the list of methods
1078 // for this class.
1079 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1080 property->getLocation(), property->getGetterName(),
1081 property->getType(), 0, CD, true, false, true,
1082 (property->getPropertyImplementation() ==
1083 ObjCPropertyDecl::Optional) ?
1084 ObjCMethodDecl::Optional :
1085 ObjCMethodDecl::Required);
1086 CD->addDecl(GetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001087 // FIXME: Eventually this shouldn't be needed, as the lexical context
1088 // and the real context should be the same.
1089 if (DeclContext *lexicalDC = property->getLexicalDeclContext())
1090 GetterMethod->setLexicalDeclContext(lexicalDC);
1091
Ted Kremenek9d64c152010-03-12 00:38:38 +00001092 } else
1093 // A user declared getter will be synthesize when @synthesize of
1094 // the property with the same name is seen in the @implementation
1095 GetterMethod->setSynthesized(true);
1096 property->setGetterMethodDecl(GetterMethod);
1097
1098 // Skip setter if property is read-only.
1099 if (!property->isReadOnly()) {
1100 // Find the default setter and if one not found, add one.
1101 if (!SetterMethod) {
1102 // No instance method of same name as property setter name was found.
1103 // Declare a setter method and add it to the list of methods
1104 // for this class.
1105 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1106 property->getLocation(),
1107 property->getSetterName(),
1108 Context.VoidTy, 0, CD, true, false, true,
1109 (property->getPropertyImplementation() ==
1110 ObjCPropertyDecl::Optional) ?
1111 ObjCMethodDecl::Optional :
1112 ObjCMethodDecl::Required);
1113 // Invent the arguments for the setter. We don't bother making a
1114 // nice name for the argument.
1115 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1116 property->getLocation(),
1117 property->getIdentifier(),
1118 property->getType(),
1119 /*TInfo=*/0,
1120 VarDecl::None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001121 VarDecl::None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001122 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001123 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001124 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001125 // FIXME: Eventually this shouldn't be needed, as the lexical context
1126 // and the real context should be the same.
1127 if (DeclContext *lexicalDC = property->getLexicalDeclContext())
1128 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001129 } else
1130 // A user declared setter will be synthesize when @synthesize of
1131 // the property with the same name is seen in the @implementation
1132 SetterMethod->setSynthesized(true);
1133 property->setSetterMethodDecl(SetterMethod);
1134 }
1135 // Add any synthesized methods to the global pool. This allows us to
1136 // handle the following, which is supported by GCC (and part of the design).
1137 //
1138 // @interface Foo
1139 // @property double bar;
1140 // @end
1141 //
1142 // void thisIsUnfortunate() {
1143 // id foo;
1144 // double bar = [foo bar];
1145 // }
1146 //
1147 if (GetterMethod)
1148 AddInstanceMethodToGlobalPool(GetterMethod);
1149 if (SetterMethod)
1150 AddInstanceMethodToGlobalPool(SetterMethod);
1151}
1152
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001153void Sema::CheckObjCPropertyAttributes(DeclPtrTy PropertyPtrTy,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001154 SourceLocation Loc,
1155 unsigned &Attributes) {
1156 // FIXME: Improve the reported location.
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001157 Decl *PDecl = PropertyPtrTy.getAs<Decl>();
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001158 if (!PDecl)
1159 return;
1160
1161 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001162 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001163
1164 // readonly and readwrite/assign/retain/copy conflict.
1165 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1166 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1167 ObjCDeclSpec::DQ_PR_assign |
1168 ObjCDeclSpec::DQ_PR_copy |
1169 ObjCDeclSpec::DQ_PR_retain))) {
1170 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1171 "readwrite" :
1172 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1173 "assign" :
1174 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1175 "copy" : "retain";
1176
1177 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1178 diag::err_objc_property_attr_mutually_exclusive :
1179 diag::warn_objc_property_attr_mutually_exclusive)
1180 << "readonly" << which;
1181 }
1182
1183 // Check for copy or retain on non-object types.
1184 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1185 !PropertyTy->isObjCObjectPointerType() &&
1186 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001187 !Context.isObjCNSObjectType(PropertyTy) &&
1188 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001189 Diag(Loc, diag::err_objc_property_requires_object)
1190 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1191 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1192 }
1193
1194 // Check for more than one of { assign, copy, retain }.
1195 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1196 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1197 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1198 << "assign" << "copy";
1199 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1200 }
1201 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1202 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1203 << "assign" << "retain";
1204 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1205 }
1206 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1207 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1208 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1209 << "copy" << "retain";
1210 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1211 }
1212 }
1213
1214 // Warn if user supplied no assignment attribute, property is
1215 // readwrite, and this is an object type.
1216 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1217 ObjCDeclSpec::DQ_PR_retain)) &&
1218 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1219 PropertyTy->isObjCObjectPointerType()) {
1220 // Skip this warning in gc-only mode.
1221 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1222 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1223
1224 // If non-gc code warn that this is likely inappropriate.
1225 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1226 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1227
1228 // FIXME: Implement warning dependent on NSCopying being
1229 // implemented. See also:
1230 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1231 // (please trim this list while you are at it).
1232 }
1233
1234 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1235 && getLangOptions().getGCMode() == LangOptions::GCOnly
1236 && PropertyTy->isBlockPointerType())
1237 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1238}