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