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