blob: 8d708607f6eb3fb9388ffdbf6bd9a794b2172078 [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
John McCall260611a2012-06-20 06:18:46 +0000935 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000936 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
937 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000938 // Note! I deliberately want it to fall thru so, we have a
939 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000940 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000941 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000942 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000943 << property->getDeclName() << Ivar->getDeclName()
944 << ClassDeclared->getDeclName();
945 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000946 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000947 // Note! I deliberately want it to fall thru so more errors are caught.
948 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000949 property->setPropertyIvarDecl(Ivar);
950
Ted Kremenek28685ab2010-03-12 00:46:40 +0000951 QualType IvarType = Context.getCanonicalType(Ivar->getType());
952
953 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000954 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +0000955 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000956 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +0000957 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000958 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000959 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000960 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000961 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000962 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
963 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000964 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000965 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000966 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000967 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000968 << property->getDeclName() << PropType
969 << Ivar->getDeclName() << IvarType;
970 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000971 // Note! I deliberately want it to fall thru so, we have a
972 // a property implementation and to avoid future warnings.
973 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000974 else {
975 // FIXME! Rules for properties are somewhat different that those
976 // for assignments. Use a new routine to consolidate all cases;
977 // specifically for property redeclarations as well as for ivars.
978 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
979 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
980 if (lhsType != rhsType &&
981 lhsType->isArithmeticType()) {
982 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
983 << property->getDeclName() << PropType
984 << Ivar->getDeclName() << IvarType;
985 Diag(Ivar->getLocation(), diag::note_ivar_decl);
986 // Fall thru - see previous comment
987 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000988 }
989 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000990 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000991 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000992 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000993 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000994 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000995 // Fall thru - see previous comment
996 }
John McCallf85e1932011-06-15 23:02:42 +0000997 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000998 if ((property->getType()->isObjCObjectPointerType() ||
999 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001000 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001001 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001002 << property->getDeclName() << Ivar->getDeclName();
1003 // Fall thru - see previous comment
1004 }
1005 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001006 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001007 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001008 } else if (PropertyIvar)
1009 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001010 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001011
Ted Kremenek28685ab2010-03-12 00:46:40 +00001012 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1013 ObjCPropertyImplDecl *PIDecl =
1014 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1015 property,
1016 (Synthesize ?
1017 ObjCPropertyImplDecl::Synthesize
1018 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001019 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001020
Fariborz Jahanian74414712012-05-15 18:12:51 +00001021 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001022 PIDecl->setInvalidDecl();
1023
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001024 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1025 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001026 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001027 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001028 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1029 // returned by the getter as it must conform to C++'s copy-return rules.
1030 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001031 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001032 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1033 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001034 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001035 VK_RValue, PropertyDiagLoc);
1036 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001037 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001038 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001039 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001040 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001041 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001042 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001043 getterMethod->getResultType(),
1044 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001045 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001046 Owned(IvarRefExpr));
1047 if (!Res.isInvalid()) {
1048 Expr *ResExpr = Res.takeAs<Expr>();
1049 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001050 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001051 PIDecl->setGetterCXXConstructor(ResExpr);
1052 }
1053 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001054 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1055 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1056 Diag(getterMethod->getLocation(),
1057 diag::warn_property_getter_owning_mismatch);
1058 Diag(property->getLocation(), diag::note_property_declare);
1059 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001060 }
1061 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1062 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001063 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1064 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001065 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001066 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001067 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1068 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001069 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001070 VK_RValue, PropertyDiagLoc);
1071 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001072 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001073 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001074 SelfExpr, true, true);
1075 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1076 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001077 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001078 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1079 VK_LValue, PropertyDiagLoc);
1080 MarkDeclRefReferenced(rhs);
1081 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001082 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001083 if (property->getPropertyAttributes() &
1084 ObjCPropertyDecl::OBJC_PR_atomic) {
1085 Expr *callExpr = Res.takeAs<Expr>();
1086 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001087 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1088 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001089 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001090 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001091 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001092 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001093 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001094 Diag(FuncDecl->getLocStart(),
1095 diag::note_callee_decl) << FuncDecl;
1096 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001097 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001098 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1099 }
1100 }
1101
Ted Kremenek28685ab2010-03-12 00:46:40 +00001102 if (IC) {
1103 if (Synthesize)
1104 if (ObjCPropertyImplDecl *PPIDecl =
1105 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1106 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1107 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1108 << PropertyIvar;
1109 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1110 }
1111
1112 if (ObjCPropertyImplDecl *PPIDecl
1113 = IC->FindPropertyImplDecl(PropertyId)) {
1114 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1115 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001116 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001117 }
1118 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001119 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001120 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001121 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001122 // Diagnose if an ivar was lazily synthesdized due to a previous
1123 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001124 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001125 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001126 ObjCIvarDecl *Ivar = 0;
1127 if (!Synthesize)
1128 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1129 else {
1130 if (PropertyIvar && PropertyIvar != PropertyId)
1131 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1132 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001133 // Issue diagnostics only if Ivar belongs to current class.
1134 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001135 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001136 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1137 << PropertyId;
1138 Ivar->setInvalidDecl();
1139 }
1140 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001141 } else {
1142 if (Synthesize)
1143 if (ObjCPropertyImplDecl *PPIDecl =
1144 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001145 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001146 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1147 << PropertyIvar;
1148 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1149 }
1150
1151 if (ObjCPropertyImplDecl *PPIDecl =
1152 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001153 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001154 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001155 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001156 }
1157 CatImplClass->addPropertyImplementation(PIDecl);
1158 }
1159
John McCalld226f652010-08-21 09:40:31 +00001160 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001161}
1162
1163//===----------------------------------------------------------------------===//
1164// Helper methods.
1165//===----------------------------------------------------------------------===//
1166
Ted Kremenek9d64c152010-03-12 00:38:38 +00001167/// DiagnosePropertyMismatch - Compares two properties for their
1168/// attributes and types and warns on a variety of inconsistencies.
1169///
1170void
1171Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1172 ObjCPropertyDecl *SuperProperty,
1173 const IdentifierInfo *inheritedName) {
1174 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1175 Property->getPropertyAttributes();
1176 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1177 SuperProperty->getPropertyAttributes();
1178 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1179 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1180 Diag(Property->getLocation(), diag::warn_readonly_property)
1181 << Property->getDeclName() << inheritedName;
1182 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1183 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1184 Diag(Property->getLocation(), diag::warn_property_attribute)
1185 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001186 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001187 unsigned CAttrRetain =
1188 (CAttr &
1189 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1190 unsigned SAttrRetain =
1191 (SAttr &
1192 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1193 bool CStrong = (CAttrRetain != 0);
1194 bool SStrong = (SAttrRetain != 0);
1195 if (CStrong != SStrong)
1196 Diag(Property->getLocation(), diag::warn_property_attribute)
1197 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1198 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001199
1200 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1201 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1202 Diag(Property->getLocation(), diag::warn_property_attribute)
1203 << Property->getDeclName() << "atomic" << inheritedName;
1204 if (Property->getSetterName() != SuperProperty->getSetterName())
1205 Diag(Property->getLocation(), diag::warn_property_attribute)
1206 << Property->getDeclName() << "setter" << inheritedName;
1207 if (Property->getGetterName() != SuperProperty->getGetterName())
1208 Diag(Property->getLocation(), diag::warn_property_attribute)
1209 << Property->getDeclName() << "getter" << inheritedName;
1210
1211 QualType LHSType =
1212 Context.getCanonicalType(SuperProperty->getType());
1213 QualType RHSType =
1214 Context.getCanonicalType(Property->getType());
1215
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001216 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001217 // Do cases not handled in above.
1218 // FIXME. For future support of covariant property types, revisit this.
1219 bool IncompatibleObjC = false;
1220 QualType ConvertedType;
1221 if (!isObjCPointerConversion(RHSType, LHSType,
1222 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001223 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001224 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1225 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001226 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1227 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001228 }
1229}
1230
1231bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1232 ObjCMethodDecl *GetterMethod,
1233 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001234 if (!GetterMethod)
1235 return false;
1236 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1237 QualType PropertyIvarType = property->getType().getNonReferenceType();
1238 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1239 if (!compat) {
1240 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1241 isa<ObjCObjectPointerType>(GetterType))
1242 compat =
1243 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001244 GetterType->getAs<ObjCObjectPointerType>(),
1245 PropertyIvarType->getAs<ObjCObjectPointerType>());
1246 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001247 != Compatible) {
1248 Diag(Loc, diag::error_property_accessor_type)
1249 << property->getDeclName() << PropertyIvarType
1250 << GetterMethod->getSelector() << GetterType;
1251 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1252 return true;
1253 } else {
1254 compat = true;
1255 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1256 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1257 if (lhsType != rhsType && lhsType->isArithmeticType())
1258 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001259 }
1260 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001261
1262 if (!compat) {
1263 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1264 << property->getDeclName()
1265 << GetterMethod->getSelector();
1266 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1267 return true;
1268 }
1269
Ted Kremenek9d64c152010-03-12 00:38:38 +00001270 return false;
1271}
1272
1273/// ComparePropertiesInBaseAndSuper - This routine compares property
1274/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001275/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001276///
1277void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1278 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1279 if (!SDecl)
1280 return;
1281 // FIXME: O(N^2)
1282 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1283 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001284 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001285 // Does property in super class has declaration in current class?
1286 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1287 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001288 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001289 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1290 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1291 SDecl->getIdentifier());
1292 }
1293 }
1294}
1295
1296/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1297/// of properties declared in a protocol and compares their attribute against
1298/// the same property declared in the class or category.
1299void
1300Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1301 ObjCProtocolDecl *PDecl) {
1302 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1303 if (!IDecl) {
1304 // Category
1305 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1306 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1307 if (!CatDecl->IsClassExtension())
1308 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1309 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001310 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001311 ObjCCategoryDecl::prop_iterator CP, CE;
1312 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001313 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001314 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001315 break;
1316 if (CP != CE)
1317 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001318 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001319 }
1320 return;
1321 }
1322 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1323 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001324 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001325 ObjCInterfaceDecl::prop_iterator CP, CE;
1326 // Is this property already in class's list of properties?
1327 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001328 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001329 break;
1330 if (CP != CE)
1331 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001332 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001333 }
1334}
1335
1336/// CompareProperties - This routine compares properties
1337/// declared in 'ClassOrProtocol' objects (which can be a class or an
1338/// inherited protocol with the list of properties for class/category 'CDecl'
1339///
John McCalld226f652010-08-21 09:40:31 +00001340void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1341 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001342 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1343
1344 if (!IDecl) {
1345 // Category
1346 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1347 assert (CatDecl && "CompareProperties");
1348 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1349 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1350 E = MDecl->protocol_end(); P != E; ++P)
1351 // Match properties of category with those of protocol (*P)
1352 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1353
1354 // Go thru the list of protocols for this category and recursively match
1355 // their properties with those in the category.
1356 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1357 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001358 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001359 } else {
1360 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1361 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1362 E = MD->protocol_end(); P != E; ++P)
1363 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1364 }
1365 return;
1366 }
1367
1368 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001369 for (ObjCInterfaceDecl::all_protocol_iterator
1370 P = MDecl->all_referenced_protocol_begin(),
1371 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001372 // Match properties of class IDecl with those of protocol (*P).
1373 MatchOneProtocolPropertiesInClass(IDecl, *P);
1374
1375 // Go thru the list of protocols for this class and recursively match
1376 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001377 for (ObjCInterfaceDecl::all_protocol_iterator
1378 P = IDecl->all_referenced_protocol_begin(),
1379 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001380 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001381 } else {
1382 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1383 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1384 E = MD->protocol_end(); P != E; ++P)
1385 MatchOneProtocolPropertiesInClass(IDecl, *P);
1386 }
1387}
1388
1389/// isPropertyReadonly - Return true if property is readonly, by searching
1390/// for the property in the class and in its categories and implementations
1391///
1392bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1393 ObjCInterfaceDecl *IDecl) {
1394 // by far the most common case.
1395 if (!PDecl->isReadOnly())
1396 return false;
1397 // Even if property is ready only, if interface has a user defined setter,
1398 // it is not considered read only.
1399 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1400 return false;
1401
1402 // Main class has the property as 'readonly'. Must search
1403 // through the category list to see if the property's
1404 // attribute has been over-ridden to 'readwrite'.
1405 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1406 Category; Category = Category->getNextClassCategory()) {
1407 // Even if property is ready only, if a category has a user defined setter,
1408 // it is not considered read only.
1409 if (Category->getInstanceMethod(PDecl->getSetterName()))
1410 return false;
1411 ObjCPropertyDecl *P =
1412 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1413 if (P && !P->isReadOnly())
1414 return false;
1415 }
1416
1417 // Also, check for definition of a setter method in the implementation if
1418 // all else failed.
1419 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1420 if (ObjCImplementationDecl *IMD =
1421 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1422 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1423 return false;
1424 } else if (ObjCCategoryImplDecl *CIMD =
1425 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1426 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1427 return false;
1428 }
1429 }
1430 // Lastly, look through the implementation (if one is in scope).
1431 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1432 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1433 return false;
1434 // If all fails, look at the super class.
1435 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1436 return isPropertyReadonly(PDecl, SIDecl);
1437 return true;
1438}
1439
1440/// CollectImmediateProperties - This routine collects all properties in
1441/// the class and its conforming protocols; but not those it its super class.
1442void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001443 ObjCContainerDecl::PropertyMap &PropMap,
1444 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001445 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1446 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1447 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001448 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001449 PropMap[Prop->getIdentifier()] = Prop;
1450 }
1451 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001452 for (ObjCInterfaceDecl::all_protocol_iterator
1453 PI = IDecl->all_referenced_protocol_begin(),
1454 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001455 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001456 }
1457 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1458 if (!CATDecl->IsClassExtension())
1459 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1460 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001461 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001462 PropMap[Prop->getIdentifier()] = Prop;
1463 }
1464 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001465 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001466 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001467 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001468 }
1469 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1470 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1471 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001472 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001473 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1474 // Exclude property for protocols which conform to class's super-class,
1475 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001476 if (!PropertyFromSuper ||
1477 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001478 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1479 if (!PropEntry)
1480 PropEntry = Prop;
1481 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001482 }
1483 // scan through protocol's protocols.
1484 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1485 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001486 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001487 }
1488}
1489
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001490/// CollectSuperClassPropertyImplementations - This routine collects list of
1491/// properties to be implemented in super class(s) and also coming from their
1492/// conforming protocols.
1493static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001494 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001495 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1496 while (SDecl) {
Anna Zaksb36ea372012-10-18 19:17:53 +00001497 SDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001498 SDecl = SDecl->getSuperClass();
1499 }
1500 }
1501}
1502
James Dennett699c9042012-06-15 07:13:21 +00001503/// \brief Default synthesizes all properties which must be synthesized
1504/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001505void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1506 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001507
Anna Zaksb36ea372012-10-18 19:17:53 +00001508 ObjCInterfaceDecl::PropertyMap PropMap;
1509 IDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001510 if (PropMap.empty())
1511 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001512 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001513 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1514
Anna Zaksb36ea372012-10-18 19:17:53 +00001515 for (ObjCInterfaceDecl::PropertyMap::iterator
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001516 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1517 ObjCPropertyDecl *Prop = P->second;
1518 // If property to be implemented in the super class, ignore.
1519 if (SuperPropMap[Prop->getIdentifier()])
1520 continue;
Anna Zaksb36ea372012-10-18 19:17:53 +00001521 // Is there a matching property synthesize/dynamic?
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001522 if (Prop->isInvalidDecl() ||
1523 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1524 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1525 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001526 // Property may have been synthesized by user.
1527 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1528 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001529 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1530 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1531 continue;
1532 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1533 continue;
1534 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001535 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1536 // We won't auto-synthesize properties declared in protocols.
1537 Diag(IMPDecl->getLocation(),
1538 diag::warn_auto_synthesizing_protocol_property);
1539 Diag(Prop->getLocation(), diag::note_property_declare);
1540 continue;
1541 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001542
1543 // We use invalid SourceLocations for the synthesized ivars since they
1544 // aren't really synthesized at a particular location; they just exist.
1545 // Saying that they are located at the @implementation isn't really going
1546 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001547 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1548 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1549 true,
1550 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001551 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001552 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001553 if (PIDecl) {
1554 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001555 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001556 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001557 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001558}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001559
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001560void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001561 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001562 return;
1563 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1564 if (!IC)
1565 return;
1566 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001567 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001568 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001569}
1570
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001571void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001572 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001573 const SelectorSet &InsMap) {
Anna Zaksb36ea372012-10-18 19:17:53 +00001574 ObjCContainerDecl::PropertyMap SuperPropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001575 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1576 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1577
Anna Zaksb36ea372012-10-18 19:17:53 +00001578 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001579 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001580 if (PropMap.empty())
1581 return;
1582
1583 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1584 for (ObjCImplDecl::propimpl_iterator
1585 I = IMPDecl->propimpl_begin(),
1586 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001587 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001588
Anna Zaksb36ea372012-10-18 19:17:53 +00001589 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001590 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1591 ObjCPropertyDecl *Prop = P->second;
1592 // Is there a matching propery synthesize/dynamic?
1593 if (Prop->isInvalidDecl() ||
1594 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001595 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001596 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001597 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001598 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001599 isa<ObjCCategoryDecl>(CDecl) ?
1600 diag::warn_setter_getter_impl_required_in_category :
1601 diag::warn_setter_getter_impl_required)
1602 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001603 Diag(Prop->getLocation(),
1604 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001605 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001606 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001607 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001608 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1609
Ted Kremenek9d64c152010-03-12 00:38:38 +00001610 }
1611
1612 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001613 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001614 isa<ObjCCategoryDecl>(CDecl) ?
1615 diag::warn_setter_getter_impl_required_in_category :
1616 diag::warn_setter_getter_impl_required)
1617 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001618 Diag(Prop->getLocation(),
1619 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001620 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001621 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001622 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001623 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001624 }
1625 }
1626}
1627
1628void
1629Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1630 ObjCContainerDecl* IDecl) {
1631 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001632 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001633 return;
1634 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1635 E = IDecl->prop_end();
1636 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001637 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001638 ObjCMethodDecl *GetterMethod = 0;
1639 ObjCMethodDecl *SetterMethod = 0;
1640 bool LookedUpGetterSetter = false;
1641
Ted Kremenek9d64c152010-03-12 00:38:38 +00001642 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001643 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001644
John McCall265941b2011-09-13 18:31:23 +00001645 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1646 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001647 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1648 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1649 LookedUpGetterSetter = true;
1650 if (GetterMethod) {
1651 Diag(GetterMethod->getLocation(),
1652 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001653 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001654 Diag(Property->getLocation(), diag::note_property_declare);
1655 }
1656 if (SetterMethod) {
1657 Diag(SetterMethod->getLocation(),
1658 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001659 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001660 Diag(Property->getLocation(), diag::note_property_declare);
1661 }
1662 }
1663
Ted Kremenek9d64c152010-03-12 00:38:38 +00001664 // We only care about readwrite atomic property.
1665 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1666 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1667 continue;
1668 if (const ObjCPropertyImplDecl *PIDecl
1669 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1670 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1671 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001672 if (!LookedUpGetterSetter) {
1673 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1674 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1675 LookedUpGetterSetter = true;
1676 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001677 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1678 SourceLocation MethodLoc =
1679 (GetterMethod ? GetterMethod->getLocation()
1680 : SetterMethod->getLocation());
1681 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001682 << Property->getIdentifier() << (GetterMethod != 0)
1683 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001684 // fixit stuff.
1685 if (!AttributesAsWritten) {
1686 if (Property->getLParenLoc().isValid()) {
1687 // @property () ... case.
1688 SourceRange PropSourceRange(Property->getAtLoc(),
1689 Property->getLParenLoc());
1690 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1691 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1692 }
1693 else {
1694 //@property id etc.
1695 SourceLocation endLoc =
1696 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1697 endLoc = endLoc.getLocWithOffset(-1);
1698 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1699 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1700 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1701 }
1702 }
1703 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1704 // @property () ... case.
1705 SourceLocation endLoc = Property->getLParenLoc();
1706 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1707 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1708 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1709 }
1710 else
1711 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001712 Diag(Property->getLocation(), diag::note_property_declare);
1713 }
1714 }
1715 }
1716}
1717
John McCallf85e1932011-06-15 23:02:42 +00001718void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001719 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001720 return;
1721
1722 for (ObjCImplementationDecl::propimpl_iterator
1723 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001724 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001725 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1726 continue;
1727
1728 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001729 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1730 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001731 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1732 if (!method)
1733 continue;
1734 ObjCMethodFamily family = method->getMethodFamily();
1735 if (family == OMF_alloc || family == OMF_copy ||
1736 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001737 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001738 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1739 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001740 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001741 Diag(PD->getLocation(), diag::note_property_declare);
1742 }
1743 }
1744 }
1745}
1746
John McCall5de74d12010-11-10 07:01:40 +00001747/// AddPropertyAttrs - Propagates attributes from a property to the
1748/// implicitly-declared getter or setter for that property.
1749static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1750 ObjCPropertyDecl *Property) {
1751 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001752 for (Decl::attr_iterator A = Property->attr_begin(),
1753 AEnd = Property->attr_end();
1754 A != AEnd; ++A) {
1755 if (isa<DeprecatedAttr>(*A) ||
1756 isa<UnavailableAttr>(*A) ||
1757 isa<AvailabilityAttr>(*A))
1758 PropertyMethod->addAttr((*A)->clone(S.Context));
1759 }
John McCall5de74d12010-11-10 07:01:40 +00001760}
1761
Ted Kremenek9d64c152010-03-12 00:38:38 +00001762/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1763/// have the property type and issue diagnostics if they don't.
1764/// Also synthesize a getter/setter method if none exist (and update the
1765/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1766/// methods is the "right" thing to do.
1767void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001768 ObjCContainerDecl *CD,
1769 ObjCPropertyDecl *redeclaredProperty,
1770 ObjCContainerDecl *lexicalDC) {
1771
Ted Kremenek9d64c152010-03-12 00:38:38 +00001772 ObjCMethodDecl *GetterMethod, *SetterMethod;
1773
1774 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1775 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1776 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1777 property->getLocation());
1778
1779 if (SetterMethod) {
1780 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1781 property->getPropertyAttributes();
1782 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1783 Context.getCanonicalType(SetterMethod->getResultType()) !=
1784 Context.VoidTy)
1785 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1786 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001787 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001788 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1789 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001790 Diag(property->getLocation(),
1791 diag::warn_accessor_property_type_mismatch)
1792 << property->getDeclName()
1793 << SetterMethod->getSelector();
1794 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1795 }
1796 }
1797
1798 // Synthesize getter/setter methods if none exist.
1799 // Find the default getter and if one not found, add one.
1800 // FIXME: The synthesized property we set here is misleading. We almost always
1801 // synthesize these methods unless the user explicitly provided prototypes
1802 // (which is odd, but allowed). Sema should be typechecking that the
1803 // declarations jive in that situation (which it is not currently).
1804 if (!GetterMethod) {
1805 // No instance method of same name as property getter name was found.
1806 // Declare a getter method and add it to the list of methods
1807 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001808 SourceLocation Loc = redeclaredProperty ?
1809 redeclaredProperty->getLocation() :
1810 property->getLocation();
1811
1812 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1813 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001814 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001815 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001816 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001817 (property->getPropertyImplementation() ==
1818 ObjCPropertyDecl::Optional) ?
1819 ObjCMethodDecl::Optional :
1820 ObjCMethodDecl::Required);
1821 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001822
1823 AddPropertyAttrs(*this, GetterMethod, property);
1824
Ted Kremenek23173d72010-05-18 21:09:07 +00001825 // FIXME: Eventually this shouldn't be needed, as the lexical context
1826 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001827 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001828 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001829 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1830 GetterMethod->addAttr(
1831 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001832 } else
1833 // A user declared getter will be synthesize when @synthesize of
1834 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001835 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001836 property->setGetterMethodDecl(GetterMethod);
1837
1838 // Skip setter if property is read-only.
1839 if (!property->isReadOnly()) {
1840 // Find the default setter and if one not found, add one.
1841 if (!SetterMethod) {
1842 // No instance method of same name as property setter name was found.
1843 // Declare a setter method and add it to the list of methods
1844 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001845 SourceLocation Loc = redeclaredProperty ?
1846 redeclaredProperty->getLocation() :
1847 property->getLocation();
1848
1849 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001850 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001851 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001852 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001853 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001854 /*isImplicitlyDeclared=*/true,
1855 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001856 (property->getPropertyImplementation() ==
1857 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001858 ObjCMethodDecl::Optional :
1859 ObjCMethodDecl::Required);
1860
Ted Kremenek9d64c152010-03-12 00:38:38 +00001861 // Invent the arguments for the setter. We don't bother making a
1862 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001863 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1864 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001865 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001866 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001867 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001868 SC_None,
1869 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001870 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001871 SetterMethod->setMethodParams(Context, Argument,
1872 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001873
1874 AddPropertyAttrs(*this, SetterMethod, property);
1875
Ted Kremenek9d64c152010-03-12 00:38:38 +00001876 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001877 // FIXME: Eventually this shouldn't be needed, as the lexical context
1878 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001879 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001880 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001881 } else
1882 // A user declared setter will be synthesize when @synthesize of
1883 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001884 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001885 property->setSetterMethodDecl(SetterMethod);
1886 }
1887 // Add any synthesized methods to the global pool. This allows us to
1888 // handle the following, which is supported by GCC (and part of the design).
1889 //
1890 // @interface Foo
1891 // @property double bar;
1892 // @end
1893 //
1894 // void thisIsUnfortunate() {
1895 // id foo;
1896 // double bar = [foo bar];
1897 // }
1898 //
1899 if (GetterMethod)
1900 AddInstanceMethodToGlobalPool(GetterMethod);
1901 if (SetterMethod)
1902 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001903
1904 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1905 if (!CurrentClass) {
1906 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1907 CurrentClass = Cat->getClassInterface();
1908 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1909 CurrentClass = Impl->getClassInterface();
1910 }
1911 if (GetterMethod)
1912 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1913 if (SetterMethod)
1914 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001915}
1916
John McCalld226f652010-08-21 09:40:31 +00001917void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001918 SourceLocation Loc,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001919 unsigned &Attributes,
1920 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001921 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001922 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001923 return;
1924
1925 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001926 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001927
David Blaikie4e4d0842012-03-11 07:00:24 +00001928 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001929 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1930 PropertyTy->isObjCRetainableType()) {
1931 // 'readonly' property with no obvious lifetime.
1932 // its life time will be determined by its backing ivar.
1933 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1934 ObjCDeclSpec::DQ_PR_copy |
1935 ObjCDeclSpec::DQ_PR_retain |
1936 ObjCDeclSpec::DQ_PR_strong |
1937 ObjCDeclSpec::DQ_PR_weak |
1938 ObjCDeclSpec::DQ_PR_assign);
1939 if ((Attributes & rel) == 0)
1940 return;
1941 }
1942
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001943 if (propertyInPrimaryClass) {
1944 // we postpone most property diagnosis until class's implementation
1945 // because, its readonly attribute may be overridden in its class
1946 // extensions making other attributes, which make no sense, to make sense.
1947 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1948 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
1949 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1950 << "readonly" << "readwrite";
1951 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001952 // readonly and readwrite/assign/retain/copy conflict.
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001953 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1954 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001955 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001956 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001957 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001958 ObjCDeclSpec::DQ_PR_retain |
1959 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001960 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1961 "readwrite" :
1962 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1963 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001964 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1965 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001966 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1967 "copy" : "retain";
1968
1969 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1970 diag::err_objc_property_attr_mutually_exclusive :
1971 diag::warn_objc_property_attr_mutually_exclusive)
1972 << "readonly" << which;
1973 }
1974
1975 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001976 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1977 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1978 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001979 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001980 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001981 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1982 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1983 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1984 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00001985 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001986 }
1987
1988 // Check for more than one of { assign, copy, retain }.
1989 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1990 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1991 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1992 << "assign" << "copy";
1993 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1994 }
1995 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1996 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1997 << "assign" << "retain";
1998 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1999 }
John McCallf85e1932011-06-15 23:02:42 +00002000 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2001 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2002 << "assign" << "strong";
2003 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2004 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002005 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002006 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2007 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2008 << "assign" << "weak";
2009 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2010 }
2011 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2012 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2013 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2014 << "unsafe_unretained" << "copy";
2015 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2016 }
2017 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2018 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2019 << "unsafe_unretained" << "retain";
2020 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2021 }
2022 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2023 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2024 << "unsafe_unretained" << "strong";
2025 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2026 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002027 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002028 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2029 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2030 << "unsafe_unretained" << "weak";
2031 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2032 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002033 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2034 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2035 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2036 << "copy" << "retain";
2037 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2038 }
John McCallf85e1932011-06-15 23:02:42 +00002039 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2040 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2041 << "copy" << "strong";
2042 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2043 }
2044 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2045 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2046 << "copy" << "weak";
2047 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2048 }
2049 }
2050 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2051 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2052 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2053 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002054 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002055 }
2056 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2057 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2058 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2059 << "strong" << "weak";
2060 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002061 }
2062
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002063 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2064 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2065 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2066 << "atomic" << "nonatomic";
2067 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2068 }
2069
Ted Kremenek9d64c152010-03-12 00:38:38 +00002070 // Warn if user supplied no assignment attribute, property is
2071 // readwrite, and this is an object type.
2072 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002073 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2074 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2075 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002076 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002077 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002078 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002079 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002080 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002081 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002082 bool isAnyClassTy =
2083 (PropertyTy->isObjCClassType() ||
2084 PropertyTy->isObjCQualifiedClassType());
2085 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2086 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002087 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002088 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002089 else if (propertyInPrimaryClass) {
2090 // Don't issue warning on property with no life time in class
2091 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002092 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002093 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002094 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002095
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002096 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002097 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002098 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002099 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002100 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002101
2102 // FIXME: Implement warning dependent on NSCopying being
2103 // implemented. See also:
2104 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2105 // (please trim this list while you are at it).
2106 }
2107
2108 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00002109 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002110 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002111 && PropertyTy->isBlockPointerType())
2112 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian7c16d582012-06-27 20:52:46 +00002113 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002114 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2115 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2116 PropertyTy->isBlockPointerType())
2117 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002118
2119 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2120 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2121 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2122
Ted Kremenek9d64c152010-03-12 00:38:38 +00002123}