blob: f42259c68694e386899ace8c006d9b0acac3aa34 [file] [log] [blame]
Ted Kremenek9d64c152010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
Fariborz Jahanian17cb3262010-05-05 21:52:17 +000018#include "clang/AST/ExprObjC.h"
Fariborz Jahanian57e264e2011-10-06 18:38:18 +000019#include "clang/AST/ExprCXX.h"
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +000020#include "clang/AST/ASTMutationListener.h"
John McCall50df6ae2010-08-25 07:03:20 +000021#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000023
24using namespace clang;
25
Ted Kremenek28685ab2010-03-12 00:46:40 +000026//===----------------------------------------------------------------------===//
27// Grammar actions.
28//===----------------------------------------------------------------------===//
29
John McCall265941b2011-09-13 18:31:23 +000030/// getImpliedARCOwnership - Given a set of property attributes and a
31/// type, infer an expected lifetime. The type's ownership qualification
32/// is not considered.
33///
34/// Returns OCL_None if the attributes as stated do not imply an ownership.
35/// Never returns OCL_Autoreleasing.
36static Qualifiers::ObjCLifetime getImpliedARCOwnership(
37 ObjCPropertyDecl::PropertyAttributeKind attrs,
38 QualType type) {
39 // retain, strong, copy, weak, and unsafe_unretained are only legal
40 // on properties of retainable pointer type.
41 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
42 ObjCPropertyDecl::OBJC_PR_strong |
43 ObjCPropertyDecl::OBJC_PR_copy)) {
Fariborz Jahanian5fa065b2011-10-13 23:45:45 +000044 return type->getObjCARCImplicitLifetime();
John McCall265941b2011-09-13 18:31:23 +000045 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
46 return Qualifiers::OCL_Weak;
47 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
48 return Qualifiers::OCL_ExplicitNone;
49 }
50
51 // assign can appear on other types, so we have to check the
52 // property type.
53 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
54 type->isObjCRetainableType()) {
55 return Qualifiers::OCL_ExplicitNone;
56 }
57
58 return Qualifiers::OCL_None;
59}
60
John McCallf85e1932011-06-15 23:02:42 +000061/// Check the internal consistency of a property declaration.
62static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
63 if (property->isInvalidDecl()) return;
64
65 ObjCPropertyDecl::PropertyAttributeKind propertyKind
66 = property->getPropertyAttributes();
67 Qualifiers::ObjCLifetime propertyLifetime
68 = property->getType().getObjCLifetime();
69
70 // Nothing to do if we don't have a lifetime.
71 if (propertyLifetime == Qualifiers::OCL_None) return;
72
John McCall265941b2011-09-13 18:31:23 +000073 Qualifiers::ObjCLifetime expectedLifetime
74 = getImpliedARCOwnership(propertyKind, property->getType());
75 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000076 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000077 // attribute. That's okay, but restore reasonable invariants by
78 // setting the property attribute according to the lifetime
79 // qualifier.
80 ObjCPropertyDecl::PropertyAttributeKind attr;
81 if (propertyLifetime == Qualifiers::OCL_Strong) {
82 attr = ObjCPropertyDecl::OBJC_PR_strong;
83 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
84 attr = ObjCPropertyDecl::OBJC_PR_weak;
85 } else {
86 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
87 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
88 }
89 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000090 return;
91 }
92
93 if (propertyLifetime == expectedLifetime) return;
94
95 property->setInvalidDecl();
96 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000097 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +000098 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +000099 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +0000100 << propertyLifetime;
101}
102
John McCalld226f652010-08-21 09:40:31 +0000103Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000104 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000105 FieldDeclarator &FD,
106 ObjCDeclSpec &ODS,
107 Selector GetterSel,
108 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000109 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000110 tok::ObjCKeywordKind MethodImplKind,
111 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000112 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000113 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
114 QualType T = TSI->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +0000115 if ((getLangOpts().getGC() != LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +0000116 T.isObjCGCWeak()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +0000117 (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +0000118 T.getObjCLifetime() == Qualifiers::OCL_Weak))
119 Attributes |= ObjCDeclSpec::DQ_PR_weak;
120
Ted Kremenek28685ab2010-03-12 00:46:40 +0000121 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
122 // default is readwrite!
123 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
124 // property is defaulted to 'assign' if it is readwrite and is
125 // not retain or copy
126 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
127 (isReadWrite &&
128 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +0000129 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
130 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
131 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
132 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000133
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000134 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000135 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000136
Ted Kremenek28685ab2010-03-12 00:46:40 +0000137 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000138 if (CDecl->IsClassExtension()) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000139 Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000140 FD, GetterSel, SetterSel,
141 isAssign, isReadWrite,
142 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000143 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000144 isOverridingProperty, TSI,
145 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000146 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000147 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
David Blaikie4e4d0842012-03-11 07:00:24 +0000148 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000149 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
150 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000151 return Res;
152 }
153
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000154 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
John McCallf85e1932011-06-15 23:02:42 +0000155 GetterSel, SetterSel,
156 isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000157 Attributes,
158 ODS.getPropertyAttributes(),
159 TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000160 if (lexicalDC)
161 Res->setLexicalDeclContext(lexicalDC);
162
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000163 // Validate the attributes on the @property.
164 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000165
David Blaikie4e4d0842012-03-11 07:00:24 +0000166 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000167 checkARCPropertyDecl(*this, Res);
168
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000169 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000170}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000171
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000172static ObjCPropertyDecl::PropertyAttributeKind
173makePropertyAttributesAsWritten(unsigned Attributes) {
174 unsigned attributesAsWritten = 0;
175 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
176 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
177 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
178 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
179 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
180 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
181 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
182 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
183 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
184 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
185 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
186 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
187 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
188 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
189 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
190 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
191 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
192 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
193 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
194 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
195 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
196 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
197 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
198 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
199
200 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
201}
202
John McCalld226f652010-08-21 09:40:31 +0000203Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000204Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000205 SourceLocation AtLoc,
206 SourceLocation LParenLoc,
207 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000208 Selector GetterSel, Selector SetterSel,
209 const bool isAssign,
210 const bool isReadWrite,
211 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000212 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000213 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000214 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000215 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000216 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000217 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000218 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000219 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000220 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
221
222 if (CCPrimary)
223 // Check for duplicate declaration of this property in current and
224 // other class extensions.
225 for (const ObjCCategoryDecl *ClsExtDecl =
226 CCPrimary->getFirstClassExtension();
227 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
228 if (ObjCPropertyDecl *prevDecl =
229 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
230 Diag(AtLoc, diag::err_duplicate_property);
231 Diag(prevDecl->getLocation(), diag::note_property_declare);
232 return 0;
233 }
234 }
235
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000236 // Create a new ObjCPropertyDecl with the DeclContext being
237 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000238 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000239 ObjCPropertyDecl *PDecl =
240 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000241 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000242 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000243 makePropertyAttributesAsWritten(AttributesAsWritten));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000244 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
245 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
246 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
247 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000248 // Set setter/getter selector name. Needed later.
249 PDecl->setGetterName(GetterSel);
250 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000251 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000252 DC->addDecl(PDecl);
253
254 // We need to look in the @interface to see if the @property was
255 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000256 if (!CCPrimary) {
257 Diag(CDecl->getLocation(), diag::err_continuation_class);
258 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000259 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000260 }
261
262 // Find the property in continuation class's primary class only.
263 ObjCPropertyDecl *PIDecl =
264 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
265
266 if (!PIDecl) {
267 // No matching property found in the primary class. Just fall thru
268 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000269 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000270 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000271 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000272 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000273
274 // A case of continuation class adding a new property in the class. This
275 // is not what it was meant for. However, gcc supports it and so should we.
276 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000277 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000278 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000279 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
280 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000281 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000282 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
283 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000284 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000285 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
286 bool IncompatibleObjC = false;
287 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000288 // Relax the strict type matching for property type in continuation class.
289 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000290 // as it narrows the object type in its primary class property. Note that
291 // this conversion is safe only because the wider type is for a 'readonly'
292 // property in primary class and 'narrowed' type for a 'readwrite' property
293 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000294 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
295 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
296 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
297 ConvertedType, IncompatibleObjC))
298 || IncompatibleObjC) {
299 Diag(AtLoc,
300 diag::err_type_mismatch_continuation_class) << PDecl->getType();
301 Diag(PIDecl->getLocation(), diag::note_property_declare);
302 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000303 }
304
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000305 // The property 'PIDecl's readonly attribute will be over-ridden
306 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000307 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000308 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
309 unsigned retainCopyNonatomic =
310 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000311 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000312 ObjCPropertyDecl::OBJC_PR_copy |
313 ObjCPropertyDecl::OBJC_PR_nonatomic);
314 if ((Attributes & retainCopyNonatomic) !=
315 (PIkind & retainCopyNonatomic)) {
316 Diag(AtLoc, diag::warn_property_attr_mismatch);
317 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000318 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000319 DeclContext *DC = cast<DeclContext>(CCPrimary);
320 if (!ObjCPropertyDecl::findPropertyDecl(DC,
321 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000322 // Protocol is not in the primary class. Must build one for it.
323 ObjCDeclSpec ProtocolPropertyODS;
324 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
325 // and ObjCPropertyDecl::PropertyAttributeKind have identical
326 // values. Should consolidate both into one enum type.
327 ProtocolPropertyODS.
328 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
329 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000330 // Must re-establish the context from class extension to primary
331 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000332 ContextRAII SavedContext(*this, CCPrimary);
333
John McCalld226f652010-08-21 09:40:31 +0000334 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000335 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000336 PIDecl->getGetterName(),
337 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000338 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000339 MethodImplKind,
340 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000341 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000342 }
343 PIDecl->makeitReadWriteAttribute();
344 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
345 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000346 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
347 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000348 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
349 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
350 PIDecl->setSetterName(SetterSel);
351 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000352 // Tailor the diagnostics for the common case where a readwrite
353 // property is declared both in the @interface and the continuation.
354 // This is a common error where the user often intended the original
355 // declaration to be readonly.
356 unsigned diag =
357 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
358 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
359 ? diag::err_use_continuation_class_redeclaration_readwrite
360 : diag::err_use_continuation_class;
361 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000362 << CCPrimary->getDeclName();
363 Diag(PIDecl->getLocation(), diag::note_property_declare);
364 }
365 *isOverridingProperty = true;
366 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000367 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000368 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
369 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000370 if (ASTMutationListener *L = Context.getASTMutationListener())
371 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000372 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000373}
374
375ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
376 ObjCContainerDecl *CDecl,
377 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000378 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000379 FieldDeclarator &FD,
380 Selector GetterSel,
381 Selector SetterSel,
382 const bool isAssign,
383 const bool isReadWrite,
384 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000385 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000386 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000387 tok::ObjCKeywordKind MethodImplKind,
388 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000389 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000390 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000391
392 // Issue a warning if property is 'assign' as default and its object, which is
393 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000394 if (getLangOpts().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000395 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000396 if (const ObjCObjectPointerType *ObjPtrTy =
397 T->getAs<ObjCObjectPointerType>()) {
398 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
399 if (IDecl)
400 if (ObjCProtocolDecl* PNSCopying =
401 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
402 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
403 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000404 }
John McCallc12c5bb2010-05-15 11:32:37 +0000405 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000406 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
407
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000408 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000409 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
410 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000411 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000412
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000413 if (ObjCPropertyDecl *prevDecl =
414 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000415 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000416 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000417 PDecl->setInvalidDecl();
418 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000419 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000420 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000421 if (lexicalDC)
422 PDecl->setLexicalDeclContext(lexicalDC);
423 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000424
425 if (T->isArrayType() || T->isFunctionType()) {
426 Diag(AtLoc, diag::err_property_type) << T;
427 PDecl->setInvalidDecl();
428 }
429
430 ProcessDeclAttributes(S, PDecl, FD.D);
431
432 // Regardless of setter/getter attribute, we save the default getter/setter
433 // selector names in anticipation of declaration of setter/getter methods.
434 PDecl->setGetterName(GetterSel);
435 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000436 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000437 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000438
Ted Kremenek28685ab2010-03-12 00:46:40 +0000439 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
440 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
441
442 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
443 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
444
445 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
446 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
447
448 if (isReadWrite)
449 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
450
451 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
452 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
453
John McCallf85e1932011-06-15 23:02:42 +0000454 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
455 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
456
457 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
458 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
459
Ted Kremenek28685ab2010-03-12 00:46:40 +0000460 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
461 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
462
John McCallf85e1932011-06-15 23:02:42 +0000463 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
464 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
465
Ted Kremenek28685ab2010-03-12 00:46:40 +0000466 if (isAssign)
467 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
468
John McCall265941b2011-09-13 18:31:23 +0000469 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000470 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
471 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000472 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000473 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000474
John McCallf85e1932011-06-15 23:02:42 +0000475 // 'unsafe_unretained' is alias for 'assign'.
476 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
477 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
478 if (isAssign)
479 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
480
Ted Kremenek28685ab2010-03-12 00:46:40 +0000481 if (MethodImplKind == tok::objc_required)
482 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
483 else if (MethodImplKind == tok::objc_optional)
484 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000485
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000486 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000487}
488
John McCallf85e1932011-06-15 23:02:42 +0000489static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
490 ObjCPropertyDecl *property,
491 ObjCIvarDecl *ivar) {
492 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
493
John McCallf85e1932011-06-15 23:02:42 +0000494 QualType ivarType = ivar->getType();
495 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000496
John McCall265941b2011-09-13 18:31:23 +0000497 // The lifetime implied by the property's attributes.
498 Qualifiers::ObjCLifetime propertyLifetime =
499 getImpliedARCOwnership(property->getPropertyAttributes(),
500 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000501
John McCall265941b2011-09-13 18:31:23 +0000502 // We're fine if they match.
503 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000504
John McCall265941b2011-09-13 18:31:23 +0000505 // These aren't valid lifetimes for object ivars; don't diagnose twice.
506 if (ivarLifetime == Qualifiers::OCL_None ||
507 ivarLifetime == Qualifiers::OCL_Autoreleasing)
508 return;
John McCallf85e1932011-06-15 23:02:42 +0000509
John McCall265941b2011-09-13 18:31:23 +0000510 switch (propertyLifetime) {
511 case Qualifiers::OCL_Strong:
512 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
513 << property->getDeclName()
514 << ivar->getDeclName()
515 << ivarLifetime;
516 break;
John McCallf85e1932011-06-15 23:02:42 +0000517
John McCall265941b2011-09-13 18:31:23 +0000518 case Qualifiers::OCL_Weak:
519 S.Diag(propertyImplLoc, diag::error_weak_property)
520 << property->getDeclName()
521 << ivar->getDeclName();
522 break;
John McCallf85e1932011-06-15 23:02:42 +0000523
John McCall265941b2011-09-13 18:31:23 +0000524 case Qualifiers::OCL_ExplicitNone:
525 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
526 << property->getDeclName()
527 << ivar->getDeclName()
528 << ((property->getPropertyAttributesAsWritten()
529 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
530 break;
John McCallf85e1932011-06-15 23:02:42 +0000531
John McCall265941b2011-09-13 18:31:23 +0000532 case Qualifiers::OCL_Autoreleasing:
533 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000534
John McCall265941b2011-09-13 18:31:23 +0000535 case Qualifiers::OCL_None:
536 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000537 return;
538 }
539
540 S.Diag(property->getLocation(), diag::note_property_declare);
541}
542
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000543/// setImpliedPropertyAttributeForReadOnlyProperty -
544/// This routine evaludates life-time attributes for a 'readonly'
545/// property with no known lifetime of its own, using backing
546/// 'ivar's attribute, if any. If no backing 'ivar', property's
547/// life-time is assumed 'strong'.
548static void setImpliedPropertyAttributeForReadOnlyProperty(
549 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
550 Qualifiers::ObjCLifetime propertyLifetime =
551 getImpliedARCOwnership(property->getPropertyAttributes(),
552 property->getType());
553 if (propertyLifetime != Qualifiers::OCL_None)
554 return;
555
556 if (!ivar) {
557 // if no backing ivar, make property 'strong'.
558 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
559 return;
560 }
561 // property assumes owenership of backing ivar.
562 QualType ivarType = ivar->getType();
563 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
564 if (ivarLifetime == Qualifiers::OCL_Strong)
565 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
566 else if (ivarLifetime == Qualifiers::OCL_Weak)
567 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
568 return;
569}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000570
571/// ActOnPropertyImplDecl - This routine performs semantic checks and
572/// builds the AST node for a property implementation declaration; declared
573/// as @synthesize or @dynamic.
574///
John McCalld226f652010-08-21 09:40:31 +0000575Decl *Sema::ActOnPropertyImplDecl(Scope *S,
576 SourceLocation AtLoc,
577 SourceLocation PropertyLoc,
578 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000579 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000580 IdentifierInfo *PropertyIvar,
581 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000582 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000583 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000584 // Make sure we have a context for the property implementation declaration.
585 if (!ClassImpDecl) {
586 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000587 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000588 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000589 if (PropertyIvarLoc.isInvalid())
590 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000591 SourceLocation PropertyDiagLoc = PropertyLoc;
592 if (PropertyDiagLoc.isInvalid())
593 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000594 ObjCPropertyDecl *property = 0;
595 ObjCInterfaceDecl* IDecl = 0;
596 // Find the class or category class where this property must have
597 // a declaration.
598 ObjCImplementationDecl *IC = 0;
599 ObjCCategoryImplDecl* CatImplClass = 0;
600 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
601 IDecl = IC->getClassInterface();
602 // We always synthesize an interface for an implementation
603 // without an interface decl. So, IDecl is always non-zero.
604 assert(IDecl &&
605 "ActOnPropertyImplDecl - @implementation without @interface");
606
607 // Look for this property declaration in the @implementation's @interface
608 property = IDecl->FindPropertyDeclaration(PropertyId);
609 if (!property) {
610 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000611 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000612 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000613 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000614 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
615 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000616 if (AtLoc.isValid())
617 Diag(AtLoc, diag::warn_implicit_atomic_property);
618 else
619 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
620 Diag(property->getLocation(), diag::note_property_declare);
621 }
622
Ted Kremenek28685ab2010-03-12 00:46:40 +0000623 if (const ObjCCategoryDecl *CD =
624 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
625 if (!CD->IsClassExtension()) {
626 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
627 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000628 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000629 }
630 }
631 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
632 if (Synthesize) {
633 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000634 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000635 }
636 IDecl = CatImplClass->getClassInterface();
637 if (!IDecl) {
638 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000639 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000640 }
641 ObjCCategoryDecl *Category =
642 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
643
644 // If category for this implementation not found, it is an error which
645 // has already been reported eralier.
646 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000647 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000648 // Look for this property declaration in @implementation's category
649 property = Category->FindPropertyDeclaration(PropertyId);
650 if (!property) {
651 Diag(PropertyLoc, diag::error_bad_category_property_decl)
652 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000653 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000654 }
655 } else {
656 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000657 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000658 }
659 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000660 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000661 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000662 // Check that we have a valid, previously declared ivar for @synthesize
663 if (Synthesize) {
664 // @synthesize
665 if (!PropertyIvar)
666 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000667 // Check that this is a previously declared 'ivar' in 'IDecl' interface
668 ObjCInterfaceDecl *ClassDeclared;
669 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
670 QualType PropType = property->getType();
671 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000672
673 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000674 diag::err_incomplete_synthesized_property,
675 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000676 Diag(property->getLocation(), diag::note_property_declare);
677 CompleteTypeErr = true;
678 }
679
David Blaikie4e4d0842012-03-11 07:00:24 +0000680 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000681 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000682 ObjCPropertyDecl::OBJC_PR_readonly) &&
683 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000684 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
685 }
686
John McCallf85e1932011-06-15 23:02:42 +0000687 ObjCPropertyDecl::PropertyAttributeKind kind
688 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000689
690 // Add GC __weak to the ivar type if the property is weak.
691 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000692 getLangOpts().getGC() != LangOptions::NonGC) {
693 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000694 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000695 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000696 Diag(property->getLocation(), diag::note_property_declare);
697 } else {
698 PropertyIvarType =
699 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000700 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000701 }
John McCall265941b2011-09-13 18:31:23 +0000702
Ted Kremenek28685ab2010-03-12 00:46:40 +0000703 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000704 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000705 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000706 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000707 !PropertyIvarType.getObjCLifetime() &&
708 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000709
John McCall265941b2011-09-13 18:31:23 +0000710 // It's an error if we have to do this and the user didn't
711 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000712 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000713 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000714 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000715 diag::err_arc_objc_property_default_assign_on_object);
716 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000717 } else {
718 Qualifiers::ObjCLifetime lifetime =
719 getImpliedARCOwnership(kind, PropertyIvarType);
720 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000721 if (lifetime == Qualifiers::OCL_Weak) {
722 bool err = false;
723 if (const ObjCObjectPointerType *ObjT =
724 PropertyIvarType->getAs<ObjCObjectPointerType>())
725 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000726 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000727 Diag(property->getLocation(), diag::note_property_declare);
728 err = true;
729 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000730 if (!err && !getLangOpts().ObjCRuntimeHasWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000731 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000732 Diag(property->getLocation(), diag::note_property_declare);
733 }
John McCallf85e1932011-06-15 23:02:42 +0000734 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000735
John McCallf85e1932011-06-15 23:02:42 +0000736 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000737 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000738 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
739 }
John McCallf85e1932011-06-15 23:02:42 +0000740 }
741
742 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000743 !getLangOpts().ObjCAutoRefCount &&
744 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000745 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000746 Diag(property->getLocation(), diag::note_property_declare);
747 }
748
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000749 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000750 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000751 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000752 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000753 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000754 if (CompleteTypeErr)
755 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000756 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000757 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000758 property->setPropertyIvarDecl(Ivar);
759
David Blaikie4e4d0842012-03-11 07:00:24 +0000760 if (!getLangOpts().ObjCNonFragileABI)
Eli Friedmane4c043d2012-05-01 22:26:06 +0000761 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
762 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000763 // Note! I deliberately want it to fall thru so, we have a
764 // a property implementation and to avoid future warnings.
David Blaikie4e4d0842012-03-11 07:00:24 +0000765 } else if (getLangOpts().ObjCNonFragileABI &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000766 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000767 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000768 << property->getDeclName() << Ivar->getDeclName()
769 << ClassDeclared->getDeclName();
770 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000771 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000772 // Note! I deliberately want it to fall thru so more errors are caught.
773 }
774 QualType IvarType = Context.getCanonicalType(Ivar->getType());
775
776 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000777 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
778 compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000779 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000780 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000781 compat =
782 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000783 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000784 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000785 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000786 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
787 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000788 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000789 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000790 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000791 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000792 << property->getDeclName() << PropType
793 << Ivar->getDeclName() << IvarType;
794 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000795 // Note! I deliberately want it to fall thru so, we have a
796 // a property implementation and to avoid future warnings.
797 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000798 else {
799 // FIXME! Rules for properties are somewhat different that those
800 // for assignments. Use a new routine to consolidate all cases;
801 // specifically for property redeclarations as well as for ivars.
802 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
803 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
804 if (lhsType != rhsType &&
805 lhsType->isArithmeticType()) {
806 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
807 << property->getDeclName() << PropType
808 << Ivar->getDeclName() << IvarType;
809 Diag(Ivar->getLocation(), diag::note_ivar_decl);
810 // Fall thru - see previous comment
811 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000812 }
813 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000814 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000815 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000816 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000817 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000818 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000819 // Fall thru - see previous comment
820 }
John McCallf85e1932011-06-15 23:02:42 +0000821 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000822 if ((property->getType()->isObjCObjectPointerType() ||
823 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000824 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000825 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000826 << property->getDeclName() << Ivar->getDeclName();
827 // Fall thru - see previous comment
828 }
829 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000830 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000831 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000832 } else if (PropertyIvar)
833 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +0000834 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000835
Ted Kremenek28685ab2010-03-12 00:46:40 +0000836 assert (property && "ActOnPropertyImplDecl - property declaration missing");
837 ObjCPropertyImplDecl *PIDecl =
838 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
839 property,
840 (Synthesize ?
841 ObjCPropertyImplDecl::Synthesize
842 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000843 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000844
Fariborz Jahanian74414712012-05-15 18:12:51 +0000845 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +0000846 PIDecl->setInvalidDecl();
847
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000848 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
849 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000850 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000851 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000852 // For Objective-C++, need to synthesize the AST for the IVAR object to be
853 // returned by the getter as it must conform to C++'s copy-return rules.
854 // FIXME. Eventually we want to do this for Objective-C as well.
855 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
856 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +0000857 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000858 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000859 Expr *IvarRefExpr =
860 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
861 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000862 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000863 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000864 SourceLocation(),
865 getterMethod->getResultType(),
866 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000867 SourceLocation(),
868 Owned(IvarRefExpr));
869 if (!Res.isInvalid()) {
870 Expr *ResExpr = Res.takeAs<Expr>();
871 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000872 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000873 PIDecl->setGetterCXXConstructor(ResExpr);
874 }
875 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000876 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
877 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
878 Diag(getterMethod->getLocation(),
879 diag::warn_property_getter_owning_mismatch);
880 Diag(property->getLocation(), diag::note_property_declare);
881 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000882 }
883 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
884 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000885 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
886 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000887 // FIXME. Eventually we want to do this for Objective-C as well.
888 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
889 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +0000890 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000891 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000892 Expr *lhs =
893 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
894 SelfExpr, true, true);
895 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
896 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +0000897 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +0000898 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +0000899 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000900 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000901 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000902 if (property->getPropertyAttributes() &
903 ObjCPropertyDecl::OBJC_PR_atomic) {
904 Expr *callExpr = Res.takeAs<Expr>();
905 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000906 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
907 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000908 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000909 if (property->getType()->isReferenceType()) {
910 Diag(PropertyLoc,
911 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000912 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000913 Diag(FuncDecl->getLocStart(),
914 diag::note_callee_decl) << FuncDecl;
915 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000916 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000917 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
918 }
919 }
920
Ted Kremenek28685ab2010-03-12 00:46:40 +0000921 if (IC) {
922 if (Synthesize)
923 if (ObjCPropertyImplDecl *PPIDecl =
924 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
925 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
926 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
927 << PropertyIvar;
928 Diag(PPIDecl->getLocation(), diag::note_previous_use);
929 }
930
931 if (ObjCPropertyImplDecl *PPIDecl
932 = IC->FindPropertyImplDecl(PropertyId)) {
933 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
934 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000935 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000936 }
937 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +0000938 if (getLangOpts().ObjCDefaultSynthProperties &&
939 getLangOpts().ObjCNonFragileABI2 &&
Ted Kremenek71207fc2012-01-05 22:47:47 +0000940 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000941 // Diagnose if an ivar was lazily synthesdized due to a previous
942 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000943 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000944 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000945 ObjCIvarDecl *Ivar = 0;
946 if (!Synthesize)
947 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
948 else {
949 if (PropertyIvar && PropertyIvar != PropertyId)
950 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
951 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000952 // Issue diagnostics only if Ivar belongs to current class.
953 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000954 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000955 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
956 << PropertyId;
957 Ivar->setInvalidDecl();
958 }
959 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000960 } else {
961 if (Synthesize)
962 if (ObjCPropertyImplDecl *PPIDecl =
963 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000964 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000965 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
966 << PropertyIvar;
967 Diag(PPIDecl->getLocation(), diag::note_previous_use);
968 }
969
970 if (ObjCPropertyImplDecl *PPIDecl =
971 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000972 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000973 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000974 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000975 }
976 CatImplClass->addPropertyImplementation(PIDecl);
977 }
978
John McCalld226f652010-08-21 09:40:31 +0000979 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000980}
981
982//===----------------------------------------------------------------------===//
983// Helper methods.
984//===----------------------------------------------------------------------===//
985
Ted Kremenek9d64c152010-03-12 00:38:38 +0000986/// DiagnosePropertyMismatch - Compares two properties for their
987/// attributes and types and warns on a variety of inconsistencies.
988///
989void
990Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
991 ObjCPropertyDecl *SuperProperty,
992 const IdentifierInfo *inheritedName) {
993 ObjCPropertyDecl::PropertyAttributeKind CAttr =
994 Property->getPropertyAttributes();
995 ObjCPropertyDecl::PropertyAttributeKind SAttr =
996 SuperProperty->getPropertyAttributes();
997 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
998 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
999 Diag(Property->getLocation(), diag::warn_readonly_property)
1000 << Property->getDeclName() << inheritedName;
1001 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1002 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1003 Diag(Property->getLocation(), diag::warn_property_attribute)
1004 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001005 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001006 unsigned CAttrRetain =
1007 (CAttr &
1008 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1009 unsigned SAttrRetain =
1010 (SAttr &
1011 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1012 bool CStrong = (CAttrRetain != 0);
1013 bool SStrong = (SAttrRetain != 0);
1014 if (CStrong != SStrong)
1015 Diag(Property->getLocation(), diag::warn_property_attribute)
1016 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1017 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001018
1019 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1020 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1021 Diag(Property->getLocation(), diag::warn_property_attribute)
1022 << Property->getDeclName() << "atomic" << inheritedName;
1023 if (Property->getSetterName() != SuperProperty->getSetterName())
1024 Diag(Property->getLocation(), diag::warn_property_attribute)
1025 << Property->getDeclName() << "setter" << inheritedName;
1026 if (Property->getGetterName() != SuperProperty->getGetterName())
1027 Diag(Property->getLocation(), diag::warn_property_attribute)
1028 << Property->getDeclName() << "getter" << inheritedName;
1029
1030 QualType LHSType =
1031 Context.getCanonicalType(SuperProperty->getType());
1032 QualType RHSType =
1033 Context.getCanonicalType(Property->getType());
1034
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001035 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001036 // Do cases not handled in above.
1037 // FIXME. For future support of covariant property types, revisit this.
1038 bool IncompatibleObjC = false;
1039 QualType ConvertedType;
1040 if (!isObjCPointerConversion(RHSType, LHSType,
1041 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001042 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001043 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1044 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001045 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1046 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001047 }
1048}
1049
1050bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1051 ObjCMethodDecl *GetterMethod,
1052 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001053 if (!GetterMethod)
1054 return false;
1055 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1056 QualType PropertyIvarType = property->getType().getNonReferenceType();
1057 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1058 if (!compat) {
1059 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1060 isa<ObjCObjectPointerType>(GetterType))
1061 compat =
1062 Context.canAssignObjCInterfaces(
1063 PropertyIvarType->getAs<ObjCObjectPointerType>(),
1064 GetterType->getAs<ObjCObjectPointerType>());
1065 else if (CheckAssignmentConstraints(Loc, PropertyIvarType, GetterType)
1066 != Compatible) {
1067 Diag(Loc, diag::error_property_accessor_type)
1068 << property->getDeclName() << PropertyIvarType
1069 << GetterMethod->getSelector() << GetterType;
1070 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1071 return true;
1072 } else {
1073 compat = true;
1074 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1075 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1076 if (lhsType != rhsType && lhsType->isArithmeticType())
1077 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001078 }
1079 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001080
1081 if (!compat) {
1082 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1083 << property->getDeclName()
1084 << GetterMethod->getSelector();
1085 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1086 return true;
1087 }
1088
Ted Kremenek9d64c152010-03-12 00:38:38 +00001089 return false;
1090}
1091
1092/// ComparePropertiesInBaseAndSuper - This routine compares property
1093/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001094/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001095///
1096void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1097 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1098 if (!SDecl)
1099 return;
1100 // FIXME: O(N^2)
1101 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1102 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie262bc182012-04-30 02:36:29 +00001103 ObjCPropertyDecl *SuperPDecl = &*S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001104 // Does property in super class has declaration in current class?
1105 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1106 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie262bc182012-04-30 02:36:29 +00001107 ObjCPropertyDecl *PDecl = &*I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001108 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1109 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1110 SDecl->getIdentifier());
1111 }
1112 }
1113}
1114
1115/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1116/// of properties declared in a protocol and compares their attribute against
1117/// the same property declared in the class or category.
1118void
1119Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1120 ObjCProtocolDecl *PDecl) {
1121 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1122 if (!IDecl) {
1123 // Category
1124 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1125 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1126 if (!CatDecl->IsClassExtension())
1127 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1128 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001129 ObjCPropertyDecl *Pr = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001130 ObjCCategoryDecl::prop_iterator CP, CE;
1131 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001132 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001133 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001134 break;
1135 if (CP != CE)
1136 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie262bc182012-04-30 02:36:29 +00001137 DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001138 }
1139 return;
1140 }
1141 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1142 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001143 ObjCPropertyDecl *Pr = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001144 ObjCInterfaceDecl::prop_iterator CP, CE;
1145 // Is this property already in class's list of properties?
1146 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001147 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001148 break;
1149 if (CP != CE)
1150 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie262bc182012-04-30 02:36:29 +00001151 DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001152 }
1153}
1154
1155/// CompareProperties - This routine compares properties
1156/// declared in 'ClassOrProtocol' objects (which can be a class or an
1157/// inherited protocol with the list of properties for class/category 'CDecl'
1158///
John McCalld226f652010-08-21 09:40:31 +00001159void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1160 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001161 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1162
1163 if (!IDecl) {
1164 // Category
1165 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1166 assert (CatDecl && "CompareProperties");
1167 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1168 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1169 E = MDecl->protocol_end(); P != E; ++P)
1170 // Match properties of category with those of protocol (*P)
1171 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1172
1173 // Go thru the list of protocols for this category and recursively match
1174 // their properties with those in the category.
1175 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1176 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001177 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001178 } else {
1179 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1180 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1181 E = MD->protocol_end(); P != E; ++P)
1182 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1183 }
1184 return;
1185 }
1186
1187 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001188 for (ObjCInterfaceDecl::all_protocol_iterator
1189 P = MDecl->all_referenced_protocol_begin(),
1190 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001191 // Match properties of class IDecl with those of protocol (*P).
1192 MatchOneProtocolPropertiesInClass(IDecl, *P);
1193
1194 // Go thru the list of protocols for this class and recursively match
1195 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001196 for (ObjCInterfaceDecl::all_protocol_iterator
1197 P = IDecl->all_referenced_protocol_begin(),
1198 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001199 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001200 } else {
1201 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1202 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1203 E = MD->protocol_end(); P != E; ++P)
1204 MatchOneProtocolPropertiesInClass(IDecl, *P);
1205 }
1206}
1207
1208/// isPropertyReadonly - Return true if property is readonly, by searching
1209/// for the property in the class and in its categories and implementations
1210///
1211bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1212 ObjCInterfaceDecl *IDecl) {
1213 // by far the most common case.
1214 if (!PDecl->isReadOnly())
1215 return false;
1216 // Even if property is ready only, if interface has a user defined setter,
1217 // it is not considered read only.
1218 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1219 return false;
1220
1221 // Main class has the property as 'readonly'. Must search
1222 // through the category list to see if the property's
1223 // attribute has been over-ridden to 'readwrite'.
1224 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1225 Category; Category = Category->getNextClassCategory()) {
1226 // Even if property is ready only, if a category has a user defined setter,
1227 // it is not considered read only.
1228 if (Category->getInstanceMethod(PDecl->getSetterName()))
1229 return false;
1230 ObjCPropertyDecl *P =
1231 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1232 if (P && !P->isReadOnly())
1233 return false;
1234 }
1235
1236 // Also, check for definition of a setter method in the implementation if
1237 // all else failed.
1238 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1239 if (ObjCImplementationDecl *IMD =
1240 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1241 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1242 return false;
1243 } else if (ObjCCategoryImplDecl *CIMD =
1244 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1245 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1246 return false;
1247 }
1248 }
1249 // Lastly, look through the implementation (if one is in scope).
1250 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1251 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1252 return false;
1253 // If all fails, look at the super class.
1254 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1255 return isPropertyReadonly(PDecl, SIDecl);
1256 return true;
1257}
1258
1259/// CollectImmediateProperties - This routine collects all properties in
1260/// the class and its conforming protocols; but not those it its super class.
1261void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001262 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1263 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001264 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1265 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1266 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001267 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001268 PropMap[Prop->getIdentifier()] = Prop;
1269 }
1270 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001271 for (ObjCInterfaceDecl::all_protocol_iterator
1272 PI = IDecl->all_referenced_protocol_begin(),
1273 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001274 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001275 }
1276 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1277 if (!CATDecl->IsClassExtension())
1278 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1279 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001280 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001281 PropMap[Prop->getIdentifier()] = Prop;
1282 }
1283 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001284 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001285 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001286 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001287 }
1288 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1289 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1290 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001291 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001292 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1293 // Exclude property for protocols which conform to class's super-class,
1294 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001295 if (!PropertyFromSuper ||
1296 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001297 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1298 if (!PropEntry)
1299 PropEntry = Prop;
1300 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001301 }
1302 // scan through protocol's protocols.
1303 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1304 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001305 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001306 }
1307}
1308
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001309/// CollectClassPropertyImplementations - This routine collects list of
1310/// properties to be implemented in the class. This includes, class's
1311/// and its conforming protocols' properties.
1312static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1313 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1314 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1315 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1316 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001317 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001318 PropMap[Prop->getIdentifier()] = Prop;
1319 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001320 for (ObjCInterfaceDecl::all_protocol_iterator
1321 PI = IDecl->all_referenced_protocol_begin(),
1322 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001323 CollectClassPropertyImplementations((*PI), PropMap);
1324 }
1325 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1326 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1327 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001328 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahanianac371502012-02-23 18:21:25 +00001329 if (!PropMap.count(Prop->getIdentifier()))
1330 PropMap[Prop->getIdentifier()] = Prop;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001331 }
1332 // scan through protocol's protocols.
1333 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1334 E = PDecl->protocol_end(); PI != E; ++PI)
1335 CollectClassPropertyImplementations((*PI), PropMap);
1336 }
1337}
1338
1339/// CollectSuperClassPropertyImplementations - This routine collects list of
1340/// properties to be implemented in super class(s) and also coming from their
1341/// conforming protocols.
1342static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1343 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1344 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1345 while (SDecl) {
1346 CollectClassPropertyImplementations(SDecl, PropMap);
1347 SDecl = SDecl->getSuperClass();
1348 }
1349 }
1350}
1351
Ted Kremenek9d64c152010-03-12 00:38:38 +00001352/// LookupPropertyDecl - Looks up a property in the current class and all
1353/// its protocols.
1354ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1355 IdentifierInfo *II) {
1356 if (const ObjCInterfaceDecl *IDecl =
1357 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1358 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1359 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001360 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001361 if (Prop->getIdentifier() == II)
1362 return Prop;
1363 }
1364 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001365 for (ObjCInterfaceDecl::all_protocol_iterator
1366 PI = IDecl->all_referenced_protocol_begin(),
1367 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001368 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1369 if (Prop)
1370 return Prop;
1371 }
1372 }
1373 else if (const ObjCProtocolDecl *PDecl =
1374 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1375 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1376 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001377 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001378 if (Prop->getIdentifier() == II)
1379 return Prop;
1380 }
1381 // scan through protocol's protocols.
1382 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1383 E = PDecl->protocol_end(); PI != E; ++PI) {
1384 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1385 if (Prop)
1386 return Prop;
1387 }
1388 }
1389 return 0;
1390}
1391
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001392static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1393 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001394 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001395 {
1396 llvm::raw_svector_ostream os(ivarName);
1397 os << '_' << Prop->getIdentifier()->getName();
1398 }
1399 return &Ctx.Idents.get(ivarName.str());
1400}
1401
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001402/// DefaultSynthesizeProperties - This routine default synthesizes all
1403/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001404void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1405 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001406
1407 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1408 CollectClassPropertyImplementations(IDecl, PropMap);
1409 if (PropMap.empty())
1410 return;
1411 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1412 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1413
1414 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1415 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1416 ObjCPropertyDecl *Prop = P->second;
1417 // If property to be implemented in the super class, ignore.
1418 if (SuperPropMap[Prop->getIdentifier()])
1419 continue;
1420 // Is there a matching propery synthesize/dynamic?
1421 if (Prop->isInvalidDecl() ||
1422 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1423 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1424 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001425 // Property may have been synthesized by user.
1426 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1427 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001428 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1429 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1430 continue;
1431 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1432 continue;
1433 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001434 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1435 // We won't auto-synthesize properties declared in protocols.
1436 Diag(IMPDecl->getLocation(),
1437 diag::warn_auto_synthesizing_protocol_property);
1438 Diag(Prop->getLocation(), diag::note_property_declare);
1439 continue;
1440 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001441
1442 // We use invalid SourceLocations for the synthesized ivars since they
1443 // aren't really synthesized at a particular location; they just exist.
1444 // Saying that they are located at the @implementation isn't really going
1445 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001446 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1447 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1448 true,
1449 /* property = */ Prop->getIdentifier(),
1450 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
1451 SourceLocation()));
1452 if (PIDecl) {
1453 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001454 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001455 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001456 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001457}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001458
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001459void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1460 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1461 return;
1462 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1463 if (!IC)
1464 return;
1465 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001466 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001467 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001468}
1469
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001470void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001471 ObjCContainerDecl *CDecl,
1472 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001473 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1474 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1475 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1476
Ted Kremenek9d64c152010-03-12 00:38:38 +00001477 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001478 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001479 if (PropMap.empty())
1480 return;
1481
1482 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1483 for (ObjCImplDecl::propimpl_iterator
1484 I = IMPDecl->propimpl_begin(),
1485 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001486 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001487
1488 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1489 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1490 ObjCPropertyDecl *Prop = P->second;
1491 // Is there a matching propery synthesize/dynamic?
1492 if (Prop->isInvalidDecl() ||
1493 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001494 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001495 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001496 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001497 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001498 isa<ObjCCategoryDecl>(CDecl) ?
1499 diag::warn_setter_getter_impl_required_in_category :
1500 diag::warn_setter_getter_impl_required)
1501 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001502 Diag(Prop->getLocation(),
1503 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001504 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1505 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001506 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001507 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1508
Ted Kremenek9d64c152010-03-12 00:38:38 +00001509 }
1510
1511 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001512 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001513 isa<ObjCCategoryDecl>(CDecl) ?
1514 diag::warn_setter_getter_impl_required_in_category :
1515 diag::warn_setter_getter_impl_required)
1516 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001517 Diag(Prop->getLocation(),
1518 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001519 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1520 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001521 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001522 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001523 }
1524 }
1525}
1526
1527void
1528Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1529 ObjCContainerDecl* IDecl) {
1530 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001531 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001532 return;
1533 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1534 E = IDecl->prop_end();
1535 I != E; ++I) {
David Blaikie262bc182012-04-30 02:36:29 +00001536 ObjCPropertyDecl *Property = &*I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001537 ObjCMethodDecl *GetterMethod = 0;
1538 ObjCMethodDecl *SetterMethod = 0;
1539 bool LookedUpGetterSetter = false;
1540
Ted Kremenek9d64c152010-03-12 00:38:38 +00001541 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001542 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001543
John McCall265941b2011-09-13 18:31:23 +00001544 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1545 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001546 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1547 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1548 LookedUpGetterSetter = true;
1549 if (GetterMethod) {
1550 Diag(GetterMethod->getLocation(),
1551 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001552 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001553 Diag(Property->getLocation(), diag::note_property_declare);
1554 }
1555 if (SetterMethod) {
1556 Diag(SetterMethod->getLocation(),
1557 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001558 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001559 Diag(Property->getLocation(), diag::note_property_declare);
1560 }
1561 }
1562
Ted Kremenek9d64c152010-03-12 00:38:38 +00001563 // We only care about readwrite atomic property.
1564 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1565 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1566 continue;
1567 if (const ObjCPropertyImplDecl *PIDecl
1568 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1569 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1570 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001571 if (!LookedUpGetterSetter) {
1572 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1573 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1574 LookedUpGetterSetter = true;
1575 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001576 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1577 SourceLocation MethodLoc =
1578 (GetterMethod ? GetterMethod->getLocation()
1579 : SetterMethod->getLocation());
1580 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001581 << Property->getIdentifier() << (GetterMethod != 0)
1582 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001583 // fixit stuff.
1584 if (!AttributesAsWritten) {
1585 if (Property->getLParenLoc().isValid()) {
1586 // @property () ... case.
1587 SourceRange PropSourceRange(Property->getAtLoc(),
1588 Property->getLParenLoc());
1589 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1590 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1591 }
1592 else {
1593 //@property id etc.
1594 SourceLocation endLoc =
1595 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1596 endLoc = endLoc.getLocWithOffset(-1);
1597 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1598 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1599 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1600 }
1601 }
1602 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1603 // @property () ... case.
1604 SourceLocation endLoc = Property->getLParenLoc();
1605 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1606 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1607 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1608 }
1609 else
1610 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001611 Diag(Property->getLocation(), diag::note_property_declare);
1612 }
1613 }
1614 }
1615}
1616
John McCallf85e1932011-06-15 23:02:42 +00001617void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001618 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001619 return;
1620
1621 for (ObjCImplementationDecl::propimpl_iterator
1622 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie262bc182012-04-30 02:36:29 +00001623 ObjCPropertyImplDecl *PID = &*i;
John McCallf85e1932011-06-15 23:02:42 +00001624 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1625 continue;
1626
1627 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001628 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1629 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001630 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1631 if (!method)
1632 continue;
1633 ObjCMethodFamily family = method->getMethodFamily();
1634 if (family == OMF_alloc || family == OMF_copy ||
1635 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001636 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001637 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1638 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001639 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001640 Diag(PD->getLocation(), diag::note_property_declare);
1641 }
1642 }
1643 }
1644}
1645
John McCall5de74d12010-11-10 07:01:40 +00001646/// AddPropertyAttrs - Propagates attributes from a property to the
1647/// implicitly-declared getter or setter for that property.
1648static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1649 ObjCPropertyDecl *Property) {
1650 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001651 for (Decl::attr_iterator A = Property->attr_begin(),
1652 AEnd = Property->attr_end();
1653 A != AEnd; ++A) {
1654 if (isa<DeprecatedAttr>(*A) ||
1655 isa<UnavailableAttr>(*A) ||
1656 isa<AvailabilityAttr>(*A))
1657 PropertyMethod->addAttr((*A)->clone(S.Context));
1658 }
John McCall5de74d12010-11-10 07:01:40 +00001659}
1660
Ted Kremenek9d64c152010-03-12 00:38:38 +00001661/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1662/// have the property type and issue diagnostics if they don't.
1663/// Also synthesize a getter/setter method if none exist (and update the
1664/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1665/// methods is the "right" thing to do.
1666void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001667 ObjCContainerDecl *CD,
1668 ObjCPropertyDecl *redeclaredProperty,
1669 ObjCContainerDecl *lexicalDC) {
1670
Ted Kremenek9d64c152010-03-12 00:38:38 +00001671 ObjCMethodDecl *GetterMethod, *SetterMethod;
1672
1673 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1674 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1675 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1676 property->getLocation());
1677
1678 if (SetterMethod) {
1679 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1680 property->getPropertyAttributes();
1681 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1682 Context.getCanonicalType(SetterMethod->getResultType()) !=
1683 Context.VoidTy)
1684 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1685 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001686 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001687 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1688 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001689 Diag(property->getLocation(),
1690 diag::warn_accessor_property_type_mismatch)
1691 << property->getDeclName()
1692 << SetterMethod->getSelector();
1693 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1694 }
1695 }
1696
1697 // Synthesize getter/setter methods if none exist.
1698 // Find the default getter and if one not found, add one.
1699 // FIXME: The synthesized property we set here is misleading. We almost always
1700 // synthesize these methods unless the user explicitly provided prototypes
1701 // (which is odd, but allowed). Sema should be typechecking that the
1702 // declarations jive in that situation (which it is not currently).
1703 if (!GetterMethod) {
1704 // No instance method of same name as property getter name was found.
1705 // Declare a getter method and add it to the list of methods
1706 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001707 SourceLocation Loc = redeclaredProperty ?
1708 redeclaredProperty->getLocation() :
1709 property->getLocation();
1710
1711 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1712 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001713 property->getType(), 0, CD, /*isInstance=*/true,
1714 /*isVariadic=*/false, /*isSynthesized=*/true,
1715 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001716 (property->getPropertyImplementation() ==
1717 ObjCPropertyDecl::Optional) ?
1718 ObjCMethodDecl::Optional :
1719 ObjCMethodDecl::Required);
1720 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001721
1722 AddPropertyAttrs(*this, GetterMethod, property);
1723
Ted Kremenek23173d72010-05-18 21:09:07 +00001724 // FIXME: Eventually this shouldn't be needed, as the lexical context
1725 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001726 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001727 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001728 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1729 GetterMethod->addAttr(
1730 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001731 } else
1732 // A user declared getter will be synthesize when @synthesize of
1733 // the property with the same name is seen in the @implementation
1734 GetterMethod->setSynthesized(true);
1735 property->setGetterMethodDecl(GetterMethod);
1736
1737 // Skip setter if property is read-only.
1738 if (!property->isReadOnly()) {
1739 // Find the default setter and if one not found, add one.
1740 if (!SetterMethod) {
1741 // No instance method of same name as property setter name was found.
1742 // Declare a setter method and add it to the list of methods
1743 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001744 SourceLocation Loc = redeclaredProperty ?
1745 redeclaredProperty->getLocation() :
1746 property->getLocation();
1747
1748 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001749 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001750 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001751 CD, /*isInstance=*/true, /*isVariadic=*/false,
1752 /*isSynthesized=*/true,
1753 /*isImplicitlyDeclared=*/true,
1754 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001755 (property->getPropertyImplementation() ==
1756 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001757 ObjCMethodDecl::Optional :
1758 ObjCMethodDecl::Required);
1759
Ted Kremenek9d64c152010-03-12 00:38:38 +00001760 // Invent the arguments for the setter. We don't bother making a
1761 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001762 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1763 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001764 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001765 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001766 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001767 SC_None,
1768 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001769 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001770 SetterMethod->setMethodParams(Context, Argument,
1771 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001772
1773 AddPropertyAttrs(*this, SetterMethod, property);
1774
Ted Kremenek9d64c152010-03-12 00:38:38 +00001775 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001776 // FIXME: Eventually this shouldn't be needed, as the lexical context
1777 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001778 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001779 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001780 } else
1781 // A user declared setter will be synthesize when @synthesize of
1782 // the property with the same name is seen in the @implementation
1783 SetterMethod->setSynthesized(true);
1784 property->setSetterMethodDecl(SetterMethod);
1785 }
1786 // Add any synthesized methods to the global pool. This allows us to
1787 // handle the following, which is supported by GCC (and part of the design).
1788 //
1789 // @interface Foo
1790 // @property double bar;
1791 // @end
1792 //
1793 // void thisIsUnfortunate() {
1794 // id foo;
1795 // double bar = [foo bar];
1796 // }
1797 //
1798 if (GetterMethod)
1799 AddInstanceMethodToGlobalPool(GetterMethod);
1800 if (SetterMethod)
1801 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001802
1803 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1804 if (!CurrentClass) {
1805 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1806 CurrentClass = Cat->getClassInterface();
1807 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1808 CurrentClass = Impl->getClassInterface();
1809 }
1810 if (GetterMethod)
1811 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1812 if (SetterMethod)
1813 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001814}
1815
John McCalld226f652010-08-21 09:40:31 +00001816void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001817 SourceLocation Loc,
1818 unsigned &Attributes) {
1819 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001820 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001821 return;
1822
1823 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001824 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001825
David Blaikie4e4d0842012-03-11 07:00:24 +00001826 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001827 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1828 PropertyTy->isObjCRetainableType()) {
1829 // 'readonly' property with no obvious lifetime.
1830 // its life time will be determined by its backing ivar.
1831 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1832 ObjCDeclSpec::DQ_PR_copy |
1833 ObjCDeclSpec::DQ_PR_retain |
1834 ObjCDeclSpec::DQ_PR_strong |
1835 ObjCDeclSpec::DQ_PR_weak |
1836 ObjCDeclSpec::DQ_PR_assign);
1837 if ((Attributes & rel) == 0)
1838 return;
1839 }
1840
Ted Kremenek9d64c152010-03-12 00:38:38 +00001841 // readonly and readwrite/assign/retain/copy conflict.
1842 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1843 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1844 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001845 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001846 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001847 ObjCDeclSpec::DQ_PR_retain |
1848 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001849 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1850 "readwrite" :
1851 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1852 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001853 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1854 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001855 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1856 "copy" : "retain";
1857
1858 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1859 diag::err_objc_property_attr_mutually_exclusive :
1860 diag::warn_objc_property_attr_mutually_exclusive)
1861 << "readonly" << which;
1862 }
1863
1864 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001865 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1866 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1867 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001868 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001869 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001870 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1871 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1872 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1873 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00001874 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001875 }
1876
1877 // Check for more than one of { assign, copy, retain }.
1878 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1879 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1880 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1881 << "assign" << "copy";
1882 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1883 }
1884 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1885 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1886 << "assign" << "retain";
1887 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1888 }
John McCallf85e1932011-06-15 23:02:42 +00001889 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1890 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1891 << "assign" << "strong";
1892 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1893 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001894 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001895 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1896 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1897 << "assign" << "weak";
1898 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1899 }
1900 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1901 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1902 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1903 << "unsafe_unretained" << "copy";
1904 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1905 }
1906 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1907 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1908 << "unsafe_unretained" << "retain";
1909 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1910 }
1911 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1912 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1913 << "unsafe_unretained" << "strong";
1914 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1915 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001916 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001917 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1918 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1919 << "unsafe_unretained" << "weak";
1920 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1921 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001922 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1923 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1924 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1925 << "copy" << "retain";
1926 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1927 }
John McCallf85e1932011-06-15 23:02:42 +00001928 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1929 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1930 << "copy" << "strong";
1931 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1932 }
1933 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1934 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1935 << "copy" << "weak";
1936 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1937 }
1938 }
1939 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1940 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1941 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1942 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001943 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001944 }
1945 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1946 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1947 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1948 << "strong" << "weak";
1949 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001950 }
1951
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00001952 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
1953 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
1954 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1955 << "atomic" << "nonatomic";
1956 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
1957 }
1958
Ted Kremenek9d64c152010-03-12 00:38:38 +00001959 // Warn if user supplied no assignment attribute, property is
1960 // readwrite, and this is an object type.
1961 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001962 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1963 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1964 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001965 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001966 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001967 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001968 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001969 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001970 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001971 bool isAnyClassTy =
1972 (PropertyTy->isObjCClassType() ||
1973 PropertyTy->isObjCQualifiedClassType());
1974 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
1975 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00001976 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001977 ;
1978 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001979 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00001980 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001981 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001982
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001983 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00001984 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001985 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001986 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001987 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001988
1989 // FIXME: Implement warning dependent on NSCopying being
1990 // implemented. See also:
1991 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1992 // (please trim this list while you are at it).
1993 }
1994
1995 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001996 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00001997 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001998 && PropertyTy->isBlockPointerType())
1999 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
David Blaikie4e4d0842012-03-11 07:00:24 +00002000 else if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002001 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2002 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2003 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2004 PropertyTy->isBlockPointerType())
2005 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002006
2007 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2008 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2009 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2010
Ted Kremenek9d64c152010-03-12 00:38:38 +00002011}