blob: 170bbde370c9f6b3a90e861c68f046c89b4438d8 [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"
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Sema/Initialization.h"
John McCall50df6ae2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.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) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000124 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000125 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
126 QualType T = TSI->getType();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000127 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000128
Bill Wendlingad017fa2012-12-20 19:22:21 +0000129 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000130 // default is readwrite!
Bill Wendlingad017fa2012-12-20 19:22:21 +0000131 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenek28685ab2010-03-12 00:46:40 +0000132 // property is defaulted to 'assign' if it is readwrite and is
133 // not retain or copy
Bill Wendlingad017fa2012-12-20 19:22:21 +0000134 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000135 (isReadWrite &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000136 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
137 !(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,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000149 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) {
Bill Wendlingad017fa2012-12-20 19:22:21 +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,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000165 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000166 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.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000172 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000173 (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
Bill Wendlingad017fa2012-12-20 19:22:21 +0000184makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000185 unsigned attributesAsWritten = 0;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000186 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000187 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000188 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000189 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000190 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000191 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000192 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000193 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000194 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000195 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000196 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000197 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000198 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000199 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000200 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000201 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000202 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000203 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000204 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000205 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000206 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000207 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000208 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000209 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,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000262 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));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000295 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000296 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000297 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000298 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,
Bill Wendlingad017fa2012-12-20 19:22:21 +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());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000362 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000363 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();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000394 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000395 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000396 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000397 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000398 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000399 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 =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000407 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000408 (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,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000435 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 &&
Bill Wendlingad017fa2012-12-20 19:22:21 +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
Bill Wendlingad017fa2012-12-20 19:22:21 +0000490 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000491 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
492
Bill Wendlingad017fa2012-12-20 19:22:21 +0000493 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000494 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
495
Bill Wendlingad017fa2012-12-20 19:22:21 +0000496 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000497 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
498
499 if (isReadWrite)
500 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
501
Bill Wendlingad017fa2012-12-20 19:22:21 +0000502 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000503 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
504
Bill Wendlingad017fa2012-12-20 19:22:21 +0000505 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000506 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
507
Bill Wendlingad017fa2012-12-20 19:22:21 +0000508 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000509 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
510
Bill Wendlingad017fa2012-12-20 19:22:21 +0000511 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000512 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
513
Bill Wendlingad017fa2012-12-20 19:22:21 +0000514 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000515 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.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000521 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000522 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'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000527 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000528 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:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000580 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000581 << 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:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000587 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000588 << 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:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000593 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000594 << 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);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000609 if (propertyImplLoc.isValid())
610 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000611}
612
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000613/// setImpliedPropertyAttributeForReadOnlyProperty -
614/// This routine evaludates life-time attributes for a 'readonly'
615/// property with no known lifetime of its own, using backing
616/// 'ivar's attribute, if any. If no backing 'ivar', property's
617/// life-time is assumed 'strong'.
618static void setImpliedPropertyAttributeForReadOnlyProperty(
619 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
620 Qualifiers::ObjCLifetime propertyLifetime =
621 getImpliedARCOwnership(property->getPropertyAttributes(),
622 property->getType());
623 if (propertyLifetime != Qualifiers::OCL_None)
624 return;
625
626 if (!ivar) {
627 // if no backing ivar, make property 'strong'.
628 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
629 return;
630 }
631 // property assumes owenership of backing ivar.
632 QualType ivarType = ivar->getType();
633 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
634 if (ivarLifetime == Qualifiers::OCL_Strong)
635 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
636 else if (ivarLifetime == Qualifiers::OCL_Weak)
637 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
638 return;
639}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000640
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000641/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
642/// attribute declared in primary class and attributes overridden in any of its
643/// class extensions.
644static void
645DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
646 ObjCPropertyDecl *property) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000647 unsigned Attributes = property->getPropertyAttributesAsWritten();
648 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000649 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
650 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000651 ObjCPropertyDecl *ClassExtProperty = 0;
652 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
653 E = CDecl->prop_end(); P != E; ++P) {
654 if ((*P)->getIdentifier() == property->getIdentifier()) {
655 ClassExtProperty = *P;
656 break;
657 }
658 }
659 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000660 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000661 unsigned classExtPropertyAttr =
662 ClassExtProperty->getPropertyAttributesAsWritten();
663 // We are issuing the warning that we postponed because class extensions
664 // can override readonly->readwrite and 'setter' attributes originally
665 // placed on class's property declaration now make sense in the overridden
666 // property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000667 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000668 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000669 (classExtPropertyAttr &
670 (ObjCDeclSpec::DQ_PR_readwrite|
671 ObjCDeclSpec::DQ_PR_assign |
672 ObjCDeclSpec::DQ_PR_unsafe_unretained |
673 ObjCDeclSpec::DQ_PR_copy |
674 ObjCDeclSpec::DQ_PR_retain |
675 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000676 continue;
677 warn = true;
678 break;
679 }
680 }
681 }
682 if (warn) {
683 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
684 ObjCDeclSpec::DQ_PR_unsafe_unretained |
685 ObjCDeclSpec::DQ_PR_copy |
686 ObjCDeclSpec::DQ_PR_retain |
687 ObjCDeclSpec::DQ_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000688 if (Attributes & setterAttrs) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000689 const char * which =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000690 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000691 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000692 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000693 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000694 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000695 "copy" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000696 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000697 "retain" : "strong";
698
699 S.Diag(property->getLocation(),
700 diag::warn_objc_property_attr_mutually_exclusive)
701 << "readonly" << which;
702 }
703 }
704
705
706}
707
Ted Kremenek28685ab2010-03-12 00:46:40 +0000708/// ActOnPropertyImplDecl - This routine performs semantic checks and
709/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000710/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000711///
John McCalld226f652010-08-21 09:40:31 +0000712Decl *Sema::ActOnPropertyImplDecl(Scope *S,
713 SourceLocation AtLoc,
714 SourceLocation PropertyLoc,
715 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000716 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000717 IdentifierInfo *PropertyIvar,
718 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000719 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000720 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000721 // Make sure we have a context for the property implementation declaration.
722 if (!ClassImpDecl) {
723 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000724 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000725 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000726 if (PropertyIvarLoc.isInvalid())
727 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000728 SourceLocation PropertyDiagLoc = PropertyLoc;
729 if (PropertyDiagLoc.isInvalid())
730 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000731 ObjCPropertyDecl *property = 0;
732 ObjCInterfaceDecl* IDecl = 0;
733 // Find the class or category class where this property must have
734 // a declaration.
735 ObjCImplementationDecl *IC = 0;
736 ObjCCategoryImplDecl* CatImplClass = 0;
737 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
738 IDecl = IC->getClassInterface();
739 // We always synthesize an interface for an implementation
740 // without an interface decl. So, IDecl is always non-zero.
741 assert(IDecl &&
742 "ActOnPropertyImplDecl - @implementation without @interface");
743
744 // Look for this property declaration in the @implementation's @interface
745 property = IDecl->FindPropertyDeclaration(PropertyId);
746 if (!property) {
747 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000748 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000749 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000750 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000751 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
752 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000753 if (AtLoc.isValid())
754 Diag(AtLoc, diag::warn_implicit_atomic_property);
755 else
756 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
757 Diag(property->getLocation(), diag::note_property_declare);
758 }
759
Ted Kremenek28685ab2010-03-12 00:46:40 +0000760 if (const ObjCCategoryDecl *CD =
761 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
762 if (!CD->IsClassExtension()) {
763 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
764 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000765 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000766 }
767 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000768
769 if (Synthesize&&
770 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
771 property->hasAttr<IBOutletAttr>() &&
772 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000773 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
774 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000775 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000776 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000777 property->getLParenLoc(), readonlyLoc)) {
778 SourceLocation endLoc =
779 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
780 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
781 Diag(property->getLocation(),
782 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
783 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
784 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000785 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000786
787 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000788
Ted Kremenek28685ab2010-03-12 00:46:40 +0000789 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
790 if (Synthesize) {
791 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000792 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000793 }
794 IDecl = CatImplClass->getClassInterface();
795 if (!IDecl) {
796 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000797 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000798 }
799 ObjCCategoryDecl *Category =
800 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
801
802 // If category for this implementation not found, it is an error which
803 // has already been reported eralier.
804 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000805 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000806 // Look for this property declaration in @implementation's category
807 property = Category->FindPropertyDeclaration(PropertyId);
808 if (!property) {
809 Diag(PropertyLoc, diag::error_bad_category_property_decl)
810 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000811 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000812 }
813 } else {
814 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000815 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000816 }
817 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000818 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000819 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000820 // Check that we have a valid, previously declared ivar for @synthesize
821 if (Synthesize) {
822 // @synthesize
823 if (!PropertyIvar)
824 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000825 // Check that this is a previously declared 'ivar' in 'IDecl' interface
826 ObjCInterfaceDecl *ClassDeclared;
827 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
828 QualType PropType = property->getType();
829 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000830
831 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000832 diag::err_incomplete_synthesized_property,
833 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000834 Diag(property->getLocation(), diag::note_property_declare);
835 CompleteTypeErr = true;
836 }
837
David Blaikie4e4d0842012-03-11 07:00:24 +0000838 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000839 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000840 ObjCPropertyDecl::OBJC_PR_readonly) &&
841 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000842 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
843 }
844
John McCallf85e1932011-06-15 23:02:42 +0000845 ObjCPropertyDecl::PropertyAttributeKind kind
846 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000847
848 // Add GC __weak to the ivar type if the property is weak.
849 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000850 getLangOpts().getGC() != LangOptions::NonGC) {
851 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000852 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000853 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000854 Diag(property->getLocation(), diag::note_property_declare);
855 } else {
856 PropertyIvarType =
857 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000858 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000859 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000860 if (AtLoc.isInvalid()) {
861 // Check when default synthesizing a property that there is
862 // an ivar matching property name and issue warning; since this
863 // is the most common case of not using an ivar used for backing
864 // property in non-default synthesis case.
865 ObjCInterfaceDecl *ClassDeclared=0;
866 ObjCIvarDecl *originalIvar =
867 IDecl->lookupInstanceVariable(property->getIdentifier(),
868 ClassDeclared);
869 if (originalIvar) {
870 Diag(PropertyDiagLoc,
871 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000872 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000873 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000874 Diag(property->getLocation(), diag::note_property_declare);
875 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000876 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000877 }
878
879 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000880 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000881 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000882 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000883 !PropertyIvarType.getObjCLifetime() &&
884 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000885
John McCall265941b2011-09-13 18:31:23 +0000886 // It's an error if we have to do this and the user didn't
887 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000888 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000889 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000890 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000891 diag::err_arc_objc_property_default_assign_on_object);
892 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000893 } else {
894 Qualifiers::ObjCLifetime lifetime =
895 getImpliedARCOwnership(kind, PropertyIvarType);
896 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000897 if (lifetime == Qualifiers::OCL_Weak) {
898 bool err = false;
899 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000900 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
901 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
902 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000903 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000904 Diag(property->getLocation(), diag::note_property_declare);
905 err = true;
906 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000907 }
John McCall0a7dd782012-08-21 02:47:43 +0000908 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000909 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000910 Diag(property->getLocation(), diag::note_property_declare);
911 }
John McCallf85e1932011-06-15 23:02:42 +0000912 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000913
John McCallf85e1932011-06-15 23:02:42 +0000914 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000915 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000916 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
917 }
John McCallf85e1932011-06-15 23:02:42 +0000918 }
919
920 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000921 !getLangOpts().ObjCAutoRefCount &&
922 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000923 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000924 Diag(property->getLocation(), diag::note_property_declare);
925 }
926
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000927 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000928 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000929 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000930 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000931 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000932 if (CompleteTypeErr)
933 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000934 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000935 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000936
John McCall260611a2012-06-20 06:18:46 +0000937 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000938 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
939 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000940 // Note! I deliberately want it to fall thru so, we have a
941 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000942 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000943 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000944 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000945 << property->getDeclName() << Ivar->getDeclName()
946 << ClassDeclared->getDeclName();
947 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000948 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000949 // Note! I deliberately want it to fall thru so more errors are caught.
950 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000951 property->setPropertyIvarDecl(Ivar);
952
Ted Kremenek28685ab2010-03-12 00:46:40 +0000953 QualType IvarType = Context.getCanonicalType(Ivar->getType());
954
955 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000956 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +0000957 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000958 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +0000959 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000960 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000961 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000962 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000963 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000964 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
965 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000966 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000967 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000968 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000969 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000970 << property->getDeclName() << PropType
971 << Ivar->getDeclName() << IvarType;
972 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000973 // Note! I deliberately want it to fall thru so, we have a
974 // a property implementation and to avoid future warnings.
975 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000976 else {
977 // FIXME! Rules for properties are somewhat different that those
978 // for assignments. Use a new routine to consolidate all cases;
979 // specifically for property redeclarations as well as for ivars.
980 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
981 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
982 if (lhsType != rhsType &&
983 lhsType->isArithmeticType()) {
984 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
985 << property->getDeclName() << PropType
986 << Ivar->getDeclName() << IvarType;
987 Diag(Ivar->getLocation(), diag::note_ivar_decl);
988 // Fall thru - see previous comment
989 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000990 }
991 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000992 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000993 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000994 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000995 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000996 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000997 // Fall thru - see previous comment
998 }
John McCallf85e1932011-06-15 23:02:42 +0000999 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001000 if ((property->getType()->isObjCObjectPointerType() ||
1001 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001002 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001003 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001004 << property->getDeclName() << Ivar->getDeclName();
1005 // Fall thru - see previous comment
1006 }
1007 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001008 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001009 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001010 } else if (PropertyIvar)
1011 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001012 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001013
Ted Kremenek28685ab2010-03-12 00:46:40 +00001014 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1015 ObjCPropertyImplDecl *PIDecl =
1016 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1017 property,
1018 (Synthesize ?
1019 ObjCPropertyImplDecl::Synthesize
1020 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001021 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001022
Fariborz Jahanian74414712012-05-15 18:12:51 +00001023 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001024 PIDecl->setInvalidDecl();
1025
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001026 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1027 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001028 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001029 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001030 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1031 // returned by the getter as it must conform to C++'s copy-return rules.
1032 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001033 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001034 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1035 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001036 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001037 VK_RValue, PropertyDiagLoc);
1038 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001039 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001040 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001041 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001042 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001043 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001044 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001045 getterMethod->getResultType(),
1046 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001047 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001048 Owned(IvarRefExpr));
1049 if (!Res.isInvalid()) {
1050 Expr *ResExpr = Res.takeAs<Expr>();
1051 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001052 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001053 PIDecl->setGetterCXXConstructor(ResExpr);
1054 }
1055 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001056 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1057 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1058 Diag(getterMethod->getLocation(),
1059 diag::warn_property_getter_owning_mismatch);
1060 Diag(property->getLocation(), diag::note_property_declare);
1061 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001062 }
1063 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1064 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001065 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1066 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001067 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001068 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001069 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1070 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001071 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001072 VK_RValue, PropertyDiagLoc);
1073 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001074 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001075 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001076 SelfExpr, true, true);
1077 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1078 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001079 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001080 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1081 VK_LValue, PropertyDiagLoc);
1082 MarkDeclRefReferenced(rhs);
1083 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001084 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001085 if (property->getPropertyAttributes() &
1086 ObjCPropertyDecl::OBJC_PR_atomic) {
1087 Expr *callExpr = Res.takeAs<Expr>();
1088 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001089 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1090 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001091 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001092 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001093 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001094 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001095 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001096 Diag(FuncDecl->getLocStart(),
1097 diag::note_callee_decl) << FuncDecl;
1098 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001099 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001100 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1101 }
1102 }
1103
Ted Kremenek28685ab2010-03-12 00:46:40 +00001104 if (IC) {
1105 if (Synthesize)
1106 if (ObjCPropertyImplDecl *PPIDecl =
1107 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1108 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1109 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1110 << PropertyIvar;
1111 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1112 }
1113
1114 if (ObjCPropertyImplDecl *PPIDecl
1115 = IC->FindPropertyImplDecl(PropertyId)) {
1116 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1117 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001118 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001119 }
1120 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001121 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001122 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001123 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001124 // Diagnose if an ivar was lazily synthesdized due to a previous
1125 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001126 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001127 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001128 ObjCIvarDecl *Ivar = 0;
1129 if (!Synthesize)
1130 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1131 else {
1132 if (PropertyIvar && PropertyIvar != PropertyId)
1133 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1134 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001135 // Issue diagnostics only if Ivar belongs to current class.
1136 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001137 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001138 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1139 << PropertyId;
1140 Ivar->setInvalidDecl();
1141 }
1142 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001143 } else {
1144 if (Synthesize)
1145 if (ObjCPropertyImplDecl *PPIDecl =
1146 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001147 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001148 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1149 << PropertyIvar;
1150 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1151 }
1152
1153 if (ObjCPropertyImplDecl *PPIDecl =
1154 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001155 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001156 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001157 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001158 }
1159 CatImplClass->addPropertyImplementation(PIDecl);
1160 }
1161
John McCalld226f652010-08-21 09:40:31 +00001162 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001163}
1164
1165//===----------------------------------------------------------------------===//
1166// Helper methods.
1167//===----------------------------------------------------------------------===//
1168
Ted Kremenek9d64c152010-03-12 00:38:38 +00001169/// DiagnosePropertyMismatch - Compares two properties for their
1170/// attributes and types and warns on a variety of inconsistencies.
1171///
1172void
1173Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1174 ObjCPropertyDecl *SuperProperty,
1175 const IdentifierInfo *inheritedName) {
1176 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1177 Property->getPropertyAttributes();
1178 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1179 SuperProperty->getPropertyAttributes();
1180 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1181 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1182 Diag(Property->getLocation(), diag::warn_readonly_property)
1183 << Property->getDeclName() << inheritedName;
1184 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1185 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1186 Diag(Property->getLocation(), diag::warn_property_attribute)
1187 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001188 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001189 unsigned CAttrRetain =
1190 (CAttr &
1191 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1192 unsigned SAttrRetain =
1193 (SAttr &
1194 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1195 bool CStrong = (CAttrRetain != 0);
1196 bool SStrong = (SAttrRetain != 0);
1197 if (CStrong != SStrong)
1198 Diag(Property->getLocation(), diag::warn_property_attribute)
1199 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1200 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001201
1202 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1203 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1204 Diag(Property->getLocation(), diag::warn_property_attribute)
1205 << Property->getDeclName() << "atomic" << inheritedName;
1206 if (Property->getSetterName() != SuperProperty->getSetterName())
1207 Diag(Property->getLocation(), diag::warn_property_attribute)
1208 << Property->getDeclName() << "setter" << inheritedName;
1209 if (Property->getGetterName() != SuperProperty->getGetterName())
1210 Diag(Property->getLocation(), diag::warn_property_attribute)
1211 << Property->getDeclName() << "getter" << inheritedName;
1212
1213 QualType LHSType =
1214 Context.getCanonicalType(SuperProperty->getType());
1215 QualType RHSType =
1216 Context.getCanonicalType(Property->getType());
1217
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001218 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001219 // Do cases not handled in above.
1220 // FIXME. For future support of covariant property types, revisit this.
1221 bool IncompatibleObjC = false;
1222 QualType ConvertedType;
1223 if (!isObjCPointerConversion(RHSType, LHSType,
1224 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001225 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001226 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1227 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001228 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1229 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001230 }
1231}
1232
1233bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1234 ObjCMethodDecl *GetterMethod,
1235 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001236 if (!GetterMethod)
1237 return false;
1238 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1239 QualType PropertyIvarType = property->getType().getNonReferenceType();
1240 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1241 if (!compat) {
1242 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1243 isa<ObjCObjectPointerType>(GetterType))
1244 compat =
1245 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001246 GetterType->getAs<ObjCObjectPointerType>(),
1247 PropertyIvarType->getAs<ObjCObjectPointerType>());
1248 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001249 != Compatible) {
1250 Diag(Loc, diag::error_property_accessor_type)
1251 << property->getDeclName() << PropertyIvarType
1252 << GetterMethod->getSelector() << GetterType;
1253 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1254 return true;
1255 } else {
1256 compat = true;
1257 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1258 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1259 if (lhsType != rhsType && lhsType->isArithmeticType())
1260 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001261 }
1262 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001263
1264 if (!compat) {
1265 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1266 << property->getDeclName()
1267 << GetterMethod->getSelector();
1268 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1269 return true;
1270 }
1271
Ted Kremenek9d64c152010-03-12 00:38:38 +00001272 return false;
1273}
1274
1275/// ComparePropertiesInBaseAndSuper - This routine compares property
1276/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001277/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001278///
1279void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1280 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1281 if (!SDecl)
1282 return;
1283 // FIXME: O(N^2)
1284 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1285 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001286 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001287 // Does property in super class has declaration in current class?
1288 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1289 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001290 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001291 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1292 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1293 SDecl->getIdentifier());
1294 }
1295 }
1296}
1297
1298/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1299/// of properties declared in a protocol and compares their attribute against
1300/// the same property declared in the class or category.
1301void
1302Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1303 ObjCProtocolDecl *PDecl) {
1304 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1305 if (!IDecl) {
1306 // Category
1307 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1308 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1309 if (!CatDecl->IsClassExtension())
1310 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1311 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001312 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001313 ObjCCategoryDecl::prop_iterator CP, CE;
1314 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001315 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001316 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001317 break;
1318 if (CP != CE)
1319 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001320 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001321 }
1322 return;
1323 }
1324 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1325 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001326 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001327 ObjCInterfaceDecl::prop_iterator CP, CE;
1328 // Is this property already in class's list of properties?
1329 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001330 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001331 break;
1332 if (CP != CE)
1333 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001334 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001335 }
1336}
1337
1338/// CompareProperties - This routine compares properties
1339/// declared in 'ClassOrProtocol' objects (which can be a class or an
1340/// inherited protocol with the list of properties for class/category 'CDecl'
1341///
John McCalld226f652010-08-21 09:40:31 +00001342void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1343 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001344 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1345
1346 if (!IDecl) {
1347 // Category
1348 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1349 assert (CatDecl && "CompareProperties");
1350 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1351 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1352 E = MDecl->protocol_end(); P != E; ++P)
1353 // Match properties of category with those of protocol (*P)
1354 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1355
1356 // Go thru the list of protocols for this category and recursively match
1357 // their properties with those in the category.
1358 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1359 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001360 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001361 } else {
1362 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1363 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1364 E = MD->protocol_end(); P != E; ++P)
1365 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1366 }
1367 return;
1368 }
1369
1370 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001371 for (ObjCInterfaceDecl::all_protocol_iterator
1372 P = MDecl->all_referenced_protocol_begin(),
1373 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001374 // Match properties of class IDecl with those of protocol (*P).
1375 MatchOneProtocolPropertiesInClass(IDecl, *P);
1376
1377 // Go thru the list of protocols for this class and recursively match
1378 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001379 for (ObjCInterfaceDecl::all_protocol_iterator
1380 P = IDecl->all_referenced_protocol_begin(),
1381 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001382 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001383 } else {
1384 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1385 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1386 E = MD->protocol_end(); P != E; ++P)
1387 MatchOneProtocolPropertiesInClass(IDecl, *P);
1388 }
1389}
1390
1391/// isPropertyReadonly - Return true if property is readonly, by searching
1392/// for the property in the class and in its categories and implementations
1393///
1394bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1395 ObjCInterfaceDecl *IDecl) {
1396 // by far the most common case.
1397 if (!PDecl->isReadOnly())
1398 return false;
1399 // Even if property is ready only, if interface has a user defined setter,
1400 // it is not considered read only.
1401 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1402 return false;
1403
1404 // Main class has the property as 'readonly'. Must search
1405 // through the category list to see if the property's
1406 // attribute has been over-ridden to 'readwrite'.
1407 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1408 Category; Category = Category->getNextClassCategory()) {
1409 // Even if property is ready only, if a category has a user defined setter,
1410 // it is not considered read only.
1411 if (Category->getInstanceMethod(PDecl->getSetterName()))
1412 return false;
1413 ObjCPropertyDecl *P =
1414 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1415 if (P && !P->isReadOnly())
1416 return false;
1417 }
1418
1419 // Also, check for definition of a setter method in the implementation if
1420 // all else failed.
1421 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1422 if (ObjCImplementationDecl *IMD =
1423 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1424 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1425 return false;
1426 } else if (ObjCCategoryImplDecl *CIMD =
1427 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1428 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1429 return false;
1430 }
1431 }
1432 // Lastly, look through the implementation (if one is in scope).
1433 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1434 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1435 return false;
1436 // If all fails, look at the super class.
1437 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1438 return isPropertyReadonly(PDecl, SIDecl);
1439 return true;
1440}
1441
1442/// CollectImmediateProperties - This routine collects all properties in
1443/// the class and its conforming protocols; but not those it its super class.
1444void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001445 ObjCContainerDecl::PropertyMap &PropMap,
1446 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001447 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1448 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1449 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001450 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001451 PropMap[Prop->getIdentifier()] = Prop;
1452 }
1453 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001454 for (ObjCInterfaceDecl::all_protocol_iterator
1455 PI = IDecl->all_referenced_protocol_begin(),
1456 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001457 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001458 }
1459 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1460 if (!CATDecl->IsClassExtension())
1461 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1462 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001463 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001464 PropMap[Prop->getIdentifier()] = Prop;
1465 }
1466 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001467 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001468 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001469 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001470 }
1471 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1472 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1473 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001474 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001475 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1476 // Exclude property for protocols which conform to class's super-class,
1477 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001478 if (!PropertyFromSuper ||
1479 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001480 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1481 if (!PropEntry)
1482 PropEntry = Prop;
1483 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001484 }
1485 // scan through protocol's protocols.
1486 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1487 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001488 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001489 }
1490}
1491
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001492/// CollectSuperClassPropertyImplementations - This routine collects list of
1493/// properties to be implemented in super class(s) and also coming from their
1494/// conforming protocols.
1495static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001496 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001497 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1498 while (SDecl) {
Anna Zaksb36ea372012-10-18 19:17:53 +00001499 SDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001500 SDecl = SDecl->getSuperClass();
1501 }
1502 }
1503}
1504
James Dennett699c9042012-06-15 07:13:21 +00001505/// \brief Default synthesizes all properties which must be synthesized
1506/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001507void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1508 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001509
Anna Zaksb36ea372012-10-18 19:17:53 +00001510 ObjCInterfaceDecl::PropertyMap PropMap;
1511 IDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001512 if (PropMap.empty())
1513 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001514 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001515 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1516
Anna Zaksb36ea372012-10-18 19:17:53 +00001517 for (ObjCInterfaceDecl::PropertyMap::iterator
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001518 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1519 ObjCPropertyDecl *Prop = P->second;
1520 // If property to be implemented in the super class, ignore.
1521 if (SuperPropMap[Prop->getIdentifier()])
1522 continue;
Anna Zaksb36ea372012-10-18 19:17:53 +00001523 // Is there a matching property synthesize/dynamic?
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001524 if (Prop->isInvalidDecl() ||
1525 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1526 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1527 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001528 // Property may have been synthesized by user.
1529 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1530 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001531 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1532 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1533 continue;
1534 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1535 continue;
1536 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001537 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1538 // We won't auto-synthesize properties declared in protocols.
1539 Diag(IMPDecl->getLocation(),
1540 diag::warn_auto_synthesizing_protocol_property);
1541 Diag(Prop->getLocation(), diag::note_property_declare);
1542 continue;
1543 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001544
1545 // We use invalid SourceLocations for the synthesized ivars since they
1546 // aren't really synthesized at a particular location; they just exist.
1547 // Saying that they are located at the @implementation isn't really going
1548 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001549 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1550 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1551 true,
1552 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001553 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001554 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001555 if (PIDecl) {
1556 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001557 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001558 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001559 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001560}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001561
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001562void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001563 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001564 return;
1565 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1566 if (!IC)
1567 return;
1568 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001569 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001570 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001571}
1572
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001573void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001574 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001575 const SelectorSet &InsMap) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001576 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1577 ObjCInterfaceDecl *IDecl;
1578 // Gather properties which need not be implemented in this class
1579 // or category.
1580 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1581 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1582 // For categories, no need to implement properties declared in
1583 // its primary class (and its super classes) if property is
1584 // declared in one of those containers.
1585 if ((IDecl = C->getClassInterface()))
1586 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap);
1587 }
1588 if (IDecl)
1589 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001590
Anna Zaksb36ea372012-10-18 19:17:53 +00001591 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001592 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001593 if (PropMap.empty())
1594 return;
1595
1596 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1597 for (ObjCImplDecl::propimpl_iterator
1598 I = IMPDecl->propimpl_begin(),
1599 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001600 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001601
Anna Zaksb36ea372012-10-18 19:17:53 +00001602 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001603 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1604 ObjCPropertyDecl *Prop = P->second;
1605 // Is there a matching propery synthesize/dynamic?
1606 if (Prop->isInvalidDecl() ||
1607 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001608 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001609 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001610 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001611 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001612 isa<ObjCCategoryDecl>(CDecl) ?
1613 diag::warn_setter_getter_impl_required_in_category :
1614 diag::warn_setter_getter_impl_required)
1615 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001616 Diag(Prop->getLocation(),
1617 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001618 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001619 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001620 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001621 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1622
Ted Kremenek9d64c152010-03-12 00:38:38 +00001623 }
1624
1625 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001626 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001627 isa<ObjCCategoryDecl>(CDecl) ?
1628 diag::warn_setter_getter_impl_required_in_category :
1629 diag::warn_setter_getter_impl_required)
1630 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001631 Diag(Prop->getLocation(),
1632 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001633 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001634 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001635 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001636 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001637 }
1638 }
1639}
1640
1641void
1642Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1643 ObjCContainerDecl* IDecl) {
1644 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001645 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001646 return;
1647 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1648 E = IDecl->prop_end();
1649 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001650 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001651 ObjCMethodDecl *GetterMethod = 0;
1652 ObjCMethodDecl *SetterMethod = 0;
1653 bool LookedUpGetterSetter = false;
1654
Bill Wendlingad017fa2012-12-20 19:22:21 +00001655 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001656 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001657
John McCall265941b2011-09-13 18:31:23 +00001658 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1659 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001660 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1661 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1662 LookedUpGetterSetter = true;
1663 if (GetterMethod) {
1664 Diag(GetterMethod->getLocation(),
1665 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001666 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001667 Diag(Property->getLocation(), diag::note_property_declare);
1668 }
1669 if (SetterMethod) {
1670 Diag(SetterMethod->getLocation(),
1671 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001672 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001673 Diag(Property->getLocation(), diag::note_property_declare);
1674 }
1675 }
1676
Ted Kremenek9d64c152010-03-12 00:38:38 +00001677 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001678 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1679 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001680 continue;
1681 if (const ObjCPropertyImplDecl *PIDecl
1682 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1683 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1684 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001685 if (!LookedUpGetterSetter) {
1686 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1687 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1688 LookedUpGetterSetter = true;
1689 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001690 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1691 SourceLocation MethodLoc =
1692 (GetterMethod ? GetterMethod->getLocation()
1693 : SetterMethod->getLocation());
1694 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001695 << Property->getIdentifier() << (GetterMethod != 0)
1696 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001697 // fixit stuff.
1698 if (!AttributesAsWritten) {
1699 if (Property->getLParenLoc().isValid()) {
1700 // @property () ... case.
1701 SourceRange PropSourceRange(Property->getAtLoc(),
1702 Property->getLParenLoc());
1703 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1704 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1705 }
1706 else {
1707 //@property id etc.
1708 SourceLocation endLoc =
1709 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1710 endLoc = endLoc.getLocWithOffset(-1);
1711 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1712 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1713 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1714 }
1715 }
1716 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1717 // @property () ... case.
1718 SourceLocation endLoc = Property->getLParenLoc();
1719 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1720 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1721 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1722 }
1723 else
1724 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001725 Diag(Property->getLocation(), diag::note_property_declare);
1726 }
1727 }
1728 }
1729}
1730
John McCallf85e1932011-06-15 23:02:42 +00001731void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001732 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001733 return;
1734
1735 for (ObjCImplementationDecl::propimpl_iterator
1736 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001737 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001738 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1739 continue;
1740
1741 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001742 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1743 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001744 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1745 if (!method)
1746 continue;
1747 ObjCMethodFamily family = method->getMethodFamily();
1748 if (family == OMF_alloc || family == OMF_copy ||
1749 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001750 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001751 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1752 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001753 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001754 Diag(PD->getLocation(), diag::note_property_declare);
1755 }
1756 }
1757 }
1758}
1759
John McCall5de74d12010-11-10 07:01:40 +00001760/// AddPropertyAttrs - Propagates attributes from a property to the
1761/// implicitly-declared getter or setter for that property.
1762static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1763 ObjCPropertyDecl *Property) {
1764 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001765 for (Decl::attr_iterator A = Property->attr_begin(),
1766 AEnd = Property->attr_end();
1767 A != AEnd; ++A) {
1768 if (isa<DeprecatedAttr>(*A) ||
1769 isa<UnavailableAttr>(*A) ||
1770 isa<AvailabilityAttr>(*A))
1771 PropertyMethod->addAttr((*A)->clone(S.Context));
1772 }
John McCall5de74d12010-11-10 07:01:40 +00001773}
1774
Ted Kremenek9d64c152010-03-12 00:38:38 +00001775/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1776/// have the property type and issue diagnostics if they don't.
1777/// Also synthesize a getter/setter method if none exist (and update the
1778/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1779/// methods is the "right" thing to do.
1780void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001781 ObjCContainerDecl *CD,
1782 ObjCPropertyDecl *redeclaredProperty,
1783 ObjCContainerDecl *lexicalDC) {
1784
Ted Kremenek9d64c152010-03-12 00:38:38 +00001785 ObjCMethodDecl *GetterMethod, *SetterMethod;
1786
1787 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1788 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1789 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1790 property->getLocation());
1791
1792 if (SetterMethod) {
1793 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1794 property->getPropertyAttributes();
1795 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1796 Context.getCanonicalType(SetterMethod->getResultType()) !=
1797 Context.VoidTy)
1798 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1799 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001800 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001801 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1802 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001803 Diag(property->getLocation(),
1804 diag::warn_accessor_property_type_mismatch)
1805 << property->getDeclName()
1806 << SetterMethod->getSelector();
1807 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1808 }
1809 }
1810
1811 // Synthesize getter/setter methods if none exist.
1812 // Find the default getter and if one not found, add one.
1813 // FIXME: The synthesized property we set here is misleading. We almost always
1814 // synthesize these methods unless the user explicitly provided prototypes
1815 // (which is odd, but allowed). Sema should be typechecking that the
1816 // declarations jive in that situation (which it is not currently).
1817 if (!GetterMethod) {
1818 // No instance method of same name as property getter name was found.
1819 // Declare a getter method and add it to the list of methods
1820 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001821 SourceLocation Loc = redeclaredProperty ?
1822 redeclaredProperty->getLocation() :
1823 property->getLocation();
1824
1825 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1826 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001827 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001828 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001829 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001830 (property->getPropertyImplementation() ==
1831 ObjCPropertyDecl::Optional) ?
1832 ObjCMethodDecl::Optional :
1833 ObjCMethodDecl::Required);
1834 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001835
1836 AddPropertyAttrs(*this, GetterMethod, property);
1837
Ted Kremenek23173d72010-05-18 21:09:07 +00001838 // FIXME: Eventually this shouldn't be needed, as the lexical context
1839 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001840 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001841 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001842 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1843 GetterMethod->addAttr(
1844 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001845 } else
1846 // A user declared getter will be synthesize when @synthesize of
1847 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001848 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001849 property->setGetterMethodDecl(GetterMethod);
1850
1851 // Skip setter if property is read-only.
1852 if (!property->isReadOnly()) {
1853 // Find the default setter and if one not found, add one.
1854 if (!SetterMethod) {
1855 // No instance method of same name as property setter name was found.
1856 // Declare a setter method and add it to the list of methods
1857 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001858 SourceLocation Loc = redeclaredProperty ?
1859 redeclaredProperty->getLocation() :
1860 property->getLocation();
1861
1862 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001863 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001864 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001865 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001866 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001867 /*isImplicitlyDeclared=*/true,
1868 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001869 (property->getPropertyImplementation() ==
1870 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001871 ObjCMethodDecl::Optional :
1872 ObjCMethodDecl::Required);
1873
Ted Kremenek9d64c152010-03-12 00:38:38 +00001874 // Invent the arguments for the setter. We don't bother making a
1875 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001876 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1877 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001878 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001879 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001880 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001881 SC_None,
1882 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001883 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001884 SetterMethod->setMethodParams(Context, Argument,
1885 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001886
1887 AddPropertyAttrs(*this, SetterMethod, property);
1888
Ted Kremenek9d64c152010-03-12 00:38:38 +00001889 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001890 // FIXME: Eventually this shouldn't be needed, as the lexical context
1891 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001892 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001893 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001894 } else
1895 // A user declared setter will be synthesize when @synthesize of
1896 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001897 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001898 property->setSetterMethodDecl(SetterMethod);
1899 }
1900 // Add any synthesized methods to the global pool. This allows us to
1901 // handle the following, which is supported by GCC (and part of the design).
1902 //
1903 // @interface Foo
1904 // @property double bar;
1905 // @end
1906 //
1907 // void thisIsUnfortunate() {
1908 // id foo;
1909 // double bar = [foo bar];
1910 // }
1911 //
1912 if (GetterMethod)
1913 AddInstanceMethodToGlobalPool(GetterMethod);
1914 if (SetterMethod)
1915 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001916
1917 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1918 if (!CurrentClass) {
1919 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1920 CurrentClass = Cat->getClassInterface();
1921 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1922 CurrentClass = Impl->getClassInterface();
1923 }
1924 if (GetterMethod)
1925 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1926 if (SetterMethod)
1927 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001928}
1929
John McCalld226f652010-08-21 09:40:31 +00001930void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001931 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001932 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001933 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001934 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001935 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001936 return;
1937
1938 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001939 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001940
David Blaikie4e4d0842012-03-11 07:00:24 +00001941 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00001942 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001943 PropertyTy->isObjCRetainableType()) {
1944 // 'readonly' property with no obvious lifetime.
1945 // its life time will be determined by its backing ivar.
1946 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1947 ObjCDeclSpec::DQ_PR_copy |
1948 ObjCDeclSpec::DQ_PR_retain |
1949 ObjCDeclSpec::DQ_PR_strong |
1950 ObjCDeclSpec::DQ_PR_weak |
1951 ObjCDeclSpec::DQ_PR_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00001952 if ((Attributes & rel) == 0)
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001953 return;
1954 }
1955
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001956 if (propertyInPrimaryClass) {
1957 // we postpone most property diagnosis until class's implementation
1958 // because, its readonly attribute may be overridden in its class
1959 // extensions making other attributes, which make no sense, to make sense.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001960 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1961 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001962 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1963 << "readonly" << "readwrite";
1964 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001965 // readonly and readwrite/assign/retain/copy conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001966 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1967 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001968 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001969 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001970 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001971 ObjCDeclSpec::DQ_PR_retain |
1972 ObjCDeclSpec::DQ_PR_strong))) {
Bill Wendlingad017fa2012-12-20 19:22:21 +00001973 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001974 "readwrite" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001975 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001976 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001977 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
John McCallf85e1932011-06-15 23:02:42 +00001978 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001979 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001980 "copy" : "retain";
1981
Bill Wendlingad017fa2012-12-20 19:22:21 +00001982 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001983 diag::err_objc_property_attr_mutually_exclusive :
1984 diag::warn_objc_property_attr_mutually_exclusive)
1985 << "readonly" << which;
1986 }
1987
1988 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001989 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001990 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1991 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001992 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001993 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00001994 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1995 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1996 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001997 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00001998 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001999 }
2000
2001 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002002 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2003 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002004 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2005 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002006 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002007 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002008 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002009 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2010 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002011 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002012 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002013 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002014 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2015 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002016 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002017 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002018 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002019 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002020 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2021 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002022 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002023 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002024 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2025 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002026 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2027 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002028 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002029 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002030 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002031 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2032 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002033 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002034 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002035 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002036 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2037 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002038 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002039 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002040 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002041 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002042 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2043 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002044 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002045 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002046 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2047 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002048 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2049 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002050 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002051 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002052 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002053 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2054 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002055 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002056 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002057 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002058 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2059 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002060 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002061 }
2062 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002063 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2064 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002065 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2066 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002067 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002068 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002069 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2070 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002071 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2072 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002073 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002074 }
2075
Bill Wendlingad017fa2012-12-20 19:22:21 +00002076 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2077 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002078 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2079 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002080 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002081 }
2082
Ted Kremenek9d64c152010-03-12 00:38:38 +00002083 // Warn if user supplied no assignment attribute, property is
2084 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002085 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002086 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2087 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2088 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002089 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002090 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002091 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002092 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002093 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002094 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002095 bool isAnyClassTy =
2096 (PropertyTy->isObjCClassType() ||
2097 PropertyTy->isObjCQualifiedClassType());
2098 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2099 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002100 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002101 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002102 else if (propertyInPrimaryClass) {
2103 // Don't issue warning on property with no life time in class
2104 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002105 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002106 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002107 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002108
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002109 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002110 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002111 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002112 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002113 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002114
2115 // FIXME: Implement warning dependent on NSCopying being
2116 // implemented. See also:
2117 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2118 // (please trim this list while you are at it).
2119 }
2120
Bill Wendlingad017fa2012-12-20 19:22:21 +00002121 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2122 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002123 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002124 && PropertyTy->isBlockPointerType())
2125 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002126 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2127 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2128 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002129 PropertyTy->isBlockPointerType())
2130 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002131
Bill Wendlingad017fa2012-12-20 19:22:21 +00002132 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2133 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002134 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2135
Ted Kremenek9d64c152010-03-12 00:38:38 +00002136}