blob: 9d849ff74fdfc7ec1eab2c6d23acfe14b4d3aab2 [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"
Ted Kremenek9d64c152010-03-12 00:38:38 +000022
23using namespace clang;
24
Ted Kremenek28685ab2010-03-12 00:46:40 +000025//===----------------------------------------------------------------------===//
26// Grammar actions.
27//===----------------------------------------------------------------------===//
28
John McCall265941b2011-09-13 18:31:23 +000029/// getImpliedARCOwnership - Given a set of property attributes and a
30/// type, infer an expected lifetime. The type's ownership qualification
31/// is not considered.
32///
33/// Returns OCL_None if the attributes as stated do not imply an ownership.
34/// Never returns OCL_Autoreleasing.
35static Qualifiers::ObjCLifetime getImpliedARCOwnership(
36 ObjCPropertyDecl::PropertyAttributeKind attrs,
37 QualType type) {
38 // retain, strong, copy, weak, and unsafe_unretained are only legal
39 // on properties of retainable pointer type.
40 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
41 ObjCPropertyDecl::OBJC_PR_strong |
42 ObjCPropertyDecl::OBJC_PR_copy)) {
Fariborz Jahanian5fa065b2011-10-13 23:45:45 +000043 return type->getObjCARCImplicitLifetime();
John McCall265941b2011-09-13 18:31:23 +000044 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
45 return Qualifiers::OCL_Weak;
46 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
47 return Qualifiers::OCL_ExplicitNone;
48 }
49
50 // assign can appear on other types, so we have to check the
51 // property type.
52 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
53 type->isObjCRetainableType()) {
54 return Qualifiers::OCL_ExplicitNone;
55 }
56
57 return Qualifiers::OCL_None;
58}
59
John McCallf85e1932011-06-15 23:02:42 +000060/// Check the internal consistency of a property declaration.
61static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
62 if (property->isInvalidDecl()) return;
63
64 ObjCPropertyDecl::PropertyAttributeKind propertyKind
65 = property->getPropertyAttributes();
66 Qualifiers::ObjCLifetime propertyLifetime
67 = property->getType().getObjCLifetime();
68
69 // Nothing to do if we don't have a lifetime.
70 if (propertyLifetime == Qualifiers::OCL_None) return;
71
John McCall265941b2011-09-13 18:31:23 +000072 Qualifiers::ObjCLifetime expectedLifetime
73 = getImpliedARCOwnership(propertyKind, property->getType());
74 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000075 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000076 // attribute. That's okay, but restore reasonable invariants by
77 // setting the property attribute according to the lifetime
78 // qualifier.
79 ObjCPropertyDecl::PropertyAttributeKind attr;
80 if (propertyLifetime == Qualifiers::OCL_Strong) {
81 attr = ObjCPropertyDecl::OBJC_PR_strong;
82 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
83 attr = ObjCPropertyDecl::OBJC_PR_weak;
84 } else {
85 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
86 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
87 }
88 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000089 return;
90 }
91
92 if (propertyLifetime == expectedLifetime) return;
93
94 property->setInvalidDecl();
95 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000096 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +000097 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +000098 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +000099 << propertyLifetime;
100}
101
John McCalld226f652010-08-21 09:40:31 +0000102Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
103 FieldDeclarator &FD,
104 ObjCDeclSpec &ODS,
105 Selector GetterSel,
106 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000107 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000108 tok::ObjCKeywordKind MethodImplKind,
109 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000110 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000111 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
112 QualType T = TSI->getType();
Douglas Gregore289d812011-09-13 17:21:33 +0000113 if ((getLangOptions().getGC() != LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +0000114 T.isObjCGCWeak()) ||
115 (getLangOptions().ObjCAutoRefCount &&
116 T.getObjCLifetime() == Qualifiers::OCL_Weak))
117 Attributes |= ObjCDeclSpec::DQ_PR_weak;
118
Ted Kremenek28685ab2010-03-12 00:46:40 +0000119 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
120 // default is readwrite!
121 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
122 // property is defaulted to 'assign' if it is readwrite and is
123 // not retain or copy
124 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
125 (isReadWrite &&
126 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +0000127 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
128 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
129 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
130 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000131
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000132 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000133 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000134
Ted Kremenek28685ab2010-03-12 00:46:40 +0000135 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000136 if (CDecl->IsClassExtension()) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000137 Decl *Res = HandlePropertyInClassExtension(S, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000138 FD, GetterSel, SetterSel,
139 isAssign, isReadWrite,
140 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000141 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000142 isOverridingProperty, TSI,
143 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000144 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000145 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000146 if (getLangOptions().ObjCAutoRefCount)
147 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
148 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000149 return Res;
150 }
151
John McCallf85e1932011-06-15 23:02:42 +0000152 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
153 GetterSel, SetterSel,
154 isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000155 Attributes,
156 ODS.getPropertyAttributes(),
157 TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000158 if (lexicalDC)
159 Res->setLexicalDeclContext(lexicalDC);
160
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000161 // Validate the attributes on the @property.
162 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000163
164 if (getLangOptions().ObjCAutoRefCount)
165 checkARCPropertyDecl(*this, Res);
166
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000167 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000168}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000169
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000170static ObjCPropertyDecl::PropertyAttributeKind
171makePropertyAttributesAsWritten(unsigned Attributes) {
172 unsigned attributesAsWritten = 0;
173 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
174 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
175 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
176 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
177 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
178 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
179 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
180 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
181 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
182 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
183 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
184 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
185 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
186 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
187 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
188 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
189 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
190 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
191 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
192 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
193 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
194 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
195 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
196 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
197
198 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
199}
200
John McCalld226f652010-08-21 09:40:31 +0000201Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000202Sema::HandlePropertyInClassExtension(Scope *S,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000203 SourceLocation AtLoc, FieldDeclarator &FD,
204 Selector GetterSel, Selector SetterSel,
205 const bool isAssign,
206 const bool isReadWrite,
207 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000208 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000209 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000210 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000211 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000212 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000213 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000214 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000215 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000216 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
217
218 if (CCPrimary)
219 // Check for duplicate declaration of this property in current and
220 // other class extensions.
221 for (const ObjCCategoryDecl *ClsExtDecl =
222 CCPrimary->getFirstClassExtension();
223 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
224 if (ObjCPropertyDecl *prevDecl =
225 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
226 Diag(AtLoc, diag::err_duplicate_property);
227 Diag(prevDecl->getLocation(), diag::note_property_declare);
228 return 0;
229 }
230 }
231
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000232 // Create a new ObjCPropertyDecl with the DeclContext being
233 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000234 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000235 ObjCPropertyDecl *PDecl =
236 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
237 PropertyId, AtLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000238 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000239 makePropertyAttributesAsWritten(AttributesAsWritten));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000240 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
241 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
242 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
243 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000244 // Set setter/getter selector name. Needed later.
245 PDecl->setGetterName(GetterSel);
246 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000247 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000248 DC->addDecl(PDecl);
249
250 // We need to look in the @interface to see if the @property was
251 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000252 if (!CCPrimary) {
253 Diag(CDecl->getLocation(), diag::err_continuation_class);
254 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000255 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000256 }
257
258 // Find the property in continuation class's primary class only.
259 ObjCPropertyDecl *PIDecl =
260 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
261
262 if (!PIDecl) {
263 // No matching property found in the primary class. Just fall thru
264 // and add property to continuation class's primary class.
265 ObjCPropertyDecl *PDecl =
266 CreatePropertyDecl(S, CCPrimary, AtLoc,
267 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000268 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000269
270 // A case of continuation class adding a new property in the class. This
271 // is not what it was meant for. However, gcc supports it and so should we.
272 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000273 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
274 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000275 if (ASTMutationListener *L = Context.getASTMutationListener())
276 L->AddedObjCPropertyInClassExtension(PDecl, /*OrigProp=*/0, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000277 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000278 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000279 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
280 bool IncompatibleObjC = false;
281 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000282 // Relax the strict type matching for property type in continuation class.
283 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000284 // as it narrows the object type in its primary class property. Note that
285 // this conversion is safe only because the wider type is for a 'readonly'
286 // property in primary class and 'narrowed' type for a 'readwrite' property
287 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000288 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
289 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
290 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
291 ConvertedType, IncompatibleObjC))
292 || IncompatibleObjC) {
293 Diag(AtLoc,
294 diag::err_type_mismatch_continuation_class) << PDecl->getType();
295 Diag(PIDecl->getLocation(), diag::note_property_declare);
296 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000297 }
298
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000299 // The property 'PIDecl's readonly attribute will be over-ridden
300 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000301 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000302 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
303 unsigned retainCopyNonatomic =
304 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000305 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000306 ObjCPropertyDecl::OBJC_PR_copy |
307 ObjCPropertyDecl::OBJC_PR_nonatomic);
308 if ((Attributes & retainCopyNonatomic) !=
309 (PIkind & retainCopyNonatomic)) {
310 Diag(AtLoc, diag::warn_property_attr_mismatch);
311 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000312 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000313 DeclContext *DC = cast<DeclContext>(CCPrimary);
314 if (!ObjCPropertyDecl::findPropertyDecl(DC,
315 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000316 // Protocol is not in the primary class. Must build one for it.
317 ObjCDeclSpec ProtocolPropertyODS;
318 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
319 // and ObjCPropertyDecl::PropertyAttributeKind have identical
320 // values. Should consolidate both into one enum type.
321 ProtocolPropertyODS.
322 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
323 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000324 // Must re-establish the context from class extension to primary
325 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000326 ContextRAII SavedContext(*this, CCPrimary);
327
John McCalld226f652010-08-21 09:40:31 +0000328 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000329 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
330 PIDecl->getGetterName(),
331 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000332 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000333 MethodImplKind,
334 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000335 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000336 }
337 PIDecl->makeitReadWriteAttribute();
338 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
339 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000340 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
341 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000342 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
343 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
344 PIDecl->setSetterName(SetterSel);
345 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000346 // Tailor the diagnostics for the common case where a readwrite
347 // property is declared both in the @interface and the continuation.
348 // This is a common error where the user often intended the original
349 // declaration to be readonly.
350 unsigned diag =
351 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
352 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
353 ? diag::err_use_continuation_class_redeclaration_readwrite
354 : diag::err_use_continuation_class;
355 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000356 << CCPrimary->getDeclName();
357 Diag(PIDecl->getLocation(), diag::note_property_declare);
358 }
359 *isOverridingProperty = true;
360 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000361 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000362 if (ASTMutationListener *L = Context.getASTMutationListener())
363 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000364 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000365}
366
367ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
368 ObjCContainerDecl *CDecl,
369 SourceLocation AtLoc,
370 FieldDeclarator &FD,
371 Selector GetterSel,
372 Selector SetterSel,
373 const bool isAssign,
374 const bool isReadWrite,
375 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000376 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000377 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000378 tok::ObjCKeywordKind MethodImplKind,
379 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000380 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000381 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000382
383 // Issue a warning if property is 'assign' as default and its object, which is
384 // gc'able conforms to NSCopying protocol
Douglas Gregore289d812011-09-13 17:21:33 +0000385 if (getLangOptions().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000386 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000387 if (const ObjCObjectPointerType *ObjPtrTy =
388 T->getAs<ObjCObjectPointerType>()) {
389 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
390 if (IDecl)
391 if (ObjCProtocolDecl* PNSCopying =
392 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
393 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
394 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000395 }
John McCallc12c5bb2010-05-15 11:32:37 +0000396 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000397 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
398
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000399 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000400 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
401 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000402 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000403
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000404 if (ObjCPropertyDecl *prevDecl =
405 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000406 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000407 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000408 PDecl->setInvalidDecl();
409 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000410 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000411 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000412 if (lexicalDC)
413 PDecl->setLexicalDeclContext(lexicalDC);
414 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000415
416 if (T->isArrayType() || T->isFunctionType()) {
417 Diag(AtLoc, diag::err_property_type) << T;
418 PDecl->setInvalidDecl();
419 }
420
421 ProcessDeclAttributes(S, PDecl, FD.D);
422
423 // Regardless of setter/getter attribute, we save the default getter/setter
424 // selector names in anticipation of declaration of setter/getter methods.
425 PDecl->setGetterName(GetterSel);
426 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000427 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000428 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000429
Ted Kremenek28685ab2010-03-12 00:46:40 +0000430 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
431 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
432
433 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
434 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
435
436 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
437 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
438
439 if (isReadWrite)
440 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
441
442 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
443 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
444
John McCallf85e1932011-06-15 23:02:42 +0000445 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
446 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
447
448 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
449 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
450
Ted Kremenek28685ab2010-03-12 00:46:40 +0000451 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
452 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
453
John McCallf85e1932011-06-15 23:02:42 +0000454 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
455 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
456
Ted Kremenek28685ab2010-03-12 00:46:40 +0000457 if (isAssign)
458 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
459
John McCall265941b2011-09-13 18:31:23 +0000460 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000461 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
462 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000463 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000464 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000465
John McCallf85e1932011-06-15 23:02:42 +0000466 // 'unsafe_unretained' is alias for 'assign'.
467 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
468 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
469 if (isAssign)
470 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
471
Ted Kremenek28685ab2010-03-12 00:46:40 +0000472 if (MethodImplKind == tok::objc_required)
473 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
474 else if (MethodImplKind == tok::objc_optional)
475 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000476
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000477 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000478}
479
John McCallf85e1932011-06-15 23:02:42 +0000480static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
481 ObjCPropertyDecl *property,
482 ObjCIvarDecl *ivar) {
483 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
484
John McCallf85e1932011-06-15 23:02:42 +0000485 QualType ivarType = ivar->getType();
486 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000487
John McCall265941b2011-09-13 18:31:23 +0000488 // The lifetime implied by the property's attributes.
489 Qualifiers::ObjCLifetime propertyLifetime =
490 getImpliedARCOwnership(property->getPropertyAttributes(),
491 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000492
John McCall265941b2011-09-13 18:31:23 +0000493 // We're fine if they match.
494 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000495
John McCall265941b2011-09-13 18:31:23 +0000496 // These aren't valid lifetimes for object ivars; don't diagnose twice.
497 if (ivarLifetime == Qualifiers::OCL_None ||
498 ivarLifetime == Qualifiers::OCL_Autoreleasing)
499 return;
John McCallf85e1932011-06-15 23:02:42 +0000500
John McCall265941b2011-09-13 18:31:23 +0000501 switch (propertyLifetime) {
502 case Qualifiers::OCL_Strong:
503 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
504 << property->getDeclName()
505 << ivar->getDeclName()
506 << ivarLifetime;
507 break;
John McCallf85e1932011-06-15 23:02:42 +0000508
John McCall265941b2011-09-13 18:31:23 +0000509 case Qualifiers::OCL_Weak:
510 S.Diag(propertyImplLoc, diag::error_weak_property)
511 << property->getDeclName()
512 << ivar->getDeclName();
513 break;
John McCallf85e1932011-06-15 23:02:42 +0000514
John McCall265941b2011-09-13 18:31:23 +0000515 case Qualifiers::OCL_ExplicitNone:
516 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
517 << property->getDeclName()
518 << ivar->getDeclName()
519 << ((property->getPropertyAttributesAsWritten()
520 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
521 break;
John McCallf85e1932011-06-15 23:02:42 +0000522
John McCall265941b2011-09-13 18:31:23 +0000523 case Qualifiers::OCL_Autoreleasing:
524 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000525
John McCall265941b2011-09-13 18:31:23 +0000526 case Qualifiers::OCL_None:
527 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000528 return;
529 }
530
531 S.Diag(property->getLocation(), diag::note_property_declare);
532}
533
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000534/// setImpliedPropertyAttributeForReadOnlyProperty -
535/// This routine evaludates life-time attributes for a 'readonly'
536/// property with no known lifetime of its own, using backing
537/// 'ivar's attribute, if any. If no backing 'ivar', property's
538/// life-time is assumed 'strong'.
539static void setImpliedPropertyAttributeForReadOnlyProperty(
540 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
541 Qualifiers::ObjCLifetime propertyLifetime =
542 getImpliedARCOwnership(property->getPropertyAttributes(),
543 property->getType());
544 if (propertyLifetime != Qualifiers::OCL_None)
545 return;
546
547 if (!ivar) {
548 // if no backing ivar, make property 'strong'.
549 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
550 return;
551 }
552 // property assumes owenership of backing ivar.
553 QualType ivarType = ivar->getType();
554 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
555 if (ivarLifetime == Qualifiers::OCL_Strong)
556 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
557 else if (ivarLifetime == Qualifiers::OCL_Weak)
558 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
559 return;
560}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000561
562/// ActOnPropertyImplDecl - This routine performs semantic checks and
563/// builds the AST node for a property implementation declaration; declared
564/// as @synthesize or @dynamic.
565///
John McCalld226f652010-08-21 09:40:31 +0000566Decl *Sema::ActOnPropertyImplDecl(Scope *S,
567 SourceLocation AtLoc,
568 SourceLocation PropertyLoc,
569 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000570 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000571 IdentifierInfo *PropertyIvar,
572 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000573 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000574 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000575 // Make sure we have a context for the property implementation declaration.
576 if (!ClassImpDecl) {
577 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000578 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000579 }
580 ObjCPropertyDecl *property = 0;
581 ObjCInterfaceDecl* IDecl = 0;
582 // Find the class or category class where this property must have
583 // a declaration.
584 ObjCImplementationDecl *IC = 0;
585 ObjCCategoryImplDecl* CatImplClass = 0;
586 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
587 IDecl = IC->getClassInterface();
588 // We always synthesize an interface for an implementation
589 // without an interface decl. So, IDecl is always non-zero.
590 assert(IDecl &&
591 "ActOnPropertyImplDecl - @implementation without @interface");
592
593 // Look for this property declaration in the @implementation's @interface
594 property = IDecl->FindPropertyDeclaration(PropertyId);
595 if (!property) {
596 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000597 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000598 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000599 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000600 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
601 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000602 if (AtLoc.isValid())
603 Diag(AtLoc, diag::warn_implicit_atomic_property);
604 else
605 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
606 Diag(property->getLocation(), diag::note_property_declare);
607 }
608
Ted Kremenek28685ab2010-03-12 00:46:40 +0000609 if (const ObjCCategoryDecl *CD =
610 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
611 if (!CD->IsClassExtension()) {
612 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
613 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000614 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000615 }
616 }
617 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
618 if (Synthesize) {
619 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000620 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000621 }
622 IDecl = CatImplClass->getClassInterface();
623 if (!IDecl) {
624 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000625 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000626 }
627 ObjCCategoryDecl *Category =
628 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
629
630 // If category for this implementation not found, it is an error which
631 // has already been reported eralier.
632 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000633 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000634 // Look for this property declaration in @implementation's category
635 property = Category->FindPropertyDeclaration(PropertyId);
636 if (!property) {
637 Diag(PropertyLoc, diag::error_bad_category_property_decl)
638 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000639 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000640 }
641 } else {
642 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000643 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000644 }
645 ObjCIvarDecl *Ivar = 0;
646 // Check that we have a valid, previously declared ivar for @synthesize
647 if (Synthesize) {
648 // @synthesize
649 if (!PropertyIvar)
650 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000651 // Check that this is a previously declared 'ivar' in 'IDecl' interface
652 ObjCInterfaceDecl *ClassDeclared;
653 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
654 QualType PropType = property->getType();
655 QualType PropertyIvarType = PropType.getNonReferenceType();
656
657 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000658 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000659 ObjCPropertyDecl::OBJC_PR_readonly) &&
660 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000661 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
662 }
663
John McCallf85e1932011-06-15 23:02:42 +0000664 ObjCPropertyDecl::PropertyAttributeKind kind
665 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000666
667 // Add GC __weak to the ivar type if the property is weak.
668 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
Douglas Gregore289d812011-09-13 17:21:33 +0000669 getLangOptions().getGC() != LangOptions::NonGC) {
John McCall265941b2011-09-13 18:31:23 +0000670 assert(!getLangOptions().ObjCAutoRefCount);
671 if (PropertyIvarType.isObjCGCStrong()) {
672 Diag(PropertyLoc, diag::err_gc_weak_property_strong_type);
673 Diag(property->getLocation(), diag::note_property_declare);
674 } else {
675 PropertyIvarType =
676 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000677 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000678 }
John McCall265941b2011-09-13 18:31:23 +0000679
Ted Kremenek28685ab2010-03-12 00:46:40 +0000680 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000681 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000682 // property attributes.
683 if (getLangOptions().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000684 !PropertyIvarType.getObjCLifetime() &&
685 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000686
John McCall265941b2011-09-13 18:31:23 +0000687 // It's an error if we have to do this and the user didn't
688 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000689 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000690 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000691 Diag(PropertyLoc,
692 diag::err_arc_objc_property_default_assign_on_object);
693 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000694 } else {
695 Qualifiers::ObjCLifetime lifetime =
696 getImpliedARCOwnership(kind, PropertyIvarType);
697 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000698 if (lifetime == Qualifiers::OCL_Weak) {
699 bool err = false;
700 if (const ObjCObjectPointerType *ObjT =
701 PropertyIvarType->getAs<ObjCObjectPointerType>())
702 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
703 Diag(PropertyLoc, diag::err_arc_weak_unavailable_property);
704 Diag(property->getLocation(), diag::note_property_declare);
705 err = true;
706 }
707 if (!err && !getLangOptions().ObjCRuntimeHasWeak) {
708 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
709 Diag(property->getLocation(), diag::note_property_declare);
710 }
John McCallf85e1932011-06-15 23:02:42 +0000711 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000712
John McCallf85e1932011-06-15 23:02:42 +0000713 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000714 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000715 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
716 }
John McCallf85e1932011-06-15 23:02:42 +0000717 }
718
719 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
720 !getLangOptions().ObjCAutoRefCount &&
Douglas Gregore289d812011-09-13 17:21:33 +0000721 getLangOptions().getGC() == LangOptions::NonGC) {
John McCallf85e1932011-06-15 23:02:42 +0000722 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
723 Diag(property->getLocation(), diag::note_property_declare);
724 }
725
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000726 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
727 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000728 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000729 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000730 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000731 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000732 IDecl->makeDeclVisibleInContext(Ivar, false);
733 property->setPropertyIvarDecl(Ivar);
734
735 if (!getLangOptions().ObjCNonFragileABI)
736 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
737 // Note! I deliberately want it to fall thru so, we have a
738 // a property implementation and to avoid future warnings.
739 } else if (getLangOptions().ObjCNonFragileABI &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000740 !declaresSameEntity(ClassDeclared, IDecl)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000741 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
742 << property->getDeclName() << Ivar->getDeclName()
743 << ClassDeclared->getDeclName();
744 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000745 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000746 // Note! I deliberately want it to fall thru so more errors are caught.
747 }
748 QualType IvarType = Context.getCanonicalType(Ivar->getType());
749
750 // Check that type of property and its ivar are type compatible.
John McCall265941b2011-09-13 18:31:23 +0000751 if (Context.getCanonicalType(PropertyIvarType) != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000752 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000753 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000754 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000755 compat =
756 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000757 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000758 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000759 else {
760 SourceLocation Loc = PropertyIvarLoc;
761 if (Loc.isInvalid())
762 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000763 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000764 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000765 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000766 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000767 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000768 << property->getDeclName() << PropType
769 << Ivar->getDeclName() << IvarType;
770 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000771 // Note! I deliberately want it to fall thru so, we have a
772 // a property implementation and to avoid future warnings.
773 }
774
775 // FIXME! Rules for properties are somewhat different that those
776 // for assignments. Use a new routine to consolidate all cases;
777 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000778 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000779 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
780 if (lhsType != rhsType &&
781 lhsType->isArithmeticType()) {
782 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000783 << property->getDeclName() << PropType
784 << Ivar->getDeclName() << IvarType;
785 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000786 // Fall thru - see previous comment
787 }
788 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000789 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000790 getLangOptions().getGC() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000791 Diag(PropertyLoc, diag::error_weak_property)
792 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000793 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000794 // Fall thru - see previous comment
795 }
John McCallf85e1932011-06-15 23:02:42 +0000796 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000797 if ((property->getType()->isObjCObjectPointerType() ||
798 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000799 getLangOptions().getGC() != LangOptions::NonGC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000800 Diag(PropertyLoc, diag::error_strong_property)
801 << property->getDeclName() << Ivar->getDeclName();
802 // Fall thru - see previous comment
803 }
804 }
John McCallf85e1932011-06-15 23:02:42 +0000805 if (getLangOptions().ObjCAutoRefCount)
806 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000807 } else if (PropertyIvar)
808 // @dynamic
809 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000810
Ted Kremenek28685ab2010-03-12 00:46:40 +0000811 assert (property && "ActOnPropertyImplDecl - property declaration missing");
812 ObjCPropertyImplDecl *PIDecl =
813 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
814 property,
815 (Synthesize ?
816 ObjCPropertyImplDecl::Synthesize
817 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000818 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000819 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
820 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000821 if (getLangOptions().CPlusPlus && Synthesize &&
822 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000823 // For Objective-C++, need to synthesize the AST for the IVAR object to be
824 // returned by the getter as it must conform to C++'s copy-return rules.
825 // FIXME. Eventually we want to do this for Objective-C as well.
826 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
827 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000828 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
829 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000830 Expr *IvarRefExpr =
831 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
832 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000833 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000834 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000835 SourceLocation(),
836 getterMethod->getResultType(),
837 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000838 SourceLocation(),
839 Owned(IvarRefExpr));
840 if (!Res.isInvalid()) {
841 Expr *ResExpr = Res.takeAs<Expr>();
842 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000843 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000844 PIDecl->setGetterCXXConstructor(ResExpr);
845 }
846 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000847 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
848 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
849 Diag(getterMethod->getLocation(),
850 diag::warn_property_getter_owning_mismatch);
851 Diag(property->getLocation(), diag::note_property_declare);
852 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000853 }
854 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
855 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000856 if (getLangOptions().CPlusPlus && Synthesize
857 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000858 // FIXME. Eventually we want to do this for Objective-C as well.
859 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
860 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000861 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
862 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000863 Expr *lhs =
864 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
865 SelfExpr, true, true);
866 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
867 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +0000868 QualType T = Param->getType().getNonReferenceType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000869 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000870 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000871 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000872 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000873 if (property->getPropertyAttributes() &
874 ObjCPropertyDecl::OBJC_PR_atomic) {
875 Expr *callExpr = Res.takeAs<Expr>();
876 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000877 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
878 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000879 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000880 if (property->getType()->isReferenceType()) {
881 Diag(PropertyLoc,
882 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000883 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000884 Diag(FuncDecl->getLocStart(),
885 diag::note_callee_decl) << FuncDecl;
886 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000887 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000888 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
889 }
890 }
891
Ted Kremenek28685ab2010-03-12 00:46:40 +0000892 if (IC) {
893 if (Synthesize)
894 if (ObjCPropertyImplDecl *PPIDecl =
895 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
896 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
897 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
898 << PropertyIvar;
899 Diag(PPIDecl->getLocation(), diag::note_previous_use);
900 }
901
902 if (ObjCPropertyImplDecl *PPIDecl
903 = IC->FindPropertyImplDecl(PropertyId)) {
904 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
905 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000906 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000907 }
908 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000909 if (getLangOptions().ObjCDefaultSynthProperties &&
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +0000910 getLangOptions().ObjCNonFragileABI2 &&
Ted Kremenek71207fc2012-01-05 22:47:47 +0000911 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000912 // Diagnose if an ivar was lazily synthesdized due to a previous
913 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000914 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000915 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000916 ObjCIvarDecl *Ivar = 0;
917 if (!Synthesize)
918 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
919 else {
920 if (PropertyIvar && PropertyIvar != PropertyId)
921 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
922 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000923 // Issue diagnostics only if Ivar belongs to current class.
924 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000925 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000926 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
927 << PropertyId;
928 Ivar->setInvalidDecl();
929 }
930 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000931 } else {
932 if (Synthesize)
933 if (ObjCPropertyImplDecl *PPIDecl =
934 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
935 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
936 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
937 << PropertyIvar;
938 Diag(PPIDecl->getLocation(), diag::note_previous_use);
939 }
940
941 if (ObjCPropertyImplDecl *PPIDecl =
942 CatImplClass->FindPropertyImplDecl(PropertyId)) {
943 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
944 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000945 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000946 }
947 CatImplClass->addPropertyImplementation(PIDecl);
948 }
949
John McCalld226f652010-08-21 09:40:31 +0000950 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000951}
952
953//===----------------------------------------------------------------------===//
954// Helper methods.
955//===----------------------------------------------------------------------===//
956
Ted Kremenek9d64c152010-03-12 00:38:38 +0000957/// DiagnosePropertyMismatch - Compares two properties for their
958/// attributes and types and warns on a variety of inconsistencies.
959///
960void
961Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
962 ObjCPropertyDecl *SuperProperty,
963 const IdentifierInfo *inheritedName) {
964 ObjCPropertyDecl::PropertyAttributeKind CAttr =
965 Property->getPropertyAttributes();
966 ObjCPropertyDecl::PropertyAttributeKind SAttr =
967 SuperProperty->getPropertyAttributes();
968 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
969 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
970 Diag(Property->getLocation(), diag::warn_readonly_property)
971 << Property->getDeclName() << inheritedName;
972 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
973 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
974 Diag(Property->getLocation(), diag::warn_property_attribute)
975 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +0000976 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +0000977 unsigned CAttrRetain =
978 (CAttr &
979 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
980 unsigned SAttrRetain =
981 (SAttr &
982 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
983 bool CStrong = (CAttrRetain != 0);
984 bool SStrong = (SAttrRetain != 0);
985 if (CStrong != SStrong)
986 Diag(Property->getLocation(), diag::warn_property_attribute)
987 << Property->getDeclName() << "retain (or strong)" << inheritedName;
988 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000989
990 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
991 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
992 Diag(Property->getLocation(), diag::warn_property_attribute)
993 << Property->getDeclName() << "atomic" << inheritedName;
994 if (Property->getSetterName() != SuperProperty->getSetterName())
995 Diag(Property->getLocation(), diag::warn_property_attribute)
996 << Property->getDeclName() << "setter" << inheritedName;
997 if (Property->getGetterName() != SuperProperty->getGetterName())
998 Diag(Property->getLocation(), diag::warn_property_attribute)
999 << Property->getDeclName() << "getter" << inheritedName;
1000
1001 QualType LHSType =
1002 Context.getCanonicalType(SuperProperty->getType());
1003 QualType RHSType =
1004 Context.getCanonicalType(Property->getType());
1005
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001006 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001007 // Do cases not handled in above.
1008 // FIXME. For future support of covariant property types, revisit this.
1009 bool IncompatibleObjC = false;
1010 QualType ConvertedType;
1011 if (!isObjCPointerConversion(RHSType, LHSType,
1012 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001013 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001014 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1015 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001016 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1017 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001018 }
1019}
1020
1021bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1022 ObjCMethodDecl *GetterMethod,
1023 SourceLocation Loc) {
1024 if (GetterMethod &&
John McCall3c3b7f92011-10-25 17:37:35 +00001025 !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(),
1026 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001027 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +00001028 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +00001029 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +00001030 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001031 if (result != Compatible) {
1032 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1033 << property->getDeclName()
1034 << GetterMethod->getSelector();
1035 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1036 return true;
1037 }
1038 }
1039 return false;
1040}
1041
1042/// ComparePropertiesInBaseAndSuper - This routine compares property
1043/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001044/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001045///
1046void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1047 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1048 if (!SDecl)
1049 return;
1050 // FIXME: O(N^2)
1051 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1052 E = SDecl->prop_end(); S != E; ++S) {
1053 ObjCPropertyDecl *SuperPDecl = (*S);
1054 // Does property in super class has declaration in current class?
1055 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1056 E = IDecl->prop_end(); I != E; ++I) {
1057 ObjCPropertyDecl *PDecl = (*I);
1058 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1059 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1060 SDecl->getIdentifier());
1061 }
1062 }
1063}
1064
1065/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1066/// of properties declared in a protocol and compares their attribute against
1067/// the same property declared in the class or category.
1068void
1069Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1070 ObjCProtocolDecl *PDecl) {
1071 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1072 if (!IDecl) {
1073 // Category
1074 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1075 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1076 if (!CatDecl->IsClassExtension())
1077 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1078 E = PDecl->prop_end(); P != E; ++P) {
1079 ObjCPropertyDecl *Pr = (*P);
1080 ObjCCategoryDecl::prop_iterator CP, CE;
1081 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001082 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001083 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1084 break;
1085 if (CP != CE)
1086 // Property protocol already exist in class. Diagnose any mismatch.
1087 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1088 }
1089 return;
1090 }
1091 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1092 E = PDecl->prop_end(); P != E; ++P) {
1093 ObjCPropertyDecl *Pr = (*P);
1094 ObjCInterfaceDecl::prop_iterator CP, CE;
1095 // Is this property already in class's list of properties?
1096 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1097 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1098 break;
1099 if (CP != CE)
1100 // Property protocol already exist in class. Diagnose any mismatch.
1101 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1102 }
1103}
1104
1105/// CompareProperties - This routine compares properties
1106/// declared in 'ClassOrProtocol' objects (which can be a class or an
1107/// inherited protocol with the list of properties for class/category 'CDecl'
1108///
John McCalld226f652010-08-21 09:40:31 +00001109void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1110 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001111 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1112
1113 if (!IDecl) {
1114 // Category
1115 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1116 assert (CatDecl && "CompareProperties");
1117 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1118 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1119 E = MDecl->protocol_end(); P != E; ++P)
1120 // Match properties of category with those of protocol (*P)
1121 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1122
1123 // Go thru the list of protocols for this category and recursively match
1124 // their properties with those in the category.
1125 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1126 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001127 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001128 } else {
1129 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1130 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1131 E = MD->protocol_end(); P != E; ++P)
1132 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1133 }
1134 return;
1135 }
1136
1137 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001138 for (ObjCInterfaceDecl::all_protocol_iterator
1139 P = MDecl->all_referenced_protocol_begin(),
1140 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001141 // Match properties of class IDecl with those of protocol (*P).
1142 MatchOneProtocolPropertiesInClass(IDecl, *P);
1143
1144 // Go thru the list of protocols for this class and recursively match
1145 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001146 for (ObjCInterfaceDecl::all_protocol_iterator
1147 P = IDecl->all_referenced_protocol_begin(),
1148 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001149 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001150 } else {
1151 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1152 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1153 E = MD->protocol_end(); P != E; ++P)
1154 MatchOneProtocolPropertiesInClass(IDecl, *P);
1155 }
1156}
1157
1158/// isPropertyReadonly - Return true if property is readonly, by searching
1159/// for the property in the class and in its categories and implementations
1160///
1161bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1162 ObjCInterfaceDecl *IDecl) {
1163 // by far the most common case.
1164 if (!PDecl->isReadOnly())
1165 return false;
1166 // Even if property is ready only, if interface has a user defined setter,
1167 // it is not considered read only.
1168 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1169 return false;
1170
1171 // Main class has the property as 'readonly'. Must search
1172 // through the category list to see if the property's
1173 // attribute has been over-ridden to 'readwrite'.
1174 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1175 Category; Category = Category->getNextClassCategory()) {
1176 // Even if property is ready only, if a category has a user defined setter,
1177 // it is not considered read only.
1178 if (Category->getInstanceMethod(PDecl->getSetterName()))
1179 return false;
1180 ObjCPropertyDecl *P =
1181 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1182 if (P && !P->isReadOnly())
1183 return false;
1184 }
1185
1186 // Also, check for definition of a setter method in the implementation if
1187 // all else failed.
1188 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1189 if (ObjCImplementationDecl *IMD =
1190 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1191 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1192 return false;
1193 } else if (ObjCCategoryImplDecl *CIMD =
1194 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1195 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1196 return false;
1197 }
1198 }
1199 // Lastly, look through the implementation (if one is in scope).
1200 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1201 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1202 return false;
1203 // If all fails, look at the super class.
1204 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1205 return isPropertyReadonly(PDecl, SIDecl);
1206 return true;
1207}
1208
1209/// CollectImmediateProperties - This routine collects all properties in
1210/// the class and its conforming protocols; but not those it its super class.
1211void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001212 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1213 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001214 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1215 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1216 E = IDecl->prop_end(); P != E; ++P) {
1217 ObjCPropertyDecl *Prop = (*P);
1218 PropMap[Prop->getIdentifier()] = Prop;
1219 }
1220 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001221 for (ObjCInterfaceDecl::all_protocol_iterator
1222 PI = IDecl->all_referenced_protocol_begin(),
1223 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001224 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001225 }
1226 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1227 if (!CATDecl->IsClassExtension())
1228 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1229 E = CATDecl->prop_end(); P != E; ++P) {
1230 ObjCPropertyDecl *Prop = (*P);
1231 PropMap[Prop->getIdentifier()] = Prop;
1232 }
1233 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001234 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001235 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001236 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001237 }
1238 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1239 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1240 E = PDecl->prop_end(); P != E; ++P) {
1241 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001242 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1243 // Exclude property for protocols which conform to class's super-class,
1244 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001245 if (!PropertyFromSuper ||
1246 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001247 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1248 if (!PropEntry)
1249 PropEntry = Prop;
1250 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001251 }
1252 // scan through protocol's protocols.
1253 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1254 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001255 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001256 }
1257}
1258
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001259/// CollectClassPropertyImplementations - This routine collects list of
1260/// properties to be implemented in the class. This includes, class's
1261/// and its conforming protocols' properties.
1262static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1263 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1264 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1265 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1266 E = IDecl->prop_end(); P != E; ++P) {
1267 ObjCPropertyDecl *Prop = (*P);
1268 PropMap[Prop->getIdentifier()] = Prop;
1269 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001270 for (ObjCInterfaceDecl::all_protocol_iterator
1271 PI = IDecl->all_referenced_protocol_begin(),
1272 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001273 CollectClassPropertyImplementations((*PI), PropMap);
1274 }
1275 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1276 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1277 E = PDecl->prop_end(); P != E; ++P) {
1278 ObjCPropertyDecl *Prop = (*P);
1279 PropMap[Prop->getIdentifier()] = Prop;
1280 }
1281 // scan through protocol's protocols.
1282 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1283 E = PDecl->protocol_end(); PI != E; ++PI)
1284 CollectClassPropertyImplementations((*PI), PropMap);
1285 }
1286}
1287
1288/// CollectSuperClassPropertyImplementations - This routine collects list of
1289/// properties to be implemented in super class(s) and also coming from their
1290/// conforming protocols.
1291static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1292 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1293 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1294 while (SDecl) {
1295 CollectClassPropertyImplementations(SDecl, PropMap);
1296 SDecl = SDecl->getSuperClass();
1297 }
1298 }
1299}
1300
Ted Kremenek9d64c152010-03-12 00:38:38 +00001301/// LookupPropertyDecl - Looks up a property in the current class and all
1302/// its protocols.
1303ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1304 IdentifierInfo *II) {
1305 if (const ObjCInterfaceDecl *IDecl =
1306 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1307 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1308 E = IDecl->prop_end(); P != E; ++P) {
1309 ObjCPropertyDecl *Prop = (*P);
1310 if (Prop->getIdentifier() == II)
1311 return Prop;
1312 }
1313 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001314 for (ObjCInterfaceDecl::all_protocol_iterator
1315 PI = IDecl->all_referenced_protocol_begin(),
1316 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001317 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1318 if (Prop)
1319 return Prop;
1320 }
1321 }
1322 else if (const ObjCProtocolDecl *PDecl =
1323 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1324 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1325 E = PDecl->prop_end(); P != E; ++P) {
1326 ObjCPropertyDecl *Prop = (*P);
1327 if (Prop->getIdentifier() == II)
1328 return Prop;
1329 }
1330 // scan through protocol's protocols.
1331 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1332 E = PDecl->protocol_end(); PI != E; ++PI) {
1333 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1334 if (Prop)
1335 return Prop;
1336 }
1337 }
1338 return 0;
1339}
1340
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001341static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1342 ASTContext &Ctx) {
1343 llvm::SmallString<128> ivarName;
1344 {
1345 llvm::raw_svector_ostream os(ivarName);
1346 os << '_' << Prop->getIdentifier()->getName();
1347 }
1348 return &Ctx.Idents.get(ivarName.str());
1349}
1350
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001351/// DefaultSynthesizeProperties - This routine default synthesizes all
1352/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001353void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1354 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001355
1356 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1357 CollectClassPropertyImplementations(IDecl, PropMap);
1358 if (PropMap.empty())
1359 return;
1360 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1361 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1362
1363 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1364 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1365 ObjCPropertyDecl *Prop = P->second;
1366 // If property to be implemented in the super class, ignore.
1367 if (SuperPropMap[Prop->getIdentifier()])
1368 continue;
1369 // Is there a matching propery synthesize/dynamic?
1370 if (Prop->isInvalidDecl() ||
1371 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1372 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1373 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001374 // Property may have been synthesized by user.
1375 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1376 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001377 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1378 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1379 continue;
1380 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1381 continue;
1382 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001383 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1384 // We won't auto-synthesize properties declared in protocols.
1385 Diag(IMPDecl->getLocation(),
1386 diag::warn_auto_synthesizing_protocol_property);
1387 Diag(Prop->getLocation(), diag::note_property_declare);
1388 continue;
1389 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001390
1391 // We use invalid SourceLocations for the synthesized ivars since they
1392 // aren't really synthesized at a particular location; they just exist.
1393 // Saying that they are located at the @implementation isn't really going
1394 // to help users.
1395 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001396 true,
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001397 /* property = */ Prop->getIdentifier(),
1398 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001399 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001400 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001401}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001402
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001403void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1404 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1405 return;
1406 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1407 if (!IC)
1408 return;
1409 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001410 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001411 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001412}
1413
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001414void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001415 ObjCContainerDecl *CDecl,
1416 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001417 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1418 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1419 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1420
Ted Kremenek9d64c152010-03-12 00:38:38 +00001421 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001422 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001423 if (PropMap.empty())
1424 return;
1425
1426 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1427 for (ObjCImplDecl::propimpl_iterator
1428 I = IMPDecl->propimpl_begin(),
1429 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1430 PropImplMap.insert((*I)->getPropertyDecl());
1431
1432 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1433 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1434 ObjCPropertyDecl *Prop = P->second;
1435 // Is there a matching propery synthesize/dynamic?
1436 if (Prop->isInvalidDecl() ||
1437 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001438 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001439 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001440 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001441 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001442 isa<ObjCCategoryDecl>(CDecl) ?
1443 diag::warn_setter_getter_impl_required_in_category :
1444 diag::warn_setter_getter_impl_required)
1445 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001446 Diag(Prop->getLocation(),
1447 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001448 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1449 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001450 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001451 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1452
Ted Kremenek9d64c152010-03-12 00:38:38 +00001453 }
1454
1455 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001456 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001457 isa<ObjCCategoryDecl>(CDecl) ?
1458 diag::warn_setter_getter_impl_required_in_category :
1459 diag::warn_setter_getter_impl_required)
1460 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001461 Diag(Prop->getLocation(),
1462 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001463 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1464 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001465 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001466 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001467 }
1468 }
1469}
1470
1471void
1472Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1473 ObjCContainerDecl* IDecl) {
1474 // Rules apply in non-GC mode only
Douglas Gregore289d812011-09-13 17:21:33 +00001475 if (getLangOptions().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001476 return;
1477 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1478 E = IDecl->prop_end();
1479 I != E; ++I) {
1480 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001481 ObjCMethodDecl *GetterMethod = 0;
1482 ObjCMethodDecl *SetterMethod = 0;
1483 bool LookedUpGetterSetter = false;
1484
Ted Kremenek9d64c152010-03-12 00:38:38 +00001485 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001486 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001487
John McCall265941b2011-09-13 18:31:23 +00001488 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1489 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001490 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1491 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1492 LookedUpGetterSetter = true;
1493 if (GetterMethod) {
1494 Diag(GetterMethod->getLocation(),
1495 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001496 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001497 Diag(Property->getLocation(), diag::note_property_declare);
1498 }
1499 if (SetterMethod) {
1500 Diag(SetterMethod->getLocation(),
1501 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001502 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001503 Diag(Property->getLocation(), diag::note_property_declare);
1504 }
1505 }
1506
Ted Kremenek9d64c152010-03-12 00:38:38 +00001507 // We only care about readwrite atomic property.
1508 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1509 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1510 continue;
1511 if (const ObjCPropertyImplDecl *PIDecl
1512 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1513 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1514 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001515 if (!LookedUpGetterSetter) {
1516 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1517 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1518 LookedUpGetterSetter = true;
1519 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001520 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1521 SourceLocation MethodLoc =
1522 (GetterMethod ? GetterMethod->getLocation()
1523 : SetterMethod->getLocation());
1524 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001525 << Property->getIdentifier() << (GetterMethod != 0)
1526 << (SetterMethod != 0);
1527 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001528 Diag(Property->getLocation(), diag::note_property_declare);
1529 }
1530 }
1531 }
1532}
1533
John McCallf85e1932011-06-15 23:02:42 +00001534void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
Douglas Gregore289d812011-09-13 17:21:33 +00001535 if (getLangOptions().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001536 return;
1537
1538 for (ObjCImplementationDecl::propimpl_iterator
1539 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1540 ObjCPropertyImplDecl *PID = *i;
1541 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1542 continue;
1543
1544 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001545 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1546 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001547 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1548 if (!method)
1549 continue;
1550 ObjCMethodFamily family = method->getMethodFamily();
1551 if (family == OMF_alloc || family == OMF_copy ||
1552 family == OMF_mutableCopy || family == OMF_new) {
1553 if (getLangOptions().ObjCAutoRefCount)
1554 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1555 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001556 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001557 Diag(PD->getLocation(), diag::note_property_declare);
1558 }
1559 }
1560 }
1561}
1562
John McCall5de74d12010-11-10 07:01:40 +00001563/// AddPropertyAttrs - Propagates attributes from a property to the
1564/// implicitly-declared getter or setter for that property.
1565static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1566 ObjCPropertyDecl *Property) {
1567 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001568 for (Decl::attr_iterator A = Property->attr_begin(),
1569 AEnd = Property->attr_end();
1570 A != AEnd; ++A) {
1571 if (isa<DeprecatedAttr>(*A) ||
1572 isa<UnavailableAttr>(*A) ||
1573 isa<AvailabilityAttr>(*A))
1574 PropertyMethod->addAttr((*A)->clone(S.Context));
1575 }
John McCall5de74d12010-11-10 07:01:40 +00001576}
1577
Ted Kremenek9d64c152010-03-12 00:38:38 +00001578/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1579/// have the property type and issue diagnostics if they don't.
1580/// Also synthesize a getter/setter method if none exist (and update the
1581/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1582/// methods is the "right" thing to do.
1583void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001584 ObjCContainerDecl *CD,
1585 ObjCPropertyDecl *redeclaredProperty,
1586 ObjCContainerDecl *lexicalDC) {
1587
Ted Kremenek9d64c152010-03-12 00:38:38 +00001588 ObjCMethodDecl *GetterMethod, *SetterMethod;
1589
1590 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1591 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1592 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1593 property->getLocation());
1594
1595 if (SetterMethod) {
1596 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1597 property->getPropertyAttributes();
1598 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1599 Context.getCanonicalType(SetterMethod->getResultType()) !=
1600 Context.VoidTy)
1601 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1602 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001603 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001604 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1605 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001606 Diag(property->getLocation(),
1607 diag::warn_accessor_property_type_mismatch)
1608 << property->getDeclName()
1609 << SetterMethod->getSelector();
1610 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1611 }
1612 }
1613
1614 // Synthesize getter/setter methods if none exist.
1615 // Find the default getter and if one not found, add one.
1616 // FIXME: The synthesized property we set here is misleading. We almost always
1617 // synthesize these methods unless the user explicitly provided prototypes
1618 // (which is odd, but allowed). Sema should be typechecking that the
1619 // declarations jive in that situation (which it is not currently).
1620 if (!GetterMethod) {
1621 // No instance method of same name as property getter name was found.
1622 // Declare a getter method and add it to the list of methods
1623 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001624 SourceLocation Loc = redeclaredProperty ?
1625 redeclaredProperty->getLocation() :
1626 property->getLocation();
1627
1628 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1629 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001630 property->getType(), 0, CD, /*isInstance=*/true,
1631 /*isVariadic=*/false, /*isSynthesized=*/true,
1632 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001633 (property->getPropertyImplementation() ==
1634 ObjCPropertyDecl::Optional) ?
1635 ObjCMethodDecl::Optional :
1636 ObjCMethodDecl::Required);
1637 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001638
1639 AddPropertyAttrs(*this, GetterMethod, property);
1640
Ted Kremenek23173d72010-05-18 21:09:07 +00001641 // FIXME: Eventually this shouldn't be needed, as the lexical context
1642 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001643 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001644 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001645 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1646 GetterMethod->addAttr(
1647 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001648 } else
1649 // A user declared getter will be synthesize when @synthesize of
1650 // the property with the same name is seen in the @implementation
1651 GetterMethod->setSynthesized(true);
1652 property->setGetterMethodDecl(GetterMethod);
1653
1654 // Skip setter if property is read-only.
1655 if (!property->isReadOnly()) {
1656 // Find the default setter and if one not found, add one.
1657 if (!SetterMethod) {
1658 // No instance method of same name as property setter name was found.
1659 // Declare a setter method and add it to the list of methods
1660 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001661 SourceLocation Loc = redeclaredProperty ?
1662 redeclaredProperty->getLocation() :
1663 property->getLocation();
1664
1665 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001666 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001667 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001668 CD, /*isInstance=*/true, /*isVariadic=*/false,
1669 /*isSynthesized=*/true,
1670 /*isImplicitlyDeclared=*/true,
1671 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001672 (property->getPropertyImplementation() ==
1673 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001674 ObjCMethodDecl::Optional :
1675 ObjCMethodDecl::Required);
1676
Ted Kremenek9d64c152010-03-12 00:38:38 +00001677 // Invent the arguments for the setter. We don't bother making a
1678 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001679 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1680 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001681 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001682 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001683 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001684 SC_None,
1685 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001686 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001687 SetterMethod->setMethodParams(Context, Argument,
1688 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001689
1690 AddPropertyAttrs(*this, SetterMethod, property);
1691
Ted Kremenek9d64c152010-03-12 00:38:38 +00001692 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001693 // FIXME: Eventually this shouldn't be needed, as the lexical context
1694 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001695 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001696 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001697 } else
1698 // A user declared setter will be synthesize when @synthesize of
1699 // the property with the same name is seen in the @implementation
1700 SetterMethod->setSynthesized(true);
1701 property->setSetterMethodDecl(SetterMethod);
1702 }
1703 // Add any synthesized methods to the global pool. This allows us to
1704 // handle the following, which is supported by GCC (and part of the design).
1705 //
1706 // @interface Foo
1707 // @property double bar;
1708 // @end
1709 //
1710 // void thisIsUnfortunate() {
1711 // id foo;
1712 // double bar = [foo bar];
1713 // }
1714 //
1715 if (GetterMethod)
1716 AddInstanceMethodToGlobalPool(GetterMethod);
1717 if (SetterMethod)
1718 AddInstanceMethodToGlobalPool(SetterMethod);
1719}
1720
John McCalld226f652010-08-21 09:40:31 +00001721void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001722 SourceLocation Loc,
1723 unsigned &Attributes) {
1724 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001725 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001726 return;
1727
1728 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001729 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001730
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001731 if (getLangOptions().ObjCAutoRefCount &&
1732 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1733 PropertyTy->isObjCRetainableType()) {
1734 // 'readonly' property with no obvious lifetime.
1735 // its life time will be determined by its backing ivar.
1736 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1737 ObjCDeclSpec::DQ_PR_copy |
1738 ObjCDeclSpec::DQ_PR_retain |
1739 ObjCDeclSpec::DQ_PR_strong |
1740 ObjCDeclSpec::DQ_PR_weak |
1741 ObjCDeclSpec::DQ_PR_assign);
1742 if ((Attributes & rel) == 0)
1743 return;
1744 }
1745
Ted Kremenek9d64c152010-03-12 00:38:38 +00001746 // readonly and readwrite/assign/retain/copy conflict.
1747 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1748 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1749 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001750 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001751 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001752 ObjCDeclSpec::DQ_PR_retain |
1753 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001754 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1755 "readwrite" :
1756 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1757 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001758 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1759 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001760 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1761 "copy" : "retain";
1762
1763 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1764 diag::err_objc_property_attr_mutually_exclusive :
1765 diag::warn_objc_property_attr_mutually_exclusive)
1766 << "readonly" << which;
1767 }
1768
1769 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001770 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1771 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1772 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001773 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001774 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001775 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1776 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1777 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1778 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001779 }
1780
1781 // Check for more than one of { assign, copy, retain }.
1782 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1783 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1784 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1785 << "assign" << "copy";
1786 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1787 }
1788 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1789 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1790 << "assign" << "retain";
1791 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1792 }
John McCallf85e1932011-06-15 23:02:42 +00001793 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1794 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1795 << "assign" << "strong";
1796 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1797 }
1798 if (getLangOptions().ObjCAutoRefCount &&
1799 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1800 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1801 << "assign" << "weak";
1802 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1803 }
1804 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1805 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1806 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1807 << "unsafe_unretained" << "copy";
1808 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1809 }
1810 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1811 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1812 << "unsafe_unretained" << "retain";
1813 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1814 }
1815 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1816 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1817 << "unsafe_unretained" << "strong";
1818 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1819 }
1820 if (getLangOptions().ObjCAutoRefCount &&
1821 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1822 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1823 << "unsafe_unretained" << "weak";
1824 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1825 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001826 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1827 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1828 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1829 << "copy" << "retain";
1830 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1831 }
John McCallf85e1932011-06-15 23:02:42 +00001832 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1833 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1834 << "copy" << "strong";
1835 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1836 }
1837 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1838 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1839 << "copy" << "weak";
1840 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1841 }
1842 }
1843 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1844 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1845 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1846 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001847 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001848 }
1849 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1850 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1851 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1852 << "strong" << "weak";
1853 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001854 }
1855
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00001856 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
1857 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
1858 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1859 << "atomic" << "nonatomic";
1860 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
1861 }
1862
Ted Kremenek9d64c152010-03-12 00:38:38 +00001863 // Warn if user supplied no assignment attribute, property is
1864 // readwrite, and this is an object type.
1865 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001866 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1867 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1868 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001869 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001870 if (getLangOptions().ObjCAutoRefCount)
1871 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001872 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001873 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001874 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001875 bool isAnyClassTy =
1876 (PropertyTy->isObjCClassType() ||
1877 PropertyTy->isObjCQualifiedClassType());
1878 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
1879 // issue any warning.
1880 if (isAnyClassTy && getLangOptions().getGC() == LangOptions::NonGC)
1881 ;
1882 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001883 // Skip this warning in gc-only mode.
Douglas Gregore289d812011-09-13 17:21:33 +00001884 if (getLangOptions().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001885 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001886
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001887 // If non-gc code warn that this is likely inappropriate.
Douglas Gregore289d812011-09-13 17:21:33 +00001888 if (getLangOptions().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001889 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001890 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001891 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001892
1893 // FIXME: Implement warning dependent on NSCopying being
1894 // implemented. See also:
1895 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1896 // (please trim this list while you are at it).
1897 }
1898
1899 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001900 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Douglas Gregore289d812011-09-13 17:21:33 +00001901 && getLangOptions().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001902 && PropertyTy->isBlockPointerType())
1903 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001904 else if (getLangOptions().ObjCAutoRefCount &&
1905 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1906 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1907 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1908 PropertyTy->isBlockPointerType())
1909 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00001910
1911 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1912 (Attributes & ObjCDeclSpec::DQ_PR_setter))
1913 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
1914
Ted Kremenek9d64c152010-03-12 00:38:38 +00001915}