blob: 7f6d8c34f4ce022f87f96bdaf7409c1ca160187e [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;
282 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
283 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
284 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
285 ConvertedType, IncompatibleObjC))
286 || IncompatibleObjC) {
287 Diag(AtLoc,
288 diag::err_type_mismatch_continuation_class) << PDecl->getType();
289 Diag(PIDecl->getLocation(), diag::note_property_declare);
290 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000291 }
292
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000293 // The property 'PIDecl's readonly attribute will be over-ridden
294 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000295 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000296 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
297 unsigned retainCopyNonatomic =
298 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000299 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000300 ObjCPropertyDecl::OBJC_PR_copy |
301 ObjCPropertyDecl::OBJC_PR_nonatomic);
302 if ((Attributes & retainCopyNonatomic) !=
303 (PIkind & retainCopyNonatomic)) {
304 Diag(AtLoc, diag::warn_property_attr_mismatch);
305 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000306 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000307 DeclContext *DC = cast<DeclContext>(CCPrimary);
308 if (!ObjCPropertyDecl::findPropertyDecl(DC,
309 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000310 // Protocol is not in the primary class. Must build one for it.
311 ObjCDeclSpec ProtocolPropertyODS;
312 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
313 // and ObjCPropertyDecl::PropertyAttributeKind have identical
314 // values. Should consolidate both into one enum type.
315 ProtocolPropertyODS.
316 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
317 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000318 // Must re-establish the context from class extension to primary
319 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000320 ContextRAII SavedContext(*this, CCPrimary);
321
John McCalld226f652010-08-21 09:40:31 +0000322 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000323 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
324 PIDecl->getGetterName(),
325 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000326 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000327 MethodImplKind,
328 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000329 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000330 }
331 PIDecl->makeitReadWriteAttribute();
332 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
333 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000334 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
335 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000336 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
337 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
338 PIDecl->setSetterName(SetterSel);
339 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000340 // Tailor the diagnostics for the common case where a readwrite
341 // property is declared both in the @interface and the continuation.
342 // This is a common error where the user often intended the original
343 // declaration to be readonly.
344 unsigned diag =
345 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
346 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
347 ? diag::err_use_continuation_class_redeclaration_readwrite
348 : diag::err_use_continuation_class;
349 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000350 << CCPrimary->getDeclName();
351 Diag(PIDecl->getLocation(), diag::note_property_declare);
352 }
353 *isOverridingProperty = true;
354 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000355 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000356 if (ASTMutationListener *L = Context.getASTMutationListener())
357 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000358 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000359}
360
361ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
362 ObjCContainerDecl *CDecl,
363 SourceLocation AtLoc,
364 FieldDeclarator &FD,
365 Selector GetterSel,
366 Selector SetterSel,
367 const bool isAssign,
368 const bool isReadWrite,
369 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000370 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000371 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000372 tok::ObjCKeywordKind MethodImplKind,
373 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000374 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000375 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000376
377 // Issue a warning if property is 'assign' as default and its object, which is
378 // gc'able conforms to NSCopying protocol
Douglas Gregore289d812011-09-13 17:21:33 +0000379 if (getLangOptions().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000380 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000381 if (const ObjCObjectPointerType *ObjPtrTy =
382 T->getAs<ObjCObjectPointerType>()) {
383 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
384 if (IDecl)
385 if (ObjCProtocolDecl* PNSCopying =
386 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
387 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
388 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000389 }
John McCallc12c5bb2010-05-15 11:32:37 +0000390 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000391 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
392
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000393 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000394 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
395 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000396 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000397
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000398 if (ObjCPropertyDecl *prevDecl =
399 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000400 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000401 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000402 PDecl->setInvalidDecl();
403 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000404 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000405 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000406 if (lexicalDC)
407 PDecl->setLexicalDeclContext(lexicalDC);
408 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000409
410 if (T->isArrayType() || T->isFunctionType()) {
411 Diag(AtLoc, diag::err_property_type) << T;
412 PDecl->setInvalidDecl();
413 }
414
415 ProcessDeclAttributes(S, PDecl, FD.D);
416
417 // Regardless of setter/getter attribute, we save the default getter/setter
418 // selector names in anticipation of declaration of setter/getter methods.
419 PDecl->setGetterName(GetterSel);
420 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000421 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000422 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000423
Ted Kremenek28685ab2010-03-12 00:46:40 +0000424 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
425 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
426
427 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
428 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
429
430 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
431 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
432
433 if (isReadWrite)
434 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
435
436 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
437 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
438
John McCallf85e1932011-06-15 23:02:42 +0000439 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
440 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
441
442 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
443 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
444
Ted Kremenek28685ab2010-03-12 00:46:40 +0000445 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
446 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
447
John McCallf85e1932011-06-15 23:02:42 +0000448 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
449 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
450
Ted Kremenek28685ab2010-03-12 00:46:40 +0000451 if (isAssign)
452 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
453
John McCall265941b2011-09-13 18:31:23 +0000454 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000455 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
456 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000457 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000458 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000459
John McCallf85e1932011-06-15 23:02:42 +0000460 // 'unsafe_unretained' is alias for 'assign'.
461 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
462 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
463 if (isAssign)
464 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
465
Ted Kremenek28685ab2010-03-12 00:46:40 +0000466 if (MethodImplKind == tok::objc_required)
467 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
468 else if (MethodImplKind == tok::objc_optional)
469 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000470
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000471 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000472}
473
John McCallf85e1932011-06-15 23:02:42 +0000474static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
475 ObjCPropertyDecl *property,
476 ObjCIvarDecl *ivar) {
477 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
478
John McCallf85e1932011-06-15 23:02:42 +0000479 QualType ivarType = ivar->getType();
480 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000481
John McCall265941b2011-09-13 18:31:23 +0000482 // The lifetime implied by the property's attributes.
483 Qualifiers::ObjCLifetime propertyLifetime =
484 getImpliedARCOwnership(property->getPropertyAttributes(),
485 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000486
John McCall265941b2011-09-13 18:31:23 +0000487 // We're fine if they match.
488 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000489
John McCall265941b2011-09-13 18:31:23 +0000490 // These aren't valid lifetimes for object ivars; don't diagnose twice.
491 if (ivarLifetime == Qualifiers::OCL_None ||
492 ivarLifetime == Qualifiers::OCL_Autoreleasing)
493 return;
John McCallf85e1932011-06-15 23:02:42 +0000494
John McCall265941b2011-09-13 18:31:23 +0000495 switch (propertyLifetime) {
496 case Qualifiers::OCL_Strong:
497 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
498 << property->getDeclName()
499 << ivar->getDeclName()
500 << ivarLifetime;
501 break;
John McCallf85e1932011-06-15 23:02:42 +0000502
John McCall265941b2011-09-13 18:31:23 +0000503 case Qualifiers::OCL_Weak:
504 S.Diag(propertyImplLoc, diag::error_weak_property)
505 << property->getDeclName()
506 << ivar->getDeclName();
507 break;
John McCallf85e1932011-06-15 23:02:42 +0000508
John McCall265941b2011-09-13 18:31:23 +0000509 case Qualifiers::OCL_ExplicitNone:
510 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
511 << property->getDeclName()
512 << ivar->getDeclName()
513 << ((property->getPropertyAttributesAsWritten()
514 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
515 break;
John McCallf85e1932011-06-15 23:02:42 +0000516
John McCall265941b2011-09-13 18:31:23 +0000517 case Qualifiers::OCL_Autoreleasing:
518 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000519
John McCall265941b2011-09-13 18:31:23 +0000520 case Qualifiers::OCL_None:
521 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000522 return;
523 }
524
525 S.Diag(property->getLocation(), diag::note_property_declare);
526}
527
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000528/// setImpliedPropertyAttributeForReadOnlyProperty -
529/// This routine evaludates life-time attributes for a 'readonly'
530/// property with no known lifetime of its own, using backing
531/// 'ivar's attribute, if any. If no backing 'ivar', property's
532/// life-time is assumed 'strong'.
533static void setImpliedPropertyAttributeForReadOnlyProperty(
534 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
535 Qualifiers::ObjCLifetime propertyLifetime =
536 getImpliedARCOwnership(property->getPropertyAttributes(),
537 property->getType());
538 if (propertyLifetime != Qualifiers::OCL_None)
539 return;
540
541 if (!ivar) {
542 // if no backing ivar, make property 'strong'.
543 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
544 return;
545 }
546 // property assumes owenership of backing ivar.
547 QualType ivarType = ivar->getType();
548 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
549 if (ivarLifetime == Qualifiers::OCL_Strong)
550 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
551 else if (ivarLifetime == Qualifiers::OCL_Weak)
552 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
553 return;
554}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000555
556/// ActOnPropertyImplDecl - This routine performs semantic checks and
557/// builds the AST node for a property implementation declaration; declared
558/// as @synthesize or @dynamic.
559///
John McCalld226f652010-08-21 09:40:31 +0000560Decl *Sema::ActOnPropertyImplDecl(Scope *S,
561 SourceLocation AtLoc,
562 SourceLocation PropertyLoc,
563 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000564 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000565 IdentifierInfo *PropertyIvar,
566 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000567 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000568 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569 // Make sure we have a context for the property implementation declaration.
570 if (!ClassImpDecl) {
571 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000572 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000573 }
574 ObjCPropertyDecl *property = 0;
575 ObjCInterfaceDecl* IDecl = 0;
576 // Find the class or category class where this property must have
577 // a declaration.
578 ObjCImplementationDecl *IC = 0;
579 ObjCCategoryImplDecl* CatImplClass = 0;
580 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
581 IDecl = IC->getClassInterface();
582 // We always synthesize an interface for an implementation
583 // without an interface decl. So, IDecl is always non-zero.
584 assert(IDecl &&
585 "ActOnPropertyImplDecl - @implementation without @interface");
586
587 // Look for this property declaration in the @implementation's @interface
588 property = IDecl->FindPropertyDeclaration(PropertyId);
589 if (!property) {
590 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000591 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000592 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000593 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000594 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
595 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000596 if (AtLoc.isValid())
597 Diag(AtLoc, diag::warn_implicit_atomic_property);
598 else
599 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
600 Diag(property->getLocation(), diag::note_property_declare);
601 }
602
Ted Kremenek28685ab2010-03-12 00:46:40 +0000603 if (const ObjCCategoryDecl *CD =
604 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
605 if (!CD->IsClassExtension()) {
606 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
607 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000608 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000609 }
610 }
611 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
612 if (Synthesize) {
613 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000614 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000615 }
616 IDecl = CatImplClass->getClassInterface();
617 if (!IDecl) {
618 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000619 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000620 }
621 ObjCCategoryDecl *Category =
622 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
623
624 // If category for this implementation not found, it is an error which
625 // has already been reported eralier.
626 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000627 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000628 // Look for this property declaration in @implementation's category
629 property = Category->FindPropertyDeclaration(PropertyId);
630 if (!property) {
631 Diag(PropertyLoc, diag::error_bad_category_property_decl)
632 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000633 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000634 }
635 } else {
636 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000637 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000638 }
639 ObjCIvarDecl *Ivar = 0;
640 // Check that we have a valid, previously declared ivar for @synthesize
641 if (Synthesize) {
642 // @synthesize
643 if (!PropertyIvar)
644 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000645 // Check that this is a previously declared 'ivar' in 'IDecl' interface
646 ObjCInterfaceDecl *ClassDeclared;
647 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
648 QualType PropType = property->getType();
649 QualType PropertyIvarType = PropType.getNonReferenceType();
650
651 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000652 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000653 ObjCPropertyDecl::OBJC_PR_readonly) &&
654 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000655 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
656 }
657
John McCallf85e1932011-06-15 23:02:42 +0000658 ObjCPropertyDecl::PropertyAttributeKind kind
659 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000660
661 // Add GC __weak to the ivar type if the property is weak.
662 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
Douglas Gregore289d812011-09-13 17:21:33 +0000663 getLangOptions().getGC() != LangOptions::NonGC) {
John McCall265941b2011-09-13 18:31:23 +0000664 assert(!getLangOptions().ObjCAutoRefCount);
665 if (PropertyIvarType.isObjCGCStrong()) {
666 Diag(PropertyLoc, diag::err_gc_weak_property_strong_type);
667 Diag(property->getLocation(), diag::note_property_declare);
668 } else {
669 PropertyIvarType =
670 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000671 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000672 }
John McCall265941b2011-09-13 18:31:23 +0000673
Ted Kremenek28685ab2010-03-12 00:46:40 +0000674 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000675 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000676 // property attributes.
677 if (getLangOptions().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000678 !PropertyIvarType.getObjCLifetime() &&
679 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000680
John McCall265941b2011-09-13 18:31:23 +0000681 // It's an error if we have to do this and the user didn't
682 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000683 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000684 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000685 Diag(PropertyLoc,
686 diag::err_arc_objc_property_default_assign_on_object);
687 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000688 } else {
689 Qualifiers::ObjCLifetime lifetime =
690 getImpliedARCOwnership(kind, PropertyIvarType);
691 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000692 if (lifetime == Qualifiers::OCL_Weak) {
693 bool err = false;
694 if (const ObjCObjectPointerType *ObjT =
695 PropertyIvarType->getAs<ObjCObjectPointerType>())
696 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
697 Diag(PropertyLoc, diag::err_arc_weak_unavailable_property);
698 Diag(property->getLocation(), diag::note_property_declare);
699 err = true;
700 }
701 if (!err && !getLangOptions().ObjCRuntimeHasWeak) {
702 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
703 Diag(property->getLocation(), diag::note_property_declare);
704 }
John McCallf85e1932011-06-15 23:02:42 +0000705 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000706
John McCallf85e1932011-06-15 23:02:42 +0000707 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000708 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000709 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
710 }
John McCallf85e1932011-06-15 23:02:42 +0000711 }
712
713 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
714 !getLangOptions().ObjCAutoRefCount &&
Douglas Gregore289d812011-09-13 17:21:33 +0000715 getLangOptions().getGC() == LangOptions::NonGC) {
John McCallf85e1932011-06-15 23:02:42 +0000716 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
717 Diag(property->getLocation(), diag::note_property_declare);
718 }
719
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000720 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
721 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000722 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000723 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000724 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000725 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000726 IDecl->makeDeclVisibleInContext(Ivar, false);
727 property->setPropertyIvarDecl(Ivar);
728
729 if (!getLangOptions().ObjCNonFragileABI)
730 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
731 // Note! I deliberately want it to fall thru so, we have a
732 // a property implementation and to avoid future warnings.
733 } else if (getLangOptions().ObjCNonFragileABI &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000734 !declaresSameEntity(ClassDeclared, IDecl)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000735 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
736 << property->getDeclName() << Ivar->getDeclName()
737 << ClassDeclared->getDeclName();
738 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000739 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000740 // Note! I deliberately want it to fall thru so more errors are caught.
741 }
742 QualType IvarType = Context.getCanonicalType(Ivar->getType());
743
744 // Check that type of property and its ivar are type compatible.
John McCall265941b2011-09-13 18:31:23 +0000745 if (Context.getCanonicalType(PropertyIvarType) != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000746 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000747 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000748 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000749 compat =
750 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000751 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000752 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000753 else {
754 SourceLocation Loc = PropertyIvarLoc;
755 if (Loc.isInvalid())
756 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000757 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000758 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000759 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000760 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000761 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000762 << property->getDeclName() << PropType
763 << Ivar->getDeclName() << IvarType;
764 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000765 // Note! I deliberately want it to fall thru so, we have a
766 // a property implementation and to avoid future warnings.
767 }
768
769 // FIXME! Rules for properties are somewhat different that those
770 // for assignments. Use a new routine to consolidate all cases;
771 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000772 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000773 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
774 if (lhsType != rhsType &&
775 lhsType->isArithmeticType()) {
776 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000777 << property->getDeclName() << PropType
778 << Ivar->getDeclName() << IvarType;
779 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000780 // Fall thru - see previous comment
781 }
782 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000783 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000784 getLangOptions().getGC() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000785 Diag(PropertyLoc, diag::error_weak_property)
786 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000787 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000788 // Fall thru - see previous comment
789 }
John McCallf85e1932011-06-15 23:02:42 +0000790 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000791 if ((property->getType()->isObjCObjectPointerType() ||
792 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000793 getLangOptions().getGC() != LangOptions::NonGC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000794 Diag(PropertyLoc, diag::error_strong_property)
795 << property->getDeclName() << Ivar->getDeclName();
796 // Fall thru - see previous comment
797 }
798 }
John McCallf85e1932011-06-15 23:02:42 +0000799 if (getLangOptions().ObjCAutoRefCount)
800 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000801 } else if (PropertyIvar)
802 // @dynamic
803 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000804
Ted Kremenek28685ab2010-03-12 00:46:40 +0000805 assert (property && "ActOnPropertyImplDecl - property declaration missing");
806 ObjCPropertyImplDecl *PIDecl =
807 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
808 property,
809 (Synthesize ?
810 ObjCPropertyImplDecl::Synthesize
811 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000812 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000813 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
814 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000815 if (getLangOptions().CPlusPlus && Synthesize &&
816 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000817 // For Objective-C++, need to synthesize the AST for the IVAR object to be
818 // returned by the getter as it must conform to C++'s copy-return rules.
819 // FIXME. Eventually we want to do this for Objective-C as well.
820 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
821 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000822 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
823 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000824 Expr *IvarRefExpr =
825 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
826 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000827 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000828 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000829 SourceLocation(),
830 getterMethod->getResultType(),
831 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000832 SourceLocation(),
833 Owned(IvarRefExpr));
834 if (!Res.isInvalid()) {
835 Expr *ResExpr = Res.takeAs<Expr>();
836 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000837 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000838 PIDecl->setGetterCXXConstructor(ResExpr);
839 }
840 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000841 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
842 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
843 Diag(getterMethod->getLocation(),
844 diag::warn_property_getter_owning_mismatch);
845 Diag(property->getLocation(), diag::note_property_declare);
846 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000847 }
848 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
849 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000850 if (getLangOptions().CPlusPlus && Synthesize
851 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000852 // FIXME. Eventually we want to do this for Objective-C as well.
853 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
854 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000855 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
856 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000857 Expr *lhs =
858 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
859 SelfExpr, true, true);
860 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
861 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +0000862 QualType T = Param->getType().getNonReferenceType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000863 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000864 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000865 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000866 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000867 if (property->getPropertyAttributes() &
868 ObjCPropertyDecl::OBJC_PR_atomic) {
869 Expr *callExpr = Res.takeAs<Expr>();
870 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000871 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
872 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000873 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000874 if (property->getType()->isReferenceType()) {
875 Diag(PropertyLoc,
876 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000877 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000878 Diag(FuncDecl->getLocStart(),
879 diag::note_callee_decl) << FuncDecl;
880 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000881 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000882 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
883 }
884 }
885
Ted Kremenek28685ab2010-03-12 00:46:40 +0000886 if (IC) {
887 if (Synthesize)
888 if (ObjCPropertyImplDecl *PPIDecl =
889 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
890 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
891 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
892 << PropertyIvar;
893 Diag(PPIDecl->getLocation(), diag::note_previous_use);
894 }
895
896 if (ObjCPropertyImplDecl *PPIDecl
897 = IC->FindPropertyImplDecl(PropertyId)) {
898 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
899 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000900 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000901 }
902 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000903 if (getLangOptions().ObjCDefaultSynthProperties &&
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +0000904 getLangOptions().ObjCNonFragileABI2 &&
Ted Kremenek71207fc2012-01-05 22:47:47 +0000905 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000906 // Diagnose if an ivar was lazily synthesdized due to a previous
907 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000908 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000909 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000910 ObjCIvarDecl *Ivar = 0;
911 if (!Synthesize)
912 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
913 else {
914 if (PropertyIvar && PropertyIvar != PropertyId)
915 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
916 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000917 // Issue diagnostics only if Ivar belongs to current class.
918 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000919 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000920 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
921 << PropertyId;
922 Ivar->setInvalidDecl();
923 }
924 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000925 } else {
926 if (Synthesize)
927 if (ObjCPropertyImplDecl *PPIDecl =
928 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
929 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
930 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
931 << PropertyIvar;
932 Diag(PPIDecl->getLocation(), diag::note_previous_use);
933 }
934
935 if (ObjCPropertyImplDecl *PPIDecl =
936 CatImplClass->FindPropertyImplDecl(PropertyId)) {
937 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
938 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000939 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000940 }
941 CatImplClass->addPropertyImplementation(PIDecl);
942 }
943
John McCalld226f652010-08-21 09:40:31 +0000944 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000945}
946
947//===----------------------------------------------------------------------===//
948// Helper methods.
949//===----------------------------------------------------------------------===//
950
Ted Kremenek9d64c152010-03-12 00:38:38 +0000951/// DiagnosePropertyMismatch - Compares two properties for their
952/// attributes and types and warns on a variety of inconsistencies.
953///
954void
955Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
956 ObjCPropertyDecl *SuperProperty,
957 const IdentifierInfo *inheritedName) {
958 ObjCPropertyDecl::PropertyAttributeKind CAttr =
959 Property->getPropertyAttributes();
960 ObjCPropertyDecl::PropertyAttributeKind SAttr =
961 SuperProperty->getPropertyAttributes();
962 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
963 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
964 Diag(Property->getLocation(), diag::warn_readonly_property)
965 << Property->getDeclName() << inheritedName;
966 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
967 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
968 Diag(Property->getLocation(), diag::warn_property_attribute)
969 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +0000970 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +0000971 unsigned CAttrRetain =
972 (CAttr &
973 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
974 unsigned SAttrRetain =
975 (SAttr &
976 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
977 bool CStrong = (CAttrRetain != 0);
978 bool SStrong = (SAttrRetain != 0);
979 if (CStrong != SStrong)
980 Diag(Property->getLocation(), diag::warn_property_attribute)
981 << Property->getDeclName() << "retain (or strong)" << inheritedName;
982 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000983
984 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
985 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
986 Diag(Property->getLocation(), diag::warn_property_attribute)
987 << Property->getDeclName() << "atomic" << inheritedName;
988 if (Property->getSetterName() != SuperProperty->getSetterName())
989 Diag(Property->getLocation(), diag::warn_property_attribute)
990 << Property->getDeclName() << "setter" << inheritedName;
991 if (Property->getGetterName() != SuperProperty->getGetterName())
992 Diag(Property->getLocation(), diag::warn_property_attribute)
993 << Property->getDeclName() << "getter" << inheritedName;
994
995 QualType LHSType =
996 Context.getCanonicalType(SuperProperty->getType());
997 QualType RHSType =
998 Context.getCanonicalType(Property->getType());
999
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001000 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001001 // Do cases not handled in above.
1002 // FIXME. For future support of covariant property types, revisit this.
1003 bool IncompatibleObjC = false;
1004 QualType ConvertedType;
1005 if (!isObjCPointerConversion(RHSType, LHSType,
1006 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001007 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001008 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1009 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001010 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1011 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001012 }
1013}
1014
1015bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1016 ObjCMethodDecl *GetterMethod,
1017 SourceLocation Loc) {
1018 if (GetterMethod &&
John McCall3c3b7f92011-10-25 17:37:35 +00001019 !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(),
1020 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001021 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +00001022 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +00001023 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +00001024 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001025 if (result != Compatible) {
1026 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1027 << property->getDeclName()
1028 << GetterMethod->getSelector();
1029 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1030 return true;
1031 }
1032 }
1033 return false;
1034}
1035
1036/// ComparePropertiesInBaseAndSuper - This routine compares property
1037/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001038/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001039///
1040void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1041 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1042 if (!SDecl)
1043 return;
1044 // FIXME: O(N^2)
1045 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1046 E = SDecl->prop_end(); S != E; ++S) {
1047 ObjCPropertyDecl *SuperPDecl = (*S);
1048 // Does property in super class has declaration in current class?
1049 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1050 E = IDecl->prop_end(); I != E; ++I) {
1051 ObjCPropertyDecl *PDecl = (*I);
1052 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1053 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1054 SDecl->getIdentifier());
1055 }
1056 }
1057}
1058
1059/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1060/// of properties declared in a protocol and compares their attribute against
1061/// the same property declared in the class or category.
1062void
1063Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1064 ObjCProtocolDecl *PDecl) {
1065 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1066 if (!IDecl) {
1067 // Category
1068 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1069 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1070 if (!CatDecl->IsClassExtension())
1071 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1072 E = PDecl->prop_end(); P != E; ++P) {
1073 ObjCPropertyDecl *Pr = (*P);
1074 ObjCCategoryDecl::prop_iterator CP, CE;
1075 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001076 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001077 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1078 break;
1079 if (CP != CE)
1080 // Property protocol already exist in class. Diagnose any mismatch.
1081 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1082 }
1083 return;
1084 }
1085 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1086 E = PDecl->prop_end(); P != E; ++P) {
1087 ObjCPropertyDecl *Pr = (*P);
1088 ObjCInterfaceDecl::prop_iterator CP, CE;
1089 // Is this property already in class's list of properties?
1090 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1091 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1092 break;
1093 if (CP != CE)
1094 // Property protocol already exist in class. Diagnose any mismatch.
1095 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1096 }
1097}
1098
1099/// CompareProperties - This routine compares properties
1100/// declared in 'ClassOrProtocol' objects (which can be a class or an
1101/// inherited protocol with the list of properties for class/category 'CDecl'
1102///
John McCalld226f652010-08-21 09:40:31 +00001103void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1104 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001105 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1106
1107 if (!IDecl) {
1108 // Category
1109 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1110 assert (CatDecl && "CompareProperties");
1111 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1112 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1113 E = MDecl->protocol_end(); P != E; ++P)
1114 // Match properties of category with those of protocol (*P)
1115 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1116
1117 // Go thru the list of protocols for this category and recursively match
1118 // their properties with those in the category.
1119 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1120 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001121 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001122 } else {
1123 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1124 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1125 E = MD->protocol_end(); P != E; ++P)
1126 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1127 }
1128 return;
1129 }
1130
1131 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001132 for (ObjCInterfaceDecl::all_protocol_iterator
1133 P = MDecl->all_referenced_protocol_begin(),
1134 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001135 // Match properties of class IDecl with those of protocol (*P).
1136 MatchOneProtocolPropertiesInClass(IDecl, *P);
1137
1138 // Go thru the list of protocols for this class and recursively match
1139 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001140 for (ObjCInterfaceDecl::all_protocol_iterator
1141 P = IDecl->all_referenced_protocol_begin(),
1142 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001143 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001144 } else {
1145 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1146 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1147 E = MD->protocol_end(); P != E; ++P)
1148 MatchOneProtocolPropertiesInClass(IDecl, *P);
1149 }
1150}
1151
1152/// isPropertyReadonly - Return true if property is readonly, by searching
1153/// for the property in the class and in its categories and implementations
1154///
1155bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1156 ObjCInterfaceDecl *IDecl) {
1157 // by far the most common case.
1158 if (!PDecl->isReadOnly())
1159 return false;
1160 // Even if property is ready only, if interface has a user defined setter,
1161 // it is not considered read only.
1162 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1163 return false;
1164
1165 // Main class has the property as 'readonly'. Must search
1166 // through the category list to see if the property's
1167 // attribute has been over-ridden to 'readwrite'.
1168 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1169 Category; Category = Category->getNextClassCategory()) {
1170 // Even if property is ready only, if a category has a user defined setter,
1171 // it is not considered read only.
1172 if (Category->getInstanceMethod(PDecl->getSetterName()))
1173 return false;
1174 ObjCPropertyDecl *P =
1175 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1176 if (P && !P->isReadOnly())
1177 return false;
1178 }
1179
1180 // Also, check for definition of a setter method in the implementation if
1181 // all else failed.
1182 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1183 if (ObjCImplementationDecl *IMD =
1184 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1185 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1186 return false;
1187 } else if (ObjCCategoryImplDecl *CIMD =
1188 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1189 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1190 return false;
1191 }
1192 }
1193 // Lastly, look through the implementation (if one is in scope).
1194 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1195 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1196 return false;
1197 // If all fails, look at the super class.
1198 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1199 return isPropertyReadonly(PDecl, SIDecl);
1200 return true;
1201}
1202
1203/// CollectImmediateProperties - This routine collects all properties in
1204/// the class and its conforming protocols; but not those it its super class.
1205void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001206 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1207 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001208 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1209 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1210 E = IDecl->prop_end(); P != E; ++P) {
1211 ObjCPropertyDecl *Prop = (*P);
1212 PropMap[Prop->getIdentifier()] = Prop;
1213 }
1214 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001215 for (ObjCInterfaceDecl::all_protocol_iterator
1216 PI = IDecl->all_referenced_protocol_begin(),
1217 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001218 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001219 }
1220 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1221 if (!CATDecl->IsClassExtension())
1222 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1223 E = CATDecl->prop_end(); P != E; ++P) {
1224 ObjCPropertyDecl *Prop = (*P);
1225 PropMap[Prop->getIdentifier()] = Prop;
1226 }
1227 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001228 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001229 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001230 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001231 }
1232 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1233 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1234 E = PDecl->prop_end(); P != E; ++P) {
1235 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001236 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1237 // Exclude property for protocols which conform to class's super-class,
1238 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001239 if (!PropertyFromSuper ||
1240 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001241 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1242 if (!PropEntry)
1243 PropEntry = Prop;
1244 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001245 }
1246 // scan through protocol's protocols.
1247 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1248 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001249 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001250 }
1251}
1252
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001253/// CollectClassPropertyImplementations - This routine collects list of
1254/// properties to be implemented in the class. This includes, class's
1255/// and its conforming protocols' properties.
1256static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1257 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1258 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1259 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1260 E = IDecl->prop_end(); P != E; ++P) {
1261 ObjCPropertyDecl *Prop = (*P);
1262 PropMap[Prop->getIdentifier()] = Prop;
1263 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001264 for (ObjCInterfaceDecl::all_protocol_iterator
1265 PI = IDecl->all_referenced_protocol_begin(),
1266 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001267 CollectClassPropertyImplementations((*PI), PropMap);
1268 }
1269 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1270 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1271 E = PDecl->prop_end(); P != E; ++P) {
1272 ObjCPropertyDecl *Prop = (*P);
1273 PropMap[Prop->getIdentifier()] = Prop;
1274 }
1275 // scan through protocol's protocols.
1276 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1277 E = PDecl->protocol_end(); PI != E; ++PI)
1278 CollectClassPropertyImplementations((*PI), PropMap);
1279 }
1280}
1281
1282/// CollectSuperClassPropertyImplementations - This routine collects list of
1283/// properties to be implemented in super class(s) and also coming from their
1284/// conforming protocols.
1285static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1286 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1287 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1288 while (SDecl) {
1289 CollectClassPropertyImplementations(SDecl, PropMap);
1290 SDecl = SDecl->getSuperClass();
1291 }
1292 }
1293}
1294
Ted Kremenek9d64c152010-03-12 00:38:38 +00001295/// LookupPropertyDecl - Looks up a property in the current class and all
1296/// its protocols.
1297ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1298 IdentifierInfo *II) {
1299 if (const ObjCInterfaceDecl *IDecl =
1300 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1301 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1302 E = IDecl->prop_end(); P != E; ++P) {
1303 ObjCPropertyDecl *Prop = (*P);
1304 if (Prop->getIdentifier() == II)
1305 return Prop;
1306 }
1307 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001308 for (ObjCInterfaceDecl::all_protocol_iterator
1309 PI = IDecl->all_referenced_protocol_begin(),
1310 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001311 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1312 if (Prop)
1313 return Prop;
1314 }
1315 }
1316 else if (const ObjCProtocolDecl *PDecl =
1317 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1318 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1319 E = PDecl->prop_end(); P != E; ++P) {
1320 ObjCPropertyDecl *Prop = (*P);
1321 if (Prop->getIdentifier() == II)
1322 return Prop;
1323 }
1324 // scan through protocol's protocols.
1325 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1326 E = PDecl->protocol_end(); PI != E; ++PI) {
1327 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1328 if (Prop)
1329 return Prop;
1330 }
1331 }
1332 return 0;
1333}
1334
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001335static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1336 ASTContext &Ctx) {
1337 llvm::SmallString<128> ivarName;
1338 {
1339 llvm::raw_svector_ostream os(ivarName);
1340 os << '_' << Prop->getIdentifier()->getName();
1341 }
1342 return &Ctx.Idents.get(ivarName.str());
1343}
1344
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001345/// DefaultSynthesizeProperties - This routine default synthesizes all
1346/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001347void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1348 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001349
1350 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1351 CollectClassPropertyImplementations(IDecl, PropMap);
1352 if (PropMap.empty())
1353 return;
1354 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1355 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1356
1357 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1358 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1359 ObjCPropertyDecl *Prop = P->second;
1360 // If property to be implemented in the super class, ignore.
1361 if (SuperPropMap[Prop->getIdentifier()])
1362 continue;
1363 // Is there a matching propery synthesize/dynamic?
1364 if (Prop->isInvalidDecl() ||
1365 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1366 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1367 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001368 // Property may have been synthesized by user.
1369 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1370 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001371 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1372 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1373 continue;
1374 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1375 continue;
1376 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001377 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1378 // We won't auto-synthesize properties declared in protocols.
1379 Diag(IMPDecl->getLocation(),
1380 diag::warn_auto_synthesizing_protocol_property);
1381 Diag(Prop->getLocation(), diag::note_property_declare);
1382 continue;
1383 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001384
1385 // We use invalid SourceLocations for the synthesized ivars since they
1386 // aren't really synthesized at a particular location; they just exist.
1387 // Saying that they are located at the @implementation isn't really going
1388 // to help users.
1389 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001390 true,
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001391 /* property = */ Prop->getIdentifier(),
1392 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001393 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001394 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001395}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001396
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001397void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1398 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1399 return;
1400 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1401 if (!IC)
1402 return;
1403 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001404 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001405 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001406}
1407
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001408void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001409 ObjCContainerDecl *CDecl,
1410 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001411 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1412 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1413 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1414
Ted Kremenek9d64c152010-03-12 00:38:38 +00001415 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001416 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001417 if (PropMap.empty())
1418 return;
1419
1420 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1421 for (ObjCImplDecl::propimpl_iterator
1422 I = IMPDecl->propimpl_begin(),
1423 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1424 PropImplMap.insert((*I)->getPropertyDecl());
1425
1426 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1427 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1428 ObjCPropertyDecl *Prop = P->second;
1429 // Is there a matching propery synthesize/dynamic?
1430 if (Prop->isInvalidDecl() ||
1431 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001432 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001433 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001434 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001435 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001436 isa<ObjCCategoryDecl>(CDecl) ?
1437 diag::warn_setter_getter_impl_required_in_category :
1438 diag::warn_setter_getter_impl_required)
1439 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001440 Diag(Prop->getLocation(),
1441 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001442 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1443 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001444 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001445 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1446
Ted Kremenek9d64c152010-03-12 00:38:38 +00001447 }
1448
1449 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001450 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001451 isa<ObjCCategoryDecl>(CDecl) ?
1452 diag::warn_setter_getter_impl_required_in_category :
1453 diag::warn_setter_getter_impl_required)
1454 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001455 Diag(Prop->getLocation(),
1456 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001457 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1458 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001459 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001460 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001461 }
1462 }
1463}
1464
1465void
1466Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1467 ObjCContainerDecl* IDecl) {
1468 // Rules apply in non-GC mode only
Douglas Gregore289d812011-09-13 17:21:33 +00001469 if (getLangOptions().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001470 return;
1471 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1472 E = IDecl->prop_end();
1473 I != E; ++I) {
1474 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001475 ObjCMethodDecl *GetterMethod = 0;
1476 ObjCMethodDecl *SetterMethod = 0;
1477 bool LookedUpGetterSetter = false;
1478
Ted Kremenek9d64c152010-03-12 00:38:38 +00001479 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001480 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001481
John McCall265941b2011-09-13 18:31:23 +00001482 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1483 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001484 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1485 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1486 LookedUpGetterSetter = true;
1487 if (GetterMethod) {
1488 Diag(GetterMethod->getLocation(),
1489 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001490 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001491 Diag(Property->getLocation(), diag::note_property_declare);
1492 }
1493 if (SetterMethod) {
1494 Diag(SetterMethod->getLocation(),
1495 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001496 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001497 Diag(Property->getLocation(), diag::note_property_declare);
1498 }
1499 }
1500
Ted Kremenek9d64c152010-03-12 00:38:38 +00001501 // We only care about readwrite atomic property.
1502 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1503 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1504 continue;
1505 if (const ObjCPropertyImplDecl *PIDecl
1506 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1507 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1508 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001509 if (!LookedUpGetterSetter) {
1510 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1511 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1512 LookedUpGetterSetter = true;
1513 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001514 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1515 SourceLocation MethodLoc =
1516 (GetterMethod ? GetterMethod->getLocation()
1517 : SetterMethod->getLocation());
1518 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001519 << Property->getIdentifier() << (GetterMethod != 0)
1520 << (SetterMethod != 0);
1521 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001522 Diag(Property->getLocation(), diag::note_property_declare);
1523 }
1524 }
1525 }
1526}
1527
John McCallf85e1932011-06-15 23:02:42 +00001528void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
Douglas Gregore289d812011-09-13 17:21:33 +00001529 if (getLangOptions().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001530 return;
1531
1532 for (ObjCImplementationDecl::propimpl_iterator
1533 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1534 ObjCPropertyImplDecl *PID = *i;
1535 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1536 continue;
1537
1538 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001539 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1540 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001541 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1542 if (!method)
1543 continue;
1544 ObjCMethodFamily family = method->getMethodFamily();
1545 if (family == OMF_alloc || family == OMF_copy ||
1546 family == OMF_mutableCopy || family == OMF_new) {
1547 if (getLangOptions().ObjCAutoRefCount)
1548 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1549 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001550 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001551 Diag(PD->getLocation(), diag::note_property_declare);
1552 }
1553 }
1554 }
1555}
1556
John McCall5de74d12010-11-10 07:01:40 +00001557/// AddPropertyAttrs - Propagates attributes from a property to the
1558/// implicitly-declared getter or setter for that property.
1559static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1560 ObjCPropertyDecl *Property) {
1561 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001562 for (Decl::attr_iterator A = Property->attr_begin(),
1563 AEnd = Property->attr_end();
1564 A != AEnd; ++A) {
1565 if (isa<DeprecatedAttr>(*A) ||
1566 isa<UnavailableAttr>(*A) ||
1567 isa<AvailabilityAttr>(*A))
1568 PropertyMethod->addAttr((*A)->clone(S.Context));
1569 }
John McCall5de74d12010-11-10 07:01:40 +00001570}
1571
Ted Kremenek9d64c152010-03-12 00:38:38 +00001572/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1573/// have the property type and issue diagnostics if they don't.
1574/// Also synthesize a getter/setter method if none exist (and update the
1575/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1576/// methods is the "right" thing to do.
1577void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001578 ObjCContainerDecl *CD,
1579 ObjCPropertyDecl *redeclaredProperty,
1580 ObjCContainerDecl *lexicalDC) {
1581
Ted Kremenek9d64c152010-03-12 00:38:38 +00001582 ObjCMethodDecl *GetterMethod, *SetterMethod;
1583
1584 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1585 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1586 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1587 property->getLocation());
1588
1589 if (SetterMethod) {
1590 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1591 property->getPropertyAttributes();
1592 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1593 Context.getCanonicalType(SetterMethod->getResultType()) !=
1594 Context.VoidTy)
1595 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1596 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001597 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001598 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1599 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001600 Diag(property->getLocation(),
1601 diag::warn_accessor_property_type_mismatch)
1602 << property->getDeclName()
1603 << SetterMethod->getSelector();
1604 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1605 }
1606 }
1607
1608 // Synthesize getter/setter methods if none exist.
1609 // Find the default getter and if one not found, add one.
1610 // FIXME: The synthesized property we set here is misleading. We almost always
1611 // synthesize these methods unless the user explicitly provided prototypes
1612 // (which is odd, but allowed). Sema should be typechecking that the
1613 // declarations jive in that situation (which it is not currently).
1614 if (!GetterMethod) {
1615 // No instance method of same name as property getter name was found.
1616 // Declare a getter method and add it to the list of methods
1617 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001618 SourceLocation Loc = redeclaredProperty ?
1619 redeclaredProperty->getLocation() :
1620 property->getLocation();
1621
1622 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1623 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001624 property->getType(), 0, CD, /*isInstance=*/true,
1625 /*isVariadic=*/false, /*isSynthesized=*/true,
1626 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001627 (property->getPropertyImplementation() ==
1628 ObjCPropertyDecl::Optional) ?
1629 ObjCMethodDecl::Optional :
1630 ObjCMethodDecl::Required);
1631 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001632
1633 AddPropertyAttrs(*this, GetterMethod, property);
1634
Ted Kremenek23173d72010-05-18 21:09:07 +00001635 // FIXME: Eventually this shouldn't be needed, as the lexical context
1636 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001637 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001638 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001639 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1640 GetterMethod->addAttr(
1641 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001642 } else
1643 // A user declared getter will be synthesize when @synthesize of
1644 // the property with the same name is seen in the @implementation
1645 GetterMethod->setSynthesized(true);
1646 property->setGetterMethodDecl(GetterMethod);
1647
1648 // Skip setter if property is read-only.
1649 if (!property->isReadOnly()) {
1650 // Find the default setter and if one not found, add one.
1651 if (!SetterMethod) {
1652 // No instance method of same name as property setter name was found.
1653 // Declare a setter method and add it to the list of methods
1654 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001655 SourceLocation Loc = redeclaredProperty ?
1656 redeclaredProperty->getLocation() :
1657 property->getLocation();
1658
1659 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001660 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001661 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001662 CD, /*isInstance=*/true, /*isVariadic=*/false,
1663 /*isSynthesized=*/true,
1664 /*isImplicitlyDeclared=*/true,
1665 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001666 (property->getPropertyImplementation() ==
1667 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001668 ObjCMethodDecl::Optional :
1669 ObjCMethodDecl::Required);
1670
Ted Kremenek9d64c152010-03-12 00:38:38 +00001671 // Invent the arguments for the setter. We don't bother making a
1672 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001673 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1674 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001675 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001676 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001677 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001678 SC_None,
1679 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001680 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001681 SetterMethod->setMethodParams(Context, Argument,
1682 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001683
1684 AddPropertyAttrs(*this, SetterMethod, property);
1685
Ted Kremenek9d64c152010-03-12 00:38:38 +00001686 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001687 // FIXME: Eventually this shouldn't be needed, as the lexical context
1688 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001689 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001690 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001691 } else
1692 // A user declared setter will be synthesize when @synthesize of
1693 // the property with the same name is seen in the @implementation
1694 SetterMethod->setSynthesized(true);
1695 property->setSetterMethodDecl(SetterMethod);
1696 }
1697 // Add any synthesized methods to the global pool. This allows us to
1698 // handle the following, which is supported by GCC (and part of the design).
1699 //
1700 // @interface Foo
1701 // @property double bar;
1702 // @end
1703 //
1704 // void thisIsUnfortunate() {
1705 // id foo;
1706 // double bar = [foo bar];
1707 // }
1708 //
1709 if (GetterMethod)
1710 AddInstanceMethodToGlobalPool(GetterMethod);
1711 if (SetterMethod)
1712 AddInstanceMethodToGlobalPool(SetterMethod);
1713}
1714
John McCalld226f652010-08-21 09:40:31 +00001715void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001716 SourceLocation Loc,
1717 unsigned &Attributes) {
1718 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001719 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001720 return;
1721
1722 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001723 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001724
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001725 if (getLangOptions().ObjCAutoRefCount &&
1726 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1727 PropertyTy->isObjCRetainableType()) {
1728 // 'readonly' property with no obvious lifetime.
1729 // its life time will be determined by its backing ivar.
1730 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1731 ObjCDeclSpec::DQ_PR_copy |
1732 ObjCDeclSpec::DQ_PR_retain |
1733 ObjCDeclSpec::DQ_PR_strong |
1734 ObjCDeclSpec::DQ_PR_weak |
1735 ObjCDeclSpec::DQ_PR_assign);
1736 if ((Attributes & rel) == 0)
1737 return;
1738 }
1739
Ted Kremenek9d64c152010-03-12 00:38:38 +00001740 // readonly and readwrite/assign/retain/copy conflict.
1741 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1742 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1743 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001744 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001745 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001746 ObjCDeclSpec::DQ_PR_retain |
1747 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001748 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1749 "readwrite" :
1750 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1751 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001752 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1753 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001754 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1755 "copy" : "retain";
1756
1757 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1758 diag::err_objc_property_attr_mutually_exclusive :
1759 diag::warn_objc_property_attr_mutually_exclusive)
1760 << "readonly" << which;
1761 }
1762
1763 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001764 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1765 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1766 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001767 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001768 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001769 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1770 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1771 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1772 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001773 }
1774
1775 // Check for more than one of { assign, copy, retain }.
1776 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1777 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1778 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1779 << "assign" << "copy";
1780 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1781 }
1782 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1783 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1784 << "assign" << "retain";
1785 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1786 }
John McCallf85e1932011-06-15 23:02:42 +00001787 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1788 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1789 << "assign" << "strong";
1790 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1791 }
1792 if (getLangOptions().ObjCAutoRefCount &&
1793 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1794 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1795 << "assign" << "weak";
1796 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1797 }
1798 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1799 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1800 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1801 << "unsafe_unretained" << "copy";
1802 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1803 }
1804 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1805 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1806 << "unsafe_unretained" << "retain";
1807 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1808 }
1809 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1810 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1811 << "unsafe_unretained" << "strong";
1812 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1813 }
1814 if (getLangOptions().ObjCAutoRefCount &&
1815 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1816 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1817 << "unsafe_unretained" << "weak";
1818 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1819 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001820 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1821 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1822 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1823 << "copy" << "retain";
1824 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1825 }
John McCallf85e1932011-06-15 23:02:42 +00001826 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1827 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1828 << "copy" << "strong";
1829 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1830 }
1831 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1832 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1833 << "copy" << "weak";
1834 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1835 }
1836 }
1837 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1838 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1839 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1840 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001841 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001842 }
1843 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1844 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1845 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1846 << "strong" << "weak";
1847 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001848 }
1849
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00001850 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
1851 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
1852 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1853 << "atomic" << "nonatomic";
1854 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
1855 }
1856
Ted Kremenek9d64c152010-03-12 00:38:38 +00001857 // Warn if user supplied no assignment attribute, property is
1858 // readwrite, and this is an object type.
1859 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001860 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1861 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1862 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001863 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001864 if (getLangOptions().ObjCAutoRefCount)
1865 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001866 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001867 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001868 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001869 bool isAnyClassTy =
1870 (PropertyTy->isObjCClassType() ||
1871 PropertyTy->isObjCQualifiedClassType());
1872 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
1873 // issue any warning.
1874 if (isAnyClassTy && getLangOptions().getGC() == LangOptions::NonGC)
1875 ;
1876 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001877 // Skip this warning in gc-only mode.
Douglas Gregore289d812011-09-13 17:21:33 +00001878 if (getLangOptions().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001879 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001880
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001881 // If non-gc code warn that this is likely inappropriate.
Douglas Gregore289d812011-09-13 17:21:33 +00001882 if (getLangOptions().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001883 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001884 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001885 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001886
1887 // FIXME: Implement warning dependent on NSCopying being
1888 // implemented. See also:
1889 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1890 // (please trim this list while you are at it).
1891 }
1892
1893 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001894 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Douglas Gregore289d812011-09-13 17:21:33 +00001895 && getLangOptions().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001896 && PropertyTy->isBlockPointerType())
1897 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001898 else if (getLangOptions().ObjCAutoRefCount &&
1899 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1900 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1901 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1902 PropertyTy->isBlockPointerType())
1903 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00001904
1905 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1906 (Attributes & ObjCDeclSpec::DQ_PR_setter))
1907 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
1908
Ted Kremenek9d64c152010-03-12 00:38:38 +00001909}