blob: fdd295ceff7c79593ce70941fee454b4060a953d [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
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
Fariborz Jahanian17cb3262010-05-05 21:52:17 +000018#include "clang/AST/ExprObjC.h"
Fariborz Jahanian57e264e2011-10-06 18:38:18 +000019#include "clang/AST/ExprCXX.h"
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +000020#include "clang/AST/ASTMutationListener.h"
John McCall50df6ae2010-08-25 07:03:20 +000021#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000023
24using namespace clang;
25
Ted Kremenek28685ab2010-03-12 00:46:40 +000026//===----------------------------------------------------------------------===//
27// Grammar actions.
28//===----------------------------------------------------------------------===//
29
John McCall265941b2011-09-13 18:31:23 +000030/// getImpliedARCOwnership - Given a set of property attributes and a
31/// type, infer an expected lifetime. The type's ownership qualification
32/// is not considered.
33///
34/// Returns OCL_None if the attributes as stated do not imply an ownership.
35/// Never returns OCL_Autoreleasing.
36static Qualifiers::ObjCLifetime getImpliedARCOwnership(
37 ObjCPropertyDecl::PropertyAttributeKind attrs,
38 QualType type) {
39 // retain, strong, copy, weak, and unsafe_unretained are only legal
40 // on properties of retainable pointer type.
41 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
42 ObjCPropertyDecl::OBJC_PR_strong |
43 ObjCPropertyDecl::OBJC_PR_copy)) {
Fariborz Jahanian5fa065b2011-10-13 23:45:45 +000044 return type->getObjCARCImplicitLifetime();
John McCall265941b2011-09-13 18:31:23 +000045 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
46 return Qualifiers::OCL_Weak;
47 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
48 return Qualifiers::OCL_ExplicitNone;
49 }
50
51 // assign can appear on other types, so we have to check the
52 // property type.
53 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
54 type->isObjCRetainableType()) {
55 return Qualifiers::OCL_ExplicitNone;
56 }
57
58 return Qualifiers::OCL_None;
59}
60
John McCallf85e1932011-06-15 23:02:42 +000061/// Check the internal consistency of a property declaration.
62static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
63 if (property->isInvalidDecl()) return;
64
65 ObjCPropertyDecl::PropertyAttributeKind propertyKind
66 = property->getPropertyAttributes();
67 Qualifiers::ObjCLifetime propertyLifetime
68 = property->getType().getObjCLifetime();
69
70 // Nothing to do if we don't have a lifetime.
71 if (propertyLifetime == Qualifiers::OCL_None) return;
72
John McCall265941b2011-09-13 18:31:23 +000073 Qualifiers::ObjCLifetime expectedLifetime
74 = getImpliedARCOwnership(propertyKind, property->getType());
75 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000076 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000077 // attribute. That's okay, but restore reasonable invariants by
78 // setting the property attribute according to the lifetime
79 // qualifier.
80 ObjCPropertyDecl::PropertyAttributeKind attr;
81 if (propertyLifetime == Qualifiers::OCL_Strong) {
82 attr = ObjCPropertyDecl::OBJC_PR_strong;
83 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
84 attr = ObjCPropertyDecl::OBJC_PR_weak;
85 } else {
86 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
87 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
88 }
89 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000090 return;
91 }
92
93 if (propertyLifetime == expectedLifetime) return;
94
95 property->setInvalidDecl();
96 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000097 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +000098 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +000099 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +0000100 << propertyLifetime;
101}
102
John McCalld226f652010-08-21 09:40:31 +0000103Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
104 FieldDeclarator &FD,
105 ObjCDeclSpec &ODS,
106 Selector GetterSel,
107 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000108 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000109 tok::ObjCKeywordKind MethodImplKind,
110 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000111 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000112 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
113 QualType T = TSI->getType();
Douglas Gregore289d812011-09-13 17:21:33 +0000114 if ((getLangOptions().getGC() != LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +0000115 T.isObjCGCWeak()) ||
116 (getLangOptions().ObjCAutoRefCount &&
117 T.getObjCLifetime() == Qualifiers::OCL_Weak))
118 Attributes |= ObjCDeclSpec::DQ_PR_weak;
119
Ted Kremenek28685ab2010-03-12 00:46:40 +0000120 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
121 // default is readwrite!
122 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
123 // property is defaulted to 'assign' if it is readwrite and is
124 // not retain or copy
125 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
126 (isReadWrite &&
127 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +0000128 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
129 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
130 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
131 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000132
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000133 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000134 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000135
Ted Kremenek28685ab2010-03-12 00:46:40 +0000136 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000137 if (CDecl->IsClassExtension()) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000138 Decl *Res = HandlePropertyInClassExtension(S, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000139 FD, GetterSel, SetterSel,
140 isAssign, isReadWrite,
141 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000142 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000143 isOverridingProperty, TSI,
144 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000145 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000146 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000147 if (getLangOptions().ObjCAutoRefCount)
148 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
149 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000150 return Res;
151 }
152
John McCallf85e1932011-06-15 23:02:42 +0000153 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
154 GetterSel, SetterSel,
155 isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000156 Attributes,
157 ODS.getPropertyAttributes(),
158 TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000159 if (lexicalDC)
160 Res->setLexicalDeclContext(lexicalDC);
161
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000162 // Validate the attributes on the @property.
163 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000164
165 if (getLangOptions().ObjCAutoRefCount)
166 checkARCPropertyDecl(*this, Res);
167
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000168 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000169}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000170
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000171static ObjCPropertyDecl::PropertyAttributeKind
172makePropertyAttributesAsWritten(unsigned Attributes) {
173 unsigned attributesAsWritten = 0;
174 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
175 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
176 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
177 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
178 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
179 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
180 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
181 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
182 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
183 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
184 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
185 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
186 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
187 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
188 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
189 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
190 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
191 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
192 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
193 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
194 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
195 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
196 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
197 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
198
199 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
200}
201
John McCalld226f652010-08-21 09:40:31 +0000202Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000203Sema::HandlePropertyInClassExtension(Scope *S,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000204 SourceLocation AtLoc, FieldDeclarator &FD,
205 Selector GetterSel, Selector SetterSel,
206 const bool isAssign,
207 const bool isReadWrite,
208 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000209 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000210 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000211 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000212 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000213 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000214 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000215 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000216 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000217 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
218
219 if (CCPrimary)
220 // Check for duplicate declaration of this property in current and
221 // other class extensions.
222 for (const ObjCCategoryDecl *ClsExtDecl =
223 CCPrimary->getFirstClassExtension();
224 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
225 if (ObjCPropertyDecl *prevDecl =
226 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
227 Diag(AtLoc, diag::err_duplicate_property);
228 Diag(prevDecl->getLocation(), diag::note_property_declare);
229 return 0;
230 }
231 }
232
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000233 // Create a new ObjCPropertyDecl with the DeclContext being
234 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000235 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000236 ObjCPropertyDecl *PDecl =
237 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
238 PropertyId, AtLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000239 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000240 makePropertyAttributesAsWritten(AttributesAsWritten));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000241 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
242 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
243 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
244 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000245 // Set setter/getter selector name. Needed later.
246 PDecl->setGetterName(GetterSel);
247 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000248 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000249 DC->addDecl(PDecl);
250
251 // We need to look in the @interface to see if the @property was
252 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000253 if (!CCPrimary) {
254 Diag(CDecl->getLocation(), diag::err_continuation_class);
255 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000256 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000257 }
258
259 // Find the property in continuation class's primary class only.
260 ObjCPropertyDecl *PIDecl =
261 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
262
263 if (!PIDecl) {
264 // No matching property found in the primary class. Just fall thru
265 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000266 ObjCPropertyDecl *PrimaryPDecl =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000267 CreatePropertyDecl(S, CCPrimary, AtLoc,
268 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000269 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000270
271 // A case of continuation class adding a new property in the class. This
272 // is not what it was meant for. However, gcc supports it and so should we.
273 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000274 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000275 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000276 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
277 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000278 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000279 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
280 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000281 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000282 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
283 bool IncompatibleObjC = false;
284 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000285 // Relax the strict type matching for property type in continuation class.
286 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000287 // as it narrows the object type in its primary class property. Note that
288 // this conversion is safe only because the wider type is for a 'readonly'
289 // property in primary class and 'narrowed' type for a 'readwrite' property
290 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000291 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
292 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
293 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
294 ConvertedType, IncompatibleObjC))
295 || IncompatibleObjC) {
296 Diag(AtLoc,
297 diag::err_type_mismatch_continuation_class) << PDecl->getType();
298 Diag(PIDecl->getLocation(), diag::note_property_declare);
299 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000300 }
301
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000302 // The property 'PIDecl's readonly attribute will be over-ridden
303 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000304 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000305 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
306 unsigned retainCopyNonatomic =
307 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000308 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000309 ObjCPropertyDecl::OBJC_PR_copy |
310 ObjCPropertyDecl::OBJC_PR_nonatomic);
311 if ((Attributes & retainCopyNonatomic) !=
312 (PIkind & retainCopyNonatomic)) {
313 Diag(AtLoc, diag::warn_property_attr_mismatch);
314 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000315 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000316 DeclContext *DC = cast<DeclContext>(CCPrimary);
317 if (!ObjCPropertyDecl::findPropertyDecl(DC,
318 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000319 // Protocol is not in the primary class. Must build one for it.
320 ObjCDeclSpec ProtocolPropertyODS;
321 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
322 // and ObjCPropertyDecl::PropertyAttributeKind have identical
323 // values. Should consolidate both into one enum type.
324 ProtocolPropertyODS.
325 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
326 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000327 // Must re-establish the context from class extension to primary
328 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000329 ContextRAII SavedContext(*this, CCPrimary);
330
John McCalld226f652010-08-21 09:40:31 +0000331 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000332 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
333 PIDecl->getGetterName(),
334 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000335 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000336 MethodImplKind,
337 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000338 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000339 }
340 PIDecl->makeitReadWriteAttribute();
341 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
342 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000343 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
344 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000345 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
346 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
347 PIDecl->setSetterName(SetterSel);
348 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000349 // Tailor the diagnostics for the common case where a readwrite
350 // property is declared both in the @interface and the continuation.
351 // This is a common error where the user often intended the original
352 // declaration to be readonly.
353 unsigned diag =
354 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
355 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
356 ? diag::err_use_continuation_class_redeclaration_readwrite
357 : diag::err_use_continuation_class;
358 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000359 << CCPrimary->getDeclName();
360 Diag(PIDecl->getLocation(), diag::note_property_declare);
361 }
362 *isOverridingProperty = true;
363 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000364 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000365 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
366 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000367 if (ASTMutationListener *L = Context.getASTMutationListener())
368 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000369 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000370}
371
372ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
373 ObjCContainerDecl *CDecl,
374 SourceLocation AtLoc,
375 FieldDeclarator &FD,
376 Selector GetterSel,
377 Selector SetterSel,
378 const bool isAssign,
379 const bool isReadWrite,
380 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000381 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000382 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000383 tok::ObjCKeywordKind MethodImplKind,
384 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000385 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000386 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000387
388 // Issue a warning if property is 'assign' as default and its object, which is
389 // gc'able conforms to NSCopying protocol
Douglas Gregore289d812011-09-13 17:21:33 +0000390 if (getLangOptions().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000391 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000392 if (const ObjCObjectPointerType *ObjPtrTy =
393 T->getAs<ObjCObjectPointerType>()) {
394 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
395 if (IDecl)
396 if (ObjCProtocolDecl* PNSCopying =
397 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
398 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
399 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000400 }
John McCallc12c5bb2010-05-15 11:32:37 +0000401 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000402 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
403
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000404 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000405 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
406 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000407 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000408
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000409 if (ObjCPropertyDecl *prevDecl =
410 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000411 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000412 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000413 PDecl->setInvalidDecl();
414 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000415 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000416 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000417 if (lexicalDC)
418 PDecl->setLexicalDeclContext(lexicalDC);
419 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000420
421 if (T->isArrayType() || T->isFunctionType()) {
422 Diag(AtLoc, diag::err_property_type) << T;
423 PDecl->setInvalidDecl();
424 }
425
426 ProcessDeclAttributes(S, PDecl, FD.D);
427
428 // Regardless of setter/getter attribute, we save the default getter/setter
429 // selector names in anticipation of declaration of setter/getter methods.
430 PDecl->setGetterName(GetterSel);
431 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000432 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000433 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000434
Ted Kremenek28685ab2010-03-12 00:46:40 +0000435 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
436 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
437
438 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
439 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
440
441 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
442 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
443
444 if (isReadWrite)
445 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
446
447 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
448 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
449
John McCallf85e1932011-06-15 23:02:42 +0000450 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
451 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
452
453 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
454 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
455
Ted Kremenek28685ab2010-03-12 00:46:40 +0000456 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
457 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
458
John McCallf85e1932011-06-15 23:02:42 +0000459 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
460 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
461
Ted Kremenek28685ab2010-03-12 00:46:40 +0000462 if (isAssign)
463 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
464
John McCall265941b2011-09-13 18:31:23 +0000465 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000466 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
467 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000468 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000469 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000470
John McCallf85e1932011-06-15 23:02:42 +0000471 // 'unsafe_unretained' is alias for 'assign'.
472 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
473 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
474 if (isAssign)
475 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
476
Ted Kremenek28685ab2010-03-12 00:46:40 +0000477 if (MethodImplKind == tok::objc_required)
478 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
479 else if (MethodImplKind == tok::objc_optional)
480 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000481
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000482 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000483}
484
John McCallf85e1932011-06-15 23:02:42 +0000485static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
486 ObjCPropertyDecl *property,
487 ObjCIvarDecl *ivar) {
488 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
489
John McCallf85e1932011-06-15 23:02:42 +0000490 QualType ivarType = ivar->getType();
491 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000492
John McCall265941b2011-09-13 18:31:23 +0000493 // The lifetime implied by the property's attributes.
494 Qualifiers::ObjCLifetime propertyLifetime =
495 getImpliedARCOwnership(property->getPropertyAttributes(),
496 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000497
John McCall265941b2011-09-13 18:31:23 +0000498 // We're fine if they match.
499 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000500
John McCall265941b2011-09-13 18:31:23 +0000501 // These aren't valid lifetimes for object ivars; don't diagnose twice.
502 if (ivarLifetime == Qualifiers::OCL_None ||
503 ivarLifetime == Qualifiers::OCL_Autoreleasing)
504 return;
John McCallf85e1932011-06-15 23:02:42 +0000505
John McCall265941b2011-09-13 18:31:23 +0000506 switch (propertyLifetime) {
507 case Qualifiers::OCL_Strong:
508 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
509 << property->getDeclName()
510 << ivar->getDeclName()
511 << ivarLifetime;
512 break;
John McCallf85e1932011-06-15 23:02:42 +0000513
John McCall265941b2011-09-13 18:31:23 +0000514 case Qualifiers::OCL_Weak:
515 S.Diag(propertyImplLoc, diag::error_weak_property)
516 << property->getDeclName()
517 << ivar->getDeclName();
518 break;
John McCallf85e1932011-06-15 23:02:42 +0000519
John McCall265941b2011-09-13 18:31:23 +0000520 case Qualifiers::OCL_ExplicitNone:
521 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
522 << property->getDeclName()
523 << ivar->getDeclName()
524 << ((property->getPropertyAttributesAsWritten()
525 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
526 break;
John McCallf85e1932011-06-15 23:02:42 +0000527
John McCall265941b2011-09-13 18:31:23 +0000528 case Qualifiers::OCL_Autoreleasing:
529 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000530
John McCall265941b2011-09-13 18:31:23 +0000531 case Qualifiers::OCL_None:
532 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000533 return;
534 }
535
536 S.Diag(property->getLocation(), diag::note_property_declare);
537}
538
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000539/// setImpliedPropertyAttributeForReadOnlyProperty -
540/// This routine evaludates life-time attributes for a 'readonly'
541/// property with no known lifetime of its own, using backing
542/// 'ivar's attribute, if any. If no backing 'ivar', property's
543/// life-time is assumed 'strong'.
544static void setImpliedPropertyAttributeForReadOnlyProperty(
545 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
546 Qualifiers::ObjCLifetime propertyLifetime =
547 getImpliedARCOwnership(property->getPropertyAttributes(),
548 property->getType());
549 if (propertyLifetime != Qualifiers::OCL_None)
550 return;
551
552 if (!ivar) {
553 // if no backing ivar, make property 'strong'.
554 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
555 return;
556 }
557 // property assumes owenership of backing ivar.
558 QualType ivarType = ivar->getType();
559 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
560 if (ivarLifetime == Qualifiers::OCL_Strong)
561 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
562 else if (ivarLifetime == Qualifiers::OCL_Weak)
563 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
564 return;
565}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000566
567/// ActOnPropertyImplDecl - This routine performs semantic checks and
568/// builds the AST node for a property implementation declaration; declared
569/// as @synthesize or @dynamic.
570///
John McCalld226f652010-08-21 09:40:31 +0000571Decl *Sema::ActOnPropertyImplDecl(Scope *S,
572 SourceLocation AtLoc,
573 SourceLocation PropertyLoc,
574 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000575 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000576 IdentifierInfo *PropertyIvar,
577 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000578 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000579 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000580 // Make sure we have a context for the property implementation declaration.
581 if (!ClassImpDecl) {
582 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000583 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000584 }
585 ObjCPropertyDecl *property = 0;
586 ObjCInterfaceDecl* IDecl = 0;
587 // Find the class or category class where this property must have
588 // a declaration.
589 ObjCImplementationDecl *IC = 0;
590 ObjCCategoryImplDecl* CatImplClass = 0;
591 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
592 IDecl = IC->getClassInterface();
593 // We always synthesize an interface for an implementation
594 // without an interface decl. So, IDecl is always non-zero.
595 assert(IDecl &&
596 "ActOnPropertyImplDecl - @implementation without @interface");
597
598 // Look for this property declaration in the @implementation's @interface
599 property = IDecl->FindPropertyDeclaration(PropertyId);
600 if (!property) {
601 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000602 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000603 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000604 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000605 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
606 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000607 if (AtLoc.isValid())
608 Diag(AtLoc, diag::warn_implicit_atomic_property);
609 else
610 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
611 Diag(property->getLocation(), diag::note_property_declare);
612 }
613
Ted Kremenek28685ab2010-03-12 00:46:40 +0000614 if (const ObjCCategoryDecl *CD =
615 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
616 if (!CD->IsClassExtension()) {
617 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
618 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000619 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000620 }
621 }
622 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
623 if (Synthesize) {
624 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000625 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000626 }
627 IDecl = CatImplClass->getClassInterface();
628 if (!IDecl) {
629 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000630 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000631 }
632 ObjCCategoryDecl *Category =
633 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
634
635 // If category for this implementation not found, it is an error which
636 // has already been reported eralier.
637 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000638 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000639 // Look for this property declaration in @implementation's category
640 property = Category->FindPropertyDeclaration(PropertyId);
641 if (!property) {
642 Diag(PropertyLoc, diag::error_bad_category_property_decl)
643 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000644 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000645 }
646 } else {
647 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000648 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000649 }
650 ObjCIvarDecl *Ivar = 0;
651 // Check that we have a valid, previously declared ivar for @synthesize
652 if (Synthesize) {
653 // @synthesize
654 if (!PropertyIvar)
655 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000656 // Check that this is a previously declared 'ivar' in 'IDecl' interface
657 ObjCInterfaceDecl *ClassDeclared;
658 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
659 QualType PropType = property->getType();
660 QualType PropertyIvarType = PropType.getNonReferenceType();
661
662 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000663 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000664 ObjCPropertyDecl::OBJC_PR_readonly) &&
665 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000666 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
667 }
668
John McCallf85e1932011-06-15 23:02:42 +0000669 ObjCPropertyDecl::PropertyAttributeKind kind
670 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000671
672 // Add GC __weak to the ivar type if the property is weak.
673 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
Douglas Gregore289d812011-09-13 17:21:33 +0000674 getLangOptions().getGC() != LangOptions::NonGC) {
John McCall265941b2011-09-13 18:31:23 +0000675 assert(!getLangOptions().ObjCAutoRefCount);
676 if (PropertyIvarType.isObjCGCStrong()) {
677 Diag(PropertyLoc, diag::err_gc_weak_property_strong_type);
678 Diag(property->getLocation(), diag::note_property_declare);
679 } else {
680 PropertyIvarType =
681 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000682 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000683 }
John McCall265941b2011-09-13 18:31:23 +0000684
Ted Kremenek28685ab2010-03-12 00:46:40 +0000685 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000686 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000687 // property attributes.
688 if (getLangOptions().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000689 !PropertyIvarType.getObjCLifetime() &&
690 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000691
John McCall265941b2011-09-13 18:31:23 +0000692 // It's an error if we have to do this and the user didn't
693 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000694 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000695 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000696 Diag(PropertyLoc,
697 diag::err_arc_objc_property_default_assign_on_object);
698 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000699 } else {
700 Qualifiers::ObjCLifetime lifetime =
701 getImpliedARCOwnership(kind, PropertyIvarType);
702 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000703 if (lifetime == Qualifiers::OCL_Weak) {
704 bool err = false;
705 if (const ObjCObjectPointerType *ObjT =
706 PropertyIvarType->getAs<ObjCObjectPointerType>())
707 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
708 Diag(PropertyLoc, diag::err_arc_weak_unavailable_property);
709 Diag(property->getLocation(), diag::note_property_declare);
710 err = true;
711 }
712 if (!err && !getLangOptions().ObjCRuntimeHasWeak) {
713 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
714 Diag(property->getLocation(), diag::note_property_declare);
715 }
John McCallf85e1932011-06-15 23:02:42 +0000716 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000717
John McCallf85e1932011-06-15 23:02:42 +0000718 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000719 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000720 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
721 }
John McCallf85e1932011-06-15 23:02:42 +0000722 }
723
724 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
725 !getLangOptions().ObjCAutoRefCount &&
Douglas Gregore289d812011-09-13 17:21:33 +0000726 getLangOptions().getGC() == LangOptions::NonGC) {
John McCallf85e1932011-06-15 23:02:42 +0000727 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
728 Diag(property->getLocation(), diag::note_property_declare);
729 }
730
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000731 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
732 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000733 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000734 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000735 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000736 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000737 IDecl->makeDeclVisibleInContext(Ivar, false);
738 property->setPropertyIvarDecl(Ivar);
739
740 if (!getLangOptions().ObjCNonFragileABI)
741 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
742 // Note! I deliberately want it to fall thru so, we have a
743 // a property implementation and to avoid future warnings.
744 } else if (getLangOptions().ObjCNonFragileABI &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000745 !declaresSameEntity(ClassDeclared, IDecl)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000746 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
747 << property->getDeclName() << Ivar->getDeclName()
748 << ClassDeclared->getDeclName();
749 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000750 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000751 // Note! I deliberately want it to fall thru so more errors are caught.
752 }
753 QualType IvarType = Context.getCanonicalType(Ivar->getType());
754
755 // Check that type of property and its ivar are type compatible.
John McCall265941b2011-09-13 18:31:23 +0000756 if (Context.getCanonicalType(PropertyIvarType) != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000757 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000758 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000759 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000760 compat =
761 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000762 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000763 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000764 else {
765 SourceLocation Loc = PropertyIvarLoc;
766 if (Loc.isInvalid())
767 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000768 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000769 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000770 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000771 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000772 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000773 << property->getDeclName() << PropType
774 << Ivar->getDeclName() << IvarType;
775 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000776 // Note! I deliberately want it to fall thru so, we have a
777 // a property implementation and to avoid future warnings.
778 }
779
780 // FIXME! Rules for properties are somewhat different that those
781 // for assignments. Use a new routine to consolidate all cases;
782 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000783 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000784 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
785 if (lhsType != rhsType &&
786 lhsType->isArithmeticType()) {
787 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000788 << property->getDeclName() << PropType
789 << Ivar->getDeclName() << IvarType;
790 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000791 // Fall thru - see previous comment
792 }
793 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000794 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000795 getLangOptions().getGC() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000796 Diag(PropertyLoc, diag::error_weak_property)
797 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000798 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000799 // Fall thru - see previous comment
800 }
John McCallf85e1932011-06-15 23:02:42 +0000801 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000802 if ((property->getType()->isObjCObjectPointerType() ||
803 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000804 getLangOptions().getGC() != LangOptions::NonGC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000805 Diag(PropertyLoc, diag::error_strong_property)
806 << property->getDeclName() << Ivar->getDeclName();
807 // Fall thru - see previous comment
808 }
809 }
John McCallf85e1932011-06-15 23:02:42 +0000810 if (getLangOptions().ObjCAutoRefCount)
811 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000812 } else if (PropertyIvar)
813 // @dynamic
814 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000815
Ted Kremenek28685ab2010-03-12 00:46:40 +0000816 assert (property && "ActOnPropertyImplDecl - property declaration missing");
817 ObjCPropertyImplDecl *PIDecl =
818 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
819 property,
820 (Synthesize ?
821 ObjCPropertyImplDecl::Synthesize
822 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000823 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000824 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
825 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000826 if (getLangOptions().CPlusPlus && Synthesize &&
827 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000828 // For Objective-C++, need to synthesize the AST for the IVAR object to be
829 // returned by the getter as it must conform to C++'s copy-return rules.
830 // FIXME. Eventually we want to do this for Objective-C as well.
831 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
832 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000833 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
834 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000835 Expr *IvarRefExpr =
836 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
837 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000838 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000839 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000840 SourceLocation(),
841 getterMethod->getResultType(),
842 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000843 SourceLocation(),
844 Owned(IvarRefExpr));
845 if (!Res.isInvalid()) {
846 Expr *ResExpr = Res.takeAs<Expr>();
847 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000848 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000849 PIDecl->setGetterCXXConstructor(ResExpr);
850 }
851 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000852 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
853 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
854 Diag(getterMethod->getLocation(),
855 diag::warn_property_getter_owning_mismatch);
856 Diag(property->getLocation(), diag::note_property_declare);
857 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000858 }
859 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
860 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000861 if (getLangOptions().CPlusPlus && Synthesize
862 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000863 // FIXME. Eventually we want to do this for Objective-C as well.
864 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
865 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000866 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
867 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000868 Expr *lhs =
869 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
870 SelfExpr, true, true);
871 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
872 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +0000873 QualType T = Param->getType().getNonReferenceType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000874 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000875 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000876 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000877 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000878 if (property->getPropertyAttributes() &
879 ObjCPropertyDecl::OBJC_PR_atomic) {
880 Expr *callExpr = Res.takeAs<Expr>();
881 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000882 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
883 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000884 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000885 if (property->getType()->isReferenceType()) {
886 Diag(PropertyLoc,
887 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000888 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000889 Diag(FuncDecl->getLocStart(),
890 diag::note_callee_decl) << FuncDecl;
891 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000892 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000893 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
894 }
895 }
896
Ted Kremenek28685ab2010-03-12 00:46:40 +0000897 if (IC) {
898 if (Synthesize)
899 if (ObjCPropertyImplDecl *PPIDecl =
900 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
901 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
902 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
903 << PropertyIvar;
904 Diag(PPIDecl->getLocation(), diag::note_previous_use);
905 }
906
907 if (ObjCPropertyImplDecl *PPIDecl
908 = IC->FindPropertyImplDecl(PropertyId)) {
909 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
910 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000911 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000912 }
913 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000914 if (getLangOptions().ObjCDefaultSynthProperties &&
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +0000915 getLangOptions().ObjCNonFragileABI2 &&
Ted Kremenek71207fc2012-01-05 22:47:47 +0000916 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000917 // Diagnose if an ivar was lazily synthesdized due to a previous
918 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000919 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000920 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000921 ObjCIvarDecl *Ivar = 0;
922 if (!Synthesize)
923 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
924 else {
925 if (PropertyIvar && PropertyIvar != PropertyId)
926 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
927 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000928 // Issue diagnostics only if Ivar belongs to current class.
929 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000930 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000931 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
932 << PropertyId;
933 Ivar->setInvalidDecl();
934 }
935 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000936 } else {
937 if (Synthesize)
938 if (ObjCPropertyImplDecl *PPIDecl =
939 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
940 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
941 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
942 << PropertyIvar;
943 Diag(PPIDecl->getLocation(), diag::note_previous_use);
944 }
945
946 if (ObjCPropertyImplDecl *PPIDecl =
947 CatImplClass->FindPropertyImplDecl(PropertyId)) {
948 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
949 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000950 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000951 }
952 CatImplClass->addPropertyImplementation(PIDecl);
953 }
954
John McCalld226f652010-08-21 09:40:31 +0000955 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000956}
957
958//===----------------------------------------------------------------------===//
959// Helper methods.
960//===----------------------------------------------------------------------===//
961
Ted Kremenek9d64c152010-03-12 00:38:38 +0000962/// DiagnosePropertyMismatch - Compares two properties for their
963/// attributes and types and warns on a variety of inconsistencies.
964///
965void
966Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
967 ObjCPropertyDecl *SuperProperty,
968 const IdentifierInfo *inheritedName) {
969 ObjCPropertyDecl::PropertyAttributeKind CAttr =
970 Property->getPropertyAttributes();
971 ObjCPropertyDecl::PropertyAttributeKind SAttr =
972 SuperProperty->getPropertyAttributes();
973 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
974 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
975 Diag(Property->getLocation(), diag::warn_readonly_property)
976 << Property->getDeclName() << inheritedName;
977 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
978 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
979 Diag(Property->getLocation(), diag::warn_property_attribute)
980 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +0000981 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +0000982 unsigned CAttrRetain =
983 (CAttr &
984 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
985 unsigned SAttrRetain =
986 (SAttr &
987 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
988 bool CStrong = (CAttrRetain != 0);
989 bool SStrong = (SAttrRetain != 0);
990 if (CStrong != SStrong)
991 Diag(Property->getLocation(), diag::warn_property_attribute)
992 << Property->getDeclName() << "retain (or strong)" << inheritedName;
993 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000994
995 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
996 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
997 Diag(Property->getLocation(), diag::warn_property_attribute)
998 << Property->getDeclName() << "atomic" << inheritedName;
999 if (Property->getSetterName() != SuperProperty->getSetterName())
1000 Diag(Property->getLocation(), diag::warn_property_attribute)
1001 << Property->getDeclName() << "setter" << inheritedName;
1002 if (Property->getGetterName() != SuperProperty->getGetterName())
1003 Diag(Property->getLocation(), diag::warn_property_attribute)
1004 << Property->getDeclName() << "getter" << inheritedName;
1005
1006 QualType LHSType =
1007 Context.getCanonicalType(SuperProperty->getType());
1008 QualType RHSType =
1009 Context.getCanonicalType(Property->getType());
1010
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001011 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001012 // Do cases not handled in above.
1013 // FIXME. For future support of covariant property types, revisit this.
1014 bool IncompatibleObjC = false;
1015 QualType ConvertedType;
1016 if (!isObjCPointerConversion(RHSType, LHSType,
1017 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001018 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001019 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1020 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001021 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1022 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001023 }
1024}
1025
1026bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1027 ObjCMethodDecl *GetterMethod,
1028 SourceLocation Loc) {
1029 if (GetterMethod &&
John McCall3c3b7f92011-10-25 17:37:35 +00001030 !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(),
1031 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001032 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +00001033 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +00001034 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +00001035 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001036 if (result != Compatible) {
1037 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1038 << property->getDeclName()
1039 << GetterMethod->getSelector();
1040 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1041 return true;
1042 }
1043 }
1044 return false;
1045}
1046
1047/// ComparePropertiesInBaseAndSuper - This routine compares property
1048/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001049/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001050///
1051void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1052 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1053 if (!SDecl)
1054 return;
1055 // FIXME: O(N^2)
1056 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1057 E = SDecl->prop_end(); S != E; ++S) {
1058 ObjCPropertyDecl *SuperPDecl = (*S);
1059 // Does property in super class has declaration in current class?
1060 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1061 E = IDecl->prop_end(); I != E; ++I) {
1062 ObjCPropertyDecl *PDecl = (*I);
1063 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1064 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1065 SDecl->getIdentifier());
1066 }
1067 }
1068}
1069
1070/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1071/// of properties declared in a protocol and compares their attribute against
1072/// the same property declared in the class or category.
1073void
1074Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1075 ObjCProtocolDecl *PDecl) {
1076 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1077 if (!IDecl) {
1078 // Category
1079 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1080 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1081 if (!CatDecl->IsClassExtension())
1082 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1083 E = PDecl->prop_end(); P != E; ++P) {
1084 ObjCPropertyDecl *Pr = (*P);
1085 ObjCCategoryDecl::prop_iterator CP, CE;
1086 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001087 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001088 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1089 break;
1090 if (CP != CE)
1091 // Property protocol already exist in class. Diagnose any mismatch.
1092 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1093 }
1094 return;
1095 }
1096 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1097 E = PDecl->prop_end(); P != E; ++P) {
1098 ObjCPropertyDecl *Pr = (*P);
1099 ObjCInterfaceDecl::prop_iterator CP, CE;
1100 // Is this property already in class's list of properties?
1101 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1102 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1103 break;
1104 if (CP != CE)
1105 // Property protocol already exist in class. Diagnose any mismatch.
1106 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1107 }
1108}
1109
1110/// CompareProperties - This routine compares properties
1111/// declared in 'ClassOrProtocol' objects (which can be a class or an
1112/// inherited protocol with the list of properties for class/category 'CDecl'
1113///
John McCalld226f652010-08-21 09:40:31 +00001114void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1115 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001116 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1117
1118 if (!IDecl) {
1119 // Category
1120 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1121 assert (CatDecl && "CompareProperties");
1122 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1123 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1124 E = MDecl->protocol_end(); P != E; ++P)
1125 // Match properties of category with those of protocol (*P)
1126 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1127
1128 // Go thru the list of protocols for this category and recursively match
1129 // their properties with those in the category.
1130 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1131 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001132 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001133 } else {
1134 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1135 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1136 E = MD->protocol_end(); P != E; ++P)
1137 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1138 }
1139 return;
1140 }
1141
1142 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001143 for (ObjCInterfaceDecl::all_protocol_iterator
1144 P = MDecl->all_referenced_protocol_begin(),
1145 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001146 // Match properties of class IDecl with those of protocol (*P).
1147 MatchOneProtocolPropertiesInClass(IDecl, *P);
1148
1149 // Go thru the list of protocols for this class and recursively match
1150 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001151 for (ObjCInterfaceDecl::all_protocol_iterator
1152 P = IDecl->all_referenced_protocol_begin(),
1153 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001154 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001155 } else {
1156 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1157 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1158 E = MD->protocol_end(); P != E; ++P)
1159 MatchOneProtocolPropertiesInClass(IDecl, *P);
1160 }
1161}
1162
1163/// isPropertyReadonly - Return true if property is readonly, by searching
1164/// for the property in the class and in its categories and implementations
1165///
1166bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1167 ObjCInterfaceDecl *IDecl) {
1168 // by far the most common case.
1169 if (!PDecl->isReadOnly())
1170 return false;
1171 // Even if property is ready only, if interface has a user defined setter,
1172 // it is not considered read only.
1173 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1174 return false;
1175
1176 // Main class has the property as 'readonly'. Must search
1177 // through the category list to see if the property's
1178 // attribute has been over-ridden to 'readwrite'.
1179 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1180 Category; Category = Category->getNextClassCategory()) {
1181 // Even if property is ready only, if a category has a user defined setter,
1182 // it is not considered read only.
1183 if (Category->getInstanceMethod(PDecl->getSetterName()))
1184 return false;
1185 ObjCPropertyDecl *P =
1186 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1187 if (P && !P->isReadOnly())
1188 return false;
1189 }
1190
1191 // Also, check for definition of a setter method in the implementation if
1192 // all else failed.
1193 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1194 if (ObjCImplementationDecl *IMD =
1195 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1196 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1197 return false;
1198 } else if (ObjCCategoryImplDecl *CIMD =
1199 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1200 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1201 return false;
1202 }
1203 }
1204 // Lastly, look through the implementation (if one is in scope).
1205 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1206 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1207 return false;
1208 // If all fails, look at the super class.
1209 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1210 return isPropertyReadonly(PDecl, SIDecl);
1211 return true;
1212}
1213
1214/// CollectImmediateProperties - This routine collects all properties in
1215/// the class and its conforming protocols; but not those it its super class.
1216void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001217 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1218 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001219 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1220 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1221 E = IDecl->prop_end(); P != E; ++P) {
1222 ObjCPropertyDecl *Prop = (*P);
1223 PropMap[Prop->getIdentifier()] = Prop;
1224 }
1225 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001226 for (ObjCInterfaceDecl::all_protocol_iterator
1227 PI = IDecl->all_referenced_protocol_begin(),
1228 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001229 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001230 }
1231 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1232 if (!CATDecl->IsClassExtension())
1233 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1234 E = CATDecl->prop_end(); P != E; ++P) {
1235 ObjCPropertyDecl *Prop = (*P);
1236 PropMap[Prop->getIdentifier()] = Prop;
1237 }
1238 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001239 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001240 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001241 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001242 }
1243 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1244 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1245 E = PDecl->prop_end(); P != E; ++P) {
1246 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001247 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1248 // Exclude property for protocols which conform to class's super-class,
1249 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001250 if (!PropertyFromSuper ||
1251 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001252 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1253 if (!PropEntry)
1254 PropEntry = Prop;
1255 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001256 }
1257 // scan through protocol's protocols.
1258 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1259 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001260 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001261 }
1262}
1263
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001264/// CollectClassPropertyImplementations - This routine collects list of
1265/// properties to be implemented in the class. This includes, class's
1266/// and its conforming protocols' properties.
1267static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1268 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1269 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1270 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1271 E = IDecl->prop_end(); P != E; ++P) {
1272 ObjCPropertyDecl *Prop = (*P);
1273 PropMap[Prop->getIdentifier()] = Prop;
1274 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001275 for (ObjCInterfaceDecl::all_protocol_iterator
1276 PI = IDecl->all_referenced_protocol_begin(),
1277 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001278 CollectClassPropertyImplementations((*PI), PropMap);
1279 }
1280 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1281 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1282 E = PDecl->prop_end(); P != E; ++P) {
1283 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahanianac371502012-02-23 18:21:25 +00001284 if (!PropMap.count(Prop->getIdentifier()))
1285 PropMap[Prop->getIdentifier()] = Prop;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001286 }
1287 // scan through protocol's protocols.
1288 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1289 E = PDecl->protocol_end(); PI != E; ++PI)
1290 CollectClassPropertyImplementations((*PI), PropMap);
1291 }
1292}
1293
1294/// CollectSuperClassPropertyImplementations - This routine collects list of
1295/// properties to be implemented in super class(s) and also coming from their
1296/// conforming protocols.
1297static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1298 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1299 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1300 while (SDecl) {
1301 CollectClassPropertyImplementations(SDecl, PropMap);
1302 SDecl = SDecl->getSuperClass();
1303 }
1304 }
1305}
1306
Ted Kremenek9d64c152010-03-12 00:38:38 +00001307/// LookupPropertyDecl - Looks up a property in the current class and all
1308/// its protocols.
1309ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1310 IdentifierInfo *II) {
1311 if (const ObjCInterfaceDecl *IDecl =
1312 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1313 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1314 E = IDecl->prop_end(); P != E; ++P) {
1315 ObjCPropertyDecl *Prop = (*P);
1316 if (Prop->getIdentifier() == II)
1317 return Prop;
1318 }
1319 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001320 for (ObjCInterfaceDecl::all_protocol_iterator
1321 PI = IDecl->all_referenced_protocol_begin(),
1322 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001323 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1324 if (Prop)
1325 return Prop;
1326 }
1327 }
1328 else if (const ObjCProtocolDecl *PDecl =
1329 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1330 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1331 E = PDecl->prop_end(); P != E; ++P) {
1332 ObjCPropertyDecl *Prop = (*P);
1333 if (Prop->getIdentifier() == II)
1334 return Prop;
1335 }
1336 // scan through protocol's protocols.
1337 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1338 E = PDecl->protocol_end(); PI != E; ++PI) {
1339 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1340 if (Prop)
1341 return Prop;
1342 }
1343 }
1344 return 0;
1345}
1346
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001347static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1348 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001349 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001350 {
1351 llvm::raw_svector_ostream os(ivarName);
1352 os << '_' << Prop->getIdentifier()->getName();
1353 }
1354 return &Ctx.Idents.get(ivarName.str());
1355}
1356
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001357/// DefaultSynthesizeProperties - This routine default synthesizes all
1358/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001359void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1360 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001361
1362 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1363 CollectClassPropertyImplementations(IDecl, PropMap);
1364 if (PropMap.empty())
1365 return;
1366 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1367 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1368
1369 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1370 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1371 ObjCPropertyDecl *Prop = P->second;
1372 // If property to be implemented in the super class, ignore.
1373 if (SuperPropMap[Prop->getIdentifier()])
1374 continue;
1375 // Is there a matching propery synthesize/dynamic?
1376 if (Prop->isInvalidDecl() ||
1377 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1378 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1379 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001380 // Property may have been synthesized by user.
1381 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1382 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001383 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1384 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1385 continue;
1386 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1387 continue;
1388 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001389 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1390 // We won't auto-synthesize properties declared in protocols.
1391 Diag(IMPDecl->getLocation(),
1392 diag::warn_auto_synthesizing_protocol_property);
1393 Diag(Prop->getLocation(), diag::note_property_declare);
1394 continue;
1395 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001396
1397 // We use invalid SourceLocations for the synthesized ivars since they
1398 // aren't really synthesized at a particular location; they just exist.
1399 // Saying that they are located at the @implementation isn't really going
1400 // to help users.
1401 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001402 true,
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001403 /* property = */ Prop->getIdentifier(),
1404 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001405 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001406 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001407}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001408
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001409void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1410 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1411 return;
1412 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1413 if (!IC)
1414 return;
1415 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001416 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001417 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001418}
1419
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001420void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001421 ObjCContainerDecl *CDecl,
1422 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001423 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1424 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1425 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1426
Ted Kremenek9d64c152010-03-12 00:38:38 +00001427 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001428 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001429 if (PropMap.empty())
1430 return;
1431
1432 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1433 for (ObjCImplDecl::propimpl_iterator
1434 I = IMPDecl->propimpl_begin(),
1435 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1436 PropImplMap.insert((*I)->getPropertyDecl());
1437
1438 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1439 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1440 ObjCPropertyDecl *Prop = P->second;
1441 // Is there a matching propery synthesize/dynamic?
1442 if (Prop->isInvalidDecl() ||
1443 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001444 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001445 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001446 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001447 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001448 isa<ObjCCategoryDecl>(CDecl) ?
1449 diag::warn_setter_getter_impl_required_in_category :
1450 diag::warn_setter_getter_impl_required)
1451 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001452 Diag(Prop->getLocation(),
1453 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001454 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1455 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001456 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001457 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1458
Ted Kremenek9d64c152010-03-12 00:38:38 +00001459 }
1460
1461 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001462 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001463 isa<ObjCCategoryDecl>(CDecl) ?
1464 diag::warn_setter_getter_impl_required_in_category :
1465 diag::warn_setter_getter_impl_required)
1466 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001467 Diag(Prop->getLocation(),
1468 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001469 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1470 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001471 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001472 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001473 }
1474 }
1475}
1476
1477void
1478Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1479 ObjCContainerDecl* IDecl) {
1480 // Rules apply in non-GC mode only
Douglas Gregore289d812011-09-13 17:21:33 +00001481 if (getLangOptions().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001482 return;
1483 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1484 E = IDecl->prop_end();
1485 I != E; ++I) {
1486 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001487 ObjCMethodDecl *GetterMethod = 0;
1488 ObjCMethodDecl *SetterMethod = 0;
1489 bool LookedUpGetterSetter = false;
1490
Ted Kremenek9d64c152010-03-12 00:38:38 +00001491 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001492 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001493
John McCall265941b2011-09-13 18:31:23 +00001494 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1495 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001496 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1497 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1498 LookedUpGetterSetter = true;
1499 if (GetterMethod) {
1500 Diag(GetterMethod->getLocation(),
1501 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001502 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001503 Diag(Property->getLocation(), diag::note_property_declare);
1504 }
1505 if (SetterMethod) {
1506 Diag(SetterMethod->getLocation(),
1507 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001508 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001509 Diag(Property->getLocation(), diag::note_property_declare);
1510 }
1511 }
1512
Ted Kremenek9d64c152010-03-12 00:38:38 +00001513 // We only care about readwrite atomic property.
1514 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1515 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1516 continue;
1517 if (const ObjCPropertyImplDecl *PIDecl
1518 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1519 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1520 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001521 if (!LookedUpGetterSetter) {
1522 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1523 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1524 LookedUpGetterSetter = true;
1525 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001526 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1527 SourceLocation MethodLoc =
1528 (GetterMethod ? GetterMethod->getLocation()
1529 : SetterMethod->getLocation());
1530 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001531 << Property->getIdentifier() << (GetterMethod != 0)
1532 << (SetterMethod != 0);
1533 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001534 Diag(Property->getLocation(), diag::note_property_declare);
1535 }
1536 }
1537 }
1538}
1539
John McCallf85e1932011-06-15 23:02:42 +00001540void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
Douglas Gregore289d812011-09-13 17:21:33 +00001541 if (getLangOptions().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001542 return;
1543
1544 for (ObjCImplementationDecl::propimpl_iterator
1545 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1546 ObjCPropertyImplDecl *PID = *i;
1547 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1548 continue;
1549
1550 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001551 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1552 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001553 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1554 if (!method)
1555 continue;
1556 ObjCMethodFamily family = method->getMethodFamily();
1557 if (family == OMF_alloc || family == OMF_copy ||
1558 family == OMF_mutableCopy || family == OMF_new) {
1559 if (getLangOptions().ObjCAutoRefCount)
1560 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1561 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001562 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001563 Diag(PD->getLocation(), diag::note_property_declare);
1564 }
1565 }
1566 }
1567}
1568
John McCall5de74d12010-11-10 07:01:40 +00001569/// AddPropertyAttrs - Propagates attributes from a property to the
1570/// implicitly-declared getter or setter for that property.
1571static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1572 ObjCPropertyDecl *Property) {
1573 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001574 for (Decl::attr_iterator A = Property->attr_begin(),
1575 AEnd = Property->attr_end();
1576 A != AEnd; ++A) {
1577 if (isa<DeprecatedAttr>(*A) ||
1578 isa<UnavailableAttr>(*A) ||
1579 isa<AvailabilityAttr>(*A))
1580 PropertyMethod->addAttr((*A)->clone(S.Context));
1581 }
John McCall5de74d12010-11-10 07:01:40 +00001582}
1583
Ted Kremenek9d64c152010-03-12 00:38:38 +00001584/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1585/// have the property type and issue diagnostics if they don't.
1586/// Also synthesize a getter/setter method if none exist (and update the
1587/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1588/// methods is the "right" thing to do.
1589void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001590 ObjCContainerDecl *CD,
1591 ObjCPropertyDecl *redeclaredProperty,
1592 ObjCContainerDecl *lexicalDC) {
1593
Ted Kremenek9d64c152010-03-12 00:38:38 +00001594 ObjCMethodDecl *GetterMethod, *SetterMethod;
1595
1596 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1597 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1598 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1599 property->getLocation());
1600
1601 if (SetterMethod) {
1602 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1603 property->getPropertyAttributes();
1604 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1605 Context.getCanonicalType(SetterMethod->getResultType()) !=
1606 Context.VoidTy)
1607 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1608 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001609 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001610 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1611 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001612 Diag(property->getLocation(),
1613 diag::warn_accessor_property_type_mismatch)
1614 << property->getDeclName()
1615 << SetterMethod->getSelector();
1616 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1617 }
1618 }
1619
1620 // Synthesize getter/setter methods if none exist.
1621 // Find the default getter and if one not found, add one.
1622 // FIXME: The synthesized property we set here is misleading. We almost always
1623 // synthesize these methods unless the user explicitly provided prototypes
1624 // (which is odd, but allowed). Sema should be typechecking that the
1625 // declarations jive in that situation (which it is not currently).
1626 if (!GetterMethod) {
1627 // No instance method of same name as property getter name was found.
1628 // Declare a getter method and add it to the list of methods
1629 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001630 SourceLocation Loc = redeclaredProperty ?
1631 redeclaredProperty->getLocation() :
1632 property->getLocation();
1633
1634 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1635 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001636 property->getType(), 0, CD, /*isInstance=*/true,
1637 /*isVariadic=*/false, /*isSynthesized=*/true,
1638 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001639 (property->getPropertyImplementation() ==
1640 ObjCPropertyDecl::Optional) ?
1641 ObjCMethodDecl::Optional :
1642 ObjCMethodDecl::Required);
1643 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001644
1645 AddPropertyAttrs(*this, GetterMethod, property);
1646
Ted Kremenek23173d72010-05-18 21:09:07 +00001647 // FIXME: Eventually this shouldn't be needed, as the lexical context
1648 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001649 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001650 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001651 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1652 GetterMethod->addAttr(
1653 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001654 } else
1655 // A user declared getter will be synthesize when @synthesize of
1656 // the property with the same name is seen in the @implementation
1657 GetterMethod->setSynthesized(true);
1658 property->setGetterMethodDecl(GetterMethod);
1659
1660 // Skip setter if property is read-only.
1661 if (!property->isReadOnly()) {
1662 // Find the default setter and if one not found, add one.
1663 if (!SetterMethod) {
1664 // No instance method of same name as property setter name was found.
1665 // Declare a setter method and add it to the list of methods
1666 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001667 SourceLocation Loc = redeclaredProperty ?
1668 redeclaredProperty->getLocation() :
1669 property->getLocation();
1670
1671 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001672 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001673 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001674 CD, /*isInstance=*/true, /*isVariadic=*/false,
1675 /*isSynthesized=*/true,
1676 /*isImplicitlyDeclared=*/true,
1677 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001678 (property->getPropertyImplementation() ==
1679 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001680 ObjCMethodDecl::Optional :
1681 ObjCMethodDecl::Required);
1682
Ted Kremenek9d64c152010-03-12 00:38:38 +00001683 // Invent the arguments for the setter. We don't bother making a
1684 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001685 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1686 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001687 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001688 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001689 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001690 SC_None,
1691 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001692 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001693 SetterMethod->setMethodParams(Context, Argument,
1694 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001695
1696 AddPropertyAttrs(*this, SetterMethod, property);
1697
Ted Kremenek9d64c152010-03-12 00:38:38 +00001698 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001699 // FIXME: Eventually this shouldn't be needed, as the lexical context
1700 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001701 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001702 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001703 } else
1704 // A user declared setter will be synthesize when @synthesize of
1705 // the property with the same name is seen in the @implementation
1706 SetterMethod->setSynthesized(true);
1707 property->setSetterMethodDecl(SetterMethod);
1708 }
1709 // Add any synthesized methods to the global pool. This allows us to
1710 // handle the following, which is supported by GCC (and part of the design).
1711 //
1712 // @interface Foo
1713 // @property double bar;
1714 // @end
1715 //
1716 // void thisIsUnfortunate() {
1717 // id foo;
1718 // double bar = [foo bar];
1719 // }
1720 //
1721 if (GetterMethod)
1722 AddInstanceMethodToGlobalPool(GetterMethod);
1723 if (SetterMethod)
1724 AddInstanceMethodToGlobalPool(SetterMethod);
1725}
1726
John McCalld226f652010-08-21 09:40:31 +00001727void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001728 SourceLocation Loc,
1729 unsigned &Attributes) {
1730 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001731 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001732 return;
1733
1734 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001735 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001736
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001737 if (getLangOptions().ObjCAutoRefCount &&
1738 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1739 PropertyTy->isObjCRetainableType()) {
1740 // 'readonly' property with no obvious lifetime.
1741 // its life time will be determined by its backing ivar.
1742 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1743 ObjCDeclSpec::DQ_PR_copy |
1744 ObjCDeclSpec::DQ_PR_retain |
1745 ObjCDeclSpec::DQ_PR_strong |
1746 ObjCDeclSpec::DQ_PR_weak |
1747 ObjCDeclSpec::DQ_PR_assign);
1748 if ((Attributes & rel) == 0)
1749 return;
1750 }
1751
Ted Kremenek9d64c152010-03-12 00:38:38 +00001752 // readonly and readwrite/assign/retain/copy conflict.
1753 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1754 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1755 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001756 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001757 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001758 ObjCDeclSpec::DQ_PR_retain |
1759 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001760 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1761 "readwrite" :
1762 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1763 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001764 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1765 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001766 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1767 "copy" : "retain";
1768
1769 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1770 diag::err_objc_property_attr_mutually_exclusive :
1771 diag::warn_objc_property_attr_mutually_exclusive)
1772 << "readonly" << which;
1773 }
1774
1775 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001776 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1777 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1778 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001779 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001780 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001781 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1782 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1783 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1784 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00001785 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001786 }
1787
1788 // Check for more than one of { assign, copy, retain }.
1789 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1790 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1791 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1792 << "assign" << "copy";
1793 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1794 }
1795 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1796 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1797 << "assign" << "retain";
1798 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1799 }
John McCallf85e1932011-06-15 23:02:42 +00001800 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1801 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1802 << "assign" << "strong";
1803 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1804 }
1805 if (getLangOptions().ObjCAutoRefCount &&
1806 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1807 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1808 << "assign" << "weak";
1809 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1810 }
1811 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1812 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1813 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1814 << "unsafe_unretained" << "copy";
1815 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1816 }
1817 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1818 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1819 << "unsafe_unretained" << "retain";
1820 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1821 }
1822 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1823 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1824 << "unsafe_unretained" << "strong";
1825 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1826 }
1827 if (getLangOptions().ObjCAutoRefCount &&
1828 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1829 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1830 << "unsafe_unretained" << "weak";
1831 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1832 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001833 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1834 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1835 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1836 << "copy" << "retain";
1837 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1838 }
John McCallf85e1932011-06-15 23:02:42 +00001839 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1840 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1841 << "copy" << "strong";
1842 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1843 }
1844 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1845 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1846 << "copy" << "weak";
1847 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1848 }
1849 }
1850 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1851 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1852 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1853 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001854 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001855 }
1856 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1857 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1858 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1859 << "strong" << "weak";
1860 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001861 }
1862
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00001863 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
1864 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
1865 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1866 << "atomic" << "nonatomic";
1867 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
1868 }
1869
Ted Kremenek9d64c152010-03-12 00:38:38 +00001870 // Warn if user supplied no assignment attribute, property is
1871 // readwrite, and this is an object type.
1872 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001873 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1874 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1875 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001876 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001877 if (getLangOptions().ObjCAutoRefCount)
1878 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001879 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001880 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001881 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001882 bool isAnyClassTy =
1883 (PropertyTy->isObjCClassType() ||
1884 PropertyTy->isObjCQualifiedClassType());
1885 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
1886 // issue any warning.
1887 if (isAnyClassTy && getLangOptions().getGC() == LangOptions::NonGC)
1888 ;
1889 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001890 // Skip this warning in gc-only mode.
Douglas Gregore289d812011-09-13 17:21:33 +00001891 if (getLangOptions().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001892 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001893
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001894 // If non-gc code warn that this is likely inappropriate.
Douglas Gregore289d812011-09-13 17:21:33 +00001895 if (getLangOptions().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001896 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001897 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001898 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001899
1900 // FIXME: Implement warning dependent on NSCopying being
1901 // implemented. See also:
1902 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1903 // (please trim this list while you are at it).
1904 }
1905
1906 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001907 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Douglas Gregore289d812011-09-13 17:21:33 +00001908 && getLangOptions().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001909 && PropertyTy->isBlockPointerType())
1910 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001911 else if (getLangOptions().ObjCAutoRefCount &&
1912 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1913 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1914 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1915 PropertyTy->isBlockPointerType())
1916 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00001917
1918 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1919 (Attributes & ObjCDeclSpec::DQ_PR_setter))
1920 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
1921
Ted Kremenek9d64c152010-03-12 00:38:38 +00001922}