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