blob: 4dc734de8ae9f1eb7095f654b1045d74946e1d80 [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"
16
17using namespace clang;
18
Ted Kremenek28685ab2010-03-12 00:46:40 +000019//===----------------------------------------------------------------------===//
20// Grammar actions.
21//===----------------------------------------------------------------------===//
22
23Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
24 FieldDeclarator &FD,
25 ObjCDeclSpec &ODS,
26 Selector GetterSel,
27 Selector SetterSel,
28 DeclPtrTy ClassCategory,
29 bool *isOverridingProperty,
30 tok::ObjCKeywordKind MethodImplKind) {
31 unsigned Attributes = ODS.getPropertyAttributes();
32 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
33 // default is readwrite!
34 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
35 // property is defaulted to 'assign' if it is readwrite and is
36 // not retain or copy
37 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
38 (isReadWrite &&
39 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
40 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000041
Ted Kremenek28685ab2010-03-12 00:46:40 +000042 QualType T = GetTypeForDeclarator(FD.D, S);
43 if (T->isReferenceType()) {
44 Diag(AtLoc, diag::error_reference_property);
45 return DeclPtrTy();
46 }
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000047 // Validate the attributes on the @property.
Ted Kremenek28685ab2010-03-12 00:46:40 +000048 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000049
50 // 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,
60 isOverridingProperty, T,
61 MethodImplKind);
Ted Kremenek28685ab2010-03-12 00:46:40 +000062
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000063 return DeclPtrTy::make(CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
64 GetterSel, SetterSel,
65 isAssign, isReadWrite,
66 Attributes, T, MethodImplKind));
67}
Ted Kremenek2d2f9362010-03-12 00:49:00 +000068
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000069Sema::DeclPtrTy
70Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
71 SourceLocation AtLoc, FieldDeclarator &FD,
72 Selector GetterSel, Selector SetterSel,
73 const bool isAssign,
74 const bool isReadWrite,
75 const unsigned Attributes,
76 bool *isOverridingProperty,
77 QualType T,
78 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000079
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000080 // Diagnose if this property is already in continuation class.
81 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000082 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenek894ae6a2010-03-15 18:47:25 +000083
Ted Kremenek9f550ff2010-03-15 20:11:46 +000084 if (ObjCPropertyDecl *prevDecl =
85 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000086 Diag(AtLoc, diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +000087 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000088 return DeclPtrTy();
89 }
90
91 // Create a new ObjCPropertyDecl with the DeclContext being
92 // the class extension.
93 ObjCPropertyDecl *PDecl =
94 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
95 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +000096 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
97 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
98 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
99 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
100
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000101 DC->addDecl(PDecl);
102
103 // We need to look in the @interface to see if the @property was
104 // already declared.
105 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
106 if (!CCPrimary) {
107 Diag(CDecl->getLocation(), diag::err_continuation_class);
108 *isOverridingProperty = true;
109 return DeclPtrTy();
110 }
111
112 // Find the property in continuation class's primary class only.
113 ObjCPropertyDecl *PIDecl =
114 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
115
116 if (!PIDecl) {
117 // No matching property found in the primary class. Just fall thru
118 // and add property to continuation class's primary class.
119 ObjCPropertyDecl *PDecl =
120 CreatePropertyDecl(S, CCPrimary, AtLoc,
121 FD, GetterSel, SetterSel, isAssign, isReadWrite,
122 Attributes, T, MethodImplKind);
123
124 // A case of continuation class adding a new property in the class. This
125 // is not what it was meant for. However, gcc supports it and so should we.
126 // Make sure setter/getters are declared here.
127 ProcessPropertyDecl(PDecl, CCPrimary);
128 return DeclPtrTy::make(PDecl);
129
130 }
131
132 // The property 'PIDecl's readonly attribute will be over-ridden
133 // with continuation class's readwrite property attribute!
134 unsigned PIkind = PIDecl->getPropertyAttributes();
135 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
136 unsigned retainCopyNonatomic =
137 (ObjCPropertyDecl::OBJC_PR_retain |
138 ObjCPropertyDecl::OBJC_PR_copy |
139 ObjCPropertyDecl::OBJC_PR_nonatomic);
140 if ((Attributes & retainCopyNonatomic) !=
141 (PIkind & retainCopyNonatomic)) {
142 Diag(AtLoc, diag::warn_property_attr_mismatch);
143 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000144 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000145 DeclContext *DC = cast<DeclContext>(CCPrimary);
146 if (!ObjCPropertyDecl::findPropertyDecl(DC,
147 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000148 // Protocol is not in the primary class. Must build one for it.
149 ObjCDeclSpec ProtocolPropertyODS;
150 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
151 // and ObjCPropertyDecl::PropertyAttributeKind have identical
152 // values. Should consolidate both into one enum type.
153 ProtocolPropertyODS.
154 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
155 PIkind);
156
157 DeclPtrTy ProtocolPtrTy =
158 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
159 PIDecl->getGetterName(),
160 PIDecl->getSetterName(),
161 DeclPtrTy::make(CCPrimary), isOverridingProperty,
162 MethodImplKind);
163 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
164 }
165 PIDecl->makeitReadWriteAttribute();
166 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
167 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
168 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
169 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
170 PIDecl->setSetterName(SetterSel);
171 } else {
172 Diag(AtLoc, diag::err_use_continuation_class)
173 << CCPrimary->getDeclName();
174 Diag(PIDecl->getLocation(), diag::note_property_declare);
175 }
176 *isOverridingProperty = true;
177 // Make sure setter decl is synthesized, and added to primary class's list.
178 ProcessPropertyDecl(PIDecl, CCPrimary);
179 return DeclPtrTy();
180}
181
182ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
183 ObjCContainerDecl *CDecl,
184 SourceLocation AtLoc,
185 FieldDeclarator &FD,
186 Selector GetterSel,
187 Selector SetterSel,
188 const bool isAssign,
189 const bool isReadWrite,
190 const unsigned Attributes,
191 QualType T,
192 tok::ObjCKeywordKind MethodImplKind){
193
194 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000195
196 // Issue a warning if property is 'assign' as default and its object, which is
197 // gc'able conforms to NSCopying protocol
198 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
199 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
200 if (T->isObjCObjectPointerType()) {
201 QualType InterfaceTy = T->getPointeeType();
202 if (const ObjCInterfaceType *OIT =
203 InterfaceTy->getAs<ObjCInterfaceType>()) {
204 ObjCInterfaceDecl *IDecl = OIT->getDecl();
205 if (IDecl)
206 if (ObjCProtocolDecl* PNSCopying =
207 LookupProtocol(&Context.Idents.get("NSCopying")))
208 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000209 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000210 }
211 }
212 if (T->isObjCInterfaceType())
213 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 }
226 else
227 DC->addDecl(PDecl);
228
229 if (T->isArrayType() || T->isFunctionType()) {
230 Diag(AtLoc, diag::err_property_type) << T;
231 PDecl->setInvalidDecl();
232 }
233
234 ProcessDeclAttributes(S, PDecl, FD.D);
235
236 // Regardless of setter/getter attribute, we save the default getter/setter
237 // selector names in anticipation of declaration of setter/getter methods.
238 PDecl->setGetterName(GetterSel);
239 PDecl->setSetterName(SetterSel);
240
241 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
242 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
243
244 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
245 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
246
247 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
248 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
249
250 if (isReadWrite)
251 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
252
253 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
254 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
255
256 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
257 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
258
259 if (isAssign)
260 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
261
262 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
263 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
264
265 if (MethodImplKind == tok::objc_required)
266 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
267 else if (MethodImplKind == tok::objc_optional)
268 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000269
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000270 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000271}
272
273
274/// ActOnPropertyImplDecl - This routine performs semantic checks and
275/// builds the AST node for a property implementation declaration; declared
276/// as @synthesize or @dynamic.
277///
278Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
279 SourceLocation PropertyLoc,
280 bool Synthesize,
281 DeclPtrTy ClassCatImpDecl,
282 IdentifierInfo *PropertyId,
283 IdentifierInfo *PropertyIvar) {
284 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
285 // Make sure we have a context for the property implementation declaration.
286 if (!ClassImpDecl) {
287 Diag(AtLoc, diag::error_missing_property_context);
288 return DeclPtrTy();
289 }
290 ObjCPropertyDecl *property = 0;
291 ObjCInterfaceDecl* IDecl = 0;
292 // Find the class or category class where this property must have
293 // a declaration.
294 ObjCImplementationDecl *IC = 0;
295 ObjCCategoryImplDecl* CatImplClass = 0;
296 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
297 IDecl = IC->getClassInterface();
298 // We always synthesize an interface for an implementation
299 // without an interface decl. So, IDecl is always non-zero.
300 assert(IDecl &&
301 "ActOnPropertyImplDecl - @implementation without @interface");
302
303 // Look for this property declaration in the @implementation's @interface
304 property = IDecl->FindPropertyDeclaration(PropertyId);
305 if (!property) {
306 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
307 return DeclPtrTy();
308 }
309 if (const ObjCCategoryDecl *CD =
310 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
311 if (!CD->IsClassExtension()) {
312 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
313 Diag(property->getLocation(), diag::note_property_declare);
314 return DeclPtrTy();
315 }
316 }
317 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
318 if (Synthesize) {
319 Diag(AtLoc, diag::error_synthesize_category_decl);
320 return DeclPtrTy();
321 }
322 IDecl = CatImplClass->getClassInterface();
323 if (!IDecl) {
324 Diag(AtLoc, diag::error_missing_property_interface);
325 return DeclPtrTy();
326 }
327 ObjCCategoryDecl *Category =
328 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
329
330 // If category for this implementation not found, it is an error which
331 // has already been reported eralier.
332 if (!Category)
333 return DeclPtrTy();
334 // Look for this property declaration in @implementation's category
335 property = Category->FindPropertyDeclaration(PropertyId);
336 if (!property) {
337 Diag(PropertyLoc, diag::error_bad_category_property_decl)
338 << Category->getDeclName();
339 return DeclPtrTy();
340 }
341 } else {
342 Diag(AtLoc, diag::error_bad_property_context);
343 return DeclPtrTy();
344 }
345 ObjCIvarDecl *Ivar = 0;
346 // Check that we have a valid, previously declared ivar for @synthesize
347 if (Synthesize) {
348 // @synthesize
349 if (!PropertyIvar)
350 PropertyIvar = PropertyId;
351 QualType PropType = Context.getCanonicalType(property->getType());
352 // Check that this is a previously declared 'ivar' in 'IDecl' interface
353 ObjCInterfaceDecl *ClassDeclared;
354 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
355 if (!Ivar) {
356 DeclContext *EnclosingContext = cast_or_null<DeclContext>(ClassImpDecl);
357 assert(EnclosingContext &&
358 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
359 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
360 PropertyIvar, PropType, /*Dinfo=*/0,
361 ObjCIvarDecl::Public,
362 (Expr *)0);
363 EnclosingContext->addDecl(Ivar);
364 IDecl->makeDeclVisibleInContext(Ivar, false);
365 property->setPropertyIvarDecl(Ivar);
366
367 if (!getLangOptions().ObjCNonFragileABI)
368 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
369 // Note! I deliberately want it to fall thru so, we have a
370 // a property implementation and to avoid future warnings.
371 } else if (getLangOptions().ObjCNonFragileABI &&
372 ClassDeclared != IDecl) {
373 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
374 << property->getDeclName() << Ivar->getDeclName()
375 << ClassDeclared->getDeclName();
376 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
377 << Ivar << Ivar->getNameAsCString();
378 // Note! I deliberately want it to fall thru so more errors are caught.
379 }
380 QualType IvarType = Context.getCanonicalType(Ivar->getType());
381
382 // Check that type of property and its ivar are type compatible.
383 if (PropType != IvarType) {
384 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
385 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000386 << property->getDeclName() << PropType
387 << Ivar->getDeclName() << IvarType;
388 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000389 // Note! I deliberately want it to fall thru so, we have a
390 // a property implementation and to avoid future warnings.
391 }
392
393 // FIXME! Rules for properties are somewhat different that those
394 // for assignments. Use a new routine to consolidate all cases;
395 // specifically for property redeclarations as well as for ivars.
396 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
397 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
398 if (lhsType != rhsType &&
399 lhsType->isArithmeticType()) {
400 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000401 << property->getDeclName() << PropType
402 << Ivar->getDeclName() << IvarType;
403 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000404 // Fall thru - see previous comment
405 }
406 // __weak is explicit. So it works on Canonical type.
407 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
408 getLangOptions().getGCMode() != LangOptions::NonGC) {
409 Diag(PropertyLoc, diag::error_weak_property)
410 << property->getDeclName() << Ivar->getDeclName();
411 // Fall thru - see previous comment
412 }
413 if ((property->getType()->isObjCObjectPointerType() ||
414 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
415 getLangOptions().getGCMode() != LangOptions::NonGC) {
416 Diag(PropertyLoc, diag::error_strong_property)
417 << property->getDeclName() << Ivar->getDeclName();
418 // Fall thru - see previous comment
419 }
420 }
421 } else if (PropertyIvar)
422 // @dynamic
423 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
424 assert (property && "ActOnPropertyImplDecl - property declaration missing");
425 ObjCPropertyImplDecl *PIDecl =
426 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
427 property,
428 (Synthesize ?
429 ObjCPropertyImplDecl::Synthesize
430 : ObjCPropertyImplDecl::Dynamic),
431 Ivar);
432 if (IC) {
433 if (Synthesize)
434 if (ObjCPropertyImplDecl *PPIDecl =
435 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
436 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
437 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
438 << PropertyIvar;
439 Diag(PPIDecl->getLocation(), diag::note_previous_use);
440 }
441
442 if (ObjCPropertyImplDecl *PPIDecl
443 = IC->FindPropertyImplDecl(PropertyId)) {
444 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
445 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
446 return DeclPtrTy();
447 }
448 IC->addPropertyImplementation(PIDecl);
449 } else {
450 if (Synthesize)
451 if (ObjCPropertyImplDecl *PPIDecl =
452 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
453 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
454 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
455 << PropertyIvar;
456 Diag(PPIDecl->getLocation(), diag::note_previous_use);
457 }
458
459 if (ObjCPropertyImplDecl *PPIDecl =
460 CatImplClass->FindPropertyImplDecl(PropertyId)) {
461 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
462 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
463 return DeclPtrTy();
464 }
465 CatImplClass->addPropertyImplementation(PIDecl);
466 }
467
468 return DeclPtrTy::make(PIDecl);
469}
470
471//===----------------------------------------------------------------------===//
472// Helper methods.
473//===----------------------------------------------------------------------===//
474
Ted Kremenek9d64c152010-03-12 00:38:38 +0000475/// DiagnosePropertyMismatch - Compares two properties for their
476/// attributes and types and warns on a variety of inconsistencies.
477///
478void
479Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
480 ObjCPropertyDecl *SuperProperty,
481 const IdentifierInfo *inheritedName) {
482 ObjCPropertyDecl::PropertyAttributeKind CAttr =
483 Property->getPropertyAttributes();
484 ObjCPropertyDecl::PropertyAttributeKind SAttr =
485 SuperProperty->getPropertyAttributes();
486 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
487 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
488 Diag(Property->getLocation(), diag::warn_readonly_property)
489 << Property->getDeclName() << inheritedName;
490 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
491 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
492 Diag(Property->getLocation(), diag::warn_property_attribute)
493 << Property->getDeclName() << "copy" << inheritedName;
494 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
495 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
496 Diag(Property->getLocation(), diag::warn_property_attribute)
497 << Property->getDeclName() << "retain" << inheritedName;
498
499 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
500 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
501 Diag(Property->getLocation(), diag::warn_property_attribute)
502 << Property->getDeclName() << "atomic" << inheritedName;
503 if (Property->getSetterName() != SuperProperty->getSetterName())
504 Diag(Property->getLocation(), diag::warn_property_attribute)
505 << Property->getDeclName() << "setter" << inheritedName;
506 if (Property->getGetterName() != SuperProperty->getGetterName())
507 Diag(Property->getLocation(), diag::warn_property_attribute)
508 << Property->getDeclName() << "getter" << inheritedName;
509
510 QualType LHSType =
511 Context.getCanonicalType(SuperProperty->getType());
512 QualType RHSType =
513 Context.getCanonicalType(Property->getType());
514
515 if (!Context.typesAreCompatible(LHSType, RHSType)) {
516 // FIXME: Incorporate this test with typesAreCompatible.
517 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
518 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
519 return;
520 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
521 << Property->getType() << SuperProperty->getType() << inheritedName;
522 }
523}
524
525bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
526 ObjCMethodDecl *GetterMethod,
527 SourceLocation Loc) {
528 if (GetterMethod &&
529 GetterMethod->getResultType() != property->getType()) {
530 AssignConvertType result = Incompatible;
531 if (property->getType()->isObjCObjectPointerType())
532 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
533 property->getType());
534 if (result != Compatible) {
535 Diag(Loc, diag::warn_accessor_property_type_mismatch)
536 << property->getDeclName()
537 << GetterMethod->getSelector();
538 Diag(GetterMethod->getLocation(), diag::note_declared_at);
539 return true;
540 }
541 }
542 return false;
543}
544
545/// ComparePropertiesInBaseAndSuper - This routine compares property
546/// declarations in base and its super class, if any, and issues
547/// diagnostics in a variety of inconsistant situations.
548///
549void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
550 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
551 if (!SDecl)
552 return;
553 // FIXME: O(N^2)
554 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
555 E = SDecl->prop_end(); S != E; ++S) {
556 ObjCPropertyDecl *SuperPDecl = (*S);
557 // Does property in super class has declaration in current class?
558 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
559 E = IDecl->prop_end(); I != E; ++I) {
560 ObjCPropertyDecl *PDecl = (*I);
561 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
562 DiagnosePropertyMismatch(PDecl, SuperPDecl,
563 SDecl->getIdentifier());
564 }
565 }
566}
567
568/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
569/// of properties declared in a protocol and compares their attribute against
570/// the same property declared in the class or category.
571void
572Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
573 ObjCProtocolDecl *PDecl) {
574 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
575 if (!IDecl) {
576 // Category
577 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
578 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
579 if (!CatDecl->IsClassExtension())
580 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
581 E = PDecl->prop_end(); P != E; ++P) {
582 ObjCPropertyDecl *Pr = (*P);
583 ObjCCategoryDecl::prop_iterator CP, CE;
584 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000585 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000586 if ((*CP)->getIdentifier() == Pr->getIdentifier())
587 break;
588 if (CP != CE)
589 // Property protocol already exist in class. Diagnose any mismatch.
590 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
591 }
592 return;
593 }
594 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
595 E = PDecl->prop_end(); P != E; ++P) {
596 ObjCPropertyDecl *Pr = (*P);
597 ObjCInterfaceDecl::prop_iterator CP, CE;
598 // Is this property already in class's list of properties?
599 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
600 if ((*CP)->getIdentifier() == Pr->getIdentifier())
601 break;
602 if (CP != CE)
603 // Property protocol already exist in class. Diagnose any mismatch.
604 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
605 }
606}
607
608/// CompareProperties - This routine compares properties
609/// declared in 'ClassOrProtocol' objects (which can be a class or an
610/// inherited protocol with the list of properties for class/category 'CDecl'
611///
612void Sema::CompareProperties(Decl *CDecl,
613 DeclPtrTy ClassOrProtocol) {
614 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
615 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
616
617 if (!IDecl) {
618 // Category
619 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
620 assert (CatDecl && "CompareProperties");
621 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
622 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
623 E = MDecl->protocol_end(); P != E; ++P)
624 // Match properties of category with those of protocol (*P)
625 MatchOneProtocolPropertiesInClass(CatDecl, *P);
626
627 // Go thru the list of protocols for this category and recursively match
628 // their properties with those in the category.
629 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
630 E = CatDecl->protocol_end(); P != E; ++P)
631 CompareProperties(CatDecl, DeclPtrTy::make(*P));
632 } else {
633 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
634 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
635 E = MD->protocol_end(); P != E; ++P)
636 MatchOneProtocolPropertiesInClass(CatDecl, *P);
637 }
638 return;
639 }
640
641 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
642 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
643 E = MDecl->protocol_end(); P != E; ++P)
644 // Match properties of class IDecl with those of protocol (*P).
645 MatchOneProtocolPropertiesInClass(IDecl, *P);
646
647 // Go thru the list of protocols for this class and recursively match
648 // their properties with those declared in the class.
649 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
650 E = IDecl->protocol_end(); P != E; ++P)
651 CompareProperties(IDecl, DeclPtrTy::make(*P));
652 } else {
653 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
654 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
655 E = MD->protocol_end(); P != E; ++P)
656 MatchOneProtocolPropertiesInClass(IDecl, *P);
657 }
658}
659
660/// isPropertyReadonly - Return true if property is readonly, by searching
661/// for the property in the class and in its categories and implementations
662///
663bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
664 ObjCInterfaceDecl *IDecl) {
665 // by far the most common case.
666 if (!PDecl->isReadOnly())
667 return false;
668 // Even if property is ready only, if interface has a user defined setter,
669 // it is not considered read only.
670 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
671 return false;
672
673 // Main class has the property as 'readonly'. Must search
674 // through the category list to see if the property's
675 // attribute has been over-ridden to 'readwrite'.
676 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
677 Category; Category = Category->getNextClassCategory()) {
678 // Even if property is ready only, if a category has a user defined setter,
679 // it is not considered read only.
680 if (Category->getInstanceMethod(PDecl->getSetterName()))
681 return false;
682 ObjCPropertyDecl *P =
683 Category->FindPropertyDeclaration(PDecl->getIdentifier());
684 if (P && !P->isReadOnly())
685 return false;
686 }
687
688 // Also, check for definition of a setter method in the implementation if
689 // all else failed.
690 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
691 if (ObjCImplementationDecl *IMD =
692 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
693 if (IMD->getInstanceMethod(PDecl->getSetterName()))
694 return false;
695 } else if (ObjCCategoryImplDecl *CIMD =
696 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
697 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
698 return false;
699 }
700 }
701 // Lastly, look through the implementation (if one is in scope).
702 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
703 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
704 return false;
705 // If all fails, look at the super class.
706 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
707 return isPropertyReadonly(PDecl, SIDecl);
708 return true;
709}
710
711/// CollectImmediateProperties - This routine collects all properties in
712/// the class and its conforming protocols; but not those it its super class.
713void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
714 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
715 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
716 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
717 E = IDecl->prop_end(); P != E; ++P) {
718 ObjCPropertyDecl *Prop = (*P);
719 PropMap[Prop->getIdentifier()] = Prop;
720 }
721 // scan through class's protocols.
722 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
723 E = IDecl->protocol_end(); PI != E; ++PI)
724 CollectImmediateProperties((*PI), PropMap);
725 }
726 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
727 if (!CATDecl->IsClassExtension())
728 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
729 E = CATDecl->prop_end(); P != E; ++P) {
730 ObjCPropertyDecl *Prop = (*P);
731 PropMap[Prop->getIdentifier()] = Prop;
732 }
733 // scan through class's protocols.
734 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
735 E = CATDecl->protocol_end(); PI != E; ++PI)
736 CollectImmediateProperties((*PI), PropMap);
737 }
738 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
739 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
740 E = PDecl->prop_end(); P != E; ++P) {
741 ObjCPropertyDecl *Prop = (*P);
742 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
743 if (!PropEntry)
744 PropEntry = Prop;
745 }
746 // scan through protocol's protocols.
747 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
748 E = PDecl->protocol_end(); PI != E; ++PI)
749 CollectImmediateProperties((*PI), PropMap);
750 }
751}
752
753/// LookupPropertyDecl - Looks up a property in the current class and all
754/// its protocols.
755ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
756 IdentifierInfo *II) {
757 if (const ObjCInterfaceDecl *IDecl =
758 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
759 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
760 E = IDecl->prop_end(); P != E; ++P) {
761 ObjCPropertyDecl *Prop = (*P);
762 if (Prop->getIdentifier() == II)
763 return Prop;
764 }
765 // scan through class's protocols.
766 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
767 E = IDecl->protocol_end(); PI != E; ++PI) {
768 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
769 if (Prop)
770 return Prop;
771 }
772 }
773 else if (const ObjCProtocolDecl *PDecl =
774 dyn_cast<ObjCProtocolDecl>(CDecl)) {
775 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
776 E = PDecl->prop_end(); P != E; ++P) {
777 ObjCPropertyDecl *Prop = (*P);
778 if (Prop->getIdentifier() == II)
779 return Prop;
780 }
781 // scan through protocol's protocols.
782 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
783 E = PDecl->protocol_end(); PI != E; ++PI) {
784 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
785 if (Prop)
786 return Prop;
787 }
788 }
789 return 0;
790}
791
792
793void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl,
794 ObjCContainerDecl *CDecl,
795 const llvm::DenseSet<Selector>& InsMap) {
796 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
797 CollectImmediateProperties(CDecl, PropMap);
798 if (PropMap.empty())
799 return;
800
801 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
802 for (ObjCImplDecl::propimpl_iterator
803 I = IMPDecl->propimpl_begin(),
804 EI = IMPDecl->propimpl_end(); I != EI; ++I)
805 PropImplMap.insert((*I)->getPropertyDecl());
806
807 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
808 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
809 ObjCPropertyDecl *Prop = P->second;
810 // Is there a matching propery synthesize/dynamic?
811 if (Prop->isInvalidDecl() ||
812 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
813 PropImplMap.count(Prop))
814 continue;
815 if (LangOpts.ObjCNonFragileABI2) {
816 ActOnPropertyImplDecl(IMPDecl->getLocation(),
817 SourceLocation(),
818 true, DeclPtrTy::make(IMPDecl),
819 Prop->getIdentifier(),
820 Prop->getIdentifier());
821 continue;
822 }
823 if (!InsMap.count(Prop->getGetterName())) {
824 Diag(Prop->getLocation(),
825 isa<ObjCCategoryDecl>(CDecl) ?
826 diag::warn_setter_getter_impl_required_in_category :
827 diag::warn_setter_getter_impl_required)
828 << Prop->getDeclName() << Prop->getGetterName();
829 Diag(IMPDecl->getLocation(),
830 diag::note_property_impl_required);
831 }
832
833 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
834 Diag(Prop->getLocation(),
835 isa<ObjCCategoryDecl>(CDecl) ?
836 diag::warn_setter_getter_impl_required_in_category :
837 diag::warn_setter_getter_impl_required)
838 << Prop->getDeclName() << Prop->getSetterName();
839 Diag(IMPDecl->getLocation(),
840 diag::note_property_impl_required);
841 }
842 }
843}
844
845void
846Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
847 ObjCContainerDecl* IDecl) {
848 // Rules apply in non-GC mode only
849 if (getLangOptions().getGCMode() != LangOptions::NonGC)
850 return;
851 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
852 E = IDecl->prop_end();
853 I != E; ++I) {
854 ObjCPropertyDecl *Property = (*I);
855 unsigned Attributes = Property->getPropertyAttributes();
856 // We only care about readwrite atomic property.
857 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
858 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
859 continue;
860 if (const ObjCPropertyImplDecl *PIDecl
861 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
862 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
863 continue;
864 ObjCMethodDecl *GetterMethod =
865 IMPDecl->getInstanceMethod(Property->getGetterName());
866 ObjCMethodDecl *SetterMethod =
867 IMPDecl->getInstanceMethod(Property->getSetterName());
868 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
869 SourceLocation MethodLoc =
870 (GetterMethod ? GetterMethod->getLocation()
871 : SetterMethod->getLocation());
872 Diag(MethodLoc, diag::warn_atomic_property_rule)
873 << Property->getIdentifier();
874 Diag(Property->getLocation(), diag::note_property_declare);
875 }
876 }
877 }
878}
879
880/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
881/// have the property type and issue diagnostics if they don't.
882/// Also synthesize a getter/setter method if none exist (and update the
883/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
884/// methods is the "right" thing to do.
885void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
886 ObjCContainerDecl *CD) {
887 ObjCMethodDecl *GetterMethod, *SetterMethod;
888
889 GetterMethod = CD->getInstanceMethod(property->getGetterName());
890 SetterMethod = CD->getInstanceMethod(property->getSetterName());
891 DiagnosePropertyAccessorMismatch(property, GetterMethod,
892 property->getLocation());
893
894 if (SetterMethod) {
895 ObjCPropertyDecl::PropertyAttributeKind CAttr =
896 property->getPropertyAttributes();
897 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
898 Context.getCanonicalType(SetterMethod->getResultType()) !=
899 Context.VoidTy)
900 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
901 if (SetterMethod->param_size() != 1 ||
902 ((*SetterMethod->param_begin())->getType() != property->getType())) {
903 Diag(property->getLocation(),
904 diag::warn_accessor_property_type_mismatch)
905 << property->getDeclName()
906 << SetterMethod->getSelector();
907 Diag(SetterMethod->getLocation(), diag::note_declared_at);
908 }
909 }
910
911 // Synthesize getter/setter methods if none exist.
912 // Find the default getter and if one not found, add one.
913 // FIXME: The synthesized property we set here is misleading. We almost always
914 // synthesize these methods unless the user explicitly provided prototypes
915 // (which is odd, but allowed). Sema should be typechecking that the
916 // declarations jive in that situation (which it is not currently).
917 if (!GetterMethod) {
918 // No instance method of same name as property getter name was found.
919 // Declare a getter method and add it to the list of methods
920 // for this class.
921 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
922 property->getLocation(), property->getGetterName(),
923 property->getType(), 0, CD, true, false, true,
924 (property->getPropertyImplementation() ==
925 ObjCPropertyDecl::Optional) ?
926 ObjCMethodDecl::Optional :
927 ObjCMethodDecl::Required);
928 CD->addDecl(GetterMethod);
929 } else
930 // A user declared getter will be synthesize when @synthesize of
931 // the property with the same name is seen in the @implementation
932 GetterMethod->setSynthesized(true);
933 property->setGetterMethodDecl(GetterMethod);
934
935 // Skip setter if property is read-only.
936 if (!property->isReadOnly()) {
937 // Find the default setter and if one not found, add one.
938 if (!SetterMethod) {
939 // No instance method of same name as property setter name was found.
940 // Declare a setter method and add it to the list of methods
941 // for this class.
942 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
943 property->getLocation(),
944 property->getSetterName(),
945 Context.VoidTy, 0, CD, true, false, true,
946 (property->getPropertyImplementation() ==
947 ObjCPropertyDecl::Optional) ?
948 ObjCMethodDecl::Optional :
949 ObjCMethodDecl::Required);
950 // Invent the arguments for the setter. We don't bother making a
951 // nice name for the argument.
952 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
953 property->getLocation(),
954 property->getIdentifier(),
955 property->getType(),
956 /*TInfo=*/0,
957 VarDecl::None,
958 0);
959 SetterMethod->setMethodParams(Context, &Argument, 1);
960 CD->addDecl(SetterMethod);
961 } else
962 // A user declared setter will be synthesize when @synthesize of
963 // the property with the same name is seen in the @implementation
964 SetterMethod->setSynthesized(true);
965 property->setSetterMethodDecl(SetterMethod);
966 }
967 // Add any synthesized methods to the global pool. This allows us to
968 // handle the following, which is supported by GCC (and part of the design).
969 //
970 // @interface Foo
971 // @property double bar;
972 // @end
973 //
974 // void thisIsUnfortunate() {
975 // id foo;
976 // double bar = [foo bar];
977 // }
978 //
979 if (GetterMethod)
980 AddInstanceMethodToGlobalPool(GetterMethod);
981 if (SetterMethod)
982 AddInstanceMethodToGlobalPool(SetterMethod);
983}
984
985void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
986 SourceLocation Loc,
987 unsigned &Attributes) {
988 // FIXME: Improve the reported location.
989
990 // readonly and readwrite/assign/retain/copy conflict.
991 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
992 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
993 ObjCDeclSpec::DQ_PR_assign |
994 ObjCDeclSpec::DQ_PR_copy |
995 ObjCDeclSpec::DQ_PR_retain))) {
996 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
997 "readwrite" :
998 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
999 "assign" :
1000 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1001 "copy" : "retain";
1002
1003 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1004 diag::err_objc_property_attr_mutually_exclusive :
1005 diag::warn_objc_property_attr_mutually_exclusive)
1006 << "readonly" << which;
1007 }
1008
1009 // Check for copy or retain on non-object types.
1010 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1011 !PropertyTy->isObjCObjectPointerType() &&
1012 !PropertyTy->isBlockPointerType() &&
1013 !Context.isObjCNSObjectType(PropertyTy)) {
1014 Diag(Loc, diag::err_objc_property_requires_object)
1015 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1016 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1017 }
1018
1019 // Check for more than one of { assign, copy, retain }.
1020 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1021 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1022 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1023 << "assign" << "copy";
1024 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1025 }
1026 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1027 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1028 << "assign" << "retain";
1029 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1030 }
1031 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1032 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1033 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1034 << "copy" << "retain";
1035 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1036 }
1037 }
1038
1039 // Warn if user supplied no assignment attribute, property is
1040 // readwrite, and this is an object type.
1041 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1042 ObjCDeclSpec::DQ_PR_retain)) &&
1043 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1044 PropertyTy->isObjCObjectPointerType()) {
1045 // Skip this warning in gc-only mode.
1046 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1047 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1048
1049 // If non-gc code warn that this is likely inappropriate.
1050 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1051 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1052
1053 // FIXME: Implement warning dependent on NSCopying being
1054 // implemented. See also:
1055 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1056 // (please trim this list while you are at it).
1057 }
1058
1059 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1060 && getLangOptions().getGCMode() == LangOptions::GCOnly
1061 && PropertyTy->isBlockPointerType())
1062 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1063}
1064
Ted Kremenek9d64c152010-03-12 00:38:38 +00001065ObjCIvarDecl*
1066Sema::SynthesizeNewPropertyIvar(ObjCInterfaceDecl *IDecl,
1067 IdentifierInfo *NameII) {
1068 ObjCIvarDecl *Ivar = 0;
1069 ObjCPropertyDecl *Prop = LookupPropertyDecl(IDecl, NameII);
1070 if (Prop && !Prop->isInvalidDecl()) {
1071 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
1072 QualType PropType = Context.getCanonicalType(Prop->getType());
1073 assert(EnclosingContext &&
1074 "null DeclContext for synthesized ivar - SynthesizeNewPropertyIvar");
1075 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext,
1076 Prop->getLocation(),
1077 NameII, PropType, /*Dinfo=*/0,
1078 ObjCIvarDecl::Public,
1079 (Expr *)0);
1080 Ivar->setLexicalDeclContext(IDecl);
1081 IDecl->addDecl(Ivar);
1082 Prop->setPropertyIvarDecl(Ivar);
1083 }
1084 return Ivar;
1085}
1086