blob: e826b47784b360caa4535620fc698b6c7dea43dc [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) {
1053 if (GetterMethod &&
John McCall3c3b7f92011-10-25 17:37:35 +00001054 !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(),
1055 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001056 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +00001057 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +00001058 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +00001059 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001060 if (result != Compatible) {
1061 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1062 << property->getDeclName()
1063 << GetterMethod->getSelector();
1064 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1065 return true;
1066 }
1067 }
1068 return false;
1069}
1070
1071/// ComparePropertiesInBaseAndSuper - This routine compares property
1072/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001073/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001074///
1075void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1076 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1077 if (!SDecl)
1078 return;
1079 // FIXME: O(N^2)
1080 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1081 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie262bc182012-04-30 02:36:29 +00001082 ObjCPropertyDecl *SuperPDecl = &*S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001083 // Does property in super class has declaration in current class?
1084 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1085 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie262bc182012-04-30 02:36:29 +00001086 ObjCPropertyDecl *PDecl = &*I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001087 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1088 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1089 SDecl->getIdentifier());
1090 }
1091 }
1092}
1093
1094/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1095/// of properties declared in a protocol and compares their attribute against
1096/// the same property declared in the class or category.
1097void
1098Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1099 ObjCProtocolDecl *PDecl) {
1100 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1101 if (!IDecl) {
1102 // Category
1103 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1104 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1105 if (!CatDecl->IsClassExtension())
1106 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1107 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001108 ObjCPropertyDecl *Pr = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001109 ObjCCategoryDecl::prop_iterator CP, CE;
1110 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001111 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001112 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001113 break;
1114 if (CP != CE)
1115 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie262bc182012-04-30 02:36:29 +00001116 DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001117 }
1118 return;
1119 }
1120 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1121 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001122 ObjCPropertyDecl *Pr = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001123 ObjCInterfaceDecl::prop_iterator CP, CE;
1124 // Is this property already in class's list of properties?
1125 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001126 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001127 break;
1128 if (CP != CE)
1129 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie262bc182012-04-30 02:36:29 +00001130 DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001131 }
1132}
1133
1134/// CompareProperties - This routine compares properties
1135/// declared in 'ClassOrProtocol' objects (which can be a class or an
1136/// inherited protocol with the list of properties for class/category 'CDecl'
1137///
John McCalld226f652010-08-21 09:40:31 +00001138void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1139 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001140 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1141
1142 if (!IDecl) {
1143 // Category
1144 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1145 assert (CatDecl && "CompareProperties");
1146 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1147 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1148 E = MDecl->protocol_end(); P != E; ++P)
1149 // Match properties of category with those of protocol (*P)
1150 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1151
1152 // Go thru the list of protocols for this category and recursively match
1153 // their properties with those in the category.
1154 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1155 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001156 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001157 } else {
1158 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1159 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1160 E = MD->protocol_end(); P != E; ++P)
1161 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1162 }
1163 return;
1164 }
1165
1166 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001167 for (ObjCInterfaceDecl::all_protocol_iterator
1168 P = MDecl->all_referenced_protocol_begin(),
1169 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001170 // Match properties of class IDecl with those of protocol (*P).
1171 MatchOneProtocolPropertiesInClass(IDecl, *P);
1172
1173 // Go thru the list of protocols for this class and recursively match
1174 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001175 for (ObjCInterfaceDecl::all_protocol_iterator
1176 P = IDecl->all_referenced_protocol_begin(),
1177 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001178 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001179 } else {
1180 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1181 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1182 E = MD->protocol_end(); P != E; ++P)
1183 MatchOneProtocolPropertiesInClass(IDecl, *P);
1184 }
1185}
1186
1187/// isPropertyReadonly - Return true if property is readonly, by searching
1188/// for the property in the class and in its categories and implementations
1189///
1190bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1191 ObjCInterfaceDecl *IDecl) {
1192 // by far the most common case.
1193 if (!PDecl->isReadOnly())
1194 return false;
1195 // Even if property is ready only, if interface has a user defined setter,
1196 // it is not considered read only.
1197 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1198 return false;
1199
1200 // Main class has the property as 'readonly'. Must search
1201 // through the category list to see if the property's
1202 // attribute has been over-ridden to 'readwrite'.
1203 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1204 Category; Category = Category->getNextClassCategory()) {
1205 // Even if property is ready only, if a category has a user defined setter,
1206 // it is not considered read only.
1207 if (Category->getInstanceMethod(PDecl->getSetterName()))
1208 return false;
1209 ObjCPropertyDecl *P =
1210 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1211 if (P && !P->isReadOnly())
1212 return false;
1213 }
1214
1215 // Also, check for definition of a setter method in the implementation if
1216 // all else failed.
1217 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1218 if (ObjCImplementationDecl *IMD =
1219 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1220 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1221 return false;
1222 } else if (ObjCCategoryImplDecl *CIMD =
1223 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1224 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1225 return false;
1226 }
1227 }
1228 // Lastly, look through the implementation (if one is in scope).
1229 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1230 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1231 return false;
1232 // If all fails, look at the super class.
1233 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1234 return isPropertyReadonly(PDecl, SIDecl);
1235 return true;
1236}
1237
1238/// CollectImmediateProperties - This routine collects all properties in
1239/// the class and its conforming protocols; but not those it its super class.
1240void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001241 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1242 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001243 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1244 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1245 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001246 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001247 PropMap[Prop->getIdentifier()] = Prop;
1248 }
1249 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001250 for (ObjCInterfaceDecl::all_protocol_iterator
1251 PI = IDecl->all_referenced_protocol_begin(),
1252 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001253 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001254 }
1255 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1256 if (!CATDecl->IsClassExtension())
1257 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1258 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001259 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001260 PropMap[Prop->getIdentifier()] = Prop;
1261 }
1262 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001263 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001264 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001265 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001266 }
1267 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1268 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1269 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001270 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001271 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1272 // Exclude property for protocols which conform to class's super-class,
1273 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001274 if (!PropertyFromSuper ||
1275 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001276 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1277 if (!PropEntry)
1278 PropEntry = Prop;
1279 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001280 }
1281 // scan through protocol's protocols.
1282 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1283 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001284 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001285 }
1286}
1287
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001288/// CollectClassPropertyImplementations - This routine collects list of
1289/// properties to be implemented in the class. This includes, class's
1290/// and its conforming protocols' properties.
1291static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1292 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1293 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1294 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1295 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001296 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001297 PropMap[Prop->getIdentifier()] = Prop;
1298 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001299 for (ObjCInterfaceDecl::all_protocol_iterator
1300 PI = IDecl->all_referenced_protocol_begin(),
1301 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001302 CollectClassPropertyImplementations((*PI), PropMap);
1303 }
1304 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1305 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1306 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001307 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahanianac371502012-02-23 18:21:25 +00001308 if (!PropMap.count(Prop->getIdentifier()))
1309 PropMap[Prop->getIdentifier()] = Prop;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001310 }
1311 // scan through protocol's protocols.
1312 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1313 E = PDecl->protocol_end(); PI != E; ++PI)
1314 CollectClassPropertyImplementations((*PI), PropMap);
1315 }
1316}
1317
1318/// CollectSuperClassPropertyImplementations - This routine collects list of
1319/// properties to be implemented in super class(s) and also coming from their
1320/// conforming protocols.
1321static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1322 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1323 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1324 while (SDecl) {
1325 CollectClassPropertyImplementations(SDecl, PropMap);
1326 SDecl = SDecl->getSuperClass();
1327 }
1328 }
1329}
1330
Ted Kremenek9d64c152010-03-12 00:38:38 +00001331/// LookupPropertyDecl - Looks up a property in the current class and all
1332/// its protocols.
1333ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1334 IdentifierInfo *II) {
1335 if (const ObjCInterfaceDecl *IDecl =
1336 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1337 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1338 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001339 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001340 if (Prop->getIdentifier() == II)
1341 return Prop;
1342 }
1343 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001344 for (ObjCInterfaceDecl::all_protocol_iterator
1345 PI = IDecl->all_referenced_protocol_begin(),
1346 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001347 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1348 if (Prop)
1349 return Prop;
1350 }
1351 }
1352 else if (const ObjCProtocolDecl *PDecl =
1353 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1354 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1355 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001356 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001357 if (Prop->getIdentifier() == II)
1358 return Prop;
1359 }
1360 // scan through protocol's protocols.
1361 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1362 E = PDecl->protocol_end(); PI != E; ++PI) {
1363 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1364 if (Prop)
1365 return Prop;
1366 }
1367 }
1368 return 0;
1369}
1370
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001371static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1372 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001373 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001374 {
1375 llvm::raw_svector_ostream os(ivarName);
1376 os << '_' << Prop->getIdentifier()->getName();
1377 }
1378 return &Ctx.Idents.get(ivarName.str());
1379}
1380
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001381/// DefaultSynthesizeProperties - This routine default synthesizes all
1382/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001383void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1384 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001385
1386 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1387 CollectClassPropertyImplementations(IDecl, PropMap);
1388 if (PropMap.empty())
1389 return;
1390 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1391 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1392
1393 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1394 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1395 ObjCPropertyDecl *Prop = P->second;
1396 // If property to be implemented in the super class, ignore.
1397 if (SuperPropMap[Prop->getIdentifier()])
1398 continue;
1399 // Is there a matching propery synthesize/dynamic?
1400 if (Prop->isInvalidDecl() ||
1401 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1402 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1403 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001404 // Property may have been synthesized by user.
1405 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1406 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001407 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1408 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1409 continue;
1410 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1411 continue;
1412 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001413 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1414 // We won't auto-synthesize properties declared in protocols.
1415 Diag(IMPDecl->getLocation(),
1416 diag::warn_auto_synthesizing_protocol_property);
1417 Diag(Prop->getLocation(), diag::note_property_declare);
1418 continue;
1419 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001420
1421 // We use invalid SourceLocations for the synthesized ivars since they
1422 // aren't really synthesized at a particular location; they just exist.
1423 // Saying that they are located at the @implementation isn't really going
1424 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001425 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1426 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1427 true,
1428 /* property = */ Prop->getIdentifier(),
1429 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
1430 SourceLocation()));
1431 if (PIDecl) {
1432 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001433 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001434 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001435 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001436}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001437
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001438void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1439 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1440 return;
1441 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1442 if (!IC)
1443 return;
1444 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001445 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001446 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001447}
1448
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001449void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001450 ObjCContainerDecl *CDecl,
1451 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001452 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1453 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1454 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1455
Ted Kremenek9d64c152010-03-12 00:38:38 +00001456 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001457 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001458 if (PropMap.empty())
1459 return;
1460
1461 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1462 for (ObjCImplDecl::propimpl_iterator
1463 I = IMPDecl->propimpl_begin(),
1464 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001465 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001466
1467 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1468 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1469 ObjCPropertyDecl *Prop = P->second;
1470 // Is there a matching propery synthesize/dynamic?
1471 if (Prop->isInvalidDecl() ||
1472 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001473 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001474 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001475 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001476 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001477 isa<ObjCCategoryDecl>(CDecl) ?
1478 diag::warn_setter_getter_impl_required_in_category :
1479 diag::warn_setter_getter_impl_required)
1480 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001481 Diag(Prop->getLocation(),
1482 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001483 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1484 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001485 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001486 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1487
Ted Kremenek9d64c152010-03-12 00:38:38 +00001488 }
1489
1490 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001491 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001492 isa<ObjCCategoryDecl>(CDecl) ?
1493 diag::warn_setter_getter_impl_required_in_category :
1494 diag::warn_setter_getter_impl_required)
1495 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001496 Diag(Prop->getLocation(),
1497 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001498 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1499 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001500 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001501 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001502 }
1503 }
1504}
1505
1506void
1507Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1508 ObjCContainerDecl* IDecl) {
1509 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001510 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001511 return;
1512 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1513 E = IDecl->prop_end();
1514 I != E; ++I) {
David Blaikie262bc182012-04-30 02:36:29 +00001515 ObjCPropertyDecl *Property = &*I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001516 ObjCMethodDecl *GetterMethod = 0;
1517 ObjCMethodDecl *SetterMethod = 0;
1518 bool LookedUpGetterSetter = false;
1519
Ted Kremenek9d64c152010-03-12 00:38:38 +00001520 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001521 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001522
John McCall265941b2011-09-13 18:31:23 +00001523 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1524 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001525 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1526 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1527 LookedUpGetterSetter = true;
1528 if (GetterMethod) {
1529 Diag(GetterMethod->getLocation(),
1530 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001531 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001532 Diag(Property->getLocation(), diag::note_property_declare);
1533 }
1534 if (SetterMethod) {
1535 Diag(SetterMethod->getLocation(),
1536 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001537 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001538 Diag(Property->getLocation(), diag::note_property_declare);
1539 }
1540 }
1541
Ted Kremenek9d64c152010-03-12 00:38:38 +00001542 // We only care about readwrite atomic property.
1543 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1544 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1545 continue;
1546 if (const ObjCPropertyImplDecl *PIDecl
1547 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1548 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1549 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001550 if (!LookedUpGetterSetter) {
1551 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1552 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1553 LookedUpGetterSetter = true;
1554 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001555 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1556 SourceLocation MethodLoc =
1557 (GetterMethod ? GetterMethod->getLocation()
1558 : SetterMethod->getLocation());
1559 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001560 << Property->getIdentifier() << (GetterMethod != 0)
1561 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001562 // fixit stuff.
1563 if (!AttributesAsWritten) {
1564 if (Property->getLParenLoc().isValid()) {
1565 // @property () ... case.
1566 SourceRange PropSourceRange(Property->getAtLoc(),
1567 Property->getLParenLoc());
1568 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1569 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1570 }
1571 else {
1572 //@property id etc.
1573 SourceLocation endLoc =
1574 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1575 endLoc = endLoc.getLocWithOffset(-1);
1576 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1577 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1578 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1579 }
1580 }
1581 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1582 // @property () ... case.
1583 SourceLocation endLoc = Property->getLParenLoc();
1584 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1585 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1586 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1587 }
1588 else
1589 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001590 Diag(Property->getLocation(), diag::note_property_declare);
1591 }
1592 }
1593 }
1594}
1595
John McCallf85e1932011-06-15 23:02:42 +00001596void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001597 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001598 return;
1599
1600 for (ObjCImplementationDecl::propimpl_iterator
1601 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie262bc182012-04-30 02:36:29 +00001602 ObjCPropertyImplDecl *PID = &*i;
John McCallf85e1932011-06-15 23:02:42 +00001603 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1604 continue;
1605
1606 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001607 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1608 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001609 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1610 if (!method)
1611 continue;
1612 ObjCMethodFamily family = method->getMethodFamily();
1613 if (family == OMF_alloc || family == OMF_copy ||
1614 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001615 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001616 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1617 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001618 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001619 Diag(PD->getLocation(), diag::note_property_declare);
1620 }
1621 }
1622 }
1623}
1624
John McCall5de74d12010-11-10 07:01:40 +00001625/// AddPropertyAttrs - Propagates attributes from a property to the
1626/// implicitly-declared getter or setter for that property.
1627static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1628 ObjCPropertyDecl *Property) {
1629 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001630 for (Decl::attr_iterator A = Property->attr_begin(),
1631 AEnd = Property->attr_end();
1632 A != AEnd; ++A) {
1633 if (isa<DeprecatedAttr>(*A) ||
1634 isa<UnavailableAttr>(*A) ||
1635 isa<AvailabilityAttr>(*A))
1636 PropertyMethod->addAttr((*A)->clone(S.Context));
1637 }
John McCall5de74d12010-11-10 07:01:40 +00001638}
1639
Ted Kremenek9d64c152010-03-12 00:38:38 +00001640/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1641/// have the property type and issue diagnostics if they don't.
1642/// Also synthesize a getter/setter method if none exist (and update the
1643/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1644/// methods is the "right" thing to do.
1645void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001646 ObjCContainerDecl *CD,
1647 ObjCPropertyDecl *redeclaredProperty,
1648 ObjCContainerDecl *lexicalDC) {
1649
Ted Kremenek9d64c152010-03-12 00:38:38 +00001650 ObjCMethodDecl *GetterMethod, *SetterMethod;
1651
1652 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1653 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1654 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1655 property->getLocation());
1656
1657 if (SetterMethod) {
1658 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1659 property->getPropertyAttributes();
1660 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1661 Context.getCanonicalType(SetterMethod->getResultType()) !=
1662 Context.VoidTy)
1663 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1664 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001665 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001666 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1667 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001668 Diag(property->getLocation(),
1669 diag::warn_accessor_property_type_mismatch)
1670 << property->getDeclName()
1671 << SetterMethod->getSelector();
1672 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1673 }
1674 }
1675
1676 // Synthesize getter/setter methods if none exist.
1677 // Find the default getter and if one not found, add one.
1678 // FIXME: The synthesized property we set here is misleading. We almost always
1679 // synthesize these methods unless the user explicitly provided prototypes
1680 // (which is odd, but allowed). Sema should be typechecking that the
1681 // declarations jive in that situation (which it is not currently).
1682 if (!GetterMethod) {
1683 // No instance method of same name as property getter name was found.
1684 // Declare a getter method and add it to the list of methods
1685 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001686 SourceLocation Loc = redeclaredProperty ?
1687 redeclaredProperty->getLocation() :
1688 property->getLocation();
1689
1690 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1691 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001692 property->getType(), 0, CD, /*isInstance=*/true,
1693 /*isVariadic=*/false, /*isSynthesized=*/true,
1694 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001695 (property->getPropertyImplementation() ==
1696 ObjCPropertyDecl::Optional) ?
1697 ObjCMethodDecl::Optional :
1698 ObjCMethodDecl::Required);
1699 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001700
1701 AddPropertyAttrs(*this, GetterMethod, property);
1702
Ted Kremenek23173d72010-05-18 21:09:07 +00001703 // FIXME: Eventually this shouldn't be needed, as the lexical context
1704 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001705 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001706 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001707 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1708 GetterMethod->addAttr(
1709 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001710 } else
1711 // A user declared getter will be synthesize when @synthesize of
1712 // the property with the same name is seen in the @implementation
1713 GetterMethod->setSynthesized(true);
1714 property->setGetterMethodDecl(GetterMethod);
1715
1716 // Skip setter if property is read-only.
1717 if (!property->isReadOnly()) {
1718 // Find the default setter and if one not found, add one.
1719 if (!SetterMethod) {
1720 // No instance method of same name as property setter name was found.
1721 // Declare a setter method and add it to the list of methods
1722 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001723 SourceLocation Loc = redeclaredProperty ?
1724 redeclaredProperty->getLocation() :
1725 property->getLocation();
1726
1727 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001728 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001729 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001730 CD, /*isInstance=*/true, /*isVariadic=*/false,
1731 /*isSynthesized=*/true,
1732 /*isImplicitlyDeclared=*/true,
1733 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001734 (property->getPropertyImplementation() ==
1735 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001736 ObjCMethodDecl::Optional :
1737 ObjCMethodDecl::Required);
1738
Ted Kremenek9d64c152010-03-12 00:38:38 +00001739 // Invent the arguments for the setter. We don't bother making a
1740 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001741 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1742 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001743 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001744 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001745 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001746 SC_None,
1747 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001748 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001749 SetterMethod->setMethodParams(Context, Argument,
1750 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001751
1752 AddPropertyAttrs(*this, SetterMethod, property);
1753
Ted Kremenek9d64c152010-03-12 00:38:38 +00001754 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001755 // FIXME: Eventually this shouldn't be needed, as the lexical context
1756 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001757 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001758 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001759 } else
1760 // A user declared setter will be synthesize when @synthesize of
1761 // the property with the same name is seen in the @implementation
1762 SetterMethod->setSynthesized(true);
1763 property->setSetterMethodDecl(SetterMethod);
1764 }
1765 // Add any synthesized methods to the global pool. This allows us to
1766 // handle the following, which is supported by GCC (and part of the design).
1767 //
1768 // @interface Foo
1769 // @property double bar;
1770 // @end
1771 //
1772 // void thisIsUnfortunate() {
1773 // id foo;
1774 // double bar = [foo bar];
1775 // }
1776 //
1777 if (GetterMethod)
1778 AddInstanceMethodToGlobalPool(GetterMethod);
1779 if (SetterMethod)
1780 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001781
1782 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1783 if (!CurrentClass) {
1784 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1785 CurrentClass = Cat->getClassInterface();
1786 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1787 CurrentClass = Impl->getClassInterface();
1788 }
1789 if (GetterMethod)
1790 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1791 if (SetterMethod)
1792 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001793}
1794
John McCalld226f652010-08-21 09:40:31 +00001795void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001796 SourceLocation Loc,
1797 unsigned &Attributes) {
1798 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001799 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001800 return;
1801
1802 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001803 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001804
David Blaikie4e4d0842012-03-11 07:00:24 +00001805 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001806 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1807 PropertyTy->isObjCRetainableType()) {
1808 // 'readonly' property with no obvious lifetime.
1809 // its life time will be determined by its backing ivar.
1810 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1811 ObjCDeclSpec::DQ_PR_copy |
1812 ObjCDeclSpec::DQ_PR_retain |
1813 ObjCDeclSpec::DQ_PR_strong |
1814 ObjCDeclSpec::DQ_PR_weak |
1815 ObjCDeclSpec::DQ_PR_assign);
1816 if ((Attributes & rel) == 0)
1817 return;
1818 }
1819
Ted Kremenek9d64c152010-03-12 00:38:38 +00001820 // readonly and readwrite/assign/retain/copy conflict.
1821 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1822 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1823 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001824 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001825 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001826 ObjCDeclSpec::DQ_PR_retain |
1827 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001828 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1829 "readwrite" :
1830 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1831 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001832 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1833 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001834 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1835 "copy" : "retain";
1836
1837 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1838 diag::err_objc_property_attr_mutually_exclusive :
1839 diag::warn_objc_property_attr_mutually_exclusive)
1840 << "readonly" << which;
1841 }
1842
1843 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001844 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1845 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1846 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001847 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001848 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001849 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1850 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1851 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1852 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00001853 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001854 }
1855
1856 // Check for more than one of { assign, copy, retain }.
1857 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1858 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1859 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1860 << "assign" << "copy";
1861 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1862 }
1863 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1864 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1865 << "assign" << "retain";
1866 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1867 }
John McCallf85e1932011-06-15 23:02:42 +00001868 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1869 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1870 << "assign" << "strong";
1871 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1872 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001873 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001874 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1875 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1876 << "assign" << "weak";
1877 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1878 }
1879 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1880 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1881 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1882 << "unsafe_unretained" << "copy";
1883 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1884 }
1885 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1886 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1887 << "unsafe_unretained" << "retain";
1888 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1889 }
1890 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1891 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1892 << "unsafe_unretained" << "strong";
1893 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1894 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001895 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001896 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1897 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1898 << "unsafe_unretained" << "weak";
1899 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1900 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001901 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1902 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1903 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1904 << "copy" << "retain";
1905 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1906 }
John McCallf85e1932011-06-15 23:02:42 +00001907 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1908 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1909 << "copy" << "strong";
1910 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1911 }
1912 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1913 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1914 << "copy" << "weak";
1915 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1916 }
1917 }
1918 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1919 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1920 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1921 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001922 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001923 }
1924 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1925 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1926 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1927 << "strong" << "weak";
1928 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001929 }
1930
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00001931 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
1932 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
1933 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1934 << "atomic" << "nonatomic";
1935 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
1936 }
1937
Ted Kremenek9d64c152010-03-12 00:38:38 +00001938 // Warn if user supplied no assignment attribute, property is
1939 // readwrite, and this is an object type.
1940 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001941 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1942 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1943 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001944 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001945 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001946 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001947 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001948 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001949 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001950 bool isAnyClassTy =
1951 (PropertyTy->isObjCClassType() ||
1952 PropertyTy->isObjCQualifiedClassType());
1953 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
1954 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00001955 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001956 ;
1957 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001958 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00001959 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001960 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001961
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001962 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00001963 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001964 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001965 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001966 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001967
1968 // FIXME: Implement warning dependent on NSCopying being
1969 // implemented. See also:
1970 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1971 // (please trim this list while you are at it).
1972 }
1973
1974 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001975 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00001976 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001977 && PropertyTy->isBlockPointerType())
1978 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
David Blaikie4e4d0842012-03-11 07:00:24 +00001979 else if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001980 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1981 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1982 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1983 PropertyTy->isBlockPointerType())
1984 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00001985
1986 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1987 (Attributes & ObjCDeclSpec::DQ_PR_setter))
1988 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
1989
Ted Kremenek9d64c152010-03-12 00:38:38 +00001990}