blob: 44e50852a55c9467bf2d060710130a4f0d221a48 [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
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000203static std::string getPropertyAttributeString(const ObjCPropertyDecl *property,
204 unsigned Attributes) {
205 std::string attr;
206 if (!Attributes)
207 return attr;
208 attr = "(";
209 bool first = true;
210 if (Attributes & ObjCPropertyDecl::OBJC_PR_readonly)
211 {attr += !first ? ", readonly" : "readonly"; first = false; }
212 if (Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)
213 {attr += !first ? ", readwrite" : "readwrite"; first = false; }
214 if (Attributes & ObjCPropertyDecl::OBJC_PR_getter)
215 {
216 if (!first)
217 attr += ", ";
218 attr += "getter=";
219 attr += property->getGetterName().getAsString();
220 first = false;
221 }
222 if (Attributes & ObjCPropertyDecl::OBJC_PR_setter)
223 {
224 if (!first)
225 attr += ", ";
226 attr += "setter=";
227 attr += property->getSetterName().getAsString();
228 first = false;
229 }
230 if (Attributes & ObjCPropertyDecl::OBJC_PR_assign)
231 {attr += !first ? ", assign" : "assign"; first = false; }
232 if (Attributes & ObjCPropertyDecl::OBJC_PR_retain)
233 {attr += !first ? ", retain" : "retain"; first = false; }
234 if (Attributes & ObjCPropertyDecl::OBJC_PR_strong)
235 {attr += !first ? ", strong" : "strong"; first = false; }
236 if (Attributes & ObjCPropertyDecl::OBJC_PR_weak)
237 {attr += !first ? ", weak" : "weak"; first = false; }
238 if (Attributes & ObjCPropertyDecl::OBJC_PR_copy)
239 {attr += !first ? ", copy" : "copy"; first = false; }
240 if (Attributes & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
241 {attr += !first ? ", unsafe_unretained" : "unsafe_unretained"; first = false; }
242 if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic)
243 {attr += !first ? ", nonatomic" : "nonatomic"; first = false; }
244 if (Attributes & ObjCPropertyDecl::OBJC_PR_atomic)
245 {attr += !first ? ", atomic" : "atomic"; first = false; }
246 attr += ")";
247 return attr;
248}
249
John McCalld226f652010-08-21 09:40:31 +0000250Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000251Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000252 SourceLocation AtLoc,
253 SourceLocation LParenLoc,
254 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000255 Selector GetterSel, Selector SetterSel,
256 const bool isAssign,
257 const bool isReadWrite,
258 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000259 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000260 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000261 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000262 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000263 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000264 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000265 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000266 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000267 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
268
269 if (CCPrimary)
270 // Check for duplicate declaration of this property in current and
271 // other class extensions.
272 for (const ObjCCategoryDecl *ClsExtDecl =
273 CCPrimary->getFirstClassExtension();
274 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
275 if (ObjCPropertyDecl *prevDecl =
276 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
277 Diag(AtLoc, diag::err_duplicate_property);
278 Diag(prevDecl->getLocation(), diag::note_property_declare);
279 return 0;
280 }
281 }
282
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000283 // Create a new ObjCPropertyDecl with the DeclContext being
284 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000285 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000286 ObjCPropertyDecl *PDecl =
287 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000288 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000289 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000290 makePropertyAttributesAsWritten(AttributesAsWritten));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000291 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
292 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
293 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
294 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000295 // Set setter/getter selector name. Needed later.
296 PDecl->setGetterName(GetterSel);
297 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000298 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000299 DC->addDecl(PDecl);
300
301 // We need to look in the @interface to see if the @property was
302 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000303 if (!CCPrimary) {
304 Diag(CDecl->getLocation(), diag::err_continuation_class);
305 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000306 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000307 }
308
309 // Find the property in continuation class's primary class only.
310 ObjCPropertyDecl *PIDecl =
311 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
312
313 if (!PIDecl) {
314 // No matching property found in the primary class. Just fall thru
315 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000316 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000317 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000318 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000319 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000320
321 // A case of continuation class adding a new property in the class. This
322 // is not what it was meant for. However, gcc supports it and so should we.
323 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000324 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000325 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000326 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
327 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000328 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000329 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
330 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000331 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000332 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
333 bool IncompatibleObjC = false;
334 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000335 // Relax the strict type matching for property type in continuation class.
336 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000337 // as it narrows the object type in its primary class property. Note that
338 // this conversion is safe only because the wider type is for a 'readonly'
339 // property in primary class and 'narrowed' type for a 'readwrite' property
340 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000341 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
342 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
343 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
344 ConvertedType, IncompatibleObjC))
345 || IncompatibleObjC) {
346 Diag(AtLoc,
347 diag::err_type_mismatch_continuation_class) << PDecl->getType();
348 Diag(PIDecl->getLocation(), diag::note_property_declare);
349 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000350 }
351
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000352 // The property 'PIDecl's readonly attribute will be over-ridden
353 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000354 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000355 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
356 unsigned retainCopyNonatomic =
357 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000358 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000359 ObjCPropertyDecl::OBJC_PR_copy |
360 ObjCPropertyDecl::OBJC_PR_nonatomic);
361 if ((Attributes & retainCopyNonatomic) !=
362 (PIkind & retainCopyNonatomic)) {
363 Diag(AtLoc, diag::warn_property_attr_mismatch);
364 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000365 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000366 DeclContext *DC = cast<DeclContext>(CCPrimary);
367 if (!ObjCPropertyDecl::findPropertyDecl(DC,
368 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000369 // Protocol is not in the primary class. Must build one for it.
370 ObjCDeclSpec ProtocolPropertyODS;
371 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
372 // and ObjCPropertyDecl::PropertyAttributeKind have identical
373 // values. Should consolidate both into one enum type.
374 ProtocolPropertyODS.
375 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
376 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000377 // Must re-establish the context from class extension to primary
378 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000379 ContextRAII SavedContext(*this, CCPrimary);
380
John McCalld226f652010-08-21 09:40:31 +0000381 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000382 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000383 PIDecl->getGetterName(),
384 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000385 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000386 MethodImplKind,
387 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000388 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000389 }
390 PIDecl->makeitReadWriteAttribute();
391 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
392 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000393 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
394 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000395 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
396 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
397 PIDecl->setSetterName(SetterSel);
398 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000399 // Tailor the diagnostics for the common case where a readwrite
400 // property is declared both in the @interface and the continuation.
401 // This is a common error where the user often intended the original
402 // declaration to be readonly.
403 unsigned diag =
404 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
405 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
406 ? diag::err_use_continuation_class_redeclaration_readwrite
407 : diag::err_use_continuation_class;
408 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000409 << CCPrimary->getDeclName();
410 Diag(PIDecl->getLocation(), diag::note_property_declare);
411 }
412 *isOverridingProperty = true;
413 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000414 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000415 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
416 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000417 if (ASTMutationListener *L = Context.getASTMutationListener())
418 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000419 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000420}
421
422ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
423 ObjCContainerDecl *CDecl,
424 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000425 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000426 FieldDeclarator &FD,
427 Selector GetterSel,
428 Selector SetterSel,
429 const bool isAssign,
430 const bool isReadWrite,
431 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000432 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000433 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000434 tok::ObjCKeywordKind MethodImplKind,
435 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000436 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000437 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000438
439 // Issue a warning if property is 'assign' as default and its object, which is
440 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000441 if (getLangOpts().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000442 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000443 if (const ObjCObjectPointerType *ObjPtrTy =
444 T->getAs<ObjCObjectPointerType>()) {
445 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
446 if (IDecl)
447 if (ObjCProtocolDecl* PNSCopying =
448 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
449 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
450 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000451 }
John McCallc12c5bb2010-05-15 11:32:37 +0000452 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000453 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
454
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000455 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000456 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
457 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000458 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000459
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000460 if (ObjCPropertyDecl *prevDecl =
461 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000462 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000463 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000464 PDecl->setInvalidDecl();
465 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000466 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000467 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000468 if (lexicalDC)
469 PDecl->setLexicalDeclContext(lexicalDC);
470 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000471
472 if (T->isArrayType() || T->isFunctionType()) {
473 Diag(AtLoc, diag::err_property_type) << T;
474 PDecl->setInvalidDecl();
475 }
476
477 ProcessDeclAttributes(S, PDecl, FD.D);
478
479 // Regardless of setter/getter attribute, we save the default getter/setter
480 // selector names in anticipation of declaration of setter/getter methods.
481 PDecl->setGetterName(GetterSel);
482 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000483 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000484 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000485
Ted Kremenek28685ab2010-03-12 00:46:40 +0000486 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
487 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
488
489 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
490 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
491
492 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
493 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
494
495 if (isReadWrite)
496 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
497
498 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
499 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
500
John McCallf85e1932011-06-15 23:02:42 +0000501 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
502 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
503
504 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
505 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
506
Ted Kremenek28685ab2010-03-12 00:46:40 +0000507 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
508 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
509
John McCallf85e1932011-06-15 23:02:42 +0000510 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
511 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
512
Ted Kremenek28685ab2010-03-12 00:46:40 +0000513 if (isAssign)
514 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
515
John McCall265941b2011-09-13 18:31:23 +0000516 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000517 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
518 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000519 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000520 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000521
John McCallf85e1932011-06-15 23:02:42 +0000522 // 'unsafe_unretained' is alias for 'assign'.
523 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
524 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
525 if (isAssign)
526 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
527
Ted Kremenek28685ab2010-03-12 00:46:40 +0000528 if (MethodImplKind == tok::objc_required)
529 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
530 else if (MethodImplKind == tok::objc_optional)
531 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000532
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000533 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000534}
535
John McCallf85e1932011-06-15 23:02:42 +0000536static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
537 ObjCPropertyDecl *property,
538 ObjCIvarDecl *ivar) {
539 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
540
John McCallf85e1932011-06-15 23:02:42 +0000541 QualType ivarType = ivar->getType();
542 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000543
John McCall265941b2011-09-13 18:31:23 +0000544 // The lifetime implied by the property's attributes.
545 Qualifiers::ObjCLifetime propertyLifetime =
546 getImpliedARCOwnership(property->getPropertyAttributes(),
547 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000548
John McCall265941b2011-09-13 18:31:23 +0000549 // We're fine if they match.
550 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000551
John McCall265941b2011-09-13 18:31:23 +0000552 // These aren't valid lifetimes for object ivars; don't diagnose twice.
553 if (ivarLifetime == Qualifiers::OCL_None ||
554 ivarLifetime == Qualifiers::OCL_Autoreleasing)
555 return;
John McCallf85e1932011-06-15 23:02:42 +0000556
John McCall265941b2011-09-13 18:31:23 +0000557 switch (propertyLifetime) {
558 case Qualifiers::OCL_Strong:
559 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
560 << property->getDeclName()
561 << ivar->getDeclName()
562 << ivarLifetime;
563 break;
John McCallf85e1932011-06-15 23:02:42 +0000564
John McCall265941b2011-09-13 18:31:23 +0000565 case Qualifiers::OCL_Weak:
566 S.Diag(propertyImplLoc, diag::error_weak_property)
567 << property->getDeclName()
568 << ivar->getDeclName();
569 break;
John McCallf85e1932011-06-15 23:02:42 +0000570
John McCall265941b2011-09-13 18:31:23 +0000571 case Qualifiers::OCL_ExplicitNone:
572 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
573 << property->getDeclName()
574 << ivar->getDeclName()
575 << ((property->getPropertyAttributesAsWritten()
576 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
577 break;
John McCallf85e1932011-06-15 23:02:42 +0000578
John McCall265941b2011-09-13 18:31:23 +0000579 case Qualifiers::OCL_Autoreleasing:
580 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000581
John McCall265941b2011-09-13 18:31:23 +0000582 case Qualifiers::OCL_None:
583 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000584 return;
585 }
586
587 S.Diag(property->getLocation(), diag::note_property_declare);
588}
589
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000590/// setImpliedPropertyAttributeForReadOnlyProperty -
591/// This routine evaludates life-time attributes for a 'readonly'
592/// property with no known lifetime of its own, using backing
593/// 'ivar's attribute, if any. If no backing 'ivar', property's
594/// life-time is assumed 'strong'.
595static void setImpliedPropertyAttributeForReadOnlyProperty(
596 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
597 Qualifiers::ObjCLifetime propertyLifetime =
598 getImpliedARCOwnership(property->getPropertyAttributes(),
599 property->getType());
600 if (propertyLifetime != Qualifiers::OCL_None)
601 return;
602
603 if (!ivar) {
604 // if no backing ivar, make property 'strong'.
605 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
606 return;
607 }
608 // property assumes owenership of backing ivar.
609 QualType ivarType = ivar->getType();
610 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
611 if (ivarLifetime == Qualifiers::OCL_Strong)
612 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
613 else if (ivarLifetime == Qualifiers::OCL_Weak)
614 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
615 return;
616}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000617
618/// ActOnPropertyImplDecl - This routine performs semantic checks and
619/// builds the AST node for a property implementation declaration; declared
620/// as @synthesize or @dynamic.
621///
John McCalld226f652010-08-21 09:40:31 +0000622Decl *Sema::ActOnPropertyImplDecl(Scope *S,
623 SourceLocation AtLoc,
624 SourceLocation PropertyLoc,
625 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000626 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000627 IdentifierInfo *PropertyIvar,
628 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000629 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000630 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000631 // Make sure we have a context for the property implementation declaration.
632 if (!ClassImpDecl) {
633 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000634 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000635 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000636 if (PropertyIvarLoc.isInvalid())
637 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000638 SourceLocation PropertyDiagLoc = PropertyLoc;
639 if (PropertyDiagLoc.isInvalid())
640 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000641 ObjCPropertyDecl *property = 0;
642 ObjCInterfaceDecl* IDecl = 0;
643 // Find the class or category class where this property must have
644 // a declaration.
645 ObjCImplementationDecl *IC = 0;
646 ObjCCategoryImplDecl* CatImplClass = 0;
647 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
648 IDecl = IC->getClassInterface();
649 // We always synthesize an interface for an implementation
650 // without an interface decl. So, IDecl is always non-zero.
651 assert(IDecl &&
652 "ActOnPropertyImplDecl - @implementation without @interface");
653
654 // Look for this property declaration in the @implementation's @interface
655 property = IDecl->FindPropertyDeclaration(PropertyId);
656 if (!property) {
657 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000658 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000659 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000660 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000661 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
662 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000663 if (AtLoc.isValid())
664 Diag(AtLoc, diag::warn_implicit_atomic_property);
665 else
666 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
667 Diag(property->getLocation(), diag::note_property_declare);
668 }
669
Ted Kremenek28685ab2010-03-12 00:46:40 +0000670 if (const ObjCCategoryDecl *CD =
671 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
672 if (!CD->IsClassExtension()) {
673 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
674 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000675 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000676 }
677 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000678
679 if (Synthesize&&
680 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
681 property->hasAttr<IBOutletAttr>() &&
682 !AtLoc.isValid()) {
683 unsigned rwPIKind = (PIkind | ObjCPropertyDecl::OBJC_PR_readwrite);
684 rwPIKind &= (~ObjCPropertyDecl::OBJC_PR_readonly);
685 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
686 Diag(property->getLocation(), diag::note_property_declare);
687 // FIXME. End location must be that of closing ')' which is currently
688 // unavailable. Need to add it.
689 SourceLocation endLoc =
690 property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
691 SourceRange PropSourceRange(property->getLParenLoc(), endLoc);
692 Diag(property->getLocation(),
693 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
694 FixItHint::CreateReplacement(PropSourceRange, getPropertyAttributeString(property,
695 rwPIKind));
696 }
697
Ted Kremenek28685ab2010-03-12 00:46:40 +0000698 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
699 if (Synthesize) {
700 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000701 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000702 }
703 IDecl = CatImplClass->getClassInterface();
704 if (!IDecl) {
705 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000706 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000707 }
708 ObjCCategoryDecl *Category =
709 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
710
711 // If category for this implementation not found, it is an error which
712 // has already been reported eralier.
713 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000714 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000715 // Look for this property declaration in @implementation's category
716 property = Category->FindPropertyDeclaration(PropertyId);
717 if (!property) {
718 Diag(PropertyLoc, diag::error_bad_category_property_decl)
719 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000720 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000721 }
722 } else {
723 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000724 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000725 }
726 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000727 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000728 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000729 // Check that we have a valid, previously declared ivar for @synthesize
730 if (Synthesize) {
731 // @synthesize
732 if (!PropertyIvar)
733 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000734 // Check that this is a previously declared 'ivar' in 'IDecl' interface
735 ObjCInterfaceDecl *ClassDeclared;
736 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
737 QualType PropType = property->getType();
738 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000739
740 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000741 diag::err_incomplete_synthesized_property,
742 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000743 Diag(property->getLocation(), diag::note_property_declare);
744 CompleteTypeErr = true;
745 }
746
David Blaikie4e4d0842012-03-11 07:00:24 +0000747 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000748 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000749 ObjCPropertyDecl::OBJC_PR_readonly) &&
750 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000751 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
752 }
753
John McCallf85e1932011-06-15 23:02:42 +0000754 ObjCPropertyDecl::PropertyAttributeKind kind
755 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000756
757 // Add GC __weak to the ivar type if the property is weak.
758 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000759 getLangOpts().getGC() != LangOptions::NonGC) {
760 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000761 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000762 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000763 Diag(property->getLocation(), diag::note_property_declare);
764 } else {
765 PropertyIvarType =
766 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000767 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000768 }
John McCall265941b2011-09-13 18:31:23 +0000769
Ted Kremenek28685ab2010-03-12 00:46:40 +0000770 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000771 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000772 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000773 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000774 !PropertyIvarType.getObjCLifetime() &&
775 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000776
John McCall265941b2011-09-13 18:31:23 +0000777 // It's an error if we have to do this and the user didn't
778 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000779 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000780 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000781 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000782 diag::err_arc_objc_property_default_assign_on_object);
783 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000784 } else {
785 Qualifiers::ObjCLifetime lifetime =
786 getImpliedARCOwnership(kind, PropertyIvarType);
787 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000788 if (lifetime == Qualifiers::OCL_Weak) {
789 bool err = false;
790 if (const ObjCObjectPointerType *ObjT =
791 PropertyIvarType->getAs<ObjCObjectPointerType>())
792 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000793 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000794 Diag(property->getLocation(), diag::note_property_declare);
795 err = true;
796 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000797 if (!err && !getLangOpts().ObjCRuntimeHasWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000798 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000799 Diag(property->getLocation(), diag::note_property_declare);
800 }
John McCallf85e1932011-06-15 23:02:42 +0000801 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000802
John McCallf85e1932011-06-15 23:02:42 +0000803 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000804 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000805 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
806 }
John McCallf85e1932011-06-15 23:02:42 +0000807 }
808
809 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000810 !getLangOpts().ObjCAutoRefCount &&
811 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000812 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000813 Diag(property->getLocation(), diag::note_property_declare);
814 }
815
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000816 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000817 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000818 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000819 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000820 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000821 if (CompleteTypeErr)
822 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000823 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000824 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000825 property->setPropertyIvarDecl(Ivar);
826
David Blaikie4e4d0842012-03-11 07:00:24 +0000827 if (!getLangOpts().ObjCNonFragileABI)
Eli Friedmane4c043d2012-05-01 22:26:06 +0000828 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
829 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000830 // Note! I deliberately want it to fall thru so, we have a
831 // a property implementation and to avoid future warnings.
David Blaikie4e4d0842012-03-11 07:00:24 +0000832 } else if (getLangOpts().ObjCNonFragileABI &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000833 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000834 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000835 << property->getDeclName() << Ivar->getDeclName()
836 << ClassDeclared->getDeclName();
837 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000838 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000839 // Note! I deliberately want it to fall thru so more errors are caught.
840 }
841 QualType IvarType = Context.getCanonicalType(Ivar->getType());
842
843 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000844 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
845 compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000846 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000847 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000848 compat =
849 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000850 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000851 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000852 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000853 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
854 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000855 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000856 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000857 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000858 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000859 << property->getDeclName() << PropType
860 << Ivar->getDeclName() << IvarType;
861 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000862 // Note! I deliberately want it to fall thru so, we have a
863 // a property implementation and to avoid future warnings.
864 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000865 else {
866 // FIXME! Rules for properties are somewhat different that those
867 // for assignments. Use a new routine to consolidate all cases;
868 // specifically for property redeclarations as well as for ivars.
869 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
870 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
871 if (lhsType != rhsType &&
872 lhsType->isArithmeticType()) {
873 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
874 << property->getDeclName() << PropType
875 << Ivar->getDeclName() << IvarType;
876 Diag(Ivar->getLocation(), diag::note_ivar_decl);
877 // Fall thru - see previous comment
878 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000879 }
880 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000881 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000882 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000883 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000884 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000885 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000886 // Fall thru - see previous comment
887 }
John McCallf85e1932011-06-15 23:02:42 +0000888 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000889 if ((property->getType()->isObjCObjectPointerType() ||
890 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000891 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000892 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000893 << property->getDeclName() << Ivar->getDeclName();
894 // Fall thru - see previous comment
895 }
896 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000897 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000898 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000899 } else if (PropertyIvar)
900 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +0000901 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000902
Ted Kremenek28685ab2010-03-12 00:46:40 +0000903 assert (property && "ActOnPropertyImplDecl - property declaration missing");
904 ObjCPropertyImplDecl *PIDecl =
905 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
906 property,
907 (Synthesize ?
908 ObjCPropertyImplDecl::Synthesize
909 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000910 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000911
Fariborz Jahanian74414712012-05-15 18:12:51 +0000912 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +0000913 PIDecl->setInvalidDecl();
914
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000915 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
916 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000917 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000918 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000919 // For Objective-C++, need to synthesize the AST for the IVAR object to be
920 // returned by the getter as it must conform to C++'s copy-return rules.
921 // FIXME. Eventually we want to do this for Objective-C as well.
922 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
923 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +0000924 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000925 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000926 Expr *IvarRefExpr =
927 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
928 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000929 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000930 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000931 SourceLocation(),
932 getterMethod->getResultType(),
933 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000934 SourceLocation(),
935 Owned(IvarRefExpr));
936 if (!Res.isInvalid()) {
937 Expr *ResExpr = Res.takeAs<Expr>();
938 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000939 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000940 PIDecl->setGetterCXXConstructor(ResExpr);
941 }
942 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000943 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
944 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
945 Diag(getterMethod->getLocation(),
946 diag::warn_property_getter_owning_mismatch);
947 Diag(property->getLocation(), diag::note_property_declare);
948 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000949 }
950 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
951 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000952 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
953 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000954 // FIXME. Eventually we want to do this for Objective-C as well.
955 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
956 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +0000957 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000958 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000959 Expr *lhs =
960 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
961 SelfExpr, true, true);
962 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
963 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +0000964 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +0000965 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +0000966 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000967 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000968 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000969 if (property->getPropertyAttributes() &
970 ObjCPropertyDecl::OBJC_PR_atomic) {
971 Expr *callExpr = Res.takeAs<Expr>();
972 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000973 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
974 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000975 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000976 if (property->getType()->isReferenceType()) {
977 Diag(PropertyLoc,
978 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000979 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +0000980 Diag(FuncDecl->getLocStart(),
981 diag::note_callee_decl) << FuncDecl;
982 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000983 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000984 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
985 }
986 }
987
Ted Kremenek28685ab2010-03-12 00:46:40 +0000988 if (IC) {
989 if (Synthesize)
990 if (ObjCPropertyImplDecl *PPIDecl =
991 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
992 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
993 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
994 << PropertyIvar;
995 Diag(PPIDecl->getLocation(), diag::note_previous_use);
996 }
997
998 if (ObjCPropertyImplDecl *PPIDecl
999 = IC->FindPropertyImplDecl(PropertyId)) {
1000 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1001 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001002 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001003 }
1004 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001005 if (getLangOpts().ObjCDefaultSynthProperties &&
1006 getLangOpts().ObjCNonFragileABI2 &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001007 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001008 // Diagnose if an ivar was lazily synthesdized due to a previous
1009 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001010 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001011 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001012 ObjCIvarDecl *Ivar = 0;
1013 if (!Synthesize)
1014 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1015 else {
1016 if (PropertyIvar && PropertyIvar != PropertyId)
1017 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1018 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001019 // Issue diagnostics only if Ivar belongs to current class.
1020 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001021 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001022 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1023 << PropertyId;
1024 Ivar->setInvalidDecl();
1025 }
1026 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001027 } else {
1028 if (Synthesize)
1029 if (ObjCPropertyImplDecl *PPIDecl =
1030 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001031 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001032 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1033 << PropertyIvar;
1034 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1035 }
1036
1037 if (ObjCPropertyImplDecl *PPIDecl =
1038 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001039 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001040 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001041 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001042 }
1043 CatImplClass->addPropertyImplementation(PIDecl);
1044 }
1045
John McCalld226f652010-08-21 09:40:31 +00001046 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001047}
1048
1049//===----------------------------------------------------------------------===//
1050// Helper methods.
1051//===----------------------------------------------------------------------===//
1052
Ted Kremenek9d64c152010-03-12 00:38:38 +00001053/// DiagnosePropertyMismatch - Compares two properties for their
1054/// attributes and types and warns on a variety of inconsistencies.
1055///
1056void
1057Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1058 ObjCPropertyDecl *SuperProperty,
1059 const IdentifierInfo *inheritedName) {
1060 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1061 Property->getPropertyAttributes();
1062 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1063 SuperProperty->getPropertyAttributes();
1064 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1065 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1066 Diag(Property->getLocation(), diag::warn_readonly_property)
1067 << Property->getDeclName() << inheritedName;
1068 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1069 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1070 Diag(Property->getLocation(), diag::warn_property_attribute)
1071 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001072 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001073 unsigned CAttrRetain =
1074 (CAttr &
1075 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1076 unsigned SAttrRetain =
1077 (SAttr &
1078 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1079 bool CStrong = (CAttrRetain != 0);
1080 bool SStrong = (SAttrRetain != 0);
1081 if (CStrong != SStrong)
1082 Diag(Property->getLocation(), diag::warn_property_attribute)
1083 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1084 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001085
1086 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1087 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1088 Diag(Property->getLocation(), diag::warn_property_attribute)
1089 << Property->getDeclName() << "atomic" << inheritedName;
1090 if (Property->getSetterName() != SuperProperty->getSetterName())
1091 Diag(Property->getLocation(), diag::warn_property_attribute)
1092 << Property->getDeclName() << "setter" << inheritedName;
1093 if (Property->getGetterName() != SuperProperty->getGetterName())
1094 Diag(Property->getLocation(), diag::warn_property_attribute)
1095 << Property->getDeclName() << "getter" << inheritedName;
1096
1097 QualType LHSType =
1098 Context.getCanonicalType(SuperProperty->getType());
1099 QualType RHSType =
1100 Context.getCanonicalType(Property->getType());
1101
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001102 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001103 // Do cases not handled in above.
1104 // FIXME. For future support of covariant property types, revisit this.
1105 bool IncompatibleObjC = false;
1106 QualType ConvertedType;
1107 if (!isObjCPointerConversion(RHSType, LHSType,
1108 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001109 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001110 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1111 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001112 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1113 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001114 }
1115}
1116
1117bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1118 ObjCMethodDecl *GetterMethod,
1119 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001120 if (!GetterMethod)
1121 return false;
1122 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1123 QualType PropertyIvarType = property->getType().getNonReferenceType();
1124 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1125 if (!compat) {
1126 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1127 isa<ObjCObjectPointerType>(GetterType))
1128 compat =
1129 Context.canAssignObjCInterfaces(
1130 PropertyIvarType->getAs<ObjCObjectPointerType>(),
1131 GetterType->getAs<ObjCObjectPointerType>());
1132 else if (CheckAssignmentConstraints(Loc, PropertyIvarType, GetterType)
1133 != Compatible) {
1134 Diag(Loc, diag::error_property_accessor_type)
1135 << property->getDeclName() << PropertyIvarType
1136 << GetterMethod->getSelector() << GetterType;
1137 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1138 return true;
1139 } else {
1140 compat = true;
1141 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1142 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1143 if (lhsType != rhsType && lhsType->isArithmeticType())
1144 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001145 }
1146 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001147
1148 if (!compat) {
1149 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1150 << property->getDeclName()
1151 << GetterMethod->getSelector();
1152 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1153 return true;
1154 }
1155
Ted Kremenek9d64c152010-03-12 00:38:38 +00001156 return false;
1157}
1158
1159/// ComparePropertiesInBaseAndSuper - This routine compares property
1160/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001161/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001162///
1163void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1164 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1165 if (!SDecl)
1166 return;
1167 // FIXME: O(N^2)
1168 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1169 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie262bc182012-04-30 02:36:29 +00001170 ObjCPropertyDecl *SuperPDecl = &*S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001171 // Does property in super class has declaration in current class?
1172 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1173 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie262bc182012-04-30 02:36:29 +00001174 ObjCPropertyDecl *PDecl = &*I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001175 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1176 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1177 SDecl->getIdentifier());
1178 }
1179 }
1180}
1181
1182/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1183/// of properties declared in a protocol and compares their attribute against
1184/// the same property declared in the class or category.
1185void
1186Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1187 ObjCProtocolDecl *PDecl) {
1188 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1189 if (!IDecl) {
1190 // Category
1191 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1192 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1193 if (!CatDecl->IsClassExtension())
1194 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1195 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001196 ObjCPropertyDecl *Pr = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001197 ObjCCategoryDecl::prop_iterator CP, CE;
1198 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001199 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001200 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001201 break;
1202 if (CP != CE)
1203 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie262bc182012-04-30 02:36:29 +00001204 DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001205 }
1206 return;
1207 }
1208 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1209 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001210 ObjCPropertyDecl *Pr = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001211 ObjCInterfaceDecl::prop_iterator CP, CE;
1212 // Is this property already in class's list of properties?
1213 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001214 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001215 break;
1216 if (CP != CE)
1217 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie262bc182012-04-30 02:36:29 +00001218 DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001219 }
1220}
1221
1222/// CompareProperties - This routine compares properties
1223/// declared in 'ClassOrProtocol' objects (which can be a class or an
1224/// inherited protocol with the list of properties for class/category 'CDecl'
1225///
John McCalld226f652010-08-21 09:40:31 +00001226void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1227 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001228 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1229
1230 if (!IDecl) {
1231 // Category
1232 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1233 assert (CatDecl && "CompareProperties");
1234 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1235 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1236 E = MDecl->protocol_end(); P != E; ++P)
1237 // Match properties of category with those of protocol (*P)
1238 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1239
1240 // Go thru the list of protocols for this category and recursively match
1241 // their properties with those in the category.
1242 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1243 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001244 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001245 } else {
1246 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1247 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1248 E = MD->protocol_end(); P != E; ++P)
1249 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1250 }
1251 return;
1252 }
1253
1254 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001255 for (ObjCInterfaceDecl::all_protocol_iterator
1256 P = MDecl->all_referenced_protocol_begin(),
1257 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001258 // Match properties of class IDecl with those of protocol (*P).
1259 MatchOneProtocolPropertiesInClass(IDecl, *P);
1260
1261 // Go thru the list of protocols for this class and recursively match
1262 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001263 for (ObjCInterfaceDecl::all_protocol_iterator
1264 P = IDecl->all_referenced_protocol_begin(),
1265 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001266 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001267 } else {
1268 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1269 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1270 E = MD->protocol_end(); P != E; ++P)
1271 MatchOneProtocolPropertiesInClass(IDecl, *P);
1272 }
1273}
1274
1275/// isPropertyReadonly - Return true if property is readonly, by searching
1276/// for the property in the class and in its categories and implementations
1277///
1278bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1279 ObjCInterfaceDecl *IDecl) {
1280 // by far the most common case.
1281 if (!PDecl->isReadOnly())
1282 return false;
1283 // Even if property is ready only, if interface has a user defined setter,
1284 // it is not considered read only.
1285 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1286 return false;
1287
1288 // Main class has the property as 'readonly'. Must search
1289 // through the category list to see if the property's
1290 // attribute has been over-ridden to 'readwrite'.
1291 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1292 Category; Category = Category->getNextClassCategory()) {
1293 // Even if property is ready only, if a category has a user defined setter,
1294 // it is not considered read only.
1295 if (Category->getInstanceMethod(PDecl->getSetterName()))
1296 return false;
1297 ObjCPropertyDecl *P =
1298 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1299 if (P && !P->isReadOnly())
1300 return false;
1301 }
1302
1303 // Also, check for definition of a setter method in the implementation if
1304 // all else failed.
1305 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1306 if (ObjCImplementationDecl *IMD =
1307 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1308 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1309 return false;
1310 } else if (ObjCCategoryImplDecl *CIMD =
1311 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1312 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1313 return false;
1314 }
1315 }
1316 // Lastly, look through the implementation (if one is in scope).
1317 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1318 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1319 return false;
1320 // If all fails, look at the super class.
1321 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1322 return isPropertyReadonly(PDecl, SIDecl);
1323 return true;
1324}
1325
1326/// CollectImmediateProperties - This routine collects all properties in
1327/// the class and its conforming protocols; but not those it its super class.
1328void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001329 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1330 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001331 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1332 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1333 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001334 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001335 PropMap[Prop->getIdentifier()] = Prop;
1336 }
1337 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001338 for (ObjCInterfaceDecl::all_protocol_iterator
1339 PI = IDecl->all_referenced_protocol_begin(),
1340 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001341 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001342 }
1343 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1344 if (!CATDecl->IsClassExtension())
1345 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1346 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001347 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001348 PropMap[Prop->getIdentifier()] = Prop;
1349 }
1350 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001351 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001352 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001353 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001354 }
1355 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1356 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1357 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001358 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001359 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1360 // Exclude property for protocols which conform to class's super-class,
1361 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001362 if (!PropertyFromSuper ||
1363 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001364 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1365 if (!PropEntry)
1366 PropEntry = Prop;
1367 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001368 }
1369 // scan through protocol's protocols.
1370 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1371 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001372 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001373 }
1374}
1375
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001376/// CollectClassPropertyImplementations - This routine collects list of
1377/// properties to be implemented in the class. This includes, class's
1378/// and its conforming protocols' properties.
1379static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1380 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1381 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1382 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1383 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001384 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001385 PropMap[Prop->getIdentifier()] = Prop;
1386 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001387 for (ObjCInterfaceDecl::all_protocol_iterator
1388 PI = IDecl->all_referenced_protocol_begin(),
1389 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001390 CollectClassPropertyImplementations((*PI), PropMap);
1391 }
1392 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1393 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1394 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001395 ObjCPropertyDecl *Prop = &*P;
Fariborz Jahanianac371502012-02-23 18:21:25 +00001396 if (!PropMap.count(Prop->getIdentifier()))
1397 PropMap[Prop->getIdentifier()] = Prop;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001398 }
1399 // scan through protocol's protocols.
1400 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1401 E = PDecl->protocol_end(); PI != E; ++PI)
1402 CollectClassPropertyImplementations((*PI), PropMap);
1403 }
1404}
1405
1406/// CollectSuperClassPropertyImplementations - This routine collects list of
1407/// properties to be implemented in super class(s) and also coming from their
1408/// conforming protocols.
1409static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1410 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1411 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1412 while (SDecl) {
1413 CollectClassPropertyImplementations(SDecl, PropMap);
1414 SDecl = SDecl->getSuperClass();
1415 }
1416 }
1417}
1418
Ted Kremenek9d64c152010-03-12 00:38:38 +00001419/// LookupPropertyDecl - Looks up a property in the current class and all
1420/// its protocols.
1421ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1422 IdentifierInfo *II) {
1423 if (const ObjCInterfaceDecl *IDecl =
1424 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1425 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1426 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001427 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001428 if (Prop->getIdentifier() == II)
1429 return Prop;
1430 }
1431 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001432 for (ObjCInterfaceDecl::all_protocol_iterator
1433 PI = IDecl->all_referenced_protocol_begin(),
1434 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001435 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1436 if (Prop)
1437 return Prop;
1438 }
1439 }
1440 else if (const ObjCProtocolDecl *PDecl =
1441 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1442 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1443 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie262bc182012-04-30 02:36:29 +00001444 ObjCPropertyDecl *Prop = &*P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001445 if (Prop->getIdentifier() == II)
1446 return Prop;
1447 }
1448 // scan through protocol's protocols.
1449 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1450 E = PDecl->protocol_end(); PI != E; ++PI) {
1451 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1452 if (Prop)
1453 return Prop;
1454 }
1455 }
1456 return 0;
1457}
1458
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001459static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1460 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001461 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001462 {
1463 llvm::raw_svector_ostream os(ivarName);
1464 os << '_' << Prop->getIdentifier()->getName();
1465 }
1466 return &Ctx.Idents.get(ivarName.str());
1467}
1468
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001469/// DefaultSynthesizeProperties - This routine default synthesizes all
1470/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001471void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1472 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001473
1474 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1475 CollectClassPropertyImplementations(IDecl, PropMap);
1476 if (PropMap.empty())
1477 return;
1478 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1479 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1480
1481 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1482 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1483 ObjCPropertyDecl *Prop = P->second;
1484 // If property to be implemented in the super class, ignore.
1485 if (SuperPropMap[Prop->getIdentifier()])
1486 continue;
1487 // Is there a matching propery synthesize/dynamic?
1488 if (Prop->isInvalidDecl() ||
1489 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1490 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1491 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001492 // Property may have been synthesized by user.
1493 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1494 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001495 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1496 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1497 continue;
1498 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1499 continue;
1500 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001501 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1502 // We won't auto-synthesize properties declared in protocols.
1503 Diag(IMPDecl->getLocation(),
1504 diag::warn_auto_synthesizing_protocol_property);
1505 Diag(Prop->getLocation(), diag::note_property_declare);
1506 continue;
1507 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001508
1509 // We use invalid SourceLocations for the synthesized ivars since they
1510 // aren't really synthesized at a particular location; they just exist.
1511 // Saying that they are located at the @implementation isn't really going
1512 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001513 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1514 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1515 true,
1516 /* property = */ Prop->getIdentifier(),
1517 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
1518 SourceLocation()));
1519 if (PIDecl) {
1520 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001521 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001522 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001523 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001524}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001525
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001526void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1527 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1528 return;
1529 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1530 if (!IC)
1531 return;
1532 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001533 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001534 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001535}
1536
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001537void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001538 ObjCContainerDecl *CDecl,
1539 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001540 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1541 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1542 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1543
Ted Kremenek9d64c152010-03-12 00:38:38 +00001544 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001545 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001546 if (PropMap.empty())
1547 return;
1548
1549 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1550 for (ObjCImplDecl::propimpl_iterator
1551 I = IMPDecl->propimpl_begin(),
1552 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001553 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001554
1555 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1556 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1557 ObjCPropertyDecl *Prop = P->second;
1558 // Is there a matching propery synthesize/dynamic?
1559 if (Prop->isInvalidDecl() ||
1560 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001561 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001562 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001563 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001564 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001565 isa<ObjCCategoryDecl>(CDecl) ?
1566 diag::warn_setter_getter_impl_required_in_category :
1567 diag::warn_setter_getter_impl_required)
1568 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001569 Diag(Prop->getLocation(),
1570 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001571 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1572 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001573 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001574 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1575
Ted Kremenek9d64c152010-03-12 00:38:38 +00001576 }
1577
1578 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001579 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001580 isa<ObjCCategoryDecl>(CDecl) ?
1581 diag::warn_setter_getter_impl_required_in_category :
1582 diag::warn_setter_getter_impl_required)
1583 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001584 Diag(Prop->getLocation(),
1585 diag::note_property_declare);
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001586 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1587 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001588 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001589 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001590 }
1591 }
1592}
1593
1594void
1595Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1596 ObjCContainerDecl* IDecl) {
1597 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001598 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001599 return;
1600 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1601 E = IDecl->prop_end();
1602 I != E; ++I) {
David Blaikie262bc182012-04-30 02:36:29 +00001603 ObjCPropertyDecl *Property = &*I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001604 ObjCMethodDecl *GetterMethod = 0;
1605 ObjCMethodDecl *SetterMethod = 0;
1606 bool LookedUpGetterSetter = false;
1607
Ted Kremenek9d64c152010-03-12 00:38:38 +00001608 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001609 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001610
John McCall265941b2011-09-13 18:31:23 +00001611 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1612 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001613 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1614 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1615 LookedUpGetterSetter = true;
1616 if (GetterMethod) {
1617 Diag(GetterMethod->getLocation(),
1618 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001619 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001620 Diag(Property->getLocation(), diag::note_property_declare);
1621 }
1622 if (SetterMethod) {
1623 Diag(SetterMethod->getLocation(),
1624 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001625 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001626 Diag(Property->getLocation(), diag::note_property_declare);
1627 }
1628 }
1629
Ted Kremenek9d64c152010-03-12 00:38:38 +00001630 // We only care about readwrite atomic property.
1631 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1632 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1633 continue;
1634 if (const ObjCPropertyImplDecl *PIDecl
1635 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1636 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1637 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001638 if (!LookedUpGetterSetter) {
1639 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1640 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1641 LookedUpGetterSetter = true;
1642 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001643 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1644 SourceLocation MethodLoc =
1645 (GetterMethod ? GetterMethod->getLocation()
1646 : SetterMethod->getLocation());
1647 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001648 << Property->getIdentifier() << (GetterMethod != 0)
1649 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001650 // fixit stuff.
1651 if (!AttributesAsWritten) {
1652 if (Property->getLParenLoc().isValid()) {
1653 // @property () ... case.
1654 SourceRange PropSourceRange(Property->getAtLoc(),
1655 Property->getLParenLoc());
1656 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1657 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1658 }
1659 else {
1660 //@property id etc.
1661 SourceLocation endLoc =
1662 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1663 endLoc = endLoc.getLocWithOffset(-1);
1664 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1665 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1666 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1667 }
1668 }
1669 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1670 // @property () ... case.
1671 SourceLocation endLoc = Property->getLParenLoc();
1672 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1673 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1674 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1675 }
1676 else
1677 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001678 Diag(Property->getLocation(), diag::note_property_declare);
1679 }
1680 }
1681 }
1682}
1683
John McCallf85e1932011-06-15 23:02:42 +00001684void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001685 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001686 return;
1687
1688 for (ObjCImplementationDecl::propimpl_iterator
1689 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie262bc182012-04-30 02:36:29 +00001690 ObjCPropertyImplDecl *PID = &*i;
John McCallf85e1932011-06-15 23:02:42 +00001691 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1692 continue;
1693
1694 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001695 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1696 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001697 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1698 if (!method)
1699 continue;
1700 ObjCMethodFamily family = method->getMethodFamily();
1701 if (family == OMF_alloc || family == OMF_copy ||
1702 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001703 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001704 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1705 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001706 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001707 Diag(PD->getLocation(), diag::note_property_declare);
1708 }
1709 }
1710 }
1711}
1712
John McCall5de74d12010-11-10 07:01:40 +00001713/// AddPropertyAttrs - Propagates attributes from a property to the
1714/// implicitly-declared getter or setter for that property.
1715static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1716 ObjCPropertyDecl *Property) {
1717 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001718 for (Decl::attr_iterator A = Property->attr_begin(),
1719 AEnd = Property->attr_end();
1720 A != AEnd; ++A) {
1721 if (isa<DeprecatedAttr>(*A) ||
1722 isa<UnavailableAttr>(*A) ||
1723 isa<AvailabilityAttr>(*A))
1724 PropertyMethod->addAttr((*A)->clone(S.Context));
1725 }
John McCall5de74d12010-11-10 07:01:40 +00001726}
1727
Ted Kremenek9d64c152010-03-12 00:38:38 +00001728/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1729/// have the property type and issue diagnostics if they don't.
1730/// Also synthesize a getter/setter method if none exist (and update the
1731/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1732/// methods is the "right" thing to do.
1733void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001734 ObjCContainerDecl *CD,
1735 ObjCPropertyDecl *redeclaredProperty,
1736 ObjCContainerDecl *lexicalDC) {
1737
Ted Kremenek9d64c152010-03-12 00:38:38 +00001738 ObjCMethodDecl *GetterMethod, *SetterMethod;
1739
1740 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1741 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1742 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1743 property->getLocation());
1744
1745 if (SetterMethod) {
1746 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1747 property->getPropertyAttributes();
1748 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1749 Context.getCanonicalType(SetterMethod->getResultType()) !=
1750 Context.VoidTy)
1751 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1752 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001753 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001754 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1755 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001756 Diag(property->getLocation(),
1757 diag::warn_accessor_property_type_mismatch)
1758 << property->getDeclName()
1759 << SetterMethod->getSelector();
1760 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1761 }
1762 }
1763
1764 // Synthesize getter/setter methods if none exist.
1765 // Find the default getter and if one not found, add one.
1766 // FIXME: The synthesized property we set here is misleading. We almost always
1767 // synthesize these methods unless the user explicitly provided prototypes
1768 // (which is odd, but allowed). Sema should be typechecking that the
1769 // declarations jive in that situation (which it is not currently).
1770 if (!GetterMethod) {
1771 // No instance method of same name as property getter name was found.
1772 // Declare a getter method and add it to the list of methods
1773 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001774 SourceLocation Loc = redeclaredProperty ?
1775 redeclaredProperty->getLocation() :
1776 property->getLocation();
1777
1778 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1779 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001780 property->getType(), 0, CD, /*isInstance=*/true,
1781 /*isVariadic=*/false, /*isSynthesized=*/true,
1782 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001783 (property->getPropertyImplementation() ==
1784 ObjCPropertyDecl::Optional) ?
1785 ObjCMethodDecl::Optional :
1786 ObjCMethodDecl::Required);
1787 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001788
1789 AddPropertyAttrs(*this, GetterMethod, property);
1790
Ted Kremenek23173d72010-05-18 21:09:07 +00001791 // FIXME: Eventually this shouldn't be needed, as the lexical context
1792 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001793 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001794 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001795 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1796 GetterMethod->addAttr(
1797 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001798 } else
1799 // A user declared getter will be synthesize when @synthesize of
1800 // the property with the same name is seen in the @implementation
1801 GetterMethod->setSynthesized(true);
1802 property->setGetterMethodDecl(GetterMethod);
1803
1804 // Skip setter if property is read-only.
1805 if (!property->isReadOnly()) {
1806 // Find the default setter and if one not found, add one.
1807 if (!SetterMethod) {
1808 // No instance method of same name as property setter name was found.
1809 // Declare a setter method and add it to the list of methods
1810 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001811 SourceLocation Loc = redeclaredProperty ?
1812 redeclaredProperty->getLocation() :
1813 property->getLocation();
1814
1815 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001816 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001817 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001818 CD, /*isInstance=*/true, /*isVariadic=*/false,
1819 /*isSynthesized=*/true,
1820 /*isImplicitlyDeclared=*/true,
1821 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001822 (property->getPropertyImplementation() ==
1823 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001824 ObjCMethodDecl::Optional :
1825 ObjCMethodDecl::Required);
1826
Ted Kremenek9d64c152010-03-12 00:38:38 +00001827 // Invent the arguments for the setter. We don't bother making a
1828 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001829 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1830 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001831 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001832 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001833 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001834 SC_None,
1835 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001836 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001837 SetterMethod->setMethodParams(Context, Argument,
1838 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001839
1840 AddPropertyAttrs(*this, SetterMethod, property);
1841
Ted Kremenek9d64c152010-03-12 00:38:38 +00001842 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001843 // FIXME: Eventually this shouldn't be needed, as the lexical context
1844 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001845 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001846 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001847 } else
1848 // A user declared setter will be synthesize when @synthesize of
1849 // the property with the same name is seen in the @implementation
1850 SetterMethod->setSynthesized(true);
1851 property->setSetterMethodDecl(SetterMethod);
1852 }
1853 // Add any synthesized methods to the global pool. This allows us to
1854 // handle the following, which is supported by GCC (and part of the design).
1855 //
1856 // @interface Foo
1857 // @property double bar;
1858 // @end
1859 //
1860 // void thisIsUnfortunate() {
1861 // id foo;
1862 // double bar = [foo bar];
1863 // }
1864 //
1865 if (GetterMethod)
1866 AddInstanceMethodToGlobalPool(GetterMethod);
1867 if (SetterMethod)
1868 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001869
1870 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1871 if (!CurrentClass) {
1872 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1873 CurrentClass = Cat->getClassInterface();
1874 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1875 CurrentClass = Impl->getClassInterface();
1876 }
1877 if (GetterMethod)
1878 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1879 if (SetterMethod)
1880 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001881}
1882
John McCalld226f652010-08-21 09:40:31 +00001883void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001884 SourceLocation Loc,
1885 unsigned &Attributes) {
1886 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001887 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001888 return;
1889
1890 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001891 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001892
David Blaikie4e4d0842012-03-11 07:00:24 +00001893 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001894 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1895 PropertyTy->isObjCRetainableType()) {
1896 // 'readonly' property with no obvious lifetime.
1897 // its life time will be determined by its backing ivar.
1898 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1899 ObjCDeclSpec::DQ_PR_copy |
1900 ObjCDeclSpec::DQ_PR_retain |
1901 ObjCDeclSpec::DQ_PR_strong |
1902 ObjCDeclSpec::DQ_PR_weak |
1903 ObjCDeclSpec::DQ_PR_assign);
1904 if ((Attributes & rel) == 0)
1905 return;
1906 }
1907
Ted Kremenek9d64c152010-03-12 00:38:38 +00001908 // readonly and readwrite/assign/retain/copy conflict.
1909 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1910 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1911 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001912 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001913 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001914 ObjCDeclSpec::DQ_PR_retain |
1915 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001916 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1917 "readwrite" :
1918 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1919 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001920 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1921 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001922 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1923 "copy" : "retain";
1924
1925 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1926 diag::err_objc_property_attr_mutually_exclusive :
1927 diag::warn_objc_property_attr_mutually_exclusive)
1928 << "readonly" << which;
1929 }
1930
1931 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001932 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1933 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1934 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001935 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001936 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001937 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1938 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1939 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1940 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00001941 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001942 }
1943
1944 // Check for more than one of { assign, copy, retain }.
1945 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1946 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1947 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1948 << "assign" << "copy";
1949 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1950 }
1951 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1952 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1953 << "assign" << "retain";
1954 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1955 }
John McCallf85e1932011-06-15 23:02:42 +00001956 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1957 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1958 << "assign" << "strong";
1959 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1960 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001961 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001962 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1963 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1964 << "assign" << "weak";
1965 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1966 }
1967 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1968 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1969 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1970 << "unsafe_unretained" << "copy";
1971 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1972 }
1973 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1974 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1975 << "unsafe_unretained" << "retain";
1976 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1977 }
1978 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1979 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1980 << "unsafe_unretained" << "strong";
1981 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1982 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001983 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001984 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1985 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1986 << "unsafe_unretained" << "weak";
1987 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1988 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001989 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1990 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1991 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1992 << "copy" << "retain";
1993 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1994 }
John McCallf85e1932011-06-15 23:02:42 +00001995 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1996 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1997 << "copy" << "strong";
1998 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1999 }
2000 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2001 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2002 << "copy" << "weak";
2003 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2004 }
2005 }
2006 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2007 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2008 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2009 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002010 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002011 }
2012 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2013 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2014 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2015 << "strong" << "weak";
2016 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002017 }
2018
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002019 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2020 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2021 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2022 << "atomic" << "nonatomic";
2023 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2024 }
2025
Ted Kremenek9d64c152010-03-12 00:38:38 +00002026 // Warn if user supplied no assignment attribute, property is
2027 // readwrite, and this is an object type.
2028 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002029 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2030 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2031 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002032 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002033 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002034 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002035 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002036 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002037 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002038 bool isAnyClassTy =
2039 (PropertyTy->isObjCClassType() ||
2040 PropertyTy->isObjCQualifiedClassType());
2041 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2042 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002043 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002044 ;
2045 else {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002046 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002047 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002048 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002049
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002050 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002051 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002052 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002053 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002054 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002055
2056 // FIXME: Implement warning dependent on NSCopying being
2057 // implemented. See also:
2058 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2059 // (please trim this list while you are at it).
2060 }
2061
2062 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002063 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002064 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002065 && PropertyTy->isBlockPointerType())
2066 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
David Blaikie4e4d0842012-03-11 07:00:24 +00002067 else if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002068 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2069 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2070 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2071 PropertyTy->isBlockPointerType())
2072 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002073
2074 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2075 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2076 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2077
Ted Kremenek9d64c152010-03-12 00:38:38 +00002078}