blob: 82b8094e768585866bfa73f711da2154780964e1 [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"
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Basic/SourceManager.h"
John McCall50df6ae2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Fariborz Jahanianfd090882012-09-21 20:46:37 +000025#include "clang/Lex/Preprocessor.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenek28685ab2010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall265941b2011-09-13 18:31:23 +000033/// getImpliedARCOwnership - Given a set of property attributes and a
34/// type, infer an expected lifetime. The type's ownership qualification
35/// is not considered.
36///
37/// Returns OCL_None if the attributes as stated do not imply an ownership.
38/// Never returns OCL_Autoreleasing.
39static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
41 QualType type) {
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld64c2eb2012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall265941b2011-09-13 18:31:23 +000048 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
52 }
53
54 // assign can appear on other types, so we have to check the
55 // property type.
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
59 }
60
61 return Qualifiers::OCL_None;
62}
63
John McCallf85e1932011-06-15 23:02:42 +000064/// Check the internal consistency of a property declaration.
65static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
66 if (property->isInvalidDecl()) return;
67
68 ObjCPropertyDecl::PropertyAttributeKind propertyKind
69 = property->getPropertyAttributes();
70 Qualifiers::ObjCLifetime propertyLifetime
71 = property->getType().getObjCLifetime();
72
73 // Nothing to do if we don't have a lifetime.
74 if (propertyLifetime == Qualifiers::OCL_None) return;
75
John McCall265941b2011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000080 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
82 // qualifier.
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
88 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
91 }
92 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +0000102 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000106static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
107 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
108 T.isObjCGCWeak()) ||
109 (S.getLangOpts().ObjCAutoRefCount &&
110 T.getObjCLifetime() == Qualifiers::OCL_Weak))
111 return ObjCDeclSpec::DQ_PR_weak;
112 return 0;
113}
114
John McCalld226f652010-08-21 09:40:31 +0000115Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000116 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000117 FieldDeclarator &FD,
118 ObjCDeclSpec &ODS,
119 Selector GetterSel,
120 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000121 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000122 tok::ObjCKeywordKind MethodImplKind,
123 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000124 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000125 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
126 QualType T = TSI->getType();
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000127 Attributes |= deduceWeakPropertyFromType(*this, T);
128
Ted Kremenek28685ab2010-03-12 00:46:40 +0000129 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
130 // default is readwrite!
131 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
132 // property is defaulted to 'assign' if it is readwrite and is
133 // not retain or copy
134 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
135 (isReadWrite &&
136 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +0000137 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
138 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
139 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
140 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000141
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000142 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000143 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000144 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000145 if (CDecl->IsClassExtension()) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000146 Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000147 FD, GetterSel, SetterSel,
148 isAssign, isReadWrite,
149 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000150 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000151 isOverridingProperty, TSI,
152 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000153 if (Res) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000154 CheckObjCPropertyAttributes(Res, AtLoc, Attributes, false);
David Blaikie4e4d0842012-03-11 07:00:24 +0000155 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000156 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
157 }
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000158 ActOnDocumentableDecl(Res);
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000159 return Res;
160 }
161
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000162 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
John McCallf85e1932011-06-15 23:02:42 +0000163 GetterSel, SetterSel,
164 isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000165 Attributes,
166 ODS.getPropertyAttributes(),
167 TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000168 if (lexicalDC)
169 Res->setLexicalDeclContext(lexicalDC);
170
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000171 // Validate the attributes on the @property.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000172 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
173 (isa<ObjCInterfaceDecl>(ClassDecl) ||
174 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000175
David Blaikie4e4d0842012-03-11 07:00:24 +0000176 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000177 checkARCPropertyDecl(*this, Res);
178
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000179 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000180 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000181}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000182
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000183static ObjCPropertyDecl::PropertyAttributeKind
184makePropertyAttributesAsWritten(unsigned Attributes) {
185 unsigned attributesAsWritten = 0;
186 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
187 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
188 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
189 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
190 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
191 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
192 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
193 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
194 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
195 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
196 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
197 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
198 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
199 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
200 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
201 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
202 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
203 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
204 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
205 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
206 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
207 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
208 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
209 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
210
211 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
212}
213
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000214static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000215 SourceLocation LParenLoc, SourceLocation &Loc) {
216 if (LParenLoc.isMacroID())
217 return false;
218
219 SourceManager &SM = Context.getSourceManager();
220 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
221 // Try to load the file buffer.
222 bool invalidTemp = false;
223 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
224 if (invalidTemp)
225 return false;
226 const char *tokenBegin = file.data() + locInfo.second;
227
228 // Lex from the start of the given location.
229 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
230 Context.getLangOpts(),
231 file.begin(), tokenBegin, file.end());
232 Token Tok;
233 do {
234 lexer.LexFromRawLexer(Tok);
235 if (Tok.is(tok::raw_identifier) &&
236 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
237 Loc = Tok.getLocation();
238 return true;
239 }
240 } while (Tok.isNot(tok::r_paren));
241 return false;
242
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000243}
244
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000245static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000246 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
247 ObjCPropertyDecl::OBJC_PR_retain |
248 ObjCPropertyDecl::OBJC_PR_copy |
249 ObjCPropertyDecl::OBJC_PR_weak |
250 ObjCPropertyDecl::OBJC_PR_strong |
251 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
252}
253
John McCalld226f652010-08-21 09:40:31 +0000254Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000255Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000256 SourceLocation AtLoc,
257 SourceLocation LParenLoc,
258 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000259 Selector GetterSel, Selector SetterSel,
260 const bool isAssign,
261 const bool isReadWrite,
262 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000263 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000264 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000265 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000266 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000267 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000268 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000269 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000270 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000271 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
272
273 if (CCPrimary)
274 // Check for duplicate declaration of this property in current and
275 // other class extensions.
276 for (const ObjCCategoryDecl *ClsExtDecl =
277 CCPrimary->getFirstClassExtension();
278 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
279 if (ObjCPropertyDecl *prevDecl =
280 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
281 Diag(AtLoc, diag::err_duplicate_property);
282 Diag(prevDecl->getLocation(), diag::note_property_declare);
283 return 0;
284 }
285 }
286
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000287 // Create a new ObjCPropertyDecl with the DeclContext being
288 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000289 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000290 ObjCPropertyDecl *PDecl =
291 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000292 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000293 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000294 makePropertyAttributesAsWritten(AttributesAsWritten));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000295 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
296 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
297 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
298 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000299 // Set setter/getter selector name. Needed later.
300 PDecl->setGetterName(GetterSel);
301 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000302 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000303 DC->addDecl(PDecl);
304
305 // We need to look in the @interface to see if the @property was
306 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000307 if (!CCPrimary) {
308 Diag(CDecl->getLocation(), diag::err_continuation_class);
309 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000310 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000311 }
312
313 // Find the property in continuation class's primary class only.
314 ObjCPropertyDecl *PIDecl =
315 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
316
317 if (!PIDecl) {
318 // No matching property found in the primary class. Just fall thru
319 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000320 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000321 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000322 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000323 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000324
325 // A case of continuation class adding a new property in the class. This
326 // is not what it was meant for. However, gcc supports it and so should we.
327 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000328 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000329 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000330 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
331 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000332 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000333 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
334 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000335 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000336 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
337 bool IncompatibleObjC = false;
338 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000339 // Relax the strict type matching for property type in continuation class.
340 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000341 // as it narrows the object type in its primary class property. Note that
342 // this conversion is safe only because the wider type is for a 'readonly'
343 // property in primary class and 'narrowed' type for a 'readwrite' property
344 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000345 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
346 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
347 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
348 ConvertedType, IncompatibleObjC))
349 || IncompatibleObjC) {
350 Diag(AtLoc,
351 diag::err_type_mismatch_continuation_class) << PDecl->getType();
352 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000353 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000354 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000355 }
356
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000357 // The property 'PIDecl's readonly attribute will be over-ridden
358 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000359 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000360 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000361 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000362 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
363 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000364 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
365 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000366 Diag(AtLoc, diag::warn_property_attr_mismatch);
367 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000368 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000369 DeclContext *DC = cast<DeclContext>(CCPrimary);
370 if (!ObjCPropertyDecl::findPropertyDecl(DC,
371 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000372 // Protocol is not in the primary class. Must build one for it.
373 ObjCDeclSpec ProtocolPropertyODS;
374 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
375 // and ObjCPropertyDecl::PropertyAttributeKind have identical
376 // values. Should consolidate both into one enum type.
377 ProtocolPropertyODS.
378 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
379 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000380 // Must re-establish the context from class extension to primary
381 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000382 ContextRAII SavedContext(*this, CCPrimary);
383
John McCalld226f652010-08-21 09:40:31 +0000384 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000385 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000386 PIDecl->getGetterName(),
387 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000388 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000389 MethodImplKind,
390 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000391 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000392 }
393 PIDecl->makeitReadWriteAttribute();
394 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
395 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000396 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
397 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000398 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
399 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
400 PIDecl->setSetterName(SetterSel);
401 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000402 // Tailor the diagnostics for the common case where a readwrite
403 // property is declared both in the @interface and the continuation.
404 // This is a common error where the user often intended the original
405 // declaration to be readonly.
406 unsigned diag =
407 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
408 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
409 ? diag::err_use_continuation_class_redeclaration_readwrite
410 : diag::err_use_continuation_class;
411 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000412 << CCPrimary->getDeclName();
413 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000414 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000415 }
416 *isOverridingProperty = true;
417 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000418 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000419 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
420 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000421 if (ASTMutationListener *L = Context.getASTMutationListener())
422 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000423 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000424}
425
426ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
427 ObjCContainerDecl *CDecl,
428 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000429 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000430 FieldDeclarator &FD,
431 Selector GetterSel,
432 Selector SetterSel,
433 const bool isAssign,
434 const bool isReadWrite,
435 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000436 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000437 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000438 tok::ObjCKeywordKind MethodImplKind,
439 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000440 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000441 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000442
443 // Issue a warning if property is 'assign' as default and its object, which is
444 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000445 if (getLangOpts().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000446 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000447 if (const ObjCObjectPointerType *ObjPtrTy =
448 T->getAs<ObjCObjectPointerType>()) {
449 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
450 if (IDecl)
451 if (ObjCProtocolDecl* PNSCopying =
452 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
453 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
454 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000455 }
John McCallc12c5bb2010-05-15 11:32:37 +0000456 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000457 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
458
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000459 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000460 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
461 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000462 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000463
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000464 if (ObjCPropertyDecl *prevDecl =
465 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000466 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000467 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000468 PDecl->setInvalidDecl();
469 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000470 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000471 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000472 if (lexicalDC)
473 PDecl->setLexicalDeclContext(lexicalDC);
474 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000475
476 if (T->isArrayType() || T->isFunctionType()) {
477 Diag(AtLoc, diag::err_property_type) << T;
478 PDecl->setInvalidDecl();
479 }
480
481 ProcessDeclAttributes(S, PDecl, FD.D);
482
483 // Regardless of setter/getter attribute, we save the default getter/setter
484 // selector names in anticipation of declaration of setter/getter methods.
485 PDecl->setGetterName(GetterSel);
486 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000487 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000488 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000489
Ted Kremenek28685ab2010-03-12 00:46:40 +0000490 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
491 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
492
493 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
494 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
495
496 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
497 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
498
499 if (isReadWrite)
500 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
501
502 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
503 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
504
John McCallf85e1932011-06-15 23:02:42 +0000505 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
506 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
507
508 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
509 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
510
Ted Kremenek28685ab2010-03-12 00:46:40 +0000511 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
512 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
513
John McCallf85e1932011-06-15 23:02:42 +0000514 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
515 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
516
Ted Kremenek28685ab2010-03-12 00:46:40 +0000517 if (isAssign)
518 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
519
John McCall265941b2011-09-13 18:31:23 +0000520 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000521 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
522 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000523 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000524 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000525
John McCallf85e1932011-06-15 23:02:42 +0000526 // 'unsafe_unretained' is alias for 'assign'.
527 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
528 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
529 if (isAssign)
530 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
531
Ted Kremenek28685ab2010-03-12 00:46:40 +0000532 if (MethodImplKind == tok::objc_required)
533 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
534 else if (MethodImplKind == tok::objc_optional)
535 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000536
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000537 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000538}
539
John McCallf85e1932011-06-15 23:02:42 +0000540static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
541 ObjCPropertyDecl *property,
542 ObjCIvarDecl *ivar) {
543 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
544
John McCallf85e1932011-06-15 23:02:42 +0000545 QualType ivarType = ivar->getType();
546 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000547
John McCall265941b2011-09-13 18:31:23 +0000548 // The lifetime implied by the property's attributes.
549 Qualifiers::ObjCLifetime propertyLifetime =
550 getImpliedARCOwnership(property->getPropertyAttributes(),
551 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000552
John McCall265941b2011-09-13 18:31:23 +0000553 // We're fine if they match.
554 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000555
John McCall265941b2011-09-13 18:31:23 +0000556 // These aren't valid lifetimes for object ivars; don't diagnose twice.
557 if (ivarLifetime == Qualifiers::OCL_None ||
558 ivarLifetime == Qualifiers::OCL_Autoreleasing)
559 return;
John McCallf85e1932011-06-15 23:02:42 +0000560
John McCalld64c2eb2012-08-20 23:36:59 +0000561 // If the ivar is private, and it's implicitly __unsafe_unretained
562 // becaues of its type, then pretend it was actually implicitly
563 // __strong. This is only sound because we're processing the
564 // property implementation before parsing any method bodies.
565 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
566 propertyLifetime == Qualifiers::OCL_Strong &&
567 ivar->getAccessControl() == ObjCIvarDecl::Private) {
568 SplitQualType split = ivarType.split();
569 if (split.Quals.hasObjCLifetime()) {
570 assert(ivarType->isObjCARCImplicitlyUnretainedType());
571 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
572 ivarType = S.Context.getQualifiedType(split);
573 ivar->setType(ivarType);
574 return;
575 }
576 }
577
John McCall265941b2011-09-13 18:31:23 +0000578 switch (propertyLifetime) {
579 case Qualifiers::OCL_Strong:
580 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
581 << property->getDeclName()
582 << ivar->getDeclName()
583 << ivarLifetime;
584 break;
John McCallf85e1932011-06-15 23:02:42 +0000585
John McCall265941b2011-09-13 18:31:23 +0000586 case Qualifiers::OCL_Weak:
587 S.Diag(propertyImplLoc, diag::error_weak_property)
588 << property->getDeclName()
589 << ivar->getDeclName();
590 break;
John McCallf85e1932011-06-15 23:02:42 +0000591
John McCall265941b2011-09-13 18:31:23 +0000592 case Qualifiers::OCL_ExplicitNone:
593 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
594 << property->getDeclName()
595 << ivar->getDeclName()
596 << ((property->getPropertyAttributesAsWritten()
597 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
598 break;
John McCallf85e1932011-06-15 23:02:42 +0000599
John McCall265941b2011-09-13 18:31:23 +0000600 case Qualifiers::OCL_Autoreleasing:
601 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000602
John McCall265941b2011-09-13 18:31:23 +0000603 case Qualifiers::OCL_None:
604 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000605 return;
606 }
607
608 S.Diag(property->getLocation(), diag::note_property_declare);
609}
610
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000611/// setImpliedPropertyAttributeForReadOnlyProperty -
612/// This routine evaludates life-time attributes for a 'readonly'
613/// property with no known lifetime of its own, using backing
614/// 'ivar's attribute, if any. If no backing 'ivar', property's
615/// life-time is assumed 'strong'.
616static void setImpliedPropertyAttributeForReadOnlyProperty(
617 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
618 Qualifiers::ObjCLifetime propertyLifetime =
619 getImpliedARCOwnership(property->getPropertyAttributes(),
620 property->getType());
621 if (propertyLifetime != Qualifiers::OCL_None)
622 return;
623
624 if (!ivar) {
625 // if no backing ivar, make property 'strong'.
626 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
627 return;
628 }
629 // property assumes owenership of backing ivar.
630 QualType ivarType = ivar->getType();
631 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
632 if (ivarLifetime == Qualifiers::OCL_Strong)
633 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
634 else if (ivarLifetime == Qualifiers::OCL_Weak)
635 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
636 return;
637}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000638
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000639/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
640/// attribute declared in primary class and attributes overridden in any of its
641/// class extensions.
642static void
643DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
644 ObjCPropertyDecl *property) {
645 unsigned Attributes = property->getPropertyAttributesAsWritten();
646 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
647 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
648 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000649 ObjCPropertyDecl *ClassExtProperty = 0;
650 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
651 E = CDecl->prop_end(); P != E; ++P) {
652 if ((*P)->getIdentifier() == property->getIdentifier()) {
653 ClassExtProperty = *P;
654 break;
655 }
656 }
657 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000658 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000659 unsigned classExtPropertyAttr =
660 ClassExtProperty->getPropertyAttributesAsWritten();
661 // We are issuing the warning that we postponed because class extensions
662 // can override readonly->readwrite and 'setter' attributes originally
663 // placed on class's property declaration now make sense in the overridden
664 // property.
665 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
666 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000667 (classExtPropertyAttr &
668 (ObjCDeclSpec::DQ_PR_readwrite|
669 ObjCDeclSpec::DQ_PR_assign |
670 ObjCDeclSpec::DQ_PR_unsafe_unretained |
671 ObjCDeclSpec::DQ_PR_copy |
672 ObjCDeclSpec::DQ_PR_retain |
673 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000674 continue;
675 warn = true;
676 break;
677 }
678 }
679 }
680 if (warn) {
681 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
682 ObjCDeclSpec::DQ_PR_unsafe_unretained |
683 ObjCDeclSpec::DQ_PR_copy |
684 ObjCDeclSpec::DQ_PR_retain |
685 ObjCDeclSpec::DQ_PR_strong);
686 if (Attributes & setterAttrs) {
687 const char * which =
688 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
689 "assign" :
690 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
691 "unsafe_unretained" :
692 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
693 "copy" :
694 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
695 "retain" : "strong";
696
697 S.Diag(property->getLocation(),
698 diag::warn_objc_property_attr_mutually_exclusive)
699 << "readonly" << which;
700 }
701 }
702
703
704}
705
Ted Kremenek28685ab2010-03-12 00:46:40 +0000706/// ActOnPropertyImplDecl - This routine performs semantic checks and
707/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000708/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000709///
John McCalld226f652010-08-21 09:40:31 +0000710Decl *Sema::ActOnPropertyImplDecl(Scope *S,
711 SourceLocation AtLoc,
712 SourceLocation PropertyLoc,
713 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000714 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000715 IdentifierInfo *PropertyIvar,
716 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000717 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000718 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000719 // Make sure we have a context for the property implementation declaration.
720 if (!ClassImpDecl) {
721 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000722 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000723 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000724 if (PropertyIvarLoc.isInvalid())
725 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000726 SourceLocation PropertyDiagLoc = PropertyLoc;
727 if (PropertyDiagLoc.isInvalid())
728 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000729 ObjCPropertyDecl *property = 0;
730 ObjCInterfaceDecl* IDecl = 0;
731 // Find the class or category class where this property must have
732 // a declaration.
733 ObjCImplementationDecl *IC = 0;
734 ObjCCategoryImplDecl* CatImplClass = 0;
735 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
736 IDecl = IC->getClassInterface();
737 // We always synthesize an interface for an implementation
738 // without an interface decl. So, IDecl is always non-zero.
739 assert(IDecl &&
740 "ActOnPropertyImplDecl - @implementation without @interface");
741
742 // Look for this property declaration in the @implementation's @interface
743 property = IDecl->FindPropertyDeclaration(PropertyId);
744 if (!property) {
745 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000746 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000747 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000748 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000749 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
750 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000751 if (AtLoc.isValid())
752 Diag(AtLoc, diag::warn_implicit_atomic_property);
753 else
754 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
755 Diag(property->getLocation(), diag::note_property_declare);
756 }
757
Ted Kremenek28685ab2010-03-12 00:46:40 +0000758 if (const ObjCCategoryDecl *CD =
759 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
760 if (!CD->IsClassExtension()) {
761 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
762 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000763 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000764 }
765 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000766
767 if (Synthesize&&
768 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
769 property->hasAttr<IBOutletAttr>() &&
770 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000771 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
772 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000773 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000774 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000775 property->getLParenLoc(), readonlyLoc)) {
776 SourceLocation endLoc =
777 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
778 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
779 Diag(property->getLocation(),
780 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
781 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
782 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000783 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000784
785 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000786
Ted Kremenek28685ab2010-03-12 00:46:40 +0000787 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
788 if (Synthesize) {
789 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000790 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000791 }
792 IDecl = CatImplClass->getClassInterface();
793 if (!IDecl) {
794 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000795 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000796 }
797 ObjCCategoryDecl *Category =
798 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
799
800 // If category for this implementation not found, it is an error which
801 // has already been reported eralier.
802 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000803 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000804 // Look for this property declaration in @implementation's category
805 property = Category->FindPropertyDeclaration(PropertyId);
806 if (!property) {
807 Diag(PropertyLoc, diag::error_bad_category_property_decl)
808 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000809 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000810 }
811 } else {
812 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000813 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000814 }
815 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000816 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000817 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000818 // Check that we have a valid, previously declared ivar for @synthesize
819 if (Synthesize) {
820 // @synthesize
821 if (!PropertyIvar)
822 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000823 // Check that this is a previously declared 'ivar' in 'IDecl' interface
824 ObjCInterfaceDecl *ClassDeclared;
825 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
826 QualType PropType = property->getType();
827 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000828
829 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000830 diag::err_incomplete_synthesized_property,
831 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000832 Diag(property->getLocation(), diag::note_property_declare);
833 CompleteTypeErr = true;
834 }
835
David Blaikie4e4d0842012-03-11 07:00:24 +0000836 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000837 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000838 ObjCPropertyDecl::OBJC_PR_readonly) &&
839 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000840 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
841 }
842
John McCallf85e1932011-06-15 23:02:42 +0000843 ObjCPropertyDecl::PropertyAttributeKind kind
844 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000845
846 // Add GC __weak to the ivar type if the property is weak.
847 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000848 getLangOpts().getGC() != LangOptions::NonGC) {
849 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000850 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000851 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000852 Diag(property->getLocation(), diag::note_property_declare);
853 } else {
854 PropertyIvarType =
855 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000856 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000857 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000858 if (AtLoc.isInvalid()) {
859 // Check when default synthesizing a property that there is
860 // an ivar matching property name and issue warning; since this
861 // is the most common case of not using an ivar used for backing
862 // property in non-default synthesis case.
863 ObjCInterfaceDecl *ClassDeclared=0;
864 ObjCIvarDecl *originalIvar =
865 IDecl->lookupInstanceVariable(property->getIdentifier(),
866 ClassDeclared);
867 if (originalIvar) {
868 Diag(PropertyDiagLoc,
869 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000870 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000871 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000872 Diag(property->getLocation(), diag::note_property_declare);
873 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000874 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000875 }
876
877 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000878 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000879 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000880 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000881 !PropertyIvarType.getObjCLifetime() &&
882 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000883
John McCall265941b2011-09-13 18:31:23 +0000884 // It's an error if we have to do this and the user didn't
885 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000886 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000887 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000888 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000889 diag::err_arc_objc_property_default_assign_on_object);
890 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000891 } else {
892 Qualifiers::ObjCLifetime lifetime =
893 getImpliedARCOwnership(kind, PropertyIvarType);
894 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000895 if (lifetime == Qualifiers::OCL_Weak) {
896 bool err = false;
897 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000898 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
899 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
900 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000901 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000902 Diag(property->getLocation(), diag::note_property_declare);
903 err = true;
904 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000905 }
John McCall0a7dd782012-08-21 02:47:43 +0000906 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000907 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000908 Diag(property->getLocation(), diag::note_property_declare);
909 }
John McCallf85e1932011-06-15 23:02:42 +0000910 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000911
John McCallf85e1932011-06-15 23:02:42 +0000912 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000913 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000914 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
915 }
John McCallf85e1932011-06-15 23:02:42 +0000916 }
917
918 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000919 !getLangOpts().ObjCAutoRefCount &&
920 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000921 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000922 Diag(property->getLocation(), diag::note_property_declare);
923 }
924
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000925 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000926 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000927 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000928 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000929 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000930 if (CompleteTypeErr)
931 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000932 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000933 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000934 property->setPropertyIvarDecl(Ivar);
935
John McCall260611a2012-06-20 06:18:46 +0000936 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000937 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
938 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000939 // Note! I deliberately want it to fall thru so, we have a
940 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000941 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000942 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000943 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000944 << property->getDeclName() << Ivar->getDeclName()
945 << ClassDeclared->getDeclName();
946 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000947 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000948 // Note! I deliberately want it to fall thru so more errors are caught.
949 }
950 QualType IvarType = Context.getCanonicalType(Ivar->getType());
951
952 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000953 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +0000954 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000955 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +0000956 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000957 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000958 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000959 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000960 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000961 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
962 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000963 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000964 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000965 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000966 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000967 << property->getDeclName() << PropType
968 << Ivar->getDeclName() << IvarType;
969 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000970 // Note! I deliberately want it to fall thru so, we have a
971 // a property implementation and to avoid future warnings.
972 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000973 else {
974 // FIXME! Rules for properties are somewhat different that those
975 // for assignments. Use a new routine to consolidate all cases;
976 // specifically for property redeclarations as well as for ivars.
977 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
978 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
979 if (lhsType != rhsType &&
980 lhsType->isArithmeticType()) {
981 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
982 << property->getDeclName() << PropType
983 << Ivar->getDeclName() << IvarType;
984 Diag(Ivar->getLocation(), diag::note_ivar_decl);
985 // Fall thru - see previous comment
986 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000987 }
988 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000989 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000990 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000991 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000992 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000993 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000994 // Fall thru - see previous comment
995 }
John McCallf85e1932011-06-15 23:02:42 +0000996 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000997 if ((property->getType()->isObjCObjectPointerType() ||
998 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000999 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001000 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001001 << property->getDeclName() << Ivar->getDeclName();
1002 // Fall thru - see previous comment
1003 }
1004 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001005 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001006 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001007 } else if (PropertyIvar)
1008 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001009 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001010
Ted Kremenek28685ab2010-03-12 00:46:40 +00001011 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1012 ObjCPropertyImplDecl *PIDecl =
1013 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1014 property,
1015 (Synthesize ?
1016 ObjCPropertyImplDecl::Synthesize
1017 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001018 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001019
Fariborz Jahanian74414712012-05-15 18:12:51 +00001020 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001021 PIDecl->setInvalidDecl();
1022
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001023 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1024 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001025 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001026 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001027 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1028 // returned by the getter as it must conform to C++'s copy-return rules.
1029 // FIXME. Eventually we want to do this for Objective-C as well.
1030 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1031 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001032 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001033 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001034 Expr *IvarRefExpr =
1035 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1036 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001037 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001038 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001039 SourceLocation(),
1040 getterMethod->getResultType(),
1041 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001042 SourceLocation(),
1043 Owned(IvarRefExpr));
1044 if (!Res.isInvalid()) {
1045 Expr *ResExpr = Res.takeAs<Expr>();
1046 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001047 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001048 PIDecl->setGetterCXXConstructor(ResExpr);
1049 }
1050 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001051 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1052 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1053 Diag(getterMethod->getLocation(),
1054 diag::warn_property_getter_owning_mismatch);
1055 Diag(property->getLocation(), diag::note_property_declare);
1056 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001057 }
1058 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1059 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001060 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1061 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001062 // FIXME. Eventually we want to do this for Objective-C as well.
1063 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1064 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001065 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001066 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001067 Expr *lhs =
1068 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
1069 SelfExpr, true, true);
1070 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1071 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001072 QualType T = Param->getType().getNonReferenceType();
John McCallf4b88a42012-03-10 09:33:50 +00001073 Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
John McCallf89e55a2010-11-18 06:31:45 +00001074 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +00001075 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +00001076 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001077 if (property->getPropertyAttributes() &
1078 ObjCPropertyDecl::OBJC_PR_atomic) {
1079 Expr *callExpr = Res.takeAs<Expr>();
1080 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001081 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1082 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001083 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001084 if (property->getType()->isReferenceType()) {
1085 Diag(PropertyLoc,
1086 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001087 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001088 Diag(FuncDecl->getLocStart(),
1089 diag::note_callee_decl) << FuncDecl;
1090 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001091 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001092 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1093 }
1094 }
1095
Ted Kremenek28685ab2010-03-12 00:46:40 +00001096 if (IC) {
1097 if (Synthesize)
1098 if (ObjCPropertyImplDecl *PPIDecl =
1099 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1100 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1101 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1102 << PropertyIvar;
1103 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1104 }
1105
1106 if (ObjCPropertyImplDecl *PPIDecl
1107 = IC->FindPropertyImplDecl(PropertyId)) {
1108 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1109 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001110 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001111 }
1112 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001113 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001114 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001115 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001116 // Diagnose if an ivar was lazily synthesdized due to a previous
1117 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001118 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001119 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001120 ObjCIvarDecl *Ivar = 0;
1121 if (!Synthesize)
1122 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1123 else {
1124 if (PropertyIvar && PropertyIvar != PropertyId)
1125 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1126 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001127 // Issue diagnostics only if Ivar belongs to current class.
1128 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001129 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001130 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1131 << PropertyId;
1132 Ivar->setInvalidDecl();
1133 }
1134 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001135 } else {
1136 if (Synthesize)
1137 if (ObjCPropertyImplDecl *PPIDecl =
1138 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001139 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001140 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1141 << PropertyIvar;
1142 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1143 }
1144
1145 if (ObjCPropertyImplDecl *PPIDecl =
1146 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001147 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001148 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001149 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001150 }
1151 CatImplClass->addPropertyImplementation(PIDecl);
1152 }
1153
John McCalld226f652010-08-21 09:40:31 +00001154 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001155}
1156
1157//===----------------------------------------------------------------------===//
1158// Helper methods.
1159//===----------------------------------------------------------------------===//
1160
Ted Kremenek9d64c152010-03-12 00:38:38 +00001161/// DiagnosePropertyMismatch - Compares two properties for their
1162/// attributes and types and warns on a variety of inconsistencies.
1163///
1164void
1165Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1166 ObjCPropertyDecl *SuperProperty,
1167 const IdentifierInfo *inheritedName) {
1168 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1169 Property->getPropertyAttributes();
1170 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1171 SuperProperty->getPropertyAttributes();
1172 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1173 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1174 Diag(Property->getLocation(), diag::warn_readonly_property)
1175 << Property->getDeclName() << inheritedName;
1176 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1177 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1178 Diag(Property->getLocation(), diag::warn_property_attribute)
1179 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001180 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001181 unsigned CAttrRetain =
1182 (CAttr &
1183 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1184 unsigned SAttrRetain =
1185 (SAttr &
1186 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1187 bool CStrong = (CAttrRetain != 0);
1188 bool SStrong = (SAttrRetain != 0);
1189 if (CStrong != SStrong)
1190 Diag(Property->getLocation(), diag::warn_property_attribute)
1191 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1192 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001193
1194 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1195 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1196 Diag(Property->getLocation(), diag::warn_property_attribute)
1197 << Property->getDeclName() << "atomic" << inheritedName;
1198 if (Property->getSetterName() != SuperProperty->getSetterName())
1199 Diag(Property->getLocation(), diag::warn_property_attribute)
1200 << Property->getDeclName() << "setter" << inheritedName;
1201 if (Property->getGetterName() != SuperProperty->getGetterName())
1202 Diag(Property->getLocation(), diag::warn_property_attribute)
1203 << Property->getDeclName() << "getter" << inheritedName;
1204
1205 QualType LHSType =
1206 Context.getCanonicalType(SuperProperty->getType());
1207 QualType RHSType =
1208 Context.getCanonicalType(Property->getType());
1209
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001210 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001211 // Do cases not handled in above.
1212 // FIXME. For future support of covariant property types, revisit this.
1213 bool IncompatibleObjC = false;
1214 QualType ConvertedType;
1215 if (!isObjCPointerConversion(RHSType, LHSType,
1216 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001217 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001218 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1219 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001220 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1221 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001222 }
1223}
1224
1225bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1226 ObjCMethodDecl *GetterMethod,
1227 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001228 if (!GetterMethod)
1229 return false;
1230 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1231 QualType PropertyIvarType = property->getType().getNonReferenceType();
1232 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1233 if (!compat) {
1234 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1235 isa<ObjCObjectPointerType>(GetterType))
1236 compat =
1237 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001238 GetterType->getAs<ObjCObjectPointerType>(),
1239 PropertyIvarType->getAs<ObjCObjectPointerType>());
1240 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001241 != Compatible) {
1242 Diag(Loc, diag::error_property_accessor_type)
1243 << property->getDeclName() << PropertyIvarType
1244 << GetterMethod->getSelector() << GetterType;
1245 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1246 return true;
1247 } else {
1248 compat = true;
1249 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1250 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1251 if (lhsType != rhsType && lhsType->isArithmeticType())
1252 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001253 }
1254 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001255
1256 if (!compat) {
1257 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1258 << property->getDeclName()
1259 << GetterMethod->getSelector();
1260 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1261 return true;
1262 }
1263
Ted Kremenek9d64c152010-03-12 00:38:38 +00001264 return false;
1265}
1266
1267/// ComparePropertiesInBaseAndSuper - This routine compares property
1268/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001269/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001270///
1271void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1272 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1273 if (!SDecl)
1274 return;
1275 // FIXME: O(N^2)
1276 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1277 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001278 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001279 // Does property in super class has declaration in current class?
1280 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1281 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001282 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001283 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1284 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1285 SDecl->getIdentifier());
1286 }
1287 }
1288}
1289
1290/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1291/// of properties declared in a protocol and compares their attribute against
1292/// the same property declared in the class or category.
1293void
1294Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1295 ObjCProtocolDecl *PDecl) {
1296 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1297 if (!IDecl) {
1298 // Category
1299 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1300 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1301 if (!CatDecl->IsClassExtension())
1302 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1303 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001304 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001305 ObjCCategoryDecl::prop_iterator CP, CE;
1306 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001307 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001308 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001309 break;
1310 if (CP != CE)
1311 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001312 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001313 }
1314 return;
1315 }
1316 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1317 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001318 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001319 ObjCInterfaceDecl::prop_iterator CP, CE;
1320 // Is this property already in class's list of properties?
1321 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001322 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001323 break;
1324 if (CP != CE)
1325 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001326 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001327 }
1328}
1329
1330/// CompareProperties - This routine compares properties
1331/// declared in 'ClassOrProtocol' objects (which can be a class or an
1332/// inherited protocol with the list of properties for class/category 'CDecl'
1333///
John McCalld226f652010-08-21 09:40:31 +00001334void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1335 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001336 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1337
1338 if (!IDecl) {
1339 // Category
1340 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1341 assert (CatDecl && "CompareProperties");
1342 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1343 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1344 E = MDecl->protocol_end(); P != E; ++P)
1345 // Match properties of category with those of protocol (*P)
1346 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1347
1348 // Go thru the list of protocols for this category and recursively match
1349 // their properties with those in the category.
1350 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1351 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001352 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001353 } else {
1354 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1355 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1356 E = MD->protocol_end(); P != E; ++P)
1357 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1358 }
1359 return;
1360 }
1361
1362 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001363 for (ObjCInterfaceDecl::all_protocol_iterator
1364 P = MDecl->all_referenced_protocol_begin(),
1365 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001366 // Match properties of class IDecl with those of protocol (*P).
1367 MatchOneProtocolPropertiesInClass(IDecl, *P);
1368
1369 // Go thru the list of protocols for this class and recursively match
1370 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001371 for (ObjCInterfaceDecl::all_protocol_iterator
1372 P = IDecl->all_referenced_protocol_begin(),
1373 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001374 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001375 } else {
1376 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1377 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1378 E = MD->protocol_end(); P != E; ++P)
1379 MatchOneProtocolPropertiesInClass(IDecl, *P);
1380 }
1381}
1382
1383/// isPropertyReadonly - Return true if property is readonly, by searching
1384/// for the property in the class and in its categories and implementations
1385///
1386bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1387 ObjCInterfaceDecl *IDecl) {
1388 // by far the most common case.
1389 if (!PDecl->isReadOnly())
1390 return false;
1391 // Even if property is ready only, if interface has a user defined setter,
1392 // it is not considered read only.
1393 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1394 return false;
1395
1396 // Main class has the property as 'readonly'. Must search
1397 // through the category list to see if the property's
1398 // attribute has been over-ridden to 'readwrite'.
1399 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1400 Category; Category = Category->getNextClassCategory()) {
1401 // Even if property is ready only, if a category has a user defined setter,
1402 // it is not considered read only.
1403 if (Category->getInstanceMethod(PDecl->getSetterName()))
1404 return false;
1405 ObjCPropertyDecl *P =
1406 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1407 if (P && !P->isReadOnly())
1408 return false;
1409 }
1410
1411 // Also, check for definition of a setter method in the implementation if
1412 // all else failed.
1413 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1414 if (ObjCImplementationDecl *IMD =
1415 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1416 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1417 return false;
1418 } else if (ObjCCategoryImplDecl *CIMD =
1419 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1420 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1421 return false;
1422 }
1423 }
1424 // Lastly, look through the implementation (if one is in scope).
1425 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1426 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1427 return false;
1428 // If all fails, look at the super class.
1429 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1430 return isPropertyReadonly(PDecl, SIDecl);
1431 return true;
1432}
1433
1434/// CollectImmediateProperties - This routine collects all properties in
1435/// the class and its conforming protocols; but not those it its super class.
1436void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001437 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1438 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001439 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1440 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1441 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001442 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001443 PropMap[Prop->getIdentifier()] = Prop;
1444 }
1445 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001446 for (ObjCInterfaceDecl::all_protocol_iterator
1447 PI = IDecl->all_referenced_protocol_begin(),
1448 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001449 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001450 }
1451 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1452 if (!CATDecl->IsClassExtension())
1453 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1454 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001455 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001456 PropMap[Prop->getIdentifier()] = Prop;
1457 }
1458 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001459 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001460 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001461 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001462 }
1463 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1464 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1465 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001466 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001467 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1468 // Exclude property for protocols which conform to class's super-class,
1469 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001470 if (!PropertyFromSuper ||
1471 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001472 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1473 if (!PropEntry)
1474 PropEntry = Prop;
1475 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001476 }
1477 // scan through protocol's protocols.
1478 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1479 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001480 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001481 }
1482}
1483
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001484/// CollectClassPropertyImplementations - This routine collects list of
1485/// properties to be implemented in the class. This includes, class's
1486/// and its conforming protocols' properties.
1487static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1488 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1489 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1490 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1491 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001492 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001493 PropMap[Prop->getIdentifier()] = Prop;
1494 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001495 for (ObjCInterfaceDecl::all_protocol_iterator
1496 PI = IDecl->all_referenced_protocol_begin(),
1497 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001498 CollectClassPropertyImplementations((*PI), PropMap);
1499 }
1500 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1501 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1502 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001503 ObjCPropertyDecl *Prop = *P;
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00001504 // Insert into PropMap if not there already.
1505 PropMap.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001506 }
1507 // scan through protocol's protocols.
1508 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1509 E = PDecl->protocol_end(); PI != E; ++PI)
1510 CollectClassPropertyImplementations((*PI), PropMap);
1511 }
1512}
1513
1514/// CollectSuperClassPropertyImplementations - This routine collects list of
1515/// properties to be implemented in super class(s) and also coming from their
1516/// conforming protocols.
1517static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1518 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1519 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1520 while (SDecl) {
1521 CollectClassPropertyImplementations(SDecl, PropMap);
1522 SDecl = SDecl->getSuperClass();
1523 }
1524 }
1525}
1526
Ted Kremenek9d64c152010-03-12 00:38:38 +00001527/// LookupPropertyDecl - Looks up a property in the current class and all
1528/// its protocols.
1529ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1530 IdentifierInfo *II) {
1531 if (const ObjCInterfaceDecl *IDecl =
1532 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1533 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1534 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001535 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001536 if (Prop->getIdentifier() == II)
1537 return Prop;
1538 }
1539 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001540 for (ObjCInterfaceDecl::all_protocol_iterator
1541 PI = IDecl->all_referenced_protocol_begin(),
1542 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001543 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1544 if (Prop)
1545 return Prop;
1546 }
1547 }
1548 else if (const ObjCProtocolDecl *PDecl =
1549 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1550 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1551 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001552 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001553 if (Prop->getIdentifier() == II)
1554 return Prop;
1555 }
1556 // scan through protocol's protocols.
1557 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1558 E = PDecl->protocol_end(); PI != E; ++PI) {
1559 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1560 if (Prop)
1561 return Prop;
1562 }
1563 }
Fariborz Jahanianfd090882012-09-21 20:46:37 +00001564 else if (const ObjCCategoryDecl *CatDecl =
1565 dyn_cast<ObjCCategoryDecl>(CDecl)) {
1566 for (ObjCContainerDecl::prop_iterator P = CatDecl->prop_begin(),
1567 E = CatDecl->prop_end(); P != E; ++P) {
1568 ObjCPropertyDecl *Prop = *P;
1569 if (Prop->getIdentifier() == II)
1570 return Prop;
1571 }
1572 }
1573 return 0;
1574}
1575
1576/// PropertyIfSetterOrGetter - Looks up the property if named declaration
1577/// is a setter or getter method backing a property.
1578ObjCPropertyDecl *Sema::PropertyIfSetterOrGetter(NamedDecl *D) {
1579 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
1580 Selector Sel = Method->getSelector();
1581 IdentifierInfo *Id = 0;
1582 if (Sel.getNumArgs() == 0)
1583 Id = Sel.getIdentifierInfoForSlot(0);
1584 else if (Sel.getNumArgs() == 1) {
1585 StringRef str = Sel.getNameForSlot(0);
1586 if (str.startswith("set")) {
1587 str = str.substr(3);
1588 char front = str.front();
1589 front = islower(front) ? toupper(front) : tolower(front);
1590 SmallString<100> PropertyName = str;
1591 PropertyName[0] = front;
1592 Id = &PP.getIdentifierTable().get(PropertyName);
1593 }
1594 }
1595 if (Id) {
1596 if (isa<ObjCInterfaceDecl>(Method->getDeclContext())) {
1597 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
1598 while (IDecl) {
1599 ObjCPropertyDecl *PDecl =
1600 LookupPropertyDecl(cast<ObjCContainerDecl>(IDecl), Id);
1601 if (PDecl)
1602 return PDecl;
1603 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1604 Category; Category = Category->getNextClassCategory())
1605 if ((PDecl =
1606 LookupPropertyDecl(cast<ObjCContainerDecl>(Category), Id)))
1607 return PDecl;
1608 IDecl = IDecl->getSuperClass();
1609 }
1610 } else if (ObjCPropertyDecl *PDecl =
1611 LookupPropertyDecl(
1612 cast<ObjCContainerDecl>(Method->getDeclContext()), Id))
1613 return PDecl;
1614 }
1615 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001616 return 0;
1617}
1618
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001619static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1620 ASTContext &Ctx) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001621 SmallString<128> ivarName;
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001622 {
1623 llvm::raw_svector_ostream os(ivarName);
1624 os << '_' << Prop->getIdentifier()->getName();
1625 }
1626 return &Ctx.Idents.get(ivarName.str());
1627}
1628
James Dennett699c9042012-06-15 07:13:21 +00001629/// \brief Default synthesizes all properties which must be synthesized
1630/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001631void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1632 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001633
1634 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1635 CollectClassPropertyImplementations(IDecl, PropMap);
1636 if (PropMap.empty())
1637 return;
1638 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1639 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1640
1641 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1642 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1643 ObjCPropertyDecl *Prop = P->second;
1644 // If property to be implemented in the super class, ignore.
1645 if (SuperPropMap[Prop->getIdentifier()])
1646 continue;
1647 // Is there a matching propery synthesize/dynamic?
1648 if (Prop->isInvalidDecl() ||
1649 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1650 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1651 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001652 // Property may have been synthesized by user.
1653 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1654 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001655 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1656 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1657 continue;
1658 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1659 continue;
1660 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001661 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1662 // We won't auto-synthesize properties declared in protocols.
1663 Diag(IMPDecl->getLocation(),
1664 diag::warn_auto_synthesizing_protocol_property);
1665 Diag(Prop->getLocation(), diag::note_property_declare);
1666 continue;
1667 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001668
1669 // We use invalid SourceLocations for the synthesized ivars since they
1670 // aren't really synthesized at a particular location; they just exist.
1671 // Saying that they are located at the @implementation isn't really going
1672 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001673 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1674 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1675 true,
1676 /* property = */ Prop->getIdentifier(),
1677 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001678 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001679 if (PIDecl) {
1680 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001681 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001682 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001683 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001684}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001685
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001686void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001687 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001688 return;
1689 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1690 if (!IC)
1691 return;
1692 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001693 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001694 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001695}
1696
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001697void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001698 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001699 const SelectorSet &InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001700 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1701 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1702 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1703
Ted Kremenek9d64c152010-03-12 00:38:38 +00001704 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001705 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001706 if (PropMap.empty())
1707 return;
1708
1709 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1710 for (ObjCImplDecl::propimpl_iterator
1711 I = IMPDecl->propimpl_begin(),
1712 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001713 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001714
1715 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1716 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1717 ObjCPropertyDecl *Prop = P->second;
1718 // Is there a matching propery synthesize/dynamic?
1719 if (Prop->isInvalidDecl() ||
1720 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001721 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001722 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001723 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001724 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001725 isa<ObjCCategoryDecl>(CDecl) ?
1726 diag::warn_setter_getter_impl_required_in_category :
1727 diag::warn_setter_getter_impl_required)
1728 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001729 Diag(Prop->getLocation(),
1730 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001731 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001732 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001733 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001734 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1735
Ted Kremenek9d64c152010-03-12 00:38:38 +00001736 }
1737
1738 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001739 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001740 isa<ObjCCategoryDecl>(CDecl) ?
1741 diag::warn_setter_getter_impl_required_in_category :
1742 diag::warn_setter_getter_impl_required)
1743 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001744 Diag(Prop->getLocation(),
1745 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001746 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001747 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001748 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001749 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001750 }
1751 }
1752}
1753
1754void
1755Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1756 ObjCContainerDecl* IDecl) {
1757 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001758 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001759 return;
1760 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1761 E = IDecl->prop_end();
1762 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001763 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001764 ObjCMethodDecl *GetterMethod = 0;
1765 ObjCMethodDecl *SetterMethod = 0;
1766 bool LookedUpGetterSetter = false;
1767
Ted Kremenek9d64c152010-03-12 00:38:38 +00001768 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001769 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001770
John McCall265941b2011-09-13 18:31:23 +00001771 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1772 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001773 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1774 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1775 LookedUpGetterSetter = true;
1776 if (GetterMethod) {
1777 Diag(GetterMethod->getLocation(),
1778 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001779 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001780 Diag(Property->getLocation(), diag::note_property_declare);
1781 }
1782 if (SetterMethod) {
1783 Diag(SetterMethod->getLocation(),
1784 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001785 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001786 Diag(Property->getLocation(), diag::note_property_declare);
1787 }
1788 }
1789
Ted Kremenek9d64c152010-03-12 00:38:38 +00001790 // We only care about readwrite atomic property.
1791 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1792 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1793 continue;
1794 if (const ObjCPropertyImplDecl *PIDecl
1795 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1796 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1797 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001798 if (!LookedUpGetterSetter) {
1799 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1800 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1801 LookedUpGetterSetter = true;
1802 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001803 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1804 SourceLocation MethodLoc =
1805 (GetterMethod ? GetterMethod->getLocation()
1806 : SetterMethod->getLocation());
1807 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001808 << Property->getIdentifier() << (GetterMethod != 0)
1809 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001810 // fixit stuff.
1811 if (!AttributesAsWritten) {
1812 if (Property->getLParenLoc().isValid()) {
1813 // @property () ... case.
1814 SourceRange PropSourceRange(Property->getAtLoc(),
1815 Property->getLParenLoc());
1816 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1817 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1818 }
1819 else {
1820 //@property id etc.
1821 SourceLocation endLoc =
1822 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1823 endLoc = endLoc.getLocWithOffset(-1);
1824 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1825 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1826 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1827 }
1828 }
1829 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1830 // @property () ... case.
1831 SourceLocation endLoc = Property->getLParenLoc();
1832 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1833 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1834 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1835 }
1836 else
1837 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001838 Diag(Property->getLocation(), diag::note_property_declare);
1839 }
1840 }
1841 }
1842}
1843
John McCallf85e1932011-06-15 23:02:42 +00001844void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001845 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001846 return;
1847
1848 for (ObjCImplementationDecl::propimpl_iterator
1849 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001850 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001851 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1852 continue;
1853
1854 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001855 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1856 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001857 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1858 if (!method)
1859 continue;
1860 ObjCMethodFamily family = method->getMethodFamily();
1861 if (family == OMF_alloc || family == OMF_copy ||
1862 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001863 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001864 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1865 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001866 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001867 Diag(PD->getLocation(), diag::note_property_declare);
1868 }
1869 }
1870 }
1871}
1872
John McCall5de74d12010-11-10 07:01:40 +00001873/// AddPropertyAttrs - Propagates attributes from a property to the
1874/// implicitly-declared getter or setter for that property.
1875static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1876 ObjCPropertyDecl *Property) {
1877 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001878 for (Decl::attr_iterator A = Property->attr_begin(),
1879 AEnd = Property->attr_end();
1880 A != AEnd; ++A) {
1881 if (isa<DeprecatedAttr>(*A) ||
1882 isa<UnavailableAttr>(*A) ||
1883 isa<AvailabilityAttr>(*A))
1884 PropertyMethod->addAttr((*A)->clone(S.Context));
1885 }
John McCall5de74d12010-11-10 07:01:40 +00001886}
1887
Ted Kremenek9d64c152010-03-12 00:38:38 +00001888/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1889/// have the property type and issue diagnostics if they don't.
1890/// Also synthesize a getter/setter method if none exist (and update the
1891/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1892/// methods is the "right" thing to do.
1893void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001894 ObjCContainerDecl *CD,
1895 ObjCPropertyDecl *redeclaredProperty,
1896 ObjCContainerDecl *lexicalDC) {
1897
Ted Kremenek9d64c152010-03-12 00:38:38 +00001898 ObjCMethodDecl *GetterMethod, *SetterMethod;
1899
1900 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1901 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1902 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1903 property->getLocation());
1904
1905 if (SetterMethod) {
1906 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1907 property->getPropertyAttributes();
1908 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1909 Context.getCanonicalType(SetterMethod->getResultType()) !=
1910 Context.VoidTy)
1911 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1912 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001913 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001914 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1915 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001916 Diag(property->getLocation(),
1917 diag::warn_accessor_property_type_mismatch)
1918 << property->getDeclName()
1919 << SetterMethod->getSelector();
1920 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1921 }
1922 }
1923
1924 // Synthesize getter/setter methods if none exist.
1925 // Find the default getter and if one not found, add one.
1926 // FIXME: The synthesized property we set here is misleading. We almost always
1927 // synthesize these methods unless the user explicitly provided prototypes
1928 // (which is odd, but allowed). Sema should be typechecking that the
1929 // declarations jive in that situation (which it is not currently).
1930 if (!GetterMethod) {
1931 // No instance method of same name as property getter name was found.
1932 // Declare a getter method and add it to the list of methods
1933 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001934 SourceLocation Loc = redeclaredProperty ?
1935 redeclaredProperty->getLocation() :
1936 property->getLocation();
1937
1938 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1939 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001940 property->getType(), 0, CD, /*isInstance=*/true,
1941 /*isVariadic=*/false, /*isSynthesized=*/true,
1942 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001943 (property->getPropertyImplementation() ==
1944 ObjCPropertyDecl::Optional) ?
1945 ObjCMethodDecl::Optional :
1946 ObjCMethodDecl::Required);
1947 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001948
1949 AddPropertyAttrs(*this, GetterMethod, property);
1950
Ted Kremenek23173d72010-05-18 21:09:07 +00001951 // FIXME: Eventually this shouldn't be needed, as the lexical context
1952 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001953 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001954 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001955 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1956 GetterMethod->addAttr(
1957 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001958 } else
1959 // A user declared getter will be synthesize when @synthesize of
1960 // the property with the same name is seen in the @implementation
1961 GetterMethod->setSynthesized(true);
1962 property->setGetterMethodDecl(GetterMethod);
1963
1964 // Skip setter if property is read-only.
1965 if (!property->isReadOnly()) {
1966 // Find the default setter and if one not found, add one.
1967 if (!SetterMethod) {
1968 // No instance method of same name as property setter name was found.
1969 // Declare a setter method and add it to the list of methods
1970 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001971 SourceLocation Loc = redeclaredProperty ?
1972 redeclaredProperty->getLocation() :
1973 property->getLocation();
1974
1975 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001976 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001977 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001978 CD, /*isInstance=*/true, /*isVariadic=*/false,
1979 /*isSynthesized=*/true,
1980 /*isImplicitlyDeclared=*/true,
1981 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001982 (property->getPropertyImplementation() ==
1983 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001984 ObjCMethodDecl::Optional :
1985 ObjCMethodDecl::Required);
1986
Ted Kremenek9d64c152010-03-12 00:38:38 +00001987 // Invent the arguments for the setter. We don't bother making a
1988 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001989 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1990 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001991 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001992 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001993 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001994 SC_None,
1995 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001996 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001997 SetterMethod->setMethodParams(Context, Argument,
1998 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001999
2000 AddPropertyAttrs(*this, SetterMethod, property);
2001
Ted Kremenek9d64c152010-03-12 00:38:38 +00002002 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00002003 // FIXME: Eventually this shouldn't be needed, as the lexical context
2004 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00002005 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00002006 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002007 } else
2008 // A user declared setter will be synthesize when @synthesize of
2009 // the property with the same name is seen in the @implementation
2010 SetterMethod->setSynthesized(true);
2011 property->setSetterMethodDecl(SetterMethod);
2012 }
2013 // Add any synthesized methods to the global pool. This allows us to
2014 // handle the following, which is supported by GCC (and part of the design).
2015 //
2016 // @interface Foo
2017 // @property double bar;
2018 // @end
2019 //
2020 // void thisIsUnfortunate() {
2021 // id foo;
2022 // double bar = [foo bar];
2023 // }
2024 //
2025 if (GetterMethod)
2026 AddInstanceMethodToGlobalPool(GetterMethod);
2027 if (SetterMethod)
2028 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002029
2030 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2031 if (!CurrentClass) {
2032 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2033 CurrentClass = Cat->getClassInterface();
2034 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2035 CurrentClass = Impl->getClassInterface();
2036 }
2037 if (GetterMethod)
2038 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2039 if (SetterMethod)
2040 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002041}
2042
John McCalld226f652010-08-21 09:40:31 +00002043void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002044 SourceLocation Loc,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002045 unsigned &Attributes,
2046 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002047 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00002048 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00002049 return;
2050
2051 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002052 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002053
David Blaikie4e4d0842012-03-11 07:00:24 +00002054 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002055 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2056 PropertyTy->isObjCRetainableType()) {
2057 // 'readonly' property with no obvious lifetime.
2058 // its life time will be determined by its backing ivar.
2059 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
2060 ObjCDeclSpec::DQ_PR_copy |
2061 ObjCDeclSpec::DQ_PR_retain |
2062 ObjCDeclSpec::DQ_PR_strong |
2063 ObjCDeclSpec::DQ_PR_weak |
2064 ObjCDeclSpec::DQ_PR_assign);
2065 if ((Attributes & rel) == 0)
2066 return;
2067 }
2068
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002069 if (propertyInPrimaryClass) {
2070 // we postpone most property diagnosis until class's implementation
2071 // because, its readonly attribute may be overridden in its class
2072 // extensions making other attributes, which make no sense, to make sense.
2073 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2074 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2075 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2076 << "readonly" << "readwrite";
2077 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002078 // readonly and readwrite/assign/retain/copy conflict.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002079 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2080 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002081 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002082 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002083 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002084 ObjCDeclSpec::DQ_PR_retain |
2085 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002086 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2087 "readwrite" :
2088 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2089 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00002090 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
2091 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00002092 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2093 "copy" : "retain";
2094
2095 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
2096 diag::err_objc_property_attr_mutually_exclusive :
2097 diag::warn_objc_property_attr_mutually_exclusive)
2098 << "readonly" << which;
2099 }
2100
2101 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00002102 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2103 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2104 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002105 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002106 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00002107 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2108 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2109 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2110 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002111 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002112 }
2113
2114 // Check for more than one of { assign, copy, retain }.
2115 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2116 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2117 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2118 << "assign" << "copy";
2119 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2120 }
2121 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2122 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2123 << "assign" << "retain";
2124 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2125 }
John McCallf85e1932011-06-15 23:02:42 +00002126 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2127 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2128 << "assign" << "strong";
2129 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2130 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002131 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002132 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2133 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2134 << "assign" << "weak";
2135 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2136 }
2137 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2138 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2139 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2140 << "unsafe_unretained" << "copy";
2141 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2142 }
2143 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2144 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2145 << "unsafe_unretained" << "retain";
2146 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2147 }
2148 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2149 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2150 << "unsafe_unretained" << "strong";
2151 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2152 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002153 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002154 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2155 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2156 << "unsafe_unretained" << "weak";
2157 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2158 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002159 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2160 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2161 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2162 << "copy" << "retain";
2163 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2164 }
John McCallf85e1932011-06-15 23:02:42 +00002165 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2166 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2167 << "copy" << "strong";
2168 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2169 }
2170 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2171 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2172 << "copy" << "weak";
2173 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2174 }
2175 }
2176 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2177 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2178 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2179 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002180 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002181 }
2182 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2183 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2184 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2185 << "strong" << "weak";
2186 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002187 }
2188
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002189 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2190 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2191 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2192 << "atomic" << "nonatomic";
2193 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2194 }
2195
Ted Kremenek9d64c152010-03-12 00:38:38 +00002196 // Warn if user supplied no assignment attribute, property is
2197 // readwrite, and this is an object type.
2198 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002199 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2200 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2201 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002202 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002203 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002204 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002205 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002206 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002207 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002208 bool isAnyClassTy =
2209 (PropertyTy->isObjCClassType() ||
2210 PropertyTy->isObjCQualifiedClassType());
2211 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2212 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002213 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002214 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002215 else if (propertyInPrimaryClass) {
2216 // Don't issue warning on property with no life time in class
2217 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002218 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002219 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002220 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002221
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002222 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002223 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002224 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002225 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002226 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002227
2228 // FIXME: Implement warning dependent on NSCopying being
2229 // implemented. See also:
2230 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2231 // (please trim this list while you are at it).
2232 }
2233
2234 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002235 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002236 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002237 && PropertyTy->isBlockPointerType())
2238 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian7c16d582012-06-27 20:52:46 +00002239 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002240 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2241 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2242 PropertyTy->isBlockPointerType())
2243 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002244
2245 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2246 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2247 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2248
Ted Kremenek9d64c152010-03-12 00:38:38 +00002249}