blob: 5a98077bd7f37e5f004ac60ab29a3ebf51728cae [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;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000661 // Check that we have a valid, previously declared ivar for @synthesize
662 if (Synthesize) {
663 // @synthesize
664 if (!PropertyIvar)
665 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000666 // Check that this is a previously declared 'ivar' in 'IDecl' interface
667 ObjCInterfaceDecl *ClassDeclared;
668 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
669 QualType PropType = property->getType();
670 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000671
672 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000673 diag::err_incomplete_synthesized_property,
674 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000675 Diag(property->getLocation(), diag::note_property_declare);
676 CompleteTypeErr = true;
677 }
678
David Blaikie4e4d0842012-03-11 07:00:24 +0000679 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000680 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000681 ObjCPropertyDecl::OBJC_PR_readonly) &&
682 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000683 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
684 }
685
John McCallf85e1932011-06-15 23:02:42 +0000686 ObjCPropertyDecl::PropertyAttributeKind kind
687 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000688
689 // Add GC __weak to the ivar type if the property is weak.
690 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000691 getLangOpts().getGC() != LangOptions::NonGC) {
692 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000693 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000694 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000695 Diag(property->getLocation(), diag::note_property_declare);
696 } else {
697 PropertyIvarType =
698 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000699 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000700 }
John McCall265941b2011-09-13 18:31:23 +0000701
Ted Kremenek28685ab2010-03-12 00:46:40 +0000702 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000703 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000704 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000705 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000706 !PropertyIvarType.getObjCLifetime() &&
707 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000708
John McCall265941b2011-09-13 18:31:23 +0000709 // It's an error if we have to do this and the user didn't
710 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000711 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000712 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000713 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000714 diag::err_arc_objc_property_default_assign_on_object);
715 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000716 } else {
717 Qualifiers::ObjCLifetime lifetime =
718 getImpliedARCOwnership(kind, PropertyIvarType);
719 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000720 if (lifetime == Qualifiers::OCL_Weak) {
721 bool err = false;
722 if (const ObjCObjectPointerType *ObjT =
723 PropertyIvarType->getAs<ObjCObjectPointerType>())
724 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000725 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000726 Diag(property->getLocation(), diag::note_property_declare);
727 err = true;
728 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000729 if (!err && !getLangOpts().ObjCRuntimeHasWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000730 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000731 Diag(property->getLocation(), diag::note_property_declare);
732 }
John McCallf85e1932011-06-15 23:02:42 +0000733 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000734
John McCallf85e1932011-06-15 23:02:42 +0000735 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000736 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000737 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
738 }
John McCallf85e1932011-06-15 23:02:42 +0000739 }
740
741 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000742 !getLangOpts().ObjCAutoRefCount &&
743 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000744 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000745 Diag(property->getLocation(), diag::note_property_declare);
746 }
747
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000748 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000749 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000750 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000751 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000752 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000753 if (CompleteTypeErr)
754 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000755 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000756 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000757 property->setPropertyIvarDecl(Ivar);
758
David Blaikie4e4d0842012-03-11 07:00:24 +0000759 if (!getLangOpts().ObjCNonFragileABI)
Eli Friedmane4c043d2012-05-01 22:26:06 +0000760 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
761 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000762 // Note! I deliberately want it to fall thru so, we have a
763 // a property implementation and to avoid future warnings.
David Blaikie4e4d0842012-03-11 07:00:24 +0000764 } else if (getLangOpts().ObjCNonFragileABI &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000765 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000766 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000767 << property->getDeclName() << Ivar->getDeclName()
768 << ClassDeclared->getDeclName();
769 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000770 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000771 // Note! I deliberately want it to fall thru so more errors are caught.
772 }
773 QualType IvarType = Context.getCanonicalType(Ivar->getType());
774
775 // Check that type of property and its ivar are type compatible.
John McCall265941b2011-09-13 18:31:23 +0000776 if (Context.getCanonicalType(PropertyIvarType) != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000777 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000778 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000779 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000780 compat =
781 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000782 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000783 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000784 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000785 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
786 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000787 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000788 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000789 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000790 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000791 << property->getDeclName() << PropType
792 << Ivar->getDeclName() << IvarType;
793 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000794 // Note! I deliberately want it to fall thru so, we have a
795 // a property implementation and to avoid future warnings.
796 }
797
798 // FIXME! Rules for properties are somewhat different that those
799 // for assignments. Use a new routine to consolidate all cases;
800 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000801 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000802 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
803 if (lhsType != rhsType &&
804 lhsType->isArithmeticType()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000805 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000806 << property->getDeclName() << PropType
807 << Ivar->getDeclName() << IvarType;
808 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000809 // Fall thru - see previous comment
810 }
811 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000812 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000813 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000814 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000815 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000816 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000817 // Fall thru - see previous comment
818 }
John McCallf85e1932011-06-15 23:02:42 +0000819 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000820 if ((property->getType()->isObjCObjectPointerType() ||
821 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000822 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000823 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000824 << property->getDeclName() << Ivar->getDeclName();
825 // Fall thru - see previous comment
826 }
827 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000828 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000829 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000830 } else if (PropertyIvar)
831 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +0000832 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000833
Ted Kremenek28685ab2010-03-12 00:46:40 +0000834 assert (property && "ActOnPropertyImplDecl - property declaration missing");
835 ObjCPropertyImplDecl *PIDecl =
836 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
837 property,
838 (Synthesize ?
839 ObjCPropertyImplDecl::Synthesize
840 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000841 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000842
843 if (CompleteTypeErr)
844 PIDecl->setInvalidDecl();
845
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000846 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
847 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000848 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000849 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000850 // For Objective-C++, need to synthesize the AST for the IVAR object to be
851 // returned by the getter as it must conform to C++'s copy-return rules.
852 // FIXME. Eventually we want to do this for Objective-C as well.
853 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
854 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +0000855 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000856 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000857 Expr *IvarRefExpr =
858 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
859 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000860 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000861 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000862 SourceLocation(),
863 getterMethod->getResultType(),
864 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000865 SourceLocation(),
866 Owned(IvarRefExpr));
867 if (!Res.isInvalid()) {
868 Expr *ResExpr = Res.takeAs<Expr>();
869 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000870 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000871 PIDecl->setGetterCXXConstructor(ResExpr);
872 }
873 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000874 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
875 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
876 Diag(getterMethod->getLocation(),
877 diag::warn_property_getter_owning_mismatch);
878 Diag(property->getLocation(), diag::note_property_declare);
879 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000880 }
881 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
882 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000883 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
884 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000885 // FIXME. Eventually we want to do this for Objective-C as well.
886 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
887 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +0000888 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000889 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000890 Expr *lhs =
891 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
892 SelfExpr, true, true);
893 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
894 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +0000895 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +0000896 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +0000897 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000898 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000899 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000900 if (property->getPropertyAttributes() &
901 ObjCPropertyDecl::OBJC_PR_atomic) {
902 Expr *callExpr = Res.takeAs<Expr>();
903 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000904 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
905 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000906 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000907 if (property->getType()->isReferenceType()) {
908 Diag(PropertyLoc,
909 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000910 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000911 Diag(FuncDecl->getLocStart(),
912 diag::note_callee_decl) << FuncDecl;
913 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000914 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000915 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
916 }
917 }
918
Ted Kremenek28685ab2010-03-12 00:46:40 +0000919 if (IC) {
920 if (Synthesize)
921 if (ObjCPropertyImplDecl *PPIDecl =
922 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
923 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
924 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
925 << PropertyIvar;
926 Diag(PPIDecl->getLocation(), diag::note_previous_use);
927 }
928
929 if (ObjCPropertyImplDecl *PPIDecl
930 = IC->FindPropertyImplDecl(PropertyId)) {
931 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
932 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000933 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000934 }
935 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +0000936 if (getLangOpts().ObjCDefaultSynthProperties &&
937 getLangOpts().ObjCNonFragileABI2 &&
Ted Kremenek71207fc2012-01-05 22:47:47 +0000938 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000939 // Diagnose if an ivar was lazily synthesdized due to a previous
940 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000941 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000942 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000943 ObjCIvarDecl *Ivar = 0;
944 if (!Synthesize)
945 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
946 else {
947 if (PropertyIvar && PropertyIvar != PropertyId)
948 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
949 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000950 // Issue diagnostics only if Ivar belongs to current class.
951 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000952 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000953 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
954 << PropertyId;
955 Ivar->setInvalidDecl();
956 }
957 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000958 } else {
959 if (Synthesize)
960 if (ObjCPropertyImplDecl *PPIDecl =
961 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000962 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000963 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
964 << PropertyIvar;
965 Diag(PPIDecl->getLocation(), diag::note_previous_use);
966 }
967
968 if (ObjCPropertyImplDecl *PPIDecl =
969 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000970 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000971 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000972 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000973 }
974 CatImplClass->addPropertyImplementation(PIDecl);
975 }
976
John McCalld226f652010-08-21 09:40:31 +0000977 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000978}
979
980//===----------------------------------------------------------------------===//
981// Helper methods.
982//===----------------------------------------------------------------------===//
983
Ted Kremenek9d64c152010-03-12 00:38:38 +0000984/// DiagnosePropertyMismatch - Compares two properties for their
985/// attributes and types and warns on a variety of inconsistencies.
986///
987void
988Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
989 ObjCPropertyDecl *SuperProperty,
990 const IdentifierInfo *inheritedName) {
991 ObjCPropertyDecl::PropertyAttributeKind CAttr =
992 Property->getPropertyAttributes();
993 ObjCPropertyDecl::PropertyAttributeKind SAttr =
994 SuperProperty->getPropertyAttributes();
995 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
996 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
997 Diag(Property->getLocation(), diag::warn_readonly_property)
998 << Property->getDeclName() << inheritedName;
999 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1000 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1001 Diag(Property->getLocation(), diag::warn_property_attribute)
1002 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001003 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001004 unsigned CAttrRetain =
1005 (CAttr &
1006 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1007 unsigned SAttrRetain =
1008 (SAttr &
1009 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1010 bool CStrong = (CAttrRetain != 0);
1011 bool SStrong = (SAttrRetain != 0);
1012 if (CStrong != SStrong)
1013 Diag(Property->getLocation(), diag::warn_property_attribute)
1014 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1015 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001016
1017 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1018 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1019 Diag(Property->getLocation(), diag::warn_property_attribute)
1020 << Property->getDeclName() << "atomic" << inheritedName;
1021 if (Property->getSetterName() != SuperProperty->getSetterName())
1022 Diag(Property->getLocation(), diag::warn_property_attribute)
1023 << Property->getDeclName() << "setter" << inheritedName;
1024 if (Property->getGetterName() != SuperProperty->getGetterName())
1025 Diag(Property->getLocation(), diag::warn_property_attribute)
1026 << Property->getDeclName() << "getter" << inheritedName;
1027
1028 QualType LHSType =
1029 Context.getCanonicalType(SuperProperty->getType());
1030 QualType RHSType =
1031 Context.getCanonicalType(Property->getType());
1032
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001033 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001034 // Do cases not handled in above.
1035 // FIXME. For future support of covariant property types, revisit this.
1036 bool IncompatibleObjC = false;
1037 QualType ConvertedType;
1038 if (!isObjCPointerConversion(RHSType, LHSType,
1039 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001040 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001041 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1042 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001043 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1044 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001045 }
1046}
1047
1048bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1049 ObjCMethodDecl *GetterMethod,
1050 SourceLocation Loc) {
1051 if (GetterMethod &&
John McCall3c3b7f92011-10-25 17:37:35 +00001052 !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(),
1053 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001054 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +00001055 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +00001056 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +00001057 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001058 if (result != Compatible) {
1059 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1060 << property->getDeclName()
1061 << GetterMethod->getSelector();
1062 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1063 return true;
1064 }
1065 }
1066 return false;
1067}
1068
1069/// ComparePropertiesInBaseAndSuper - This routine compares property
1070/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001071/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001072///
1073void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1074 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1075 if (!SDecl)
1076 return;
1077 // FIXME: O(N^2)
1078 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1079 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie262bc182012-04-30 02:36:29 +00001080 ObjCPropertyDecl *SuperPDecl = &*S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001081 // Does property in super class has declaration in current class?
1082 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1083 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie262bc182012-04-30 02:36:29 +00001084 ObjCPropertyDecl *PDecl = &*I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001085 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1086 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1087 SDecl->getIdentifier());
1088 }
1089 }
1090}
1091
1092/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1093/// of properties declared in a protocol and compares their attribute against
1094/// the same property declared in the class or category.
1095void
1096Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1097 ObjCProtocolDecl *PDecl) {
1098 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1099 if (!IDecl) {
1100 // Category
1101 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1102 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1103 if (!CatDecl->IsClassExtension())
1104 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1105 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001106 ObjCPropertyDecl *Pr = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001107 ObjCCategoryDecl::prop_iterator CP, CE;
1108 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001109 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001110 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001111 break;
1112 if (CP != CE)
1113 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie262bc182012-04-30 02:36:29 +00001114 DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001115 }
1116 return;
1117 }
1118 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1119 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001120 ObjCPropertyDecl *Pr = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001121 ObjCInterfaceDecl::prop_iterator CP, CE;
1122 // Is this property already in class's list of properties?
1123 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001124 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001125 break;
1126 if (CP != CE)
1127 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie262bc182012-04-30 02:36:29 +00001128 DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001129 }
1130}
1131
1132/// CompareProperties - This routine compares properties
1133/// declared in 'ClassOrProtocol' objects (which can be a class or an
1134/// inherited protocol with the list of properties for class/category 'CDecl'
1135///
John McCalld226f652010-08-21 09:40:31 +00001136void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1137 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001138 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1139
1140 if (!IDecl) {
1141 // Category
1142 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1143 assert (CatDecl && "CompareProperties");
1144 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1145 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1146 E = MDecl->protocol_end(); P != E; ++P)
1147 // Match properties of category with those of protocol (*P)
1148 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1149
1150 // Go thru the list of protocols for this category and recursively match
1151 // their properties with those in the category.
1152 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1153 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001154 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001155 } else {
1156 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1157 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1158 E = MD->protocol_end(); P != E; ++P)
1159 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1160 }
1161 return;
1162 }
1163
1164 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001165 for (ObjCInterfaceDecl::all_protocol_iterator
1166 P = MDecl->all_referenced_protocol_begin(),
1167 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001168 // Match properties of class IDecl with those of protocol (*P).
1169 MatchOneProtocolPropertiesInClass(IDecl, *P);
1170
1171 // Go thru the list of protocols for this class and recursively match
1172 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001173 for (ObjCInterfaceDecl::all_protocol_iterator
1174 P = IDecl->all_referenced_protocol_begin(),
1175 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001176 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001177 } else {
1178 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1179 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1180 E = MD->protocol_end(); P != E; ++P)
1181 MatchOneProtocolPropertiesInClass(IDecl, *P);
1182 }
1183}
1184
1185/// isPropertyReadonly - Return true if property is readonly, by searching
1186/// for the property in the class and in its categories and implementations
1187///
1188bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1189 ObjCInterfaceDecl *IDecl) {
1190 // by far the most common case.
1191 if (!PDecl->isReadOnly())
1192 return false;
1193 // Even if property is ready only, if interface has a user defined setter,
1194 // it is not considered read only.
1195 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1196 return false;
1197
1198 // Main class has the property as 'readonly'. Must search
1199 // through the category list to see if the property's
1200 // attribute has been over-ridden to 'readwrite'.
1201 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1202 Category; Category = Category->getNextClassCategory()) {
1203 // Even if property is ready only, if a category has a user defined setter,
1204 // it is not considered read only.
1205 if (Category->getInstanceMethod(PDecl->getSetterName()))
1206 return false;
1207 ObjCPropertyDecl *P =
1208 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1209 if (P && !P->isReadOnly())
1210 return false;
1211 }
1212
1213 // Also, check for definition of a setter method in the implementation if
1214 // all else failed.
1215 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1216 if (ObjCImplementationDecl *IMD =
1217 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1218 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1219 return false;
1220 } else if (ObjCCategoryImplDecl *CIMD =
1221 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1222 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1223 return false;
1224 }
1225 }
1226 // Lastly, look through the implementation (if one is in scope).
1227 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1228 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1229 return false;
1230 // If all fails, look at the super class.
1231 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1232 return isPropertyReadonly(PDecl, SIDecl);
1233 return true;
1234}
1235
1236/// CollectImmediateProperties - This routine collects all properties in
1237/// the class and its conforming protocols; but not those it its super class.
1238void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001239 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1240 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001241 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1242 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1243 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001244 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001245 PropMap[Prop->getIdentifier()] = Prop;
1246 }
1247 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001248 for (ObjCInterfaceDecl::all_protocol_iterator
1249 PI = IDecl->all_referenced_protocol_begin(),
1250 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001251 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001252 }
1253 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1254 if (!CATDecl->IsClassExtension())
1255 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1256 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001257 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001258 PropMap[Prop->getIdentifier()] = Prop;
1259 }
1260 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001261 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001262 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001263 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001264 }
1265 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1266 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1267 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001268 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001269 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1270 // Exclude property for protocols which conform to class's super-class,
1271 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001272 if (!PropertyFromSuper ||
1273 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001274 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1275 if (!PropEntry)
1276 PropEntry = Prop;
1277 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001278 }
1279 // scan through protocol's protocols.
1280 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1281 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001282 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001283 }
1284}
1285
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001286/// CollectClassPropertyImplementations - This routine collects list of
1287/// properties to be implemented in the class. This includes, class's
1288/// and its conforming protocols' properties.
1289static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1290 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1291 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1292 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1293 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001294 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001295 PropMap[Prop->getIdentifier()] = Prop;
1296 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001297 for (ObjCInterfaceDecl::all_protocol_iterator
1298 PI = IDecl->all_referenced_protocol_begin(),
1299 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001300 CollectClassPropertyImplementations((*PI), PropMap);
1301 }
1302 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1303 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1304 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001305 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahanianac371502012-02-23 18:21:25 +00001306 if (!PropMap.count(Prop->getIdentifier()))
1307 PropMap[Prop->getIdentifier()] = Prop;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001308 }
1309 // scan through protocol's protocols.
1310 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1311 E = PDecl->protocol_end(); PI != E; ++PI)
1312 CollectClassPropertyImplementations((*PI), PropMap);
1313 }
1314}
1315
1316/// CollectSuperClassPropertyImplementations - This routine collects list of
1317/// properties to be implemented in super class(s) and also coming from their
1318/// conforming protocols.
1319static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1320 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1321 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1322 while (SDecl) {
1323 CollectClassPropertyImplementations(SDecl, PropMap);
1324 SDecl = SDecl->getSuperClass();
1325 }
1326 }
1327}
1328
Ted Kremenek9d64c152010-03-12 00:38:38 +00001329/// LookupPropertyDecl - Looks up a property in the current class and all
1330/// its protocols.
1331ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1332 IdentifierInfo *II) {
1333 if (const ObjCInterfaceDecl *IDecl =
1334 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1335 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1336 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001337 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001338 if (Prop->getIdentifier() == II)
1339 return Prop;
1340 }
1341 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001342 for (ObjCInterfaceDecl::all_protocol_iterator
1343 PI = IDecl->all_referenced_protocol_begin(),
1344 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001345 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1346 if (Prop)
1347 return Prop;
1348 }
1349 }
1350 else if (const ObjCProtocolDecl *PDecl =
1351 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1352 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1353 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001354 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001355 if (Prop->getIdentifier() == II)
1356 return Prop;
1357 }
1358 // scan through protocol's protocols.
1359 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1360 E = PDecl->protocol_end(); PI != E; ++PI) {
1361 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1362 if (Prop)
1363 return Prop;
1364 }
1365 }
1366 return 0;
1367}
1368
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001369static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1370 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001371 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001372 {
1373 llvm::raw_svector_ostream os(ivarName);
1374 os << '_' << Prop->getIdentifier()->getName();
1375 }
1376 return &Ctx.Idents.get(ivarName.str());
1377}
1378
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001379/// DefaultSynthesizeProperties - This routine default synthesizes all
1380/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001381void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1382 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001383
1384 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1385 CollectClassPropertyImplementations(IDecl, PropMap);
1386 if (PropMap.empty())
1387 return;
1388 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1389 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1390
1391 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1392 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1393 ObjCPropertyDecl *Prop = P->second;
1394 // If property to be implemented in the super class, ignore.
1395 if (SuperPropMap[Prop->getIdentifier()])
1396 continue;
1397 // Is there a matching propery synthesize/dynamic?
1398 if (Prop->isInvalidDecl() ||
1399 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1400 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1401 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001402 // Property may have been synthesized by user.
1403 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1404 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001405 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1406 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1407 continue;
1408 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1409 continue;
1410 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001411 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1412 // We won't auto-synthesize properties declared in protocols.
1413 Diag(IMPDecl->getLocation(),
1414 diag::warn_auto_synthesizing_protocol_property);
1415 Diag(Prop->getLocation(), diag::note_property_declare);
1416 continue;
1417 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001418
1419 // We use invalid SourceLocations for the synthesized ivars since they
1420 // aren't really synthesized at a particular location; they just exist.
1421 // Saying that they are located at the @implementation isn't really going
1422 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001423 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1424 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1425 true,
1426 /* property = */ Prop->getIdentifier(),
1427 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
1428 SourceLocation()));
1429 if (PIDecl) {
1430 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001431 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001432 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001433 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001434}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001435
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001436void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1437 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1438 return;
1439 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1440 if (!IC)
1441 return;
1442 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001443 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001444 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001445}
1446
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001447void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001448 ObjCContainerDecl *CDecl,
1449 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001450 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1451 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1452 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1453
Ted Kremenek9d64c152010-03-12 00:38:38 +00001454 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001455 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001456 if (PropMap.empty())
1457 return;
1458
1459 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1460 for (ObjCImplDecl::propimpl_iterator
1461 I = IMPDecl->propimpl_begin(),
1462 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001463 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001464
1465 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1466 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1467 ObjCPropertyDecl *Prop = P->second;
1468 // Is there a matching propery synthesize/dynamic?
1469 if (Prop->isInvalidDecl() ||
1470 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001471 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001472 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001473 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001474 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001475 isa<ObjCCategoryDecl>(CDecl) ?
1476 diag::warn_setter_getter_impl_required_in_category :
1477 diag::warn_setter_getter_impl_required)
1478 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001479 Diag(Prop->getLocation(),
1480 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001481 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1482 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001483 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001484 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1485
Ted Kremenek9d64c152010-03-12 00:38:38 +00001486 }
1487
1488 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001489 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001490 isa<ObjCCategoryDecl>(CDecl) ?
1491 diag::warn_setter_getter_impl_required_in_category :
1492 diag::warn_setter_getter_impl_required)
1493 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001494 Diag(Prop->getLocation(),
1495 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001496 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1497 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001498 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001499 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001500 }
1501 }
1502}
1503
1504void
1505Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1506 ObjCContainerDecl* IDecl) {
1507 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001508 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001509 return;
1510 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1511 E = IDecl->prop_end();
1512 I != E; ++I) {
David Blaikie262bc182012-04-30 02:36:29 +00001513 ObjCPropertyDecl *Property = &*I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001514 ObjCMethodDecl *GetterMethod = 0;
1515 ObjCMethodDecl *SetterMethod = 0;
1516 bool LookedUpGetterSetter = false;
1517
Ted Kremenek9d64c152010-03-12 00:38:38 +00001518 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001519 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001520
John McCall265941b2011-09-13 18:31:23 +00001521 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1522 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001523 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1524 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1525 LookedUpGetterSetter = true;
1526 if (GetterMethod) {
1527 Diag(GetterMethod->getLocation(),
1528 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001529 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001530 Diag(Property->getLocation(), diag::note_property_declare);
1531 }
1532 if (SetterMethod) {
1533 Diag(SetterMethod->getLocation(),
1534 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001535 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001536 Diag(Property->getLocation(), diag::note_property_declare);
1537 }
1538 }
1539
Ted Kremenek9d64c152010-03-12 00:38:38 +00001540 // We only care about readwrite atomic property.
1541 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1542 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1543 continue;
1544 if (const ObjCPropertyImplDecl *PIDecl
1545 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1546 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1547 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001548 if (!LookedUpGetterSetter) {
1549 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1550 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1551 LookedUpGetterSetter = true;
1552 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001553 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1554 SourceLocation MethodLoc =
1555 (GetterMethod ? GetterMethod->getLocation()
1556 : SetterMethod->getLocation());
1557 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001558 << Property->getIdentifier() << (GetterMethod != 0)
1559 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001560 // fixit stuff.
1561 if (!AttributesAsWritten) {
1562 if (Property->getLParenLoc().isValid()) {
1563 // @property () ... case.
1564 SourceRange PropSourceRange(Property->getAtLoc(),
1565 Property->getLParenLoc());
1566 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1567 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1568 }
1569 else {
1570 //@property id etc.
1571 SourceLocation endLoc =
1572 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1573 endLoc = endLoc.getLocWithOffset(-1);
1574 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1575 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1576 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1577 }
1578 }
1579 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1580 // @property () ... case.
1581 SourceLocation endLoc = Property->getLParenLoc();
1582 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1583 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1584 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1585 }
1586 else
1587 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001588 Diag(Property->getLocation(), diag::note_property_declare);
1589 }
1590 }
1591 }
1592}
1593
John McCallf85e1932011-06-15 23:02:42 +00001594void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001595 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001596 return;
1597
1598 for (ObjCImplementationDecl::propimpl_iterator
1599 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie262bc182012-04-30 02:36:29 +00001600 ObjCPropertyImplDecl *PID = &*i;
John McCallf85e1932011-06-15 23:02:42 +00001601 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1602 continue;
1603
1604 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001605 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1606 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001607 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1608 if (!method)
1609 continue;
1610 ObjCMethodFamily family = method->getMethodFamily();
1611 if (family == OMF_alloc || family == OMF_copy ||
1612 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001613 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001614 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1615 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001616 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001617 Diag(PD->getLocation(), diag::note_property_declare);
1618 }
1619 }
1620 }
1621}
1622
John McCall5de74d12010-11-10 07:01:40 +00001623/// AddPropertyAttrs - Propagates attributes from a property to the
1624/// implicitly-declared getter or setter for that property.
1625static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1626 ObjCPropertyDecl *Property) {
1627 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001628 for (Decl::attr_iterator A = Property->attr_begin(),
1629 AEnd = Property->attr_end();
1630 A != AEnd; ++A) {
1631 if (isa<DeprecatedAttr>(*A) ||
1632 isa<UnavailableAttr>(*A) ||
1633 isa<AvailabilityAttr>(*A))
1634 PropertyMethod->addAttr((*A)->clone(S.Context));
1635 }
John McCall5de74d12010-11-10 07:01:40 +00001636}
1637
Ted Kremenek9d64c152010-03-12 00:38:38 +00001638/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1639/// have the property type and issue diagnostics if they don't.
1640/// Also synthesize a getter/setter method if none exist (and update the
1641/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1642/// methods is the "right" thing to do.
1643void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001644 ObjCContainerDecl *CD,
1645 ObjCPropertyDecl *redeclaredProperty,
1646 ObjCContainerDecl *lexicalDC) {
1647
Ted Kremenek9d64c152010-03-12 00:38:38 +00001648 ObjCMethodDecl *GetterMethod, *SetterMethod;
1649
1650 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1651 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1652 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1653 property->getLocation());
1654
1655 if (SetterMethod) {
1656 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1657 property->getPropertyAttributes();
1658 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1659 Context.getCanonicalType(SetterMethod->getResultType()) !=
1660 Context.VoidTy)
1661 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1662 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001663 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001664 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1665 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001666 Diag(property->getLocation(),
1667 diag::warn_accessor_property_type_mismatch)
1668 << property->getDeclName()
1669 << SetterMethod->getSelector();
1670 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1671 }
1672 }
1673
1674 // Synthesize getter/setter methods if none exist.
1675 // Find the default getter and if one not found, add one.
1676 // FIXME: The synthesized property we set here is misleading. We almost always
1677 // synthesize these methods unless the user explicitly provided prototypes
1678 // (which is odd, but allowed). Sema should be typechecking that the
1679 // declarations jive in that situation (which it is not currently).
1680 if (!GetterMethod) {
1681 // No instance method of same name as property getter name was found.
1682 // Declare a getter method and add it to the list of methods
1683 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001684 SourceLocation Loc = redeclaredProperty ?
1685 redeclaredProperty->getLocation() :
1686 property->getLocation();
1687
1688 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1689 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001690 property->getType(), 0, CD, /*isInstance=*/true,
1691 /*isVariadic=*/false, /*isSynthesized=*/true,
1692 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001693 (property->getPropertyImplementation() ==
1694 ObjCPropertyDecl::Optional) ?
1695 ObjCMethodDecl::Optional :
1696 ObjCMethodDecl::Required);
1697 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001698
1699 AddPropertyAttrs(*this, GetterMethod, property);
1700
Ted Kremenek23173d72010-05-18 21:09:07 +00001701 // FIXME: Eventually this shouldn't be needed, as the lexical context
1702 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001703 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001704 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001705 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1706 GetterMethod->addAttr(
1707 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001708 } else
1709 // A user declared getter will be synthesize when @synthesize of
1710 // the property with the same name is seen in the @implementation
1711 GetterMethod->setSynthesized(true);
1712 property->setGetterMethodDecl(GetterMethod);
1713
1714 // Skip setter if property is read-only.
1715 if (!property->isReadOnly()) {
1716 // Find the default setter and if one not found, add one.
1717 if (!SetterMethod) {
1718 // No instance method of same name as property setter name was found.
1719 // Declare a setter method and add it to the list of methods
1720 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001721 SourceLocation Loc = redeclaredProperty ?
1722 redeclaredProperty->getLocation() :
1723 property->getLocation();
1724
1725 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001726 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001727 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001728 CD, /*isInstance=*/true, /*isVariadic=*/false,
1729 /*isSynthesized=*/true,
1730 /*isImplicitlyDeclared=*/true,
1731 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001732 (property->getPropertyImplementation() ==
1733 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001734 ObjCMethodDecl::Optional :
1735 ObjCMethodDecl::Required);
1736
Ted Kremenek9d64c152010-03-12 00:38:38 +00001737 // Invent the arguments for the setter. We don't bother making a
1738 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001739 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1740 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001741 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001742 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001743 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001744 SC_None,
1745 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001746 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001747 SetterMethod->setMethodParams(Context, Argument,
1748 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001749
1750 AddPropertyAttrs(*this, SetterMethod, property);
1751
Ted Kremenek9d64c152010-03-12 00:38:38 +00001752 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001753 // FIXME: Eventually this shouldn't be needed, as the lexical context
1754 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001755 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001756 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001757 } else
1758 // A user declared setter will be synthesize when @synthesize of
1759 // the property with the same name is seen in the @implementation
1760 SetterMethod->setSynthesized(true);
1761 property->setSetterMethodDecl(SetterMethod);
1762 }
1763 // Add any synthesized methods to the global pool. This allows us to
1764 // handle the following, which is supported by GCC (and part of the design).
1765 //
1766 // @interface Foo
1767 // @property double bar;
1768 // @end
1769 //
1770 // void thisIsUnfortunate() {
1771 // id foo;
1772 // double bar = [foo bar];
1773 // }
1774 //
1775 if (GetterMethod)
1776 AddInstanceMethodToGlobalPool(GetterMethod);
1777 if (SetterMethod)
1778 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001779
1780 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1781 if (!CurrentClass) {
1782 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1783 CurrentClass = Cat->getClassInterface();
1784 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1785 CurrentClass = Impl->getClassInterface();
1786 }
1787 if (GetterMethod)
1788 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1789 if (SetterMethod)
1790 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001791}
1792
John McCalld226f652010-08-21 09:40:31 +00001793void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001794 SourceLocation Loc,
1795 unsigned &Attributes) {
1796 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001797 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001798 return;
1799
1800 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001801 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001802
David Blaikie4e4d0842012-03-11 07:00:24 +00001803 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001804 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1805 PropertyTy->isObjCRetainableType()) {
1806 // 'readonly' property with no obvious lifetime.
1807 // its life time will be determined by its backing ivar.
1808 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1809 ObjCDeclSpec::DQ_PR_copy |
1810 ObjCDeclSpec::DQ_PR_retain |
1811 ObjCDeclSpec::DQ_PR_strong |
1812 ObjCDeclSpec::DQ_PR_weak |
1813 ObjCDeclSpec::DQ_PR_assign);
1814 if ((Attributes & rel) == 0)
1815 return;
1816 }
1817
Ted Kremenek9d64c152010-03-12 00:38:38 +00001818 // readonly and readwrite/assign/retain/copy conflict.
1819 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1820 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1821 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001822 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001823 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001824 ObjCDeclSpec::DQ_PR_retain |
1825 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001826 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1827 "readwrite" :
1828 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1829 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001830 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1831 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001832 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1833 "copy" : "retain";
1834
1835 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1836 diag::err_objc_property_attr_mutually_exclusive :
1837 diag::warn_objc_property_attr_mutually_exclusive)
1838 << "readonly" << which;
1839 }
1840
1841 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001842 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1843 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1844 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001845 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001846 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001847 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1848 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1849 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1850 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00001851 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001852 }
1853
1854 // Check for more than one of { assign, copy, retain }.
1855 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1856 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1857 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1858 << "assign" << "copy";
1859 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1860 }
1861 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1862 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1863 << "assign" << "retain";
1864 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1865 }
John McCallf85e1932011-06-15 23:02:42 +00001866 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1867 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1868 << "assign" << "strong";
1869 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1870 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001871 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001872 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1873 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1874 << "assign" << "weak";
1875 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1876 }
1877 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1878 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1879 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1880 << "unsafe_unretained" << "copy";
1881 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1882 }
1883 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1884 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1885 << "unsafe_unretained" << "retain";
1886 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1887 }
1888 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1889 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1890 << "unsafe_unretained" << "strong";
1891 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1892 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001893 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001894 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1895 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1896 << "unsafe_unretained" << "weak";
1897 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1898 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001899 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1900 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1901 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1902 << "copy" << "retain";
1903 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1904 }
John McCallf85e1932011-06-15 23:02:42 +00001905 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1906 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1907 << "copy" << "strong";
1908 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1909 }
1910 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1911 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1912 << "copy" << "weak";
1913 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1914 }
1915 }
1916 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1917 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1918 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1919 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001920 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001921 }
1922 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1923 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1924 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1925 << "strong" << "weak";
1926 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001927 }
1928
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00001929 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
1930 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
1931 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1932 << "atomic" << "nonatomic";
1933 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
1934 }
1935
Ted Kremenek9d64c152010-03-12 00:38:38 +00001936 // Warn if user supplied no assignment attribute, property is
1937 // readwrite, and this is an object type.
1938 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001939 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1940 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1941 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001942 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001943 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001944 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001945 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001946 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00001947 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001948 bool isAnyClassTy =
1949 (PropertyTy->isObjCClassType() ||
1950 PropertyTy->isObjCQualifiedClassType());
1951 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
1952 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00001953 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001954 ;
1955 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001956 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00001957 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001958 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001959
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001960 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00001961 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001962 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00001963 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001964 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001965
1966 // FIXME: Implement warning dependent on NSCopying being
1967 // implemented. See also:
1968 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1969 // (please trim this list while you are at it).
1970 }
1971
1972 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001973 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00001974 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001975 && PropertyTy->isBlockPointerType())
1976 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
David Blaikie4e4d0842012-03-11 07:00:24 +00001977 else if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001978 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1979 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1980 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1981 PropertyTy->isBlockPointerType())
1982 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00001983
1984 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1985 (Attributes & ObjCDeclSpec::DQ_PR_setter))
1986 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
1987
Ted Kremenek9d64c152010-03-12 00:38:38 +00001988}