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