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