1 //===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements semantic analysis for Objective C @property and
11 // @synthesize declarations.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Sema/SemaInternal.h"
16 #include "clang/Sema/Initialization.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/ExprObjC.h"
19 #include "llvm/ADT/DenseSet.h"
21 using namespace clang;
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 /// Check the internal consistency of a property declaration.
28 static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
29 if (property->isInvalidDecl()) return;
31 ObjCPropertyDecl::PropertyAttributeKind propertyKind
32 = property->getPropertyAttributes();
33 Qualifiers::ObjCLifetime propertyLifetime
34 = property->getType().getObjCLifetime();
36 // Nothing to do if we don't have a lifetime.
37 if (propertyLifetime == Qualifiers::OCL_None) return;
39 Qualifiers::ObjCLifetime expectedLifetime;
42 // Strong properties should have either strong or no lifetime.
43 if (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
44 ObjCPropertyDecl::OBJC_PR_strong |
45 ObjCPropertyDecl::OBJC_PR_copy)) {
46 expectedLifetime = Qualifiers::OCL_Strong;
48 } else if (propertyKind & ObjCPropertyDecl::OBJC_PR_weak) {
49 expectedLifetime = Qualifiers::OCL_Weak;
51 } else if (propertyKind & (ObjCPropertyDecl::OBJC_PR_assign |
52 ObjCPropertyDecl::OBJC_PR_unsafe_unretained) &&
53 property->getType()->isObjCRetainableType()) {
54 expectedLifetime = Qualifiers::OCL_ExplicitNone;
57 // We have a lifetime qualifier but no dominating property
58 // attribute. That's okay.
62 if (propertyLifetime == expectedLifetime) return;
64 property->setInvalidDecl();
65 S.Diag(property->getLocation(),
66 diag::err_arc_inconsistent_property_ownership)
67 << property->getDeclName()
72 Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
78 bool *isOverridingProperty,
79 tok::ObjCKeywordKind MethodImplKind,
80 DeclContext *lexicalDC) {
81 unsigned Attributes = ODS.getPropertyAttributes();
82 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
83 QualType T = TSI->getType();
84 if ((getLangOptions().getGCMode() != LangOptions::NonGC &&
86 (getLangOptions().ObjCAutoRefCount &&
87 T.getObjCLifetime() == Qualifiers::OCL_Weak))
88 Attributes |= ObjCDeclSpec::DQ_PR_weak;
90 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
91 // default is readwrite!
92 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
93 // property is defaulted to 'assign' if it is readwrite and is
95 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
97 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
98 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
99 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
100 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
101 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
103 // Proceed with constructing the ObjCPropertDecls.
104 ObjCContainerDecl *ClassDecl =
105 cast<ObjCContainerDecl>(ClassCategory);
107 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
108 if (CDecl->IsClassExtension()) {
109 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
110 FD, GetterSel, SetterSel,
111 isAssign, isReadWrite,
113 isOverridingProperty, TSI,
116 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
117 if (getLangOptions().ObjCAutoRefCount)
118 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
123 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
124 GetterSel, SetterSel,
125 isAssign, isReadWrite,
126 Attributes, TSI, MethodImplKind);
128 Res->setLexicalDeclContext(lexicalDC);
130 // Validate the attributes on the @property.
131 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
133 if (getLangOptions().ObjCAutoRefCount)
134 checkARCPropertyDecl(*this, Res);
140 Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
141 SourceLocation AtLoc, FieldDeclarator &FD,
142 Selector GetterSel, Selector SetterSel,
144 const bool isReadWrite,
145 const unsigned Attributes,
146 bool *isOverridingProperty,
148 tok::ObjCKeywordKind MethodImplKind) {
150 // Diagnose if this property is already in continuation class.
151 DeclContext *DC = cast<DeclContext>(CDecl);
152 IdentifierInfo *PropertyId = FD.D.getIdentifier();
153 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
156 // Check for duplicate declaration of this property in current and
157 // other class extensions.
158 for (const ObjCCategoryDecl *ClsExtDecl =
159 CCPrimary->getFirstClassExtension();
160 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
161 if (ObjCPropertyDecl *prevDecl =
162 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
163 Diag(AtLoc, diag::err_duplicate_property);
164 Diag(prevDecl->getLocation(), diag::note_property_declare);
169 // Create a new ObjCPropertyDecl with the DeclContext being
170 // the class extension.
171 // FIXME. We should really be using CreatePropertyDecl for this.
172 ObjCPropertyDecl *PDecl =
173 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
174 PropertyId, AtLoc, T);
175 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
176 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
177 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
178 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
179 // Set setter/getter selector name. Needed later.
180 PDecl->setGetterName(GetterSel);
181 PDecl->setSetterName(SetterSel);
182 ProcessDeclAttributes(S, PDecl, FD.D);
185 // We need to look in the @interface to see if the @property was
188 Diag(CDecl->getLocation(), diag::err_continuation_class);
189 *isOverridingProperty = true;
193 // Find the property in continuation class's primary class only.
194 ObjCPropertyDecl *PIDecl =
195 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
198 // No matching property found in the primary class. Just fall thru
199 // and add property to continuation class's primary class.
200 ObjCPropertyDecl *PDecl =
201 CreatePropertyDecl(S, CCPrimary, AtLoc,
202 FD, GetterSel, SetterSel, isAssign, isReadWrite,
203 Attributes, T, MethodImplKind, DC);
205 // A case of continuation class adding a new property in the class. This
206 // is not what it was meant for. However, gcc supports it and so should we.
207 // Make sure setter/getters are declared here.
208 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
209 /* lexicalDC = */ CDecl);
213 // The property 'PIDecl's readonly attribute will be over-ridden
214 // with continuation class's readwrite property attribute!
215 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
216 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
217 unsigned retainCopyNonatomic =
218 (ObjCPropertyDecl::OBJC_PR_retain |
219 ObjCPropertyDecl::OBJC_PR_strong |
220 ObjCPropertyDecl::OBJC_PR_copy |
221 ObjCPropertyDecl::OBJC_PR_nonatomic);
222 if ((Attributes & retainCopyNonatomic) !=
223 (PIkind & retainCopyNonatomic)) {
224 Diag(AtLoc, diag::warn_property_attr_mismatch);
225 Diag(PIDecl->getLocation(), diag::note_property_declare);
227 DeclContext *DC = cast<DeclContext>(CCPrimary);
228 if (!ObjCPropertyDecl::findPropertyDecl(DC,
229 PIDecl->getDeclName().getAsIdentifierInfo())) {
230 // Protocol is not in the primary class. Must build one for it.
231 ObjCDeclSpec ProtocolPropertyODS;
232 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
233 // and ObjCPropertyDecl::PropertyAttributeKind have identical
234 // values. Should consolidate both into one enum type.
236 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
239 Decl *ProtocolPtrTy =
240 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
241 PIDecl->getGetterName(),
242 PIDecl->getSetterName(),
243 CCPrimary, isOverridingProperty,
245 /* lexicalDC = */ CDecl);
246 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
248 PIDecl->makeitReadWriteAttribute();
249 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
250 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
251 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
252 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
253 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
254 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
255 PIDecl->setSetterName(SetterSel);
257 // Tailor the diagnostics for the common case where a readwrite
258 // property is declared both in the @interface and the continuation.
259 // This is a common error where the user often intended the original
260 // declaration to be readonly.
262 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
263 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
264 ? diag::err_use_continuation_class_redeclaration_readwrite
265 : diag::err_use_continuation_class;
267 << CCPrimary->getDeclName();
268 Diag(PIDecl->getLocation(), diag::note_property_declare);
270 *isOverridingProperty = true;
271 // Make sure setter decl is synthesized, and added to primary class's list.
272 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
276 ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
277 ObjCContainerDecl *CDecl,
278 SourceLocation AtLoc,
283 const bool isReadWrite,
284 const unsigned Attributes,
285 TypeSourceInfo *TInfo,
286 tok::ObjCKeywordKind MethodImplKind,
287 DeclContext *lexicalDC){
288 IdentifierInfo *PropertyId = FD.D.getIdentifier();
289 QualType T = TInfo->getType();
291 // Issue a warning if property is 'assign' as default and its object, which is
292 // gc'able conforms to NSCopying protocol
293 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
294 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
295 if (const ObjCObjectPointerType *ObjPtrTy =
296 T->getAs<ObjCObjectPointerType>()) {
297 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
299 if (ObjCProtocolDecl* PNSCopying =
300 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
301 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
302 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
304 if (T->isObjCObjectType())
305 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
307 DeclContext *DC = cast<DeclContext>(CDecl);
308 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
309 FD.D.getIdentifierLoc(),
310 PropertyId, AtLoc, TInfo);
312 if (ObjCPropertyDecl *prevDecl =
313 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
314 Diag(PDecl->getLocation(), diag::err_duplicate_property);
315 Diag(prevDecl->getLocation(), diag::note_property_declare);
316 PDecl->setInvalidDecl();
321 PDecl->setLexicalDeclContext(lexicalDC);
324 if (T->isArrayType() || T->isFunctionType()) {
325 Diag(AtLoc, diag::err_property_type) << T;
326 PDecl->setInvalidDecl();
329 ProcessDeclAttributes(S, PDecl, FD.D);
331 // Regardless of setter/getter attribute, we save the default getter/setter
332 // selector names in anticipation of declaration of setter/getter methods.
333 PDecl->setGetterName(GetterSel);
334 PDecl->setSetterName(SetterSel);
336 unsigned attributesAsWritten = 0;
337 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
338 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
339 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
340 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
341 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
342 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
343 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
344 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
345 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
346 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
347 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
348 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
349 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
350 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
351 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
352 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
353 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
354 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
355 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
356 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
357 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
358 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
359 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
360 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
362 PDecl->setPropertyAttributesAsWritten(
363 (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten);
365 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
366 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
368 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
369 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
371 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
372 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
375 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
377 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
378 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
380 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
381 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
383 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
384 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
386 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
387 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
389 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
390 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
393 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
395 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
396 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
397 else if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
398 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
400 // 'unsafe_unretained' is alias for 'assign'.
401 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
402 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
404 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
406 if (MethodImplKind == tok::objc_required)
407 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
408 else if (MethodImplKind == tok::objc_optional)
409 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
414 static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
415 ObjCPropertyDecl *property,
416 ObjCIvarDecl *ivar) {
417 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
419 QualType propertyType = property->getType();
420 Qualifiers::ObjCLifetime propertyLifetime = propertyType.getObjCLifetime();
421 ObjCPropertyDecl::PropertyAttributeKind propertyKind
422 = property->getPropertyAttributes();
424 QualType ivarType = ivar->getType();
425 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
427 // Case 1: strong properties.
428 if (propertyLifetime == Qualifiers::OCL_Strong ||
429 (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
430 ObjCPropertyDecl::OBJC_PR_strong |
431 ObjCPropertyDecl::OBJC_PR_copy))) {
432 switch (ivarLifetime) {
433 case Qualifiers::OCL_Strong:
437 case Qualifiers::OCL_None:
438 case Qualifiers::OCL_Autoreleasing:
439 // These aren't valid lifetimes for object ivars; don't diagnose twice.
442 case Qualifiers::OCL_ExplicitNone:
443 case Qualifiers::OCL_Weak:
444 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
445 << property->getDeclName()
446 << ivar->getDeclName()
451 // Case 2: weak properties.
452 } else if (propertyLifetime == Qualifiers::OCL_Weak ||
453 (propertyKind & ObjCPropertyDecl::OBJC_PR_weak)) {
454 switch (ivarLifetime) {
455 case Qualifiers::OCL_Weak:
459 case Qualifiers::OCL_None:
460 case Qualifiers::OCL_Autoreleasing:
461 // These aren't valid lifetimes for object ivars; don't diagnose twice.
464 case Qualifiers::OCL_ExplicitNone:
465 case Qualifiers::OCL_Strong:
466 S.Diag(propertyImplLoc, diag::error_weak_property)
467 << property->getDeclName()
468 << ivar->getDeclName();
472 // Case 3: assign properties.
473 } else if ((propertyKind & ObjCPropertyDecl::OBJC_PR_assign) &&
474 propertyType->isObjCRetainableType()) {
475 switch (ivarLifetime) {
476 case Qualifiers::OCL_ExplicitNone:
480 case Qualifiers::OCL_None:
481 case Qualifiers::OCL_Autoreleasing:
482 // These aren't valid lifetimes for object ivars; don't diagnose twice.
485 case Qualifiers::OCL_Weak:
486 case Qualifiers::OCL_Strong:
487 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
488 << property->getDeclName()
489 << ivar->getDeclName();
493 // Any other property should be ignored.
498 S.Diag(property->getLocation(), diag::note_property_declare);
502 /// ActOnPropertyImplDecl - This routine performs semantic checks and
503 /// builds the AST node for a property implementation declaration; declared
504 /// as @synthesize or @dynamic.
506 Decl *Sema::ActOnPropertyImplDecl(Scope *S,
507 SourceLocation AtLoc,
508 SourceLocation PropertyLoc,
510 Decl *ClassCatImpDecl,
511 IdentifierInfo *PropertyId,
512 IdentifierInfo *PropertyIvar,
513 SourceLocation PropertyIvarLoc) {
514 ObjCContainerDecl *ClassImpDecl =
515 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
516 // Make sure we have a context for the property implementation declaration.
518 Diag(AtLoc, diag::error_missing_property_context);
521 ObjCPropertyDecl *property = 0;
522 ObjCInterfaceDecl* IDecl = 0;
523 // Find the class or category class where this property must have
525 ObjCImplementationDecl *IC = 0;
526 ObjCCategoryImplDecl* CatImplClass = 0;
527 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
528 IDecl = IC->getClassInterface();
529 // We always synthesize an interface for an implementation
530 // without an interface decl. So, IDecl is always non-zero.
532 "ActOnPropertyImplDecl - @implementation without @interface");
534 // Look for this property declaration in the @implementation's @interface
535 property = IDecl->FindPropertyDeclaration(PropertyId);
537 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
540 unsigned PIkind = property->getPropertyAttributesAsWritten();
541 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
542 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
544 Diag(AtLoc, diag::warn_implicit_atomic_property);
546 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
547 Diag(property->getLocation(), diag::note_property_declare);
550 if (const ObjCCategoryDecl *CD =
551 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
552 if (!CD->IsClassExtension()) {
553 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
554 Diag(property->getLocation(), diag::note_property_declare);
558 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
560 Diag(AtLoc, diag::error_synthesize_category_decl);
563 IDecl = CatImplClass->getClassInterface();
565 Diag(AtLoc, diag::error_missing_property_interface);
568 ObjCCategoryDecl *Category =
569 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
571 // If category for this implementation not found, it is an error which
572 // has already been reported eralier.
575 // Look for this property declaration in @implementation's category
576 property = Category->FindPropertyDeclaration(PropertyId);
578 Diag(PropertyLoc, diag::error_bad_category_property_decl)
579 << Category->getDeclName();
583 Diag(AtLoc, diag::error_bad_property_context);
586 ObjCIvarDecl *Ivar = 0;
587 // Check that we have a valid, previously declared ivar for @synthesize
589 if (getLangOptions().ObjCAutoRefCount &&
590 !property->hasWrittenStorageAttribute() &&
591 property->getType()->isObjCRetainableType()) {
592 Diag(PropertyLoc, diag::err_arc_objc_property_default_assign_on_object);
593 Diag(property->getLocation(), diag::note_property_declare);
598 PropertyIvar = PropertyId;
599 ObjCPropertyDecl::PropertyAttributeKind kind
600 = property->getPropertyAttributes();
601 QualType PropType = Context.getCanonicalType(property->getType());
602 QualType PropertyIvarType = PropType;
603 if (PropType->isReferenceType())
604 PropertyIvarType = cast<ReferenceType>(PropType)->getPointeeType();
605 // Check that this is a previously declared 'ivar' in 'IDecl' interface
606 ObjCInterfaceDecl *ClassDeclared;
607 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
609 // In ARC, give the ivar a lifetime qualifier based on its
610 // property attributes.
611 if (getLangOptions().ObjCAutoRefCount &&
612 !PropertyIvarType.getObjCLifetime()) {
614 // retain/copy have retaining lifetime.
615 if (kind & (ObjCPropertyDecl::OBJC_PR_retain |
616 ObjCPropertyDecl::OBJC_PR_strong |
617 ObjCPropertyDecl::OBJC_PR_copy)) {
619 qs.addObjCLifetime(Qualifiers::OCL_Strong);
620 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
622 else if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
623 if (!getLangOptions().ObjCRuntimeHasWeak) {
624 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
625 Diag(property->getLocation(), diag::note_property_declare);
628 qs.addObjCLifetime(Qualifiers::OCL_Weak);
629 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
631 else if (kind & ObjCPropertyDecl::OBJC_PR_assign &&
632 PropertyIvarType->isObjCRetainableType()) {
633 // assume that an 'assign' property synthesizes __unsafe_unretained
636 qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
637 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
641 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
642 !getLangOptions().ObjCAutoRefCount &&
643 getLangOptions().getGCMode() == LangOptions::NonGC) {
644 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
645 Diag(property->getLocation(), diag::note_property_declare);
648 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
649 PropertyLoc, PropertyLoc, PropertyIvar,
650 PropertyIvarType, /*Dinfo=*/0,
651 ObjCIvarDecl::Private,
653 ClassImpDecl->addDecl(Ivar);
654 IDecl->makeDeclVisibleInContext(Ivar, false);
655 property->setPropertyIvarDecl(Ivar);
657 if (!getLangOptions().ObjCNonFragileABI)
658 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
659 // Note! I deliberately want it to fall thru so, we have a
660 // a property implementation and to avoid future warnings.
661 } else if (getLangOptions().ObjCNonFragileABI &&
662 ClassDeclared != IDecl) {
663 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
664 << property->getDeclName() << Ivar->getDeclName()
665 << ClassDeclared->getDeclName();
666 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
667 << Ivar << Ivar->getName();
668 // Note! I deliberately want it to fall thru so more errors are caught.
670 QualType IvarType = Context.getCanonicalType(Ivar->getType());
672 // Check that type of property and its ivar are type compatible.
673 if (PropertyIvarType != IvarType) {
675 if (isa<ObjCObjectPointerType>(PropertyIvarType)
676 && isa<ObjCObjectPointerType>(IvarType))
678 Context.canAssignObjCInterfaces(
679 PropertyIvarType->getAs<ObjCObjectPointerType>(),
680 IvarType->getAs<ObjCObjectPointerType>());
682 SourceLocation Loc = PropertyIvarLoc;
685 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
689 Diag(PropertyLoc, diag::error_property_ivar_type)
690 << property->getDeclName() << PropType
691 << Ivar->getDeclName() << IvarType;
692 Diag(Ivar->getLocation(), diag::note_ivar_decl);
693 // Note! I deliberately want it to fall thru so, we have a
694 // a property implementation and to avoid future warnings.
697 // FIXME! Rules for properties are somewhat different that those
698 // for assignments. Use a new routine to consolidate all cases;
699 // specifically for property redeclarations as well as for ivars.
700 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
701 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
702 if (lhsType != rhsType &&
703 lhsType->isArithmeticType()) {
704 Diag(PropertyLoc, diag::error_property_ivar_type)
705 << property->getDeclName() << PropType
706 << Ivar->getDeclName() << IvarType;
707 Diag(Ivar->getLocation(), diag::note_ivar_decl);
708 // Fall thru - see previous comment
710 // __weak is explicit. So it works on Canonical type.
711 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
712 getLangOptions().getGCMode() != LangOptions::NonGC)) {
713 Diag(PropertyLoc, diag::error_weak_property)
714 << property->getDeclName() << Ivar->getDeclName();
715 // Fall thru - see previous comment
717 // Fall thru - see previous comment
718 if ((property->getType()->isObjCObjectPointerType() ||
719 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
720 getLangOptions().getGCMode() != LangOptions::NonGC) {
721 Diag(PropertyLoc, diag::error_strong_property)
722 << property->getDeclName() << Ivar->getDeclName();
723 // Fall thru - see previous comment
726 if (getLangOptions().ObjCAutoRefCount)
727 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
728 } else if (PropertyIvar)
730 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
732 assert (property && "ActOnPropertyImplDecl - property declaration missing");
733 ObjCPropertyImplDecl *PIDecl =
734 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
737 ObjCPropertyImplDecl::Synthesize
738 : ObjCPropertyImplDecl::Dynamic),
739 Ivar, PropertyIvarLoc);
740 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
741 getterMethod->createImplicitParams(Context, IDecl);
742 if (getLangOptions().CPlusPlus && Synthesize &&
743 Ivar->getType()->isRecordType()) {
744 // For Objective-C++, need to synthesize the AST for the IVAR object to be
745 // returned by the getter as it must conform to C++'s copy-return rules.
746 // FIXME. Eventually we want to do this for Objective-C as well.
747 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
748 DeclRefExpr *SelfExpr =
749 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
750 VK_RValue, SourceLocation());
752 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
753 SelfExpr, true, true);
755 PerformCopyInitialization(InitializedEntity::InitializeResult(
757 getterMethod->getResultType(),
761 if (!Res.isInvalid()) {
762 Expr *ResExpr = Res.takeAs<Expr>();
764 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
765 PIDecl->setGetterCXXConstructor(ResExpr);
768 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
769 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
770 Diag(getterMethod->getLocation(),
771 diag::warn_property_getter_owning_mismatch);
772 Diag(property->getLocation(), diag::note_property_declare);
775 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
776 setterMethod->createImplicitParams(Context, IDecl);
777 if (getLangOptions().CPlusPlus && Synthesize
778 && Ivar->getType()->isRecordType()) {
779 // FIXME. Eventually we want to do this for Objective-C as well.
780 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
781 DeclRefExpr *SelfExpr =
782 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
783 VK_RValue, SourceLocation());
785 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
786 SelfExpr, true, true);
787 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
788 ParmVarDecl *Param = (*P);
789 QualType T = Param->getType();
790 if (T->isReferenceType())
791 T = T->getAs<ReferenceType>()->getPointeeType();
792 Expr *rhs = new (Context) DeclRefExpr(Param, T,
793 VK_LValue, SourceLocation());
794 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
795 BO_Assign, lhs, rhs);
796 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
802 if (ObjCPropertyImplDecl *PPIDecl =
803 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
804 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
805 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
807 Diag(PPIDecl->getLocation(), diag::note_previous_use);
810 if (ObjCPropertyImplDecl *PPIDecl
811 = IC->FindPropertyImplDecl(PropertyId)) {
812 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
813 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
816 IC->addPropertyImplementation(PIDecl);
817 if (getLangOptions().ObjCDefaultSynthProperties &&
818 getLangOptions().ObjCNonFragileABI2) {
819 // Diagnose if an ivar was lazily synthesdized due to a previous
820 // use and if 1) property is @dynamic or 2) property is synthesized
821 // but it requires an ivar of different name.
822 ObjCInterfaceDecl *ClassDeclared=0;
823 ObjCIvarDecl *Ivar = 0;
825 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
827 if (PropertyIvar && PropertyIvar != PropertyId)
828 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
830 // Issue diagnostics only if Ivar belongs to current class.
831 if (Ivar && Ivar->getSynthesize() &&
832 IC->getClassInterface() == ClassDeclared) {
833 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
835 Ivar->setInvalidDecl();
840 if (ObjCPropertyImplDecl *PPIDecl =
841 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
842 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
843 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
845 Diag(PPIDecl->getLocation(), diag::note_previous_use);
848 if (ObjCPropertyImplDecl *PPIDecl =
849 CatImplClass->FindPropertyImplDecl(PropertyId)) {
850 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
851 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
854 CatImplClass->addPropertyImplementation(PIDecl);
860 //===----------------------------------------------------------------------===//
862 //===----------------------------------------------------------------------===//
864 /// DiagnosePropertyMismatch - Compares two properties for their
865 /// attributes and types and warns on a variety of inconsistencies.
868 Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
869 ObjCPropertyDecl *SuperProperty,
870 const IdentifierInfo *inheritedName) {
871 ObjCPropertyDecl::PropertyAttributeKind CAttr =
872 Property->getPropertyAttributes();
873 ObjCPropertyDecl::PropertyAttributeKind SAttr =
874 SuperProperty->getPropertyAttributes();
875 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
876 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
877 Diag(Property->getLocation(), diag::warn_readonly_property)
878 << Property->getDeclName() << inheritedName;
879 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
880 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
881 Diag(Property->getLocation(), diag::warn_property_attribute)
882 << Property->getDeclName() << "copy" << inheritedName;
884 unsigned CAttrRetain =
886 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
887 unsigned SAttrRetain =
889 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
890 bool CStrong = (CAttrRetain != 0);
891 bool SStrong = (SAttrRetain != 0);
892 if (CStrong != SStrong)
893 Diag(Property->getLocation(), diag::warn_property_attribute)
894 << Property->getDeclName() << "retain (or strong)" << inheritedName;
897 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
898 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
899 Diag(Property->getLocation(), diag::warn_property_attribute)
900 << Property->getDeclName() << "atomic" << inheritedName;
901 if (Property->getSetterName() != SuperProperty->getSetterName())
902 Diag(Property->getLocation(), diag::warn_property_attribute)
903 << Property->getDeclName() << "setter" << inheritedName;
904 if (Property->getGetterName() != SuperProperty->getGetterName())
905 Diag(Property->getLocation(), diag::warn_property_attribute)
906 << Property->getDeclName() << "getter" << inheritedName;
909 Context.getCanonicalType(SuperProperty->getType());
911 Context.getCanonicalType(Property->getType());
913 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
914 // Do cases not handled in above.
915 // FIXME. For future support of covariant property types, revisit this.
916 bool IncompatibleObjC = false;
917 QualType ConvertedType;
918 if (!isObjCPointerConversion(RHSType, LHSType,
919 ConvertedType, IncompatibleObjC) ||
921 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
922 << Property->getType() << SuperProperty->getType() << inheritedName;
926 bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
927 ObjCMethodDecl *GetterMethod,
928 SourceLocation Loc) {
930 GetterMethod->getResultType() != property->getType()) {
931 AssignConvertType result = Incompatible;
932 if (property->getType()->isObjCObjectPointerType())
933 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
934 property->getType());
935 if (result != Compatible) {
936 Diag(Loc, diag::warn_accessor_property_type_mismatch)
937 << property->getDeclName()
938 << GetterMethod->getSelector();
939 Diag(GetterMethod->getLocation(), diag::note_declared_at);
946 /// ComparePropertiesInBaseAndSuper - This routine compares property
947 /// declarations in base and its super class, if any, and issues
948 /// diagnostics in a variety of inconsistent situations.
950 void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
951 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
955 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
956 E = SDecl->prop_end(); S != E; ++S) {
957 ObjCPropertyDecl *SuperPDecl = (*S);
958 // Does property in super class has declaration in current class?
959 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
960 E = IDecl->prop_end(); I != E; ++I) {
961 ObjCPropertyDecl *PDecl = (*I);
962 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
963 DiagnosePropertyMismatch(PDecl, SuperPDecl,
964 SDecl->getIdentifier());
969 /// MatchOneProtocolPropertiesInClass - This routine goes thru the list
970 /// of properties declared in a protocol and compares their attribute against
971 /// the same property declared in the class or category.
973 Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
974 ObjCProtocolDecl *PDecl) {
975 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
978 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
979 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
980 if (!CatDecl->IsClassExtension())
981 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
982 E = PDecl->prop_end(); P != E; ++P) {
983 ObjCPropertyDecl *Pr = (*P);
984 ObjCCategoryDecl::prop_iterator CP, CE;
985 // Is this property already in category's list of properties?
986 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
987 if ((*CP)->getIdentifier() == Pr->getIdentifier())
990 // Property protocol already exist in class. Diagnose any mismatch.
991 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
995 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
996 E = PDecl->prop_end(); P != E; ++P) {
997 ObjCPropertyDecl *Pr = (*P);
998 ObjCInterfaceDecl::prop_iterator CP, CE;
999 // Is this property already in class's list of properties?
1000 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1001 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1004 // Property protocol already exist in class. Diagnose any mismatch.
1005 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1009 /// CompareProperties - This routine compares properties
1010 /// declared in 'ClassOrProtocol' objects (which can be a class or an
1011 /// inherited protocol with the list of properties for class/category 'CDecl'
1013 void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1014 Decl *ClassDecl = ClassOrProtocol;
1015 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1019 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1020 assert (CatDecl && "CompareProperties");
1021 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1022 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1023 E = MDecl->protocol_end(); P != E; ++P)
1024 // Match properties of category with those of protocol (*P)
1025 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1027 // Go thru the list of protocols for this category and recursively match
1028 // their properties with those in the category.
1029 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1030 E = CatDecl->protocol_end(); P != E; ++P)
1031 CompareProperties(CatDecl, *P);
1033 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1034 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1035 E = MD->protocol_end(); P != E; ++P)
1036 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1041 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
1042 for (ObjCInterfaceDecl::all_protocol_iterator
1043 P = MDecl->all_referenced_protocol_begin(),
1044 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
1045 // Match properties of class IDecl with those of protocol (*P).
1046 MatchOneProtocolPropertiesInClass(IDecl, *P);
1048 // Go thru the list of protocols for this class and recursively match
1049 // their properties with those declared in the class.
1050 for (ObjCInterfaceDecl::all_protocol_iterator
1051 P = IDecl->all_referenced_protocol_begin(),
1052 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
1053 CompareProperties(IDecl, *P);
1055 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1056 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1057 E = MD->protocol_end(); P != E; ++P)
1058 MatchOneProtocolPropertiesInClass(IDecl, *P);
1062 /// isPropertyReadonly - Return true if property is readonly, by searching
1063 /// for the property in the class and in its categories and implementations
1065 bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1066 ObjCInterfaceDecl *IDecl) {
1067 // by far the most common case.
1068 if (!PDecl->isReadOnly())
1070 // Even if property is ready only, if interface has a user defined setter,
1071 // it is not considered read only.
1072 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1075 // Main class has the property as 'readonly'. Must search
1076 // through the category list to see if the property's
1077 // attribute has been over-ridden to 'readwrite'.
1078 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1079 Category; Category = Category->getNextClassCategory()) {
1080 // Even if property is ready only, if a category has a user defined setter,
1081 // it is not considered read only.
1082 if (Category->getInstanceMethod(PDecl->getSetterName()))
1084 ObjCPropertyDecl *P =
1085 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1086 if (P && !P->isReadOnly())
1090 // Also, check for definition of a setter method in the implementation if
1092 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1093 if (ObjCImplementationDecl *IMD =
1094 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1095 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1097 } else if (ObjCCategoryImplDecl *CIMD =
1098 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1099 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1103 // Lastly, look through the implementation (if one is in scope).
1104 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1105 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1107 // If all fails, look at the super class.
1108 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1109 return isPropertyReadonly(PDecl, SIDecl);
1113 /// CollectImmediateProperties - This routine collects all properties in
1114 /// the class and its conforming protocols; but not those it its super class.
1115 void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
1116 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1117 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
1118 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1119 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1120 E = IDecl->prop_end(); P != E; ++P) {
1121 ObjCPropertyDecl *Prop = (*P);
1122 PropMap[Prop->getIdentifier()] = Prop;
1124 // scan through class's protocols.
1125 for (ObjCInterfaceDecl::all_protocol_iterator
1126 PI = IDecl->all_referenced_protocol_begin(),
1127 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
1128 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
1130 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1131 if (!CATDecl->IsClassExtension())
1132 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1133 E = CATDecl->prop_end(); P != E; ++P) {
1134 ObjCPropertyDecl *Prop = (*P);
1135 PropMap[Prop->getIdentifier()] = Prop;
1137 // scan through class's protocols.
1138 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1139 E = CATDecl->protocol_end(); PI != E; ++PI)
1140 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
1142 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1143 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1144 E = PDecl->prop_end(); P != E; ++P) {
1145 ObjCPropertyDecl *Prop = (*P);
1146 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1147 // Exclude property for protocols which conform to class's super-class,
1148 // as super-class has to implement the property.
1149 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
1150 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1155 // scan through protocol's protocols.
1156 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1157 E = PDecl->protocol_end(); PI != E; ++PI)
1158 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
1162 /// CollectClassPropertyImplementations - This routine collects list of
1163 /// properties to be implemented in the class. This includes, class's
1164 /// and its conforming protocols' properties.
1165 static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1166 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1167 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1168 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1169 E = IDecl->prop_end(); P != E; ++P) {
1170 ObjCPropertyDecl *Prop = (*P);
1171 PropMap[Prop->getIdentifier()] = Prop;
1173 for (ObjCInterfaceDecl::all_protocol_iterator
1174 PI = IDecl->all_referenced_protocol_begin(),
1175 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
1176 CollectClassPropertyImplementations((*PI), PropMap);
1178 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1179 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1180 E = PDecl->prop_end(); P != E; ++P) {
1181 ObjCPropertyDecl *Prop = (*P);
1182 PropMap[Prop->getIdentifier()] = Prop;
1184 // scan through protocol's protocols.
1185 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1186 E = PDecl->protocol_end(); PI != E; ++PI)
1187 CollectClassPropertyImplementations((*PI), PropMap);
1191 /// CollectSuperClassPropertyImplementations - This routine collects list of
1192 /// properties to be implemented in super class(s) and also coming from their
1193 /// conforming protocols.
1194 static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1195 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1196 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1198 CollectClassPropertyImplementations(SDecl, PropMap);
1199 SDecl = SDecl->getSuperClass();
1204 /// LookupPropertyDecl - Looks up a property in the current class and all
1206 ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1207 IdentifierInfo *II) {
1208 if (const ObjCInterfaceDecl *IDecl =
1209 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1210 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1211 E = IDecl->prop_end(); P != E; ++P) {
1212 ObjCPropertyDecl *Prop = (*P);
1213 if (Prop->getIdentifier() == II)
1216 // scan through class's protocols.
1217 for (ObjCInterfaceDecl::all_protocol_iterator
1218 PI = IDecl->all_referenced_protocol_begin(),
1219 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
1220 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1225 else if (const ObjCProtocolDecl *PDecl =
1226 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1227 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1228 E = PDecl->prop_end(); P != E; ++P) {
1229 ObjCPropertyDecl *Prop = (*P);
1230 if (Prop->getIdentifier() == II)
1233 // scan through protocol's protocols.
1234 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1235 E = PDecl->protocol_end(); PI != E; ++PI) {
1236 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1244 /// DefaultSynthesizeProperties - This routine default synthesizes all
1245 /// properties which must be synthesized in class's @implementation.
1246 void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
1247 ObjCInterfaceDecl *IDecl) {
1249 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1250 CollectClassPropertyImplementations(IDecl, PropMap);
1251 if (PropMap.empty())
1253 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1254 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1256 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1257 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1258 ObjCPropertyDecl *Prop = P->second;
1259 // If property to be implemented in the super class, ignore.
1260 if (SuperPropMap[Prop->getIdentifier()])
1262 // Is there a matching propery synthesize/dynamic?
1263 if (Prop->isInvalidDecl() ||
1264 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1265 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1267 // Property may have been synthesized by user.
1268 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1270 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1271 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1273 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1278 // We use invalid SourceLocations for the synthesized ivars since they
1279 // aren't really synthesized at a particular location; they just exist.
1280 // Saying that they are located at the @implementation isn't really going
1282 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1284 Prop->getIdentifier(), Prop->getIdentifier(),
1289 void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
1290 ObjCContainerDecl *CDecl,
1291 const llvm::DenseSet<Selector>& InsMap) {
1292 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1293 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1294 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1296 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1297 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
1298 if (PropMap.empty())
1301 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1302 for (ObjCImplDecl::propimpl_iterator
1303 I = IMPDecl->propimpl_begin(),
1304 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1305 PropImplMap.insert((*I)->getPropertyDecl());
1307 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1308 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1309 ObjCPropertyDecl *Prop = P->second;
1310 // Is there a matching propery synthesize/dynamic?
1311 if (Prop->isInvalidDecl() ||
1312 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1313 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
1315 if (!InsMap.count(Prop->getGetterName())) {
1316 Diag(Prop->getLocation(),
1317 isa<ObjCCategoryDecl>(CDecl) ?
1318 diag::warn_setter_getter_impl_required_in_category :
1319 diag::warn_setter_getter_impl_required)
1320 << Prop->getDeclName() << Prop->getGetterName();
1321 Diag(IMPDecl->getLocation(),
1322 diag::note_property_impl_required);
1325 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1326 Diag(Prop->getLocation(),
1327 isa<ObjCCategoryDecl>(CDecl) ?
1328 diag::warn_setter_getter_impl_required_in_category :
1329 diag::warn_setter_getter_impl_required)
1330 << Prop->getDeclName() << Prop->getSetterName();
1331 Diag(IMPDecl->getLocation(),
1332 diag::note_property_impl_required);
1338 Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1339 ObjCContainerDecl* IDecl) {
1340 // Rules apply in non-GC mode only
1341 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1343 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1344 E = IDecl->prop_end();
1346 ObjCPropertyDecl *Property = (*I);
1347 ObjCMethodDecl *GetterMethod = 0;
1348 ObjCMethodDecl *SetterMethod = 0;
1349 bool LookedUpGetterSetter = false;
1351 unsigned Attributes = Property->getPropertyAttributes();
1352 unsigned AttributesAsWrittern = Property->getPropertyAttributesAsWritten();
1354 if (!(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_atomic) &&
1355 !(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
1356 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1357 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1358 LookedUpGetterSetter = true;
1360 Diag(GetterMethod->getLocation(),
1361 diag::warn_default_atomic_custom_getter_setter)
1362 << Property->getIdentifier() << 0;
1363 Diag(Property->getLocation(), diag::note_property_declare);
1366 Diag(SetterMethod->getLocation(),
1367 diag::warn_default_atomic_custom_getter_setter)
1368 << Property->getIdentifier() << 1;
1369 Diag(Property->getLocation(), diag::note_property_declare);
1373 // We only care about readwrite atomic property.
1374 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1375 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1377 if (const ObjCPropertyImplDecl *PIDecl
1378 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1379 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1381 if (!LookedUpGetterSetter) {
1382 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1383 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1384 LookedUpGetterSetter = true;
1386 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1387 SourceLocation MethodLoc =
1388 (GetterMethod ? GetterMethod->getLocation()
1389 : SetterMethod->getLocation());
1390 Diag(MethodLoc, diag::warn_atomic_property_rule)
1391 << Property->getIdentifier();
1392 Diag(Property->getLocation(), diag::note_property_declare);
1398 void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
1399 if (getLangOptions().getGCMode() == LangOptions::GCOnly)
1402 for (ObjCImplementationDecl::propimpl_iterator
1403 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1404 ObjCPropertyImplDecl *PID = *i;
1405 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1408 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
1409 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1410 !D->getInstanceMethod(PD->getGetterName())) {
1411 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1414 ObjCMethodFamily family = method->getMethodFamily();
1415 if (family == OMF_alloc || family == OMF_copy ||
1416 family == OMF_mutableCopy || family == OMF_new) {
1417 if (getLangOptions().ObjCAutoRefCount)
1418 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1420 Diag(PID->getLocation(), diag::warn_ownin_getter_rule);
1421 Diag(PD->getLocation(), diag::note_property_declare);
1427 /// AddPropertyAttrs - Propagates attributes from a property to the
1428 /// implicitly-declared getter or setter for that property.
1429 static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1430 ObjCPropertyDecl *Property) {
1431 // Should we just clone all attributes over?
1432 for (Decl::attr_iterator A = Property->attr_begin(),
1433 AEnd = Property->attr_end();
1435 if (isa<DeprecatedAttr>(*A) ||
1436 isa<UnavailableAttr>(*A) ||
1437 isa<AvailabilityAttr>(*A))
1438 PropertyMethod->addAttr((*A)->clone(S.Context));
1442 /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1443 /// have the property type and issue diagnostics if they don't.
1444 /// Also synthesize a getter/setter method if none exist (and update the
1445 /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1446 /// methods is the "right" thing to do.
1447 void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1448 ObjCContainerDecl *CD,
1449 ObjCPropertyDecl *redeclaredProperty,
1450 ObjCContainerDecl *lexicalDC) {
1452 ObjCMethodDecl *GetterMethod, *SetterMethod;
1454 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1455 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1456 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1457 property->getLocation());
1460 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1461 property->getPropertyAttributes();
1462 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1463 Context.getCanonicalType(SetterMethod->getResultType()) !=
1465 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1466 if (SetterMethod->param_size() != 1 ||
1467 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1468 Diag(property->getLocation(),
1469 diag::warn_accessor_property_type_mismatch)
1470 << property->getDeclName()
1471 << SetterMethod->getSelector();
1472 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1476 // Synthesize getter/setter methods if none exist.
1477 // Find the default getter and if one not found, add one.
1478 // FIXME: The synthesized property we set here is misleading. We almost always
1479 // synthesize these methods unless the user explicitly provided prototypes
1480 // (which is odd, but allowed). Sema should be typechecking that the
1481 // declarations jive in that situation (which it is not currently).
1482 if (!GetterMethod) {
1483 // No instance method of same name as property getter name was found.
1484 // Declare a getter method and add it to the list of methods
1486 SourceLocation Loc = redeclaredProperty ?
1487 redeclaredProperty->getLocation() :
1488 property->getLocation();
1490 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1491 property->getGetterName(),
1492 property->getType(), 0, CD, true, false, true,
1494 (property->getPropertyImplementation() ==
1495 ObjCPropertyDecl::Optional) ?
1496 ObjCMethodDecl::Optional :
1497 ObjCMethodDecl::Required);
1498 CD->addDecl(GetterMethod);
1500 AddPropertyAttrs(*this, GetterMethod, property);
1502 // FIXME: Eventually this shouldn't be needed, as the lexical context
1503 // and the real context should be the same.
1505 GetterMethod->setLexicalDeclContext(lexicalDC);
1506 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1507 GetterMethod->addAttr(
1508 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
1510 // A user declared getter will be synthesize when @synthesize of
1511 // the property with the same name is seen in the @implementation
1512 GetterMethod->setSynthesized(true);
1513 property->setGetterMethodDecl(GetterMethod);
1515 // Skip setter if property is read-only.
1516 if (!property->isReadOnly()) {
1517 // Find the default setter and if one not found, add one.
1518 if (!SetterMethod) {
1519 // No instance method of same name as property setter name was found.
1520 // Declare a setter method and add it to the list of methods
1522 SourceLocation Loc = redeclaredProperty ?
1523 redeclaredProperty->getLocation() :
1524 property->getLocation();
1527 ObjCMethodDecl::Create(Context, Loc, Loc,
1528 property->getSetterName(), Context.VoidTy, 0,
1529 CD, true, false, true, false,
1530 (property->getPropertyImplementation() ==
1531 ObjCPropertyDecl::Optional) ?
1532 ObjCMethodDecl::Optional :
1533 ObjCMethodDecl::Required);
1535 // Invent the arguments for the setter. We don't bother making a
1536 // nice name for the argument.
1537 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1539 property->getIdentifier(),
1540 property->getType().getUnqualifiedType(),
1545 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
1547 AddPropertyAttrs(*this, SetterMethod, property);
1549 CD->addDecl(SetterMethod);
1550 // FIXME: Eventually this shouldn't be needed, as the lexical context
1551 // and the real context should be the same.
1553 SetterMethod->setLexicalDeclContext(lexicalDC);
1555 // A user declared setter will be synthesize when @synthesize of
1556 // the property with the same name is seen in the @implementation
1557 SetterMethod->setSynthesized(true);
1558 property->setSetterMethodDecl(SetterMethod);
1560 // Add any synthesized methods to the global pool. This allows us to
1561 // handle the following, which is supported by GCC (and part of the design).
1564 // @property double bar;
1567 // void thisIsUnfortunate() {
1569 // double bar = [foo bar];
1573 AddInstanceMethodToGlobalPool(GetterMethod);
1575 AddInstanceMethodToGlobalPool(SetterMethod);
1578 void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
1580 unsigned &Attributes) {
1581 // FIXME: Improve the reported location.
1582 if (!PDecl || PDecl->isInvalidDecl())
1585 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
1586 QualType PropertyTy = PropertyDecl->getType();
1588 // readonly and readwrite/assign/retain/copy conflict.
1589 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1590 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1591 ObjCDeclSpec::DQ_PR_assign |
1592 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1593 ObjCDeclSpec::DQ_PR_copy |
1594 ObjCDeclSpec::DQ_PR_retain |
1595 ObjCDeclSpec::DQ_PR_strong))) {
1596 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1598 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1600 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1601 "unsafe_unretained" :
1602 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1605 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1606 diag::err_objc_property_attr_mutually_exclusive :
1607 diag::warn_objc_property_attr_mutually_exclusive)
1608 << "readonly" << which;
1611 // Check for copy or retain on non-object types.
1612 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1613 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1614 !PropertyTy->isObjCRetainableType() &&
1615 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
1616 Diag(Loc, diag::err_objc_property_requires_object)
1617 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1618 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1619 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1620 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
1623 // Check for more than one of { assign, copy, retain }.
1624 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1625 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1626 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1627 << "assign" << "copy";
1628 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1630 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1631 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1632 << "assign" << "retain";
1633 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1635 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1636 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1637 << "assign" << "strong";
1638 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1640 if (getLangOptions().ObjCAutoRefCount &&
1641 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1642 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1643 << "assign" << "weak";
1644 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1646 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1647 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1648 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1649 << "unsafe_unretained" << "copy";
1650 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1652 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1653 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1654 << "unsafe_unretained" << "retain";
1655 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1657 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1658 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1659 << "unsafe_unretained" << "strong";
1660 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1662 if (getLangOptions().ObjCAutoRefCount &&
1663 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1664 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1665 << "unsafe_unretained" << "weak";
1666 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1668 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1669 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1670 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1671 << "copy" << "retain";
1672 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1674 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1675 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1676 << "copy" << "strong";
1677 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1679 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1680 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1681 << "copy" << "weak";
1682 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1685 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1686 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1687 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1688 << "retain" << "weak";
1689 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1691 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1692 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1693 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1694 << "strong" << "weak";
1695 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1698 // Warn if user supplied no assignment attribute, property is
1699 // readwrite, and this is an object type.
1700 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1701 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1702 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1703 ObjCDeclSpec::DQ_PR_weak)) &&
1704 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1705 PropertyTy->isObjCObjectPointerType()) {
1706 // Skip this warning in gc-only mode.
1707 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1708 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1710 // If non-gc code warn that this is likely inappropriate.
1711 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1712 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1714 // FIXME: Implement warning dependent on NSCopying being
1715 // implemented. See also:
1716 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1717 // (please trim this list while you are at it).
1720 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1721 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
1722 && getLangOptions().getGCMode() == LangOptions::GCOnly
1723 && PropertyTy->isBlockPointerType())
1724 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);