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