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